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 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
79 int debug_level = 0; /* 0 = no debugging syslog() calls */
85 int kernel_lockd_client;
88 char **hosts, *svcport_str = NULL;
89 static int mallocd_svcport = 0;
91 static int sock_fdcnt;
92 static int sock_fdpos;
95 char **addrs; /* actually (netid, uaddr) pairs */
96 int naddrs; /* count of how many (netid, uaddr) pairs */
97 char localhost[] = "localhost";
99 static int create_service(struct netconfig *nconf);
100 static void complete_service(struct netconfig *nconf, char *port_str);
101 static void clearout_service(void);
102 static void out_of_mem(void) __dead2;
104 void nlm_prog_0(struct svc_req *, SVCXPRT *);
105 void nlm_prog_1(struct svc_req *, SVCXPRT *);
106 void nlm_prog_3(struct svc_req *, SVCXPRT *);
107 void nlm_prog_4(struct svc_req *, SVCXPRT *);
110 void sigalarm_handler(void);
113 * XXX move to some header file.
115 #define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
118 main(int argc, char **argv)
122 char *endptr, **hosts_bak;
123 struct sigaction sigalarm;
124 int grace_period = 30;
125 struct netconfig *nconf;
127 int maxrec = RPC_MAXDATASIZE;
128 in_port_t svcport = 0;
129 int attempt_cnt, port_len, port_pos, ret;
132 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
135 debug_level = atoi(optarg);
142 grace_period = atoi(optarg);
150 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
151 if (hosts_bak == NULL) {
153 for (i = 0; i < nhosts; i++)
160 hosts[nhosts - 1] = strdup(optarg);
161 if (hosts[nhosts - 1] == NULL) {
162 for (i = 0; i < (nhosts - 1); i++)
170 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
171 if (endptr == NULL || *endptr != '\0' ||
172 svcport == 0 || svcport >= IPPORT_MAX)
174 svcport_str = strdup(optarg);
181 if (geteuid()) { /* This command allowed only to root */
182 fprintf(stderr, "Sorry. You are not superuser\n");
186 kernel_lockd = FALSE;
187 kernel_lockd_client = FALSE;
188 if (modfind("nfslockd") < 0) {
189 if (kldload("nfslockd") < 0) {
190 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
198 if (getosreldate() >= 800040)
199 kernel_lockd_client = TRUE;
202 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
203 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
204 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
205 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
208 * Check if IPv6 support is present.
210 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
216 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
219 * If no hosts were specified, add a wildcard entry to bind to
220 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
224 hosts = malloc(sizeof(char**));
232 hosts_bak = realloc(hosts, (nhosts + 2) *
234 if (hosts_bak == NULL) {
235 for (i = 0; i < nhosts; i++)
243 hosts[nhosts - 2] = "::1";
245 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
246 if (hosts_bak == NULL) {
247 for (i = 0; i < nhosts; i++)
257 hosts[nhosts - 1] = "127.0.0.1";
261 if (!kernel_lockd_client) {
263 * For the case where we have a kernel lockd but it
264 * doesn't provide client locking, we run a cut-down
265 * RPC service on a local-domain socket. The kernel's
266 * RPC server will pass what it can't handle (mainly
267 * client replies) down to us.
269 struct sockaddr_un sun;
273 memset(&sun, 0, sizeof sun);
274 sun.sun_family = AF_LOCAL;
275 unlink(_PATH_RPCLOCKDSOCK);
276 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
277 sun.sun_len = SUN_LEN(&sun);
278 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
280 err(1, "Can't create local lockd socket");
282 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
283 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
284 err(1, "Can't bind local lockd socket");
287 if (listen(fd, SOMAXCONN) < 0) {
288 err(1, "Can't listen on local lockd socket");
290 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
292 err(1, "Can't create transport for local lockd socket");
294 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
295 err(1, "Can't register service for local lockd socket");
300 * We need to look up the addresses so that we can
301 * hand uaddrs (ascii encoded address+port strings) to
304 nc_handle = setnetconfig();
305 while ((nconf = getnetconfig(nc_handle))) {
306 /* We want to listen only on udp6, tcp6, udp, tcp transports */
307 if (nconf->nc_flag & NC_VISIBLE) {
308 /* Skip if there's no IPv6 support */
309 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
312 create_service(nconf);
316 endnetconfig(nc_handle);
323 nc_handle = setnetconfig();
324 while ((nconf = getnetconfig(nc_handle))) {
325 /* We want to listen only on udp6, tcp6, udp, tcp transports */
326 if (nconf->nc_flag & NC_VISIBLE) {
327 /* Skip if there's no IPv6 support */
328 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
331 ret = create_service(nconf);
333 /* Ignore this call */
337 * Failed to bind port, so close
338 * off all sockets created and
339 * try again if the port# was
340 * dynamically assigned via
344 if (mallocd_svcport != 0 &&
353 "bindresvport_sa: %m");
358 * Start over at the first
364 nc_handle = setnetconfig();
366 } else if (mallocd_svcport != 0 &&
367 attempt_cnt == GETPORT_MAXTRY) {
369 * For the last attempt, allow
370 * different port #s for each
371 * nconf by saving the
372 * svcport_str and setting it
375 port_list = realloc(port_list,
378 if (port_list == NULL)
380 port_list[port_len++] =
390 * Successfully bound the ports, so call complete_service() to
391 * do the rest of the setup on the service(s).
395 nc_handle = setnetconfig();
396 while ((nconf = getnetconfig(nc_handle))) {
397 /* We want to listen only on udp6, tcp6, udp, tcp transports */
398 if (nconf->nc_flag & NC_VISIBLE) {
399 /* Skip if there's no IPv6 support */
400 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
402 } else if (port_list != NULL) {
403 if (port_pos >= port_len) {
408 complete_service(nconf,
409 port_list[port_pos++]);
411 complete_service(nconf, svcport_str);
414 endnetconfig(nc_handle);
416 if (port_list != NULL) {
417 for (port_pos = 0; port_pos < port_len; port_pos++)
418 free(port_list[port_pos]);
424 * Note that it is NOT sensible to run this program from inetd - the
425 * protocol assumes that it will run immediately at boot time.
427 if (daemon(0, debug_level > 0)) {
428 err(1, "cannot fork");
432 openlog("rpc.lockd", 0, LOG_DAEMON);
434 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
436 syslog(LOG_INFO, "Starting");
438 sigalarm.sa_handler = (sig_t) sigalarm_handler;
439 sigemptyset(&sigalarm.sa_mask);
440 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
441 sigalarm.sa_flags |= SA_RESTART;
442 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
443 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
449 if (!kernel_lockd_client) {
451 client_pid = client_request();
454 * Create a child process to enter the kernel and then
455 * wait for RPCs on our local domain socket.
458 nlm_syscall(debug_level, grace_period,
464 * The kernel lockd implementation provides
465 * both client and server so we don't need to
468 nlm_syscall(debug_level, grace_period, naddrs, addrs);
476 client_pid = client_request();
478 svc_run(); /* Should never return */
484 * This routine creates and binds sockets on the appropriate
485 * addresses if lockd for user NLM, or perform a lookup of
486 * addresses for the kernel to create transports.
488 * It gets called one time for each transport.
490 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
491 * bind failed with EADDRINUSE.
493 * Any file descriptors that have been created are stored in sock_fd and
494 * the total count of them is maintained in sock_fdcnt.
497 create_service(struct netconfig *nconf)
499 struct addrinfo hints, *res = NULL;
500 struct sockaddr_in *sin;
501 struct sockaddr_in6 *sin6;
502 struct __rpc_sockinfo si;
507 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
510 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
511 (nconf->nc_semantics != NC_TPI_COTS) &&
512 (nconf->nc_semantics != NC_TPI_COTS_ORD))
513 return (1); /* not my type */
516 * XXX - using RPC library internal functions.
518 if (!__rpc_nconf2sockinfo(nconf, &si)) {
519 syslog(LOG_ERR, "cannot get information for %s",
524 /* Get rpc.statd's address on this transport */
525 memset(&hints, 0, sizeof hints);
526 hints.ai_family = si.si_af;
527 hints.ai_socktype = si.si_socktype;
528 hints.ai_protocol = si.si_proto;
531 * Bind to specific IPs if asked to
534 while (nhostsbak > 0) {
537 hints.ai_flags = AI_PASSIVE;
540 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
543 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
546 * XXX - using RPC library internal functions.
548 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
549 syslog(LOG_ERR, "cannot create socket for %s",
555 switch (hints.ai_family) {
557 if (inet_pton(AF_INET, hosts[nhostsbak],
559 hints.ai_flags |= AI_NUMERICHOST;
562 * Skip if we have an AF_INET6 address.
564 if (inet_pton(AF_INET6, hosts[nhostsbak],
573 if (inet_pton(AF_INET6, hosts[nhostsbak],
575 hints.ai_flags |= AI_NUMERICHOST;
578 * Skip if we have an AF_INET address.
580 if (inet_pton(AF_INET, hosts[nhostsbak],
593 * If no hosts were specified, just bind to INADDR_ANY
595 if (strcmp("*", hosts[nhostsbak]) == 0) {
596 if (svcport_str == NULL) {
597 if ((res = malloc(sizeof(struct addrinfo))) == NULL)
600 res->ai_flags = hints.ai_flags;
601 res->ai_family = hints.ai_family;
602 res->ai_protocol = hints.ai_protocol;
603 switch (res->ai_family) {
605 sin = malloc(sizeof(struct sockaddr_in));
608 sin->sin_family = AF_INET;
609 sin->sin_port = htons(0);
610 sin->sin_addr.s_addr = htonl(INADDR_ANY);
611 res->ai_addr = (struct sockaddr*) sin;
612 res->ai_addrlen = (socklen_t)
613 sizeof(struct sockaddr_in);
616 sin6 = malloc(sizeof(struct sockaddr_in6));
619 sin6->sin6_family = AF_INET6;
620 sin6->sin6_port = htons(0);
621 sin6->sin6_addr = in6addr_any;
622 res->ai_addr = (struct sockaddr*) sin6;
623 res->ai_addrlen = (socklen_t)
624 sizeof(struct sockaddr_in6);
628 "bad address family %d",
633 if ((aicode = getaddrinfo(NULL, svcport_str,
634 &hints, &res)) != 0) {
636 "cannot get local address for %s: %s",
638 gai_strerror(aicode));
645 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
646 &hints, &res)) != 0) {
648 "cannot get local address for %s: %s",
649 nconf->nc_netid, gai_strerror(aicode));
657 struct netbuf servaddr;
661 * Look up addresses for the kernel to create transports for.
663 servaddr.len = servaddr.maxlen = res->ai_addrlen;
664 servaddr.buf = res->ai_addr;
665 uaddr = taddr2uaddr(nconf, &servaddr);
667 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
670 addrs[2 * naddrs] = strdup(nconf->nc_netid);
671 addrs[2 * naddrs + 1] = uaddr;
675 sock_fd[sock_fdcnt - 1] = fd;
677 /* Now, attempt the bind. */
678 r = bindresvport_sa(fd, res->ai_addr);
680 if (errno == EADDRINUSE && mallocd_svcport != 0) {
681 if (mallocd_res != 0) {
688 syslog(LOG_ERR, "bindresvport_sa: %m");
692 if (svcport_str == NULL) {
693 svcport_str = malloc(NI_MAXSERV * sizeof(char));
694 if (svcport_str == NULL)
698 if (getnameinfo(res->ai_addr,
699 res->ai_addr->sa_len, NULL, NI_MAXHOST,
700 svcport_str, NI_MAXSERV * sizeof(char),
701 NI_NUMERICHOST | NI_NUMERICSERV))
702 errx(1, "Cannot get port number");
706 if (mallocd_res != 0) {
717 * Called after all the create_service() calls have succeeded, to complete
718 * the setup and registration.
721 complete_service(struct netconfig *nconf, char *port_str)
723 struct addrinfo hints, *res = NULL;
724 struct __rpc_sockinfo si;
725 struct netbuf servaddr;
726 SVCXPRT *transp = NULL;
727 int aicode, fd, nhostsbak;
730 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
731 (nconf->nc_semantics != NC_TPI_COTS) &&
732 (nconf->nc_semantics != NC_TPI_COTS_ORD))
733 return; /* not my type */
736 * XXX - using RPC library internal functions.
738 if (!__rpc_nconf2sockinfo(nconf, &si)) {
739 syslog(LOG_ERR, "cannot get information for %s",
745 while (nhostsbak > 0) {
747 if (sock_fdpos >= sock_fdcnt) {
748 /* Should never happen. */
749 syslog(LOG_ERR, "Ran out of socket fd's");
752 fd = sock_fd[sock_fdpos++];
756 if (nconf->nc_semantics != NC_TPI_CLTS)
757 listen(fd, SOMAXCONN);
759 transp = svc_tli_create(fd, nconf, NULL,
760 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
762 if (transp != (SVCXPRT *) NULL) {
763 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
766 "can't register %s NLM_PROG, NLM_SM service",
769 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
772 "can't register %s NLM_PROG, NLM_VERS service",
775 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
778 "can't register %s NLM_PROG, NLM_VERSX service",
781 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
784 "can't register %s NLM_PROG, NLM_VERS4 service",
788 syslog(LOG_WARNING, "can't create %s services",
791 if (registered == 0) {
793 memset(&hints, 0, sizeof hints);
794 hints.ai_flags = AI_PASSIVE;
795 hints.ai_family = si.si_af;
796 hints.ai_socktype = si.si_socktype;
797 hints.ai_protocol = si.si_proto;
799 if ((aicode = getaddrinfo(NULL, port_str, &hints,
801 syslog(LOG_ERR, "cannot get local address: %s",
802 gai_strerror(aicode));
806 servaddr.buf = malloc(res->ai_addrlen);
807 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
808 servaddr.len = res->ai_addrlen;
810 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
811 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
812 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
813 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
822 * Clear out sockets after a failure to bind one of them, so that the
823 * cycle of socket creation/binding can start anew.
826 clearout_service(void)
830 for (i = 0; i < sock_fdcnt; i++) {
831 if (sock_fd[i] >= 0) {
832 shutdown(sock_fd[i], SHUT_RDWR);
839 sigalarm_handler(void)
848 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
849 " [-g <grace period>] [-h <bindip>] [-p <port>]");
854 * Reset the NSM state-of-the-world and acquire its state.
862 char name[] = "NFS NLM";
866 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
867 * as I know. Leave it empty for now.
869 memset(&id, 0, sizeof(id));
874 * The statd program must already be registered when lockd runs.
877 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
878 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
879 if (ret == RPC_PROGUNAVAIL) {
880 syslog(LOG_WARNING, "%lu %s", SM_PROG,
889 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
893 nsm_state = stat.state;
895 /* setup constant data for SM_MON calls */
896 mon_host.mon_id.my_id.my_name = localhost;
897 mon_host.mon_id.my_id.my_prog = NLM_PROG;
898 mon_host.mon_id.my_id.my_vers = NLM_SM;
899 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
903 * Out of memory, fatal
907 syslog(LOG_ERR, "out of memory");