1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */
6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the FreeBSD project
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
43 * main() function for NFS lock daemon. Most of the code in this
44 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
46 * The actual program logic is in the file lock_proc.c
49 #include <sys/param.h>
50 #include <sys/linker.h>
51 #include <sys/module.h>
52 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
67 #include <netconfig.h>
71 #include <rpc/rpc_com.h>
72 #include <rpcsvc/sm_inter.h>
75 #include <rpcsvc/nlm_prot.h>
77 int debug_level = 0; /* 0 = no debugging syslog() calls */
83 int kernel_lockd_client;
86 char **hosts, *svcport_str = NULL;
89 char **addrs; /* actually (netid, uaddr) pairs */
90 int naddrs; /* count of how many (netid, uaddr) pairs */
91 char localhost[] = "localhost";
93 void create_service(struct netconfig *nconf);
94 void lookup_addresses(struct netconfig *nconf);
96 void nlm_prog_0(struct svc_req *, SVCXPRT *);
97 void nlm_prog_1(struct svc_req *, SVCXPRT *);
98 void nlm_prog_3(struct svc_req *, SVCXPRT *);
99 void nlm_prog_4(struct svc_req *, SVCXPRT *);
100 void out_of_mem(void);
103 void sigalarm_handler(void);
106 * XXX move to some header file.
108 #define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
111 main(int argc, char **argv)
115 char *endptr, **hosts_bak;
116 struct sigaction sigalarm;
117 int grace_period = 30;
118 struct netconfig *nconf;
120 int maxrec = RPC_MAXDATASIZE;
121 in_port_t svcport = 0;
123 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
126 debug_level = atoi(optarg);
133 grace_period = atoi(optarg);
142 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
143 if (hosts_bak == NULL) {
145 for (i = 0; i < nhosts; i++)
152 hosts[nhosts - 1] = strdup(optarg);
153 if (hosts[nhosts - 1] == NULL) {
154 for (i = 0; i < (nhosts - 1); i++)
162 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
163 if (endptr == NULL || *endptr != '\0' ||
164 svcport == 0 || svcport >= IPPORT_MAX)
166 svcport_str = strdup(optarg);
174 if (geteuid()) { /* This command allowed only to root */
175 fprintf(stderr, "Sorry. You are not superuser\n");
179 kernel_lockd = FALSE;
180 kernel_lockd_client = FALSE;
181 if (modfind("nfslockd") < 0) {
182 if (kldload("nfslockd") < 0) {
183 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
191 if (getosreldate() >= 800040)
192 kernel_lockd_client = TRUE;
195 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
196 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
197 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
198 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
201 * Check if IPv6 support is present.
203 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
209 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
212 * If no hosts were specified, add a wildcard entry to bind to
213 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
217 hosts = malloc(sizeof(char**));
226 hosts_bak = realloc(hosts, (nhosts + 2) *
228 if (hosts_bak == NULL) {
229 for (i = 0; i < nhosts; i++)
237 hosts[nhosts - 2] = "::1";
239 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
240 if (hosts_bak == NULL) {
241 for (i = 0; i < nhosts; i++)
251 hosts[nhosts - 1] = "127.0.0.1";
255 if (!kernel_lockd_client) {
257 * For the case where we have a kernel lockd but it
258 * doesn't provide client locking, we run a cut-down
259 * RPC service on a local-domain socket. The kernel's
260 * RPC server will pass what it can't handle (mainly
261 * client replies) down to us.
263 struct sockaddr_un sun;
267 memset(&sun, 0, sizeof sun);
268 sun.sun_family = AF_LOCAL;
269 unlink(_PATH_RPCLOCKDSOCK);
270 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
271 sun.sun_len = SUN_LEN(&sun);
272 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
274 err(1, "Can't create local lockd socket");
276 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
277 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
278 err(1, "Can't bind local lockd socket");
281 if (listen(fd, SOMAXCONN) < 0) {
282 err(1, "Can't listen on local lockd socket");
284 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
286 err(1, "Can't create transport for local lockd socket");
288 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
289 err(1, "Can't register service for local lockd socket");
294 * We need to look up the addresses so that we can
295 * hand uaddrs (ascii encoded address+port strings) to
298 nc_handle = setnetconfig();
299 while ((nconf = getnetconfig(nc_handle))) {
300 /* We want to listen only on udp6, tcp6, udp, tcp transports */
301 if (nconf->nc_flag & NC_VISIBLE) {
302 /* Skip if there's no IPv6 support */
303 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
306 lookup_addresses(nconf);
310 endnetconfig(nc_handle);
312 nc_handle = setnetconfig();
313 while ((nconf = getnetconfig(nc_handle))) {
314 /* We want to listen only on udp6, tcp6, udp, tcp transports */
315 if (nconf->nc_flag & NC_VISIBLE) {
316 /* Skip if there's no IPv6 support */
317 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
320 create_service(nconf);
324 endnetconfig(nc_handle);
328 * Note that it is NOT sensible to run this program from inetd - the
329 * protocol assumes that it will run immediately at boot time.
331 if (daemon(0, debug_level > 0)) {
332 err(1, "cannot fork");
336 openlog("rpc.lockd", 0, LOG_DAEMON);
338 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
340 syslog(LOG_INFO, "Starting");
342 sigalarm.sa_handler = (sig_t) sigalarm_handler;
343 sigemptyset(&sigalarm.sa_mask);
344 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
345 sigalarm.sa_flags |= SA_RESTART;
346 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
347 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
353 if (!kernel_lockd_client) {
355 client_pid = client_request();
358 * Create a child process to enter the kernel and then
359 * wait for RPCs on our local domain socket.
362 nlm_syscall(debug_level, grace_period,
368 * The kernel lockd implementation provides
369 * both client and server so we don't need to
372 nlm_syscall(debug_level, grace_period, naddrs, addrs);
380 client_pid = client_request();
382 svc_run(); /* Should never return */
388 * This routine creates and binds sockets on the appropriate
389 * addresses. It gets called one time for each transport and
390 * registrates the service with rpcbind on that trasport.
393 create_service(struct netconfig *nconf)
395 struct addrinfo hints, *res = NULL;
396 struct sockaddr_in *sin;
397 struct sockaddr_in6 *sin6;
398 struct __rpc_sockinfo si;
399 struct netbuf servaddr;
400 SVCXPRT *transp = NULL;
406 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
408 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
409 (nconf->nc_semantics != NC_TPI_COTS) &&
410 (nconf->nc_semantics != NC_TPI_COTS_ORD))
411 return; /* not my type */
414 * XXX - using RPC library internal functions.
416 if (!__rpc_nconf2sockinfo(nconf, &si)) {
417 syslog(LOG_ERR, "cannot get information for %s",
422 /* Get rpc.statd's address on this transport */
423 memset(&hints, 0, sizeof hints);
424 hints.ai_flags = AI_PASSIVE;
425 hints.ai_family = si.si_af;
426 hints.ai_socktype = si.si_socktype;
427 hints.ai_protocol = si.si_proto;
430 * Bind to specific IPs if asked to
433 while (nhostsbak > 0) {
437 * XXX - using RPC library internal functions.
439 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
440 syslog(LOG_ERR, "cannot create socket for %s",
445 switch (hints.ai_family) {
447 if (inet_pton(AF_INET, hosts[nhostsbak],
449 hints.ai_flags &= AI_NUMERICHOST;
452 * Skip if we have an AF_INET6 address.
454 if (inet_pton(AF_INET6, hosts[nhostsbak],
462 if (inet_pton(AF_INET6, hosts[nhostsbak],
464 hints.ai_flags &= AI_NUMERICHOST;
467 * Skip if we have an AF_INET address.
469 if (inet_pton(AF_INET, hosts[nhostsbak],
481 * If no hosts were specified, just bind to INADDR_ANY
483 if (strcmp("*", hosts[nhostsbak]) == 0) {
484 if (svcport_str == NULL) {
485 res = malloc(sizeof(struct addrinfo));
488 res->ai_flags = hints.ai_flags;
489 res->ai_family = hints.ai_family;
490 res->ai_protocol = hints.ai_protocol;
491 switch (res->ai_family) {
493 sin = malloc(sizeof(struct sockaddr_in));
496 sin->sin_family = AF_INET;
497 sin->sin_port = htons(0);
498 sin->sin_addr.s_addr = htonl(INADDR_ANY);
499 res->ai_addr = (struct sockaddr*) sin;
500 res->ai_addrlen = (socklen_t)
501 sizeof(res->ai_addr);
504 sin6 = malloc(sizeof(struct sockaddr_in6));
507 sin6->sin6_family = AF_INET6;
508 sin6->sin6_port = htons(0);
509 sin6->sin6_addr = in6addr_any;
510 res->ai_addr = (struct sockaddr*) sin6;
511 res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
517 if ((aicode = getaddrinfo(NULL, svcport_str,
518 &hints, &res)) != 0) {
520 "cannot get local address for %s: %s",
522 gai_strerror(aicode));
527 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
528 &hints, &res)) != 0) {
530 "cannot get local address for %s: %s",
531 nconf->nc_netid, gai_strerror(aicode));
536 r = bindresvport_sa(fd, res->ai_addr);
538 syslog(LOG_ERR, "bindresvport_sa: %m");
542 if (nconf->nc_semantics != NC_TPI_CLTS)
543 listen(fd, SOMAXCONN);
545 transp = svc_tli_create(fd, nconf, NULL,
546 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
548 if (transp != (SVCXPRT *) NULL) {
549 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
552 "can't register %s NLM_PROG, NLM_SM service",
555 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
558 "can't register %s NLM_PROG, NLM_VERS service",
561 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
564 "can't register %s NLM_PROG, NLM_VERSX service",
567 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
570 "can't register %s NLM_PROG, NLM_VERS4 service",
574 syslog(LOG_WARNING, "can't create %s services",
577 if (registered == 0) {
579 memset(&hints, 0, sizeof hints);
580 hints.ai_flags = AI_PASSIVE;
581 hints.ai_family = si.si_af;
582 hints.ai_socktype = si.si_socktype;
583 hints.ai_protocol = si.si_proto;
585 if (svcport_str == NULL) {
586 svcport_str = malloc(NI_MAXSERV * sizeof(char));
587 if (svcport_str == NULL)
590 if (getnameinfo(res->ai_addr,
591 res->ai_addr->sa_len, NULL, NI_MAXHOST,
592 svcport_str, NI_MAXSERV * sizeof(char),
593 NI_NUMERICHOST | NI_NUMERICSERV))
594 errx(1, "Cannot get port number");
597 if((aicode = getaddrinfo(NULL, svcport_str, &hints,
599 syslog(LOG_ERR, "cannot get local address: %s",
600 gai_strerror(aicode));
604 servaddr.buf = malloc(res->ai_addrlen);
605 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
606 servaddr.len = res->ai_addrlen;
608 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
609 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
610 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
611 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
620 * Look up addresses for the kernel to create transports for.
623 lookup_addresses(struct netconfig *nconf)
625 struct addrinfo hints, *res = NULL;
626 struct sockaddr_in *sin;
627 struct sockaddr_in6 *sin6;
628 struct __rpc_sockinfo si;
629 struct netbuf servaddr;
632 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
635 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
636 (nconf->nc_semantics != NC_TPI_COTS) &&
637 (nconf->nc_semantics != NC_TPI_COTS_ORD))
638 return; /* not my type */
641 * XXX - using RPC library internal functions.
643 if (!__rpc_nconf2sockinfo(nconf, &si)) {
644 syslog(LOG_ERR, "cannot get information for %s",
649 /* Get rpc.statd's address on this transport */
650 memset(&hints, 0, sizeof hints);
651 hints.ai_flags = AI_PASSIVE;
652 hints.ai_family = si.si_af;
653 hints.ai_socktype = si.si_socktype;
654 hints.ai_protocol = si.si_proto;
657 * Bind to specific IPs if asked to
660 while (nhostsbak > 0) {
663 switch (hints.ai_family) {
665 if (inet_pton(AF_INET, hosts[nhostsbak],
667 hints.ai_flags &= AI_NUMERICHOST;
670 * Skip if we have an AF_INET6 address.
672 if (inet_pton(AF_INET6, hosts[nhostsbak],
679 if (inet_pton(AF_INET6, hosts[nhostsbak],
681 hints.ai_flags &= AI_NUMERICHOST;
684 * Skip if we have an AF_INET address.
686 if (inet_pton(AF_INET, hosts[nhostsbak],
697 * If no hosts were specified, just bind to INADDR_ANY
699 if (strcmp("*", hosts[nhostsbak]) == 0) {
700 if (svcport_str == NULL) {
701 res = malloc(sizeof(struct addrinfo));
704 res->ai_flags = hints.ai_flags;
705 res->ai_family = hints.ai_family;
706 res->ai_protocol = hints.ai_protocol;
707 switch (res->ai_family) {
709 sin = malloc(sizeof(struct sockaddr_in));
712 sin->sin_family = AF_INET;
713 sin->sin_port = htons(0);
714 sin->sin_addr.s_addr = htonl(INADDR_ANY);
715 res->ai_addr = (struct sockaddr*) sin;
716 res->ai_addrlen = (socklen_t)
717 sizeof(res->ai_addr);
720 sin6 = malloc(sizeof(struct sockaddr_in6));
723 sin6->sin6_family = AF_INET6;
724 sin6->sin6_port = htons(0);
725 sin6->sin6_addr = in6addr_any;
726 res->ai_addr = (struct sockaddr*) sin6;
727 res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
733 if ((aicode = getaddrinfo(NULL, svcport_str,
734 &hints, &res)) != 0) {
736 "cannot get local address for %s: %s",
738 gai_strerror(aicode));
743 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
744 &hints, &res)) != 0) {
746 "cannot get local address for %s: %s",
747 nconf->nc_netid, gai_strerror(aicode));
752 servaddr.len = servaddr.maxlen = res->ai_addr->sa_len;
753 servaddr.buf = res->ai_addr;
754 uaddr = taddr2uaddr(nconf, &servaddr);
756 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
759 addrs[2 * naddrs] = strdup(nconf->nc_netid);
760 addrs[2 * naddrs + 1] = uaddr;
766 sigalarm_handler(void)
775 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
776 " [-g <grace period>] [-h <bindip>] [-p <port>]");
781 * Reset the NSM state-of-the-world and acquire its state.
789 char name[] = "NFS NLM";
793 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
794 * as I know. Leave it empty for now.
796 memset(&id, 0, sizeof(id));
801 * The statd program must already be registered when lockd runs.
804 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
805 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
806 if (ret == RPC_PROGUNAVAIL) {
807 syslog(LOG_WARNING, "%lu %s", SM_PROG,
816 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
820 nsm_state = stat.state;
822 /* setup constant data for SM_MON calls */
823 mon_host.mon_id.my_id.my_name = localhost;
824 mon_host.mon_id.my_id.my_prog = NLM_PROG;
825 mon_host.mon_id.my_id.my_vers = NLM_SM;
826 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
830 * Out of memory, fatal
834 syslog(LOG_ERR, "out of memory");