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);
189 endnetconfig(nc_handle);
191 /* catch the usual termination signals for graceful exit */
192 (void) signal(SIGCHLD, reap);
193 (void) signal(SIGINT, terminate);
194 (void) signal(SIGTERM, terminate);
195 (void) signal(SIGQUIT, terminate);
196 /* ignore others that could get sent */
197 (void) signal(SIGPIPE, SIG_IGN);
198 (void) signal(SIGHUP, SIG_IGN);
199 (void) signal(SIGUSR1, SIG_IGN);
200 (void) signal(SIGUSR2, SIG_IGN);
207 printf("rpcbind debugging enabled.");
209 printf(" Will abort on errors!\n");
215 err(1, "fork failed");
221 if((p = getpwnam(RUN_AS)) == NULL) {
222 syslog(LOG_ERR, "cannot get uid of daemon: %m");
225 if (setuid(p->pw_uid) == -1) {
226 syslog(LOG_ERR, "setuid to daemon failed: %m");
234 syslog(LOG_ERR, "svc_run returned unexpectedly");
242 * Adds the entry into the rpcbind database.
243 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
244 * Returns 0 if succeeds, else fails
247 init_transport(struct netconfig *nconf)
251 struct addrinfo hints, *res = NULL;
252 struct __rpc_sockinfo si;
254 int status; /* bound checking ? */
260 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
261 struct sockaddr_un sun;
264 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
265 (nconf->nc_semantics != NC_TPI_COTS) &&
266 (nconf->nc_semantics != NC_TPI_COTS_ORD))
267 return (1); /* not my type */
273 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
274 nconf->nc_netid, nconf->nc_nlookups);
275 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
277 fprintf(stderr, "[%d] - %s\n", i, *s);
282 * XXX - using RPC library internal functions.
284 if ((strcmp(nconf->nc_netid, "local") == 0) ||
285 (strcmp(nconf->nc_netid, "unix") == 0)) {
287 * For other transports we call this later, for each socket we
290 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
292 if (errno == EPROTONOSUPPORT)
294 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
300 if (!__rpc_nconf2sockinfo(nconf, &si)) {
301 syslog(LOG_ERR, "cannot get information for %s",
306 if ((strcmp(nconf->nc_netid, "local") == 0) ||
307 (strcmp(nconf->nc_netid, "unix") == 0)) {
308 memset(&sun, 0, sizeof sun);
309 sun.sun_family = AF_LOCAL;
310 unlink(_PATH_RPCBINDSOCK);
311 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
312 sun.sun_len = SUN_LEN(&sun);
313 addrlen = sizeof (struct sockaddr_un);
314 sa = (struct sockaddr *)&sun;
316 /* Get rpcbind's address on this transport */
318 memset(&hints, 0, sizeof hints);
319 hints.ai_flags = AI_PASSIVE;
320 hints.ai_family = si.si_af;
321 hints.ai_socktype = si.si_socktype;
322 hints.ai_protocol = si.si_proto;
325 if ((strcmp(nconf->nc_netid, "local") != 0) &&
326 (strcmp(nconf->nc_netid, "unix") != 0)) {
328 * If no hosts were specified, just bind to INADDR_ANY.
329 * Otherwise make sure 127.0.0.1 is added to the list.
331 nhostsbak = nhosts + 1;
332 hosts = realloc(hosts, nhostsbak * sizeof(char *));
336 if (hints.ai_family == AF_INET) {
337 hosts[nhostsbak - 1] = "127.0.0.1";
338 } else if (hints.ai_family == AF_INET6) {
339 hosts[nhostsbak - 1] = "::1";
345 * Bind to specific IPs if asked to
348 while (nhostsbak > 0) {
351 * XXX - using RPC library internal functions.
353 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
355 if (errno == EPROTONOSUPPORT &&
356 nconf->nc_semantics != NC_TPI_CLTS)
358 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
359 "cannot create socket for %s", nconf->nc_netid);
362 switch (hints.ai_family) {
364 if (inet_pton(AF_INET, hosts[nhostsbak],
366 hints.ai_flags &= AI_NUMERICHOST;
369 * Skip if we have an AF_INET6 adress.
371 if (inet_pton(AF_INET6,
372 hosts[nhostsbak], host_addr) == 1) {
379 if (inet_pton(AF_INET6, hosts[nhostsbak],
381 hints.ai_flags &= AI_NUMERICHOST;
384 * Skip if we have an AF_INET adress.
386 if (inet_pton(AF_INET, hosts[nhostsbak],
392 if (setsockopt(fd, IPPROTO_IPV6,
393 IPV6_V6ONLY, &on, sizeof on) < 0) {
395 "can't set v6-only binding for "
405 * If no hosts were specified, just bind to INADDR_ANY
407 if (strcmp("*", hosts[nhostsbak]) == 0)
408 hosts[nhostsbak] = NULL;
409 if ((strcmp(nconf->nc_netid, "local") != 0) &&
410 (strcmp(nconf->nc_netid, "unix") != 0)) {
411 if ((aicode = getaddrinfo(hosts[nhostsbak],
412 servname, &hints, &res)) != 0) {
414 "cannot get local address for %s: %s",
415 nconf->nc_netid, gai_strerror(aicode));
418 addrlen = res->ai_addrlen;
419 sa = (struct sockaddr *)res->ai_addr;
421 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
422 if (bind(fd, sa, addrlen) != 0) {
423 syslog(LOG_ERR, "cannot bind %s on %s: %m",
424 (hosts[nhostsbak] == NULL) ? "*" :
425 hosts[nhostsbak], nconf->nc_netid);
431 (void)umask(oldmask);
433 /* Copy the address */
434 taddr.addr.len = taddr.addr.maxlen = addrlen;
435 taddr.addr.buf = malloc(addrlen);
436 if (taddr.addr.buf == NULL) {
438 "cannot allocate memory for %s address",
444 memcpy(taddr.addr.buf, sa, addrlen);
448 * for debugging print out our universal
455 nb.len = nb.maxlen = sa->sa_len;
456 uaddr = taddr2uaddr(nconf, &nb);
457 (void)fprintf(stderr,
458 "rpcbind : my address is %s\n", uaddr);
463 if (nconf->nc_semantics != NC_TPI_CLTS)
464 listen(fd, SOMAXCONN);
466 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
467 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
468 if (my_xprt == (SVCXPRT *)NULL) {
469 syslog(LOG_ERR, "%s: could not create service",
477 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
478 if (bind(fd, sa, addrlen) < 0) {
479 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
484 (void) umask(oldmask);
486 /* Copy the address */
487 taddr.addr.len = taddr.addr.maxlen = addrlen;
488 taddr.addr.buf = malloc(addrlen);
489 if (taddr.addr.buf == NULL) {
490 syslog(LOG_ERR, "cannot allocate memory for %s address",
496 memcpy(taddr.addr.buf, sa, addrlen);
499 /* for debugging print out our universal address */
504 nb.len = nb.maxlen = sa->sa_len;
505 uaddr = taddr2uaddr(nconf, &nb);
506 (void) fprintf(stderr, "rpcbind : my address is %s\n",
512 if (nconf->nc_semantics != NC_TPI_CLTS)
513 listen(fd, SOMAXCONN);
515 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
516 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
517 if (my_xprt == (SVCXPRT *)NULL) {
518 syslog(LOG_ERR, "%s: could not create service",
526 * Register both the versions for tcp/ip, udp/ip and local.
528 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
529 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
530 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
531 (strcmp(nconf->nc_netid, "unix") == 0) ||
532 (strcmp(nconf->nc_netid, "local") == 0)) {
533 struct pmaplist *pml;
535 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
537 syslog(LOG_ERR, "could not register on %s",
541 pml = malloc(sizeof (struct pmaplist));
543 syslog(LOG_ERR, "no memory!");
546 pml->pml_map.pm_prog = PMAPPROG;
547 pml->pml_map.pm_vers = PMAPVERS;
548 pml->pml_map.pm_port = PMAPPORT;
549 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
552 "cannot have more than one TCP transport");
555 tcptrans = strdup(nconf->nc_netid);
556 pml->pml_map.pm_prot = IPPROTO_TCP;
558 /* Let's snarf the universal address */
559 /* "h1.h2.h3.h4.p1.p2" */
560 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
561 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
564 "cannot have more than one UDP transport");
567 udptrans = strdup(nconf->nc_netid);
568 pml->pml_map.pm_prot = IPPROTO_UDP;
570 /* Let's snarf the universal address */
571 /* "h1.h2.h3.h4.p1.p2" */
572 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
573 } else if (strcmp(nconf->nc_netid, "local") == 0)
574 pml->pml_map.pm_prot = IPPROTO_ST;
575 else if (strcmp(nconf->nc_netid, "unix") == 0)
576 pml->pml_map.pm_prot = IPPROTO_ST;
577 pml->pml_next = list_pml;
580 /* Add version 3 information */
581 pml = malloc(sizeof (struct pmaplist));
583 syslog(LOG_ERR, "no memory!");
586 pml->pml_map = list_pml->pml_map;
587 pml->pml_map.pm_vers = RPCBVERS;
588 pml->pml_next = list_pml;
591 /* Add version 4 information */
592 pml = malloc (sizeof (struct pmaplist));
594 syslog(LOG_ERR, "no memory!");
597 pml->pml_map = list_pml->pml_map;
598 pml->pml_map.pm_vers = RPCBVERS4;
599 pml->pml_next = list_pml;
602 /* Also add version 2 stuff to rpcbind list */
603 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
607 /* version 3 registration */
608 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
609 syslog(LOG_ERR, "could not register %s version 3",
613 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
615 /* version 4 registration */
616 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
617 syslog(LOG_ERR, "could not register %s version 4",
621 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
623 /* decide if bound checking works for this transport */
624 status = add_bndlist(nconf, &taddr.addr);
628 fprintf(stderr, "Error in finding bind status for %s\n",
630 } else if (status == 0) {
631 fprintf(stderr, "check binding for %s\n",
633 } else if (status > 0) {
634 fprintf(stderr, "No check binding for %s\n",
640 * rmtcall only supported on CLTS transports for now.
642 if (nconf->nc_semantics == NC_TPI_CLTS) {
643 status = create_rmtcall_fd(nconf);
649 "Could not create rmtcall fd for %s\n",
652 fprintf(stderr, "rmtcall fd for %s is %d\n",
653 nconf->nc_netid, status);
665 * Create the list of addresses that we're bound to. Normally, this
666 * list is empty because we're listening on the wildcard address
667 * (nhost == 0). If -h is specified on the command line, then
668 * bound_sa will have a list of the addresses that the program binds
669 * to specifically. This function takes that list and converts them to
670 * struct sockaddr * and stores them in bound_sa.
673 update_bound_sa(void)
675 struct addrinfo hints, *res = NULL;
680 bound_sa = malloc(sizeof(*bound_sa) * nhosts);
681 memset(&hints, 0, sizeof(hints));
682 hints.ai_family = PF_UNSPEC;
683 for (i = 0; i < nhosts; i++) {
684 if (getaddrinfo(hosts[i], NULL, &hints, &res) != 0)
686 bound_sa[i] = malloc(res->ai_addrlen);
687 memcpy(bound_sa[i], res->ai_addr, res->ai_addrlen);
692 * Match the sa against the list of addresses we've bound to. If
693 * we've not specifically bound to anything, we match everything.
694 * Otherwise, if the IPv4 or IPv6 address matches one of the addresses
695 * in bound_sa, we return true. If not, we return false.
698 listen_addr(const struct sockaddr *sa)
703 * If nhosts == 0, then there were no -h options on the
704 * command line, so all addresses are addresses we're
709 for (i = 0; i < nhosts; i++) {
710 if (bound_sa[i] == NULL ||
711 sa->sa_family != bound_sa[i]->sa_family)
713 switch (sa->sa_family) {
715 if (memcmp(&SA2SINADDR(sa), &SA2SINADDR(bound_sa[i]),
716 sizeof(struct in_addr)) == 0)
721 if (memcmp(&SA2SIN6ADDR(sa), &SA2SIN6ADDR(bound_sa[i]),
722 sizeof(struct in6_addr)) == 0)
734 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
739 rbl = malloc(sizeof (rpcblist));
741 syslog(LOG_ERR, "no memory!");
745 rbl->rpcb_map.r_prog = prog;
746 rbl->rpcb_map.r_vers = vers;
747 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
748 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
749 rbl->rpcb_map.r_owner = strdup(superuser);
750 rbl->rpcb_next = list_rbl; /* Attach to global list */
755 * Catch the signal and die
758 terminate(int dummy __unused)
760 close(rpcbindlockfd);
763 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
764 write_warmstart(); /* Dump yourself */
773 write_warmstart(); /* Dump yourself */
778 /* get command line options */
780 parseargs(int argc, char *argv[])
789 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
795 doabort = 1; /* when debugging, do an abort on */
796 break; /* errors; for rpcbind developers */
803 hosts = realloc(hosts, nhosts * sizeof(char *));
805 errx(1, "Out of memory");
806 hosts[nhosts - 1] = strdup(optarg);
807 if (hosts[nhosts - 1] == NULL)
808 errx(1, "Out of memory");
829 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
834 if (doabort && !debugging) {
836 "-a (abort) specified without -d (debugging) -- ignored.\n");
843 reap(int dummy __unused)
845 int save_errno = errno;
847 while (wait3(NULL, WNOHANG, NULL) > 0)
853 toggle_verboselog(int dummy __unused)
855 verboselog = !verboselog;