#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: lock_proc.c,v 1.10 2012/06/18 20:30:32 tron Exp $");
#endif
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <netconfig.h>
#include <rpc/rpc.h>
#include <rpcsvc/sm_inter.h>
#include "lockd.h"
#include <rpcsvc/nlm_prot.h>
#include "lockd_lock.h"
#define CLIENT_CACHE_SIZE 64
#define CLIENT_CACHE_LIFETIME 120
static void log_from_addr(const char *, struct svc_req *);
static int addrcmp(const struct sockaddr *, const struct sockaddr *);
static void nlmtonlm4(struct nlm_lock *, struct nlm4_lock *);
static void
log_from_addr(const char *fun_name, struct svc_req *req)
{
struct sockaddr *addr;
char hostname_buf[NI_MAXHOST];
addr = svc_getrpccaller(req->rq_xprt)->buf;
if (getnameinfo(addr, (socklen_t)addr->sa_len, hostname_buf,
sizeof(hostname_buf), NULL, 0, 0) != 0)
return;
syslog(LOG_DEBUG, "%s from %s", fun_name, hostname_buf);
}
static CLIENT *clnt_cache_ptr[CLIENT_CACHE_SIZE];
static long clnt_cache_time[CLIENT_CACHE_SIZE];
static struct sockaddr_storage clnt_cache_addr[CLIENT_CACHE_SIZE];
static int clnt_cache_next_to_use = 0;
static int
addrcmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
{
size_t len;
const void *p1, *p2;
if (sa1->sa_family != sa2->sa_family)
return -1;
switch (sa1->sa_family) {
case AF_INET:
p1 = &((const struct sockaddr_in *)(const void *)sa1)->sin_addr;
p2 = &((const struct sockaddr_in *)(const void *)sa2)->sin_addr;
len = 4;
break;
case AF_INET6:
p1 = &((const struct sockaddr_in6 *)(const void *)sa1)->sin6_addr;
p2 = &((const struct sockaddr_in6 *)(const void *)sa2)->sin6_addr;
len = 16;
break;
default:
return -1;
}
return memcmp(p1, p2, len);
}
CLIENT *
get_client(struct sockaddr *host_addr, rpcvers_t vers)
{
CLIENT *client;
struct timeval retry_time, time_now;
int i;
const char *netid;
struct netconfig *nconf;
char host[NI_MAXHOST];
int error;
(void)gettimeofday(&time_now, NULL);
for (i = 0; i < CLIENT_CACHE_SIZE; i++) {
client = clnt_cache_ptr[i];
if (client && ((clnt_cache_time[i] + CLIENT_CACHE_LIFETIME)
< time_now.tv_sec)) {
if (debug_level > 3)
syslog(LOG_DEBUG, "Expired CLIENT* in cache");
clnt_cache_time[i] = 0L;
clnt_destroy(client);
clnt_cache_ptr[i] = NULL;
client = NULL;
}
if (client && !addrcmp((const struct sockaddr *)(const void *)
&clnt_cache_addr[i], host_addr)) {
if (debug_level > 3)
syslog(LOG_DEBUG, "Found CLIENT* in cache");
return client;
}
}
if (clnt_cache_ptr[clnt_cache_next_to_use]) {
clnt_destroy(clnt_cache_ptr[clnt_cache_next_to_use]);
clnt_cache_ptr[clnt_cache_next_to_use] = NULL;
}
error = getnameinfo(host_addr, (socklen_t)host_addr->sa_len, host,
sizeof(host), NULL, 0, NI_NUMERICHOST);
if (error != 0) {
syslog(LOG_ERR, "unable to get name string for caller: %s",
gai_strerror(error));
return NULL;
}
#if 1
if (host_addr->sa_family == AF_INET6)
netid = "udp6";
else
netid = "udp";
#else
if (host_addr->sa_family == AF_INET6)
netid = "tcp6";
else
netid = "tcp";
#endif
nconf = getnetconfigent(netid);
if (nconf == NULL) {
syslog(LOG_ERR, "could not get netconfig info for '%s': "
"no /etc/netconfig file?", netid);
return NULL;
}
client = clnt_tp_create(host, NLM_PROG, vers, nconf);
freenetconfigent(nconf);
if (!client) {
syslog(LOG_ERR, "%s", clnt_spcreateerror("clntudp_create"));
syslog(LOG_ERR, "Unable to return result to %s", host);
return NULL;
}
clnt_cache_ptr[clnt_cache_next_to_use] = client;
(void)memcpy(&clnt_cache_addr[clnt_cache_next_to_use], host_addr,
(size_t)host_addr->sa_len);
clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec;
if (++clnt_cache_next_to_use >= CLIENT_CACHE_SIZE)
clnt_cache_next_to_use = 0;
retry_time.tv_sec = -1;
retry_time.tv_usec = -1;
clnt_control(client, CLSET_TIMEOUT, (char *)(void *)&retry_time);
if (debug_level > 3)
syslog(LOG_DEBUG, "Created CLIENT* for %s", host);
return client;
}
void
transmit_result(int opcode, nlm_res *result, struct sockaddr *addr)
{
static char dummy;
CLIENT *cli;
struct timeval timeo;
int success;
if ((cli = get_client(addr, NLM_VERS)) != NULL) {
timeo.tv_sec = 0;
timeo.tv_usec = 0;
success = clnt_call(cli, (rpcproc_t)opcode, xdr_nlm_res,
result, xdr_void, &dummy, timeo);
if (debug_level > 2)
syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
success, clnt_sperrno(success));
}
}
void
transmit4_result(int opcode, nlm4_res *result, struct sockaddr *addr)
{
static char dummy;
CLIENT *cli;
struct timeval timeo;
int success;
if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
timeo.tv_sec = 0;
timeo.tv_usec = 0;
success = clnt_call(cli, (rpcproc_t)opcode, xdr_nlm4_res,
result, xdr_void, &dummy, timeo);
if (debug_level > 2)
syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
success, clnt_sperrno(success));
}
}
static void
nlmtonlm4(struct nlm_lock *arg, struct nlm4_lock *arg4)
{
(void)memcpy(arg4, arg, sizeof(nlm_lock));
arg4->l_offset = arg->l_offset;
arg4->l_len = arg->l_len;
}
nlm_testres *
nlm_test_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
{
static nlm_testres result;
struct nlm4_lock arg4;
struct nlm4_holder *holder;
nlmtonlm4(&arg->alock, &arg4);
if (debug_level)
log_from_addr("nlm_test", rqstp);
holder = testlock(&arg4, 0);
result.cookie = arg->cookie;
if (holder == NULL) {
result.stat.stat = nlm_granted;
} else {
result.stat.stat = nlm_denied;
(void)memcpy(&result.stat.nlm_testrply_u.holder, holder,
sizeof(struct nlm_holder));
result.stat.nlm_testrply_u.holder.l_offset =
(unsigned int)holder->l_offset;
result.stat.nlm_testrply_u.holder.l_len =
(unsigned int)holder->l_len;
}
return &result;
}
void *
nlm_test_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
{
nlm_testres result;
static char dummy;
struct sockaddr *addr;
CLIENT *cli;
int success;
struct timeval timeo;
struct nlm4_lock arg4;
struct nlm4_holder *holder;
nlmtonlm4(&arg->alock, &arg4);
if (debug_level)
log_from_addr("nlm_test_msg", rqstp);
holder = testlock(&arg4, 0);
result.cookie = arg->cookie;
if (holder == NULL) {
result.stat.stat = nlm_granted;
} else {
result.stat.stat = nlm_denied;
(void)memcpy(&result.stat.nlm_testrply_u.holder, holder,
sizeof(struct nlm_holder));
result.stat.nlm_testrply_u.holder.l_offset =
(unsigned int)holder->l_offset;
result.stat.nlm_testrply_u.holder.l_len =
(unsigned int)holder->l_len;
}
addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
if ((cli = get_client(addr, NLM_VERS)) != NULL) {
timeo.tv_sec = 0;
timeo.tv_usec = 0;
success = clnt_call(cli, NLM_TEST_RES, xdr_nlm_testres,
&result, xdr_void, &dummy, timeo);
if (debug_level > 2)
syslog(LOG_DEBUG, "clnt_call returns %d", success);
}
return NULL;
}
nlm_res *
nlm_lock_1_svc(nlm_lockargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
struct nlm4_lockargs arg4;
nlmtonlm4(&arg->alock, &arg4.alock);
arg4.cookie = arg->cookie;
arg4.block = arg->block;
arg4.exclusive = arg->exclusive;
arg4.reclaim = arg->reclaim;
arg4.state = arg->state;
if (debug_level)
log_from_addr("nlm_lock", rqstp);
result.cookie = arg->cookie;
result.stat.stat = getlock(&arg4, rqstp, LOCK_MON);
return &result;
}
void *
nlm_lock_msg_1_svc(nlm_lockargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
struct nlm4_lockargs arg4;
nlmtonlm4(&arg->alock, &arg4.alock);
arg4.cookie = arg->cookie;
arg4.block = arg->block;
arg4.exclusive = arg->exclusive;
arg4.reclaim = arg->reclaim;
arg4.state = arg->state;
if (debug_level)
log_from_addr("nlm_lock_msg", rqstp);
result.cookie = arg->cookie;
result.stat.stat = getlock(&arg4, rqstp, LOCK_ASYNC | LOCK_MON);
transmit_result(NLM_LOCK_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
nlm_res *
nlm_cancel_1_svc(nlm_cancargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
struct nlm4_lock arg4;
nlmtonlm4(&arg->alock, &arg4);
if (debug_level)
log_from_addr("nlm_cancel", rqstp);
result.cookie = arg->cookie;
result.stat.stat = unlock(&arg4, LOCK_CANCEL);
return &result;
}
void *
nlm_cancel_msg_1_svc(nlm_cancargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
struct nlm4_lock arg4;
nlmtonlm4(&arg->alock, &arg4);
if (debug_level)
log_from_addr("nlm_cancel_msg", rqstp);
result.cookie = arg->cookie;
result.stat.stat = unlock(&arg4, LOCK_CANCEL);
transmit_result(NLM_CANCEL_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
nlm_res *
nlm_unlock_1_svc(nlm_unlockargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
struct nlm4_lock arg4;
nlmtonlm4(&arg->alock, &arg4);
if (debug_level)
log_from_addr("nlm_unlock", rqstp);
result.stat.stat = unlock(&arg4, 0);
result.cookie = arg->cookie;
return &result;
}
void *
nlm_unlock_msg_1_svc(nlm_unlockargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
struct nlm4_lock arg4;
nlmtonlm4(&arg->alock, &arg4);
if (debug_level)
log_from_addr("nlm_unlock_msg", rqstp);
result.stat.stat = unlock(&arg4, 0);
result.cookie = arg->cookie;
transmit_result(NLM_UNLOCK_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
nlm_res *
nlm_granted_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
if (debug_level)
log_from_addr("nlm_granted", rqstp);
result.cookie = arg->cookie;
result.stat.stat = nlm_granted;
return &result;
}
void *
nlm_granted_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
if (debug_level)
log_from_addr("nlm_granted_msg", rqstp);
result.cookie = arg->cookie;
result.stat.stat = nlm_granted;
transmit_result(NLM_GRANTED_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
void *
nlm_test_res_1_svc(nlm_testres *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm_test_res", rqstp);
return NULL;
}
void *
nlm_lock_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm_lock_res", rqstp);
return NULL;
}
void *
nlm_cancel_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm_cancel_res", rqstp);
return NULL;
}
void *
nlm_unlock_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm_unlock_res", rqstp);
return NULL;
}
void *
nlm_granted_res_1_svc(nlm_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm_granted_res", rqstp);
return NULL;
}
nlm_shareres *
nlm_share_3_svc(nlm_shareargs *arg, struct svc_req *rqstp)
{
static nlm_shareres result;
if (debug_level)
log_from_addr("nlm_share", rqstp);
result.cookie = arg->cookie;
result.stat = nlm_granted;
result.sequence = 1234356;
return &result;
}
nlm_shareres *
nlm_unshare_3_svc(nlm_shareargs *arg, struct svc_req *rqstp)
{
static nlm_shareres result;
if (debug_level)
log_from_addr("nlm_unshare", rqstp);
result.cookie = arg->cookie;
result.stat = nlm_granted;
result.sequence = 1234356;
return &result;
}
nlm_res *
nlm_nm_lock_3_svc(nlm_lockargs *arg, struct svc_req *rqstp)
{
static nlm_res result;
if (debug_level)
log_from_addr("nlm_nm_lock", rqstp);
result.cookie = arg->cookie;
result.stat.stat = nlm_granted;
return &result;
}
void *
nlm_free_all_3_svc(nlm_notify *arg, struct svc_req *rqstp)
{
static char dummy;
if (debug_level)
log_from_addr("nlm_free_all", rqstp);
return &dummy;
}
nlm4_testres *
nlm4_test_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
{
static nlm4_testres result;
struct nlm4_holder *holder;
if (debug_level)
log_from_addr("nlm4_test", rqstp);
holder = testlock(&arg->alock, LOCK_V4);
result.cookie = arg->cookie;
if (holder == NULL) {
result.stat.stat = nlm4_granted;
} else {
result.stat.stat = nlm4_denied;
(void)memcpy(&result.stat.nlm4_testrply_u.holder, holder,
sizeof(struct nlm4_holder));
}
return &result;
}
void *
nlm4_test_msg_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
{
nlm4_testres result;
static char dummy;
struct sockaddr *addr;
CLIENT *cli;
int success;
struct timeval timeo;
struct nlm4_holder *holder;
if (debug_level)
log_from_addr("nlm4_test_msg", rqstp);
holder = testlock(&arg->alock, LOCK_V4);
result.cookie = arg->cookie;
if (holder == NULL) {
result.stat.stat = nlm4_granted;
} else {
result.stat.stat = nlm4_denied;
(void)memcpy(&result.stat.nlm4_testrply_u.holder, holder,
sizeof(struct nlm4_holder));
}
addr = svc_getrpccaller(rqstp->rq_xprt)->buf;
if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
timeo.tv_sec = 0;
timeo.tv_usec = 0;
success = clnt_call(cli, NLM4_TEST_RES, xdr_nlm4_testres,
&result, xdr_void, &dummy, timeo);
if (debug_level > 2)
syslog(LOG_DEBUG, "clnt_call returns %d", success);
}
return NULL;
}
nlm4_res *
nlm4_lock_4_svc(nlm4_lockargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_lock", rqstp);
result.cookie = arg->cookie;
result.stat.stat = (enum nlm4_stats)getlock(arg, rqstp,
LOCK_MON | LOCK_V4);
return &result;
}
void *
nlm4_lock_msg_4_svc(nlm4_lockargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_lock_msg", rqstp);
result.cookie = arg->cookie;
result.stat.stat = (enum nlm4_stats)getlock(arg, rqstp,
LOCK_MON | LOCK_ASYNC | LOCK_V4);
transmit4_result(NLM4_LOCK_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
nlm4_res *
nlm4_cancel_4_svc(nlm4_cancargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_cancel", rqstp);
result.cookie = arg->cookie;
result.stat.stat = (enum nlm4_stats)unlock(&arg->alock, LOCK_CANCEL);
return &result;
}
void *
nlm4_cancel_msg_4_svc(nlm4_cancargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_cancel_msg", rqstp);
result.cookie = arg->cookie;
result.stat.stat = (enum nlm4_stats)unlock(&arg->alock,
LOCK_CANCEL | LOCK_V4);
transmit4_result(NLM4_CANCEL_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
nlm4_res *
nlm4_unlock_4_svc(nlm4_unlockargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_unlock", rqstp);
result.stat.stat = (enum nlm4_stats)unlock(&arg->alock, LOCK_V4);
result.cookie = arg->cookie;
return &result;
}
void *
nlm4_unlock_msg_4_svc(nlm4_unlockargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_unlock_msg", rqstp);
result.stat.stat = (enum nlm4_stats)unlock(&arg->alock, LOCK_V4);
result.cookie = arg->cookie;
transmit4_result(NLM4_UNLOCK_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
nlm4_res *
nlm4_granted_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_granted", rqstp);
result.cookie = arg->cookie;
result.stat.stat = nlm4_granted;
return &result;
}
void *
nlm4_granted_msg_4_svc(nlm4_testargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_granted_msg", rqstp);
result.cookie = arg->cookie;
result.stat.stat = nlm4_granted;
transmit4_result(NLM4_GRANTED_RES, &result,
(struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf);
return NULL;
}
void *
nlm4_test_res_4_svc(nlm4_testres *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm4_test_res", rqstp);
return NULL;
}
void *
nlm4_lock_res_4_svc(nlm4_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm4_lock_res", rqstp);
return NULL;
}
void *
nlm4_cancel_res_4_svc(nlm4_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm4_cancel_res", rqstp);
return NULL;
}
void *
nlm4_unlock_res_4_svc(nlm4_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm4_unlock_res", rqstp);
return NULL;
}
void *
nlm4_granted_res_4_svc(nlm4_res *arg, struct svc_req *rqstp)
{
if (debug_level)
log_from_addr("nlm4_granted_res", rqstp);
return NULL;
}
nlm4_shareres *
nlm4_share_4_svc(nlm4_shareargs *arg, struct svc_req *rqstp)
{
static nlm4_shareres result;
if (debug_level)
log_from_addr("nlm4_share", rqstp);
result.cookie = arg->cookie;
result.stat = nlm4_granted;
result.sequence = 1234356;
return &result;
}
nlm4_shareres *
nlm4_unshare_4_svc(nlm4_shareargs *arg, struct svc_req *rqstp)
{
static nlm4_shareres result;
if (debug_level)
log_from_addr("nlm_unshare", rqstp);
result.cookie = arg->cookie;
result.stat = nlm4_granted;
result.sequence = 1234356;
return &result;
}
nlm4_res *
nlm4_nm_lock_4_svc(nlm4_lockargs *arg, struct svc_req *rqstp)
{
static nlm4_res result;
if (debug_level)
log_from_addr("nlm4_nm_lock", rqstp);
result.cookie = arg->cookie;
result.stat.stat = nlm4_granted;
return &result;
}
void *
nlm4_free_all_4_svc(nlm_notify *arg, struct svc_req *rqstp)
{
static char dummy;
if (debug_level)
log_from_addr("nlm4_free_all", rqstp);
return &dummy;
}
void *
nlm_sm_notify_0_svc(struct nlm_sm_status *arg, struct svc_req *rqstp)
{
static char dummy;
notify(arg->mon_name, arg->state);
return &dummy;
}