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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
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 $
34 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
39 * Implements the program, version to address mapping for rpc.
43 #include <sys/types.h>
45 #include <sys/errno.h>
47 #include <sys/resource.h>
49 #include <sys/signal.h>
50 #include <sys/socket.h>
53 #include <rpc/rpc_com.h>
55 #include <netinet/in.h>
57 #include <arpa/inet.h>
61 #include <netconfig.h>
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 */
77 /* who to suid to if -s is given */
78 #define RUN_AS "daemon"
80 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
84 int oldstyle_local = 0;
95 static int warmstart = 0; /* Grab an old copy of registrations. */
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 */
105 static char servname[] = "rpcbind";
106 static char superuser[] = "superuser";
108 static int init_transport(struct netconfig *);
109 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
111 static void terminate(int);
112 static void parseargs(int, char *[]);
115 main(int argc, char *argv[])
117 struct netconfig *nconf;
118 void *nc_handle; /* Net config handle */
120 int maxrec = RPC_MAXDATASIZE;
122 parseargs(argc, argv);
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);
129 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
130 errx(1, "another rpcbind is already running. Aborting");
132 getrlimit(RLIMIT_NOFILE, &rl);
133 if (rl.rlim_cur < 128) {
134 if (rl.rlim_max <= 128)
135 rl.rlim_cur = rl.rlim_max;
138 setrlimit(RLIMIT_NOFILE, &rl);
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");
145 nc_handle = setnetconfig(); /* open netconfig file */
146 if (nc_handle == NULL) {
147 syslog(LOG_ERR, "could not read /etc/netconfig");
155 nconf = getnetconfigent("local");
157 nconf = getnetconfigent("unix");
159 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
163 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
165 init_transport(nconf);
167 while ((nconf = getnetconfig(nc_handle))) {
168 if (nconf->nc_flag & NC_VISIBLE)
169 if (ipv6_only != 1 || strcmp(nconf->nc_protofmly, "inet") != 0)
170 init_transport(nconf);
172 endnetconfig(nc_handle);
174 /* catch the usual termination signals for graceful exit */
175 signal(SIGCHLD, reap);
176 signal(SIGINT, terminate);
177 signal(SIGTERM, terminate);
178 signal(SIGQUIT, terminate);
179 /* ignore others that could get sent */
180 signal(SIGPIPE, SIG_IGN);
181 signal(SIGHUP, SIG_IGN);
182 signal(SIGUSR1, SIG_IGN);
183 signal(SIGUSR2, SIG_IGN);
190 printf("rpcbind debugging enabled.");
192 printf(" Will abort on errors!\n");
198 err(1, "fork failed");
204 if((p = getpwnam(RUN_AS)) == NULL) {
205 syslog(LOG_ERR, "cannot get uid of daemon: %m");
208 if (setuid(p->pw_uid) == -1) {
209 syslog(LOG_ERR, "setuid to daemon failed: %m");
217 syslog(LOG_ERR, "svc_run returned unexpectedly");
225 * Adds the entry into the rpcbind database.
226 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
227 * Returns 0 if succeeds, else fails
230 init_transport(struct netconfig *nconf)
234 struct addrinfo hints, *res = NULL;
235 struct __rpc_sockinfo si;
237 int status; /* bound checking ? */
243 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
244 struct sockaddr_un sun;
247 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
248 (nconf->nc_semantics != NC_TPI_COTS) &&
249 (nconf->nc_semantics != NC_TPI_COTS_ORD))
250 return (1); /* not my type */
256 fprintf(stderr, "%s: %ld lookup routines :\n",
257 nconf->nc_netid, nconf->nc_nlookups);
258 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
260 fprintf(stderr, "[%d] - %s\n", i, *s);
265 * XXX - using RPC library internal functions.
267 if ((strcmp(nconf->nc_netid, "local") == 0) ||
268 (strcmp(nconf->nc_netid, "unix") == 0)) {
270 * For other transports we call this later, for each socket we
273 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
275 if (errno == EPROTONOSUPPORT)
277 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
283 if (!__rpc_nconf2sockinfo(nconf, &si)) {
284 syslog(LOG_ERR, "cannot get information for %s",
289 if ((strcmp(nconf->nc_netid, "local") == 0) ||
290 (strcmp(nconf->nc_netid, "unix") == 0)) {
291 memset(&sun, 0, sizeof sun);
292 sun.sun_family = AF_LOCAL;
293 unlink(_PATH_RPCBINDSOCK);
294 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
295 sun.sun_len = SUN_LEN(&sun);
296 addrlen = sizeof (struct sockaddr_un);
297 sa = (struct sockaddr *)&sun;
299 /* Get rpcbind's address on this transport */
301 memset(&hints, 0, sizeof hints);
302 hints.ai_flags = AI_PASSIVE;
303 hints.ai_family = si.si_af;
304 hints.ai_socktype = si.si_socktype;
305 hints.ai_protocol = si.si_proto;
308 if ((strcmp(nconf->nc_netid, "local") != 0) &&
309 (strcmp(nconf->nc_netid, "unix") != 0)) {
311 * If no hosts were specified, just bind to INADDR_ANY.
312 * Otherwise make sure 127.0.0.1 is added to the list.
316 hosts = realloc(hosts, nhostsbak * sizeof(char *));
320 if (hints.ai_family == AF_INET) {
321 hosts[nhostsbak - 1] = "127.0.0.1";
322 } else if (hints.ai_family == AF_INET6) {
323 hosts[nhostsbak - 1] = "::1";
329 * Bind to specific IPs if asked to
332 while (nhostsbak > 0) {
335 * XXX - using RPC library internal functions.
337 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
339 if (errno == EPROTONOSUPPORT &&
340 nconf->nc_semantics != NC_TPI_CLTS)
342 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
343 "cannot create socket for %s", nconf->nc_netid);
346 switch (hints.ai_family) {
348 if (inet_pton(AF_INET, hosts[nhostsbak],
350 hints.ai_flags &= AI_NUMERICHOST;
353 * Skip if we have an AF_INET6 address.
355 if (inet_pton(AF_INET6,
356 hosts[nhostsbak], host_addr) == 1) {
363 if (inet_pton(AF_INET6, hosts[nhostsbak],
365 hints.ai_flags &= AI_NUMERICHOST;
368 * Skip if we have an AF_INET address.
370 if (inet_pton(AF_INET, hosts[nhostsbak],
376 if (setsockopt(fd, IPPROTO_IPV6,
377 IPV6_V6ONLY, &on, sizeof on) < 0) {
379 "can't set v6-only binding for "
389 * If no hosts were specified, just bind to INADDR_ANY
391 if (strcmp("*", hosts[nhostsbak]) == 0)
392 hosts[nhostsbak] = NULL;
393 if ((strcmp(nconf->nc_netid, "local") != 0) &&
394 (strcmp(nconf->nc_netid, "unix") != 0)) {
395 if ((aicode = getaddrinfo(hosts[nhostsbak],
396 servname, &hints, &res)) != 0) {
398 "cannot get local address for %s: %s",
399 nconf->nc_netid, gai_strerror(aicode));
402 addrlen = res->ai_addrlen;
403 sa = (struct sockaddr *)res->ai_addr;
405 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
406 if (bind(fd, sa, addrlen) != 0) {
407 syslog(LOG_ERR, "cannot bind %s on %s: %m",
408 (hosts[nhostsbak] == NULL) ? "*" :
409 hosts[nhostsbak], nconf->nc_netid);
417 /* Copy the address */
418 taddr.addr.len = taddr.addr.maxlen = addrlen;
419 taddr.addr.buf = malloc(addrlen);
420 if (taddr.addr.buf == NULL) {
422 "cannot allocate memory for %s address",
428 memcpy(taddr.addr.buf, sa, addrlen);
432 * for debugging print out our universal
439 nb.len = nb.maxlen = sa->sa_len;
440 uaddr = taddr2uaddr(nconf, &nb);
441 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
446 if (nconf->nc_semantics != NC_TPI_CLTS)
447 listen(fd, SOMAXCONN);
449 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
450 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
451 if (my_xprt == NULL) {
452 syslog(LOG_ERR, "%s: could not create service",
460 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
461 if (bind(fd, sa, addrlen) < 0) {
462 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
469 /* Copy the address */
470 taddr.addr.len = taddr.addr.maxlen = addrlen;
471 taddr.addr.buf = malloc(addrlen);
472 if (taddr.addr.buf == NULL) {
473 syslog(LOG_ERR, "cannot allocate memory for %s address",
479 memcpy(taddr.addr.buf, sa, addrlen);
482 /* for debugging print out our universal address */
487 nb.len = nb.maxlen = sa->sa_len;
488 uaddr = taddr2uaddr(nconf, &nb);
489 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
494 if (nconf->nc_semantics != NC_TPI_CLTS)
495 listen(fd, SOMAXCONN);
497 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
498 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
499 if (my_xprt == NULL) {
500 syslog(LOG_ERR, "%s: could not create service",
508 * Register both the versions for tcp/ip, udp/ip and local.
510 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
511 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
512 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
513 (strcmp(nconf->nc_netid, "unix") == 0) ||
514 (strcmp(nconf->nc_netid, "local") == 0)) {
515 struct pmaplist *pml;
517 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
519 syslog(LOG_ERR, "could not register on %s",
523 pml = malloc(sizeof (struct pmaplist));
525 syslog(LOG_ERR, "no memory!");
528 pml->pml_map.pm_prog = PMAPPROG;
529 pml->pml_map.pm_vers = PMAPVERS;
530 pml->pml_map.pm_port = PMAPPORT;
531 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
534 "cannot have more than one TCP transport");
537 tcptrans = strdup(nconf->nc_netid);
538 pml->pml_map.pm_prot = IPPROTO_TCP;
540 /* Let's snarf the universal address */
541 /* "h1.h2.h3.h4.p1.p2" */
542 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
543 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
546 "cannot have more than one UDP transport");
549 udptrans = strdup(nconf->nc_netid);
550 pml->pml_map.pm_prot = IPPROTO_UDP;
552 /* Let's snarf the universal address */
553 /* "h1.h2.h3.h4.p1.p2" */
554 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
555 } else if (strcmp(nconf->nc_netid, "local") == 0)
556 pml->pml_map.pm_prot = IPPROTO_ST;
557 else if (strcmp(nconf->nc_netid, "unix") == 0)
558 pml->pml_map.pm_prot = IPPROTO_ST;
559 pml->pml_next = list_pml;
562 /* Add version 3 information */
563 pml = malloc(sizeof (struct pmaplist));
565 syslog(LOG_ERR, "no memory!");
568 pml->pml_map = list_pml->pml_map;
569 pml->pml_map.pm_vers = RPCBVERS;
570 pml->pml_next = list_pml;
573 /* Add version 4 information */
574 pml = malloc (sizeof (struct pmaplist));
576 syslog(LOG_ERR, "no memory!");
579 pml->pml_map = list_pml->pml_map;
580 pml->pml_map.pm_vers = RPCBVERS4;
581 pml->pml_next = list_pml;
584 /* Also add version 2 stuff to rpcbind list */
585 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
589 /* version 3 registration */
590 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
591 syslog(LOG_ERR, "could not register %s version 3",
595 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
597 /* version 4 registration */
598 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
599 syslog(LOG_ERR, "could not register %s version 4",
603 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
605 /* decide if bound checking works for this transport */
606 status = add_bndlist(nconf, &taddr.addr);
610 fprintf(stderr, "Error in finding bind status for %s\n",
612 } else if (status == 0) {
613 fprintf(stderr, "check binding for %s\n",
615 } else if (status > 0) {
616 fprintf(stderr, "No check binding for %s\n",
622 * rmtcall only supported on CLTS transports for now.
624 if (nconf->nc_semantics == NC_TPI_CLTS) {
625 status = create_rmtcall_fd(nconf);
631 "Could not create rmtcall fd for %s\n",
634 fprintf(stderr, "rmtcall fd for %s is %d\n",
635 nconf->nc_netid, status);
647 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
652 rbl = malloc(sizeof (rpcblist));
654 syslog(LOG_ERR, "no memory!");
658 rbl->rpcb_map.r_prog = prog;
659 rbl->rpcb_map.r_vers = vers;
660 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
661 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
662 rbl->rpcb_map.r_owner = strdup(superuser);
663 rbl->rpcb_next = list_rbl; /* Attach to global list */
668 * Catch the signal and die
671 terminate(int dummy __unused)
673 close(rpcbindlockfd);
676 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
677 write_warmstart(); /* Dump yourself */
686 write_warmstart(); /* Dump yourself */
691 /* get command line options */
693 parseargs(int argc, char *argv[])
702 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
708 doabort = 1; /* when debugging, do an abort on */
709 break; /* errors; for rpcbind developers */
716 hosts = realloc(hosts, nhosts * sizeof(char *));
718 errx(1, "Out of memory");
719 hosts[nhosts - 1] = strdup(optarg);
720 if (hosts[nhosts - 1] == NULL)
721 errx(1, "Out of memory");
742 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
747 if (doabort && !debugging) {
749 "-a (abort) specified without -d (debugging) -- ignored.\n");
756 reap(int dummy __unused)
758 int save_errno = errno;
760 while (wait3(NULL, WNOHANG, NULL) > 0)
766 toggle_verboselog(int dummy __unused)
768 verboselog = !verboselog;