1 /* $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $ */
5 * Copyright (c) 2009, Sun Microsystems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * - Neither the name of Sun Microsystems, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
39 static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
45 * Implements the program, version to address mapping for rpc.
49 #include <sys/types.h>
51 #include <sys/errno.h>
53 #include <sys/resource.h>
55 #include <sys/signal.h>
56 #include <sys/socket.h>
59 #include <rpc/rpc_com.h>
61 #include <netinet/in.h>
63 #include <arpa/inet.h>
67 #include <netconfig.h>
78 /* Global variables */
79 int debugging = 0; /* Tell me what's going on */
80 int doabort = 0; /* When debugging, do an abort on errors */
81 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
83 /* who to suid to if -s is given */
84 #define RUN_AS "daemon"
86 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
88 static int runasdaemon = 0;
90 int oldstyle_local = 0;
96 static char **hosts = NULL;
97 static struct sockaddr **bound_sa;
98 static int ipv6_only = 0;
99 static int nhosts = 0;
101 static int rpcbindlockfd;
105 static int warmstart = 0; /* Grab an old copy of registrations. */
109 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
110 char *udptrans; /* Name of UDP transport */
111 char *tcptrans; /* Name of TCP transport */
112 char *udp_uaddr; /* Universal UDP address */
113 char *tcp_uaddr; /* Universal TCP address */
115 static char servname[] = "rpcbind";
116 static char superuser[] = "superuser";
118 int main(int, char *[]);
120 static int init_transport(struct netconfig *);
121 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
123 static void terminate(int);
124 static void parseargs(int, char *[]);
125 static void update_bound_sa(void);
128 main(int argc, char *argv[])
130 struct netconfig *nconf;
131 void *nc_handle; /* Net config handle */
133 int maxrec = RPC_MAXDATASIZE;
135 parseargs(argc, argv);
139 /* Check that another rpcbind isn't already running. */
140 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
141 O_RDONLY|O_CREAT, 0444))) == -1)
142 err(1, "%s", RPCBINDDLOCK);
144 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
145 errx(1, "another rpcbind is already running. Aborting");
147 getrlimit(RLIMIT_NOFILE, &rl);
148 if (rl.rlim_cur < 128) {
149 if (rl.rlim_max <= 128)
150 rl.rlim_cur = rl.rlim_max;
153 setrlimit(RLIMIT_NOFILE, &rl);
155 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
156 if (geteuid()) { /* This command allowed only to root */
157 fprintf(stderr, "Sorry. You are not superuser\n");
160 nc_handle = setnetconfig(); /* open netconfig file */
161 if (nc_handle == NULL) {
162 syslog(LOG_ERR, "could not read /etc/netconfig");
170 nconf = getnetconfigent("local");
172 nconf = getnetconfigent("unix");
174 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
178 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
180 init_transport(nconf);
182 while ((nconf = getnetconfig(nc_handle))) {
183 if (nconf->nc_flag & NC_VISIBLE) {
184 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
188 init_transport(nconf);
191 endnetconfig(nc_handle);
193 /* catch the usual termination signals for graceful exit */
194 (void) signal(SIGCHLD, reap);
195 (void) signal(SIGINT, terminate);
196 (void) signal(SIGTERM, terminate);
197 (void) signal(SIGQUIT, terminate);
198 /* ignore others that could get sent */
199 (void) signal(SIGPIPE, SIG_IGN);
200 (void) signal(SIGHUP, SIG_IGN);
201 (void) signal(SIGUSR1, SIG_IGN);
202 (void) signal(SIGUSR2, SIG_IGN);
209 printf("rpcbind debugging enabled.");
211 printf(" Will abort on errors!\n");
217 err(1, "fork failed");
223 if((p = getpwnam(RUN_AS)) == NULL) {
224 syslog(LOG_ERR, "cannot get uid of daemon: %m");
227 if (setuid(p->pw_uid) == -1) {
228 syslog(LOG_ERR, "setuid to daemon failed: %m");
236 syslog(LOG_ERR, "svc_run returned unexpectedly");
244 * Adds the entry into the rpcbind database.
245 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
246 * Returns 0 if succeeds, else fails
249 init_transport(struct netconfig *nconf)
253 struct addrinfo hints, *res = NULL;
254 struct __rpc_sockinfo si;
256 int status; /* bound checking ? */
262 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
263 struct sockaddr_un sun;
266 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
267 (nconf->nc_semantics != NC_TPI_COTS) &&
268 (nconf->nc_semantics != NC_TPI_COTS_ORD))
269 return (1); /* not my type */
275 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
276 nconf->nc_netid, nconf->nc_nlookups);
277 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
279 fprintf(stderr, "[%d] - %s\n", i, *s);
284 * XXX - using RPC library internal functions.
286 if ((strcmp(nconf->nc_netid, "local") == 0) ||
287 (strcmp(nconf->nc_netid, "unix") == 0)) {
289 * For other transports we call this later, for each socket we
292 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
294 if (errno == EAFNOSUPPORT)
296 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
302 if (!__rpc_nconf2sockinfo(nconf, &si)) {
303 syslog(LOG_ERR, "cannot get information for %s",
308 if ((strcmp(nconf->nc_netid, "local") == 0) ||
309 (strcmp(nconf->nc_netid, "unix") == 0)) {
310 memset(&sun, 0, sizeof sun);
311 sun.sun_family = AF_LOCAL;
312 unlink(_PATH_RPCBINDSOCK);
313 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
314 sun.sun_len = SUN_LEN(&sun);
315 addrlen = sizeof (struct sockaddr_un);
316 sa = (struct sockaddr *)&sun;
318 /* Get rpcbind's address on this transport */
320 memset(&hints, 0, sizeof hints);
321 hints.ai_flags = AI_PASSIVE;
322 hints.ai_family = si.si_af;
323 hints.ai_socktype = si.si_socktype;
324 hints.ai_protocol = si.si_proto;
327 if ((strcmp(nconf->nc_netid, "local") != 0) &&
328 (strcmp(nconf->nc_netid, "unix") != 0)) {
330 * If no hosts were specified, just bind to INADDR_ANY.
331 * Otherwise make sure 127.0.0.1 is added to the list.
333 nhostsbak = nhosts + 1;
334 hosts = realloc(hosts, nhostsbak * sizeof(char *));
338 if (hints.ai_family == AF_INET) {
339 hosts[nhostsbak - 1] = "127.0.0.1";
340 } else if (hints.ai_family == AF_INET6) {
341 hosts[nhostsbak - 1] = "::1";
347 * Bind to specific IPs if asked to
350 while (nhostsbak > 0) {
353 * XXX - using RPC library internal functions.
355 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
357 if (errno == EAFNOSUPPORT &&
358 nconf->nc_semantics != NC_TPI_CLTS)
360 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
361 "cannot create socket for %s", nconf->nc_netid);
364 switch (hints.ai_family) {
366 if (inet_pton(AF_INET, hosts[nhostsbak],
368 hints.ai_flags &= AI_NUMERICHOST;
371 * Skip if we have an AF_INET6 address.
373 if (inet_pton(AF_INET6,
374 hosts[nhostsbak], host_addr) == 1) {
381 if (inet_pton(AF_INET6, hosts[nhostsbak],
383 hints.ai_flags &= AI_NUMERICHOST;
386 * Skip if we have an AF_INET address.
388 if (inet_pton(AF_INET, hosts[nhostsbak],
394 if (setsockopt(fd, IPPROTO_IPV6,
395 IPV6_V6ONLY, &on, sizeof on) < 0) {
397 "can't set v6-only binding for "
407 * If no hosts were specified, just bind to INADDR_ANY
409 if (strcmp("*", hosts[nhostsbak]) == 0)
410 hosts[nhostsbak] = NULL;
411 if ((strcmp(nconf->nc_netid, "local") != 0) &&
412 (strcmp(nconf->nc_netid, "unix") != 0)) {
413 if ((aicode = getaddrinfo(hosts[nhostsbak],
414 servname, &hints, &res)) != 0) {
416 "cannot get local address for %s: %s",
417 nconf->nc_netid, gai_strerror(aicode));
420 addrlen = res->ai_addrlen;
421 sa = (struct sockaddr *)res->ai_addr;
423 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
424 if (bind(fd, sa, addrlen) != 0) {
425 syslog(LOG_ERR, "cannot bind %s on %s: %m",
426 (hosts[nhostsbak] == NULL) ? "*" :
427 hosts[nhostsbak], nconf->nc_netid);
433 (void)umask(oldmask);
435 /* Copy the address */
436 taddr.addr.len = taddr.addr.maxlen = addrlen;
437 taddr.addr.buf = malloc(addrlen);
438 if (taddr.addr.buf == NULL) {
440 "cannot allocate memory for %s address",
446 memcpy(taddr.addr.buf, sa, addrlen);
450 * for debugging print out our universal
457 nb.len = nb.maxlen = sa->sa_len;
458 uaddr = taddr2uaddr(nconf, &nb);
459 (void)fprintf(stderr,
460 "rpcbind : my address is %s\n", uaddr);
465 if (nconf->nc_semantics != NC_TPI_CLTS)
466 listen(fd, SOMAXCONN);
468 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
469 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
470 if (my_xprt == (SVCXPRT *)NULL) {
471 syslog(LOG_ERR, "%s: could not create service",
479 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
480 if (bind(fd, sa, addrlen) < 0) {
481 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
486 (void) umask(oldmask);
488 /* Copy the address */
489 taddr.addr.len = taddr.addr.maxlen = addrlen;
490 taddr.addr.buf = malloc(addrlen);
491 if (taddr.addr.buf == NULL) {
492 syslog(LOG_ERR, "cannot allocate memory for %s address",
498 memcpy(taddr.addr.buf, sa, addrlen);
501 /* for debugging print out our universal address */
506 nb.len = nb.maxlen = sa->sa_len;
507 uaddr = taddr2uaddr(nconf, &nb);
508 (void) fprintf(stderr, "rpcbind : my address is %s\n",
514 if (nconf->nc_semantics != NC_TPI_CLTS)
515 listen(fd, SOMAXCONN);
517 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
518 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
519 if (my_xprt == (SVCXPRT *)NULL) {
520 syslog(LOG_ERR, "%s: could not create service",
528 * Register both the versions for tcp/ip, udp/ip and local.
530 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
531 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
532 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
533 (strcmp(nconf->nc_netid, "unix") == 0) ||
534 (strcmp(nconf->nc_netid, "local") == 0)) {
535 struct pmaplist *pml;
537 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
539 syslog(LOG_ERR, "could not register on %s",
543 pml = malloc(sizeof (struct pmaplist));
545 syslog(LOG_ERR, "no memory!");
548 pml->pml_map.pm_prog = PMAPPROG;
549 pml->pml_map.pm_vers = PMAPVERS;
550 pml->pml_map.pm_port = PMAPPORT;
551 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
554 "cannot have more than one TCP transport");
557 tcptrans = strdup(nconf->nc_netid);
558 pml->pml_map.pm_prot = IPPROTO_TCP;
560 /* Let's snarf the universal address */
561 /* "h1.h2.h3.h4.p1.p2" */
562 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
563 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
566 "cannot have more than one UDP transport");
569 udptrans = strdup(nconf->nc_netid);
570 pml->pml_map.pm_prot = IPPROTO_UDP;
572 /* Let's snarf the universal address */
573 /* "h1.h2.h3.h4.p1.p2" */
574 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
575 } else if (strcmp(nconf->nc_netid, "local") == 0)
576 pml->pml_map.pm_prot = IPPROTO_ST;
577 else if (strcmp(nconf->nc_netid, "unix") == 0)
578 pml->pml_map.pm_prot = IPPROTO_ST;
579 pml->pml_next = list_pml;
582 /* Add version 3 information */
583 pml = malloc(sizeof (struct pmaplist));
585 syslog(LOG_ERR, "no memory!");
588 pml->pml_map = list_pml->pml_map;
589 pml->pml_map.pm_vers = RPCBVERS;
590 pml->pml_next = list_pml;
593 /* Add version 4 information */
594 pml = malloc (sizeof (struct pmaplist));
596 syslog(LOG_ERR, "no memory!");
599 pml->pml_map = list_pml->pml_map;
600 pml->pml_map.pm_vers = RPCBVERS4;
601 pml->pml_next = list_pml;
604 /* Also add version 2 stuff to rpcbind list */
605 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
609 /* version 3 registration */
610 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
611 syslog(LOG_ERR, "could not register %s version 3",
615 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
617 /* version 4 registration */
618 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
619 syslog(LOG_ERR, "could not register %s version 4",
623 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
625 /* decide if bound checking works for this transport */
626 status = add_bndlist(nconf, &taddr.addr);
630 fprintf(stderr, "Error in finding bind status for %s\n",
632 } else if (status == 0) {
633 fprintf(stderr, "check binding for %s\n",
635 } else if (status > 0) {
636 fprintf(stderr, "No check binding for %s\n",
642 * rmtcall only supported on CLTS transports for now.
644 if (nconf->nc_semantics == NC_TPI_CLTS) {
645 status = create_rmtcall_fd(nconf);
651 "Could not create rmtcall fd for %s\n",
654 fprintf(stderr, "rmtcall fd for %s is %d\n",
655 nconf->nc_netid, status);
667 * Create the list of addresses that we're bound to. Normally, this
668 * list is empty because we're listening on the wildcard address
669 * (nhost == 0). If -h is specified on the command line, then
670 * bound_sa will have a list of the addresses that the program binds
671 * to specifically. This function takes that list and converts them to
672 * struct sockaddr * and stores them in bound_sa.
675 update_bound_sa(void)
677 struct addrinfo hints, *res = NULL;
682 bound_sa = malloc(sizeof(*bound_sa) * nhosts);
683 memset(&hints, 0, sizeof(hints));
684 hints.ai_family = PF_UNSPEC;
685 for (i = 0; i < nhosts; i++) {
686 if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
688 bound_sa[i] = malloc(res->ai_addrlen);
689 memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
694 * Match the sa against the list of addresses we've bound to. If
695 * we've not specifically bound to anything, we match everything.
696 * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
697 * in bound_sa, we return true. If not, we return false.
700 listen_addr(const struct sockaddr *sa)
705 * If nhosts == 0, then there were no -h options on the
706 * command line, so all addresses are addresses we're
711 for (i = 0; i < nhosts; i++) {
712 if (bound_sa[i] == NULL ||
713 sa->sa_family != bound_sa[i]->sa_family)
715 switch (sa->sa_family) {
717 if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
718 sizeof(struct in_addr)) == 0)
723 if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
724 sizeof(struct in6_addr)) == 0)
736 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
741 rbl = malloc(sizeof (rpcblist));
743 syslog(LOG_ERR, "no memory!");
747 rbl->rpcb_map.r_prog = prog;
748 rbl->rpcb_map.r_vers = vers;
749 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
750 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
751 rbl->rpcb_map.r_owner = strdup(superuser);
752 rbl->rpcb_next = list_rbl; /* Attach to global list */
757 * Catch the signal and die
760 terminate(int dummy __unused)
762 close(rpcbindlockfd);
765 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
766 write_warmstart(); /* Dump yourself */
775 write_warmstart(); /* Dump yourself */
780 /* get command line options */
782 parseargs(int argc, char *argv[])
796 while ((c = getopt(argc, argv, "6adh:iLls" WRAPOP WSOP)) != -1) {
802 doabort = 1; /* when debugging, do an abort on */
803 break; /* errors; for rpcbind developers */
810 hosts = realloc(hosts, nhosts * sizeof(char *));
812 errx(1, "Out of memory");
813 hosts[nhosts - 1] = strdup(optarg);
814 if (hosts[nhosts - 1] == NULL)
815 errx(1, "Out of memory");
841 "usage: rpcbind [-6adiLls%s%s] [-h bindip]\n",
846 if (doabort && !debugging) {
848 "-a (abort) specified without -d (debugging) -- ignored.\n");
855 reap(int dummy __unused)
857 int save_errno = errno;
859 while (wait3(NULL, WNOHANG, NULL) > 0)
865 toggle_verboselog(int dummy __unused)
867 verboselog = !verboselog;