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 void lookup_addresses(struct netconfig *nconf);
104 void out_of_mem(void);
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;
122 struct netconfig *nconf;
124 int maxrec = RPC_MAXDATASIZE;
125 in_port_t svcport = 0;
126 int attempt_cnt, port_len, port_pos, ret;
129 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
132 debug_level = atoi(optarg);
139 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);
180 if (geteuid()) { /* This command allowed only to root */
181 fprintf(stderr, "Sorry. You are not superuser\n");
185 kernel_lockd = FALSE;
186 kernel_lockd_client = FALSE;
187 if (modfind("nfslockd") < 0) {
188 if (kldload("nfslockd") < 0) {
189 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
197 if (getosreldate() >= 800040)
198 kernel_lockd_client = TRUE;
201 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
202 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
203 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
204 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
207 * Check if IPv6 support is present.
209 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
215 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
218 * If no hosts were specified, add a wildcard entry to bind to
219 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
223 hosts = malloc(sizeof(char *));
227 hosts[0] = strdup("*");
232 hosts_bak = realloc(hosts, (nhosts + 2) *
234 if (hosts_bak == NULL) {
235 for (i = 0; i < nhosts; i++)
243 hosts[nhosts - 2] = strdup("::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] = strdup("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 lookup_addresses(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. 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.
488 * Any file descriptors that have been created are stored in sock_fd and
489 * the total count of them is maintained in sock_fdcnt.
492 create_service(struct netconfig *nconf)
494 struct addrinfo hints, *res = NULL;
495 struct sockaddr_in *sin;
496 struct sockaddr_in6 *sin6;
497 struct __rpc_sockinfo si;
502 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
505 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
506 (nconf->nc_semantics != NC_TPI_COTS) &&
507 (nconf->nc_semantics != NC_TPI_COTS_ORD))
508 return (1); /* not my type */
511 * XXX - using RPC library internal functions.
513 if (!__rpc_nconf2sockinfo(nconf, &si)) {
514 syslog(LOG_ERR, "cannot get information for %s",
519 /* Get rpc.statd's address on this transport */
520 memset(&hints, 0, sizeof hints);
521 hints.ai_family = si.si_af;
522 hints.ai_socktype = si.si_socktype;
523 hints.ai_protocol = si.si_proto;
526 * Bind to specific IPs if asked to
529 while (nhostsbak > 0) {
531 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
534 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
536 hints.ai_flags = AI_PASSIVE;
539 * XXX - using RPC library internal functions.
541 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
542 syslog(LOG_ERR, "cannot create socket for %s",
547 switch (hints.ai_family) {
549 if (inet_pton(AF_INET, hosts[nhostsbak],
551 hints.ai_flags |= AI_NUMERICHOST;
554 * Skip if we have an AF_INET6 address.
556 if (inet_pton(AF_INET6, hosts[nhostsbak],
564 if (inet_pton(AF_INET6, hosts[nhostsbak],
566 hints.ai_flags |= AI_NUMERICHOST;
569 * Skip if we have an AF_INET address.
571 if (inet_pton(AF_INET, hosts[nhostsbak],
583 * If no hosts were specified, just bind to INADDR_ANY
585 if (strcmp("*", hosts[nhostsbak]) == 0) {
586 if (svcport_str == NULL) {
587 res = malloc(sizeof(struct addrinfo));
591 res->ai_flags = hints.ai_flags;
592 res->ai_family = hints.ai_family;
593 res->ai_protocol = hints.ai_protocol;
594 switch (res->ai_family) {
596 sin = malloc(sizeof(struct sockaddr_in));
599 sin->sin_family = AF_INET;
600 sin->sin_port = htons(0);
601 sin->sin_addr.s_addr = htonl(INADDR_ANY);
602 res->ai_addr = (struct sockaddr*) sin;
603 res->ai_addrlen = (socklen_t)
604 sizeof(struct sockaddr_in);
607 sin6 = malloc(sizeof(struct sockaddr_in6));
610 sin6->sin6_family = AF_INET6;
611 sin6->sin6_port = htons(0);
612 sin6->sin6_addr = in6addr_any;
613 res->ai_addr = (struct sockaddr*) sin6;
614 res->ai_addrlen = (socklen_t)
615 sizeof(struct sockaddr_in6);
624 if ((aicode = getaddrinfo(NULL, svcport_str,
625 &hints, &res)) != 0) {
627 "cannot get local address for %s: %s",
629 gai_strerror(aicode));
635 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
636 &hints, &res)) != 0) {
638 "cannot get local address for %s: %s",
639 nconf->nc_netid, gai_strerror(aicode));
647 sock_fd[sock_fdcnt - 1] = fd;
649 /* Now, attempt the bind. */
650 r = bindresvport_sa(fd, res->ai_addr);
652 if (errno == EADDRINUSE && mallocd_svcport != 0) {
653 if (mallocd_res != 0) {
660 syslog(LOG_ERR, "bindresvport_sa: %m");
664 if (svcport_str == NULL) {
665 svcport_str = malloc(NI_MAXSERV * sizeof(char));
666 if (svcport_str == NULL)
670 if (getnameinfo(res->ai_addr,
671 res->ai_addr->sa_len, NULL, NI_MAXHOST,
672 svcport_str, NI_MAXSERV * sizeof(char),
673 NI_NUMERICHOST | NI_NUMERICSERV))
674 errx(1, "Cannot get port number");
676 if (mallocd_res != 0) {
687 * Called after all the create_service() calls have succeeded, to complete
688 * the setup and registration.
691 complete_service(struct netconfig *nconf, char *port_str)
693 struct addrinfo hints, *res = NULL;
694 struct __rpc_sockinfo si;
695 struct netbuf servaddr;
696 SVCXPRT *transp = NULL;
697 int aicode, fd, nhostsbak;
700 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
701 (nconf->nc_semantics != NC_TPI_COTS) &&
702 (nconf->nc_semantics != NC_TPI_COTS_ORD))
703 return; /* not my type */
706 * XXX - using RPC library internal functions.
708 if (!__rpc_nconf2sockinfo(nconf, &si)) {
709 syslog(LOG_ERR, "cannot get information for %s",
715 while (nhostsbak > 0) {
717 if (sock_fdpos >= sock_fdcnt) {
718 /* Should never happen. */
719 syslog(LOG_ERR, "Ran out of socket fd's");
722 fd = sock_fd[sock_fdpos++];
726 if (nconf->nc_semantics != NC_TPI_CLTS)
727 listen(fd, SOMAXCONN);
729 transp = svc_tli_create(fd, nconf, NULL,
730 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
732 if (transp != (SVCXPRT *) NULL) {
733 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
736 "can't register %s NLM_PROG, NLM_SM service",
739 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
742 "can't register %s NLM_PROG, NLM_VERS service",
745 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
748 "can't register %s NLM_PROG, NLM_VERSX service",
751 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
754 "can't register %s NLM_PROG, NLM_VERS4 service",
758 syslog(LOG_WARNING, "can't create %s services",
761 if (registered == 0) {
763 memset(&hints, 0, sizeof hints);
764 hints.ai_flags = AI_PASSIVE;
765 hints.ai_family = si.si_af;
766 hints.ai_socktype = si.si_socktype;
767 hints.ai_protocol = si.si_proto;
769 if ((aicode = getaddrinfo(NULL, port_str, &hints,
771 syslog(LOG_ERR, "cannot get local address: %s",
772 gai_strerror(aicode));
776 servaddr.buf = malloc(res->ai_addrlen);
777 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
778 servaddr.len = res->ai_addrlen;
780 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
781 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
782 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
783 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
792 * Clear out sockets after a failure to bind one of them, so that the
793 * cycle of socket creation/binding can start anew.
796 clearout_service(void)
800 for (i = 0; i < sock_fdcnt; i++) {
801 if (sock_fd[i] >= 0) {
802 shutdown(sock_fd[i], SHUT_RDWR);
809 * Look up addresses for the kernel to create transports for.
812 lookup_addresses(struct netconfig *nconf)
814 struct addrinfo hints, *res = NULL;
815 struct sockaddr_in *sin;
816 struct sockaddr_in6 *sin6;
817 struct __rpc_sockinfo si;
818 struct netbuf servaddr;
821 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
824 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
825 (nconf->nc_semantics != NC_TPI_COTS) &&
826 (nconf->nc_semantics != NC_TPI_COTS_ORD))
827 return; /* not my type */
830 * XXX - using RPC library internal functions.
832 if (!__rpc_nconf2sockinfo(nconf, &si)) {
833 syslog(LOG_ERR, "cannot get information for %s",
838 /* Get rpc.statd's address on this transport */
839 memset(&hints, 0, sizeof hints);
840 hints.ai_flags = AI_PASSIVE;
841 hints.ai_family = si.si_af;
842 hints.ai_socktype = si.si_socktype;
843 hints.ai_protocol = si.si_proto;
846 * Bind to specific IPs if asked to
849 while (nhostsbak > 0) {
852 switch (hints.ai_family) {
854 if (inet_pton(AF_INET, hosts[nhostsbak],
856 hints.ai_flags &= AI_NUMERICHOST;
859 * Skip if we have an AF_INET6 address.
861 if (inet_pton(AF_INET6, hosts[nhostsbak],
868 if (inet_pton(AF_INET6, hosts[nhostsbak],
870 hints.ai_flags &= AI_NUMERICHOST;
873 * Skip if we have an AF_INET address.
875 if (inet_pton(AF_INET, hosts[nhostsbak],
886 * If no hosts were specified, just bind to INADDR_ANY
888 if (strcmp("*", hosts[nhostsbak]) == 0) {
889 if (svcport_str == NULL) {
890 res = malloc(sizeof(struct addrinfo));
893 res->ai_flags = hints.ai_flags;
894 res->ai_family = hints.ai_family;
895 res->ai_protocol = hints.ai_protocol;
896 switch (res->ai_family) {
898 sin = malloc(sizeof(struct sockaddr_in));
901 sin->sin_family = AF_INET;
902 sin->sin_port = htons(0);
903 sin->sin_addr.s_addr = htonl(INADDR_ANY);
904 res->ai_addr = (struct sockaddr*) sin;
905 res->ai_addrlen = (socklen_t)
906 sizeof(res->ai_addr);
909 sin6 = malloc(sizeof(struct sockaddr_in6));
912 sin6->sin6_family = AF_INET6;
913 sin6->sin6_port = htons(0);
914 sin6->sin6_addr = in6addr_any;
915 res->ai_addr = (struct sockaddr*) sin6;
916 res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
922 if ((aicode = getaddrinfo(NULL, svcport_str,
923 &hints, &res)) != 0) {
925 "cannot get local address for %s: %s",
927 gai_strerror(aicode));
932 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
933 &hints, &res)) != 0) {
935 "cannot get local address for %s: %s",
936 nconf->nc_netid, gai_strerror(aicode));
941 servaddr.len = servaddr.maxlen = res->ai_addr->sa_len;
942 servaddr.buf = res->ai_addr;
943 uaddr = taddr2uaddr(nconf, &servaddr);
945 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
948 addrs[2 * naddrs] = strdup(nconf->nc_netid);
949 addrs[2 * naddrs + 1] = uaddr;
955 sigalarm_handler(void)
964 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
965 " [-g <grace period>] [-h <bindip>] [-p <port>]");
970 * Reset the NSM state-of-the-world and acquire its state.
978 char name[] = "NFS NLM";
982 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
983 * as I know. Leave it empty for now.
985 memset(&id, 0, sizeof(id));
990 * The statd program must already be registered when lockd runs.
993 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
994 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
995 if (ret == RPC_PROGUNAVAIL) {
996 syslog(LOG_WARNING, "%lu %s", SM_PROG,
1005 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
1009 nsm_state = stat.state;
1011 /* setup constant data for SM_MON calls */
1012 mon_host.mon_id.my_id.my_name = localhost;
1013 mon_host.mon_id.my_id.my_prog = NLM_PROG;
1014 mon_host.mon_id.my_id.my_vers = NLM_SM;
1015 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
1019 * Out of memory, fatal
1023 syslog(LOG_ERR, "out of memory");