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