xref: /freebsd/usr.sbin/rpcbind/rpcbind.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*	$NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
34  */
35 
36 /*
37  * rpcbind.c
38  * Implements the program, version to address mapping for rpc.
39  *
40  */
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/wait.h>
48 #include <sys/signal.h>
49 #include <sys/socket.h>
50 #include <sys/un.h>
51 #include <rpc/rpc.h>
52 #include <rpc/rpc_com.h>
53 #ifdef PORTMAP
54 #include <netinet/in.h>
55 #endif
56 #include <arpa/inet.h>
57 #include <fcntl.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <netconfig.h>
61 #include <stdlib.h>
62 #include <unistd.h>
63 #include <syslog.h>
64 #include <err.h>
65 #include <pwd.h>
66 #include <string.h>
67 #include <errno.h>
68 #include "rpcbind.h"
69 
70 /* Global variables */
71 int debugging = 0;	/* Tell me what's going on */
72 int doabort = 0;	/* When debugging, do an abort on errors */
73 int terminate_rfd;	/* Pipefd to wake on signal */
74 volatile sig_atomic_t doterminate = 0;	/* Terminal signal received */
75 rpcblist_ptr list_rbl;	/* A list of version 3/4 rpcbind services */
76 int rpcbindlockfd;
77 
78 /* who to suid to if -s is given */
79 #define RUN_AS  "daemon"
80 
81 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
82 
83 static int runasdaemon = 0;
84 int insecure = 0;
85 int oldstyle_local = 0;
86 #ifdef LIBWRAP
87 int libwrap = 0;
88 #endif
89 int nofork = 0;
90 int verboselog = 0;
91 int nobind_localhost = 0;
92 
93 static char **hosts = NULL;
94 static struct sockaddr **bound_sa;
95 static int ipv6_only = 0;
96 static int nhosts = 0;
97 static int on = 1;
98 static int terminate_wfd;
99 
100 #ifdef WARMSTART
101 /* Local Variable */
102 static int warmstart = 0;	/* Grab an old copy of registrations. */
103 #endif
104 
105 #ifdef PORTMAP
106 struct pmaplist *list_pml;	/* A list of version 2 rpcbind services */
107 char *udptrans;		/* Name of UDP transport */
108 char *tcptrans;		/* Name of TCP transport */
109 char *udp_uaddr;	/* Universal UDP address */
110 char *tcp_uaddr;	/* Universal TCP address */
111 #endif
112 static char servname[] = "rpcbind";
113 static char superuser[] = "superuser";
114 
115 int main(int, char *[]);
116 
117 static int init_transport(struct netconfig *);
118 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
119 			     struct netbuf *);
120 static void terminate(int);
121 static void parseargs(int, char *[]);
122 static void update_bound_sa(void);
123 
124 int
125 main(int argc, char *argv[])
126 {
127 	struct netconfig *nconf;
128 	void *nc_handle;	/* Net config handle */
129 	struct rlimit rl;
130 	int maxrec = RPC_MAXDATASIZE;
131 	int error, fds[2];
132 
133 	parseargs(argc, argv);
134 
135 	update_bound_sa();
136 
137 	/* Check that another rpcbind isn't already running. */
138 	if ((rpcbindlockfd = (open(RPCBINDDLOCK,
139 	    O_RDONLY|O_CREAT, 0444))) == -1)
140 		err(1, "%s", RPCBINDDLOCK);
141 
142 	if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
143 		errx(1, "another rpcbind is already running. Aborting");
144 
145 	getrlimit(RLIMIT_NOFILE, &rl);
146 	if (rl.rlim_cur < 128) {
147 		if (rl.rlim_max <= 128)
148 			rl.rlim_cur = rl.rlim_max;
149 		else
150 			rl.rlim_cur = 128;
151 		setrlimit(RLIMIT_NOFILE, &rl);
152 	}
153 	openlog("rpcbind", LOG_CONS, LOG_DAEMON);
154 	if (geteuid()) { /* This command allowed only to root */
155 		fprintf(stderr, "Sorry. You are not superuser\n");
156 		exit(1);
157 	}
158 	nc_handle = setnetconfig(); 	/* open netconfig file */
159 	if (nc_handle == NULL) {
160 		syslog(LOG_ERR, "could not read /etc/netconfig");
161 		exit(1);
162 	}
163 #ifdef PORTMAP
164 	udptrans = "";
165 	tcptrans = "";
166 #endif
167 
168 	nconf = getnetconfigent("local");
169 	if (nconf == NULL)
170 		nconf = getnetconfigent("unix");
171 	if (nconf == NULL) {
172 		syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
173 		exit(1);
174 	}
175 
176 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
177 
178 	init_transport(nconf);
179 
180 	while ((nconf = getnetconfig(nc_handle))) {
181 	    if (nconf->nc_flag & NC_VISIBLE) {
182 	    	if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
183 		    "inet") == 0) {
184 		    /* DO NOTHING */
185 		} else
186 		    init_transport(nconf);
187 	    }
188 	}
189 	endnetconfig(nc_handle);
190 
191 	/*
192 	 * Allocate pipe fd to wake main thread from signal handler in non-racy
193 	 * way.
194 	 */
195 	error = pipe(fds);
196 	if (error != 0)
197 		err(1, "pipe failed");
198 	terminate_rfd = fds[0];
199 	terminate_wfd = fds[1];
200 
201 	/* catch the usual termination signals for graceful exit */
202 	(void) signal(SIGCHLD, reap);
203 	(void) signal(SIGINT, terminate);
204 	(void) signal(SIGTERM, terminate);
205 	(void) signal(SIGQUIT, terminate);
206 	/* ignore others that could get sent */
207 	(void) signal(SIGPIPE, SIG_IGN);
208 	(void) signal(SIGHUP, SIG_IGN);
209 	(void) signal(SIGUSR1, SIG_IGN);
210 	(void) signal(SIGUSR2, SIG_IGN);
211 #ifdef WARMSTART
212 	if (warmstart) {
213 		read_warmstart();
214 	}
215 #endif
216 	if (debugging) {
217 		printf("rpcbind debugging enabled.");
218 		if (doabort) {
219 			printf("  Will abort on errors!\n");
220 		} else {
221 			printf("\n");
222 		}
223 	} else if (!nofork) {
224 		if (daemon(0, 0))
225 			err(1, "fork failed");
226 	}
227 
228 	if (runasdaemon) {
229 		struct passwd *p;
230 
231 		if((p = getpwnam(RUN_AS)) == NULL) {
232 			syslog(LOG_ERR, "cannot get uid of daemon: %m");
233 			exit(1);
234 		}
235 		if (setuid(p->pw_uid) == -1) {
236 			syslog(LOG_ERR, "setuid to daemon failed: %m");
237 			exit(1);
238 		}
239 	}
240 
241 	network_init();
242 
243 	my_svc_run();
244 	syslog(LOG_ERR, "svc_run returned unexpectedly");
245 	rpcbind_abort();
246 	/* NOTREACHED */
247 
248 	return 0;
249 }
250 
251 /*
252  * Adds the entry into the rpcbind database.
253  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
254  * Returns 0 if succeeds, else fails
255  */
256 static int
257 init_transport(struct netconfig *nconf)
258 {
259 	int fd;
260 	struct t_bind taddr;
261 	struct addrinfo hints, *res = NULL;
262 	struct __rpc_sockinfo si;
263 	SVCXPRT	*my_xprt;
264 	int status;	/* bound checking ? */
265 	int aicode;
266 	int addrlen;
267 	int nhostsbak;
268 	int bound;
269 	struct sockaddr *sa;
270 	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
271 	struct sockaddr_un sun;
272 	mode_t oldmask;
273 
274 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
275 	    (nconf->nc_semantics != NC_TPI_COTS) &&
276 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
277 	    return (1);	/* not my type */
278 #ifdef ND_DEBUG
279 	if (debugging) {
280 	    int i;
281 	    char **s;
282 
283 	    (void)fprintf(stderr, "%s: %ld lookup routines :\n",
284 		nconf->nc_netid, nconf->nc_nlookups);
285 	    for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
286 		i++, s++)
287 		fprintf(stderr, "[%d] - %s\n", i, *s);
288 	}
289 #endif
290 
291 	/*
292 	 * XXX - using RPC library internal functions.
293 	 */
294 	if ((strcmp(nconf->nc_netid, "local") == 0) ||
295 	    (strcmp(nconf->nc_netid, "unix") == 0)) {
296 	    /*
297 	     * For other transports we call this later, for each socket we
298 	     * like to bind.
299 	     */
300 	    if ((fd = __rpc_nconf2fd(nconf)) < 0) {
301 		int non_fatal = 0;
302 		if (errno == EAFNOSUPPORT)
303 		    non_fatal = 1;
304 		syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
305 		    nconf->nc_netid);
306 		return (1);
307 	    }
308 	}
309 
310 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
311 	    syslog(LOG_ERR, "cannot get information for %s",
312 		nconf->nc_netid);
313 	    return (1);
314 	}
315 
316 	if ((strcmp(nconf->nc_netid, "local") == 0) ||
317 	    (strcmp(nconf->nc_netid, "unix") == 0)) {
318 	    memset(&sun, 0, sizeof sun);
319 	    sun.sun_family = AF_LOCAL;
320 	    unlink(_PATH_RPCBINDSOCK);
321 	    strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
322 	    sun.sun_len = SUN_LEN(&sun);
323 	    addrlen = sizeof (struct sockaddr_un);
324 	    sa = (struct sockaddr *)&sun;
325 	} else {
326 	    /* Get rpcbind's address on this transport */
327 
328 	    memset(&hints, 0, sizeof hints);
329 	    hints.ai_flags = AI_PASSIVE;
330 	    hints.ai_family = si.si_af;
331 	    hints.ai_socktype = si.si_socktype;
332 	    hints.ai_protocol = si.si_proto;
333 	}
334 
335 	if ((strcmp(nconf->nc_netid, "local") != 0) &&
336 	    (strcmp(nconf->nc_netid, "unix") != 0)) {
337 	    /*
338 	     * If no hosts were specified, just bind to INADDR_ANY.
339 	     * Otherwise  make sure 127.0.0.1 is added to the list.
340 	     */
341 	    nhostsbak = nhosts + 1;
342 	    hosts = realloc(hosts, nhostsbak * sizeof(char *));
343 	    if (nhostsbak == 1)
344 	        hosts[0] = "*";
345 	    else {
346 		if (hints.ai_family == AF_INET && nobind_localhost == 0) {
347 		    hosts[nhostsbak - 1] = "127.0.0.1";
348 		} else if (hints.ai_family == AF_INET6 && nobind_localhost == 0) {
349 		    hosts[nhostsbak - 1] = "::1";
350 		} else
351 		    return 1;
352 	    }
353 
354 	    /*
355 	     * Bind to specific IPs if asked to
356 	     */
357 	    bound = 0;
358 	    while (nhostsbak > 0) {
359 		--nhostsbak;
360 		/*
361 		 * XXX - using RPC library internal functions.
362 		 */
363 		if ((fd = __rpc_nconf2fd(nconf)) < 0) {
364 		    int non_fatal = 0;
365 		    if (errno == EAFNOSUPPORT &&
366 			nconf->nc_semantics != NC_TPI_CLTS)
367 			non_fatal = 1;
368 		    syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
369 			"cannot create socket for %s", nconf->nc_netid);
370 		    return (1);
371 		}
372 		switch (hints.ai_family) {
373 		case AF_INET:
374 		    if (inet_pton(AF_INET, hosts[nhostsbak],
375 			host_addr) == 1) {
376 			hints.ai_flags &= AI_NUMERICHOST;
377 		    } else {
378 			/*
379 			 * Skip if we have an AF_INET6 address.
380 			 */
381 			if (inet_pton(AF_INET6,
382 			    hosts[nhostsbak], host_addr) == 1) {
383 			    close(fd);
384 			    continue;
385 			}
386 		    }
387 		    break;
388 		case AF_INET6:
389 		    if (inet_pton(AF_INET6, hosts[nhostsbak],
390 			host_addr) == 1) {
391 			hints.ai_flags &= AI_NUMERICHOST;
392 		    } else {
393 			/*
394 			 * Skip if we have an AF_INET address.
395 			 */
396 			if (inet_pton(AF_INET, hosts[nhostsbak],
397 			    host_addr) == 1) {
398 				close(fd);
399 				continue;
400 			}
401 		    }
402 		    if (setsockopt(fd, IPPROTO_IPV6,
403 			IPV6_V6ONLY, &on, sizeof on) < 0) {
404 			syslog(LOG_ERR,
405 			    "can't set v6-only binding for "
406 			    "ipv6 socket: %m");
407 			continue;
408 		    }
409 		    break;
410 		default:
411 		    break;
412 		}
413 
414 		/*
415 		 * If no hosts were specified, just bind to INADDR_ANY
416 		 */
417 		if (strcmp("*", hosts[nhostsbak]) == 0)
418 		    hosts[nhostsbak] = NULL;
419 		if ((aicode = getaddrinfo(hosts[nhostsbak], servname, &hints,
420 		    &res)) != 0) {
421 			syslog(LOG_ERR, "cannot get local address for %s: %s",
422 			    nconf->nc_netid, gai_strerror(aicode));
423 			continue;
424 		}
425 		addrlen = res->ai_addrlen;
426 		sa = (struct sockaddr *)res->ai_addr;
427 		oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
428 		if (bind(fd, sa, addrlen) != 0) {
429 		    syslog(LOG_ERR, "cannot bind %s on %s: %m",
430 			(hosts[nhostsbak] == NULL) ? "*" :
431 			    hosts[nhostsbak], nconf->nc_netid);
432 		    if (res != NULL)
433 			freeaddrinfo(res);
434 		    continue;
435 		} else
436 		    bound = 1;
437 		(void)umask(oldmask);
438 
439 		/* Copy the address */
440 		taddr.addr.len = taddr.addr.maxlen = addrlen;
441 		taddr.addr.buf = malloc(addrlen);
442 		if (taddr.addr.buf == NULL) {
443 		    syslog(LOG_ERR,
444 			"cannot allocate memory for %s address",
445 			nconf->nc_netid);
446 		    if (res != NULL)
447 			freeaddrinfo(res);
448 		    return 1;
449 		}
450 		memcpy(taddr.addr.buf, sa, addrlen);
451 #ifdef ND_DEBUG
452 		if (debugging) {
453 		    /*
454 		     * for debugging print out our universal
455 		     * address
456 		     */
457 		    char *uaddr;
458 		    struct netbuf nb;
459 
460 		    nb.buf = sa;
461 		    nb.len = nb.maxlen = sa->sa_len;
462 		    uaddr = taddr2uaddr(nconf, &nb);
463 		    (void)fprintf(stderr,
464 			"rpcbind : my address is %s\n", uaddr);
465 		    (void)free(uaddr);
466 		}
467 #endif
468 
469 		if (nconf->nc_semantics != NC_TPI_CLTS)
470 		    listen(fd, SOMAXCONN);
471 
472 		my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
473 		    RPC_MAXDATASIZE, RPC_MAXDATASIZE);
474 		if (my_xprt == (SVCXPRT *)NULL) {
475 		    syslog(LOG_ERR, "%s: could not create service",
476 			nconf->nc_netid);
477 		    goto error;
478 		}
479 	    }
480 	    if (!bound)
481 		return 1;
482 	} else {
483 	    oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
484 	    if (bind(fd, sa, addrlen) < 0) {
485 		syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
486 		if (res != NULL)
487 		    freeaddrinfo(res);
488 		return 1;
489 	    }
490 	    (void) umask(oldmask);
491 
492 	    /* Copy the address */
493 	    taddr.addr.len = taddr.addr.maxlen = addrlen;
494 	    taddr.addr.buf = malloc(addrlen);
495 	    if (taddr.addr.buf == NULL) {
496 		syslog(LOG_ERR, "cannot allocate memory for %s address",
497 		    nconf->nc_netid);
498 		if (res != NULL)
499 		    freeaddrinfo(res);
500 		return 1;
501 	    }
502 	    memcpy(taddr.addr.buf, sa, addrlen);
503 #ifdef ND_DEBUG
504 	    if (debugging) {
505 		/* for debugging print out our universal address */
506 		char *uaddr;
507 		struct netbuf nb;
508 
509 		nb.buf = sa;
510 		nb.len = nb.maxlen = sa->sa_len;
511 		uaddr = taddr2uaddr(nconf, &nb);
512 		(void) fprintf(stderr, "rpcbind : my address is %s\n",
513 		    uaddr);
514 		(void) free(uaddr);
515 	    }
516 #endif
517 
518 	    if (nconf->nc_semantics != NC_TPI_CLTS)
519 		listen(fd, SOMAXCONN);
520 
521 	    my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
522 		RPC_MAXDATASIZE, RPC_MAXDATASIZE);
523 	    if (my_xprt == (SVCXPRT *)NULL) {
524 		syslog(LOG_ERR, "%s: could not create service",
525 		    nconf->nc_netid);
526 		goto error;
527 	    }
528 	}
529 
530 #ifdef PORTMAP
531 	/*
532 	 * Register both the versions for tcp/ip, udp/ip and local.
533 	 */
534 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
535 		(strcmp(nconf->nc_proto, NC_TCP) == 0 ||
536 		strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
537 		(strcmp(nconf->nc_netid, "unix") == 0) ||
538 		(strcmp(nconf->nc_netid, "local") == 0)) {
539 		struct pmaplist *pml;
540 
541 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
542 			pmap_service, 0)) {
543 			syslog(LOG_ERR, "could not register on %s",
544 					nconf->nc_netid);
545 			goto error;
546 		}
547 		pml = malloc(sizeof (struct pmaplist));
548 		if (pml == NULL) {
549 			syslog(LOG_ERR, "no memory!");
550 			exit(1);
551 		}
552 		pml->pml_map.pm_prog = PMAPPROG;
553 		pml->pml_map.pm_vers = PMAPVERS;
554 		pml->pml_map.pm_port = PMAPPORT;
555 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
556 			if (tcptrans[0]) {
557 				free(pml);
558 				pml = NULL;
559 				syslog(LOG_ERR,
560 				"cannot have more than one TCP transport");
561 				goto error;
562 			}
563 			tcptrans = strdup(nconf->nc_netid);
564 			pml->pml_map.pm_prot = IPPROTO_TCP;
565 
566 			/* Let's snarf the universal address */
567 			/* "h1.h2.h3.h4.p1.p2" */
568 			tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
569 		} else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
570 			if (udptrans[0]) {
571 				syslog(LOG_ERR,
572 				"cannot have more than one UDP transport");
573 				goto error;
574 			}
575 			udptrans = strdup(nconf->nc_netid);
576 			pml->pml_map.pm_prot = IPPROTO_UDP;
577 
578 			/* Let's snarf the universal address */
579 			/* "h1.h2.h3.h4.p1.p2" */
580 			udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
581 		} else if (strcmp(nconf->nc_netid, "local") == 0)
582 			pml->pml_map.pm_prot = IPPROTO_ST;
583 		else if (strcmp(nconf->nc_netid, "unix") == 0)
584 			pml->pml_map.pm_prot = IPPROTO_ST;
585 		pml->pml_next = list_pml;
586 		list_pml = pml;
587 
588 		/* Add version 3 information */
589 		pml = malloc(sizeof (struct pmaplist));
590 		if (pml == NULL) {
591 			syslog(LOG_ERR, "no memory!");
592 			exit(1);
593 		}
594 		pml->pml_map = list_pml->pml_map;
595 		pml->pml_map.pm_vers = RPCBVERS;
596 		pml->pml_next = list_pml;
597 		list_pml = pml;
598 
599 		/* Add version 4 information */
600 		pml = malloc (sizeof (struct pmaplist));
601 		if (pml == NULL) {
602 			syslog(LOG_ERR, "no memory!");
603 			exit(1);
604 		}
605 		pml->pml_map = list_pml->pml_map;
606 		pml->pml_map.pm_vers = RPCBVERS4;
607 		pml->pml_next = list_pml;
608 		list_pml = pml;
609 
610 		/* Also add version 2 stuff to rpcbind list */
611 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
612 	}
613 #endif
614 
615 	/* version 3 registration */
616 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
617 		syslog(LOG_ERR, "could not register %s version 3",
618 				nconf->nc_netid);
619 		goto error;
620 	}
621 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
622 
623 	/* version 4 registration */
624 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
625 		syslog(LOG_ERR, "could not register %s version 4",
626 				nconf->nc_netid);
627 		goto error;
628 	}
629 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
630 
631 	/* decide if bound checking works for this transport */
632 	status = add_bndlist(nconf, &taddr.addr);
633 #ifdef BIND_DEBUG
634 	if (debugging) {
635 		if (status < 0) {
636 			fprintf(stderr, "Error in finding bind status for %s\n",
637 				nconf->nc_netid);
638 		} else if (status == 0) {
639 			fprintf(stderr, "check binding for %s\n",
640 				nconf->nc_netid);
641 		} else if (status > 0) {
642 			fprintf(stderr, "No check binding for %s\n",
643 				nconf->nc_netid);
644 		}
645 	}
646 #endif
647 	/*
648 	 * rmtcall only supported on CLTS transports for now.
649 	 */
650 	if (nconf->nc_semantics == NC_TPI_CLTS) {
651 		status = create_rmtcall_fd(nconf);
652 
653 #ifdef BIND_DEBUG
654 		if (debugging) {
655 			if (status < 0) {
656 				fprintf(stderr,
657 				    "Could not create rmtcall fd for %s\n",
658 					nconf->nc_netid);
659 			} else {
660 				fprintf(stderr, "rmtcall fd for %s is %d\n",
661 					nconf->nc_netid, status);
662 			}
663 		}
664 #endif
665 	}
666 	return (0);
667 error:
668 	close(fd);
669 	return (1);
670 }
671 
672 /*
673  * Create the list of addresses that we're bound to.  Normally, this
674  * list is empty because we're listening on the wildcard address
675  * (nhost == 0).  If -h is specified on the command line, then
676  * bound_sa will have a list of the addresses that the program binds
677  * to specifically.  This function takes that list and converts them to
678  * struct sockaddr * and stores them in bound_sa.
679  */
680 static void
681 update_bound_sa(void)
682 {
683 	struct addrinfo hints, *res = NULL;
684 	int i;
685 
686 	if (nhosts == 0)
687 		return;
688 	bound_sa = malloc(sizeof(*bound_sa) * nhosts);
689 	memset(&hints, 0, sizeof(hints));
690 	hints.ai_family = PF_UNSPEC;
691 	for (i = 0; i < nhosts; i++)  {
692 		if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
693 			continue;
694 		bound_sa[i] = malloc(res->ai_addrlen);
695 		memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
696 	}
697 }
698 
699 /*
700  * Match the sa against the list of addresses we've bound to.  If
701  * we've not specifically bound to anything, we match everything.
702  * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
703  * in bound_sa, we return true.  If not, we return false.
704  */
705 int
706 listen_addr(const struct sockaddr *sa)
707 {
708 	int i;
709 
710 	/*
711 	 * If nhosts == 0, then there were no -h options on the
712 	 * command line, so all addresses are addresses we're
713 	 * listening to.
714 	 */
715 	if (nhosts == 0)
716 		return 1;
717 	for (i = 0; i < nhosts; i++) {
718 		if (bound_sa[i] == NULL ||
719 		    sa->sa_family != bound_sa[i]->sa_family)
720 			continue;
721 		switch (sa->sa_family) {
722 		case AF_INET:
723 		  	if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
724 			    sizeof(struct in_addr)) == 0)
725 				return (1);
726 			break;
727 #ifdef INET6
728 		case AF_INET6:
729 		  	if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
730 			    sizeof(struct in6_addr)) == 0)
731 				return (1);
732 			break;
733 #endif
734 		default:
735 			break;
736 		}
737 	}
738 	return (0);
739 }
740 
741 static void
742 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
743 	    struct netbuf *addr)
744 {
745 	rpcblist_ptr rbl;
746 
747 	rbl = malloc(sizeof (rpcblist));
748 	if (rbl == NULL) {
749 		syslog(LOG_ERR, "no memory!");
750 		exit(1);
751 	}
752 
753 	rbl->rpcb_map.r_prog = prog;
754 	rbl->rpcb_map.r_vers = vers;
755 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
756 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
757 	rbl->rpcb_map.r_owner = strdup(superuser);
758 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
759 	list_rbl = rbl;
760 }
761 
762 /*
763  * Catch the signal and die
764  */
765 static void
766 terminate(int signum)
767 {
768 	char c = '\0';
769 	ssize_t wr;
770 
771 	doterminate = signum;
772 	wr = write(terminate_wfd, &c, 1);
773 	if (wr < 1)
774 		_exit(2);
775 }
776 
777 void
778 rpcbind_abort(void)
779 {
780 #ifdef WARMSTART
781 	write_warmstart();	/* Dump yourself */
782 #endif
783 	abort();
784 }
785 
786 /* get command line options */
787 static void
788 parseargs(int argc, char *argv[])
789 {
790 	int c;
791 
792 #ifdef WARMSTART
793 #define	WSOP	"w"
794 #else
795 #define	WSOP	""
796 #endif
797 #ifdef LIBWRAP
798 #define WRAPOP	"W"
799 #else
800 #define WRAPOP	""
801 #endif
802 	while ((c = getopt(argc, argv, "6adh:IiLlNs" WRAPOP WSOP)) != -1) {
803 		switch (c) {
804 		case '6':
805 			ipv6_only = 1;
806 			break;
807 		case 'a':
808 			doabort = 1;	/* when debugging, do an abort on */
809 			break;		/* errors; for rpcbind developers */
810 					/* only! */
811 		case 'd':
812 			debugging = 1;
813 			break;
814 		case 'h':
815 			++nhosts;
816 			hosts = realloc(hosts, nhosts * sizeof(char *));
817 			if (hosts == NULL)
818 				errx(1, "Out of memory");
819 			hosts[nhosts - 1] = strdup(optarg);
820 			if (hosts[nhosts - 1] == NULL)
821 				errx(1, "Out of memory");
822 			break;
823 		case 'I':
824 			nobind_localhost = 1;
825 			break;
826 		case 'i':
827 			insecure = 1;
828 			break;
829 		case 'L':
830 			oldstyle_local = 1;
831 			break;
832 		case 'l':
833 			verboselog = 1;
834 			break;
835 		case 'N':
836 			nofork = 1;
837 			break;
838 		case 's':
839 			runasdaemon = 1;
840 			break;
841 #ifdef LIBWRAP
842 		case 'W':
843 			libwrap = 1;
844 			break;
845 #endif
846 #ifdef WARMSTART
847 		case 'w':
848 			warmstart = 1;
849 			break;
850 #endif
851 		default:	/* error */
852 			fprintf(stderr,
853 			    "usage: rpcbind [-6adIiLls%s%s] [-h bindip]\n",
854 			    WRAPOP, WSOP);
855 			exit (1);
856 		}
857 	}
858 	if (doabort && !debugging) {
859 	    fprintf(stderr,
860 		"-a (abort) specified without -d (debugging) -- ignored.\n");
861 	    doabort = 0;
862 	}
863 #undef WSOP
864 }
865 
866 void
867 reap(int dummy __unused)
868 {
869 	int save_errno = errno;
870 
871 	while (wait3(NULL, WNOHANG, NULL) > 0)
872 		;
873 	errno = save_errno;
874 }
875 
876 void
877 toggle_verboselog(int dummy __unused)
878 {
879 	verboselog = !verboselog;
880 }
881