Home | History | Annotate | Line # | Download | only in rpcbind
rpcbind.c revision 1.27
      1 /*	$NetBSD: rpcbind.c,v 1.27 2019/01/03 19:26:50 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 		}
    639 #ifdef IPPROTO_ST
    640 		else if (strcmp(nconf->nc_netid, "local") == 0)
    641 			pml->pml_map.pm_prot = IPPROTO_ST;
    642 #endif
    643 		pml->pml_next = list_pml;
    644 		list_pml = pml;
    645 
    646 		/* Add version 3 information */
    647 		pml = malloc(sizeof(*pml));
    648 		if (pml == NULL) {
    649 			syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    650 			goto error;
    651 		}
    652 		pml->pml_map = list_pml->pml_map;
    653 		pml->pml_map.pm_vers = RPCBVERS;
    654 		pml->pml_next = list_pml;
    655 		list_pml = pml;
    656 
    657 		/* Add version 4 information */
    658 		pml = malloc(sizeof(*pml));
    659 		if (pml == NULL) {
    660 			syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    661 			goto error;
    662 		}
    663 		pml->pml_map = list_pml->pml_map;
    664 		pml->pml_map.pm_vers = RPCBVERS4;
    665 		pml->pml_next = list_pml;
    666 		list_pml = pml;
    667 
    668 		/* Also add version 2 stuff to rpcbind list */
    669 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
    670 	}
    671 #endif
    672 
    673 	/* version 3 registration */
    674 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
    675 		syslog(LOG_ERR, "Could not register %s version 3",
    676 		    nconf->nc_netid);
    677 		goto error;
    678 	}
    679 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
    680 
    681 	/* version 4 registration */
    682 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
    683 		syslog(LOG_ERR, "Could not register %s version 4",
    684 		    nconf->nc_netid);
    685 		goto error;
    686 	}
    687 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
    688 
    689 	/* decide if bound checking works for this transport */
    690 	status = add_bndlist(nconf, &taddr.addr);
    691 #ifdef RPCBIND_DEBUG
    692 	if (debugging) {
    693 		if (status < 0) {
    694 			fprintf(stderr, "Error in finding bind status for %s\n",
    695 				nconf->nc_netid);
    696 		} else if (status == 0) {
    697 			fprintf(stderr, "check binding for %s\n",
    698 				nconf->nc_netid);
    699 		} else if (status > 0) {
    700 			fprintf(stderr, "No check binding for %s\n",
    701 				nconf->nc_netid);
    702 		}
    703 	}
    704 #else
    705 	__USE(status);
    706 #endif
    707 	/*
    708 	 * rmtcall only supported on CLTS transports for now.
    709 	 */
    710 	if (nconf->nc_semantics == NC_TPI_CLTS) {
    711 		status = create_rmtcall_fd(nconf);
    712 
    713 #ifdef RPCBIND_DEBUG
    714 		if (debugging) {
    715 			if (status < 0) {
    716 				fprintf(stderr,
    717 				    "Could not create rmtcall fd for %s\n",
    718 					nconf->nc_netid);
    719 			} else {
    720 				fprintf(stderr, "rmtcall fd for %s is %d\n",
    721 					nconf->nc_netid, status);
    722 			}
    723 		}
    724 #endif
    725 	}
    726 	return (0);
    727 error:
    728 #ifdef RPCBIND_RUMP
    729 	(void)rump_sys_close(fd);
    730 #else
    731 	(void)close(fd);
    732 #endif
    733 	return (1);
    734 }
    735 
    736 /*
    737  * Create the list of addresses that we're bound to.  Normally, this
    738  * list is empty because we're listening on the wildcard address
    739  * (nhost == 0).  If -h is specified on the command line, then
    740  * bound_sa will have a list of the addresses that the program binds
    741  * to specifically.  This function takes that list and converts them to
    742  * struct sockaddr * and stores them in bound_sa.
    743  */
    744 static void
    745 update_bound_sa(void)
    746 {
    747 	struct addrinfo hints, *res = NULL;
    748 	int i;
    749 
    750 	if (nhosts == 0)
    751 		return;
    752 	bound_sa = calloc(nhosts, sizeof(*bound_sa));
    753 	if (bound_sa == NULL)
    754 		err(EXIT_FAILURE, "no space for bound address array");
    755 	memset(&hints, 0, sizeof(hints));
    756 	hints.ai_family = PF_UNSPEC;
    757 	for (i = 0; i < nhosts; i++)  {
    758 		if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
    759 			continue;
    760 		bound_sa[i] = malloc(res->ai_addrlen);
    761 		if (bound_sa[i] == NULL)
    762 		    err(EXIT_FAILURE, "no space for bound address");
    763 		memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
    764 	}
    765 }
    766 
    767 /*
    768  * Match the sa against the list of addresses we've bound to.  If
    769  * we've not specifically bound to anything, we match everything.
    770  * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
    771  * in bound_sa, we return true.  If not, we return false.
    772  */
    773 int
    774 listen_addr(const struct sockaddr *sa)
    775 {
    776 	int i;
    777 
    778 	/*
    779 	 * If nhosts == 0, then there were no -h options on the
    780 	 * command line, so all addresses are addresses we're
    781 	 * listening to.
    782 	 */
    783 	if (nhosts == 0)
    784 		return 1;
    785 	for (i = 0; i < nhosts; i++) {
    786 		if (bound_sa[i] == NULL ||
    787 		    sa->sa_family != bound_sa[i]->sa_family)
    788 			continue;
    789 		switch (sa->sa_family) {
    790 		case AF_INET:
    791 		  	if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
    792 			    sizeof(struct in_addr)) == 0)
    793 				return (1);
    794 			break;
    795 #ifdef INET6
    796 		case AF_INET6:
    797 		  	if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
    798 			    sizeof(struct in6_addr)) == 0)
    799 				return (1);
    800 			break;
    801 #endif
    802 		default:
    803 			break;
    804 		}
    805 	}
    806 	return (0);
    807 }
    808 
    809 static void
    810 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
    811 	    struct netbuf *addr)
    812 {
    813 	rpcblist_ptr rbl;
    814 
    815 	rbl = calloc(1, sizeof(*rbl));
    816 	if (rbl == NULL) {
    817 		syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    818 		return;
    819 	}
    820 
    821 	rbl->rpcb_map.r_prog = prog;
    822 	rbl->rpcb_map.r_vers = vers;
    823 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
    824 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
    825 	rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
    826 	if (rbl->rpcb_map.r_netid == NULL ||
    827 	    rbl->rpcb_map.r_addr == NULL ||
    828 	    rbl->rpcb_map.r_owner == NULL)
    829 	{
    830 	    free(rbl->rpcb_map.r_netid);
    831 	    free(rbl->rpcb_map.r_addr);
    832 	    free(rbl->rpcb_map.r_owner);
    833 	    free(rbl);
    834 	    syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    835 	    return;
    836 	}
    837 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
    838 	list_rbl = rbl;
    839 }
    840 
    841 /*
    842  * Catch the signal and die
    843  */
    844 static void
    845 terminate(int signum __unused)
    846 {
    847 #ifndef RPCBIND_RUMP
    848 	close(rpcbindlockfd);
    849 #endif
    850 #ifdef WARMSTART
    851 	syslog(LOG_ERR,
    852 	    "rpcbind terminating on signal %d. Restart with \"rpcbind -w\"",
    853 	    signum);
    854 	write_warmstart();	/* Dump yourself */
    855 #endif
    856 #ifdef RPCBIND_RUMP
    857 	exit(2);
    858 #else
    859 	exit(EXIT_FAILURE);
    860 #endif
    861 }
    862 
    863 void
    864 rpcbind_abort(void)
    865 {
    866 #ifdef WARMSTART
    867 	write_warmstart();	/* Dump yourself */
    868 #endif
    869 	abort();
    870 }
    871 
    872 #ifndef RPCBIND_RUMP
    873 /* get command line options */
    874 static void
    875 parseargs(int argc, char *argv[])
    876 {
    877 	int c;
    878 
    879 #ifdef WARMSTART
    880 #define	WSOP	"w"
    881 #else
    882 #define	WSOP	""
    883 #endif
    884 #ifdef LIBWRAP
    885 #define WRAPOP	"W"
    886 #else
    887 #define WRAPOP	""
    888 #endif
    889 	while ((c = getopt(argc, argv, "6adh:iLls" WRAPOP WSOP)) != -1) {
    890 		switch (c) {
    891 		case '6':
    892 			ipv6_only = 1;
    893 			break;
    894 		case 'a':
    895 			doabort = 1;	/* when debugging, do an abort on */
    896 			break;		/* errors; for rpcbind developers */
    897 					/* only! */
    898 		case 'd':
    899 			debugging = 1;
    900 			break;
    901 		case 'h':
    902 			++nhosts;
    903 			hosts = realloc(hosts, nhosts * sizeof(*hosts));
    904 			if (hosts == NULL)
    905 				err(EXIT_FAILURE, "Can't allocate host array");
    906 			hosts[nhosts - 1] = strdup(optarg);
    907 			if (hosts[nhosts - 1] == NULL)
    908 				err(EXIT_FAILURE, "Can't allocate host");
    909 			break;
    910 		case 'i':
    911 			insecure = 1;
    912 			break;
    913 		case 'L':
    914 			oldstyle_local = 1;
    915 			break;
    916 		case 'l':
    917 			verboselog = 1;
    918 			break;
    919 		case 's':
    920 			runasdaemon = 1;
    921 			break;
    922 #ifdef LIBWRAP
    923 		case 'W':
    924 			libwrap = 1;
    925 			break;
    926 #endif
    927 #ifdef WARMSTART
    928 		case 'w':
    929 			warmstart = 1;
    930 			break;
    931 #endif
    932 		default:	/* error */
    933 			fprintf(stderr,	"usage: rpcbind [-Idwils]\n");
    934 			fprintf(stderr,
    935 			    "Usage: %s [-6adiLls%s%s] [-h bindip]\n",
    936 			    getprogname(), WRAPOP, WSOP);
    937 			exit(EXIT_FAILURE);
    938 		}
    939 	}
    940 	if (doabort && !debugging) {
    941 	    fprintf(stderr,
    942 		"-a (abort) specified without -d (debugging) -- ignored.\n");
    943 	    doabort = 0;
    944 	}
    945 #undef WRAPOP
    946 #undef WSOP
    947 }
    948 #endif
    949 
    950 void
    951 reap(int dummy __unused)
    952 {
    953 	int save_errno = errno;
    954 
    955 	while (wait3(NULL, WNOHANG, NULL) > 0)
    956 		;
    957 	errno = save_errno;
    958 }
    959 
    960 void
    961 toggle_verboselog(int dummy __unused)
    962 {
    963 	verboselog = !verboselog;
    964 }
    965