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