2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)rpcbind.c 1.19 94/04/25 SMI; 1.35 89/04/21 Copyr 1984 Sun Micro
29 * $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $
30 * $FreeBSD: src/usr.sbin/rpcbind/rpcbind.c,v 1.20 2008/02/14 20:12:23 yar Exp $
33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
38 * Implements the program, version to address mapping for rpc.
42 #include <sys/types.h>
44 #include <sys/errno.h>
46 #include <sys/resource.h>
48 #include <sys/signal.h>
49 #include <sys/socket.h>
52 #include <rpc/rpc_com.h>
54 #include <netinet/in.h>
56 #include <arpa/inet.h>
60 #include <netconfig.h>
70 /* Global variables */
71 int debugging = 0; /* Tell me what's going on */
72 int doabort = 0; /* When debugging, do an abort on errors */
73 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
75 /* who to suid to if -s is given */
76 #define RUN_AS "daemon"
78 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
82 int oldstyle_local = 0;
93 static int warmstart = 0; /* Grab an old copy of registrations. */
97 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
98 char *udptrans; /* Name of UDP transport */
99 char *tcptrans; /* Name of TCP transport */
100 char *udp_uaddr; /* Universal UDP address */
101 char *tcp_uaddr; /* Universal TCP address */
103 static char servname[] = "rpcbind";
104 static char superuser[] = "superuser";
106 static int init_transport(struct netconfig *);
107 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
109 static void terminate(int);
110 static void parseargs(int, char *[]);
113 main(int argc, char *argv[])
115 struct netconfig *nconf;
116 void *nc_handle; /* Net config handle */
118 int maxrec = RPC_MAXDATASIZE;
120 parseargs(argc, argv);
122 /* Check that another rpcbind isn't already running. */
123 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
124 O_RDONLY|O_CREAT, 0444))) == -1)
125 err(1, "%s", RPCBINDDLOCK);
127 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
128 errx(1, "another rpcbind is already running. Aborting");
130 getrlimit(RLIMIT_NOFILE, &rl);
131 if (rl.rlim_cur < 128) {
132 if (rl.rlim_max <= 128)
133 rl.rlim_cur = rl.rlim_max;
136 setrlimit(RLIMIT_NOFILE, &rl);
138 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
139 if (geteuid()) { /* This command allowed only to root */
140 fprintf(stderr, "Sorry. You are not superuser\n");
143 nc_handle = setnetconfig(); /* open netconfig file */
144 if (nc_handle == NULL) {
145 syslog(LOG_ERR, "could not read /etc/netconfig");
153 nconf = getnetconfigent("local");
155 nconf = getnetconfigent("unix");
157 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
161 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
163 init_transport(nconf);
165 while ((nconf = getnetconfig(nc_handle))) {
166 if (nconf->nc_flag & NC_VISIBLE)
167 if (ipv6_only != 1 || strcmp(nconf->nc_protofmly, "inet") != 0)
168 init_transport(nconf);
170 endnetconfig(nc_handle);
172 /* catch the usual termination signals for graceful exit */
173 signal(SIGCHLD, reap);
174 signal(SIGINT, terminate);
175 signal(SIGTERM, terminate);
176 signal(SIGQUIT, terminate);
177 /* ignore others that could get sent */
178 signal(SIGPIPE, SIG_IGN);
179 signal(SIGHUP, SIG_IGN);
180 signal(SIGUSR1, SIG_IGN);
181 signal(SIGUSR2, SIG_IGN);
188 printf("rpcbind debugging enabled.");
190 printf(" Will abort on errors!\n");
196 err(1, "fork failed");
202 if((p = getpwnam(RUN_AS)) == NULL) {
203 syslog(LOG_ERR, "cannot get uid of daemon: %m");
206 if (setuid(p->pw_uid) == -1) {
207 syslog(LOG_ERR, "setuid to daemon failed: %m");
215 syslog(LOG_ERR, "svc_run returned unexpectedly");
223 * Adds the entry into the rpcbind database.
224 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
225 * Returns 0 if succeeds, else fails
228 init_transport(struct netconfig *nconf)
232 struct addrinfo hints, *res = NULL;
233 struct __rpc_sockinfo si;
235 int status; /* bound checking ? */
241 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
242 struct sockaddr_un sun;
245 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
246 (nconf->nc_semantics != NC_TPI_COTS) &&
247 (nconf->nc_semantics != NC_TPI_COTS_ORD))
248 return (1); /* not my type */
254 fprintf(stderr, "%s: %ld lookup routines :\n",
255 nconf->nc_netid, nconf->nc_nlookups);
256 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
258 fprintf(stderr, "[%d] - %s\n", i, *s);
263 * XXX - using RPC library internal functions.
265 if ((strcmp(nconf->nc_netid, "local") == 0) ||
266 (strcmp(nconf->nc_netid, "unix") == 0)) {
268 * For other transports we call this later, for each socket we
271 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
273 if (errno == EPROTONOSUPPORT)
275 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
281 if (!__rpc_nconf2sockinfo(nconf, &si)) {
282 syslog(LOG_ERR, "cannot get information for %s",
287 if ((strcmp(nconf->nc_netid, "local") == 0) ||
288 (strcmp(nconf->nc_netid, "unix") == 0)) {
289 memset(&sun, 0, sizeof sun);
290 sun.sun_family = AF_LOCAL;
291 unlink(_PATH_RPCBINDSOCK);
292 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
293 sun.sun_len = SUN_LEN(&sun);
294 addrlen = sizeof (struct sockaddr_un);
295 sa = (struct sockaddr *)&sun;
297 /* Get rpcbind's address on this transport */
299 memset(&hints, 0, sizeof hints);
300 hints.ai_flags = AI_PASSIVE;
301 hints.ai_family = si.si_af;
302 hints.ai_socktype = si.si_socktype;
303 hints.ai_protocol = si.si_proto;
306 if ((strcmp(nconf->nc_netid, "local") != 0) &&
307 (strcmp(nconf->nc_netid, "unix") != 0)) {
309 * If no hosts were specified, just bind to INADDR_ANY.
310 * Otherwise make sure 127.0.0.1 is added to the list.
314 hosts = realloc(hosts, nhostsbak * sizeof(char *));
318 if (hints.ai_family == AF_INET) {
319 hosts[nhostsbak - 1] = "127.0.0.1";
320 } else if (hints.ai_family == AF_INET6) {
321 hosts[nhostsbak - 1] = "::1";
327 * Bind to specific IPs if asked to
330 while (nhostsbak > 0) {
333 * XXX - using RPC library internal functions.
335 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
337 if (errno == EPROTONOSUPPORT &&
338 nconf->nc_semantics != NC_TPI_CLTS)
340 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
341 "cannot create socket for %s", nconf->nc_netid);
344 switch (hints.ai_family) {
346 if (inet_pton(AF_INET, hosts[nhostsbak],
348 hints.ai_flags &= AI_NUMERICHOST;
351 * Skip if we have an AF_INET6 address.
353 if (inet_pton(AF_INET6,
354 hosts[nhostsbak], host_addr) == 1) {
361 if (inet_pton(AF_INET6, hosts[nhostsbak],
363 hints.ai_flags &= AI_NUMERICHOST;
366 * Skip if we have an AF_INET address.
368 if (inet_pton(AF_INET, hosts[nhostsbak],
374 if (setsockopt(fd, IPPROTO_IPV6,
375 IPV6_V6ONLY, &on, sizeof on) < 0) {
377 "can't set v6-only binding for "
387 * If no hosts were specified, just bind to INADDR_ANY
389 if (strcmp("*", hosts[nhostsbak]) == 0)
390 hosts[nhostsbak] = NULL;
391 if ((strcmp(nconf->nc_netid, "local") != 0) &&
392 (strcmp(nconf->nc_netid, "unix") != 0)) {
393 if ((aicode = getaddrinfo(hosts[nhostsbak],
394 servname, &hints, &res)) != 0) {
396 "cannot get local address for %s: %s",
397 nconf->nc_netid, gai_strerror(aicode));
400 addrlen = res->ai_addrlen;
401 sa = (struct sockaddr *)res->ai_addr;
403 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
404 if (bind(fd, sa, addrlen) != 0) {
405 syslog(LOG_ERR, "cannot bind %s on %s: %m",
406 (hosts[nhostsbak] == NULL) ? "*" :
407 hosts[nhostsbak], nconf->nc_netid);
415 /* Copy the address */
416 taddr.addr.len = taddr.addr.maxlen = addrlen;
417 taddr.addr.buf = malloc(addrlen);
418 if (taddr.addr.buf == NULL) {
420 "cannot allocate memory for %s address",
426 memcpy(taddr.addr.buf, sa, addrlen);
430 * for debugging print out our universal
437 nb.len = nb.maxlen = sa->sa_len;
438 uaddr = taddr2uaddr(nconf, &nb);
439 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
444 if (nconf->nc_semantics != NC_TPI_CLTS)
445 listen(fd, SOMAXCONN);
447 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
448 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
449 if (my_xprt == NULL) {
450 syslog(LOG_ERR, "%s: could not create service",
458 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
459 if (bind(fd, sa, addrlen) < 0) {
460 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
467 /* Copy the address */
468 taddr.addr.len = taddr.addr.maxlen = addrlen;
469 taddr.addr.buf = malloc(addrlen);
470 if (taddr.addr.buf == NULL) {
471 syslog(LOG_ERR, "cannot allocate memory for %s address",
477 memcpy(taddr.addr.buf, sa, addrlen);
480 /* for debugging print out our universal address */
485 nb.len = nb.maxlen = sa->sa_len;
486 uaddr = taddr2uaddr(nconf, &nb);
487 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
492 if (nconf->nc_semantics != NC_TPI_CLTS)
493 listen(fd, SOMAXCONN);
495 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
496 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
497 if (my_xprt == NULL) {
498 syslog(LOG_ERR, "%s: could not create service",
506 * Register both the versions for tcp/ip, udp/ip and local.
508 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
509 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
510 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
511 (strcmp(nconf->nc_netid, "unix") == 0) ||
512 (strcmp(nconf->nc_netid, "local") == 0)) {
513 struct pmaplist *pml;
515 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
517 syslog(LOG_ERR, "could not register on %s",
521 pml = malloc(sizeof (struct pmaplist));
523 syslog(LOG_ERR, "no memory!");
526 pml->pml_map.pm_prog = PMAPPROG;
527 pml->pml_map.pm_vers = PMAPVERS;
528 pml->pml_map.pm_port = PMAPPORT;
529 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
532 "cannot have more than one TCP transport");
535 tcptrans = strdup(nconf->nc_netid);
536 pml->pml_map.pm_prot = IPPROTO_TCP;
538 /* Let's snarf the universal address */
539 /* "h1.h2.h3.h4.p1.p2" */
540 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
541 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
544 "cannot have more than one UDP transport");
547 udptrans = strdup(nconf->nc_netid);
548 pml->pml_map.pm_prot = IPPROTO_UDP;
550 /* Let's snarf the universal address */
551 /* "h1.h2.h3.h4.p1.p2" */
552 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
553 } else if (strcmp(nconf->nc_netid, "local") == 0)
554 pml->pml_map.pm_prot = IPPROTO_ST;
555 else if (strcmp(nconf->nc_netid, "unix") == 0)
556 pml->pml_map.pm_prot = IPPROTO_ST;
557 pml->pml_next = list_pml;
560 /* Add version 3 information */
561 pml = malloc(sizeof (struct pmaplist));
563 syslog(LOG_ERR, "no memory!");
566 pml->pml_map = list_pml->pml_map;
567 pml->pml_map.pm_vers = RPCBVERS;
568 pml->pml_next = list_pml;
571 /* Add version 4 information */
572 pml = malloc (sizeof (struct pmaplist));
574 syslog(LOG_ERR, "no memory!");
577 pml->pml_map = list_pml->pml_map;
578 pml->pml_map.pm_vers = RPCBVERS4;
579 pml->pml_next = list_pml;
582 /* Also add version 2 stuff to rpcbind list */
583 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
587 /* version 3 registration */
588 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
589 syslog(LOG_ERR, "could not register %s version 3",
593 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
595 /* version 4 registration */
596 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
597 syslog(LOG_ERR, "could not register %s version 4",
601 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
603 /* decide if bound checking works for this transport */
604 status = add_bndlist(nconf, &taddr.addr);
608 fprintf(stderr, "Error in finding bind status for %s\n",
610 } else if (status == 0) {
611 fprintf(stderr, "check binding for %s\n",
613 } else if (status > 0) {
614 fprintf(stderr, "No check binding for %s\n",
620 * rmtcall only supported on CLTS transports for now.
622 if (nconf->nc_semantics == NC_TPI_CLTS) {
623 status = create_rmtcall_fd(nconf);
629 "Could not create rmtcall fd for %s\n",
632 fprintf(stderr, "rmtcall fd for %s is %d\n",
633 nconf->nc_netid, status);
645 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
650 rbl = malloc(sizeof (rpcblist));
652 syslog(LOG_ERR, "no memory!");
656 rbl->rpcb_map.r_prog = prog;
657 rbl->rpcb_map.r_vers = vers;
658 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
659 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
660 rbl->rpcb_map.r_owner = strdup(superuser);
661 rbl->rpcb_next = list_rbl; /* Attach to global list */
666 * Catch the signal and die
669 terminate(int dummy __unused)
671 close(rpcbindlockfd);
674 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
675 write_warmstart(); /* Dump yourself */
684 write_warmstart(); /* Dump yourself */
689 /* get command line options */
691 parseargs(int argc, char *argv[])
700 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
706 doabort = 1; /* when debugging, do an abort on */
707 break; /* errors; for rpcbind developers */
714 hosts = realloc(hosts, nhosts * sizeof(char *));
716 errx(1, "Out of memory");
717 hosts[nhosts - 1] = strdup(optarg);
718 if (hosts[nhosts - 1] == NULL)
719 errx(1, "Out of memory");
740 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
745 if (doabort && !debugging) {
747 "-a (abort) specified without -d (debugging) -- ignored.\n");
754 reap(int dummy __unused)
756 int save_errno = errno;
758 while (wait3(NULL, WNOHANG, NULL) > 0)
764 toggle_verboselog(int dummy __unused)
766 verboselog = !verboselog;