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/types.h>
50 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
64 #include <netconfig.h>
68 #include <rpc/rpc_com.h>
69 #include <rpcsvc/sm_inter.h>
72 #include <rpcsvc/nlm_prot.h>
74 int debug_level = 0; /* 0 = no debugging syslog() calls */
81 char **hosts, *svcport_str = NULL;
85 void create_service(struct netconfig *nconf);
87 void nlm_prog_0(struct svc_req *, SVCXPRT *);
88 void nlm_prog_1(struct svc_req *, SVCXPRT *);
89 void nlm_prog_3(struct svc_req *, SVCXPRT *);
90 void nlm_prog_4(struct svc_req *, SVCXPRT *);
91 void out_of_mem(void);
94 void sigalarm_handler(void);
97 main(int argc, char **argv)
101 char *endptr, **hosts_bak;
102 struct sigaction sigalarm;
103 int grace_period = 30;
104 struct netconfig *nconf;
106 int maxrec = RPC_MAXDATASIZE;
107 in_port_t svcport = 0;
109 while ((ch = getopt(argc, argv, "d:g:h:p:")) != (-1)) {
112 debug_level = atoi(optarg);
119 grace_period = atoi(optarg);
128 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
129 if (hosts_bak == NULL) {
131 for (i = 0; i < nhosts; i++)
138 hosts[nhosts - 1] = strdup(optarg);
139 if (hosts[nhosts - 1] == NULL) {
140 for (i = 0; i < (nhosts - 1); i++)
148 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
149 if (endptr == NULL || *endptr != '\0' ||
150 svcport == 0 || svcport >= IPPORT_MAX)
152 svcport_str = strdup(optarg);
160 if (geteuid()) { /* This command allowed only to root */
161 fprintf(stderr, "Sorry. You are not superuser\n");
165 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
166 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
167 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
168 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
171 * Check if IPv6 support is present.
173 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
179 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
182 * If no hosts were specified, add a wildcard entry to bind to
183 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
187 hosts = malloc(sizeof(char**));
196 hosts_bak = realloc(hosts, (nhosts + 2) *
198 if (hosts_bak == NULL) {
199 for (i = 0; i < nhosts; i++)
207 hosts[nhosts - 2] = "::1";
209 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
210 if (hosts_bak == NULL) {
211 for (i = 0; i < nhosts; i++)
221 hosts[nhosts - 1] = "127.0.0.1";
224 nc_handle = setnetconfig();
225 while ((nconf = getnetconfig(nc_handle))) {
226 /* We want to listen only on udp6, tcp6, udp, tcp transports */
227 if (nconf->nc_flag & NC_VISIBLE) {
228 /* Skip if there's no IPv6 support */
229 if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) {
232 create_service(nconf);
236 endnetconfig(nc_handle);
239 * Note that it is NOT sensible to run this program from inetd - the
240 * protocol assumes that it will run immediately at boot time.
242 if (daemon(0, debug_level > 0)) {
243 err(1, "cannot fork");
247 openlog("rpc.lockd", 0, LOG_DAEMON);
249 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
251 syslog(LOG_INFO, "Starting");
253 sigalarm.sa_handler = (sig_t) sigalarm_handler;
254 sigemptyset(&sigalarm.sa_mask);
255 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
256 sigalarm.sa_flags |= SA_RESTART;
257 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
258 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
267 client_pid = client_request();
269 svc_run(); /* Should never return */
274 * This routine creates and binds sockets on the appropriate
275 * addresses. It gets called one time for each transport and
276 * registrates the service with rpcbind on that trasport.
279 create_service(struct netconfig *nconf)
281 struct addrinfo hints, *res = NULL;
282 struct sockaddr_in *sin;
283 struct sockaddr_in6 *sin6;
284 struct __rpc_sockinfo si;
285 struct netbuf servaddr;
286 SVCXPRT *transp = NULL;
292 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
294 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
295 (nconf->nc_semantics != NC_TPI_COTS) &&
296 (nconf->nc_semantics != NC_TPI_COTS_ORD))
297 return; /* not my type */
300 * XXX - using RPC library internal functions.
302 if (!__rpc_nconf2sockinfo(nconf, &si)) {
303 syslog(LOG_ERR, "cannot get information for %s",
308 /* Get rpc.statd's address on this transport */
309 memset(&hints, 0, sizeof hints);
310 hints.ai_flags = AI_PASSIVE;
311 hints.ai_family = si.si_af;
312 hints.ai_socktype = si.si_socktype;
313 hints.ai_protocol = si.si_proto;
316 * Bind to specific IPs if asked to
319 while (nhostsbak > 0) {
323 * XXX - using RPC library internal functions.
325 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
326 syslog(LOG_ERR, "cannot create socket for %s",
331 switch (hints.ai_family) {
333 if (inet_pton(AF_INET, hosts[nhostsbak],
335 hints.ai_flags &= AI_NUMERICHOST;
338 * Skip if we have an AF_INET6 address.
340 if (inet_pton(AF_INET6, hosts[nhostsbak],
348 if (inet_pton(AF_INET6, hosts[nhostsbak],
350 hints.ai_flags &= AI_NUMERICHOST;
353 * Skip if we have an AF_INET address.
355 if (inet_pton(AF_INET, hosts[nhostsbak],
367 * If no hosts were specified, just bind to INADDR_ANY
369 if (strcmp("*", hosts[nhostsbak]) == 0) {
370 if (svcport_str == NULL) {
371 res = malloc(sizeof(struct addrinfo));
374 res->ai_flags = hints.ai_flags;
375 res->ai_family = hints.ai_family;
376 res->ai_protocol = hints.ai_protocol;
377 switch (res->ai_family) {
379 sin = malloc(sizeof(struct sockaddr_in));
382 sin->sin_family = AF_INET;
383 sin->sin_port = htons(0);
384 sin->sin_addr.s_addr = htonl(INADDR_ANY);
385 res->ai_addr = (struct sockaddr*) sin;
386 res->ai_addrlen = (socklen_t)
387 sizeof(res->ai_addr);
390 sin6 = malloc(sizeof(struct sockaddr_in6));
393 sin6->sin6_family = AF_INET6;
394 sin6->sin6_port = htons(0);
395 sin6->sin6_addr = in6addr_any;
396 res->ai_addr = (struct sockaddr*) sin6;
397 res->ai_addrlen = (socklen_t) sizeof(res->ai_addr);
403 if ((aicode = getaddrinfo(NULL, svcport_str,
404 &hints, &res)) != 0) {
406 "cannot get local address for %s: %s",
408 gai_strerror(aicode));
413 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
414 &hints, &res)) != 0) {
416 "cannot get local address for %s: %s",
417 nconf->nc_netid, gai_strerror(aicode));
422 r = bindresvport_sa(fd, res->ai_addr);
424 syslog(LOG_ERR, "bindresvport_sa: %m");
428 transp = svc_tli_create(fd, nconf, NULL,
429 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
431 if (transp != (SVCXPRT *) NULL) {
432 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0,
435 "can't register %s NLM_PROG, NLM_SM service",
438 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1,
441 "can't register %s NLM_PROG, NLM_VERS service",
444 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3,
447 "can't register %s NLM_PROG, NLM_VERSX service",
450 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4,
453 "can't register %s NLM_PROG, NLM_VERS4 service",
457 syslog(LOG_WARNING, "can't create %s services",
460 if (registered == 0) {
462 memset(&hints, 0, sizeof hints);
463 hints.ai_flags = AI_PASSIVE;
464 hints.ai_family = si.si_af;
465 hints.ai_socktype = si.si_socktype;
466 hints.ai_protocol = si.si_proto;
468 if (svcport_str == NULL) {
469 svcport_str = malloc(NI_MAXSERV * sizeof(char));
470 if (svcport_str == NULL)
473 if (getnameinfo(res->ai_addr,
474 res->ai_addr->sa_len, NULL, NI_MAXHOST,
475 svcport_str, NI_MAXSERV * sizeof(char),
476 NI_NUMERICHOST | NI_NUMERICSERV))
477 errx(1, "Cannot get port number");
480 if((aicode = getaddrinfo(NULL, svcport_str, &hints,
482 syslog(LOG_ERR, "cannot get local address: %s",
483 gai_strerror(aicode));
487 servaddr.buf = malloc(res->ai_addrlen);
488 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
489 servaddr.len = res->ai_addrlen;
491 rpcb_set(NLM_PROG, NLM_SM, nconf, &servaddr);
492 rpcb_set(NLM_PROG, NLM_VERS, nconf, &servaddr);
493 rpcb_set(NLM_PROG, NLM_VERSX, nconf, &servaddr);
494 rpcb_set(NLM_PROG, NLM_VERS4, nconf, &servaddr);
503 sigalarm_handler(void)
512 errx(1, "usage: rpc.lockd [-d <debuglevel>]"
513 " [-g <grace period>] [-h <bindip>] [-p <port>]");
518 * Reset the NSM state-of-the-world and acquire its state.
526 char name[] = "NFS NLM";
527 char localhost[] = "localhost";
531 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
532 * as I know. Leave it empty for now.
534 memset(&id, 0, sizeof(id));
539 * The statd program must already be registered when lockd runs.
542 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
543 (xdrproc_t)xdr_my_id, &id, (xdrproc_t)xdr_sm_stat, &stat);
544 if (ret == RPC_PROGUNAVAIL) {
545 syslog(LOG_WARNING, "%lu %s", SM_PROG,
554 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
558 nsm_state = stat.state;
560 /* setup constant data for SM_MON calls */
561 mon_host.mon_id.my_id.my_name = localhost;
562 mon_host.mon_id.my_id.my_prog = NLM_PROG;
563 mon_host.mon_id.my_id.my_vers = NLM_SM;
564 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
568 * Out of memory, fatal
572 syslog(LOG_ERR, "out of memory");