1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
7 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the FreeBSD project
20 * 4. Neither the name of the author nor the names of any co-contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
44 * main() function for NFS lock daemon. Most of the code in this
45 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
47 * The actual program logic is in the file lock_proc.c
50 #include <sys/param.h>
51 #include <sys/linker.h>
52 #include <sys/module.h>
53 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
68 #include <netconfig.h>
72 #include <rpc/rpc_com.h>
73 #include <rpcsvc/sm_inter.h>
76 #include <rpcsvc/nlm_prot.h>
78 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
80 int debug_level = 0; /* 0 = no debugging syslog() calls */
86 int kernel_lockd_client;
89 char **hosts, *svcport_str = NULL;
90 static int mallocd_svcport = 0;
92 static int sock_fdcnt;
93 static int sock_fdpos;
96 char **addrs; /* actually (netid, uaddr) pairs */
97 int naddrs; /* count of how many (netid, uaddr) pairs */
98 char localhost[] = "localhost";
100 static int create_service(struct netconfig *nconf);
101 static void complete_service(struct netconfig *nconf, char *port_str);
102 static void clearout_service(void);
103 static void out_of_mem(void) __dead2;
107 void sigalarm_handler(void);
110 * XXX move to some header file.
112 #define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
115 main(int argc, char **argv)
119 char *endptr, **hosts_bak;
120 struct sigaction sigalarm;
121 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:Fg:h:p:")) != (-1)) {
133 debug_level = atoi(optarg);
143 grace_period = atoi(optarg);
151 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
152 if (hosts_bak == NULL) {
154 for (i = 0; i < nhosts; i++)
161 hosts[nhosts - 1] = strdup(optarg);
162 if (hosts[nhosts - 1] == NULL) {
163 for (i = 0; i < (nhosts - 1); i++)
171 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
172 if (endptr == NULL || *endptr != '\0' ||
173 svcport == 0 || svcport >= IPPORT_MAX)
175 svcport_str = strdup(optarg);
182 if (geteuid()) { /* This command allowed only to root */
183 fprintf(stderr, "Sorry. You are not superuser\n");
187 kernel_lockd = FALSE;
188 kernel_lockd_client = FALSE;
189 if (modfind("nfslockd") < 0) {
190 if (kldload("nfslockd") < 0) {
191 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
199 if (getosreldate() >= 800040)
200 kernel_lockd_client = TRUE;
203 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
204 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
205 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
206 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
209 * Check if IPv6 support is present.
211 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
217 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
220 * If no hosts were specified, add a wildcard entry to bind to
221 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
225 hosts = malloc(sizeof(char *));
229 hosts[0] = strdup("*");
233 hosts_bak = realloc(hosts, (nhosts + 2) *
235 if (hosts_bak == NULL) {
236 for (i = 0; i < nhosts; i++)
244 hosts[nhosts - 2] = strdup("::1");
246 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
247 if (hosts_bak == NULL) {
248 for (i = 0; i < nhosts; i++)
258 hosts[nhosts - 1] = strdup("127.0.0.1");
262 if (!kernel_lockd_client) {
264 * For the case where we have a kernel lockd but it
265 * doesn't provide client locking, we run a cut-down
266 * RPC service on a local-domain socket. The kernel's
267 * RPC server will pass what it can't handle (mainly
268 * client replies) down to us.
270 struct sockaddr_un sun;
274 memset(&sun, 0, sizeof sun);
275 sun.sun_family = AF_LOCAL;
276 unlink(_PATH_RPCLOCKDSOCK);
277 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
278 sun.sun_len = SUN_LEN(&sun);
279 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
281 err(1, "Can't create local lockd socket");
283 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
284 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
285 err(1, "Can't bind local lockd socket");
288 if (listen(fd, SOMAXCONN) < 0) {
289 err(1, "Can't listen on local lockd socket");
291 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
293 err(1, "Can't create transport for local lockd socket");
295 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
296 err(1, "Can't register service for local lockd socket");
301 * We need to look up the addresses so that we can
302 * hand uaddrs (ascii encoded address+port strings) to
305 nc_handle = setnetconfig();
306 while ((nconf = getnetconfig(nc_handle))) {
307 /* We want to listen only on udp6, tcp6, udp, tcp transports */
308 if (nconf->nc_flag & NC_VISIBLE) {
309 /* Skip if there's no IPv6 support */
310 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
313 create_service(nconf);
317 endnetconfig(nc_handle);
324 nc_handle = setnetconfig();
325 while ((nconf = getnetconfig(nc_handle))) {
326 /* We want to listen only on udp6, tcp6, udp, tcp transports */
327 if (nconf->nc_flag & NC_VISIBLE) {
328 /* Skip if there's no IPv6 support */
329 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
332 ret = create_service(nconf);
334 /* Ignore this call */
338 * Failed to bind port, so close
339 * off all sockets created and
340 * try again if the port# was
341 * dynamically assigned via
345 if (mallocd_svcport != 0 &&
354 "bindresvport_sa: %m");
359 * Start over at the first
365 nc_handle = setnetconfig();
367 } else if (mallocd_svcport != 0 &&
368 attempt_cnt == GETPORT_MAXTRY) {
370 * For the last attempt, allow
371 * different port #s for each
372 * nconf by saving the
373 * svcport_str and setting it
376 port_list = realloc(port_list,
379 if (port_list == NULL)
381 port_list[port_len++] =
391 * Successfully bound the ports, so call complete_service() to
392 * do the rest of the setup on the service(s).
396 nc_handle = setnetconfig();
397 while ((nconf = getnetconfig(nc_handle))) {
398 /* We want to listen only on udp6, tcp6, udp, tcp transports */
399 if (nconf->nc_flag & NC_VISIBLE) {
400 /* Skip if there's no IPv6 support */
401 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
403 } else if (port_list != NULL) {
404 if (port_pos >= port_len) {
409 complete_service(nconf,
410 port_list[port_pos++]);
412 complete_service(nconf, svcport_str);
415 endnetconfig(nc_handle);
417 if (port_list != NULL) {
418 for (port_pos = 0; port_pos < port_len; port_pos++)
419 free(port_list[port_pos]);
425 * Note that it is NOT sensible to run this program from inetd - the
426 * protocol assumes that it will run immediately at boot time.
428 if ((foreground == 0) && daemon(0, 0)) {
429 err(1, "cannot fork");
433 openlog("rpc.lockd", 0, LOG_DAEMON);
435 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
437 syslog(LOG_INFO, "Starting");
439 sigalarm.sa_handler = (sig_t) sigalarm_handler;
440 sigemptyset(&sigalarm.sa_mask);
441 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
442 sigalarm.sa_flags |= SA_RESTART;
443 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
444 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
450 if (!kernel_lockd_client) {
452 client_pid = client_request();
455 * Create a child process to enter the kernel and then
456 * wait for RPCs on our local domain socket.
459 nlm_syscall(debug_level, grace_period,
465 * The kernel lockd implementation provides
466 * both client and server so we don't need to
469 nlm_syscall(debug_level, grace_period, naddrs, addrs);
477 client_pid = client_request();
479 svc_run(); /* Should never return */
485 * This routine creates and binds sockets on the appropriate
486 * addresses if lockd for user NLM, or perform a lookup of
487 * addresses for the kernel to create transports.
489 * It gets called one time for each transport.
491 * It returns 0 upon success, 1 for ignore the call and -1 to indicate
492 * bind failed with EADDRINUSE.
494 * Any file descriptors that have been created are stored in sock_fd and
495 * the total count of them is maintained in sock_fdcnt.
498 create_service(struct netconfig *nconf)
500 struct addrinfo hints, *res = NULL;
501 struct sockaddr_in *sin;
502 struct sockaddr_in6 *sin6;
503 struct __rpc_sockinfo si;
508 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
511 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
512 (nconf->nc_semantics != NC_TPI_COTS) &&
513 (nconf->nc_semantics != NC_TPI_COTS_ORD))
514 return (1); /* not my type */
517 * XXX - using RPC library internal functions.
519 if (!__rpc_nconf2sockinfo(nconf, &si)) {
520 syslog(LOG_ERR, "cannot get information for %s",
525 /* Get rpc.statd's address on this transport */
526 memset(&hints, 0, sizeof hints);
527 hints.ai_family = si.si_af;
528 hints.ai_socktype = si.si_socktype;
529 hints.ai_protocol = si.si_proto;
532 * Bind to specific IPs if asked to
535 while (nhostsbak > 0) {
538 hints.ai_flags = AI_PASSIVE;
541 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
544 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
547 * XXX - using RPC library internal functions.
549 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
550 syslog(LOG_ERR, "cannot create socket for %s",
556 switch (hints.ai_family) {
558 if (inet_pton(AF_INET, hosts[nhostsbak],
560 hints.ai_flags |= AI_NUMERICHOST;
563 * Skip if we have an AF_INET6 address.
565 if (inet_pton(AF_INET6, hosts[nhostsbak],
574 if (inet_pton(AF_INET6, hosts[nhostsbak],
576 hints.ai_flags |= AI_NUMERICHOST;
579 * Skip if we have an AF_INET address.
581 if (inet_pton(AF_INET, hosts[nhostsbak],
594 * If no hosts were specified, just bind to INADDR_ANY
596 if (strcmp("*", hosts[nhostsbak]) == 0) {
597 if (svcport_str == NULL) {
598 if ((res = malloc(sizeof(struct addrinfo))) == NULL)
601 res->ai_flags = hints.ai_flags;
602 res->ai_family = hints.ai_family;
603 res->ai_protocol = hints.ai_protocol;
604 switch (res->ai_family) {
606 sin = malloc(sizeof(struct sockaddr_in));
609 sin->sin_family = AF_INET;
610 sin->sin_port = htons(0);
611 sin->sin_addr.s_addr = htonl(INADDR_ANY);
612 res->ai_addr = (struct sockaddr*) sin;
613 res->ai_addrlen = (socklen_t)
614 sizeof(struct sockaddr_in);
617 sin6 = malloc(sizeof(struct sockaddr_in6));
620 sin6->sin6_family = AF_INET6;
621 sin6->sin6_port = htons(0);
622 sin6->sin6_addr = in6addr_any;
623 res->ai_addr = (struct sockaddr*) sin6;
624 res->ai_addrlen = (socklen_t)
625 sizeof(struct sockaddr_in6);
629 "bad address family %d",
634 if ((aicode = getaddrinfo(NULL, svcport_str,
635 &hints, &res)) != 0) {
637 "cannot get local address for %s: %s",
639 gai_strerror(aicode));
646 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
647 &hints, &res)) != 0) {
649 "cannot get local address for %s: %s",
650 nconf->nc_netid, gai_strerror(aicode));
658 struct netbuf servaddr;
662 * Look up addresses for the kernel to create transports for.
664 servaddr.len = servaddr.maxlen = res->ai_addrlen;
665 servaddr.buf = res->ai_addr;
666 uaddr = taddr2uaddr(nconf, &servaddr);
668 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
671 addrs[2 * naddrs] = strdup(nconf->nc_netid);
672 addrs[2 * naddrs + 1] = uaddr;
676 sock_fd[sock_fdcnt - 1] = fd;
678 /* Now, attempt the bind. */
679 r = bindresvport_sa(fd, res->ai_addr);
681 if (errno == EADDRINUSE && mallocd_svcport != 0) {
682 if (mallocd_res != 0) {
689 syslog(LOG_ERR, "bindresvport_sa: %m");
693 if (svcport_str == NULL) {
694 svcport_str = malloc(NI_MAXSERV * sizeof(char));
695 if (svcport_str == NULL)
699 if (getnameinfo(res->ai_addr,
700 res->ai_addr->sa_len, NULL, NI_MAXHOST,
701 svcport_str, NI_MAXSERV * sizeof(char),
702 NI_NUMERICHOST | NI_NUMERICSERV))
703 errx(1, "Cannot get port number");
707 if (mallocd_res != 0) {
718 * Called after all the create_service() calls have succeeded, to complete
719 * the setup and registration.
722 complete_service(struct netconfig *nconf, char *port_str)
724 struct addrinfo hints, *res = NULL;
725 struct __rpc_sockinfo si;
726 struct netbuf servaddr;
727 SVCXPRT *transp = NULL;
728 int aicode, fd, nhostsbak;
731 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
732 (nconf->nc_semantics != NC_TPI_COTS) &&
733 (nconf->nc_semantics != NC_TPI_COTS_ORD))
734 return; /* not my type */
737 * XXX - using RPC library internal functions.
739 if (!__rpc_nconf2sockinfo(nconf, &si)) {
740 syslog(LOG_ERR, "cannot get information for %s",
746 while (nhostsbak > 0) {
748 if (sock_fdpos >= sock_fdcnt) {
749 /* Should never happen. */
750 syslog(LOG_ERR, "Ran out of socket fd's");
753 fd = sock_fd[sock_fdpos++];
757 if (nconf->nc_semantics != NC_TPI_CLTS)
758 listen(fd, SOMAXCONN);
760 transp = svc_tli_create(fd, nconf, NULL,
761 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
763 if (transp != (SVCXPRT *) NULL) {
764 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
767 "can't register %s NLM_PROG, NLM_SM service",
770 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
773 "can't register %s NLM_PROG, NLM_VERS service",
776 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
779 "can't register %s NLM_PROG, NLM_VERSX service",
782 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
785 "can't register %s NLM_PROG, NLM_VERS4 service",
789 syslog(LOG_WARNING, "can't create %s services",
792 if (registered == 0) {
794 memset(&hints, 0, sizeof hints);
795 hints.ai_flags = AI_PASSIVE;
796 hints.ai_family = si.si_af;
797 hints.ai_socktype = si.si_socktype;
798 hints.ai_protocol = si.si_proto;
800 if ((aicode = getaddrinfo(NULL, port_str, &hints,
802 syslog(LOG_ERR, "cannot get local address: %s",
803 gai_strerror(aicode));
807 servaddr.buf = malloc(res->ai_addrlen);
808 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
809 servaddr.len = res->ai_addrlen;
811 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
812 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
813 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
814 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
823 * Clear out sockets after a failure to bind one of them, so that the
824 * cycle of socket creation/binding can start anew.
827 clearout_service(void)
831 for (i = 0; i < sock_fdcnt; i++) {
832 if (sock_fd[i] >= 0) {
833 shutdown(sock_fd[i], SHUT_RDWR);
840 sigalarm_handler(void)
849 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
850 " [-F] [-g <grace period>] [-h <bindip>] [-p <port>]");
855 * Reset the NSM state-of-the-world and acquire its state.
863 char name[] = "NFS NLM";
867 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
868 * as I know. Leave it empty for now.
870 memset(&id, 0, sizeof(id));
875 * The statd program must already be registered when lockd runs.
878 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
879 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
880 if (ret == RPC_PROGUNAVAIL) {
881 syslog(LOG_WARNING, "%lu %s", SM_PROG,
890 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
894 nsm_state = stat.state;
896 /* setup constant data for SM_MON calls */
897 mon_host.mon_id.my_id.my_name = localhost;
898 mon_host.mon_id.my_id.my_prog = NLM_PROG;
899 mon_host.mon_id.my_id.my_vers = NLM_SM;
900 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
904 * Out of memory, fatal
909 syslog(LOG_ERR, "out of memory");