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