1*43d0286dSnia /* $NetBSD: rpcbind.c,v 1.31 2021/10/30 11:04:48 nia Exp $ */
2d687de29Sfvdl
334b7ffd9Schristos /*-
434b7ffd9Schristos * Copyright (c) 2009, Sun Microsystems, Inc.
534b7ffd9Schristos * All rights reserved.
6d687de29Sfvdl *
734b7ffd9Schristos * Redistribution and use in source and binary forms, with or without
834b7ffd9Schristos * modification, are permitted provided that the following conditions are met:
934b7ffd9Schristos * - Redistributions of source code must retain the above copyright notice,
1034b7ffd9Schristos * this list of conditions and the following disclaimer.
1134b7ffd9Schristos * - Redistributions in binary form must reproduce the above copyright notice,
1234b7ffd9Schristos * this list of conditions and the following disclaimer in the documentation
1334b7ffd9Schristos * and/or other materials provided with the distribution.
1434b7ffd9Schristos * - Neither the name of Sun Microsystems, Inc. nor the names of its
1534b7ffd9Schristos * contributors may be used to endorse or promote products derived
1634b7ffd9Schristos * from this software without specific prior written permission.
17d687de29Sfvdl *
1834b7ffd9Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1934b7ffd9Schristos * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2034b7ffd9Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2134b7ffd9Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2234b7ffd9Schristos * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2334b7ffd9Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2434b7ffd9Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2534b7ffd9Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2634b7ffd9Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2734b7ffd9Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2834b7ffd9Schristos * POSSIBILITY OF SUCH DAMAGE.
29d687de29Sfvdl */
30d687de29Sfvdl /*
31d687de29Sfvdl * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
32d687de29Sfvdl */
33d687de29Sfvdl
34d687de29Sfvdl /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
35d687de29Sfvdl
36d687de29Sfvdl #if 0
37d687de29Sfvdl #ifndef lint
38d687de29Sfvdl static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
39d687de29Sfvdl #endif
40d687de29Sfvdl #endif
41d687de29Sfvdl
42d687de29Sfvdl /*
43d687de29Sfvdl * rpcbind.c
44d687de29Sfvdl * Implements the program, version to address mapping for rpc.
45d687de29Sfvdl *
46d687de29Sfvdl */
47d687de29Sfvdl
48d687de29Sfvdl #include <sys/types.h>
49d687de29Sfvdl #include <sys/stat.h>
50d687de29Sfvdl #include <sys/errno.h>
51d687de29Sfvdl #include <sys/time.h>
52d687de29Sfvdl #include <sys/resource.h>
53d687de29Sfvdl #include <sys/wait.h>
54d687de29Sfvdl #include <sys/signal.h>
55d687de29Sfvdl #include <sys/socket.h>
56d687de29Sfvdl #include <sys/un.h>
57d687de29Sfvdl #include <rpc/rpc.h>
5834b7ffd9Schristos #include <rpc/rpc_com.h>
59d687de29Sfvdl #ifdef PORTMAP
60d687de29Sfvdl #include <netinet/in.h>
61d687de29Sfvdl #endif
6234b7ffd9Schristos #include <arpa/inet.h>
6334b7ffd9Schristos #include <fcntl.h>
64d687de29Sfvdl #include <netdb.h>
65d687de29Sfvdl #include <stdio.h>
66d687de29Sfvdl #include <netconfig.h>
67d687de29Sfvdl #include <stdlib.h>
68d687de29Sfvdl #include <unistd.h>
69d687de29Sfvdl #include <syslog.h>
70d687de29Sfvdl #include <err.h>
71d687de29Sfvdl #include <util.h>
72d687de29Sfvdl #include <pwd.h>
73d687de29Sfvdl #include <string.h>
74d687de29Sfvdl #include <errno.h>
75d687de29Sfvdl #include "rpcbind.h"
76d687de29Sfvdl
771e8cf510Schristos #ifdef RPCBIND_RUMP
781e8cf510Schristos #include <semaphore.h>
791e8cf510Schristos
801e8cf510Schristos #include <rump/rump.h>
81aa97815bSkamil #include <rump/rump_syscallshotgun.h>
821e8cf510Schristos #include <rump/rump_syscalls.h>
831e8cf510Schristos
841e8cf510Schristos #include "svc_fdset.h"
851e8cf510Schristos
861e8cf510Schristos extern sem_t gensem;
871e8cf510Schristos #define DEBUGGING 1
881e8cf510Schristos #else
891e8cf510Schristos #define DEBUGGING 0
901e8cf510Schristos #endif
911e8cf510Schristos
92d687de29Sfvdl /* Global variables */
931e8cf510Schristos int debugging = DEBUGGING; /* Tell me what's going on */
94d687de29Sfvdl int doabort = 0; /* When debugging, do an abort on errors */
95d687de29Sfvdl rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
96d687de29Sfvdl
97d687de29Sfvdl /* who to suid to if -s is given */
98d687de29Sfvdl #define RUN_AS "daemon"
99d687de29Sfvdl
10034b7ffd9Schristos #define RPCBINDDLOCK "/var/run/rpcbind.lock"
10134b7ffd9Schristos
10234b7ffd9Schristos static int runasdaemon = 0;
103d687de29Sfvdl int insecure = 0;
104d687de29Sfvdl int oldstyle_local = 0;
10534b7ffd9Schristos #ifdef LIBWRAP
10634b7ffd9Schristos int libwrap = 0;
10734b7ffd9Schristos #endif
108d687de29Sfvdl int verboselog = 0;
109d687de29Sfvdl
11034b7ffd9Schristos static char **hosts = NULL;
11134b7ffd9Schristos static struct sockaddr **bound_sa;
11234b7ffd9Schristos static int ipv6_only = 0;
11334b7ffd9Schristos static int nhosts = 0;
11434b7ffd9Schristos static int on = 1;
115b766bbedSchristos #ifndef RPCBIND_RUMP
11634b7ffd9Schristos static int rpcbindlockfd;
117b766bbedSchristos #endif
11834b7ffd9Schristos
119d687de29Sfvdl #ifdef WARMSTART
120d687de29Sfvdl /* Local Variable */
12134b7ffd9Schristos static int warmstart = 0; /* Grab an old copy of registrations */
122d687de29Sfvdl #endif
123d687de29Sfvdl
124d687de29Sfvdl #ifdef PORTMAP
125d687de29Sfvdl struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
126de327a01Schristos const char *udptrans; /* Name of UDP transport */
127de327a01Schristos const char *tcptrans; /* Name of TCP transport */
128de327a01Schristos const char *udp_uaddr; /* Universal UDP address */
129de327a01Schristos const char *tcp_uaddr; /* Universal TCP address */
130d687de29Sfvdl #endif
1314ef5a01aSchristos static const char servname[] = "sunrpc";
132d687de29Sfvdl
133724513abSchristos const char rpcbind_superuser[] = "superuser";
134724513abSchristos const char rpcbind_unknown[] = "unknown";
135d687de29Sfvdl
136de327a01Schristos static int init_transport(struct netconfig *);
137de327a01Schristos static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
138de327a01Schristos struct netbuf *);
139bec77c5fSjoerg __dead static void terminate(int);
14034b7ffd9Schristos static void update_bound_sa(void);
1411e8cf510Schristos #ifndef RPCBIND_RUMP
142de327a01Schristos static void parseargs(int, char *[]);
143d687de29Sfvdl
144d687de29Sfvdl int
main(int argc,char * argv[])145d687de29Sfvdl main(int argc, char *argv[])
1461e8cf510Schristos #else
1471e8cf510Schristos int rpcbind_main(void *);
1481e8cf510Schristos int
1491e8cf510Schristos rpcbind_main(void *arg)
1501e8cf510Schristos #endif
151d687de29Sfvdl {
152d687de29Sfvdl struct netconfig *nconf;
153d687de29Sfvdl void *nc_handle; /* Net config handle */
154d687de29Sfvdl struct rlimit rl;
155d4f83aa2Sfvdl int maxrec = RPC_MAXDATASIZE;
156d687de29Sfvdl
1571e8cf510Schristos #ifdef RPCBIND_RUMP
1581e8cf510Schristos svc_fdset_init(SVC_FDSET_MT);
1591e8cf510Schristos #else
160d687de29Sfvdl parseargs(argc, argv);
1611e8cf510Schristos #endif
162d687de29Sfvdl
163f4ad1a12Schristos if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
164f4ad1a12Schristos err(EXIT_FAILURE, "getrlimit(RLIMIT_NOFILE)");
165f4ad1a12Schristos
166f4ad1a12Schristos if (rl.rlim_cur < 128) {
167d687de29Sfvdl if (rl.rlim_max <= 128)
168d687de29Sfvdl rl.rlim_cur = rl.rlim_max;
169d687de29Sfvdl else
170d687de29Sfvdl rl.rlim_cur = 128;
171f4ad1a12Schristos if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
172c0ca3685Sdholland err(EXIT_FAILURE, "setrlimit(RLIMIT_NOFILE)");
173c0ca3685Sdholland }
17434b7ffd9Schristos update_bound_sa();
17534b7ffd9Schristos
176b766bbedSchristos #ifndef RPCBIND_RUMP
17734b7ffd9Schristos /* Check that another rpcbind isn't already running. */
17834b7ffd9Schristos if ((rpcbindlockfd = open(RPCBINDDLOCK, O_RDONLY|O_CREAT, 0444)) == -1)
17905d4f66dSchristos err(EXIT_FAILURE, "%s", RPCBINDDLOCK);
18034b7ffd9Schristos
18134b7ffd9Schristos if (flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
18205d4f66dSchristos errx(EXIT_FAILURE,
18305d4f66dSchristos "another rpcbind is already running. Aborting");
18434b7ffd9Schristos
18534b7ffd9Schristos if (geteuid()) /* This command allowed only to root */
18634b7ffd9Schristos errx(EXIT_FAILURE, "Sorry. You are not superuser\n");
18734b7ffd9Schristos #endif
188d687de29Sfvdl nc_handle = setnetconfig(); /* open netconfig file */
189de327a01Schristos if (nc_handle == NULL)
190f4ad1a12Schristos errx(EXIT_FAILURE, "could not read /etc/netconfig");
19134b7ffd9Schristos
192d687de29Sfvdl #ifdef PORTMAP
193d687de29Sfvdl udptrans = "";
194d687de29Sfvdl tcptrans = "";
195d687de29Sfvdl #endif
196d687de29Sfvdl
197d687de29Sfvdl nconf = getnetconfigent("local");
198de327a01Schristos if (nconf == NULL)
199f4ad1a12Schristos errx(EXIT_FAILURE, "can't find local transport");
200d4f83aa2Sfvdl
201d4f83aa2Sfvdl rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
202d4f83aa2Sfvdl
203d687de29Sfvdl init_transport(nconf);
204d687de29Sfvdl
205d687de29Sfvdl while ((nconf = getnetconfig(nc_handle))) {
20634b7ffd9Schristos if (nconf->nc_flag & NC_VISIBLE) {
20734b7ffd9Schristos if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
20834b7ffd9Schristos "inet") == 0) {
20934b7ffd9Schristos /* DO NOTHING */
21034b7ffd9Schristos } else
211d687de29Sfvdl init_transport(nconf);
212d687de29Sfvdl }
21334b7ffd9Schristos }
214d687de29Sfvdl endnetconfig(nc_handle);
215d687de29Sfvdl
216d687de29Sfvdl /* catch the usual termination signals for graceful exit */
217d687de29Sfvdl (void) signal(SIGCHLD, reap);
218d687de29Sfvdl (void) signal(SIGINT, terminate);
219d687de29Sfvdl (void) signal(SIGTERM, terminate);
220d687de29Sfvdl (void) signal(SIGQUIT, terminate);
221d687de29Sfvdl /* ignore others that could get sent */
222d687de29Sfvdl (void) signal(SIGPIPE, SIG_IGN);
2231e8cf510Schristos #ifndef RPCBIND_RUMP
224d687de29Sfvdl (void) signal(SIGHUP, SIG_IGN);
2251e8cf510Schristos #endif
226d687de29Sfvdl (void) signal(SIGUSR1, SIG_IGN);
227d687de29Sfvdl (void) signal(SIGUSR2, SIG_IGN);
228d687de29Sfvdl #ifdef WARMSTART
229d687de29Sfvdl if (warmstart) {
230d687de29Sfvdl read_warmstart();
231d687de29Sfvdl }
232d687de29Sfvdl #endif
233d687de29Sfvdl if (debugging) {
234d687de29Sfvdl printf("rpcbind debugging enabled.");
235d687de29Sfvdl if (doabort) {
236d687de29Sfvdl printf(" Will abort on errors!\n");
237d687de29Sfvdl } else {
238d687de29Sfvdl printf("\n");
239d687de29Sfvdl }
240d687de29Sfvdl } else {
241d687de29Sfvdl if (daemon(0, 0))
242f4ad1a12Schristos err(EXIT_FAILURE, "fork failed");
243d687de29Sfvdl }
244de327a01Schristos
245de327a01Schristos openlog("rpcbind", 0, LOG_DAEMON);
246d687de29Sfvdl pidfile(NULL);
247d687de29Sfvdl
248d687de29Sfvdl if (runasdaemon) {
249d687de29Sfvdl struct passwd *p;
250d687de29Sfvdl
251d687de29Sfvdl if((p = getpwnam(RUN_AS)) == NULL) {
252d687de29Sfvdl syslog(LOG_ERR, "cannot get uid of daemon: %m");
253f4ad1a12Schristos exit(EXIT_FAILURE);
254d687de29Sfvdl }
255d687de29Sfvdl if (setuid(p->pw_uid) == -1) {
256d687de29Sfvdl syslog(LOG_ERR, "setuid to daemon failed: %m");
257f4ad1a12Schristos exit(EXIT_FAILURE);
258d687de29Sfvdl }
259d687de29Sfvdl }
260d687de29Sfvdl
261d687de29Sfvdl network_init();
262d687de29Sfvdl
2631e8cf510Schristos #ifdef RPCBIND_RUMP
2641e8cf510Schristos sem_post(&gensem);
2651e8cf510Schristos #endif
266d687de29Sfvdl my_svc_run();
267d687de29Sfvdl syslog(LOG_ERR, "svc_run returned unexpectedly");
268d687de29Sfvdl rpcbind_abort();
269d687de29Sfvdl /* NOTREACHED */
270d687de29Sfvdl
271f4ad1a12Schristos return EXIT_SUCCESS;
272d687de29Sfvdl }
273d687de29Sfvdl
274d687de29Sfvdl /*
275d687de29Sfvdl * Adds the entry into the rpcbind database.
276d687de29Sfvdl * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
277d687de29Sfvdl * Returns 0 if succeeds, else fails
278d687de29Sfvdl */
279d687de29Sfvdl static int
init_transport(struct netconfig * nconf)280d687de29Sfvdl init_transport(struct netconfig *nconf)
281d687de29Sfvdl {
282d687de29Sfvdl int fd;
283d687de29Sfvdl struct t_bind taddr;
284d687de29Sfvdl struct addrinfo hints, *res = NULL;
285d687de29Sfvdl struct __rpc_sockinfo si;
286d687de29Sfvdl SVCXPRT *my_xprt;
287d687de29Sfvdl int status; /* bound checking ? */
288d687de29Sfvdl int aicode;
289d687de29Sfvdl int addrlen;
29034b7ffd9Schristos int nhostsbak;
29134b7ffd9Schristos int bound;
29234b7ffd9Schristos u_int32_t host_addr[4]; /* IPv4 or IPv6 */
293d687de29Sfvdl struct sockaddr *sa;
294d687de29Sfvdl struct sockaddr_un sun;
29534b7ffd9Schristos #ifndef RPCBIND_RUMP
29634b7ffd9Schristos mode_t oldmask;
29734b7ffd9Schristos #endif
298d687de29Sfvdl
299d687de29Sfvdl if ((nconf->nc_semantics != NC_TPI_CLTS) &&
300d687de29Sfvdl (nconf->nc_semantics != NC_TPI_COTS) &&
301d687de29Sfvdl (nconf->nc_semantics != NC_TPI_COTS_ORD))
302de327a01Schristos return 1; /* not my type */
303b504fee4Sdsl #ifdef RPCBIND_DEBUG
304d687de29Sfvdl if (debugging) {
305f525b84eSchristos unsigned int i;
306d687de29Sfvdl char **s;
307d687de29Sfvdl
308d687de29Sfvdl (void)fprintf(stderr, "%s: %ld lookup routines :\n",
309d687de29Sfvdl nconf->nc_netid, nconf->nc_nlookups);
310d687de29Sfvdl for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
311d687de29Sfvdl i++, s++)
312f525b84eSchristos (void)fprintf(stderr, "[%u] - %s\n", i, *s);
313d687de29Sfvdl }
314d687de29Sfvdl #endif
315d687de29Sfvdl
316d687de29Sfvdl /*
317d687de29Sfvdl * XXX - using RPC library internal functions.
318d687de29Sfvdl */
31934b7ffd9Schristos if (strcmp(nconf->nc_netid, "local") == 0) {
32034b7ffd9Schristos /*
32134b7ffd9Schristos * For other transports we call this later, for each socket we
32234b7ffd9Schristos * like to bind.
32334b7ffd9Schristos */
324d687de29Sfvdl if ((fd = __rpc_nconf2fd(nconf)) < 0) {
32534b7ffd9Schristos int non_fatal = 0;
3262219d66eSchristos if (errno == EAFNOSUPPORT)
32734b7ffd9Schristos non_fatal = 1;
32834b7ffd9Schristos syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
32934b7ffd9Schristos "Cannot create socket for `%s'", nconf->nc_netid);
330de327a01Schristos return 1;
331d687de29Sfvdl }
33234b7ffd9Schristos } else
33334b7ffd9Schristos fd = -1;
334d687de29Sfvdl
335d687de29Sfvdl if (!__rpc_nconf2sockinfo(nconf, &si)) {
33634b7ffd9Schristos syslog(LOG_ERR, "Cannot get information for `%s'",
33734b7ffd9Schristos nconf->nc_netid);
338de327a01Schristos return 1;
339d687de29Sfvdl }
340d687de29Sfvdl
34134b7ffd9Schristos if (strcmp(nconf->nc_netid, "local") == 0) {
342de327a01Schristos (void)memset(&sun, 0, sizeof sun);
343d687de29Sfvdl sun.sun_family = AF_LOCAL;
3441e8cf510Schristos #ifdef RPCBIND_RUMP
3451e8cf510Schristos (void)rump_sys_unlink(_PATH_RPCBINDSOCK);
3461e8cf510Schristos #else
347de327a01Schristos (void)unlink(_PATH_RPCBINDSOCK);
3481e8cf510Schristos #endif
349de327a01Schristos (void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
350de327a01Schristos sizeof(sun.sun_path));
351d687de29Sfvdl sun.sun_len = SUN_LEN(&sun);
352d687de29Sfvdl addrlen = sizeof(struct sockaddr_un);
353d687de29Sfvdl sa = (struct sockaddr *)&sun;
354d687de29Sfvdl } else {
355d687de29Sfvdl /* Get rpcbind's address on this transport */
356d687de29Sfvdl
357de327a01Schristos (void)memset(&hints, 0, sizeof hints);
358d687de29Sfvdl hints.ai_flags = AI_PASSIVE;
359d687de29Sfvdl hints.ai_family = si.si_af;
360d687de29Sfvdl hints.ai_socktype = si.si_socktype;
361d687de29Sfvdl hints.ai_protocol = si.si_proto;
36234b7ffd9Schristos }
36334b7ffd9Schristos
36434b7ffd9Schristos if (strcmp(nconf->nc_netid, "local") != 0) {
36534b7ffd9Schristos /*
36634b7ffd9Schristos * If no hosts were specified, just bind to INADDR_ANY.
36734b7ffd9Schristos * Otherwise make sure 127.0.0.1 is added to the list.
36834b7ffd9Schristos */
36934b7ffd9Schristos nhostsbak = nhosts + 1;
370*43d0286dSnia if (reallocarr(&hosts, nhostsbak, sizeof(*hosts)) != 0) {
37105d4f66dSchristos syslog(LOG_ERR, "Can't grow hosts array");
37205d4f66dSchristos return 1;
37305d4f66dSchristos }
37434b7ffd9Schristos if (nhostsbak == 1)
37534b7ffd9Schristos hosts[0] = __UNCONST("*");
37634b7ffd9Schristos else {
37734b7ffd9Schristos if (hints.ai_family == AF_INET) {
37834b7ffd9Schristos hosts[nhostsbak - 1] = __UNCONST("127.0.0.1");
37934b7ffd9Schristos } else if (hints.ai_family == AF_INET6) {
38034b7ffd9Schristos hosts[nhostsbak - 1] = __UNCONST("::1");
38134b7ffd9Schristos } else
382d687de29Sfvdl return 1;
383d687de29Sfvdl }
38434b7ffd9Schristos
38534b7ffd9Schristos /*
38634b7ffd9Schristos * Bind to specific IPs if asked to
38734b7ffd9Schristos */
38834b7ffd9Schristos bound = 0;
38934b7ffd9Schristos while (nhostsbak > 0) {
39034b7ffd9Schristos --nhostsbak;
39134b7ffd9Schristos /*
39234b7ffd9Schristos * XXX - using RPC library internal functions.
39334b7ffd9Schristos */
39434b7ffd9Schristos if ((fd = __rpc_nconf2fd(nconf)) < 0) {
39534b7ffd9Schristos int non_fatal = 0;
39634b7ffd9Schristos if (errno == EAFNOSUPPORT &&
39734b7ffd9Schristos nconf->nc_semantics != NC_TPI_CLTS)
39834b7ffd9Schristos non_fatal = 1;
39934b7ffd9Schristos syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
40034b7ffd9Schristos "cannot create socket for %s",
40134b7ffd9Schristos nconf->nc_netid);
40234b7ffd9Schristos return 1;
40334b7ffd9Schristos }
40434b7ffd9Schristos switch (hints.ai_family) {
40534b7ffd9Schristos case AF_INET:
40634b7ffd9Schristos if (inet_pton(AF_INET, hosts[nhostsbak],
40734b7ffd9Schristos host_addr) == 1) {
40834b7ffd9Schristos hints.ai_flags &= AI_NUMERICHOST;
40934b7ffd9Schristos } else {
41034b7ffd9Schristos /*
41134b7ffd9Schristos * Skip if we have an AF_INET6 address.
41234b7ffd9Schristos */
41334b7ffd9Schristos if (inet_pton(AF_INET6,
41434b7ffd9Schristos hosts[nhostsbak], host_addr) == 1) {
41534b7ffd9Schristos close(fd);
41634b7ffd9Schristos continue;
41734b7ffd9Schristos }
41834b7ffd9Schristos }
41934b7ffd9Schristos break;
42034b7ffd9Schristos case AF_INET6:
42134b7ffd9Schristos if (inet_pton(AF_INET6, hosts[nhostsbak],
42234b7ffd9Schristos host_addr) == 1) {
42334b7ffd9Schristos hints.ai_flags &= AI_NUMERICHOST;
42434b7ffd9Schristos } else {
42534b7ffd9Schristos /*
42634b7ffd9Schristos * Skip if we have an AF_INET address.
42734b7ffd9Schristos */
42834b7ffd9Schristos if (inet_pton(AF_INET, hosts[nhostsbak],
42934b7ffd9Schristos host_addr) == 1) {
43034b7ffd9Schristos close(fd);
43134b7ffd9Schristos continue;
43234b7ffd9Schristos }
43334b7ffd9Schristos }
43434b7ffd9Schristos if (setsockopt(fd, IPPROTO_IPV6,
43534b7ffd9Schristos IPV6_V6ONLY, &on, sizeof on) < 0) {
43634b7ffd9Schristos syslog(LOG_ERR,
43734b7ffd9Schristos "can't set v6-only binding for "
43834b7ffd9Schristos "ipv6 socket: %m");
43934b7ffd9Schristos continue;
44034b7ffd9Schristos }
44134b7ffd9Schristos break;
44234b7ffd9Schristos default:
44334b7ffd9Schristos break;
44434b7ffd9Schristos }
44534b7ffd9Schristos
44634b7ffd9Schristos /*
44734b7ffd9Schristos * If no hosts were specified, just bind to INADDR_ANY
44834b7ffd9Schristos */
44934b7ffd9Schristos if (strcmp("*", hosts[nhostsbak]) == 0)
45034b7ffd9Schristos hosts[nhostsbak] = NULL;
45134b7ffd9Schristos if (strcmp(nconf->nc_netid, "local") != 0) {
45234b7ffd9Schristos if ((aicode = getaddrinfo(hosts[nhostsbak],
45334b7ffd9Schristos servname, &hints, &res)) != 0) {
45434b7ffd9Schristos syslog(LOG_ERR,
45534b7ffd9Schristos "cannot get local address for %s: %s",
45634b7ffd9Schristos nconf->nc_netid,
45734b7ffd9Schristos gai_strerror(aicode));
45834b7ffd9Schristos continue;
45934b7ffd9Schristos }
460d687de29Sfvdl addrlen = res->ai_addrlen;
461d687de29Sfvdl sa = (struct sockaddr *)res->ai_addr;
462d687de29Sfvdl }
46334b7ffd9Schristos #ifndef RPCBIND_RUMP
46434b7ffd9Schristos oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
46534b7ffd9Schristos #endif
46634b7ffd9Schristos if (bind(fd, sa, addrlen) != 0) {
46734b7ffd9Schristos syslog(LOG_ERR, "cannot bind %s on %s: %m",
46834b7ffd9Schristos (hosts[nhostsbak] == NULL) ? "*" :
46934b7ffd9Schristos hosts[nhostsbak], nconf->nc_netid);
470d687de29Sfvdl if (res != NULL)
471d687de29Sfvdl freeaddrinfo(res);
47234b7ffd9Schristos continue;
47334b7ffd9Schristos } else
47434b7ffd9Schristos bound = 1;
4751e8cf510Schristos #ifndef RPCBIND_RUMP
47634b7ffd9Schristos (void)umask(oldmask);
4771e8cf510Schristos #endif
478d687de29Sfvdl
479d687de29Sfvdl /* Copy the address */
480d687de29Sfvdl taddr.addr.len = taddr.addr.maxlen = addrlen;
481d687de29Sfvdl taddr.addr.buf = malloc(addrlen);
482d687de29Sfvdl if (taddr.addr.buf == NULL) {
4835794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory",
4845794a3e6Schristos __func__);
485d687de29Sfvdl if (res != NULL)
486d687de29Sfvdl freeaddrinfo(res);
487d687de29Sfvdl return 1;
488d687de29Sfvdl }
48934b7ffd9Schristos memcpy(taddr.addr.buf, sa, addrlen);
49034b7ffd9Schristos #ifdef RPCBIND_DEBUG
49134b7ffd9Schristos if (debugging) {
49234b7ffd9Schristos /*
49334b7ffd9Schristos * for debugging print out our universal
49434b7ffd9Schristos * address
49534b7ffd9Schristos */
49634b7ffd9Schristos char *uaddr;
49734b7ffd9Schristos struct netbuf nb;
49834b7ffd9Schristos
49934b7ffd9Schristos nb.buf = sa;
50034b7ffd9Schristos nb.len = nb.maxlen = sa->sa_len;
50134b7ffd9Schristos uaddr = taddr2uaddr(nconf, &nb);
50234b7ffd9Schristos (void)fprintf(stderr,
50334b7ffd9Schristos "rpcbind : my address is %s\n", uaddr);
50434b7ffd9Schristos (void)free(uaddr);
50534b7ffd9Schristos }
50634b7ffd9Schristos #endif
50734b7ffd9Schristos
50834b7ffd9Schristos if (nconf->nc_semantics != NC_TPI_CLTS)
50934b7ffd9Schristos listen(fd, SOMAXCONN);
51034b7ffd9Schristos
51134b7ffd9Schristos my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
51234b7ffd9Schristos RPC_MAXDATASIZE, RPC_MAXDATASIZE);
51334b7ffd9Schristos if (my_xprt == NULL) {
51434b7ffd9Schristos syslog(LOG_ERR,
51534b7ffd9Schristos "Could not create service for `%s'",
51634b7ffd9Schristos nconf->nc_netid);
51734b7ffd9Schristos goto error;
51834b7ffd9Schristos }
51934b7ffd9Schristos }
52034b7ffd9Schristos if (!bound)
52134b7ffd9Schristos return 1;
52234b7ffd9Schristos } else {
52334b7ffd9Schristos #ifndef RPCBIND_RUMP
52434b7ffd9Schristos oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
52534b7ffd9Schristos #endif
52634b7ffd9Schristos if (bind(fd, sa, addrlen) < 0) {
52734b7ffd9Schristos syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
52834b7ffd9Schristos if (res != NULL)
52934b7ffd9Schristos freeaddrinfo(res);
53034b7ffd9Schristos return 1;
53134b7ffd9Schristos }
53234b7ffd9Schristos #ifndef RPCBIND_RUMP
53334b7ffd9Schristos (void) umask(oldmask);
53434b7ffd9Schristos #endif
53534b7ffd9Schristos
53634b7ffd9Schristos /* Copy the address */
53734b7ffd9Schristos taddr.addr.len = taddr.addr.maxlen = addrlen;
53834b7ffd9Schristos taddr.addr.buf = malloc(addrlen);
53934b7ffd9Schristos if (taddr.addr.buf == NULL) {
5405794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
54134b7ffd9Schristos if (res != NULL)
54234b7ffd9Schristos freeaddrinfo(res);
54334b7ffd9Schristos return 1;
54434b7ffd9Schristos }
54534b7ffd9Schristos memcpy(taddr.addr.buf, sa, addrlen);
546b504fee4Sdsl #ifdef RPCBIND_DEBUG
547d687de29Sfvdl if (debugging) {
548d687de29Sfvdl /* for debugging print out our universal address */
549d687de29Sfvdl char *uaddr;
550d687de29Sfvdl struct netbuf nb;
551d687de29Sfvdl
552d687de29Sfvdl nb.buf = sa;
553d687de29Sfvdl nb.len = nb.maxlen = sa->sa_len;
554d687de29Sfvdl uaddr = taddr2uaddr(nconf, &nb);
55534b7ffd9Schristos (void) fprintf(stderr, "rpcbind : my address is %s\n",
55634b7ffd9Schristos uaddr);
557d687de29Sfvdl (void) free(uaddr);
558d687de29Sfvdl }
559d687de29Sfvdl #endif
560d687de29Sfvdl
561d687de29Sfvdl if (nconf->nc_semantics != NC_TPI_CLTS)
562d687de29Sfvdl listen(fd, SOMAXCONN);
563d687de29Sfvdl
56434b7ffd9Schristos my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
56534b7ffd9Schristos RPC_MAXDATASIZE, RPC_MAXDATASIZE);
5665794a3e6Schristos if (my_xprt == NULL) {
56734b7ffd9Schristos syslog(LOG_ERR, "%s: could not create service",
56834b7ffd9Schristos nconf->nc_netid);
569d687de29Sfvdl goto error;
570d687de29Sfvdl }
57134b7ffd9Schristos }
572d687de29Sfvdl
573d687de29Sfvdl #ifdef PORTMAP
574d687de29Sfvdl /*
575d687de29Sfvdl * Register both the versions for tcp/ip, udp/ip and local.
576d687de29Sfvdl */
577d687de29Sfvdl if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
578d687de29Sfvdl (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
579d687de29Sfvdl strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
580d687de29Sfvdl strcmp(nconf->nc_netid, "local") == 0) {
581d687de29Sfvdl struct pmaplist *pml;
582d687de29Sfvdl
583d687de29Sfvdl if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
584c40a2514Sfvdl pmap_service, 0)) {
58534b7ffd9Schristos syslog(LOG_ERR, "Could not register on `%s'",
58634b7ffd9Schristos nconf->nc_netid);
587d687de29Sfvdl goto error;
588d687de29Sfvdl }
58905d4f66dSchristos pml = malloc(sizeof(*pml));
59079fc501cSchristos if (pml == NULL) {
5915794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
592de327a01Schristos goto error;
593d687de29Sfvdl }
59434b7ffd9Schristos
595d687de29Sfvdl pml->pml_map.pm_prog = PMAPPROG;
596d687de29Sfvdl pml->pml_map.pm_vers = PMAPVERS;
597d687de29Sfvdl pml->pml_map.pm_port = PMAPPORT;
598d687de29Sfvdl if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
599d687de29Sfvdl if (tcptrans[0]) {
60034b7ffd9Schristos syslog(LOG_ERR,
601de327a01Schristos "Cannot have more than one TCP transport");
60279fc501cSchristos free(pml);
603d687de29Sfvdl goto error;
604d687de29Sfvdl }
605d687de29Sfvdl tcptrans = strdup(nconf->nc_netid);
606de327a01Schristos if (tcptrans == NULL) {
607de327a01Schristos free(pml);
6085794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory",
6095794a3e6Schristos __func__);
610de327a01Schristos goto error;
611de327a01Schristos }
612d687de29Sfvdl pml->pml_map.pm_prot = IPPROTO_TCP;
613d687de29Sfvdl
614d687de29Sfvdl /* Let's snarf the universal address */
615d687de29Sfvdl /* "h1.h2.h3.h4.p1.p2" */
616d687de29Sfvdl tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
617d687de29Sfvdl } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
618d687de29Sfvdl if (udptrans[0]) {
619de327a01Schristos free(pml);
62034b7ffd9Schristos syslog(LOG_ERR,
621de327a01Schristos "Cannot have more than one UDP transport");
622d687de29Sfvdl goto error;
623d687de29Sfvdl }
624d687de29Sfvdl udptrans = strdup(nconf->nc_netid);
625de327a01Schristos if (udptrans == NULL) {
626de327a01Schristos free(pml);
6275794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory",
6285794a3e6Schristos __func__);
629de327a01Schristos goto error;
630de327a01Schristos }
631d687de29Sfvdl pml->pml_map.pm_prot = IPPROTO_UDP;
632d687de29Sfvdl
633d687de29Sfvdl /* Let's snarf the universal address */
634d687de29Sfvdl /* "h1.h2.h3.h4.p1.p2" */
635d687de29Sfvdl udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
636159a288fSchristos } else if (strcmp(nconf->nc_netid, "local") == 0) {
63734b7ffd9Schristos #ifdef IPPROTO_ST
63834b7ffd9Schristos pml->pml_map.pm_prot = IPPROTO_ST;
639159a288fSchristos #else
640159a288fSchristos pml->pml_map.pm_prot = 0;
64134b7ffd9Schristos #endif
642159a288fSchristos }
643d687de29Sfvdl pml->pml_next = list_pml;
644d687de29Sfvdl list_pml = pml;
645d687de29Sfvdl
646d687de29Sfvdl /* Add version 3 information */
64705d4f66dSchristos pml = malloc(sizeof(*pml));
64879fc501cSchristos if (pml == NULL) {
6495794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
650de327a01Schristos goto error;
651d687de29Sfvdl }
652d687de29Sfvdl pml->pml_map = list_pml->pml_map;
653d687de29Sfvdl pml->pml_map.pm_vers = RPCBVERS;
654d687de29Sfvdl pml->pml_next = list_pml;
655d687de29Sfvdl list_pml = pml;
656d687de29Sfvdl
657d687de29Sfvdl /* Add version 4 information */
65805d4f66dSchristos pml = malloc(sizeof(*pml));
65979fc501cSchristos if (pml == NULL) {
6605794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
661de327a01Schristos goto error;
662d687de29Sfvdl }
663d687de29Sfvdl pml->pml_map = list_pml->pml_map;
664d687de29Sfvdl pml->pml_map.pm_vers = RPCBVERS4;
665d687de29Sfvdl pml->pml_next = list_pml;
666d687de29Sfvdl list_pml = pml;
667d687de29Sfvdl
668d687de29Sfvdl /* Also add version 2 stuff to rpcbind list */
669d687de29Sfvdl rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
670d687de29Sfvdl }
671d687de29Sfvdl #endif
672d687de29Sfvdl
673d687de29Sfvdl /* version 3 registration */
674d687de29Sfvdl if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
67534b7ffd9Schristos syslog(LOG_ERR, "Could not register %s version 3",
67634b7ffd9Schristos nconf->nc_netid);
677d687de29Sfvdl goto error;
678d687de29Sfvdl }
679d687de29Sfvdl rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
680d687de29Sfvdl
681d687de29Sfvdl /* version 4 registration */
682d687de29Sfvdl if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
68334b7ffd9Schristos syslog(LOG_ERR, "Could not register %s version 4",
68434b7ffd9Schristos nconf->nc_netid);
685d687de29Sfvdl goto error;
686d687de29Sfvdl }
687d687de29Sfvdl rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
688d687de29Sfvdl
689d687de29Sfvdl /* decide if bound checking works for this transport */
690d687de29Sfvdl status = add_bndlist(nconf, &taddr.addr);
691b504fee4Sdsl #ifdef RPCBIND_DEBUG
692d687de29Sfvdl if (debugging) {
693d687de29Sfvdl if (status < 0) {
694d687de29Sfvdl fprintf(stderr, "Error in finding bind status for %s\n",
695d687de29Sfvdl nconf->nc_netid);
696d687de29Sfvdl } else if (status == 0) {
697d687de29Sfvdl fprintf(stderr, "check binding for %s\n",
698d687de29Sfvdl nconf->nc_netid);
699d687de29Sfvdl } else if (status > 0) {
700d687de29Sfvdl fprintf(stderr, "No check binding for %s\n",
701d687de29Sfvdl nconf->nc_netid);
702d687de29Sfvdl }
703d687de29Sfvdl }
7040a77b69aSchristos #else
7050a77b69aSchristos __USE(status);
706d687de29Sfvdl #endif
707d687de29Sfvdl /*
708d687de29Sfvdl * rmtcall only supported on CLTS transports for now.
709d687de29Sfvdl */
710d687de29Sfvdl if (nconf->nc_semantics == NC_TPI_CLTS) {
711d687de29Sfvdl status = create_rmtcall_fd(nconf);
712d687de29Sfvdl
713b504fee4Sdsl #ifdef RPCBIND_DEBUG
714d687de29Sfvdl if (debugging) {
715d687de29Sfvdl if (status < 0) {
716d687de29Sfvdl fprintf(stderr,
717d687de29Sfvdl "Could not create rmtcall fd for %s\n",
718d687de29Sfvdl nconf->nc_netid);
719d687de29Sfvdl } else {
720d687de29Sfvdl fprintf(stderr, "rmtcall fd for %s is %d\n",
721d687de29Sfvdl nconf->nc_netid, status);
722d687de29Sfvdl }
723d687de29Sfvdl }
724d687de29Sfvdl #endif
725d687de29Sfvdl }
726d687de29Sfvdl return (0);
727d687de29Sfvdl error:
7281e8cf510Schristos #ifdef RPCBIND_RUMP
7291e8cf510Schristos (void)rump_sys_close(fd);
7301e8cf510Schristos #else
731de327a01Schristos (void)close(fd);
7321e8cf510Schristos #endif
733d687de29Sfvdl return (1);
734d687de29Sfvdl }
735d687de29Sfvdl
73634b7ffd9Schristos /*
73734b7ffd9Schristos * Create the list of addresses that we're bound to. Normally, this
73834b7ffd9Schristos * list is empty because we're listening on the wildcard address
73934b7ffd9Schristos * (nhost == 0). If -h is specified on the command line, then
74034b7ffd9Schristos * bound_sa will have a list of the addresses that the program binds
74134b7ffd9Schristos * to specifically. This function takes that list and converts them to
74234b7ffd9Schristos * struct sockaddr * and stores them in bound_sa.
74334b7ffd9Schristos */
74434b7ffd9Schristos static void
update_bound_sa(void)74534b7ffd9Schristos update_bound_sa(void)
74634b7ffd9Schristos {
74734b7ffd9Schristos struct addrinfo hints, *res = NULL;
74834b7ffd9Schristos int i;
74934b7ffd9Schristos
75034b7ffd9Schristos if (nhosts == 0)
75134b7ffd9Schristos return;
75205d4f66dSchristos bound_sa = calloc(nhosts, sizeof(*bound_sa));
75305d4f66dSchristos if (bound_sa == NULL)
75405d4f66dSchristos err(EXIT_FAILURE, "no space for bound address array");
75534b7ffd9Schristos memset(&hints, 0, sizeof(hints));
75634b7ffd9Schristos hints.ai_family = PF_UNSPEC;
75734b7ffd9Schristos for (i = 0; i < nhosts; i++) {
75834b7ffd9Schristos if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
75934b7ffd9Schristos continue;
76034b7ffd9Schristos bound_sa[i] = malloc(res->ai_addrlen);
76105d4f66dSchristos if (bound_sa[i] == NULL)
76205d4f66dSchristos err(EXIT_FAILURE, "no space for bound address");
76334b7ffd9Schristos memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
76434b7ffd9Schristos }
76534b7ffd9Schristos }
76634b7ffd9Schristos
76734b7ffd9Schristos /*
76834b7ffd9Schristos * Match the sa against the list of addresses we've bound to. If
76934b7ffd9Schristos * we've not specifically bound to anything, we match everything.
77034b7ffd9Schristos * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
77134b7ffd9Schristos * in bound_sa, we return true. If not, we return false.
77234b7ffd9Schristos */
77334b7ffd9Schristos int
listen_addr(const struct sockaddr * sa)77434b7ffd9Schristos listen_addr(const struct sockaddr *sa)
77534b7ffd9Schristos {
77634b7ffd9Schristos int i;
77734b7ffd9Schristos
77834b7ffd9Schristos /*
77934b7ffd9Schristos * If nhosts == 0, then there were no -h options on the
78034b7ffd9Schristos * command line, so all addresses are addresses we're
78134b7ffd9Schristos * listening to.
78234b7ffd9Schristos */
78334b7ffd9Schristos if (nhosts == 0)
78434b7ffd9Schristos return 1;
78534b7ffd9Schristos for (i = 0; i < nhosts; i++) {
78634b7ffd9Schristos if (bound_sa[i] == NULL ||
78734b7ffd9Schristos sa->sa_family != bound_sa[i]->sa_family)
78834b7ffd9Schristos continue;
78934b7ffd9Schristos switch (sa->sa_family) {
79034b7ffd9Schristos case AF_INET:
79134b7ffd9Schristos if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
79234b7ffd9Schristos sizeof(struct in_addr)) == 0)
79334b7ffd9Schristos return (1);
79434b7ffd9Schristos break;
79534b7ffd9Schristos #ifdef INET6
79634b7ffd9Schristos case AF_INET6:
79734b7ffd9Schristos if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
79834b7ffd9Schristos sizeof(struct in6_addr)) == 0)
79934b7ffd9Schristos return (1);
80034b7ffd9Schristos break;
80134b7ffd9Schristos #endif
80234b7ffd9Schristos default:
80334b7ffd9Schristos break;
80434b7ffd9Schristos }
80534b7ffd9Schristos }
80634b7ffd9Schristos return (0);
80734b7ffd9Schristos }
80834b7ffd9Schristos
809d687de29Sfvdl static void
rbllist_add(rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,struct netbuf * addr)810d687de29Sfvdl rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
811d687de29Sfvdl struct netbuf *addr)
812d687de29Sfvdl {
813d687de29Sfvdl rpcblist_ptr rbl;
814d687de29Sfvdl
8155794a3e6Schristos rbl = calloc(1, sizeof(*rbl));
81679fc501cSchristos if (rbl == NULL) {
8175794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
818de327a01Schristos return;
819d687de29Sfvdl }
820d687de29Sfvdl
821d687de29Sfvdl rbl->rpcb_map.r_prog = prog;
822d687de29Sfvdl rbl->rpcb_map.r_vers = vers;
823d687de29Sfvdl rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
824d687de29Sfvdl rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
825724513abSchristos rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
8265794a3e6Schristos if (rbl->rpcb_map.r_netid == NULL ||
8275794a3e6Schristos rbl->rpcb_map.r_addr == NULL ||
8285794a3e6Schristos rbl->rpcb_map.r_owner == NULL)
8295794a3e6Schristos {
8305794a3e6Schristos free(rbl->rpcb_map.r_netid);
8315794a3e6Schristos free(rbl->rpcb_map.r_addr);
8325794a3e6Schristos free(rbl->rpcb_map.r_owner);
8335794a3e6Schristos free(rbl);
8345794a3e6Schristos syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
8355794a3e6Schristos return;
8365794a3e6Schristos }
837d687de29Sfvdl rbl->rpcb_next = list_rbl; /* Attach to global list */
838d687de29Sfvdl list_rbl = rbl;
839d687de29Sfvdl }
840d687de29Sfvdl
841d687de29Sfvdl /*
842d687de29Sfvdl * Catch the signal and die
843d687de29Sfvdl */
844d687de29Sfvdl static void
terminate(int signum __unused)84534b7ffd9Schristos terminate(int signum __unused)
846d687de29Sfvdl {
847b766bbedSchristos #ifndef RPCBIND_RUMP
84834b7ffd9Schristos close(rpcbindlockfd);
849b766bbedSchristos #endif
850d687de29Sfvdl #ifdef WARMSTART
851d687de29Sfvdl syslog(LOG_ERR,
85234b7ffd9Schristos "rpcbind terminating on signal %d. Restart with \"rpcbind -w\"",
85334b7ffd9Schristos signum);
854d687de29Sfvdl write_warmstart(); /* Dump yourself */
855d687de29Sfvdl #endif
8561e8cf510Schristos #ifdef RPCBIND_RUMP
8571e8cf510Schristos exit(2);
8581e8cf510Schristos #else
859f4ad1a12Schristos exit(EXIT_FAILURE);
8601e8cf510Schristos #endif
861d687de29Sfvdl }
862d687de29Sfvdl
863d687de29Sfvdl void
rpcbind_abort(void)86434b7ffd9Schristos rpcbind_abort(void)
865d687de29Sfvdl {
866d687de29Sfvdl #ifdef WARMSTART
867d687de29Sfvdl write_warmstart(); /* Dump yourself */
868d687de29Sfvdl #endif
869d687de29Sfvdl abort();
870d687de29Sfvdl }
871d687de29Sfvdl
8721e8cf510Schristos #ifndef RPCBIND_RUMP
873d687de29Sfvdl /* get command line options */
874d687de29Sfvdl static void
parseargs(int argc,char * argv[])875d687de29Sfvdl parseargs(int argc, char *argv[])
876d687de29Sfvdl {
877d687de29Sfvdl int c;
878d687de29Sfvdl
87934b7ffd9Schristos #ifdef WARMSTART
88034b7ffd9Schristos #define WSOP "w"
88134b7ffd9Schristos #else
88234b7ffd9Schristos #define WSOP ""
88334b7ffd9Schristos #endif
88434b7ffd9Schristos #ifdef LIBWRAP
88534b7ffd9Schristos #define WRAPOP "W"
88634b7ffd9Schristos #else
88734b7ffd9Schristos #define WRAPOP ""
88834b7ffd9Schristos #endif
88934b7ffd9Schristos while ((c = getopt(argc, argv, "6adh:iLls" WRAPOP WSOP)) != -1) {
890d687de29Sfvdl switch (c) {
89134b7ffd9Schristos case '6':
89234b7ffd9Schristos ipv6_only = 1;
89334b7ffd9Schristos break;
894d687de29Sfvdl case 'a':
895d687de29Sfvdl doabort = 1; /* when debugging, do an abort on */
896d687de29Sfvdl break; /* errors; for rpcbind developers */
897d687de29Sfvdl /* only! */
898d687de29Sfvdl case 'd':
899f525b84eSchristos debugging++;
900d687de29Sfvdl break;
90134b7ffd9Schristos case 'h':
90234b7ffd9Schristos ++nhosts;
903*43d0286dSnia if (reallocarr(&hosts, nhosts, sizeof(*hosts)) != 0)
90405d4f66dSchristos err(EXIT_FAILURE, "Can't allocate host array");
90534b7ffd9Schristos hosts[nhosts - 1] = strdup(optarg);
90634b7ffd9Schristos if (hosts[nhosts - 1] == NULL)
90705d4f66dSchristos err(EXIT_FAILURE, "Can't allocate host");
90834b7ffd9Schristos break;
909d687de29Sfvdl case 'i':
910d687de29Sfvdl insecure = 1;
911d687de29Sfvdl break;
912d687de29Sfvdl case 'L':
913d687de29Sfvdl oldstyle_local = 1;
914d687de29Sfvdl break;
915d687de29Sfvdl case 'l':
916d687de29Sfvdl verboselog = 1;
917d687de29Sfvdl break;
918d687de29Sfvdl case 's':
919d687de29Sfvdl runasdaemon = 1;
920d687de29Sfvdl break;
92134b7ffd9Schristos #ifdef LIBWRAP
92234b7ffd9Schristos case 'W':
92334b7ffd9Schristos libwrap = 1;
92434b7ffd9Schristos break;
92534b7ffd9Schristos #endif
926d687de29Sfvdl #ifdef WARMSTART
927d687de29Sfvdl case 'w':
928d687de29Sfvdl warmstart = 1;
929d687de29Sfvdl break;
930d687de29Sfvdl #endif
931d687de29Sfvdl default: /* error */
932d687de29Sfvdl fprintf(stderr, "usage: rpcbind [-Idwils]\n");
93334b7ffd9Schristos fprintf(stderr,
93434b7ffd9Schristos "Usage: %s [-6adiLls%s%s] [-h bindip]\n",
93534b7ffd9Schristos getprogname(), WRAPOP, WSOP);
936f4ad1a12Schristos exit(EXIT_FAILURE);
937d687de29Sfvdl }
938d687de29Sfvdl }
939d687de29Sfvdl if (doabort && !debugging) {
940d687de29Sfvdl fprintf(stderr,
941d687de29Sfvdl "-a (abort) specified without -d (debugging) -- ignored.\n");
942d687de29Sfvdl doabort = 0;
943d687de29Sfvdl }
94434b7ffd9Schristos #undef WRAPOP
94534b7ffd9Schristos #undef WSOP
946d687de29Sfvdl }
9471e8cf510Schristos #endif
948d687de29Sfvdl
949d687de29Sfvdl void
reap(int dummy __unused)95034b7ffd9Schristos reap(int dummy __unused)
951d687de29Sfvdl {
952d687de29Sfvdl int save_errno = errno;
953d687de29Sfvdl
954d687de29Sfvdl while (wait3(NULL, WNOHANG, NULL) > 0)
955d687de29Sfvdl ;
956d687de29Sfvdl errno = save_errno;
957d687de29Sfvdl }
958d687de29Sfvdl
959d687de29Sfvdl void
toggle_verboselog(int dummy __unused)96034b7ffd9Schristos toggle_verboselog(int dummy __unused)
961d687de29Sfvdl {
962d687de29Sfvdl verboselog = !verboselog;
963d687de29Sfvdl }
964