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