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;
95 struct sockaddr **bound_sa;
103 static int warmstart = 0; /* Grab an old copy of registrations. */
107 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
108 char *udptrans; /* Name of UDP transport */
109 char *tcptrans; /* Name of TCP transport */
110 char *udp_uaddr; /* Universal UDP address */
111 char *tcp_uaddr; /* Universal TCP address */
113 static char servname[] = "rpcbind";
114 static char superuser[] = "superuser";
116 int main(int, char *[]);
118 static int init_transport(struct netconfig *);
119 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
121 static void terminate(int);
122 static void parseargs(int, char *[]);
123 static void update_bound_sa(void);
126 main(int argc, char *argv[])
128 struct netconfig *nconf;
129 void *nc_handle; /* Net config handle */
131 int maxrec = RPC_MAXDATASIZE;
133 parseargs(argc, argv);
137 /* Check that another rpcbind isn't already running. */
138 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
139 O_RDONLY|O_CREAT, 0444))) == -1)
140 err(1, "%s", RPCBINDDLOCK);
142 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
143 errx(1, "another rpcbind is already running. Aborting");
145 getrlimit(RLIMIT_NOFILE, &rl);
146 if (rl.rlim_cur < 128) {
147 if (rl.rlim_max <= 128)
148 rl.rlim_cur = rl.rlim_max;
151 setrlimit(RLIMIT_NOFILE, &rl);
153 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
154 if (geteuid()) { /* This command allowed only to root */
155 fprintf(stderr, "Sorry. You are not superuser\n");
158 nc_handle = setnetconfig(); /* open netconfig file */
159 if (nc_handle == NULL) {
160 syslog(LOG_ERR, "could not read /etc/netconfig");
168 nconf = getnetconfigent("local");
170 nconf = getnetconfigent("unix");
172 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
176 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
178 init_transport(nconf);
180 while ((nconf = getnetconfig(nc_handle))) {
181 if (nconf->nc_flag & NC_VISIBLE)
182 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
186 init_transport(nconf);
188 endnetconfig(nc_handle);
190 /* catch the usual termination signals for graceful exit */
191 (void) signal(SIGCHLD, reap);
192 (void) signal(SIGINT, terminate);
193 (void) signal(SIGTERM, terminate);
194 (void) signal(SIGQUIT, terminate);
195 /* ignore others that could get sent */
196 (void) signal(SIGPIPE, SIG_IGN);
197 (void) signal(SIGHUP, SIG_IGN);
198 (void) signal(SIGUSR1, SIG_IGN);
199 (void) signal(SIGUSR2, SIG_IGN);
206 printf("rpcbind debugging enabled.");
208 printf(" Will abort on errors!\n");
214 err(1, "fork failed");
220 if((p = getpwnam(RUN_AS)) == NULL) {
221 syslog(LOG_ERR, "cannot get uid of daemon: %m");
224 if (setuid(p->pw_uid) == -1) {
225 syslog(LOG_ERR, "setuid to daemon failed: %m");
233 syslog(LOG_ERR, "svc_run returned unexpectedly");
241 * Adds the entry into the rpcbind database.
242 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
243 * Returns 0 if succeeds, else fails
246 init_transport(struct netconfig *nconf)
250 struct addrinfo hints, *res = NULL;
251 struct __rpc_sockinfo si;
253 int status; /* bound checking ? */
259 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
260 struct sockaddr_un sun;
263 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
264 (nconf->nc_semantics != NC_TPI_COTS) &&
265 (nconf->nc_semantics != NC_TPI_COTS_ORD))
266 return (1); /* not my type */
272 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
273 nconf->nc_netid, nconf->nc_nlookups);
274 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
276 fprintf(stderr, "[%d] - %s\n", i, *s);
281 * XXX - using RPC library internal functions.
283 if ((strcmp(nconf->nc_netid, "local") == 0) ||
284 (strcmp(nconf->nc_netid, "unix") == 0)) {
286 * For other transports we call this later, for each socket we
289 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
291 if (errno == EPROTONOSUPPORT)
293 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
299 if (!__rpc_nconf2sockinfo(nconf, &si)) {
300 syslog(LOG_ERR, "cannot get information for %s",
305 if ((strcmp(nconf->nc_netid, "local") == 0) ||
306 (strcmp(nconf->nc_netid, "unix") == 0)) {
307 memset(&sun, 0, sizeof sun);
308 sun.sun_family = AF_LOCAL;
309 unlink(_PATH_RPCBINDSOCK);
310 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
311 sun.sun_len = SUN_LEN(&sun);
312 addrlen = sizeof (struct sockaddr_un);
313 sa = (struct sockaddr *)&sun;
315 /* Get rpcbind's address on this transport */
317 memset(&hints, 0, sizeof hints);
318 hints.ai_flags = AI_PASSIVE;
319 hints.ai_family = si.si_af;
320 hints.ai_socktype = si.si_socktype;
321 hints.ai_protocol = si.si_proto;
324 if ((strcmp(nconf->nc_netid, "local") != 0) &&
325 (strcmp(nconf->nc_netid, "unix") != 0)) {
327 * If no hosts were specified, just bind to INADDR_ANY.
328 * Otherwise make sure 127.0.0.1 is added to the list.
330 nhostsbak = nhosts + 1;
331 hosts = realloc(hosts, nhostsbak * sizeof(char *));
335 if (hints.ai_family == AF_INET) {
336 hosts[nhostsbak - 1] = "127.0.0.1";
337 } else if (hints.ai_family == AF_INET6) {
338 hosts[nhostsbak - 1] = "::1";
344 * Bind to specific IPs if asked to
347 while (nhostsbak > 0) {
350 * XXX - using RPC library internal functions.
352 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
354 if (errno == EPROTONOSUPPORT &&
355 nconf->nc_semantics != NC_TPI_CLTS)
357 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
358 "cannot create socket for %s", nconf->nc_netid);
361 switch (hints.ai_family) {
363 if (inet_pton(AF_INET, hosts[nhostsbak],
365 hints.ai_flags &= AI_NUMERICHOST;
368 * Skip if we have an AF_INET6 adress.
370 if (inet_pton(AF_INET6,
371 hosts[nhostsbak], host_addr) == 1) {
378 if (inet_pton(AF_INET6, hosts[nhostsbak],
380 hints.ai_flags &= AI_NUMERICHOST;
383 * Skip if we have an AF_INET adress.
385 if (inet_pton(AF_INET, hosts[nhostsbak],
391 if (setsockopt(fd, IPPROTO_IPV6,
392 IPV6_V6ONLY, &on, sizeof on) < 0) {
394 "can't set v6-only binding for "
404 * If no hosts were specified, just bind to INADDR_ANY
406 if (strcmp("*", hosts[nhostsbak]) == 0)
407 hosts[nhostsbak] = NULL;
408 if ((strcmp(nconf->nc_netid, "local") != 0) &&
409 (strcmp(nconf->nc_netid, "unix") != 0)) {
410 if ((aicode = getaddrinfo(hosts[nhostsbak],
411 servname, &hints, &res)) != 0) {
413 "cannot get local address for %s: %s",
414 nconf->nc_netid, gai_strerror(aicode));
417 addrlen = res->ai_addrlen;
418 sa = (struct sockaddr *)res->ai_addr;
420 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
421 if (bind(fd, sa, addrlen) != 0) {
422 syslog(LOG_ERR, "cannot bind %s on %s: %m",
423 (hosts[nhostsbak] == NULL) ? "*" :
424 hosts[nhostsbak], nconf->nc_netid);
430 (void)umask(oldmask);
432 /* Copy the address */
433 taddr.addr.len = taddr.addr.maxlen = addrlen;
434 taddr.addr.buf = malloc(addrlen);
435 if (taddr.addr.buf == NULL) {
437 "cannot allocate memory for %s address",
443 memcpy(taddr.addr.buf, sa, addrlen);
447 * for debugging print out our universal
454 nb.len = nb.maxlen = sa->sa_len;
455 uaddr = taddr2uaddr(nconf, &nb);
456 (void)fprintf(stderr,
457 "rpcbind : my address is %s\n", uaddr);
462 if (nconf->nc_semantics != NC_TPI_CLTS)
463 listen(fd, SOMAXCONN);
465 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
466 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
467 if (my_xprt == (SVCXPRT *)NULL) {
468 syslog(LOG_ERR, "%s: could not create service",
476 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
477 if (bind(fd, sa, addrlen) < 0) {
478 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
483 (void) umask(oldmask);
485 /* Copy the address */
486 taddr.addr.len = taddr.addr.maxlen = addrlen;
487 taddr.addr.buf = malloc(addrlen);
488 if (taddr.addr.buf == NULL) {
489 syslog(LOG_ERR, "cannot allocate memory for %s address",
495 memcpy(taddr.addr.buf, sa, addrlen);
498 /* for debugging print out our universal address */
503 nb.len = nb.maxlen = sa->sa_len;
504 uaddr = taddr2uaddr(nconf, &nb);
505 (void) fprintf(stderr, "rpcbind : my address is %s\n",
511 if (nconf->nc_semantics != NC_TPI_CLTS)
512 listen(fd, SOMAXCONN);
514 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
515 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
516 if (my_xprt == (SVCXPRT *)NULL) {
517 syslog(LOG_ERR, "%s: could not create service",
525 * Register both the versions for tcp/ip, udp/ip and local.
527 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
528 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
529 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
530 (strcmp(nconf->nc_netid, "unix") == 0) ||
531 (strcmp(nconf->nc_netid, "local") == 0)) {
532 struct pmaplist *pml;
534 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
536 syslog(LOG_ERR, "could not register on %s",
540 pml = malloc(sizeof (struct pmaplist));
542 syslog(LOG_ERR, "no memory!");
545 pml->pml_map.pm_prog = PMAPPROG;
546 pml->pml_map.pm_vers = PMAPVERS;
547 pml->pml_map.pm_port = PMAPPORT;
548 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
551 "cannot have more than one TCP transport");
554 tcptrans = strdup(nconf->nc_netid);
555 pml->pml_map.pm_prot = IPPROTO_TCP;
557 /* Let's snarf the universal address */
558 /* "h1.h2.h3.h4.p1.p2" */
559 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
560 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
563 "cannot have more than one UDP transport");
566 udptrans = strdup(nconf->nc_netid);
567 pml->pml_map.pm_prot = IPPROTO_UDP;
569 /* Let's snarf the universal address */
570 /* "h1.h2.h3.h4.p1.p2" */
571 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
572 } else if (strcmp(nconf->nc_netid, "local") == 0)
573 pml->pml_map.pm_prot = IPPROTO_ST;
574 else if (strcmp(nconf->nc_netid, "unix") == 0)
575 pml->pml_map.pm_prot = IPPROTO_ST;
576 pml->pml_next = list_pml;
579 /* Add version 3 information */
580 pml = malloc(sizeof (struct pmaplist));
582 syslog(LOG_ERR, "no memory!");
585 pml->pml_map = list_pml->pml_map;
586 pml->pml_map.pm_vers = RPCBVERS;
587 pml->pml_next = list_pml;
590 /* Add version 4 information */
591 pml = malloc (sizeof (struct pmaplist));
593 syslog(LOG_ERR, "no memory!");
596 pml->pml_map = list_pml->pml_map;
597 pml->pml_map.pm_vers = RPCBVERS4;
598 pml->pml_next = list_pml;
601 /* Also add version 2 stuff to rpcbind list */
602 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
606 /* version 3 registration */
607 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
608 syslog(LOG_ERR, "could not register %s version 3",
612 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
614 /* version 4 registration */
615 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
616 syslog(LOG_ERR, "could not register %s version 4",
620 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
622 /* decide if bound checking works for this transport */
623 status = add_bndlist(nconf, &taddr.addr);
627 fprintf(stderr, "Error in finding bind status for %s\n",
629 } else if (status == 0) {
630 fprintf(stderr, "check binding for %s\n",
632 } else if (status > 0) {
633 fprintf(stderr, "No check binding for %s\n",
639 * rmtcall only supported on CLTS transports for now.
641 if (nconf->nc_semantics == NC_TPI_CLTS) {
642 status = create_rmtcall_fd(nconf);
648 "Could not create rmtcall fd for %s\n",
651 fprintf(stderr, "rmtcall fd for %s is %d\n",
652 nconf->nc_netid, status);
664 * Create the list of addresses that we're bound to. Normally, this
665 * list is empty because we're listening on the wildcard address
666 * (nhost == 0). If -h is specified on the command line, then
667 * bound_sa will have a list of the addresses that the program binds
668 * to specifically. This function takes that list and converts them to
669 * struct sockaddr * and stores them in bound_sa.
672 update_bound_sa(void)
674 struct addrinfo hints, *res = NULL;
679 bound_sa = malloc(sizeof(*bound_sa) * nhosts);
680 memset(&hints, 0, sizeof(hints));
681 hints.ai_family = PF_UNSPEC;
682 for (i = 0; i < nhosts; i++) {
683 if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
685 bound_sa[i] = malloc(res->ai_addrlen);
686 memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
691 * Match the sa against the list of addresses we've bound to. If
692 * we've not specifically bound to anything, we match everything.
693 * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
694 * in bound_sa, we return true. If not, we return false.
697 listen_addr(const struct sockaddr *sa)
702 * If nhosts == 0, then there were no -h options on the
703 * command line, so all addresses are addresses we're
708 for (i = 0; i < nhosts; i++) {
709 if (bound_sa[i] == NULL ||
710 sa->sa_family != bound_sa[i]->sa_family)
712 switch (sa->sa_family) {
714 if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
715 sizeof(struct in_addr)) == 0)
720 if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
721 sizeof(struct in6_addr)) == 0)
733 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
738 rbl = malloc(sizeof (rpcblist));
740 syslog(LOG_ERR, "no memory!");
744 rbl->rpcb_map.r_prog = prog;
745 rbl->rpcb_map.r_vers = vers;
746 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
747 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
748 rbl->rpcb_map.r_owner = strdup(superuser);
749 rbl->rpcb_next = list_rbl; /* Attach to global list */
754 * Catch the signal and die
757 terminate(int dummy __unused)
759 close(rpcbindlockfd);
762 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
763 write_warmstart(); /* Dump yourself */
772 write_warmstart(); /* Dump yourself */
777 /* get command line options */
779 parseargs(int argc, char *argv[])
788 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
794 doabort = 1; /* when debugging, do an abort on */
795 break; /* errors; for rpcbind developers */
802 hosts = realloc(hosts, nhosts * sizeof(char *));
804 errx(1, "Out of memory");
805 hosts[nhosts - 1] = strdup(optarg);
806 if (hosts[nhosts - 1] == NULL)
807 errx(1, "Out of memory");
828 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
833 if (doabort && !debugging) {
835 "-a (abort) specified without -d (debugging) -- ignored.\n");
842 reap(int dummy __unused)
844 int save_errno = errno;
846 while (wait3(NULL, WNOHANG, NULL) > 0)
852 toggle_verboselog(int dummy __unused)
854 verboselog = !verboselog;