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