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