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