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;
124 struct netconfig *nconf;
126 int maxrec = RPC_MAXDATASIZE;
127 in_port_t svcport = 0;
128 int attempt_cnt, port_len, port_pos, ret;
131 while ((ch = getopt(argc, argv, "d:Fg:h:p:")) != (-1)) {
134 debug_level = atoi(optarg);
144 grace_period = atoi(optarg);
152 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
153 if (hosts_bak == NULL) {
155 for (i = 0; i < nhosts; i++)
162 hosts[nhosts - 1] = strdup(optarg);
163 if (hosts[nhosts - 1] == NULL) {
164 for (i = 0; i < (nhosts - 1); i++)
172 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
173 if (endptr == NULL || *endptr != '\0' ||
174 svcport == 0 || svcport >= IPPORT_MAX)
176 svcport_str = strdup(optarg);
183 if (geteuid()) { /* This command allowed only to root */
184 fprintf(stderr, "Sorry. You are not superuser\n");
188 kernel_lockd = FALSE;
189 kernel_lockd_client = FALSE;
190 if (modfind("nfslockd") < 0) {
191 if (kldload("nfslockd") < 0) {
192 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
200 if (getosreldate() >= 800040)
201 kernel_lockd_client = TRUE;
204 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
205 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
206 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
207 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
210 * Check if IPv6 support is present.
212 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
218 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
221 * If no hosts were specified, add a wildcard entry to bind to
222 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
226 hosts = malloc(sizeof(char *));
230 hosts[0] = strdup("*");
234 hosts_bak = realloc(hosts, (nhosts + 2) *
236 if (hosts_bak == NULL) {
237 for (i = 0; i < nhosts; i++)
245 hosts[nhosts - 2] = strdup("::1");
247 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
248 if (hosts_bak == NULL) {
249 for (i = 0; i < nhosts; i++)
259 hosts[nhosts - 1] = strdup("127.0.0.1");
263 if (!kernel_lockd_client) {
265 * For the case where we have a kernel lockd but it
266 * doesn't provide client locking, we run a cut-down
267 * RPC service on a local-domain socket. The kernel's
268 * RPC server will pass what it can't handle (mainly
269 * client replies) down to us.
271 struct sockaddr_un sun;
275 memset(&sun, 0, sizeof sun);
276 sun.sun_family = AF_LOCAL;
277 unlink(_PATH_RPCLOCKDSOCK);
278 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
279 sun.sun_len = SUN_LEN(&sun);
280 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
282 err(1, "Can't create local lockd socket");
284 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
285 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
286 err(1, "Can't bind local lockd socket");
289 if (listen(fd, SOMAXCONN) < 0) {
290 err(1, "Can't listen on local lockd socket");
292 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
294 err(1, "Can't create transport for local lockd socket");
296 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
297 err(1, "Can't register service for local lockd socket");
302 * We need to look up the addresses so that we can
303 * hand uaddrs (ascii encoded address+port strings) to
306 nc_handle = setnetconfig();
307 while ((nconf = getnetconfig(nc_handle))) {
308 /* We want to listen only on udp6, tcp6, udp, tcp transports */
309 if (nconf->nc_flag & NC_VISIBLE) {
310 /* Skip if there's no IPv6 support */
311 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
314 create_service(nconf);
318 endnetconfig(nc_handle);
325 nc_handle = setnetconfig();
326 while ((nconf = getnetconfig(nc_handle))) {
327 /* We want to listen only on udp6, tcp6, udp, tcp transports */
328 if (nconf->nc_flag & NC_VISIBLE) {
329 /* Skip if there's no IPv6 support */
330 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
333 ret = create_service(nconf);
335 /* Ignore this call */
339 * Failed to bind port, so close
340 * off all sockets created and
341 * try again if the port# was
342 * dynamically assigned via
346 if (mallocd_svcport != 0 &&
355 "bindresvport_sa: %m");
360 * Start over at the first
366 nc_handle = setnetconfig();
368 } else if (mallocd_svcport != 0 &&
369 attempt_cnt == GETPORT_MAXTRY) {
371 * For the last attempt, allow
372 * different port #s for each
373 * nconf by saving the
374 * svcport_str and setting it
377 port_list = realloc(port_list,
380 if (port_list == NULL)
382 port_list[port_len++] =
392 * Successfully bound the ports, so call complete_service() to
393 * do the rest of the setup on the service(s).
397 nc_handle = setnetconfig();
398 while ((nconf = getnetconfig(nc_handle))) {
399 /* We want to listen only on udp6, tcp6, udp, tcp transports */
400 if (nconf->nc_flag & NC_VISIBLE) {
401 /* Skip if there's no IPv6 support */
402 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
404 } else if (port_list != NULL) {
405 if (port_pos >= port_len) {
410 complete_service(nconf,
411 port_list[port_pos++]);
413 complete_service(nconf, svcport_str);
416 endnetconfig(nc_handle);
418 if (port_list != NULL) {
419 for (port_pos = 0; port_pos < port_len; port_pos++)
420 free(port_list[port_pos]);
426 * Note that it is NOT sensible to run this program from inetd - the
427 * protocol assumes that it will run immediately at boot time.
429 if ((foreground == 0) && daemon(0, debug_level > 0)) {
430 err(1, "cannot fork");
434 openlog("rpc.lockd", 0, LOG_DAEMON);
436 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
438 syslog(LOG_INFO, "Starting");
440 sigalarm.sa_handler = (sig_t) sigalarm_handler;
441 sigemptyset(&sigalarm.sa_mask);
442 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
443 sigalarm.sa_flags |= SA_RESTART;
444 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
445 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
451 if (!kernel_lockd_client) {
453 client_pid = client_request();
456 * Create a child process to enter the kernel and then
457 * wait for RPCs on our local domain socket.
460 nlm_syscall(debug_level, grace_period,
466 * The kernel lockd implementation provides
467 * both client and server so we don't need to
470 nlm_syscall(debug_level, grace_period, naddrs, addrs);
478 client_pid = client_request();
480 svc_run(); /* Should never return */
486 * This routine creates and binds sockets on the appropriate
487 * addresses if lockd for user NLM, or perform a lookup of
488 * addresses for the kernel to create transports.
490 * It gets called one time for each transport.
492 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
493 * bind failed with EADDRINUSE.
495 * Any file descriptors that have been created are stored in sock_fd and
496 * the total count of them is maintained in sock_fdcnt.
499 create_service(struct netconfig *nconf)
501 struct addrinfo hints, *res = NULL;
502 struct sockaddr_in *sin;
503 struct sockaddr_in6 *sin6;
504 struct __rpc_sockinfo si;
509 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
512 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
513 (nconf->nc_semantics != NC_TPI_COTS) &&
514 (nconf->nc_semantics != NC_TPI_COTS_ORD))
515 return (1); /* not my type */
518 * XXX - using RPC library internal functions.
520 if (!__rpc_nconf2sockinfo(nconf, &si)) {
521 syslog(LOG_ERR, "cannot get information for %s",
526 /* Get rpc.statd's address on this transport */
527 memset(&hints, 0, sizeof hints);
528 hints.ai_family = si.si_af;
529 hints.ai_socktype = si.si_socktype;
530 hints.ai_protocol = si.si_proto;
533 * Bind to specific IPs if asked to
536 while (nhostsbak > 0) {
539 hints.ai_flags = AI_PASSIVE;
542 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
545 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
548 * XXX - using RPC library internal functions.
550 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
551 syslog(LOG_ERR, "cannot create socket for %s",
557 switch (hints.ai_family) {
559 if (inet_pton(AF_INET, hosts[nhostsbak],
561 hints.ai_flags |= AI_NUMERICHOST;
564 * Skip if we have an AF_INET6 address.
566 if (inet_pton(AF_INET6, hosts[nhostsbak],
575 if (inet_pton(AF_INET6, hosts[nhostsbak],
577 hints.ai_flags |= AI_NUMERICHOST;
580 * Skip if we have an AF_INET address.
582 if (inet_pton(AF_INET, hosts[nhostsbak],
595 * If no hosts were specified, just bind to INADDR_ANY
597 if (strcmp("*", hosts[nhostsbak]) == 0) {
598 if (svcport_str == NULL) {
599 if ((res = malloc(sizeof(struct addrinfo))) == NULL)
602 res->ai_flags = hints.ai_flags;
603 res->ai_family = hints.ai_family;
604 res->ai_protocol = hints.ai_protocol;
605 switch (res->ai_family) {
607 sin = malloc(sizeof(struct sockaddr_in));
610 sin->sin_family = AF_INET;
611 sin->sin_port = htons(0);
612 sin->sin_addr.s_addr = htonl(INADDR_ANY);
613 res->ai_addr = (struct sockaddr*) sin;
614 res->ai_addrlen = (socklen_t)
615 sizeof(struct sockaddr_in);
618 sin6 = malloc(sizeof(struct sockaddr_in6));
621 sin6->sin6_family = AF_INET6;
622 sin6->sin6_port = htons(0);
623 sin6->sin6_addr = in6addr_any;
624 res->ai_addr = (struct sockaddr*) sin6;
625 res->ai_addrlen = (socklen_t)
626 sizeof(struct sockaddr_in6);
630 "bad address family %d",
635 if ((aicode = getaddrinfo(NULL, svcport_str,
636 &hints, &res)) != 0) {
638 "cannot get local address for %s: %s",
640 gai_strerror(aicode));
647 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
648 &hints, &res)) != 0) {
650 "cannot get local address for %s: %s",
651 nconf->nc_netid, gai_strerror(aicode));
659 struct netbuf servaddr;
663 * Look up addresses for the kernel to create transports for.
665 servaddr.len = servaddr.maxlen = res->ai_addrlen;
666 servaddr.buf = res->ai_addr;
667 uaddr = taddr2uaddr(nconf, &servaddr);
669 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
672 addrs[2 * naddrs] = strdup(nconf->nc_netid);
673 addrs[2 * naddrs + 1] = uaddr;
677 sock_fd[sock_fdcnt - 1] = fd;
679 /* Now, attempt the bind. */
680 r = bindresvport_sa(fd, res->ai_addr);
682 if (errno == EADDRINUSE && mallocd_svcport != 0) {
683 if (mallocd_res != 0) {
690 syslog(LOG_ERR, "bindresvport_sa: %m");
694 if (svcport_str == NULL) {
695 svcport_str = malloc(NI_MAXSERV * sizeof(char));
696 if (svcport_str == NULL)
700 if (getnameinfo(res->ai_addr,
701 res->ai_addr->sa_len, NULL, NI_MAXHOST,
702 svcport_str, NI_MAXSERV * sizeof(char),
703 NI_NUMERICHOST | NI_NUMERICSERV))
704 errx(1, "Cannot get port number");
708 if (mallocd_res != 0) {
719 * Called after all the create_service() calls have succeeded, to complete
720 * the setup and registration.
723 complete_service(struct netconfig *nconf, char *port_str)
725 struct addrinfo hints, *res = NULL;
726 struct __rpc_sockinfo si;
727 struct netbuf servaddr;
728 SVCXPRT *transp = NULL;
729 int aicode, fd, nhostsbak;
732 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
733 (nconf->nc_semantics != NC_TPI_COTS) &&
734 (nconf->nc_semantics != NC_TPI_COTS_ORD))
735 return; /* not my type */
738 * XXX - using RPC library internal functions.
740 if (!__rpc_nconf2sockinfo(nconf, &si)) {
741 syslog(LOG_ERR, "cannot get information for %s",
747 while (nhostsbak > 0) {
749 if (sock_fdpos >= sock_fdcnt) {
750 /* Should never happen. */
751 syslog(LOG_ERR, "Ran out of socket fd's");
754 fd = sock_fd[sock_fdpos++];
758 if (nconf->nc_semantics != NC_TPI_CLTS)
759 listen(fd, SOMAXCONN);
761 transp = svc_tli_create(fd, nconf, NULL,
762 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
764 if (transp != (SVCXPRT *) NULL) {
765 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
768 "can't register %s NLM_PROG, NLM_SM service",
771 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
774 "can't register %s NLM_PROG, NLM_VERS service",
777 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
780 "can't register %s NLM_PROG, NLM_VERSX service",
783 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
786 "can't register %s NLM_PROG, NLM_VERS4 service",
790 syslog(LOG_WARNING, "can't create %s services",
793 if (registered == 0) {
795 memset(&hints, 0, sizeof hints);
796 hints.ai_flags = AI_PASSIVE;
797 hints.ai_family = si.si_af;
798 hints.ai_socktype = si.si_socktype;
799 hints.ai_protocol = si.si_proto;
801 if ((aicode = getaddrinfo(NULL, port_str, &hints,
803 syslog(LOG_ERR, "cannot get local address: %s",
804 gai_strerror(aicode));
808 servaddr.buf = malloc(res->ai_addrlen);
809 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
810 servaddr.len = res->ai_addrlen;
812 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
813 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
814 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
815 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
824 * Clear out sockets after a failure to bind one of them, so that the
825 * cycle of socket creation/binding can start anew.
828 clearout_service(void)
832 for (i = 0; i < sock_fdcnt; i++) {
833 if (sock_fd[i] >= 0) {
834 shutdown(sock_fd[i], SHUT_RDWR);
841 sigalarm_handler(void)
850 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
851 " [-F] [-g <grace period>] [-h <bindip>] [-p <port>]");
856 * Reset the NSM state-of-the-world and acquire its state.
864 char name[] = "NFS NLM";
868 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
869 * as I know. Leave it empty for now.
871 memset(&id, 0, sizeof(id));
876 * The statd program must already be registered when lockd runs.
879 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
880 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
881 if (ret == RPC_PROGUNAVAIL) {
882 syslog(LOG_WARNING, "%lu %s", SM_PROG,
891 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
895 nsm_state = stat.state;
897 /* setup constant data for SM_MON calls */
898 mon_host.mon_id.my_id.my_name = localhost;
899 mon_host.mon_id.my_id.my_prog = NLM_PROG;
900 mon_host.mon_id.my_id.my_vers = NLM_SM;
901 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
905 * Out of memory, fatal
909 syslog(LOG_ERR, "out of memory");