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;
101 static int warmstart = 0; /* Grab an old copy of registrations. */
105 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
106 char *udptrans; /* Name of UDP transport */
107 char *tcptrans; /* Name of TCP transport */
108 char *udp_uaddr; /* Universal UDP address */
109 char *tcp_uaddr; /* Universal TCP address */
111 static char servname[] = "rpcbind";
112 static char superuser[] = "superuser";
114 int main __P((int, char *[]));
116 static int init_transport __P((struct netconfig *));
117 static void rbllist_add __P((rpcprog_t, rpcvers_t, struct netconfig *,
119 static void terminate __P((int));
120 static void parseargs __P((int, char *[]));
123 main(int argc, char *argv[])
125 struct netconfig *nconf;
126 void *nc_handle; /* Net config handle */
128 int maxrec = RPC_MAXDATASIZE;
130 parseargs(argc, argv);
132 /* Check that another rpcbind isn't already running. */
133 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
134 O_RDONLY|O_CREAT, 0444))) == -1)
135 err(1, "%s", RPCBINDDLOCK);
137 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
138 errx(1, "another rpcbind is already running. Aborting");
140 getrlimit(RLIMIT_NOFILE, &rl);
141 if (rl.rlim_cur < 128) {
142 if (rl.rlim_max <= 128)
143 rl.rlim_cur = rl.rlim_max;
146 setrlimit(RLIMIT_NOFILE, &rl);
148 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
149 if (geteuid()) { /* This command allowed only to root */
150 fprintf(stderr, "Sorry. You are not superuser\n");
153 nc_handle = setnetconfig(); /* open netconfig file */
154 if (nc_handle == NULL) {
155 syslog(LOG_ERR, "could not read /etc/netconfig");
163 nconf = getnetconfigent("local");
165 nconf = getnetconfigent("unix");
167 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
171 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
173 init_transport(nconf);
175 while ((nconf = getnetconfig(nc_handle))) {
176 if (nconf->nc_flag & NC_VISIBLE)
177 init_transport(nconf);
179 endnetconfig(nc_handle);
181 /* catch the usual termination signals for graceful exit */
182 (void) signal(SIGCHLD, reap);
183 (void) signal(SIGINT, terminate);
184 (void) signal(SIGTERM, terminate);
185 (void) signal(SIGQUIT, terminate);
186 /* ignore others that could get sent */
187 (void) signal(SIGPIPE, SIG_IGN);
188 (void) signal(SIGHUP, SIG_IGN);
189 (void) signal(SIGUSR1, SIG_IGN);
190 (void) signal(SIGUSR2, SIG_IGN);
197 printf("rpcbind debugging enabled.");
199 printf(" Will abort on errors!\n");
205 err(1, "fork failed");
211 if((p = getpwnam(RUN_AS)) == NULL) {
212 syslog(LOG_ERR, "cannot get uid of daemon: %m");
215 if (setuid(p->pw_uid) == -1) {
216 syslog(LOG_ERR, "setuid to daemon failed: %m");
224 syslog(LOG_ERR, "svc_run returned unexpectedly");
232 * Adds the entry into the rpcbind database.
233 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
234 * Returns 0 if succeeds, else fails
237 init_transport(struct netconfig *nconf)
241 struct addrinfo hints, *res = NULL;
242 struct __rpc_sockinfo si;
244 int status; /* bound checking ? */
250 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
251 struct sockaddr_un sun;
254 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
255 (nconf->nc_semantics != NC_TPI_COTS) &&
256 (nconf->nc_semantics != NC_TPI_COTS_ORD))
257 return (1); /* not my type */
263 (void) fprintf(stderr, "%s: %ld lookup routines :\n",
264 nconf->nc_netid, nconf->nc_nlookups);
265 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
267 fprintf(stderr, "[%d] - %s\n", i, *s);
272 * XXX - using RPC library internal functions. For NC_TPI_CLTS
273 * we call this later, for each socket we like to bind.
275 if (nconf->nc_semantics != NC_TPI_CLTS) {
276 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
279 if (errno == EPROTONOSUPPORT)
281 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
287 if (!__rpc_nconf2sockinfo(nconf, &si)) {
288 syslog(LOG_ERR, "cannot get information for %s",
293 if ((strcmp(nconf->nc_netid, "local") == 0) ||
294 (strcmp(nconf->nc_netid, "unix") == 0)) {
295 memset(&sun, 0, sizeof sun);
296 sun.sun_family = AF_LOCAL;
297 unlink(_PATH_RPCBINDSOCK);
298 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
299 sun.sun_len = SUN_LEN(&sun);
300 addrlen = sizeof (struct sockaddr_un);
301 sa = (struct sockaddr *)&sun;
303 /* Get rpcbind's address on this transport */
305 memset(&hints, 0, sizeof hints);
306 hints.ai_flags = AI_PASSIVE;
307 hints.ai_family = si.si_af;
308 hints.ai_socktype = si.si_socktype;
309 hints.ai_protocol = si.si_proto;
311 if (nconf->nc_semantics == NC_TPI_CLTS) {
313 * If no hosts were specified, just bind to INADDR_ANY. Otherwise
314 * make sure 127.0.0.1 is added to the list.
318 hosts = realloc(hosts, nhostsbak * sizeof(char *));
322 if (hints.ai_family == AF_INET) {
323 hosts[nhostsbak - 1] = "127.0.0.1";
324 } else if (hints.ai_family == AF_INET6) {
325 hosts[nhostsbak - 1] = "::1";
331 * Bind to specific IPs if asked to
334 while (nhostsbak > 0) {
337 * XXX - using RPC library internal functions.
339 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
340 syslog(LOG_ERR, "cannot create socket for %s",
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 adress.
353 if (inet_pton(AF_INET6,
354 hosts[nhostsbak], host_addr) == 1)
359 if (inet_pton(AF_INET6, hosts[nhostsbak],
361 hints.ai_flags &= AI_NUMERICHOST;
364 * Skip if we have an AF_INET adress.
366 if (inet_pton(AF_INET, hosts[nhostsbak],
370 if (setsockopt(fd, IPPROTO_IPV6,
371 IPV6_V6ONLY, &on, sizeof on) < 0) {
373 "can't set v6-only binding for "
383 * If no hosts were specified, just bind to INADDR_ANY
385 if (strcmp("*", hosts[nhostsbak]) == 0)
386 hosts[nhostsbak] = NULL;
388 if ((aicode = getaddrinfo(hosts[nhostsbak],
389 servname, &hints, &res)) != 0) {
391 "cannot get local address for %s: %s",
392 nconf->nc_netid, gai_strerror(aicode));
395 addrlen = res->ai_addrlen;
396 sa = (struct sockaddr *)res->ai_addr;
397 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
398 if (bind(fd, sa, addrlen) != 0) {
399 syslog(LOG_ERR, "cannot bind %s on %s: %m",
400 (hosts[nhostsbak] == NULL) ? "*" :
401 hosts[nhostsbak], nconf->nc_netid);
407 (void) umask(oldmask);
409 /* Copy the address */
410 taddr.addr.len = taddr.addr.maxlen = addrlen;
411 taddr.addr.buf = malloc(addrlen);
412 if (taddr.addr.buf == NULL) {
414 "cannot allocate memory for %s address",
420 memcpy(taddr.addr.buf, sa, addrlen);
424 * for debugging print out our universal
431 nb.len = nb.maxlen = sa->sa_len;
432 uaddr = taddr2uaddr(nconf, &nb);
433 (void) fprintf(stderr,
434 "rpcbind : my address is %s\n", uaddr);
439 if (nconf->nc_semantics != NC_TPI_CLTS)
440 listen(fd, SOMAXCONN);
442 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
443 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
444 if (my_xprt == (SVCXPRT *)NULL) {
445 syslog(LOG_ERR, "%s: could not create service",
453 if ((strcmp(nconf->nc_netid, "local") != 0) &&
454 (strcmp(nconf->nc_netid, "unix") != 0)) {
455 if ((aicode = getaddrinfo(NULL, servname, &hints, &res))
458 "cannot get local address for %s: %s",
459 nconf->nc_netid, gai_strerror(aicode));
462 addrlen = res->ai_addrlen;
463 sa = (struct sockaddr *)res->ai_addr;
465 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
466 if (bind(fd, sa, addrlen) < 0) {
467 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
472 (void) umask(oldmask);
474 /* Copy the address */
475 taddr.addr.len = taddr.addr.maxlen = addrlen;
476 taddr.addr.buf = malloc(addrlen);
477 if (taddr.addr.buf == NULL) {
478 syslog(LOG_ERR, "cannot allocate memory for %s address",
484 memcpy(taddr.addr.buf, sa, addrlen);
487 /* for debugging print out our universal address */
492 nb.len = nb.maxlen = sa->sa_len;
493 uaddr = taddr2uaddr(nconf, &nb);
494 (void) fprintf(stderr, "rpcbind : my address is %s\n",
500 if (nconf->nc_semantics != NC_TPI_CLTS)
501 listen(fd, SOMAXCONN);
503 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
504 if (my_xprt == (SVCXPRT *)NULL) {
505 syslog(LOG_ERR, "%s: could not create service",
513 * Register both the versions for tcp/ip, udp/ip and local.
515 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
516 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
517 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
518 (strcmp(nconf->nc_netid, "unix") == 0) ||
519 (strcmp(nconf->nc_netid, "local") == 0)) {
520 struct pmaplist *pml;
522 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
524 syslog(LOG_ERR, "could not register on %s",
528 pml = malloc(sizeof (struct pmaplist));
530 syslog(LOG_ERR, "no memory!");
533 pml->pml_map.pm_prog = PMAPPROG;
534 pml->pml_map.pm_vers = PMAPVERS;
535 pml->pml_map.pm_port = PMAPPORT;
536 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
539 "cannot have more than one TCP transport");
542 tcptrans = strdup(nconf->nc_netid);
543 pml->pml_map.pm_prot = IPPROTO_TCP;
545 /* Let's snarf the universal address */
546 /* "h1.h2.h3.h4.p1.p2" */
547 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
548 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
551 "cannot have more than one UDP transport");
554 udptrans = strdup(nconf->nc_netid);
555 pml->pml_map.pm_prot = IPPROTO_UDP;
557 /* Let's snarf the universal address */
558 /* "h1.h2.h3.h4.p1.p2" */
559 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
560 } else if (strcmp(nconf->nc_netid, "local") == 0)
561 pml->pml_map.pm_prot = IPPROTO_ST;
562 else if (strcmp(nconf->nc_netid, "unix") == 0)
563 pml->pml_map.pm_prot = IPPROTO_ST;
564 pml->pml_next = list_pml;
567 /* Add version 3 information */
568 pml = malloc(sizeof (struct pmaplist));
570 syslog(LOG_ERR, "no memory!");
573 pml->pml_map = list_pml->pml_map;
574 pml->pml_map.pm_vers = RPCBVERS;
575 pml->pml_next = list_pml;
578 /* Add version 4 information */
579 pml = malloc (sizeof (struct pmaplist));
581 syslog(LOG_ERR, "no memory!");
584 pml->pml_map = list_pml->pml_map;
585 pml->pml_map.pm_vers = RPCBVERS4;
586 pml->pml_next = list_pml;
589 /* Also add version 2 stuff to rpcbind list */
590 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
594 /* version 3 registration */
595 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
596 syslog(LOG_ERR, "could not register %s version 3",
600 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
602 /* version 4 registration */
603 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
604 syslog(LOG_ERR, "could not register %s version 4",
608 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
610 /* decide if bound checking works for this transport */
611 status = add_bndlist(nconf, &taddr.addr);
615 fprintf(stderr, "Error in finding bind status for %s\n",
617 } else if (status == 0) {
618 fprintf(stderr, "check binding for %s\n",
620 } else if (status > 0) {
621 fprintf(stderr, "No check binding for %s\n",
627 * rmtcall only supported on CLTS transports for now.
629 if (nconf->nc_semantics == NC_TPI_CLTS) {
630 status = create_rmtcall_fd(nconf);
636 "Could not create rmtcall fd for %s\n",
639 fprintf(stderr, "rmtcall fd for %s is %d\n",
640 nconf->nc_netid, status);
652 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
657 rbl = malloc(sizeof (rpcblist));
659 syslog(LOG_ERR, "no memory!");
663 rbl->rpcb_map.r_prog = prog;
664 rbl->rpcb_map.r_vers = vers;
665 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
666 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
667 rbl->rpcb_map.r_owner = strdup(superuser);
668 rbl->rpcb_next = list_rbl; /* Attach to global list */
673 * Catch the signal and die
676 terminate(int dummy __unused)
678 close(rpcbindlockfd);
681 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
682 write_warmstart(); /* Dump yourself */
691 write_warmstart(); /* Dump yourself */
696 /* get command line options */
698 parseargs(int argc, char *argv[])
707 while ((c = getopt(argc, argv, "adh:iLls" WSOP)) != -1) {
710 doabort = 1; /* when debugging, do an abort on */
711 break; /* errors; for rpcbind developers */
718 hosts = realloc(hosts, nhosts * sizeof(char *));
720 errx(1, "Out of memory");
721 hosts[nhosts - 1] = strdup(optarg);
722 if (hosts[nhosts - 1] == NULL)
723 errx(1, "Out of memory");
744 "usage: rpcbind [-adiLls%s] [-h bindip]\n",
749 if (doabort && !debugging) {
751 "-a (abort) specified without -d (debugging) -- ignored.\n");
758 reap(int dummy __unused)
760 int save_errno = errno;
762 while (wait3(NULL, WNOHANG, NULL) > 0)
768 toggle_verboselog(int dummy __unused)
770 verboselog = !verboselog;