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>
63 #include <arpa/inet.h>
67 #include <netconfig.h>
78 /* Global variables */
79 int debugging = 0; /* Tell me what's going on */
80 int doabort = 0; /* When debugging, do an abort on errors */
81 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
83 /* who to suid to if -s is given */
84 #define RUN_AS "daemon"
86 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
90 int oldstyle_local = 0;
100 static int warmstart = 0; /* Grab a old copy of registrations */
104 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
105 char *udptrans; /* Name of UDP transport */
106 char *tcptrans; /* Name of TCP transport */
107 char *udp_uaddr; /* Universal UDP address */
108 char *tcp_uaddr; /* Universal TCP address */
110 static char servname[] = "rpcbind";
111 static char superuser[] = "superuser";
113 int main __P((int, char *[]));
115 static int init_transport __P((struct netconfig *));
116 static void rbllist_add __P((rpcprog_t, rpcvers_t, struct netconfig *,
118 static void terminate __P((int));
119 static void parseargs __P((int, char *[]));
122 main(int argc, char *argv[])
124 struct netconfig *nconf;
125 void *nc_handle; /* Net config handle */
128 parseargs(argc, argv);
130 /* Check that another rpcbind isn't already running. */
131 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
132 O_RDONLY|O_CREAT, 0444))) == -1)
133 err(1, "%s", RPCBINDDLOCK);
135 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
136 errx(1, "another rpcbind is already running. Aborting");
138 getrlimit(RLIMIT_NOFILE, &rl);
139 if (rl.rlim_cur < 128) {
140 if (rl.rlim_max <= 128)
141 rl.rlim_cur = rl.rlim_max;
144 setrlimit(RLIMIT_NOFILE, &rl);
146 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
147 if (geteuid()) { /* This command allowed only to root */
148 fprintf(stderr, "Sorry. You are not superuser\n");
151 nc_handle = setnetconfig(); /* open netconfig file */
152 if (nc_handle == NULL) {
153 syslog(LOG_ERR, "could not read /etc/netconfig");
161 nconf = getnetconfigent("unix");
163 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
166 init_transport(nconf);
168 while ((nconf = getnetconfig(nc_handle))) {
169 if (nconf->nc_flag & NC_VISIBLE)
170 init_transport(nconf);
172 endnetconfig(nc_handle);
174 /* catch the usual termination signals for graceful exit */
175 (void) signal(SIGCHLD, reap);
176 (void) signal(SIGINT, terminate);
177 (void) signal(SIGTERM, terminate);
178 (void) signal(SIGQUIT, terminate);
179 /* ignore others that could get sent */
180 (void) signal(SIGPIPE, SIG_IGN);
181 (void) signal(SIGHUP, SIG_IGN);
182 (void) signal(SIGUSR1, SIG_IGN);
183 (void) signal(SIGUSR2, SIG_IGN);
190 printf("rpcbind debugging enabled.");
192 printf(" Will abort on errors!\n");
198 err(1, "fork failed");
204 if((p = getpwnam(RUN_AS)) == NULL) {
205 syslog(LOG_ERR, "cannot get uid of daemon: %m");
208 if (setuid(p->pw_uid) == -1) {
209 syslog(LOG_ERR, "setuid to daemon failed: %m");
217 syslog(LOG_ERR, "svc_run returned unexpectedly");
225 * Adds the entry into the rpcbind database.
226 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
227 * Returns 0 if succeeds, else fails
230 init_transport(struct netconfig *nconf)
234 struct addrinfo hints, *res = NULL;
235 struct __rpc_sockinfo si;
237 int status; /* bound checking ? */
243 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
244 struct sockaddr_un sun;
247 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
248 (nconf->nc_semantics != NC_TPI_COTS) &&
249 (nconf->nc_semantics != NC_TPI_COTS_ORD))
250 return (1); /* not my type */
256 (void) fprintf(stderr, "%s: %ld lookup routines :\n",
257 nconf->nc_netid, nconf->nc_nlookups);
258 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
260 fprintf(stderr, "[%d] - %s\n", i, *s);
265 * XXX - using RPC library internal functions. For NC_TPI_CLTS
266 * we call this later, for each socket we like to bind.
268 if (nconf->nc_semantics != NC_TPI_CLTS) {
269 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
270 syslog(LOG_ERR, "cannot create socket for %s",
276 if (!__rpc_nconf2sockinfo(nconf, &si)) {
277 syslog(LOG_ERR, "cannot get information for %s",
282 if (!strcmp(nconf->nc_netid, "unix")) {
283 memset(&sun, 0, sizeof sun);
284 sun.sun_family = AF_LOCAL;
285 unlink(_PATH_RPCBINDSOCK);
286 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
287 sun.sun_len = SUN_LEN(&sun);
288 addrlen = sizeof (struct sockaddr_un);
289 sa = (struct sockaddr *)&sun;
291 /* Get rpcbind's address on this transport */
293 memset(&hints, 0, sizeof hints);
294 hints.ai_flags = AI_PASSIVE;
295 hints.ai_family = si.si_af;
296 hints.ai_socktype = si.si_socktype;
297 hints.ai_protocol = si.si_proto;
299 if (nconf->nc_semantics == NC_TPI_CLTS) {
301 * If no hosts were specified, just bind to INADDR_ANY. Otherwise
302 * make sure 127.0.0.1 is added to the list.
306 hosts = realloc(hosts, nhostsbak * sizeof(char *));
310 if (hints.ai_family == AF_INET) {
311 hosts[nhostsbak - 1] = "127.0.0.1";
312 } else if (hints.ai_family == AF_INET6) {
313 hosts[nhostsbak - 1] = "::1";
319 * Bind to specific IPs if asked to
322 while (nhostsbak > 0) {
325 * XXX - using RPC library internal functions.
327 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
328 syslog(LOG_ERR, "cannot create socket for %s",
332 switch (hints.ai_family) {
334 if (inet_pton(AF_INET, hosts[nhostsbak],
336 hints.ai_flags &= AI_NUMERICHOST;
339 * Skip if we have a AF_INET6 adress
341 if (inet_pton(AF_INET6,
342 hosts[nhostsbak], host_addr) == 1)
347 if (inet_pton(AF_INET6, hosts[nhostsbak],
349 hints.ai_flags &= AI_NUMERICHOST;
352 * Skip if we have a AF_INET adress
354 if (inet_pton(AF_INET, hosts[nhostsbak],
358 if (setsockopt(fd, IPPROTO_IPV6,
359 IPV6_V6ONLY, &on, sizeof on) < 0) {
361 "can't set v6-only binding for "
371 * If no hosts were specified, just bind to INADDR_ANY
373 if (strcmp("*", hosts[nhostsbak]) == 0)
374 hosts[nhostsbak] = NULL;
376 if ((aicode = getaddrinfo(hosts[nhostsbak],
377 servname, &hints, &res)) != 0) {
379 "cannot get local address for %s: %s",
380 nconf->nc_netid, gai_strerror(aicode));
383 addrlen = res->ai_addrlen;
384 sa = (struct sockaddr *)res->ai_addr;
385 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
386 if (bind(fd, sa, addrlen) != 0) {
387 syslog(LOG_ERR, "cannot bind %s on %s: %m",
388 (hosts[nhostsbak] == NULL) ? "*" :
389 hosts[nhostsbak], nconf->nc_netid);
395 (void) umask(oldmask);
397 /* Copy the address */
398 taddr.addr.len = taddr.addr.maxlen = addrlen;
399 taddr.addr.buf = malloc(addrlen);
400 if (taddr.addr.buf == NULL) {
402 "cannot allocate memory for %s address",
408 memcpy(taddr.addr.buf, sa, addrlen);
412 * for debugging print out our universal
419 nb.len = nb.maxlen = sa->sa_len;
420 uaddr = taddr2uaddr(nconf, &nb);
421 (void) fprintf(stderr,
422 "rpcbind : my address is %s\n", uaddr);
427 if (nconf->nc_semantics != NC_TPI_CLTS)
428 listen(fd, SOMAXCONN);
430 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
432 if (my_xprt == (SVCXPRT *)NULL) {
433 syslog(LOG_ERR, "%s: could not create service",
441 if (strcmp(nconf->nc_netid, "unix") != 0) {
442 if ((aicode = getaddrinfo(NULL, servname, &hints, &res))
445 "cannot get local address for %s: %s",
446 nconf->nc_netid, gai_strerror(aicode));
449 addrlen = res->ai_addrlen;
450 sa = (struct sockaddr *)res->ai_addr;
452 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
453 if (bind(fd, sa, addrlen) < 0) {
454 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
459 (void) umask(oldmask);
461 /* Copy the address */
462 taddr.addr.len = taddr.addr.maxlen = addrlen;
463 taddr.addr.buf = malloc(addrlen);
464 if (taddr.addr.buf == NULL) {
465 syslog(LOG_ERR, "cannot allocate memory for %s address",
471 memcpy(taddr.addr.buf, sa, addrlen);
474 /* for debugging print out our universal address */
479 nb.len = nb.maxlen = sa->sa_len;
480 uaddr = taddr2uaddr(nconf, &nb);
481 (void) fprintf(stderr, "rpcbind : my address is %s\n",
487 if (nconf->nc_semantics != NC_TPI_CLTS)
488 listen(fd, SOMAXCONN);
490 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, 0, 0);
491 if (my_xprt == (SVCXPRT *)NULL) {
492 syslog(LOG_ERR, "%s: could not create service",
500 * Register both the versions for tcp/ip, udp/ip and local.
502 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
503 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
504 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
505 strcmp(nconf->nc_netid, "unix") == 0) {
506 struct pmaplist *pml;
508 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
509 pmap_service, NULL)) {
510 syslog(LOG_ERR, "could not register on %s",
514 pml = malloc(sizeof (struct pmaplist));
516 syslog(LOG_ERR, "no memory!");
519 pml->pml_map.pm_prog = PMAPPROG;
520 pml->pml_map.pm_vers = PMAPVERS;
521 pml->pml_map.pm_port = PMAPPORT;
522 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
525 "cannot have more than one TCP transport");
528 tcptrans = strdup(nconf->nc_netid);
529 pml->pml_map.pm_prot = IPPROTO_TCP;
531 /* Let's snarf the universal address */
532 /* "h1.h2.h3.h4.p1.p2" */
533 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
534 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
537 "cannot have more than one UDP transport");
540 udptrans = strdup(nconf->nc_netid);
541 pml->pml_map.pm_prot = IPPROTO_UDP;
543 /* Let's snarf the universal address */
544 /* "h1.h2.h3.h4.p1.p2" */
545 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
546 } else if (strcmp(nconf->nc_netid, "unix") == 0)
547 pml->pml_map.pm_prot = IPPROTO_ST;
548 pml->pml_next = list_pml;
551 /* Add version 3 information */
552 pml = malloc(sizeof (struct pmaplist));
554 syslog(LOG_ERR, "no memory!");
557 pml->pml_map = list_pml->pml_map;
558 pml->pml_map.pm_vers = RPCBVERS;
559 pml->pml_next = list_pml;
562 /* Add version 4 information */
563 pml = malloc (sizeof (struct pmaplist));
565 syslog(LOG_ERR, "no memory!");
568 pml->pml_map = list_pml->pml_map;
569 pml->pml_map.pm_vers = RPCBVERS4;
570 pml->pml_next = list_pml;
573 /* Also add version 2 stuff to rpcbind list */
574 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
578 /* version 3 registration */
579 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
580 syslog(LOG_ERR, "could not register %s version 3",
584 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
586 /* version 4 registration */
587 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
588 syslog(LOG_ERR, "could not register %s version 4",
592 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
594 /* decide if bound checking works for this transport */
595 status = add_bndlist(nconf, &taddr.addr);
599 fprintf(stderr, "Error in finding bind status for %s\n",
601 } else if (status == 0) {
602 fprintf(stderr, "check binding for %s\n",
604 } else if (status > 0) {
605 fprintf(stderr, "No check binding for %s\n",
611 * rmtcall only supported on CLTS transports for now.
613 if (nconf->nc_semantics == NC_TPI_CLTS) {
614 status = create_rmtcall_fd(nconf);
620 "Could not create rmtcall fd for %s\n",
623 fprintf(stderr, "rmtcall fd for %s is %d\n",
624 nconf->nc_netid, status);
636 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
641 rbl = malloc(sizeof (rpcblist));
643 syslog(LOG_ERR, "no memory!");
647 rbl->rpcb_map.r_prog = prog;
648 rbl->rpcb_map.r_vers = vers;
649 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
650 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
651 rbl->rpcb_map.r_owner = strdup(superuser);
652 rbl->rpcb_next = list_rbl; /* Attach to global list */
657 * Catch the signal and die
660 terminate(int dummy __unused)
662 close(rpcbindlockfd);
665 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
666 write_warmstart(); /* Dump yourself */
675 write_warmstart(); /* Dump yourself */
680 /* get command line options */
682 parseargs(int argc, char *argv[])
686 while ((c = getopt(argc, argv, "dwah:ilLs")) != -1) {
689 doabort = 1; /* when debugging, do an abort on */
690 break; /* errors; for rpcbind developers */
697 hosts = realloc(hosts, nhosts * sizeof(char *));
699 errx(1, "Out of memory");
700 hosts[nhosts - 1] = strdup(optarg);
701 if (hosts[nhosts - 1] == NULL)
702 errx(1, "Out of memory");
722 fprintf(stderr, "usage: rpcbind [-Idwils]\n");
726 if (doabort && !debugging) {
728 "-a (abort) specified without -d (debugging) -- ignored.\n");
734 reap(int dummy __unused)
736 int save_errno = errno;
738 while (wait3(NULL, WNOHANG, NULL) > 0)
744 toggle_verboselog(int dummy __unused)
746 verboselog = !verboselog;