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