1 /* $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $ */
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.
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.
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.
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.
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.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
36 /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
40 static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
46 * Implements the program, version to address mapping for rpc.
50 #include <sys/types.h>
52 #include <sys/errno.h>
54 #include <sys/resource.h>
56 #include <sys/signal.h>
57 #include <sys/socket.h>
60 #include <rpc/rpc_com.h>
62 #include <netinet/in.h>
64 #include <arpa/inet.h>
68 #include <netconfig.h>
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 */
84 /* who to suid to if -s is given */
85 #define RUN_AS "daemon"
87 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
91 int oldstyle_local = 0;
102 static int warmstart = 0; /* Grab an old copy of registrations. */
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 */
112 static char servname[] = "rpcbind";
113 static char superuser[] = "superuser";
115 int main __P((int, char *[]));
117 static int init_transport __P((struct netconfig *));
118 static void rbllist_add __P((rpcprog_t, rpcvers_t, struct netconfig *,
120 static void terminate __P((int));
121 static void parseargs __P((int, char *[]));
124 main(int argc, char *argv[])
126 struct netconfig *nconf;
127 void *nc_handle; /* Net config handle */
129 int maxrec = RPC_MAXDATASIZE;
131 parseargs(argc, argv);
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);
138 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
139 errx(1, "another rpcbind is already running. Aborting");
141 getrlimit(RLIMIT_NOFILE, &rl);
142 if (rl.rlim_cur < 128) {
143 if (rl.rlim_max <= 128)
144 rl.rlim_cur = rl.rlim_max;
147 setrlimit(RLIMIT_NOFILE, &rl);
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");
154 nc_handle = setnetconfig(); /* open netconfig file */
155 if (nc_handle == NULL) {
156 syslog(LOG_ERR, "could not read /etc/netconfig");
164 nconf = getnetconfigent("local");
166 nconf = getnetconfigent("unix");
168 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
172 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
174 init_transport(nconf);
176 while ((nconf = getnetconfig(nc_handle))) {
177 if (nconf->nc_flag & NC_VISIBLE)
178 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
182 init_transport(nconf);
184 endnetconfig(nc_handle);
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);
202 printf("rpcbind debugging enabled.");
204 printf(" Will abort on errors!\n");
210 err(1, "fork failed");
216 if((p = getpwnam(RUN_AS)) == NULL) {
217 syslog(LOG_ERR, "cannot get uid of daemon: %m");
220 if (setuid(p->pw_uid) == -1) {
221 syslog(LOG_ERR, "setuid to daemon failed: %m");
229 syslog(LOG_ERR, "svc_run returned unexpectedly");
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
242 init_transport(struct netconfig *nconf)
246 struct addrinfo hints, *res = NULL;
247 struct __rpc_sockinfo si;
249 int status; /* bound checking ? */
255 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
256 struct sockaddr_un sun;
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 */
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;
272 fprintf(stderr, "[%d] - %s\n", i, *s);
277 * XXX - using RPC library internal functions.
279 if ((strcmp(nconf->nc_netid, "local") == 0) ||
280 (strcmp(nconf->nc_netid, "unix") == 0)) {
282 * For other transports we call this later, for each socket we
285 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
287 if (errno == EPROTONOSUPPORT)
289 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
295 if (!__rpc_nconf2sockinfo(nconf, &si)) {
296 syslog(LOG_ERR, "cannot get information for %s",
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;
311 /* Get rpcbind's address on this transport */
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;
320 if ((strcmp(nconf->nc_netid, "local") != 0) &&
321 (strcmp(nconf->nc_netid, "unix") != 0)) {
323 * If no hosts were specified, just bind to INADDR_ANY.
324 * Otherwise make sure 127.0.0.1 is added to the list.
328 hosts = realloc(hosts, nhostsbak * sizeof(char *));
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";
341 * Bind to specific IPs if asked to
344 while (nhostsbak > 0) {
347 * XXX - using RPC library internal functions.
349 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
351 if (errno == EPROTONOSUPPORT &&
352 nconf->nc_semantics != NC_TPI_CLTS)
354 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
355 "cannot create socket for %s", nconf->nc_netid);
358 switch (hints.ai_family) {
360 if (inet_pton(AF_INET, hosts[nhostsbak],
362 hints.ai_flags &= AI_NUMERICHOST;
365 * Skip if we have an AF_INET6 adress.
367 if (inet_pton(AF_INET6,
368 hosts[nhostsbak], host_addr) == 1) {
375 if (inet_pton(AF_INET6, hosts[nhostsbak],
377 hints.ai_flags &= AI_NUMERICHOST;
380 * Skip if we have an AF_INET adress.
382 if (inet_pton(AF_INET, hosts[nhostsbak],
388 if (setsockopt(fd, IPPROTO_IPV6,
389 IPV6_V6ONLY, &on, sizeof on) < 0) {
391 "can't set v6-only binding for "
401 * If no hosts were specified, just bind to INADDR_ANY
403 if (strcmp("*", hosts[nhostsbak]) == 0)
404 hosts[nhostsbak] = NULL;
405 if ((strcmp(nconf->nc_netid, "local") != 0) &&
406 (strcmp(nconf->nc_netid, "unix") != 0)) {
407 if ((aicode = getaddrinfo(hosts[nhostsbak],
408 servname, &hints, &res)) != 0) {
410 "cannot get local address for %s: %s",
411 nconf->nc_netid, gai_strerror(aicode));
414 addrlen = res->ai_addrlen;
415 sa = (struct sockaddr *)res->ai_addr;
417 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
418 if (bind(fd, sa, addrlen) != 0) {
419 syslog(LOG_ERR, "cannot bind %s on %s: %m",
420 (hosts[nhostsbak] == NULL) ? "*" :
421 hosts[nhostsbak], nconf->nc_netid);
427 (void)umask(oldmask);
429 /* Copy the address */
430 taddr.addr.len = taddr.addr.maxlen = addrlen;
431 taddr.addr.buf = malloc(addrlen);
432 if (taddr.addr.buf == NULL) {
434 "cannot allocate memory for %s address",
440 memcpy(taddr.addr.buf, sa, addrlen);
444 * for debugging print out our universal
451 nb.len = nb.maxlen = sa->sa_len;
452 uaddr = taddr2uaddr(nconf, &nb);
453 (void)fprintf(stderr,
454 "rpcbind : my address is %s\n", uaddr);
459 if (nconf->nc_semantics != NC_TPI_CLTS)
460 listen(fd, SOMAXCONN);
462 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
463 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
464 if (my_xprt == (SVCXPRT *)NULL) {
465 syslog(LOG_ERR, "%s: could not create service",
473 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
474 if (bind(fd, sa, addrlen) < 0) {
475 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
480 (void) umask(oldmask);
482 /* Copy the address */
483 taddr.addr.len = taddr.addr.maxlen = addrlen;
484 taddr.addr.buf = malloc(addrlen);
485 if (taddr.addr.buf == NULL) {
486 syslog(LOG_ERR, "cannot allocate memory for %s address",
492 memcpy(taddr.addr.buf, sa, addrlen);
495 /* for debugging print out our universal address */
500 nb.len = nb.maxlen = sa->sa_len;
501 uaddr = taddr2uaddr(nconf, &nb);
502 (void) fprintf(stderr, "rpcbind : my address is %s\n",
508 if (nconf->nc_semantics != NC_TPI_CLTS)
509 listen(fd, SOMAXCONN);
511 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
512 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
513 if (my_xprt == (SVCXPRT *)NULL) {
514 syslog(LOG_ERR, "%s: could not create service",
522 * Register both the versions for tcp/ip, udp/ip and local.
524 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
525 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
526 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
527 (strcmp(nconf->nc_netid, "unix") == 0) ||
528 (strcmp(nconf->nc_netid, "local") == 0)) {
529 struct pmaplist *pml;
531 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
533 syslog(LOG_ERR, "could not register on %s",
537 pml = malloc(sizeof (struct pmaplist));
539 syslog(LOG_ERR, "no memory!");
542 pml->pml_map.pm_prog = PMAPPROG;
543 pml->pml_map.pm_vers = PMAPVERS;
544 pml->pml_map.pm_port = PMAPPORT;
545 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
548 "cannot have more than one TCP transport");
551 tcptrans = strdup(nconf->nc_netid);
552 pml->pml_map.pm_prot = IPPROTO_TCP;
554 /* Let's snarf the universal address */
555 /* "h1.h2.h3.h4.p1.p2" */
556 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
557 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
560 "cannot have more than one UDP transport");
563 udptrans = strdup(nconf->nc_netid);
564 pml->pml_map.pm_prot = IPPROTO_UDP;
566 /* Let's snarf the universal address */
567 /* "h1.h2.h3.h4.p1.p2" */
568 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
569 } else if (strcmp(nconf->nc_netid, "local") == 0)
570 pml->pml_map.pm_prot = IPPROTO_ST;
571 else if (strcmp(nconf->nc_netid, "unix") == 0)
572 pml->pml_map.pm_prot = IPPROTO_ST;
573 pml->pml_next = list_pml;
576 /* Add version 3 information */
577 pml = malloc(sizeof (struct pmaplist));
579 syslog(LOG_ERR, "no memory!");
582 pml->pml_map = list_pml->pml_map;
583 pml->pml_map.pm_vers = RPCBVERS;
584 pml->pml_next = list_pml;
587 /* Add version 4 information */
588 pml = malloc (sizeof (struct pmaplist));
590 syslog(LOG_ERR, "no memory!");
593 pml->pml_map = list_pml->pml_map;
594 pml->pml_map.pm_vers = RPCBVERS4;
595 pml->pml_next = list_pml;
598 /* Also add version 2 stuff to rpcbind list */
599 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
603 /* version 3 registration */
604 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
605 syslog(LOG_ERR, "could not register %s version 3",
609 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
611 /* version 4 registration */
612 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
613 syslog(LOG_ERR, "could not register %s version 4",
617 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
619 /* decide if bound checking works for this transport */
620 status = add_bndlist(nconf, &taddr.addr);
624 fprintf(stderr, "Error in finding bind status for %s\n",
626 } else if (status == 0) {
627 fprintf(stderr, "check binding for %s\n",
629 } else if (status > 0) {
630 fprintf(stderr, "No check binding for %s\n",
636 * rmtcall only supported on CLTS transports for now.
638 if (nconf->nc_semantics == NC_TPI_CLTS) {
639 status = create_rmtcall_fd(nconf);
645 "Could not create rmtcall fd for %s\n",
648 fprintf(stderr, "rmtcall fd for %s is %d\n",
649 nconf->nc_netid, status);
661 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
666 rbl = malloc(sizeof (rpcblist));
668 syslog(LOG_ERR, "no memory!");
672 rbl->rpcb_map.r_prog = prog;
673 rbl->rpcb_map.r_vers = vers;
674 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
675 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
676 rbl->rpcb_map.r_owner = strdup(superuser);
677 rbl->rpcb_next = list_rbl; /* Attach to global list */
682 * Catch the signal and die
685 terminate(int dummy __unused)
687 close(rpcbindlockfd);
690 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
691 write_warmstart(); /* Dump yourself */
700 write_warmstart(); /* Dump yourself */
705 /* get command line options */
707 parseargs(int argc, char *argv[])
716 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
722 doabort = 1; /* when debugging, do an abort on */
723 break; /* errors; for rpcbind developers */
730 hosts = realloc(hosts, nhosts * sizeof(char *));
732 errx(1, "Out of memory");
733 hosts[nhosts - 1] = strdup(optarg);
734 if (hosts[nhosts - 1] == NULL)
735 errx(1, "Out of memory");
756 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
761 if (doabort && !debugging) {
763 "-a (abort) specified without -d (debugging) -- ignored.\n");
770 reap(int dummy __unused)
772 int save_errno = errno;
774 while (wait3(NULL, WNOHANG, NULL) > 0)
780 toggle_verboselog(int dummy __unused)
782 verboselog = !verboselog;