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;
102 static int warmstart = 0; /* Grab an old copy of registrations. */
106 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
107 char *udptrans; /* Name of UDP transport */
108 char *tcptrans; /* Name of TCP transport */
109 char *udp_uaddr; /* Universal UDP address */
110 char *tcp_uaddr; /* Universal TCP address */
112 static char servname[] = "rpcbind";
113 static char superuser[] = "superuser";
115 int main __P((int, char *[]));
117 static int init_transport __P((struct netconfig *));
118 static void rbllist_add __P((rpcprog_t, rpcvers_t, struct netconfig *,
120 static void terminate __P((int));
121 static void parseargs __P((int, char *[]));
124 main(int argc, char *argv[])
126 struct netconfig *nconf;
127 void *nc_handle; /* Net config handle */
129 int maxrec = RPC_MAXDATASIZE;
131 parseargs(argc, argv);
133 /* Check that another rpcbind isn't already running. */
134 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
135 O_RDONLY|O_CREAT, 0444))) == -1)
136 err(1, "%s", RPCBINDDLOCK);
138 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
139 errx(1, "another rpcbind is already running. Aborting");
141 getrlimit(RLIMIT_NOFILE, &rl);
142 if (rl.rlim_cur < 128) {
143 if (rl.rlim_max <= 128)
144 rl.rlim_cur = rl.rlim_max;
147 setrlimit(RLIMIT_NOFILE, &rl);
149 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
150 if (geteuid()) { /* This command allowed only to root */
151 fprintf(stderr, "Sorry. You are not superuser\n");
154 nc_handle = setnetconfig(); /* open netconfig file */
155 if (nc_handle == NULL) {
156 syslog(LOG_ERR, "could not read /etc/netconfig");
164 nconf = getnetconfigent("local");
166 nconf = getnetconfigent("unix");
168 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
172 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
174 init_transport(nconf);
176 while ((nconf = getnetconfig(nc_handle))) {
177 if (nconf->nc_flag & NC_VISIBLE)
178 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
182 init_transport(nconf);
184 endnetconfig(nc_handle);
186 /* catch the usual termination signals for graceful exit */
187 (void) signal(SIGCHLD, reap);
188 (void) signal(SIGINT, terminate);
189 (void) signal(SIGTERM, terminate);
190 (void) signal(SIGQUIT, terminate);
191 /* ignore others that could get sent */
192 (void) signal(SIGPIPE, SIG_IGN);
193 (void) signal(SIGHUP, SIG_IGN);
194 (void) signal(SIGUSR1, SIG_IGN);
195 (void) signal(SIGUSR2, SIG_IGN);
202 printf("rpcbind debugging enabled.");
204 printf(" Will abort on errors!\n");
210 err(1, "fork failed");
216 if((p = getpwnam(RUN_AS)) == NULL) {
217 syslog(LOG_ERR, "cannot get uid of daemon: %m");
220 if (setuid(p->pw_uid) == -1) {
221 syslog(LOG_ERR, "setuid to daemon failed: %m");
229 syslog(LOG_ERR, "svc_run returned unexpectedly");
237 * Adds the entry into the rpcbind database.
238 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
239 * Returns 0 if succeeds, else fails
242 init_transport(struct netconfig *nconf)
246 struct addrinfo hints, *res = NULL;
247 struct __rpc_sockinfo si;
249 int status; /* bound checking ? */
255 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
256 struct sockaddr_un sun;
259 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
260 (nconf->nc_semantics != NC_TPI_COTS) &&
261 (nconf->nc_semantics != NC_TPI_COTS_ORD))
262 return (1); /* not my type */
268 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
269 nconf->nc_netid, nconf->nc_nlookups);
270 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
272 fprintf(stderr, "[%d] - %s\n", i, *s);
277 * XXX - using RPC library internal functions.
279 if ((strcmp(nconf->nc_netid, "local") == 0) ||
280 (strcmp(nconf->nc_netid, "unix") == 0)) {
282 * For other transports we call this later, for each socket we
285 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
287 if (errno == EPROTONOSUPPORT)
289 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
295 if (!__rpc_nconf2sockinfo(nconf, &si)) {
296 syslog(LOG_ERR, "cannot get information for %s",
301 if ((strcmp(nconf->nc_netid, "local") == 0) ||
302 (strcmp(nconf->nc_netid, "unix") == 0)) {
303 memset(&sun, 0, sizeof sun);
304 sun.sun_family = AF_LOCAL;
305 unlink(_PATH_RPCBINDSOCK);
306 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
307 sun.sun_len = SUN_LEN(&sun);
308 addrlen = sizeof (struct sockaddr_un);
309 sa = (struct sockaddr *)&sun;
311 /* Get rpcbind's address on this transport */
313 memset(&hints, 0, sizeof hints);
314 hints.ai_flags = AI_PASSIVE;
315 hints.ai_family = si.si_af;
316 hints.ai_socktype = si.si_socktype;
317 hints.ai_protocol = si.si_proto;
320 if ((strcmp(nconf->nc_netid, "local") != 0) &&
321 (strcmp(nconf->nc_netid, "unix") != 0)) {
323 * If no hosts were specified, just bind to INADDR_ANY.
324 * Otherwise make sure 127.0.0.1 is added to the list.
328 hosts = realloc(hosts, nhostsbak * sizeof(char *));
332 if (hints.ai_family == AF_INET) {
333 hosts[nhostsbak - 1] = "127.0.0.1";
334 } else if (hints.ai_family == AF_INET6) {
335 hosts[nhostsbak - 1] = "::1";
341 * Bind to specific IPs if asked to
344 while (nhostsbak > 0) {
347 * XXX - using RPC library internal functions.
349 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
351 if (errno == EPROTONOSUPPORT &&
352 nconf->nc_semantics != NC_TPI_CLTS)
354 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
355 "cannot create socket for %s", nconf->nc_netid);
358 switch (hints.ai_family) {
360 if (inet_pton(AF_INET, hosts[nhostsbak],
362 hints.ai_flags &= AI_NUMERICHOST;
365 * Skip if we have an AF_INET6 adress.
367 if (inet_pton(AF_INET6,
368 hosts[nhostsbak], host_addr) == 1)
373 if (inet_pton(AF_INET6, hosts[nhostsbak],
375 hints.ai_flags &= AI_NUMERICHOST;
378 * Skip if we have an AF_INET adress.
380 if (inet_pton(AF_INET, hosts[nhostsbak],
384 if (setsockopt(fd, IPPROTO_IPV6,
385 IPV6_V6ONLY, &on, sizeof on) < 0) {
387 "can't set v6-only binding for "
397 * If no hosts were specified, just bind to INADDR_ANY
399 if (strcmp("*", hosts[nhostsbak]) == 0)
400 hosts[nhostsbak] = NULL;
401 if ((strcmp(nconf->nc_netid, "local") != 0) &&
402 (strcmp(nconf->nc_netid, "unix") != 0)) {
403 if ((aicode = getaddrinfo(hosts[nhostsbak],
404 servname, &hints, &res)) != 0) {
406 "cannot get local address for %s: %s",
407 nconf->nc_netid, gai_strerror(aicode));
410 addrlen = res->ai_addrlen;
411 sa = (struct sockaddr *)res->ai_addr;
413 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
414 if (bind(fd, sa, addrlen) != 0) {
415 syslog(LOG_ERR, "cannot bind %s on %s: %m",
416 (hosts[nhostsbak] == NULL) ? "*" :
417 hosts[nhostsbak], nconf->nc_netid);
423 (void)umask(oldmask);
425 /* Copy the address */
426 taddr.addr.len = taddr.addr.maxlen = addrlen;
427 taddr.addr.buf = malloc(addrlen);
428 if (taddr.addr.buf == NULL) {
430 "cannot allocate memory for %s address",
436 memcpy(taddr.addr.buf, sa, addrlen);
440 * for debugging print out our universal
447 nb.len = nb.maxlen = sa->sa_len;
448 uaddr = taddr2uaddr(nconf, &nb);
449 (void)fprintf(stderr,
450 "rpcbind : my address is %s\n", uaddr);
455 if (nconf->nc_semantics != NC_TPI_CLTS)
456 listen(fd, SOMAXCONN);
458 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
459 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
460 if (my_xprt == (SVCXPRT *)NULL) {
461 syslog(LOG_ERR, "%s: could not create service",
469 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
470 if (bind(fd, sa, addrlen) < 0) {
471 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
476 (void) umask(oldmask);
478 /* Copy the address */
479 taddr.addr.len = taddr.addr.maxlen = addrlen;
480 taddr.addr.buf = malloc(addrlen);
481 if (taddr.addr.buf == NULL) {
482 syslog(LOG_ERR, "cannot allocate memory for %s address",
488 memcpy(taddr.addr.buf, sa, addrlen);
491 /* for debugging print out our universal address */
496 nb.len = nb.maxlen = sa->sa_len;
497 uaddr = taddr2uaddr(nconf, &nb);
498 (void) fprintf(stderr, "rpcbind : my address is %s\n",
504 if (nconf->nc_semantics != NC_TPI_CLTS)
505 listen(fd, SOMAXCONN);
507 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
508 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
509 if (my_xprt == (SVCXPRT *)NULL) {
510 syslog(LOG_ERR, "%s: could not create service",
518 * Register both the versions for tcp/ip, udp/ip and local.
520 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
521 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
522 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
523 (strcmp(nconf->nc_netid, "unix") == 0) ||
524 (strcmp(nconf->nc_netid, "local") == 0)) {
525 struct pmaplist *pml;
527 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
529 syslog(LOG_ERR, "could not register on %s",
533 pml = malloc(sizeof (struct pmaplist));
535 syslog(LOG_ERR, "no memory!");
538 pml->pml_map.pm_prog = PMAPPROG;
539 pml->pml_map.pm_vers = PMAPVERS;
540 pml->pml_map.pm_port = PMAPPORT;
541 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
544 "cannot have more than one TCP transport");
547 tcptrans = strdup(nconf->nc_netid);
548 pml->pml_map.pm_prot = IPPROTO_TCP;
550 /* Let's snarf the universal address */
551 /* "h1.h2.h3.h4.p1.p2" */
552 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
553 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
556 "cannot have more than one UDP transport");
559 udptrans = strdup(nconf->nc_netid);
560 pml->pml_map.pm_prot = IPPROTO_UDP;
562 /* Let's snarf the universal address */
563 /* "h1.h2.h3.h4.p1.p2" */
564 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
565 } else if (strcmp(nconf->nc_netid, "local") == 0)
566 pml->pml_map.pm_prot = IPPROTO_ST;
567 else if (strcmp(nconf->nc_netid, "unix") == 0)
568 pml->pml_map.pm_prot = IPPROTO_ST;
569 pml->pml_next = list_pml;
572 /* Add version 3 information */
573 pml = malloc(sizeof (struct pmaplist));
575 syslog(LOG_ERR, "no memory!");
578 pml->pml_map = list_pml->pml_map;
579 pml->pml_map.pm_vers = RPCBVERS;
580 pml->pml_next = list_pml;
583 /* Add version 4 information */
584 pml = malloc (sizeof (struct pmaplist));
586 syslog(LOG_ERR, "no memory!");
589 pml->pml_map = list_pml->pml_map;
590 pml->pml_map.pm_vers = RPCBVERS4;
591 pml->pml_next = list_pml;
594 /* Also add version 2 stuff to rpcbind list */
595 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
599 /* version 3 registration */
600 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
601 syslog(LOG_ERR, "could not register %s version 3",
605 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
607 /* version 4 registration */
608 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
609 syslog(LOG_ERR, "could not register %s version 4",
613 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
615 /* decide if bound checking works for this transport */
616 status = add_bndlist(nconf, &taddr.addr);
620 fprintf(stderr, "Error in finding bind status for %s\n",
622 } else if (status == 0) {
623 fprintf(stderr, "check binding for %s\n",
625 } else if (status > 0) {
626 fprintf(stderr, "No check binding for %s\n",
632 * rmtcall only supported on CLTS transports for now.
634 if (nconf->nc_semantics == NC_TPI_CLTS) {
635 status = create_rmtcall_fd(nconf);
641 "Could not create rmtcall fd for %s\n",
644 fprintf(stderr, "rmtcall fd for %s is %d\n",
645 nconf->nc_netid, status);
657 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
662 rbl = malloc(sizeof (rpcblist));
664 syslog(LOG_ERR, "no memory!");
668 rbl->rpcb_map.r_prog = prog;
669 rbl->rpcb_map.r_vers = vers;
670 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
671 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
672 rbl->rpcb_map.r_owner = strdup(superuser);
673 rbl->rpcb_next = list_rbl; /* Attach to global list */
678 * Catch the signal and die
681 terminate(int dummy __unused)
683 close(rpcbindlockfd);
686 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
687 write_warmstart(); /* Dump yourself */
696 write_warmstart(); /* Dump yourself */
701 /* get command line options */
703 parseargs(int argc, char *argv[])
712 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
718 doabort = 1; /* when debugging, do an abort on */
719 break; /* errors; for rpcbind developers */
726 hosts = realloc(hosts, nhosts * sizeof(char *));
728 errx(1, "Out of memory");
729 hosts[nhosts - 1] = strdup(optarg);
730 if (hosts[nhosts - 1] == NULL)
731 errx(1, "Out of memory");
752 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
757 if (doabort && !debugging) {
759 "-a (abort) specified without -d (debugging) -- ignored.\n");
766 reap(int dummy __unused)
768 int save_errno = errno;
770 while (wait3(NULL, WNOHANG, NULL) > 0)
776 toggle_verboselog(int dummy __unused)
778 verboselog = !verboselog;