1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
8 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the FreeBSD project
21 * 4. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
45 * main() function for NFS lock daemon. Most of the code in this
46 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
48 * The actual program logic is in the file lock_proc.c
51 #include <sys/param.h>
52 #include <sys/linker.h>
53 #include <sys/module.h>
54 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
69 #include <netconfig.h>
73 #include <rpc/rpc_com.h>
74 #include <rpcsvc/sm_inter.h>
77 #include <rpcsvc/nlm_prot.h>
79 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
81 int debug_level = 0; /* 0 = no debugging syslog() calls */
87 int kernel_lockd_client;
90 char **hosts, *svcport_str = NULL;
91 static int mallocd_svcport = 0;
93 static int sock_fdcnt;
94 static int sock_fdpos;
97 char **addrs; /* actually (netid, uaddr) pairs */
98 int naddrs; /* count of how many (netid, uaddr) pairs */
99 char localhost[] = "localhost";
101 static int create_service(struct netconfig *nconf);
102 static void complete_service(struct netconfig *nconf, char *port_str);
103 static void clearout_service(void);
104 static void out_of_mem(void) __dead2;
108 void sigalarm_handler(void);
111 * XXX move to some header file.
113 #define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
116 main(int argc, char **argv)
120 char *endptr, **hosts_bak;
121 struct sigaction sigalarm;
122 int grace_period = 30;
123 struct netconfig *nconf;
125 int maxrec = RPC_MAXDATASIZE;
126 in_port_t svcport = 0;
127 int attempt_cnt, port_len, port_pos, ret;
130 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
133 debug_level = atoi(optarg);
140 grace_period = atoi(optarg);
148 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
149 if (hosts_bak == NULL) {
151 for (i = 0; i < nhosts; i++)
158 hosts[nhosts - 1] = strdup(optarg);
159 if (hosts[nhosts - 1] == NULL) {
160 for (i = 0; i < (nhosts - 1); i++)
168 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
169 if (endptr == NULL || *endptr != '\0' ||
170 svcport == 0 || svcport >= IPPORT_MAX)
172 svcport_str = strdup(optarg);
179 if (geteuid()) { /* This command allowed only to root */
180 fprintf(stderr, "Sorry. You are not superuser\n");
184 kernel_lockd = FALSE;
185 kernel_lockd_client = FALSE;
186 if (modfind("nfslockd") < 0) {
187 if (kldload("nfslockd") < 0) {
188 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
196 if (getosreldate() >= 800040)
197 kernel_lockd_client = TRUE;
200 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
201 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
202 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
203 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
206 * Check if IPv6 support is present.
208 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
214 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
217 * If no hosts were specified, add a wildcard entry to bind to
218 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
222 hosts = malloc(sizeof(char *));
226 hosts[0] = strdup("*");
230 hosts_bak = realloc(hosts, (nhosts + 2) *
232 if (hosts_bak == NULL) {
233 for (i = 0; i < nhosts; i++)
241 hosts[nhosts - 2] = strdup("::1");
243 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
244 if (hosts_bak == NULL) {
245 for (i = 0; i < nhosts; i++)
255 hosts[nhosts - 1] = strdup("127.0.0.1");
259 if (!kernel_lockd_client) {
261 * For the case where we have a kernel lockd but it
262 * doesn't provide client locking, we run a cut-down
263 * RPC service on a local-domain socket. The kernel's
264 * RPC server will pass what it can't handle (mainly
265 * client replies) down to us.
267 struct sockaddr_un sun;
271 memset(&sun, 0, sizeof sun);
272 sun.sun_family = AF_LOCAL;
273 unlink(_PATH_RPCLOCKDSOCK);
274 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
275 sun.sun_len = SUN_LEN(&sun);
276 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
278 err(1, "Can't create local lockd socket");
280 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
281 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
282 err(1, "Can't bind local lockd socket");
285 if (listen(fd, SOMAXCONN) < 0) {
286 err(1, "Can't listen on local lockd socket");
288 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
290 err(1, "Can't create transport for local lockd socket");
292 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
293 err(1, "Can't register service for local lockd socket");
298 * We need to look up the addresses so that we can
299 * hand uaddrs (ascii encoded address+port strings) to
302 nc_handle = setnetconfig();
303 while ((nconf = getnetconfig(nc_handle))) {
304 /* We want to listen only on udp6, tcp6, udp, tcp transports */
305 if (nconf->nc_flag & NC_VISIBLE) {
306 /* Skip if there's no IPv6 support */
307 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
310 create_service(nconf);
314 endnetconfig(nc_handle);
321 nc_handle = setnetconfig();
322 while ((nconf = getnetconfig(nc_handle))) {
323 /* We want to listen only on udp6, tcp6, udp, tcp transports */
324 if (nconf->nc_flag & NC_VISIBLE) {
325 /* Skip if there's no IPv6 support */
326 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
329 ret = create_service(nconf);
331 /* Ignore this call */
335 * Failed to bind port, so close
336 * off all sockets created and
337 * try again if the port# was
338 * dynamically assigned via
342 if (mallocd_svcport != 0 &&
351 "bindresvport_sa: %m");
356 * Start over at the first
362 nc_handle = setnetconfig();
364 } else if (mallocd_svcport != 0 &&
365 attempt_cnt == GETPORT_MAXTRY) {
367 * For the last attempt, allow
368 * different port #s for each
369 * nconf by saving the
370 * svcport_str and setting it
373 port_list = realloc(port_list,
376 if (port_list == NULL)
378 port_list[port_len++] =
388 * Successfully bound the ports, so call complete_service() to
389 * do the rest of the setup on the service(s).
393 nc_handle = setnetconfig();
394 while ((nconf = getnetconfig(nc_handle))) {
395 /* We want to listen only on udp6, tcp6, udp, tcp transports */
396 if (nconf->nc_flag & NC_VISIBLE) {
397 /* Skip if there's no IPv6 support */
398 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
400 } else if (port_list != NULL) {
401 if (port_pos >= port_len) {
406 complete_service(nconf,
407 port_list[port_pos++]);
409 complete_service(nconf, svcport_str);
412 endnetconfig(nc_handle);
414 if (port_list != NULL) {
415 for (port_pos = 0; port_pos < port_len; port_pos++)
416 free(port_list[port_pos]);
422 * Note that it is NOT sensible to run this program from inetd - the
423 * protocol assumes that it will run immediately at boot time.
425 if (daemon(0, debug_level > 0)) {
426 err(1, "cannot fork");
430 openlog("rpc.lockd", 0, LOG_DAEMON);
432 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
434 syslog(LOG_INFO, "Starting");
436 sigalarm.sa_handler = (sig_t) sigalarm_handler;
437 sigemptyset(&sigalarm.sa_mask);
438 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
439 sigalarm.sa_flags |= SA_RESTART;
440 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
441 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
447 if (!kernel_lockd_client) {
449 client_pid = client_request();
452 * Create a child process to enter the kernel and then
453 * wait for RPCs on our local domain socket.
456 nlm_syscall(debug_level, grace_period,
462 * The kernel lockd implementation provides
463 * both client and server so we don't need to
466 nlm_syscall(debug_level, grace_period, naddrs, addrs);
474 client_pid = client_request();
476 svc_run(); /* Should never return */
482 * This routine creates and binds sockets on the appropriate
483 * addresses if lockd for user NLM, or perform a lookup of
484 * addresses for the kernel to create transports.
486 * It gets called one time for each transport.
488 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
489 * bind failed with EADDRINUSE.
491 * Any file descriptors that have been created are stored in sock_fd and
492 * the total count of them is maintained in sock_fdcnt.
495 create_service(struct netconfig *nconf)
497 struct addrinfo hints, *res = NULL;
498 struct sockaddr_in *sin;
499 struct sockaddr_in6 *sin6;
500 struct __rpc_sockinfo si;
505 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
508 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
509 (nconf->nc_semantics != NC_TPI_COTS) &&
510 (nconf->nc_semantics != NC_TPI_COTS_ORD))
511 return (1); /* not my type */
514 * XXX - using RPC library internal functions.
516 if (!__rpc_nconf2sockinfo(nconf, &si)) {
517 syslog(LOG_ERR, "cannot get information for %s",
522 /* Get rpc.statd's address on this transport */
523 memset(&hints, 0, sizeof hints);
524 hints.ai_family = si.si_af;
525 hints.ai_socktype = si.si_socktype;
526 hints.ai_protocol = si.si_proto;
529 * Bind to specific IPs if asked to
532 while (nhostsbak > 0) {
535 hints.ai_flags = AI_PASSIVE;
538 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
541 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
544 * XXX - using RPC library internal functions.
546 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
547 syslog(LOG_ERR, "cannot create socket for %s",
553 switch (hints.ai_family) {
555 if (inet_pton(AF_INET, hosts[nhostsbak],
557 hints.ai_flags |= AI_NUMERICHOST;
560 * Skip if we have an AF_INET6 address.
562 if (inet_pton(AF_INET6, hosts[nhostsbak],
571 if (inet_pton(AF_INET6, hosts[nhostsbak],
573 hints.ai_flags |= AI_NUMERICHOST;
576 * Skip if we have an AF_INET address.
578 if (inet_pton(AF_INET, hosts[nhostsbak],
591 * If no hosts were specified, just bind to INADDR_ANY
593 if (strcmp("*", hosts[nhostsbak]) == 0) {
594 if (svcport_str == NULL) {
595 if ((res = malloc(sizeof(struct addrinfo))) == NULL)
598 res->ai_flags = hints.ai_flags;
599 res->ai_family = hints.ai_family;
600 res->ai_protocol = hints.ai_protocol;
601 switch (res->ai_family) {
603 sin = malloc(sizeof(struct sockaddr_in));
606 sin->sin_family = AF_INET;
607 sin->sin_port = htons(0);
608 sin->sin_addr.s_addr = htonl(INADDR_ANY);
609 res->ai_addr = (struct sockaddr*) sin;
610 res->ai_addrlen = (socklen_t)
611 sizeof(struct sockaddr_in);
614 sin6 = malloc(sizeof(struct sockaddr_in6));
617 sin6->sin6_family = AF_INET6;
618 sin6->sin6_port = htons(0);
619 sin6->sin6_addr = in6addr_any;
620 res->ai_addr = (struct sockaddr*) sin6;
621 res->ai_addrlen = (socklen_t)
622 sizeof(struct sockaddr_in6);
626 "bad address family %d",
631 if ((aicode = getaddrinfo(NULL, svcport_str,
632 &hints, &res)) != 0) {
634 "cannot get local address for %s: %s",
636 gai_strerror(aicode));
643 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
644 &hints, &res)) != 0) {
646 "cannot get local address for %s: %s",
647 nconf->nc_netid, gai_strerror(aicode));
655 struct netbuf servaddr;
659 * Look up addresses for the kernel to create transports for.
661 servaddr.len = servaddr.maxlen = res->ai_addrlen;
662 servaddr.buf = res->ai_addr;
663 uaddr = taddr2uaddr(nconf, &servaddr);
665 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
668 addrs[2 * naddrs] = strdup(nconf->nc_netid);
669 addrs[2 * naddrs + 1] = uaddr;
673 sock_fd[sock_fdcnt - 1] = fd;
675 /* Now, attempt the bind. */
676 r = bindresvport_sa(fd, res->ai_addr);
678 if (errno == EADDRINUSE && mallocd_svcport != 0) {
679 if (mallocd_res != 0) {
686 syslog(LOG_ERR, "bindresvport_sa: %m");
690 if (svcport_str == NULL) {
691 svcport_str = malloc(NI_MAXSERV * sizeof(char));
692 if (svcport_str == NULL)
696 if (getnameinfo(res->ai_addr,
697 res->ai_addr->sa_len, NULL, NI_MAXHOST,
698 svcport_str, NI_MAXSERV * sizeof(char),
699 NI_NUMERICHOST | NI_NUMERICSERV))
700 errx(1, "Cannot get port number");
704 if (mallocd_res != 0) {
715 * Called after all the create_service() calls have succeeded, to complete
716 * the setup and registration.
719 complete_service(struct netconfig *nconf, char *port_str)
721 struct addrinfo hints, *res = NULL;
722 struct __rpc_sockinfo si;
723 struct netbuf servaddr;
724 SVCXPRT *transp = NULL;
725 int aicode, fd, nhostsbak;
728 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
729 (nconf->nc_semantics != NC_TPI_COTS) &&
730 (nconf->nc_semantics != NC_TPI_COTS_ORD))
731 return; /* not my type */
734 * XXX - using RPC library internal functions.
736 if (!__rpc_nconf2sockinfo(nconf, &si)) {
737 syslog(LOG_ERR, "cannot get information for %s",
743 while (nhostsbak > 0) {
745 if (sock_fdpos >= sock_fdcnt) {
746 /* Should never happen. */
747 syslog(LOG_ERR, "Ran out of socket fd's");
750 fd = sock_fd[sock_fdpos++];
754 if (nconf->nc_semantics != NC_TPI_CLTS)
755 listen(fd, SOMAXCONN);
757 transp = svc_tli_create(fd, nconf, NULL,
758 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
760 if (transp != (SVCXPRT *) NULL) {
761 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
764 "can't register %s NLM_PROG, NLM_SM service",
767 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
770 "can't register %s NLM_PROG, NLM_VERS service",
773 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
776 "can't register %s NLM_PROG, NLM_VERSX service",
779 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
782 "can't register %s NLM_PROG, NLM_VERS4 service",
786 syslog(LOG_WARNING, "can't create %s services",
789 if (registered == 0) {
791 memset(&hints, 0, sizeof hints);
792 hints.ai_flags = AI_PASSIVE;
793 hints.ai_family = si.si_af;
794 hints.ai_socktype = si.si_socktype;
795 hints.ai_protocol = si.si_proto;
797 if ((aicode = getaddrinfo(NULL, port_str, &hints,
799 syslog(LOG_ERR, "cannot get local address: %s",
800 gai_strerror(aicode));
804 servaddr.buf = malloc(res->ai_addrlen);
805 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
806 servaddr.len = res->ai_addrlen;
808 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
809 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
810 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
811 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
820 * Clear out sockets after a failure to bind one of them, so that the
821 * cycle of socket creation/binding can start anew.
824 clearout_service(void)
828 for (i = 0; i < sock_fdcnt; i++) {
829 if (sock_fd[i] >= 0) {
830 shutdown(sock_fd[i], SHUT_RDWR);
837 sigalarm_handler(void)
846 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
847 " [-g <grace period>] [-h <bindip>] [-p <port>]");
852 * Reset the NSM state-of-the-world and acquire its state.
860 char name[] = "NFS NLM";
864 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
865 * as I know. Leave it empty for now.
867 memset(&id, 0, sizeof(id));
872 * The statd program must already be registered when lockd runs.
875 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
876 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
877 if (ret == RPC_PROGUNAVAIL) {
878 syslog(LOG_WARNING, "%lu %s", SM_PROG,
887 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
891 nsm_state = stat.state;
893 /* setup constant data for SM_MON calls */
894 mon_host.mon_id.my_id.my_name = localhost;
895 mon_host.mon_id.my_id.my_prog = NLM_PROG;
896 mon_host.mon_id.my_id.my_vers = NLM_SM;
897 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
901 * Out of memory, fatal
905 syslog(LOG_ERR, "out of memory");