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;
106 void sigalarm_handler(void);
109 * XXX move to some header file.
111 #define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
114 main(int argc, char **argv)
118 char *endptr, **hosts_bak;
119 struct sigaction sigalarm;
120 int grace_period = 30;
121 struct netconfig *nconf;
123 int maxrec = RPC_MAXDATASIZE;
124 in_port_t svcport = 0;
125 int attempt_cnt, port_len, port_pos, ret;
128 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
131 debug_level = atoi(optarg);
138 grace_period = atoi(optarg);
146 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
147 if (hosts_bak == NULL) {
149 for (i = 0; i < nhosts; i++)
156 hosts[nhosts - 1] = strdup(optarg);
157 if (hosts[nhosts - 1] == NULL) {
158 for (i = 0; i < (nhosts - 1); i++)
166 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
167 if (endptr == NULL || *endptr != '\0' ||
168 svcport == 0 || svcport >= IPPORT_MAX)
170 svcport_str = strdup(optarg);
177 if (geteuid()) { /* This command allowed only to root */
178 fprintf(stderr, "Sorry. You are not superuser\n");
182 kernel_lockd = FALSE;
183 kernel_lockd_client = FALSE;
184 if (modfind("nfslockd") < 0) {
185 if (kldload("nfslockd") < 0) {
186 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
194 if (getosreldate() >= 800040)
195 kernel_lockd_client = TRUE;
198 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
199 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
200 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
201 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
204 * Check if IPv6 support is present.
206 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
212 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
215 * If no hosts were specified, add a wildcard entry to bind to
216 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
220 hosts = malloc(sizeof(char *));
224 hosts[0] = strdup("*");
228 hosts_bak = realloc(hosts, (nhosts + 2) *
230 if (hosts_bak == NULL) {
231 for (i = 0; i < nhosts; i++)
239 hosts[nhosts - 2] = strdup("::1");
241 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
242 if (hosts_bak == NULL) {
243 for (i = 0; i < nhosts; i++)
253 hosts[nhosts - 1] = strdup("127.0.0.1");
257 if (!kernel_lockd_client) {
259 * For the case where we have a kernel lockd but it
260 * doesn't provide client locking, we run a cut-down
261 * RPC service on a local-domain socket. The kernel's
262 * RPC server will pass what it can't handle (mainly
263 * client replies) down to us.
265 struct sockaddr_un sun;
269 memset(&sun, 0, sizeof sun);
270 sun.sun_family = AF_LOCAL;
271 unlink(_PATH_RPCLOCKDSOCK);
272 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
273 sun.sun_len = SUN_LEN(&sun);
274 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
276 err(1, "Can't create local lockd socket");
278 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
279 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
280 err(1, "Can't bind local lockd socket");
283 if (listen(fd, SOMAXCONN) < 0) {
284 err(1, "Can't listen on local lockd socket");
286 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
288 err(1, "Can't create transport for local lockd socket");
290 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
291 err(1, "Can't register service for local lockd socket");
296 * We need to look up the addresses so that we can
297 * hand uaddrs (ascii encoded address+port strings) to
300 nc_handle = setnetconfig();
301 while ((nconf = getnetconfig(nc_handle))) {
302 /* We want to listen only on udp6, tcp6, udp, tcp transports */
303 if (nconf->nc_flag & NC_VISIBLE) {
304 /* Skip if there's no IPv6 support */
305 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
308 create_service(nconf);
312 endnetconfig(nc_handle);
319 nc_handle = setnetconfig();
320 while ((nconf = getnetconfig(nc_handle))) {
321 /* We want to listen only on udp6, tcp6, udp, tcp transports */
322 if (nconf->nc_flag & NC_VISIBLE) {
323 /* Skip if there's no IPv6 support */
324 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
327 ret = create_service(nconf);
329 /* Ignore this call */
333 * Failed to bind port, so close
334 * off all sockets created and
335 * try again if the port# was
336 * dynamically assigned via
340 if (mallocd_svcport != 0 &&
349 "bindresvport_sa: %m");
354 * Start over at the first
360 nc_handle = setnetconfig();
362 } else if (mallocd_svcport != 0 &&
363 attempt_cnt == GETPORT_MAXTRY) {
365 * For the last attempt, allow
366 * different port #s for each
367 * nconf by saving the
368 * svcport_str and setting it
371 port_list = realloc(port_list,
374 if (port_list == NULL)
376 port_list[port_len++] =
386 * Successfully bound the ports, so call complete_service() to
387 * do the rest of the setup on the service(s).
391 nc_handle = setnetconfig();
392 while ((nconf = getnetconfig(nc_handle))) {
393 /* We want to listen only on udp6, tcp6, udp, tcp transports */
394 if (nconf->nc_flag & NC_VISIBLE) {
395 /* Skip if there's no IPv6 support */
396 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
398 } else if (port_list != NULL) {
399 if (port_pos >= port_len) {
404 complete_service(nconf,
405 port_list[port_pos++]);
407 complete_service(nconf, svcport_str);
410 endnetconfig(nc_handle);
412 if (port_list != NULL) {
413 for (port_pos = 0; port_pos < port_len; port_pos++)
414 free(port_list[port_pos]);
420 * Note that it is NOT sensible to run this program from inetd - the
421 * protocol assumes that it will run immediately at boot time.
423 if (daemon(0, debug_level > 0)) {
424 err(1, "cannot fork");
428 openlog("rpc.lockd", 0, LOG_DAEMON);
430 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
432 syslog(LOG_INFO, "Starting");
434 sigalarm.sa_handler = (sig_t) sigalarm_handler;
435 sigemptyset(&sigalarm.sa_mask);
436 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
437 sigalarm.sa_flags |= SA_RESTART;
438 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
439 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
445 if (!kernel_lockd_client) {
447 client_pid = client_request();
450 * Create a child process to enter the kernel and then
451 * wait for RPCs on our local domain socket.
454 nlm_syscall(debug_level, grace_period,
460 * The kernel lockd implementation provides
461 * both client and server so we don't need to
464 nlm_syscall(debug_level, grace_period, naddrs, addrs);
472 client_pid = client_request();
474 svc_run(); /* Should never return */
480 * This routine creates and binds sockets on the appropriate
481 * addresses if lockd for user NLM, or perform a lookup of
482 * addresses for the kernel to create transports.
484 * It gets called one time for each transport.
486 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
487 * bind failed with EADDRINUSE.
489 * Any file descriptors that have been created are stored in sock_fd and
490 * the total count of them is maintained in sock_fdcnt.
493 create_service(struct netconfig *nconf)
495 struct addrinfo hints, *res = NULL;
496 struct sockaddr_in *sin;
497 struct sockaddr_in6 *sin6;
498 struct __rpc_sockinfo si;
503 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
506 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
507 (nconf->nc_semantics != NC_TPI_COTS) &&
508 (nconf->nc_semantics != NC_TPI_COTS_ORD))
509 return (1); /* not my type */
512 * XXX - using RPC library internal functions.
514 if (!__rpc_nconf2sockinfo(nconf, &si)) {
515 syslog(LOG_ERR, "cannot get information for %s",
520 /* Get rpc.statd's address on this transport */
521 memset(&hints, 0, sizeof hints);
522 hints.ai_family = si.si_af;
523 hints.ai_socktype = si.si_socktype;
524 hints.ai_protocol = si.si_proto;
527 * Bind to specific IPs if asked to
530 while (nhostsbak > 0) {
533 hints.ai_flags = AI_PASSIVE;
536 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
539 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
542 * XXX - using RPC library internal functions.
544 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
545 syslog(LOG_ERR, "cannot create socket for %s",
551 switch (hints.ai_family) {
553 if (inet_pton(AF_INET, hosts[nhostsbak],
555 hints.ai_flags |= AI_NUMERICHOST;
558 * Skip if we have an AF_INET6 address.
560 if (inet_pton(AF_INET6, hosts[nhostsbak],
569 if (inet_pton(AF_INET6, hosts[nhostsbak],
571 hints.ai_flags |= AI_NUMERICHOST;
574 * Skip if we have an AF_INET address.
576 if (inet_pton(AF_INET, hosts[nhostsbak],
589 * If no hosts were specified, just bind to INADDR_ANY
591 if (strcmp("*", hosts[nhostsbak]) == 0) {
592 if (svcport_str == NULL) {
593 if ((res = malloc(sizeof(struct addrinfo))) == NULL)
596 res->ai_flags = hints.ai_flags;
597 res->ai_family = hints.ai_family;
598 res->ai_protocol = hints.ai_protocol;
599 switch (res->ai_family) {
601 sin = malloc(sizeof(struct sockaddr_in));
604 sin->sin_family = AF_INET;
605 sin->sin_port = htons(0);
606 sin->sin_addr.s_addr = htonl(INADDR_ANY);
607 res->ai_addr = (struct sockaddr*) sin;
608 res->ai_addrlen = (socklen_t)
609 sizeof(struct sockaddr_in);
612 sin6 = malloc(sizeof(struct sockaddr_in6));
615 sin6->sin6_family = AF_INET6;
616 sin6->sin6_port = htons(0);
617 sin6->sin6_addr = in6addr_any;
618 res->ai_addr = (struct sockaddr*) sin6;
619 res->ai_addrlen = (socklen_t)
620 sizeof(struct sockaddr_in6);
624 "bad address family %d",
629 if ((aicode = getaddrinfo(NULL, svcport_str,
630 &hints, &res)) != 0) {
632 "cannot get local address for %s: %s",
634 gai_strerror(aicode));
641 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
642 &hints, &res)) != 0) {
644 "cannot get local address for %s: %s",
645 nconf->nc_netid, gai_strerror(aicode));
653 struct netbuf servaddr;
657 * Look up addresses for the kernel to create transports for.
659 servaddr.len = servaddr.maxlen = res->ai_addrlen;
660 servaddr.buf = res->ai_addr;
661 uaddr = taddr2uaddr(nconf, &servaddr);
663 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
666 addrs[2 * naddrs] = strdup(nconf->nc_netid);
667 addrs[2 * naddrs + 1] = uaddr;
671 sock_fd[sock_fdcnt - 1] = fd;
673 /* Now, attempt the bind. */
674 r = bindresvport_sa(fd, res->ai_addr);
676 if (errno == EADDRINUSE && mallocd_svcport != 0) {
677 if (mallocd_res != 0) {
684 syslog(LOG_ERR, "bindresvport_sa: %m");
688 if (svcport_str == NULL) {
689 svcport_str = malloc(NI_MAXSERV * sizeof(char));
690 if (svcport_str == NULL)
694 if (getnameinfo(res->ai_addr,
695 res->ai_addr->sa_len, NULL, NI_MAXHOST,
696 svcport_str, NI_MAXSERV * sizeof(char),
697 NI_NUMERICHOST | NI_NUMERICSERV))
698 errx(1, "Cannot get port number");
702 if (mallocd_res != 0) {
713 * Called after all the create_service() calls have succeeded, to complete
714 * the setup and registration.
717 complete_service(struct netconfig *nconf, char *port_str)
719 struct addrinfo hints, *res = NULL;
720 struct __rpc_sockinfo si;
721 struct netbuf servaddr;
722 SVCXPRT *transp = NULL;
723 int aicode, fd, nhostsbak;
726 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
727 (nconf->nc_semantics != NC_TPI_COTS) &&
728 (nconf->nc_semantics != NC_TPI_COTS_ORD))
729 return; /* not my type */
732 * XXX - using RPC library internal functions.
734 if (!__rpc_nconf2sockinfo(nconf, &si)) {
735 syslog(LOG_ERR, "cannot get information for %s",
741 while (nhostsbak > 0) {
743 if (sock_fdpos >= sock_fdcnt) {
744 /* Should never happen. */
745 syslog(LOG_ERR, "Ran out of socket fd's");
748 fd = sock_fd[sock_fdpos++];
752 if (nconf->nc_semantics != NC_TPI_CLTS)
753 listen(fd, SOMAXCONN);
755 transp = svc_tli_create(fd, nconf, NULL,
756 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
758 if (transp != (SVCXPRT *) NULL) {
759 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
762 "can't register %s NLM_PROG, NLM_SM service",
765 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
768 "can't register %s NLM_PROG, NLM_VERS service",
771 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
774 "can't register %s NLM_PROG, NLM_VERSX service",
777 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
780 "can't register %s NLM_PROG, NLM_VERS4 service",
784 syslog(LOG_WARNING, "can't create %s services",
787 if (registered == 0) {
789 memset(&hints, 0, sizeof hints);
790 hints.ai_flags = AI_PASSIVE;
791 hints.ai_family = si.si_af;
792 hints.ai_socktype = si.si_socktype;
793 hints.ai_protocol = si.si_proto;
795 if ((aicode = getaddrinfo(NULL, port_str, &hints,
797 syslog(LOG_ERR, "cannot get local address: %s",
798 gai_strerror(aicode));
802 servaddr.buf = malloc(res->ai_addrlen);
803 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
804 servaddr.len = res->ai_addrlen;
806 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
807 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
808 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
809 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
818 * Clear out sockets after a failure to bind one of them, so that the
819 * cycle of socket creation/binding can start anew.
822 clearout_service(void)
826 for (i = 0; i < sock_fdcnt; i++) {
827 if (sock_fd[i] >= 0) {
828 shutdown(sock_fd[i], SHUT_RDWR);
835 sigalarm_handler(void)
844 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
845 " [-g <grace period>] [-h <bindip>] [-p <port>]");
850 * Reset the NSM state-of-the-world and acquire its state.
858 char name[] = "NFS NLM";
862 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
863 * as I know. Leave it empty for now.
865 memset(&id, 0, sizeof(id));
870 * The statd program must already be registered when lockd runs.
873 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
874 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
875 if (ret == RPC_PROGUNAVAIL) {
876 syslog(LOG_WARNING, "%lu %s", SM_PROG,
885 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
889 nsm_state = stat.state;
891 /* setup constant data for SM_MON calls */
892 mon_host.mon_id.my_id.my_name = localhost;
893 mon_host.mon_id.my_id.my_prog = NLM_PROG;
894 mon_host.mon_id.my_id.my_vers = NLM_SM;
895 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
899 * Out of memory, fatal
903 syslog(LOG_ERR, "out of memory");