1 /* $NetBSD: rpcbind.c,v 1.1 2000/06/02 23:15:42 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>
61 #include <netinet/in.h>
65 #include <netconfig.h>
76 /* Global variables */
77 int debugging = 0; /* Tell me what's going on */
78 int doabort = 0; /* When debugging, do an abort on errors */
79 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
81 /* who to suid to if -s is given */
82 #define RUN_AS "daemon"
86 int oldstyle_local = 0;
95 static int warmstart = 0; /* Grab a old copy of registrations */
99 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
100 char *udptrans; /* Name of UDP transport */
101 char *tcptrans; /* Name of TCP transport */
102 char *udp_uaddr; /* Universal UDP address */
103 char *tcp_uaddr; /* Universal TCP address */
105 static char servname[] = "rpcbind";
106 static char superuser[] = "superuser";
108 int main __P((int, char *[]));
110 static int init_transport __P((struct netconfig *));
111 static void rbllist_add __P((rpcprog_t, rpcvers_t, struct netconfig *,
113 static void terminate __P((int));
114 static void parseargs __P((int, char *[]));
117 main(int argc, char *argv[])
119 struct netconfig *nconf;
120 void *nc_handle; /* Net config handle */
123 parseargs(argc, argv);
125 getrlimit(RLIMIT_NOFILE, &rl);
126 if (rl.rlim_cur < 128) {
127 if (rl.rlim_max <= 128)
128 rl.rlim_cur = rl.rlim_max;
131 setrlimit(RLIMIT_NOFILE, &rl);
133 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
134 if (geteuid()) { /* This command allowed only to root */
135 fprintf(stderr, "Sorry. You are not superuser\n");
138 nc_handle = setnetconfig(); /* open netconfig file */
139 if (nc_handle == NULL) {
140 syslog(LOG_ERR, "could not read /etc/netconfig");
148 nconf = getnetconfigent("unix");
150 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
153 init_transport(nconf);
155 while ((nconf = getnetconfig(nc_handle))) {
156 if (nconf->nc_flag & NC_VISIBLE)
157 init_transport(nconf);
159 endnetconfig(nc_handle);
161 /* catch the usual termination signals for graceful exit */
162 (void) signal(SIGCHLD, reap);
163 (void) signal(SIGINT, terminate);
164 (void) signal(SIGTERM, terminate);
165 (void) signal(SIGQUIT, terminate);
166 /* ignore others that could get sent */
167 (void) signal(SIGPIPE, SIG_IGN);
168 (void) signal(SIGHUP, SIG_IGN);
169 (void) signal(SIGUSR1, SIG_IGN);
170 (void) signal(SIGUSR2, SIG_IGN);
177 printf("rpcbind debugging enabled.");
179 printf(" Will abort on errors!\n");
185 err(1, "fork failed");
191 if((p = getpwnam(RUN_AS)) == NULL) {
192 syslog(LOG_ERR, "cannot get uid of daemon: %m");
195 if (setuid(p->pw_uid) == -1) {
196 syslog(LOG_ERR, "setuid to daemon failed: %m");
204 syslog(LOG_ERR, "svc_run returned unexpectedly");
212 * Adds the entry into the rpcbind database.
213 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
214 * Returns 0 if succeeds, else fails
217 init_transport(struct netconfig *nconf)
221 struct addrinfo hints, *res = NULL;
222 struct __rpc_sockinfo si;
224 int status; /* bound checking ? */
230 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
231 struct sockaddr_un sun;
234 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
235 (nconf->nc_semantics != NC_TPI_COTS) &&
236 (nconf->nc_semantics != NC_TPI_COTS_ORD))
237 return (1); /* not my type */
243 (void) fprintf(stderr, "%s: %ld lookup routines :\n",
244 nconf->nc_netid, nconf->nc_nlookups);
245 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
247 fprintf(stderr, "[%d] - %s\n", i, *s);
252 * XXX - using RPC library internal functions. For NC_TPI_CLTS
253 * we call this later, for each socket we like to bind.
255 if (nconf->nc_semantics != NC_TPI_CLTS) {
256 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
257 syslog(LOG_ERR, "cannot create socket for %s",
263 if (!__rpc_nconf2sockinfo(nconf, &si)) {
264 syslog(LOG_ERR, "cannot get information for %s",
269 if (!strcmp(nconf->nc_netid, "unix")) {
270 memset(&sun, 0, sizeof sun);
271 sun.sun_family = AF_LOCAL;
272 unlink(_PATH_RPCBINDSOCK);
273 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
274 sun.sun_len = SUN_LEN(&sun);
275 addrlen = sizeof (struct sockaddr_un);
276 sa = (struct sockaddr *)&sun;
278 /* Get rpcbind's address on this transport */
280 memset(&hints, 0, sizeof hints);
281 hints.ai_flags = AI_PASSIVE;
282 hints.ai_family = si.si_af;
283 hints.ai_socktype = si.si_socktype;
284 hints.ai_protocol = si.si_proto;
286 if (nconf->nc_semantics == NC_TPI_CLTS) {
288 * If no hosts were specified, just bind to INADDR_ANY. Otherwise
289 * make sure 127.0.0.1 is added to the list.
293 hosts = realloc(hosts, nhostsbak * sizeof(char *));
297 if (hints.ai_family == AF_INET) {
298 hosts[nhostsbak - 1] = "127.0.0.1";
299 } else if (hints.ai_family == AF_INET6) {
300 hosts[nhostsbak - 1] = "::1";
306 * Bind to specific IPs if asked to
309 while (nhostsbak > 0) {
312 * XXX - using RPC library internal functions.
314 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
315 syslog(LOG_ERR, "cannot create socket for %s",
319 switch (hints.ai_family) {
321 if (inet_pton(AF_INET, hosts[nhostsbak],
323 hints.ai_flags &= AI_NUMERICHOST;
326 * Skip if we have a AF_INET6 adress
328 if (inet_pton(AF_INET6,
329 hosts[nhostsbak], host_addr) == 1)
334 if (inet_pton(AF_INET6, hosts[nhostsbak],
336 hints.ai_flags &= AI_NUMERICHOST;
339 * Skip if we have a AF_INET adress
341 if (inet_pton(AF_INET, hosts[nhostsbak],
345 if (setsockopt(fd, IPPROTO_IPV6,
346 IPV6_V6ONLY, &on, sizeof on) < 0) {
348 "can't set v6-only binding for "
358 * If no hosts were specified, just bind to INADDR_ANY
360 if (strcmp("*", hosts[nhostsbak]) == 0)
361 hosts[nhostsbak] = NULL;
363 if ((aicode = getaddrinfo(hosts[nhostsbak],
364 servname, &hints, &res)) != 0) {
366 "cannot get local address for %s: %s",
367 nconf->nc_netid, gai_strerror(aicode));
370 addrlen = res->ai_addrlen;
371 sa = (struct sockaddr *)res->ai_addr;
372 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
373 if (bind(fd, sa, addrlen) != 0) {
374 syslog(LOG_ERR, "cannot bind %s on %s: %m",
375 (hosts[nhostsbak] == NULL) ? "*" :
376 hosts[nhostsbak], nconf->nc_netid);
382 (void) umask(oldmask);
384 /* Copy the address */
385 taddr.addr.len = taddr.addr.maxlen = addrlen;
386 taddr.addr.buf = malloc(addrlen);
387 if (taddr.addr.buf == NULL) {
389 "cannot allocate memory for %s address",
395 memcpy(taddr.addr.buf, sa, addrlen);
399 * for debugging print out our universal
406 nb.len = nb.maxlen = sa->sa_len;
407 uaddr = taddr2uaddr(nconf, &nb);
408 (void) fprintf(stderr,
409 "rpcbind : my address is %s\n", uaddr);
414 if (nconf->nc_semantics != NC_TPI_CLTS)
415 listen(fd, SOMAXCONN);
417 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
419 if (my_xprt == (SVCXPRT *)NULL) {
420 syslog(LOG_ERR, "%s: could not create service",
428 if (strcmp(nconf->nc_netid, "unix") != 0) {
429 if ((aicode = getaddrinfo(NULL, servname, &hints, &res))
432 "cannot get local address for %s: %s",
433 nconf->nc_netid, gai_strerror(aicode));
436 addrlen = res->ai_addrlen;
437 sa = (struct sockaddr *)res->ai_addr;
439 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
440 if (bind(fd, sa, addrlen) < 0) {
441 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
446 (void) umask(oldmask);
448 /* Copy the address */
449 taddr.addr.len = taddr.addr.maxlen = addrlen;
450 taddr.addr.buf = malloc(addrlen);
451 if (taddr.addr.buf == NULL) {
452 syslog(LOG_ERR, "cannot allocate memory for %s address",
458 memcpy(taddr.addr.buf, sa, addrlen);
461 /* for debugging print out our universal address */
466 nb.len = nb.maxlen = sa->sa_len;
467 uaddr = taddr2uaddr(nconf, &nb);
468 (void) fprintf(stderr, "rpcbind : my address is %s\n",
474 if (nconf->nc_semantics != NC_TPI_CLTS)
475 listen(fd, SOMAXCONN);
477 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, 0, 0);
478 if (my_xprt == (SVCXPRT *)NULL) {
479 syslog(LOG_ERR, "%s: could not create service",
487 * Register both the versions for tcp/ip, udp/ip and local.
489 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
490 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
491 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
492 strcmp(nconf->nc_netid, "unix") == 0) {
493 struct pmaplist *pml;
495 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
496 pmap_service, NULL)) {
497 syslog(LOG_ERR, "could not register on %s",
501 pml = malloc(sizeof (struct pmaplist));
503 syslog(LOG_ERR, "no memory!");
506 pml->pml_map.pm_prog = PMAPPROG;
507 pml->pml_map.pm_vers = PMAPVERS;
508 pml->pml_map.pm_port = PMAPPORT;
509 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
512 "cannot have more than one TCP transport");
515 tcptrans = strdup(nconf->nc_netid);
516 pml->pml_map.pm_prot = IPPROTO_TCP;
518 /* Let's snarf the universal address */
519 /* "h1.h2.h3.h4.p1.p2" */
520 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
521 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
524 "cannot have more than one UDP transport");
527 udptrans = strdup(nconf->nc_netid);
528 pml->pml_map.pm_prot = IPPROTO_UDP;
530 /* Let's snarf the universal address */
531 /* "h1.h2.h3.h4.p1.p2" */
532 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
533 } else if (strcmp(nconf->nc_netid, "unix") == 0)
534 pml->pml_map.pm_prot = IPPROTO_ST;
535 pml->pml_next = list_pml;
538 /* Add version 3 information */
539 pml = malloc(sizeof (struct pmaplist));
541 syslog(LOG_ERR, "no memory!");
544 pml->pml_map = list_pml->pml_map;
545 pml->pml_map.pm_vers = RPCBVERS;
546 pml->pml_next = list_pml;
549 /* Add version 4 information */
550 pml = malloc (sizeof (struct pmaplist));
552 syslog(LOG_ERR, "no memory!");
555 pml->pml_map = list_pml->pml_map;
556 pml->pml_map.pm_vers = RPCBVERS4;
557 pml->pml_next = list_pml;
560 /* Also add version 2 stuff to rpcbind list */
561 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
565 /* version 3 registration */
566 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
567 syslog(LOG_ERR, "could not register %s version 3",
571 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
573 /* version 4 registration */
574 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
575 syslog(LOG_ERR, "could not register %s version 4",
579 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
581 /* decide if bound checking works for this transport */
582 status = add_bndlist(nconf, &taddr.addr);
586 fprintf(stderr, "Error in finding bind status for %s\n",
588 } else if (status == 0) {
589 fprintf(stderr, "check binding for %s\n",
591 } else if (status > 0) {
592 fprintf(stderr, "No check binding for %s\n",
598 * rmtcall only supported on CLTS transports for now.
600 if (nconf->nc_semantics == NC_TPI_CLTS) {
601 status = create_rmtcall_fd(nconf);
607 "Could not create rmtcall fd for %s\n",
610 fprintf(stderr, "rmtcall fd for %s is %d\n",
611 nconf->nc_netid, status);
623 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
628 rbl = malloc(sizeof (rpcblist));
630 syslog(LOG_ERR, "no memory!");
634 rbl->rpcb_map.r_prog = prog;
635 rbl->rpcb_map.r_vers = vers;
636 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
637 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
638 rbl->rpcb_map.r_owner = strdup(superuser);
639 rbl->rpcb_next = list_rbl; /* Attach to global list */
644 * Catch the signal and die
651 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
652 write_warmstart(); /* Dump yourself */
661 write_warmstart(); /* Dump yourself */
666 /* get command line options */
668 parseargs(int argc, char *argv[])
672 while ((c = getopt(argc, argv, "dwah:ilLs")) != -1) {
675 doabort = 1; /* when debugging, do an abort on */
676 break; /* errors; for rpcbind developers */
683 hosts = realloc(hosts, nhosts * sizeof(char *));
685 errx(1, "Out of memory");
686 hosts[nhosts - 1] = strdup(optarg);
687 if (hosts[nhosts - 1] == NULL)
688 errx(1, "Out of memory");
708 fprintf(stderr, "usage: rpcbind [-Idwils]\n");
712 if (doabort && !debugging) {
714 "-a (abort) specified without -d (debugging) -- ignored.\n");
722 int save_errno = errno;
724 while (wait3(NULL, WNOHANG, NULL) > 0)
730 toggle_verboselog(int dummy)
732 verboselog = !verboselog;