Deleted Added
sdiff udiff text old ( 203710 ) new ( 224001 )
full compact
1/* $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $ */
2/* $FreeBSD: head/usr.sbin/rpcbind/rpcbind.c 203710 2010-02-09 18:10:56Z imp $ */
3
4/*
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32/*
33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
34 */
35
36/* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
37
38#if 0
39#ifndef lint
40static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
41#endif
42#endif
43
44/*
45 * rpcbind.c
46 * Implements the program, version to address mapping for rpc.
47 *
48 */
49
50#include <sys/types.h>
51#include <sys/stat.h>
52#include <sys/errno.h>
53#include <sys/time.h>
54#include <sys/resource.h>
55#include <sys/wait.h>
56#include <sys/signal.h>
57#include <sys/socket.h>
58#include <sys/un.h>
59#include <rpc/rpc.h>
60#include <rpc/rpc_com.h>
61#ifdef PORTMAP
62#include <netinet/in.h>
63#endif
64#include <arpa/inet.h>
65#include <fcntl.h>
66#include <netdb.h>
67#include <stdio.h>
68#include <netconfig.h>
69#include <stdlib.h>
70#include <unistd.h>
71#include <syslog.h>
72#include <err.h>
73#include <libutil.h>
74#include <pwd.h>
75#include <string.h>
76#include <errno.h>
77#include "rpcbind.h"
78
79/* Global variables */
80int debugging = 0; /* Tell me what's going on */
81int doabort = 0; /* When debugging, do an abort on errors */
82rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
83
84/* who to suid to if -s is given */
85#define RUN_AS "daemon"
86
87#define RPCBINDDLOCK "/var/run/rpcbind.lock"
88
89int runasdaemon = 0;
90int insecure = 0;
91int oldstyle_local = 0;
92int verboselog = 0;
93
94char **hosts = NULL;
95struct sockaddr **bound_sa;
96int ipv6_only = 0;
97int nhosts = 0;
98int on = 1;
99int rpcbindlockfd;
100
101#ifdef WARMSTART
102/* Local Variable */
103static int warmstart = 0; /* Grab an old copy of registrations. */
104#endif
105
106#ifdef PORTMAP
107struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
108char *udptrans; /* Name of UDP transport */
109char *tcptrans; /* Name of TCP transport */
110char *udp_uaddr; /* Universal UDP address */
111char *tcp_uaddr; /* Universal TCP address */
112#endif
113static char servname[] = "rpcbind";
114static char superuser[] = "superuser";
115
116int main(int, char *[]);
117
118static int init_transport(struct netconfig *);
119static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
120 struct netbuf *);
121static void terminate(int);
122static void parseargs(int, char *[]);
123static void update_bound_sa(void);
124
125int
126main(int argc, char *argv[])
127{
128 struct netconfig *nconf;
129 void *nc_handle; /* Net config handle */
130 struct rlimit rl;
131 int maxrec = RPC_MAXDATASIZE;
132
133 parseargs(argc, argv);
134
135 update_bound_sa();
136
137 /* Check that another rpcbind isn't already running. */
138 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
139 O_RDONLY|O_CREAT, 0444))) == -1)
140 err(1, "%s", RPCBINDDLOCK);
141
142 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
143 errx(1, "another rpcbind is already running. Aborting");
144
145 getrlimit(RLIMIT_NOFILE, &rl);
146 if (rl.rlim_cur < 128) {
147 if (rl.rlim_max <= 128)
148 rl.rlim_cur = rl.rlim_max;
149 else
150 rl.rlim_cur = 128;
151 setrlimit(RLIMIT_NOFILE, &rl);
152 }
153 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
154 if (geteuid()) { /* This command allowed only to root */
155 fprintf(stderr, "Sorry. You are not superuser\n");
156 exit(1);
157 }
158 nc_handle = setnetconfig(); /* open netconfig file */
159 if (nc_handle == NULL) {
160 syslog(LOG_ERR, "could not read /etc/netconfig");
161 exit(1);
162 }
163#ifdef PORTMAP
164 udptrans = "";
165 tcptrans = "";
166#endif
167
168 nconf = getnetconfigent("local");
169 if (nconf == NULL)
170 nconf = getnetconfigent("unix");
171 if (nconf == NULL) {
172 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
173 exit(1);
174 }
175
176 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
177
178 init_transport(nconf);
179
180 while ((nconf = getnetconfig(nc_handle))) {
181 if (nconf->nc_flag & NC_VISIBLE)
182 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
183 "inet") == 0) {
184 /* DO NOTHING */
185 } else
186 init_transport(nconf);
187 }
188 endnetconfig(nc_handle);
189
190 /* catch the usual termination signals for graceful exit */
191 (void) signal(SIGCHLD, reap);
192 (void) signal(SIGINT, terminate);
193 (void) signal(SIGTERM, terminate);
194 (void) signal(SIGQUIT, terminate);
195 /* ignore others that could get sent */
196 (void) signal(SIGPIPE, SIG_IGN);
197 (void) signal(SIGHUP, SIG_IGN);
198 (void) signal(SIGUSR1, SIG_IGN);
199 (void) signal(SIGUSR2, SIG_IGN);
200#ifdef WARMSTART
201 if (warmstart) {
202 read_warmstart();
203 }
204#endif
205 if (debugging) {
206 printf("rpcbind debugging enabled.");
207 if (doabort) {
208 printf(" Will abort on errors!\n");
209 } else {
210 printf("\n");
211 }
212 } else {
213 if (daemon(0, 0))
214 err(1, "fork failed");
215 }
216
217 if (runasdaemon) {
218 struct passwd *p;
219
220 if((p = getpwnam(RUN_AS)) == NULL) {
221 syslog(LOG_ERR, "cannot get uid of daemon: %m");
222 exit(1);
223 }
224 if (setuid(p->pw_uid) == -1) {
225 syslog(LOG_ERR, "setuid to daemon failed: %m");
226 exit(1);
227 }
228 }
229
230 network_init();
231
232 my_svc_run();
233 syslog(LOG_ERR, "svc_run returned unexpectedly");
234 rpcbind_abort();
235 /* NOTREACHED */
236
237 return 0;
238}
239
240/*
241 * Adds the entry into the rpcbind database.
242 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
243 * Returns 0 if succeeds, else fails
244 */
245static int
246init_transport(struct netconfig *nconf)
247{
248 int fd;
249 struct t_bind taddr;
250 struct addrinfo hints, *res = NULL;
251 struct __rpc_sockinfo si;
252 SVCXPRT *my_xprt;
253 int status; /* bound checking ? */
254 int aicode;
255 int addrlen;
256 int nhostsbak;
257 int bound;
258 struct sockaddr *sa;
259 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
260 struct sockaddr_un sun;
261 mode_t oldmask;
262
263 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
264 (nconf->nc_semantics != NC_TPI_COTS) &&
265 (nconf->nc_semantics != NC_TPI_COTS_ORD))
266 return (1); /* not my type */
267#ifdef ND_DEBUG
268 if (debugging) {
269 int i;
270 char **s;
271
272 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
273 nconf->nc_netid, nconf->nc_nlookups);
274 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
275 i++, s++)
276 fprintf(stderr, "[%d] - %s\n", i, *s);
277 }
278#endif
279
280 /*
281 * XXX - using RPC library internal functions.
282 */
283 if ((strcmp(nconf->nc_netid, "local") == 0) ||
284 (strcmp(nconf->nc_netid, "unix") == 0)) {
285 /*
286 * For other transports we call this later, for each socket we
287 * like to bind.
288 */
289 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
290 int non_fatal = 0;
291 if (errno == EPROTONOSUPPORT)
292 non_fatal = 1;
293 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
294 nconf->nc_netid);
295 return (1);
296 }
297 }
298
299 if (!__rpc_nconf2sockinfo(nconf, &si)) {
300 syslog(LOG_ERR, "cannot get information for %s",
301 nconf->nc_netid);
302 return (1);
303 }
304
305 if ((strcmp(nconf->nc_netid, "local") == 0) ||
306 (strcmp(nconf->nc_netid, "unix") == 0)) {
307 memset(&sun, 0, sizeof sun);
308 sun.sun_family = AF_LOCAL;
309 unlink(_PATH_RPCBINDSOCK);
310 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
311 sun.sun_len = SUN_LEN(&sun);
312 addrlen = sizeof (struct sockaddr_un);
313 sa = (struct sockaddr *)&sun;
314 } else {
315 /* Get rpcbind's address on this transport */
316
317 memset(&hints, 0, sizeof hints);
318 hints.ai_flags = AI_PASSIVE;
319 hints.ai_family = si.si_af;
320 hints.ai_socktype = si.si_socktype;
321 hints.ai_protocol = si.si_proto;
322 }
323
324 if ((strcmp(nconf->nc_netid, "local") != 0) &&
325 (strcmp(nconf->nc_netid, "unix") != 0)) {
326 /*
327 * If no hosts were specified, just bind to INADDR_ANY.
328 * Otherwise make sure 127.0.0.1 is added to the list.
329 */
330 nhostsbak = nhosts + 1;
331 hosts = realloc(hosts, nhostsbak * sizeof(char *));
332 if (nhostsbak == 1)
333 hosts[0] = "*";
334 else {
335 if (hints.ai_family == AF_INET) {
336 hosts[nhostsbak - 1] = "127.0.0.1";
337 } else if (hints.ai_family == AF_INET6) {
338 hosts[nhostsbak - 1] = "::1";
339 } else
340 return 1;
341 }
342
343 /*
344 * Bind to specific IPs if asked to
345 */
346 bound = 0;
347 while (nhostsbak > 0) {
348 --nhostsbak;
349 /*
350 * XXX - using RPC library internal functions.
351 */
352 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
353 int non_fatal = 0;
354 if (errno == EPROTONOSUPPORT &&
355 nconf->nc_semantics != NC_TPI_CLTS)
356 non_fatal = 1;
357 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
358 "cannot create socket for %s", nconf->nc_netid);
359 return (1);
360 }
361 switch (hints.ai_family) {
362 case AF_INET:
363 if (inet_pton(AF_INET, hosts[nhostsbak],
364 host_addr) == 1) {
365 hints.ai_flags &= AI_NUMERICHOST;
366 } else {
367 /*
368 * Skip if we have an AF_INET6 adress.
369 */
370 if (inet_pton(AF_INET6,
371 hosts[nhostsbak], host_addr) == 1) {
372 close(fd);
373 continue;
374 }
375 }
376 break;
377 case AF_INET6:
378 if (inet_pton(AF_INET6, hosts[nhostsbak],
379 host_addr) == 1) {
380 hints.ai_flags &= AI_NUMERICHOST;
381 } else {
382 /*
383 * Skip if we have an AF_INET adress.
384 */
385 if (inet_pton(AF_INET, hosts[nhostsbak],
386 host_addr) == 1) {
387 close(fd);
388 continue;
389 }
390 }
391 if (setsockopt(fd, IPPROTO_IPV6,
392 IPV6_V6ONLY, &on, sizeof on) < 0) {
393 syslog(LOG_ERR,
394 "can't set v6-only binding for "
395 "ipv6 socket: %m");
396 continue;
397 }
398 break;
399 default:
400 break;
401 }
402
403 /*
404 * If no hosts were specified, just bind to INADDR_ANY
405 */
406 if (strcmp("*", hosts[nhostsbak]) == 0)
407 hosts[nhostsbak] = NULL;
408 if ((strcmp(nconf->nc_netid, "local") != 0) &&
409 (strcmp(nconf->nc_netid, "unix") != 0)) {
410 if ((aicode = getaddrinfo(hosts[nhostsbak],
411 servname, &hints, &res)) != 0) {
412 syslog(LOG_ERR,
413 "cannot get local address for %s: %s",
414 nconf->nc_netid, gai_strerror(aicode));
415 continue;
416 }
417 addrlen = res->ai_addrlen;
418 sa = (struct sockaddr *)res->ai_addr;
419 }
420 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
421 if (bind(fd, sa, addrlen) != 0) {
422 syslog(LOG_ERR, "cannot bind %s on %s: %m",
423 (hosts[nhostsbak] == NULL) ? "*" :
424 hosts[nhostsbak], nconf->nc_netid);
425 if (res != NULL)
426 freeaddrinfo(res);
427 continue;
428 } else
429 bound = 1;
430 (void)umask(oldmask);
431
432 /* Copy the address */
433 taddr.addr.len = taddr.addr.maxlen = addrlen;
434 taddr.addr.buf = malloc(addrlen);
435 if (taddr.addr.buf == NULL) {
436 syslog(LOG_ERR,
437 "cannot allocate memory for %s address",
438 nconf->nc_netid);
439 if (res != NULL)
440 freeaddrinfo(res);
441 return 1;
442 }
443 memcpy(taddr.addr.buf, sa, addrlen);
444#ifdef ND_DEBUG
445 if (debugging) {
446 /*
447 * for debugging print out our universal
448 * address
449 */
450 char *uaddr;
451 struct netbuf nb;
452
453 nb.buf = sa;
454 nb.len = nb.maxlen = sa->sa_len;
455 uaddr = taddr2uaddr(nconf, &nb);
456 (void)fprintf(stderr,
457 "rpcbind : my address is %s\n", uaddr);
458 (void)free(uaddr);
459 }
460#endif
461
462 if (nconf->nc_semantics != NC_TPI_CLTS)
463 listen(fd, SOMAXCONN);
464
465 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
466 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
467 if (my_xprt == (SVCXPRT *)NULL) {
468 syslog(LOG_ERR, "%s: could not create service",
469 nconf->nc_netid);
470 goto error;
471 }
472 }
473 if (!bound)
474 return 1;
475 } else {
476 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
477 if (bind(fd, sa, addrlen) < 0) {
478 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
479 if (res != NULL)
480 freeaddrinfo(res);
481 return 1;
482 }
483 (void) umask(oldmask);
484
485 /* Copy the address */
486 taddr.addr.len = taddr.addr.maxlen = addrlen;
487 taddr.addr.buf = malloc(addrlen);
488 if (taddr.addr.buf == NULL) {
489 syslog(LOG_ERR, "cannot allocate memory for %s address",
490 nconf->nc_netid);
491 if (res != NULL)
492 freeaddrinfo(res);
493 return 1;
494 }
495 memcpy(taddr.addr.buf, sa, addrlen);
496#ifdef ND_DEBUG
497 if (debugging) {
498 /* for debugging print out our universal address */
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, "rpcbind : my address is %s\n",
506 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 == (SVCXPRT *)NULL) {
517 syslog(LOG_ERR, "%s: could not create service",
518 nconf->nc_netid);
519 goto error;
520 }
521 }
522
523#ifdef PORTMAP
524 /*
525 * Register both the versions for tcp/ip, udp/ip and local.
526 */
527 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
528 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
529 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
530 (strcmp(nconf->nc_netid, "unix") == 0) ||
531 (strcmp(nconf->nc_netid, "local") == 0)) {
532 struct pmaplist *pml;
533
534 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
535 pmap_service, 0)) {
536 syslog(LOG_ERR, "could not register on %s",
537 nconf->nc_netid);
538 goto error;
539 }
540 pml = malloc(sizeof (struct pmaplist));
541 if (pml == NULL) {
542 syslog(LOG_ERR, "no memory!");
543 exit(1);
544 }
545 pml->pml_map.pm_prog = PMAPPROG;
546 pml->pml_map.pm_vers = PMAPVERS;
547 pml->pml_map.pm_port = PMAPPORT;
548 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
549 if (tcptrans[0]) {
550 syslog(LOG_ERR,
551 "cannot have more than one TCP transport");
552 goto error;
553 }
554 tcptrans = strdup(nconf->nc_netid);
555 pml->pml_map.pm_prot = IPPROTO_TCP;
556
557 /* Let's snarf the universal address */
558 /* "h1.h2.h3.h4.p1.p2" */
559 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
560 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
561 if (udptrans[0]) {
562 syslog(LOG_ERR,
563 "cannot have more than one UDP transport");
564 goto error;
565 }
566 udptrans = strdup(nconf->nc_netid);
567 pml->pml_map.pm_prot = IPPROTO_UDP;
568
569 /* Let's snarf the universal address */
570 /* "h1.h2.h3.h4.p1.p2" */
571 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
572 } else if (strcmp(nconf->nc_netid, "local") == 0)
573 pml->pml_map.pm_prot = IPPROTO_ST;
574 else if (strcmp(nconf->nc_netid, "unix") == 0)
575 pml->pml_map.pm_prot = IPPROTO_ST;
576 pml->pml_next = list_pml;
577 list_pml = pml;
578
579 /* Add version 3 information */
580 pml = malloc(sizeof (struct pmaplist));
581 if (pml == NULL) {
582 syslog(LOG_ERR, "no memory!");
583 exit(1);
584 }
585 pml->pml_map = list_pml->pml_map;
586 pml->pml_map.pm_vers = RPCBVERS;
587 pml->pml_next = list_pml;
588 list_pml = pml;
589
590 /* Add version 4 information */
591 pml = malloc (sizeof (struct pmaplist));
592 if (pml == NULL) {
593 syslog(LOG_ERR, "no memory!");
594 exit(1);
595 }
596 pml->pml_map = list_pml->pml_map;
597 pml->pml_map.pm_vers = RPCBVERS4;
598 pml->pml_next = list_pml;
599 list_pml = pml;
600
601 /* Also add version 2 stuff to rpcbind list */
602 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
603 }
604#endif
605
606 /* version 3 registration */
607 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
608 syslog(LOG_ERR, "could not register %s version 3",
609 nconf->nc_netid);
610 goto error;
611 }
612 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
613
614 /* version 4 registration */
615 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
616 syslog(LOG_ERR, "could not register %s version 4",
617 nconf->nc_netid);
618 goto error;
619 }
620 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
621
622 /* decide if bound checking works for this transport */
623 status = add_bndlist(nconf, &taddr.addr);
624#ifdef BIND_DEBUG
625 if (debugging) {
626 if (status < 0) {
627 fprintf(stderr, "Error in finding bind status for %s\n",
628 nconf->nc_netid);
629 } else if (status == 0) {
630 fprintf(stderr, "check binding for %s\n",
631 nconf->nc_netid);
632 } else if (status > 0) {
633 fprintf(stderr, "No check binding for %s\n",
634 nconf->nc_netid);
635 }
636 }
637#endif
638 /*
639 * rmtcall only supported on CLTS transports for now.
640 */
641 if (nconf->nc_semantics == NC_TPI_CLTS) {
642 status = create_rmtcall_fd(nconf);
643
644#ifdef BIND_DEBUG
645 if (debugging) {
646 if (status < 0) {
647 fprintf(stderr,
648 "Could not create rmtcall fd for %s\n",
649 nconf->nc_netid);
650 } else {
651 fprintf(stderr, "rmtcall fd for %s is %d\n",
652 nconf->nc_netid, status);
653 }
654 }
655#endif
656 }
657 return (0);
658error:
659 close(fd);
660 return (1);
661}
662
663/*
664 * Create the list of addresses that we're bound to. Normally, this
665 * list is empty because we're listening on the wildcard address
666 * (nhost == 0). If -h is specified on the command line, then
667 * bound_sa will have a list of the addresses that the program binds
668 * to specifically. This function takes that list and converts them to
669 * struct sockaddr * and stores them in bound_sa.
670 */
671static void
672update_bound_sa(void)
673{
674 struct addrinfo hints, *res = NULL;
675 int i;
676
677 if (nhosts == 0)
678 return;
679 bound_sa = malloc(sizeof(*bound_sa) * nhosts);
680 memset(&hints, 0, sizeof(hints));
681 hints.ai_family = PF_UNSPEC;
682 for (i = 0; i < nhosts; i++) {
683 if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
684 continue;
685 bound_sa[i] = malloc(res->ai_addrlen);
686 memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
687 }
688}
689
690/*
691 * Match the sa against the list of addresses we've bound to. If
692 * we've not specifically bound to anything, we match everything.
693 * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
694 * in bound_sa, we return true. If not, we return false.
695 */
696int
697listen_addr(const struct sockaddr *sa)
698{
699 int i;
700
701 /*
702 * If nhosts == 0, then there were no -h options on the
703 * command line, so all addresses are addresses we're
704 * listening to.
705 */
706 if (nhosts == 0)
707 return 1;
708 for (i = 0; i < nhosts; i++) {
709 if (bound_sa[i] == NULL ||
710 sa->sa_family != bound_sa[i]->sa_family)
711 continue;
712 switch (sa->sa_family) {
713 case AF_INET:
714 if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
715 sizeof(struct in_addr)) == 0)
716 return (1);
717 break;
718#ifdef INET6
719 case AF_INET6:
720 if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
721 sizeof(struct in6_addr)) == 0)
722 return (1);
723 break;
724#endif
725 default:
726 break;
727 }
728 }
729 return (0);
730}
731
732static void
733rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
734 struct netbuf *addr)
735{
736 rpcblist_ptr rbl;
737
738 rbl = malloc(sizeof (rpcblist));
739 if (rbl == NULL) {
740 syslog(LOG_ERR, "no memory!");
741 exit(1);
742 }
743
744 rbl->rpcb_map.r_prog = prog;
745 rbl->rpcb_map.r_vers = vers;
746 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
747 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
748 rbl->rpcb_map.r_owner = strdup(superuser);
749 rbl->rpcb_next = list_rbl; /* Attach to global list */
750 list_rbl = rbl;
751}
752
753/*
754 * Catch the signal and die
755 */
756static void
757terminate(int dummy __unused)
758{
759 close(rpcbindlockfd);
760#ifdef WARMSTART
761 syslog(LOG_ERR,
762 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
763 write_warmstart(); /* Dump yourself */
764#endif
765 exit(2);
766}
767
768void
769rpcbind_abort()
770{
771#ifdef WARMSTART
772 write_warmstart(); /* Dump yourself */
773#endif
774 abort();
775}
776
777/* get command line options */
778static void
779parseargs(int argc, char *argv[])
780{
781 int c;
782
783#ifdef WARMSTART
784#define WSOP "w"
785#else
786#define WSOP ""
787#endif
788 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
789 switch (c) {
790 case '6':
791 ipv6_only = 1;
792 break;
793 case 'a':
794 doabort = 1; /* when debugging, do an abort on */
795 break; /* errors; for rpcbind developers */
796 /* only! */
797 case 'd':
798 debugging = 1;
799 break;
800 case 'h':
801 ++nhosts;
802 hosts = realloc(hosts, nhosts * sizeof(char *));
803 if (hosts == NULL)
804 errx(1, "Out of memory");
805 hosts[nhosts - 1] = strdup(optarg);
806 if (hosts[nhosts - 1] == NULL)
807 errx(1, "Out of memory");
808 break;
809 case 'i':
810 insecure = 1;
811 break;
812 case 'L':
813 oldstyle_local = 1;
814 break;
815 case 'l':
816 verboselog = 1;
817 break;
818 case 's':
819 runasdaemon = 1;
820 break;
821#ifdef WARMSTART
822 case 'w':
823 warmstart = 1;
824 break;
825#endif
826 default: /* error */
827 fprintf(stderr,
828 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
829 WSOP);
830 exit (1);
831 }
832 }
833 if (doabort && !debugging) {
834 fprintf(stderr,
835 "-a (abort) specified without -d (debugging) -- ignored.\n");
836 doabort = 0;
837 }
838#undef WSOP
839}
840
841void
842reap(int dummy __unused)
843{
844 int save_errno = errno;
845
846 while (wait3(NULL, WNOHANG, NULL) > 0)
847 ;
848 errno = save_errno;
849}
850
851void
852toggle_verboselog(int dummy __unused)
853{
854 verboselog = !verboselog;
855}