xref: /freebsd/usr.sbin/rpcbind/rpcbind.c (revision d8a99eaa1f06bc3b3c017dce3e6ddf6b1fa37bcb)
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 ((strcmp(nconf->nc_netid, "local") != 0) &&
420 		    (strcmp(nconf->nc_netid, "unix") != 0)) {
421 		    if ((aicode = getaddrinfo(hosts[nhostsbak],
422 			servname, &hints, &res)) != 0) {
423 			syslog(LOG_ERR,
424 			    "cannot get local address for %s: %s",
425 			    nconf->nc_netid, gai_strerror(aicode));
426 			continue;
427 		    }
428 		    addrlen = res->ai_addrlen;
429 		    sa = (struct sockaddr *)res->ai_addr;
430 		}
431 		oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
432 		if (bind(fd, sa, addrlen) != 0) {
433 		    syslog(LOG_ERR, "cannot bind %s on %s: %m",
434 			(hosts[nhostsbak] == NULL) ? "*" :
435 			    hosts[nhostsbak], nconf->nc_netid);
436 		    if (res != NULL)
437 			freeaddrinfo(res);
438 		    continue;
439 		} else
440 		    bound = 1;
441 		(void)umask(oldmask);
442 
443 		/* Copy the address */
444 		taddr.addr.len = taddr.addr.maxlen = addrlen;
445 		taddr.addr.buf = malloc(addrlen);
446 		if (taddr.addr.buf == NULL) {
447 		    syslog(LOG_ERR,
448 			"cannot allocate memory for %s address",
449 			nconf->nc_netid);
450 		    if (res != NULL)
451 			freeaddrinfo(res);
452 		    return 1;
453 		}
454 		memcpy(taddr.addr.buf, sa, addrlen);
455 #ifdef ND_DEBUG
456 		if (debugging) {
457 		    /*
458 		     * for debugging print out our universal
459 		     * address
460 		     */
461 		    char *uaddr;
462 		    struct netbuf nb;
463 
464 		    nb.buf = sa;
465 		    nb.len = nb.maxlen = sa->sa_len;
466 		    uaddr = taddr2uaddr(nconf, &nb);
467 		    (void)fprintf(stderr,
468 			"rpcbind : my address is %s\n", uaddr);
469 		    (void)free(uaddr);
470 		}
471 #endif
472 
473 		if (nconf->nc_semantics != NC_TPI_CLTS)
474 		    listen(fd, SOMAXCONN);
475 
476 		my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
477 		    RPC_MAXDATASIZE, RPC_MAXDATASIZE);
478 		if (my_xprt == (SVCXPRT *)NULL) {
479 		    syslog(LOG_ERR, "%s: could not create service",
480 			nconf->nc_netid);
481 		    goto error;
482 		}
483 	    }
484 	    if (!bound)
485 		return 1;
486 	} else {
487 	    oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
488 	    if (bind(fd, sa, addrlen) < 0) {
489 		syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
490 		if (res != NULL)
491 		    freeaddrinfo(res);
492 		return 1;
493 	    }
494 	    (void) umask(oldmask);
495 
496 	    /* Copy the address */
497 	    taddr.addr.len = taddr.addr.maxlen = addrlen;
498 	    taddr.addr.buf = malloc(addrlen);
499 	    if (taddr.addr.buf == NULL) {
500 		syslog(LOG_ERR, "cannot allocate memory for %s address",
501 		    nconf->nc_netid);
502 		if (res != NULL)
503 		    freeaddrinfo(res);
504 		return 1;
505 	    }
506 	    memcpy(taddr.addr.buf, sa, addrlen);
507 #ifdef ND_DEBUG
508 	    if (debugging) {
509 		/* for debugging print out our universal address */
510 		char *uaddr;
511 		struct netbuf nb;
512 
513 		nb.buf = sa;
514 		nb.len = nb.maxlen = sa->sa_len;
515 		uaddr = taddr2uaddr(nconf, &nb);
516 		(void) fprintf(stderr, "rpcbind : my address is %s\n",
517 		    uaddr);
518 		(void) free(uaddr);
519 	    }
520 #endif
521 
522 	    if (nconf->nc_semantics != NC_TPI_CLTS)
523 		listen(fd, SOMAXCONN);
524 
525 	    my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
526 		RPC_MAXDATASIZE, RPC_MAXDATASIZE);
527 	    if (my_xprt == (SVCXPRT *)NULL) {
528 		syslog(LOG_ERR, "%s: could not create service",
529 		    nconf->nc_netid);
530 		goto error;
531 	    }
532 	}
533 
534 #ifdef PORTMAP
535 	/*
536 	 * Register both the versions for tcp/ip, udp/ip and local.
537 	 */
538 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
539 		(strcmp(nconf->nc_proto, NC_TCP) == 0 ||
540 		strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
541 		(strcmp(nconf->nc_netid, "unix") == 0) ||
542 		(strcmp(nconf->nc_netid, "local") == 0)) {
543 		struct pmaplist *pml;
544 
545 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
546 			pmap_service, 0)) {
547 			syslog(LOG_ERR, "could not register on %s",
548 					nconf->nc_netid);
549 			goto error;
550 		}
551 		pml = malloc(sizeof (struct pmaplist));
552 		if (pml == NULL) {
553 			syslog(LOG_ERR, "no memory!");
554 			exit(1);
555 		}
556 		pml->pml_map.pm_prog = PMAPPROG;
557 		pml->pml_map.pm_vers = PMAPVERS;
558 		pml->pml_map.pm_port = PMAPPORT;
559 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
560 			if (tcptrans[0]) {
561 				free(pml);
562 				pml = NULL;
563 				syslog(LOG_ERR,
564 				"cannot have more than one TCP transport");
565 				goto error;
566 			}
567 			tcptrans = strdup(nconf->nc_netid);
568 			pml->pml_map.pm_prot = IPPROTO_TCP;
569 
570 			/* Let's snarf the universal address */
571 			/* "h1.h2.h3.h4.p1.p2" */
572 			tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
573 		} else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
574 			if (udptrans[0]) {
575 				syslog(LOG_ERR,
576 				"cannot have more than one UDP transport");
577 				goto error;
578 			}
579 			udptrans = strdup(nconf->nc_netid);
580 			pml->pml_map.pm_prot = IPPROTO_UDP;
581 
582 			/* Let's snarf the universal address */
583 			/* "h1.h2.h3.h4.p1.p2" */
584 			udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
585 		} else if (strcmp(nconf->nc_netid, "local") == 0)
586 			pml->pml_map.pm_prot = IPPROTO_ST;
587 		else if (strcmp(nconf->nc_netid, "unix") == 0)
588 			pml->pml_map.pm_prot = IPPROTO_ST;
589 		pml->pml_next = list_pml;
590 		list_pml = pml;
591 
592 		/* Add version 3 information */
593 		pml = malloc(sizeof (struct pmaplist));
594 		if (pml == NULL) {
595 			syslog(LOG_ERR, "no memory!");
596 			exit(1);
597 		}
598 		pml->pml_map = list_pml->pml_map;
599 		pml->pml_map.pm_vers = RPCBVERS;
600 		pml->pml_next = list_pml;
601 		list_pml = pml;
602 
603 		/* Add version 4 information */
604 		pml = malloc (sizeof (struct pmaplist));
605 		if (pml == NULL) {
606 			syslog(LOG_ERR, "no memory!");
607 			exit(1);
608 		}
609 		pml->pml_map = list_pml->pml_map;
610 		pml->pml_map.pm_vers = RPCBVERS4;
611 		pml->pml_next = list_pml;
612 		list_pml = pml;
613 
614 		/* Also add version 2 stuff to rpcbind list */
615 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
616 	}
617 #endif
618 
619 	/* version 3 registration */
620 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
621 		syslog(LOG_ERR, "could not register %s version 3",
622 				nconf->nc_netid);
623 		goto error;
624 	}
625 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
626 
627 	/* version 4 registration */
628 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
629 		syslog(LOG_ERR, "could not register %s version 4",
630 				nconf->nc_netid);
631 		goto error;
632 	}
633 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
634 
635 	/* decide if bound checking works for this transport */
636 	status = add_bndlist(nconf, &taddr.addr);
637 #ifdef BIND_DEBUG
638 	if (debugging) {
639 		if (status < 0) {
640 			fprintf(stderr, "Error in finding bind status for %s\n",
641 				nconf->nc_netid);
642 		} else if (status == 0) {
643 			fprintf(stderr, "check binding for %s\n",
644 				nconf->nc_netid);
645 		} else if (status > 0) {
646 			fprintf(stderr, "No check binding for %s\n",
647 				nconf->nc_netid);
648 		}
649 	}
650 #endif
651 	/*
652 	 * rmtcall only supported on CLTS transports for now.
653 	 */
654 	if (nconf->nc_semantics == NC_TPI_CLTS) {
655 		status = create_rmtcall_fd(nconf);
656 
657 #ifdef BIND_DEBUG
658 		if (debugging) {
659 			if (status < 0) {
660 				fprintf(stderr,
661 				    "Could not create rmtcall fd for %s\n",
662 					nconf->nc_netid);
663 			} else {
664 				fprintf(stderr, "rmtcall fd for %s is %d\n",
665 					nconf->nc_netid, status);
666 			}
667 		}
668 #endif
669 	}
670 	return (0);
671 error:
672 	close(fd);
673 	return (1);
674 }
675 
676 /*
677  * Create the list of addresses that we're bound to.  Normally, this
678  * list is empty because we're listening on the wildcard address
679  * (nhost == 0).  If -h is specified on the command line, then
680  * bound_sa will have a list of the addresses that the program binds
681  * to specifically.  This function takes that list and converts them to
682  * struct sockaddr * and stores them in bound_sa.
683  */
684 static void
685 update_bound_sa(void)
686 {
687 	struct addrinfo hints, *res = NULL;
688 	int i;
689 
690 	if (nhosts == 0)
691 		return;
692 	bound_sa = malloc(sizeof(*bound_sa) * nhosts);
693 	memset(&hints, 0, sizeof(hints));
694 	hints.ai_family = PF_UNSPEC;
695 	for (i = 0; i < nhosts; i++)  {
696 		if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
697 			continue;
698 		bound_sa[i] = malloc(res->ai_addrlen);
699 		memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
700 	}
701 }
702 
703 /*
704  * Match the sa against the list of addresses we've bound to.  If
705  * we've not specifically bound to anything, we match everything.
706  * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
707  * in bound_sa, we return true.  If not, we return false.
708  */
709 int
710 listen_addr(const struct sockaddr *sa)
711 {
712 	int i;
713 
714 	/*
715 	 * If nhosts == 0, then there were no -h options on the
716 	 * command line, so all addresses are addresses we're
717 	 * listening to.
718 	 */
719 	if (nhosts == 0)
720 		return 1;
721 	for (i = 0; i < nhosts; i++) {
722 		if (bound_sa[i] == NULL ||
723 		    sa->sa_family != bound_sa[i]->sa_family)
724 			continue;
725 		switch (sa->sa_family) {
726 		case AF_INET:
727 		  	if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
728 			    sizeof(struct in_addr)) == 0)
729 				return (1);
730 			break;
731 #ifdef INET6
732 		case AF_INET6:
733 		  	if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
734 			    sizeof(struct in6_addr)) == 0)
735 				return (1);
736 			break;
737 #endif
738 		default:
739 			break;
740 		}
741 	}
742 	return (0);
743 }
744 
745 static void
746 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
747 	    struct netbuf *addr)
748 {
749 	rpcblist_ptr rbl;
750 
751 	rbl = malloc(sizeof (rpcblist));
752 	if (rbl == NULL) {
753 		syslog(LOG_ERR, "no memory!");
754 		exit(1);
755 	}
756 
757 	rbl->rpcb_map.r_prog = prog;
758 	rbl->rpcb_map.r_vers = vers;
759 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
760 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
761 	rbl->rpcb_map.r_owner = strdup(superuser);
762 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
763 	list_rbl = rbl;
764 }
765 
766 /*
767  * Catch the signal and die
768  */
769 static void
770 terminate(int signum)
771 {
772 	char c = '\0';
773 	ssize_t wr;
774 
775 	doterminate = signum;
776 	wr = write(terminate_wfd, &c, 1);
777 	if (wr < 1)
778 		_exit(2);
779 }
780 
781 void
782 rpcbind_abort(void)
783 {
784 #ifdef WARMSTART
785 	write_warmstart();	/* Dump yourself */
786 #endif
787 	abort();
788 }
789 
790 /* get command line options */
791 static void
792 parseargs(int argc, char *argv[])
793 {
794 	int c;
795 
796 #ifdef WARMSTART
797 #define	WSOP	"w"
798 #else
799 #define	WSOP	""
800 #endif
801 #ifdef LIBWRAP
802 #define WRAPOP	"W"
803 #else
804 #define WRAPOP	""
805 #endif
806 	while ((c = getopt(argc, argv, "6adh:IiLlNs" WRAPOP WSOP)) != -1) {
807 		switch (c) {
808 		case '6':
809 			ipv6_only = 1;
810 			break;
811 		case 'a':
812 			doabort = 1;	/* when debugging, do an abort on */
813 			break;		/* errors; for rpcbind developers */
814 					/* only! */
815 		case 'd':
816 			debugging = 1;
817 			break;
818 		case 'h':
819 			++nhosts;
820 			hosts = realloc(hosts, nhosts * sizeof(char *));
821 			if (hosts == NULL)
822 				errx(1, "Out of memory");
823 			hosts[nhosts - 1] = strdup(optarg);
824 			if (hosts[nhosts - 1] == NULL)
825 				errx(1, "Out of memory");
826 			break;
827 		case 'I':
828 			nobind_localhost = 1;
829 			break;
830 		case 'i':
831 			insecure = 1;
832 			break;
833 		case 'L':
834 			oldstyle_local = 1;
835 			break;
836 		case 'l':
837 			verboselog = 1;
838 			break;
839 		case 'N':
840 			nofork = 1;
841 			break;
842 		case 's':
843 			runasdaemon = 1;
844 			break;
845 #ifdef LIBWRAP
846 		case 'W':
847 			libwrap = 1;
848 			break;
849 #endif
850 #ifdef WARMSTART
851 		case 'w':
852 			warmstart = 1;
853 			break;
854 #endif
855 		default:	/* error */
856 			fprintf(stderr,
857 			    "usage: rpcbind [-6adIiLls%s%s] [-h bindip]\n",
858 			    WRAPOP, WSOP);
859 			exit (1);
860 		}
861 	}
862 	if (doabort && !debugging) {
863 	    fprintf(stderr,
864 		"-a (abort) specified without -d (debugging) -- ignored.\n");
865 	    doabort = 0;
866 	}
867 #undef WSOP
868 }
869 
870 void
871 reap(int dummy __unused)
872 {
873 	int save_errno = errno;
874 
875 	while (wait3(NULL, WNOHANG, NULL) > 0)
876 		;
877 	errno = save_errno;
878 }
879 
880 void
881 toggle_verboselog(int dummy __unused)
882 {
883 	verboselog = !verboselog;
884 }
885