1 /* $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 2009, Sun Microsystems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
37 /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
41 static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
47 * Implements the program, version to address mapping for rpc.
51 #include <sys/types.h>
53 #include <sys/errno.h>
55 #include <sys/resource.h>
57 #include <sys/signal.h>
58 #include <sys/socket.h>
61 #include <rpc/rpc_com.h>
63 #include <netinet/in.h>
65 #include <arpa/inet.h>
69 #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 int terminate_rfd; /* Pipefd to wake on signal */
83 volatile sig_atomic_t doterminate = 0; /* Terminal signal received */
84 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
87 /* who to suid to if -s is given */
88 #define RUN_AS "daemon"
90 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
92 static int runasdaemon = 0;
94 int oldstyle_local = 0;
100 static char **hosts = NULL;
101 static struct sockaddr **bound_sa;
102 static int ipv6_only = 0;
103 static int nhosts = 0;
105 static int terminate_wfd;
109 static int warmstart = 0; /* Grab an old copy of registrations. */
113 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
114 char *udptrans; /* Name of UDP transport */
115 char *tcptrans; /* Name of TCP transport */
116 char *udp_uaddr; /* Universal UDP address */
117 char *tcp_uaddr; /* Universal TCP address */
119 static char servname[] = "rpcbind";
120 static char superuser[] = "superuser";
122 int main(int, char *[]);
124 static int init_transport(struct netconfig *);
125 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
127 static void terminate(int);
128 static void parseargs(int, char *[]);
129 static void update_bound_sa(void);
132 main(int argc, char *argv[])
134 struct netconfig *nconf;
135 void *nc_handle; /* Net config handle */
137 int maxrec = RPC_MAXDATASIZE;
140 parseargs(argc, argv);
144 /* Check that another rpcbind isn't already running. */
145 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
146 O_RDONLY|O_CREAT, 0444))) == -1)
147 err(1, "%s", RPCBINDDLOCK);
149 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
150 errx(1, "another rpcbind is already running. Aborting");
152 getrlimit(RLIMIT_NOFILE, &rl);
153 if (rl.rlim_cur < 128) {
154 if (rl.rlim_max <= 128)
155 rl.rlim_cur = rl.rlim_max;
158 setrlimit(RLIMIT_NOFILE, &rl);
160 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
161 if (geteuid()) { /* This command allowed only to root */
162 fprintf(stderr, "Sorry. You are not superuser\n");
165 nc_handle = setnetconfig(); /* open netconfig file */
166 if (nc_handle == NULL) {
167 syslog(LOG_ERR, "could not read /etc/netconfig");
175 nconf = getnetconfigent("local");
177 nconf = getnetconfigent("unix");
179 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
183 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
185 init_transport(nconf);
187 while ((nconf = getnetconfig(nc_handle))) {
188 if (nconf->nc_flag & NC_VISIBLE) {
189 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
193 init_transport(nconf);
196 endnetconfig(nc_handle);
199 * Allocate pipe fd to wake main thread from signal handler in non-racy
204 err(1, "pipe failed");
205 terminate_rfd = fds[0];
206 terminate_wfd = fds[1];
208 /* catch the usual termination signals for graceful exit */
209 (void) signal(SIGCHLD, reap);
210 (void) signal(SIGINT, terminate);
211 (void) signal(SIGTERM, terminate);
212 (void) signal(SIGQUIT, terminate);
213 /* ignore others that could get sent */
214 (void) signal(SIGPIPE, SIG_IGN);
215 (void) signal(SIGHUP, SIG_IGN);
216 (void) signal(SIGUSR1, SIG_IGN);
217 (void) signal(SIGUSR2, SIG_IGN);
224 printf("rpcbind debugging enabled.");
226 printf(" Will abort on errors!\n");
232 err(1, "fork failed");
238 if((p = getpwnam(RUN_AS)) == NULL) {
239 syslog(LOG_ERR, "cannot get uid of daemon: %m");
242 if (setuid(p->pw_uid) == -1) {
243 syslog(LOG_ERR, "setuid to daemon failed: %m");
251 syslog(LOG_ERR, "svc_run returned unexpectedly");
259 * Adds the entry into the rpcbind database.
260 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
261 * Returns 0 if succeeds, else fails
264 init_transport(struct netconfig *nconf)
268 struct addrinfo hints, *res = NULL;
269 struct __rpc_sockinfo si;
271 int status; /* bound checking ? */
277 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
278 struct sockaddr_un sun;
281 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
282 (nconf->nc_semantics != NC_TPI_COTS) &&
283 (nconf->nc_semantics != NC_TPI_COTS_ORD))
284 return (1); /* not my type */
290 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
291 nconf->nc_netid, nconf->nc_nlookups);
292 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
294 fprintf(stderr, "[%d] - %s\n", i, *s);
299 * XXX - using RPC library internal functions.
301 if ((strcmp(nconf->nc_netid, "local") == 0) ||
302 (strcmp(nconf->nc_netid, "unix") == 0)) {
304 * For other transports we call this later, for each socket we
307 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
309 if (errno == EAFNOSUPPORT)
311 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
317 if (!__rpc_nconf2sockinfo(nconf, &si)) {
318 syslog(LOG_ERR, "cannot get information for %s",
323 if ((strcmp(nconf->nc_netid, "local") == 0) ||
324 (strcmp(nconf->nc_netid, "unix") == 0)) {
325 memset(&sun, 0, sizeof sun);
326 sun.sun_family = AF_LOCAL;
327 unlink(_PATH_RPCBINDSOCK);
328 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
329 sun.sun_len = SUN_LEN(&sun);
330 addrlen = sizeof (struct sockaddr_un);
331 sa = (struct sockaddr *)&sun;
333 /* Get rpcbind's address on this transport */
335 memset(&hints, 0, sizeof hints);
336 hints.ai_flags = AI_PASSIVE;
337 hints.ai_family = si.si_af;
338 hints.ai_socktype = si.si_socktype;
339 hints.ai_protocol = si.si_proto;
342 if ((strcmp(nconf->nc_netid, "local") != 0) &&
343 (strcmp(nconf->nc_netid, "unix") != 0)) {
345 * If no hosts were specified, just bind to INADDR_ANY.
346 * Otherwise make sure 127.0.0.1 is added to the list.
348 nhostsbak = nhosts + 1;
349 hosts = realloc(hosts, nhostsbak * sizeof(char *));
353 if (hints.ai_family == AF_INET) {
354 hosts[nhostsbak - 1] = "127.0.0.1";
355 } else if (hints.ai_family == AF_INET6) {
356 hosts[nhostsbak - 1] = "::1";
362 * Bind to specific IPs if asked to
365 while (nhostsbak > 0) {
368 * XXX - using RPC library internal functions.
370 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
372 if (errno == EAFNOSUPPORT &&
373 nconf->nc_semantics != NC_TPI_CLTS)
375 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
376 "cannot create socket for %s", nconf->nc_netid);
379 switch (hints.ai_family) {
381 if (inet_pton(AF_INET, hosts[nhostsbak],
383 hints.ai_flags &= AI_NUMERICHOST;
386 * Skip if we have an AF_INET6 address.
388 if (inet_pton(AF_INET6,
389 hosts[nhostsbak], host_addr) == 1) {
396 if (inet_pton(AF_INET6, hosts[nhostsbak],
398 hints.ai_flags &= AI_NUMERICHOST;
401 * Skip if we have an AF_INET address.
403 if (inet_pton(AF_INET, hosts[nhostsbak],
409 if (setsockopt(fd, IPPROTO_IPV6,
410 IPV6_V6ONLY, &on, sizeof on) < 0) {
412 "can't set v6-only binding for "
422 * If no hosts were specified, just bind to INADDR_ANY
424 if (strcmp("*", hosts[nhostsbak]) == 0)
425 hosts[nhostsbak] = NULL;
426 if ((strcmp(nconf->nc_netid, "local") != 0) &&
427 (strcmp(nconf->nc_netid, "unix") != 0)) {
428 if ((aicode = getaddrinfo(hosts[nhostsbak],
429 servname, &hints, &res)) != 0) {
431 "cannot get local address for %s: %s",
432 nconf->nc_netid, gai_strerror(aicode));
435 addrlen = res->ai_addrlen;
436 sa = (struct sockaddr *)res->ai_addr;
438 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
439 if (bind(fd, sa, addrlen) != 0) {
440 syslog(LOG_ERR, "cannot bind %s on %s: %m",
441 (hosts[nhostsbak] == NULL) ? "*" :
442 hosts[nhostsbak], nconf->nc_netid);
448 (void)umask(oldmask);
450 /* Copy the address */
451 taddr.addr.len = taddr.addr.maxlen = addrlen;
452 taddr.addr.buf = malloc(addrlen);
453 if (taddr.addr.buf == NULL) {
455 "cannot allocate memory for %s address",
461 memcpy(taddr.addr.buf, sa, addrlen);
465 * for debugging print out our universal
472 nb.len = nb.maxlen = sa->sa_len;
473 uaddr = taddr2uaddr(nconf, &nb);
474 (void)fprintf(stderr,
475 "rpcbind : my address is %s\n", uaddr);
480 if (nconf->nc_semantics != NC_TPI_CLTS)
481 listen(fd, SOMAXCONN);
483 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
484 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
485 if (my_xprt == (SVCXPRT *)NULL) {
486 syslog(LOG_ERR, "%s: could not create service",
494 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
495 if (bind(fd, sa, addrlen) < 0) {
496 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
501 (void) umask(oldmask);
503 /* Copy the address */
504 taddr.addr.len = taddr.addr.maxlen = addrlen;
505 taddr.addr.buf = malloc(addrlen);
506 if (taddr.addr.buf == NULL) {
507 syslog(LOG_ERR, "cannot allocate memory for %s address",
513 memcpy(taddr.addr.buf, sa, addrlen);
516 /* for debugging print out our universal address */
521 nb.len = nb.maxlen = sa->sa_len;
522 uaddr = taddr2uaddr(nconf, &nb);
523 (void) fprintf(stderr, "rpcbind : my address is %s\n",
529 if (nconf->nc_semantics != NC_TPI_CLTS)
530 listen(fd, SOMAXCONN);
532 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
533 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
534 if (my_xprt == (SVCXPRT *)NULL) {
535 syslog(LOG_ERR, "%s: could not create service",
543 * Register both the versions for tcp/ip, udp/ip and local.
545 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
546 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
547 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
548 (strcmp(nconf->nc_netid, "unix") == 0) ||
549 (strcmp(nconf->nc_netid, "local") == 0)) {
550 struct pmaplist *pml;
552 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
554 syslog(LOG_ERR, "could not register on %s",
558 pml = malloc(sizeof (struct pmaplist));
560 syslog(LOG_ERR, "no memory!");
563 pml->pml_map.pm_prog = PMAPPROG;
564 pml->pml_map.pm_vers = PMAPVERS;
565 pml->pml_map.pm_port = PMAPPORT;
566 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
571 "cannot have more than one TCP transport");
574 tcptrans = strdup(nconf->nc_netid);
575 pml->pml_map.pm_prot = IPPROTO_TCP;
577 /* Let's snarf the universal address */
578 /* "h1.h2.h3.h4.p1.p2" */
579 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
580 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
583 "cannot have more than one UDP transport");
586 udptrans = strdup(nconf->nc_netid);
587 pml->pml_map.pm_prot = IPPROTO_UDP;
589 /* Let's snarf the universal address */
590 /* "h1.h2.h3.h4.p1.p2" */
591 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
592 } else if (strcmp(nconf->nc_netid, "local") == 0)
593 pml->pml_map.pm_prot = IPPROTO_ST;
594 else if (strcmp(nconf->nc_netid, "unix") == 0)
595 pml->pml_map.pm_prot = IPPROTO_ST;
596 pml->pml_next = list_pml;
599 /* Add version 3 information */
600 pml = malloc(sizeof (struct pmaplist));
602 syslog(LOG_ERR, "no memory!");
605 pml->pml_map = list_pml->pml_map;
606 pml->pml_map.pm_vers = RPCBVERS;
607 pml->pml_next = list_pml;
610 /* Add version 4 information */
611 pml = malloc (sizeof (struct pmaplist));
613 syslog(LOG_ERR, "no memory!");
616 pml->pml_map = list_pml->pml_map;
617 pml->pml_map.pm_vers = RPCBVERS4;
618 pml->pml_next = list_pml;
621 /* Also add version 2 stuff to rpcbind list */
622 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
626 /* version 3 registration */
627 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
628 syslog(LOG_ERR, "could not register %s version 3",
632 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
634 /* version 4 registration */
635 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
636 syslog(LOG_ERR, "could not register %s version 4",
640 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
642 /* decide if bound checking works for this transport */
643 status = add_bndlist(nconf, &taddr.addr);
647 fprintf(stderr, "Error in finding bind status for %s\n",
649 } else if (status == 0) {
650 fprintf(stderr, "check binding for %s\n",
652 } else if (status > 0) {
653 fprintf(stderr, "No check binding for %s\n",
659 * rmtcall only supported on CLTS transports for now.
661 if (nconf->nc_semantics == NC_TPI_CLTS) {
662 status = create_rmtcall_fd(nconf);
668 "Could not create rmtcall fd for %s\n",
671 fprintf(stderr, "rmtcall fd for %s is %d\n",
672 nconf->nc_netid, status);
684 * Create the list of addresses that we're bound to. Normally, this
685 * list is empty because we're listening on the wildcard address
686 * (nhost == 0). If -h is specified on the command line, then
687 * bound_sa will have a list of the addresses that the program binds
688 * to specifically. This function takes that list and converts them to
689 * struct sockaddr * and stores them in bound_sa.
692 update_bound_sa(void)
694 struct addrinfo hints, *res = NULL;
699 bound_sa = malloc(sizeof(*bound_sa) * nhosts);
700 memset(&hints, 0, sizeof(hints));
701 hints.ai_family = PF_UNSPEC;
702 for (i = 0; i < nhosts; i++) {
703 if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
705 bound_sa[i] = malloc(res->ai_addrlen);
706 memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
711 * Match the sa against the list of addresses we've bound to. If
712 * we've not specifically bound to anything, we match everything.
713 * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
714 * in bound_sa, we return true. If not, we return false.
717 listen_addr(const struct sockaddr *sa)
722 * If nhosts == 0, then there were no -h options on the
723 * command line, so all addresses are addresses we're
728 for (i = 0; i < nhosts; i++) {
729 if (bound_sa[i] == NULL ||
730 sa->sa_family != bound_sa[i]->sa_family)
732 switch (sa->sa_family) {
734 if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
735 sizeof(struct in_addr)) == 0)
740 if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
741 sizeof(struct in6_addr)) == 0)
753 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
758 rbl = malloc(sizeof (rpcblist));
760 syslog(LOG_ERR, "no memory!");
764 rbl->rpcb_map.r_prog = prog;
765 rbl->rpcb_map.r_vers = vers;
766 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
767 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
768 rbl->rpcb_map.r_owner = strdup(superuser);
769 rbl->rpcb_next = list_rbl; /* Attach to global list */
774 * Catch the signal and die
777 terminate(int signum)
782 doterminate = signum;
783 wr = write(terminate_wfd, &c, 1);
792 write_warmstart(); /* Dump yourself */
797 /* get command line options */
799 parseargs(int argc, char *argv[])
813 while ((c = getopt(argc, argv, "6adh:iLls" WRAPOP WSOP)) != -1) {
819 doabort = 1; /* when debugging, do an abort on */
820 break; /* errors; for rpcbind developers */
827 hosts = realloc(hosts, nhosts * sizeof(char *));
829 errx(1, "Out of memory");
830 hosts[nhosts - 1] = strdup(optarg);
831 if (hosts[nhosts - 1] == NULL)
832 errx(1, "Out of memory");
858 "usage: rpcbind [-6adiLls%s%s] [-h bindip]\n",
863 if (doabort && !debugging) {
865 "-a (abort) specified without -d (debugging) -- ignored.\n");
872 reap(int dummy __unused)
874 int save_errno = errno;
876 while (wait3(NULL, WNOHANG, NULL) > 0)
882 toggle_verboselog(int dummy __unused)
884 verboselog = !verboselog;