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 int debug_level = 0; /* 0 = no debugging syslog() calls */
85 char **hosts, *svcport_str = NULL;
88 char **addrs; /* actually (netid, uaddr) pairs */
89 int naddrs; /* count of how many (netid, uaddr) pairs */
91 void create_service(struct netconfig *nconf);
92 void lookup_addresses(struct netconfig *nconf);
94 void nlm_prog_0(struct svc_req *, SVCXPRT *);
95 void nlm_prog_1(struct svc_req *, SVCXPRT *);
96 void nlm_prog_3(struct svc_req *, SVCXPRT *);
97 void nlm_prog_4(struct svc_req *, SVCXPRT *);
98 void out_of_mem(void);
101 void sigalarm_handler(void);
104 * XXX move to some header file.
106 #define _PATH_RPCLOCKDSOCK "/var/run/rpclockd.sock"
109 main(int argc, char **argv)
113 char *endptr, **hosts_bak;
114 struct sigaction sigalarm;
115 int grace_period = 30;
116 struct netconfig *nconf;
118 int maxrec = RPC_MAXDATASIZE;
119 in_port_t svcport = 0;
121 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
124 debug_level = atoi(optarg);
131 grace_period = atoi(optarg);
140 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
141 if (hosts_bak == NULL) {
143 for (i = 0; i < nhosts; i++)
150 hosts[nhosts - 1] = strdup(optarg);
151 if (hosts[nhosts - 1] == NULL) {
152 for (i = 0; i < (nhosts - 1); i++)
160 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
161 if (endptr == NULL || *endptr != '\0' ||
162 svcport == 0 || svcport >= IPPORT_MAX)
164 svcport_str = strdup(optarg);
172 if (geteuid()) { /* This command allowed only to root */
173 fprintf(stderr, "Sorry. You are not superuser\n");
177 kernel_lockd = FALSE;
178 if (modfind("nfslockd") < 0) {
179 if (kldload("nfslockd") < 0) {
180 fprintf(stderr, "Can't find or load kernel support for rpc.lockd - using non-kernel implementation\n");
188 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
189 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
190 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
191 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
194 * Check if IPv6 support is present.
196 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
202 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
205 * If no hosts were specified, add a wildcard entry to bind to
206 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
210 hosts = malloc(sizeof(char**));
219 hosts_bak = realloc(hosts, (nhosts + 2) *
221 if (hosts_bak == NULL) {
222 for (i = 0; i < nhosts; i++)
230 hosts[nhosts - 2] = "::1";
232 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
233 if (hosts_bak == NULL) {
234 for (i = 0; i < nhosts; i++)
244 hosts[nhosts - 1] = "127.0.0.1";
249 * For the kernel lockd case, we run a cut-down RPC
250 * service on a local-domain socket. The kernel's RPC
251 * server will pass what it can't handle (mainly
252 * client replies) down to us. This can go away
253 * entirely if/when we move the client side of NFS
254 * locking into the kernel.
256 struct sockaddr_un sun;
260 memset(&sun, 0, sizeof sun);
261 sun.sun_family = AF_LOCAL;
262 unlink(_PATH_RPCLOCKDSOCK);
263 strcpy(sun.sun_path, _PATH_RPCLOCKDSOCK);
264 sun.sun_len = SUN_LEN(&sun);
265 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
267 err(1, "Can't create local lockd socket");
269 oldmask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
270 if (bind(fd, (struct sockaddr *) &sun, sun.sun_len) < 0) {
271 err(1, "Can't bind local lockd socket");
274 if (listen(fd, SOMAXCONN) < 0) {
275 err(1, "Can't listen on local lockd socket");
277 xprt = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
279 err(1, "Can't create transport for local lockd socket");
281 if (!svc_reg(xprt, NLM_PROG, NLM_VERS4, nlm_prog_4, NULL)) {
282 err(1, "Can't register service for local lockd socket");
286 * We need to look up the addresses so that we can
287 * hand uaddrs (ascii encoded address+port strings) to
290 nc_handle = setnetconfig();
291 while ((nconf = getnetconfig(nc_handle))) {
292 /* We want to listen only on udp6, tcp6, udp, tcp transports */
293 if (nconf->nc_flag & NC_VISIBLE) {
294 /* Skip if there's no IPv6 support */
295 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
298 lookup_addresses(nconf);
302 endnetconfig(nc_handle);
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 create_service(nconf);
316 endnetconfig(nc_handle);
320 * Note that it is NOT sensible to run this program from inetd - the
321 * protocol assumes that it will run immediately at boot time.
323 if (daemon(0, debug_level > 0)) {
324 err(1, "cannot fork");
328 openlog("rpc.lockd", 0, LOG_DAEMON);
330 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
332 syslog(LOG_INFO, "Starting");
334 sigalarm.sa_handler = (sig_t) sigalarm_handler;
335 sigemptyset(&sigalarm.sa_mask);
336 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
337 sigalarm.sa_flags |= SA_RESTART;
338 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
339 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
346 client_pid = client_request();
349 * Create a child process to enter the kernel and then
350 * wait for RPCs on our local domain socket.
353 nlm_syscall(debug_level, grace_period, naddrs, addrs);
362 client_pid = client_request();
364 svc_run(); /* Should never return */
370 * This routine creates and binds sockets on the appropriate
371 * addresses. It gets called one time for each transport and
372 * registrates the service with rpcbind on that trasport.
375 create_service(struct netconfig *nconf)
377 struct addrinfo hints, *res = NULL;
378 struct sockaddr_in *sin;
379 struct sockaddr_in6 *sin6;
380 struct __rpc_sockinfo si;
381 struct netbuf servaddr;
382 SVCXPRT *transp = NULL;
388 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
390 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
391 (nconf->nc_semantics != NC_TPI_COTS) &&
392 (nconf->nc_semantics != NC_TPI_COTS_ORD))
393 return; /* not my type */
396 * XXX - using RPC library internal functions.
398 if (!__rpc_nconf2sockinfo(nconf, &si)) {
399 syslog(LOG_ERR, "cannot get information for %s",
404 /* Get rpc.statd's address on this transport */
405 memset(&hints, 0, sizeof hints);
406 hints.ai_flags = AI_PASSIVE;
407 hints.ai_family = si.si_af;
408 hints.ai_socktype = si.si_socktype;
409 hints.ai_protocol = si.si_proto;
412 * Bind to specific IPs if asked to
415 while (nhostsbak > 0) {
419 * XXX - using RPC library internal functions.
421 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
422 syslog(LOG_ERR, "cannot create socket for %s",
427 switch (hints.ai_family) {
429 if (inet_pton(AF_INET, hosts[nhostsbak],
431 hints.ai_flags &= AI_NUMERICHOST;
434 * Skip if we have an AF_INET6 address.
436 if (inet_pton(AF_INET6, hosts[nhostsbak],
444 if (inet_pton(AF_INET6, hosts[nhostsbak],
446 hints.ai_flags &= AI_NUMERICHOST;
449 * Skip if we have an AF_INET address.
451 if (inet_pton(AF_INET, hosts[nhostsbak],
463 * If no hosts were specified, just bind to INADDR_ANY
465 if (strcmp("*", hosts[nhostsbak]) == 0) {
466 if (svcport_str == NULL) {
467 res = malloc(sizeof(struct addrinfo));
470 res->ai_flags = hints.ai_flags;
471 res->ai_family = hints.ai_family;
472 res->ai_protocol = hints.ai_protocol;
473 switch (res->ai_family) {
475 sin = malloc(sizeof(struct sockaddr_in));
478 sin->sin_family = AF_INET;
479 sin->sin_port = htons(0);
480 sin->sin_addr.s_addr = htonl(INADDR_ANY);
481 res->ai_addr = (struct sockaddr*) sin;
482 res->ai_addrlen = (socklen_t)
483 sizeof(res->ai_addr);
486 sin6 = malloc(sizeof(struct sockaddr_in6));
489 sin6->sin6_family = AF_INET6;
490 sin6->sin6_port = htons(0);
491 sin6->sin6_addr = in6addr_any;
492 res->ai_addr = (struct sockaddr*) sin6;
493 res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
499 if ((aicode = getaddrinfo(NULL, svcport_str,
500 &hints, &res)) != 0) {
502 "cannot get local address for %s: %s",
504 gai_strerror(aicode));
509 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
510 &hints, &res)) != 0) {
512 "cannot get local address for %s: %s",
513 nconf->nc_netid, gai_strerror(aicode));
518 r = bindresvport_sa(fd, res->ai_addr);
520 syslog(LOG_ERR, "bindresvport_sa: %m");
524 if (nconf->nc_semantics != NC_TPI_CLTS)
525 listen(fd, SOMAXCONN);
527 transp = svc_tli_create(fd, nconf, NULL,
528 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
530 if (transp != (SVCXPRT *) NULL) {
531 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
534 "can't register %s NLM_PROG, NLM_SM service",
537 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
540 "can't register %s NLM_PROG, NLM_VERS service",
543 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
546 "can't register %s NLM_PROG, NLM_VERSX service",
549 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
552 "can't register %s NLM_PROG, NLM_VERS4 service",
556 syslog(LOG_WARNING, "can't create %s services",
559 if (registered == 0) {
561 memset(&hints, 0, sizeof hints);
562 hints.ai_flags = AI_PASSIVE;
563 hints.ai_family = si.si_af;
564 hints.ai_socktype = si.si_socktype;
565 hints.ai_protocol = si.si_proto;
567 if (svcport_str == NULL) {
568 svcport_str = malloc(NI_MAXSERV * sizeof(char));
569 if (svcport_str == NULL)
572 if (getnameinfo(res->ai_addr,
573 res->ai_addr->sa_len, NULL, NI_MAXHOST,
574 svcport_str, NI_MAXSERV * sizeof(char),
575 NI_NUMERICHOST | NI_NUMERICSERV))
576 errx(1, "Cannot get port number");
579 if((aicode = getaddrinfo(NULL, svcport_str, &hints,
581 syslog(LOG_ERR, "cannot get local address: %s",
582 gai_strerror(aicode));
586 servaddr.buf = malloc(res->ai_addrlen);
587 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
588 servaddr.len = res->ai_addrlen;
590 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
591 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
592 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
593 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
602 * Look up addresses for the kernel to create transports for.
605 lookup_addresses(struct netconfig *nconf)
607 struct addrinfo hints, *res = NULL;
608 struct sockaddr_in *sin;
609 struct sockaddr_in6 *sin6;
610 struct __rpc_sockinfo si;
611 struct netbuf servaddr;
612 SVCXPRT *transp = NULL;
617 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
620 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
621 (nconf->nc_semantics != NC_TPI_COTS) &&
622 (nconf->nc_semantics != NC_TPI_COTS_ORD))
623 return; /* not my type */
626 * XXX - using RPC library internal functions.
628 if (!__rpc_nconf2sockinfo(nconf, &si)) {
629 syslog(LOG_ERR, "cannot get information for %s",
634 /* Get rpc.statd's address on this transport */
635 memset(&hints, 0, sizeof hints);
636 hints.ai_flags = AI_PASSIVE;
637 hints.ai_family = si.si_af;
638 hints.ai_socktype = si.si_socktype;
639 hints.ai_protocol = si.si_proto;
642 * Bind to specific IPs if asked to
645 while (nhostsbak > 0) {
648 switch (hints.ai_family) {
650 if (inet_pton(AF_INET, hosts[nhostsbak],
652 hints.ai_flags &= AI_NUMERICHOST;
655 * Skip if we have an AF_INET6 address.
657 if (inet_pton(AF_INET6, hosts[nhostsbak],
664 if (inet_pton(AF_INET6, hosts[nhostsbak],
666 hints.ai_flags &= AI_NUMERICHOST;
669 * Skip if we have an AF_INET address.
671 if (inet_pton(AF_INET, hosts[nhostsbak],
682 * If no hosts were specified, just bind to INADDR_ANY
684 if (strcmp("*", hosts[nhostsbak]) == 0) {
685 if (svcport_str == NULL) {
686 res = malloc(sizeof(struct addrinfo));
689 res->ai_flags = hints.ai_flags;
690 res->ai_family = hints.ai_family;
691 res->ai_protocol = hints.ai_protocol;
692 switch (res->ai_family) {
694 sin = malloc(sizeof(struct sockaddr_in));
697 sin->sin_family = AF_INET;
698 sin->sin_port = htons(0);
699 sin->sin_addr.s_addr = htonl(INADDR_ANY);
700 res->ai_addr = (struct sockaddr*) sin;
701 res->ai_addrlen = (socklen_t)
702 sizeof(res->ai_addr);
705 sin6 = malloc(sizeof(struct sockaddr_in6));
708 sin6->sin6_family = AF_INET6;
709 sin6->sin6_port = htons(0);
710 sin6->sin6_addr = in6addr_any;
711 res->ai_addr = (struct sockaddr*) sin6;
712 res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
718 if ((aicode = getaddrinfo(NULL, svcport_str,
719 &hints, &res)) != 0) {
721 "cannot get local address for %s: %s",
723 gai_strerror(aicode));
728 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
729 &hints, &res)) != 0) {
731 "cannot get local address for %s: %s",
732 nconf->nc_netid, gai_strerror(aicode));
737 servaddr.len = servaddr.maxlen = res->ai_addr->sa_len;
738 servaddr.buf = res->ai_addr;
739 uaddr = taddr2uaddr(nconf, &servaddr);
741 addrs = realloc(addrs, 2 * (naddrs + 1) * sizeof(char *));
744 addrs[2 * naddrs] = strdup(nconf->nc_netid);
745 addrs[2 * naddrs + 1] = uaddr;
751 sigalarm_handler(void)
760 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
761 " [-g <grace period>] [-h <bindip>] [-p <port>]");
766 * Reset the NSM state-of-the-world and acquire its state.
774 char name[] = "NFS NLM";
775 char localhost[] = "localhost";
779 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
780 * as I know. Leave it empty for now.
782 memset(&id, 0, sizeof(id));
787 * The statd program must already be registered when lockd runs.
790 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
791 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
792 if (ret == RPC_PROGUNAVAIL) {
793 syslog(LOG_WARNING, "%lu %s", SM_PROG,
802 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
806 nsm_state = stat.state;
808 /* setup constant data for SM_MON calls */
809 mon_host.mon_id.my_id.my_name = localhost;
810 mon_host.mon_id.my_id.my_prog = NLM_PROG;
811 mon_host.mon_id.my_id.my_vers = NLM_SM;
812 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
816 * Out of memory, fatal
820 syslog(LOG_ERR, "out of memory");