sh: Add simple CDPATH test.
[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,
170                     "inet") == 0) {
171                     /* DO NOTHING */
172                 } else
173                     init_transport(nconf);
174         }
175         endnetconfig(nc_handle);
176
177         /* catch the usual termination signals for graceful exit */
178         signal(SIGCHLD, reap);
179         signal(SIGINT, terminate);
180         signal(SIGTERM, terminate);
181         signal(SIGQUIT, terminate);
182         /* ignore others that could get sent */
183         signal(SIGPIPE, SIG_IGN);
184         signal(SIGHUP, SIG_IGN);
185         signal(SIGUSR1, SIG_IGN);
186         signal(SIGUSR2, SIG_IGN);
187 #ifdef WARMSTART
188         if (warmstart) {
189                 read_warmstart();
190         }
191 #endif
192         if (debugging) {
193                 printf("rpcbind debugging enabled.");
194                 if (doabort) {
195                         printf("  Will abort on errors!\n");
196                 } else {
197                         printf("\n");
198                 }
199         } else {
200                 if (daemon(0, 0))
201                         err(1, "fork failed");
202         }
203
204         if (runasdaemon) {
205                 struct passwd *p;
206
207                 if((p = getpwnam(RUN_AS)) == NULL) {
208                         syslog(LOG_ERR, "cannot get uid of daemon: %m");
209                         exit(1);
210                 }
211                 if (setuid(p->pw_uid) == -1) {
212                         syslog(LOG_ERR, "setuid to daemon failed: %m");
213                         exit(1);
214                 }
215         }
216
217         network_init();
218
219         my_svc_run();
220         syslog(LOG_ERR, "svc_run returned unexpectedly");
221         rpcbind_abort();
222         /* NOTREACHED */
223
224         return 0;
225 }
226
227 /*
228  * Adds the entry into the rpcbind database.
229  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
230  * Returns 0 if succeeds, else fails
231  */
232 static int
233 init_transport(struct netconfig *nconf)
234 {
235         int fd;
236         struct t_bind taddr;
237         struct addrinfo hints, *res = NULL;
238         struct __rpc_sockinfo si;
239         SVCXPRT *my_xprt;
240         int status;     /* bound checking ? */
241         int aicode;
242         int addrlen;
243         int nhostsbak;
244         int bound;
245         struct sockaddr *sa;
246         u_int32_t host_addr[4];  /* IPv4 or IPv6 */
247         struct sockaddr_un sun;
248         mode_t oldmask;
249
250         if ((nconf->nc_semantics != NC_TPI_CLTS) &&
251             (nconf->nc_semantics != NC_TPI_COTS) &&
252             (nconf->nc_semantics != NC_TPI_COTS_ORD))
253             return (1); /* not my type */
254 #ifdef ND_DEBUG
255         if (debugging) {
256             int i;
257             char **s;
258
259             fprintf(stderr, "%s: %ld lookup routines :\n",
260                 nconf->nc_netid, nconf->nc_nlookups);
261             for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
262                 i++, s++)
263                 fprintf(stderr, "[%d] - %s\n", i, *s);
264         }
265 #endif
266
267         /*
268          * XXX - using RPC library internal functions.
269          */
270         if ((strcmp(nconf->nc_netid, "local") == 0) ||
271             (strcmp(nconf->nc_netid, "unix") == 0)) {
272             /*
273              * For other transports we call this later, for each socket we
274              * like to bind.
275              */
276             if ((fd = __rpc_nconf2fd(nconf)) < 0) {
277                 int non_fatal = 0;
278                 if (errno == EPROTONOSUPPORT)
279                     non_fatal = 1;
280                 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
281                     nconf->nc_netid);
282                 return (1);
283             }
284         }
285
286         if (!__rpc_nconf2sockinfo(nconf, &si)) {
287             syslog(LOG_ERR, "cannot get information for %s",
288                 nconf->nc_netid);
289             return (1);
290         }
291
292         if ((strcmp(nconf->nc_netid, "local") == 0) ||
293             (strcmp(nconf->nc_netid, "unix") == 0)) {
294             memset(&sun, 0, sizeof sun);
295             sun.sun_family = AF_LOCAL;
296             unlink(_PATH_RPCBINDSOCK);
297             strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
298             sun.sun_len = SUN_LEN(&sun);
299             addrlen = sizeof (struct sockaddr_un);
300             sa = (struct sockaddr *)&sun;
301         } else {
302             /* Get rpcbind's address on this transport */
303
304             memset(&hints, 0, sizeof hints);
305             hints.ai_flags = AI_PASSIVE;
306             hints.ai_family = si.si_af;
307             hints.ai_socktype = si.si_socktype;
308             hints.ai_protocol = si.si_proto;
309         }
310
311         if ((strcmp(nconf->nc_netid, "local") != 0) &&
312             (strcmp(nconf->nc_netid, "unix") != 0)) {
313             /*
314              * If no hosts were specified, just bind to INADDR_ANY.
315              * Otherwise  make sure 127.0.0.1 is added to the list.
316              */
317             nhostsbak = nhosts;
318             nhostsbak++;
319             hosts = realloc(hosts, nhostsbak * sizeof(char *));
320             if (nhostsbak == 1)
321                 hosts[0] = "*";
322             else {
323                 if (hints.ai_family == AF_INET) {
324                     hosts[nhostsbak - 1] = "127.0.0.1";
325                 } else if (hints.ai_family == AF_INET6) {
326                     hosts[nhostsbak - 1] = "::1";
327                 } else
328                     return 1;
329             }
330
331             /*
332              * Bind to specific IPs if asked to
333              */
334             bound = 0;
335             while (nhostsbak > 0) {
336                 --nhostsbak;
337                 /*
338                  * XXX - using RPC library internal functions.
339                  */
340                 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
341                     int non_fatal = 0;
342                     if (errno == EPROTONOSUPPORT &&
343                         nconf->nc_semantics != NC_TPI_CLTS)
344                         non_fatal = 1;
345                     syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
346                         "cannot create socket for %s", nconf->nc_netid);
347                     return (1);
348                 }
349                 switch (hints.ai_family) {
350                 case AF_INET:
351                     if (inet_pton(AF_INET, hosts[nhostsbak],
352                         host_addr) == 1) {
353                         hints.ai_flags &= AI_NUMERICHOST;
354                     } else {
355                         /*
356                          * Skip if we have an AF_INET6 address.
357                          */
358                         if (inet_pton(AF_INET6,
359                             hosts[nhostsbak], host_addr) == 1) {
360                             close(fd);
361                             continue;
362                         }
363                     }
364                     break;
365                 case AF_INET6:
366                     if (inet_pton(AF_INET6, hosts[nhostsbak],
367                         host_addr) == 1) {
368                         hints.ai_flags &= AI_NUMERICHOST;
369                     } else {
370                         /*
371                          * Skip if we have an AF_INET address.
372                          */
373                         if (inet_pton(AF_INET, hosts[nhostsbak],
374                             host_addr) == 1) {
375                                 close(fd);
376                                 continue;
377                         }
378                     }
379                     if (setsockopt(fd, IPPROTO_IPV6,
380                         IPV6_V6ONLY, &on, sizeof on) < 0) {
381                         syslog(LOG_ERR,
382                             "can't set v6-only binding for "
383                             "ipv6 socket: %m");
384                         continue;
385                     }
386                     break;
387                 default:
388                     break;
389                 }
390
391                 /*
392                  * If no hosts were specified, just bind to INADDR_ANY
393                  */
394                 if (strcmp("*", hosts[nhostsbak]) == 0)
395                     hosts[nhostsbak] = NULL;
396                 if ((strcmp(nconf->nc_netid, "local") != 0) &&
397                     (strcmp(nconf->nc_netid, "unix") != 0)) {
398                     if ((aicode = getaddrinfo(hosts[nhostsbak],
399                         servname, &hints, &res)) != 0) {
400                         syslog(LOG_ERR,
401                             "cannot get local address for %s: %s",
402                             nconf->nc_netid, gai_strerror(aicode));
403                         continue;
404                     }
405                     addrlen = res->ai_addrlen;
406                     sa = (struct sockaddr *)res->ai_addr;
407                 }
408                 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
409                 if (bind(fd, sa, addrlen) != 0) {
410                     syslog(LOG_ERR, "cannot bind %s on %s: %m",
411                         (hosts[nhostsbak] == NULL) ? "*" :
412                             hosts[nhostsbak], nconf->nc_netid);
413                     if (res != NULL)
414                         freeaddrinfo(res);
415                     continue;
416                 } else
417                     bound = 1;
418                 umask(oldmask);
419
420                 /* Copy the address */
421                 taddr.addr.len = taddr.addr.maxlen = addrlen;
422                 taddr.addr.buf = malloc(addrlen);
423                 if (taddr.addr.buf == NULL) {
424                     syslog(LOG_ERR,
425                         "cannot allocate memory for %s address",
426                         nconf->nc_netid);
427                     if (res != NULL)
428                         freeaddrinfo(res);
429                     return 1;
430                 }
431                 memcpy(taddr.addr.buf, sa, addrlen);
432 #ifdef ND_DEBUG
433                 if (debugging) {
434                     /*
435                      * for debugging print out our universal
436                      * address
437                      */
438                     char *uaddr;
439                     struct netbuf nb;
440
441                     nb.buf = sa;
442                     nb.len = nb.maxlen = sa->sa_len;
443                     uaddr = taddr2uaddr(nconf, &nb);
444                     fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
445                     free(uaddr);
446                 }
447 #endif
448
449                 if (nconf->nc_semantics != NC_TPI_CLTS)
450                     listen(fd, SOMAXCONN);
451
452                 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
453                     RPC_MAXDATASIZE, RPC_MAXDATASIZE);
454                 if (my_xprt == NULL) {
455                     syslog(LOG_ERR, "%s: could not create service",
456                         nconf->nc_netid);
457                     goto error;
458                 }
459             }
460             if (!bound)
461                 return 1;
462         } else {
463             oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
464             if (bind(fd, sa, addrlen) < 0) {
465                 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
466                 if (res != NULL)
467                     freeaddrinfo(res);
468                 return 1;
469             }
470             umask(oldmask);
471
472             /* Copy the address */
473             taddr.addr.len = taddr.addr.maxlen = addrlen;
474             taddr.addr.buf = malloc(addrlen);
475             if (taddr.addr.buf == NULL) {
476                 syslog(LOG_ERR, "cannot allocate memory for %s address",
477                     nconf->nc_netid);
478                 if (res != NULL)
479                     freeaddrinfo(res);
480                 return 1;
481             }
482             memcpy(taddr.addr.buf, sa, addrlen);
483 #ifdef ND_DEBUG
484             if (debugging) {
485                 /* for debugging print out our universal address */
486                 char *uaddr;
487                 struct netbuf nb;
488
489                 nb.buf = sa;
490                 nb.len = nb.maxlen = sa->sa_len;
491                 uaddr = taddr2uaddr(nconf, &nb);
492                 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
493                 free(uaddr);
494             }
495 #endif
496
497             if (nconf->nc_semantics != NC_TPI_CLTS)
498                 listen(fd, SOMAXCONN);
499
500             my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
501                 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
502             if (my_xprt == NULL) {
503                 syslog(LOG_ERR, "%s: could not create service",
504                     nconf->nc_netid);
505                 goto error;
506             }
507         }
508
509 #ifdef PORTMAP
510         /*
511          * Register both the versions for tcp/ip, udp/ip and local.
512          */
513         if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
514                 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
515                 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
516                 (strcmp(nconf->nc_netid, "unix") == 0) ||
517                 (strcmp(nconf->nc_netid, "local") == 0)) {
518                 struct pmaplist *pml;
519
520                 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
521                         pmap_service, 0)) {
522                         syslog(LOG_ERR, "could not register on %s",
523                                         nconf->nc_netid);
524                         goto error;
525                 }
526                 pml = malloc(sizeof (struct pmaplist));
527                 if (pml == NULL) {
528                         syslog(LOG_ERR, "no memory!");
529                         exit(1);
530                 }
531                 pml->pml_map.pm_prog = PMAPPROG;
532                 pml->pml_map.pm_vers = PMAPVERS;
533                 pml->pml_map.pm_port = PMAPPORT;
534                 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
535                         if (tcptrans[0]) {
536                                 syslog(LOG_ERR,
537                                 "cannot have more than one TCP transport");
538                                 goto error;
539                         }
540                         tcptrans = strdup(nconf->nc_netid);
541                         pml->pml_map.pm_prot = IPPROTO_TCP;
542
543                         /* Let's snarf the universal address */
544                         /* "h1.h2.h3.h4.p1.p2" */
545                         tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
546                 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
547                         if (udptrans[0]) {
548                                 syslog(LOG_ERR,
549                                 "cannot have more than one UDP transport");
550                                 goto error;
551                         }
552                         udptrans = strdup(nconf->nc_netid);
553                         pml->pml_map.pm_prot = IPPROTO_UDP;
554
555                         /* Let's snarf the universal address */
556                         /* "h1.h2.h3.h4.p1.p2" */
557                         udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
558                 } else if (strcmp(nconf->nc_netid, "local") == 0)
559                         pml->pml_map.pm_prot = IPPROTO_ST;
560                 else if (strcmp(nconf->nc_netid, "unix") == 0)
561                         pml->pml_map.pm_prot = IPPROTO_ST;
562                 pml->pml_next = list_pml;
563                 list_pml = pml;
564
565                 /* Add version 3 information */
566                 pml = malloc(sizeof (struct pmaplist));
567                 if (pml == NULL) {
568                         syslog(LOG_ERR, "no memory!");
569                         exit(1);
570                 }
571                 pml->pml_map = list_pml->pml_map;
572                 pml->pml_map.pm_vers = RPCBVERS;
573                 pml->pml_next = list_pml;
574                 list_pml = pml;
575
576                 /* Add version 4 information */
577                 pml = malloc (sizeof (struct pmaplist));
578                 if (pml == NULL) {
579                         syslog(LOG_ERR, "no memory!");
580                         exit(1);
581                 }
582                 pml->pml_map = list_pml->pml_map;
583                 pml->pml_map.pm_vers = RPCBVERS4;
584                 pml->pml_next = list_pml;
585                 list_pml = pml;
586
587                 /* Also add version 2 stuff to rpcbind list */
588                 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
589         }
590 #endif
591
592         /* version 3 registration */
593         if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
594                 syslog(LOG_ERR, "could not register %s version 3",
595                                 nconf->nc_netid);
596                 goto error;
597         }
598         rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
599
600         /* version 4 registration */
601         if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
602                 syslog(LOG_ERR, "could not register %s version 4",
603                                 nconf->nc_netid);
604                 goto error;
605         }
606         rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
607
608         /* decide if bound checking works for this transport */
609         status = add_bndlist(nconf, &taddr.addr);
610 #ifdef BIND_DEBUG
611         if (debugging) {
612                 if (status < 0) {
613                         fprintf(stderr, "Error in finding bind status for %s\n",
614                                 nconf->nc_netid);
615                 } else if (status == 0) {
616                         fprintf(stderr, "check binding for %s\n",
617                                 nconf->nc_netid);
618                 } else if (status > 0) {
619                         fprintf(stderr, "No check binding for %s\n",
620                                 nconf->nc_netid);
621                 }
622         }
623 #endif
624         /*
625          * rmtcall only supported on CLTS transports for now.
626          */
627         if (nconf->nc_semantics == NC_TPI_CLTS) {
628                 status = create_rmtcall_fd(nconf);
629
630 #ifdef BIND_DEBUG
631                 if (debugging) {
632                         if (status < 0) {
633                                 fprintf(stderr,
634                                     "Could not create rmtcall fd for %s\n",
635                                         nconf->nc_netid);
636                         } else {
637                                 fprintf(stderr, "rmtcall fd for %s is %d\n",
638                                         nconf->nc_netid, status);
639                         }
640                 }
641 #endif
642         }
643         return (0);
644 error:
645         close(fd);
646         return (1);
647 }
648
649 static void
650 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
651             struct netbuf *addr)
652 {
653         rpcblist_ptr rbl;
654
655         rbl = malloc(sizeof (rpcblist));
656         if (rbl == NULL) {
657                 syslog(LOG_ERR, "no memory!");
658                 exit(1);
659         }
660
661         rbl->rpcb_map.r_prog = prog;
662         rbl->rpcb_map.r_vers = vers;
663         rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
664         rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
665         rbl->rpcb_map.r_owner = strdup(superuser);
666         rbl->rpcb_next = list_rbl;      /* Attach to global list */
667         list_rbl = rbl;
668 }
669
670 /*
671  * Catch the signal and die
672  */
673 static void
674 terminate(int dummy __unused)
675 {
676         close(rpcbindlockfd);
677 #ifdef WARMSTART
678         syslog(LOG_ERR,
679                 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
680         write_warmstart();      /* Dump yourself */
681 #endif
682         exit(2);
683 }
684
685 void
686 rpcbind_abort(void)
687 {
688 #ifdef WARMSTART
689         write_warmstart();      /* Dump yourself */
690 #endif
691         abort();
692 }
693
694 /* get command line options */
695 static void
696 parseargs(int argc, char *argv[])
697 {
698         int c;
699
700 #ifdef WARMSTART
701 #define WSOP    "w"
702 #else
703 #define WSOP    ""
704 #endif
705         while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
706                 switch (c) {
707                 case '6':
708                         ipv6_only = 1;
709                         break;
710                 case 'a':
711                         doabort = 1;    /* when debugging, do an abort on */
712                         break;          /* errors; for rpcbind developers */
713                                         /* only! */
714                 case 'd':
715                         debugging = 1;
716                         break;
717                 case 'h':
718                         ++nhosts;
719                         hosts = realloc(hosts, nhosts * sizeof(char *));
720                         if (hosts == NULL)
721                                 errx(1, "Out of memory");
722                         hosts[nhosts - 1] = strdup(optarg);
723                         if (hosts[nhosts - 1] == NULL)
724                                 errx(1, "Out of memory");
725                         break;
726                 case 'i':
727                         insecure = 1;
728                         break;
729                 case 'L':
730                         oldstyle_local = 1;
731                         break;
732                 case 'l':
733                         verboselog = 1;
734                         break;
735                 case 's':
736                         runasdaemon = 1;
737                         break;
738 #ifdef WARMSTART
739                 case 'w':
740                         warmstart = 1;
741                         break;
742 #endif
743                 default:        /* error */
744                         fprintf(stderr,
745                             "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
746                             WSOP);
747                         exit (1);
748                 }
749         }
750         if (doabort && !debugging) {
751             fprintf(stderr,
752                 "-a (abort) specified without -d (debugging) -- ignored.\n");
753             doabort = 0;
754         }
755 #undef WSOP
756 }
757
758 void
759 reap(int dummy __unused)
760 {
761         int save_errno = errno;
762
763         while (wait3(NULL, WNOHANG, NULL) > 0)
764                 ;
765         errno = save_errno;
766 }
767
768 void
769 toggle_verboselog(int dummy __unused)
770 {
771         verboselog = !verboselog;
772 }