2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpcbind.c 1.19 94/04/25 SMI; 1.35 89/04/21 Copyr 1984 Sun Micro
30 * $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $
31 * $FreeBSD: src/usr.sbin/rpcbind/rpcbind.c,v 1.20 2008/02/14 20:12:23 yar Exp $
35 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
40 * Implements the program, version to address mapping for rpc.
44 #include <sys/types.h>
46 #include <sys/errno.h>
48 #include <sys/resource.h>
50 #include <sys/signal.h>
51 #include <sys/socket.h>
54 #include <rpc/rpc_com.h>
56 #include <netinet/in.h>
58 #include <arpa/inet.h>
62 #include <netconfig.h>
73 /* Global variables */
74 int debugging = 0; /* Tell me what's going on */
75 int doabort = 0; /* When debugging, do an abort on errors */
76 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
78 /* who to suid to if -s is given */
79 #define RUN_AS "daemon"
81 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
85 int oldstyle_local = 0;
96 static int warmstart = 0; /* Grab an old copy of registrations. */
100 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
101 char *udptrans; /* Name of UDP transport */
102 char *tcptrans; /* Name of TCP transport */
103 char *udp_uaddr; /* Universal UDP address */
104 char *tcp_uaddr; /* Universal TCP address */
106 static char servname[] = "rpcbind";
107 static char superuser[] = "superuser";
109 static int init_transport(struct netconfig *);
110 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
112 static void terminate(int);
113 static void parseargs(int, char *[]);
116 main(int argc, char *argv[])
118 struct netconfig *nconf;
119 void *nc_handle; /* Net config handle */
121 int maxrec = RPC_MAXDATASIZE;
123 parseargs(argc, argv);
125 /* Check that another rpcbind isn't already running. */
126 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
127 O_RDONLY|O_CREAT, 0444))) == -1)
128 err(1, "%s", RPCBINDDLOCK);
130 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
131 errx(1, "another rpcbind is already running. Aborting");
133 getrlimit(RLIMIT_NOFILE, &rl);
134 if (rl.rlim_cur < 128) {
135 if (rl.rlim_max <= 128)
136 rl.rlim_cur = rl.rlim_max;
139 setrlimit(RLIMIT_NOFILE, &rl);
141 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
142 if (geteuid()) { /* This command allowed only to root */
143 fprintf(stderr, "Sorry. You are not superuser\n");
146 nc_handle = setnetconfig(); /* open netconfig file */
147 if (nc_handle == NULL) {
148 syslog(LOG_ERR, "could not read /etc/netconfig");
156 nconf = getnetconfigent("local");
158 nconf = getnetconfigent("unix");
160 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
164 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
166 init_transport(nconf);
168 while ((nconf = getnetconfig(nc_handle))) {
169 if (nconf->nc_flag & NC_VISIBLE)
170 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
174 init_transport(nconf);
176 endnetconfig(nc_handle);
178 /* catch the usual termination signals for graceful exit */
179 signal(SIGCHLD, reap);
180 signal(SIGINT, terminate);
181 signal(SIGTERM, terminate);
182 signal(SIGQUIT, terminate);
183 /* ignore others that could get sent */
184 signal(SIGPIPE, SIG_IGN);
185 signal(SIGHUP, SIG_IGN);
186 signal(SIGUSR1, SIG_IGN);
187 signal(SIGUSR2, SIG_IGN);
194 printf("rpcbind debugging enabled.");
196 printf(" Will abort on errors!\n");
202 err(1, "fork failed");
208 if((p = getpwnam(RUN_AS)) == NULL) {
209 syslog(LOG_ERR, "cannot get uid of daemon: %m");
212 if (setuid(p->pw_uid) == -1) {
213 syslog(LOG_ERR, "setuid to daemon failed: %m");
221 syslog(LOG_ERR, "svc_run returned unexpectedly");
229 * Adds the entry into the rpcbind database.
230 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
231 * Returns 0 if succeeds, else fails
234 init_transport(struct netconfig *nconf)
238 struct addrinfo hints, *res = NULL;
239 struct __rpc_sockinfo si;
241 int status; /* bound checking ? */
247 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
248 struct sockaddr_un sun;
251 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
252 (nconf->nc_semantics != NC_TPI_COTS) &&
253 (nconf->nc_semantics != NC_TPI_COTS_ORD))
254 return (1); /* not my type */
260 fprintf(stderr, "%s: %ld lookup routines :\n",
261 nconf->nc_netid, nconf->nc_nlookups);
262 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
264 fprintf(stderr, "[%d] - %s\n", i, *s);
269 * XXX - using RPC library internal functions.
271 if ((strcmp(nconf->nc_netid, "local") == 0) ||
272 (strcmp(nconf->nc_netid, "unix") == 0)) {
274 * For other transports we call this later, for each socket we
277 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;
312 if ((strcmp(nconf->nc_netid, "local") != 0) &&
313 (strcmp(nconf->nc_netid, "unix") != 0)) {
315 * If no hosts were specified, just bind to INADDR_ANY.
316 * Otherwise make sure 127.0.0.1 is added to the list.
320 hosts = realloc(hosts, nhostsbak * sizeof(char *));
324 if (hints.ai_family == AF_INET) {
325 hosts[nhostsbak - 1] = "127.0.0.1";
326 } else if (hints.ai_family == AF_INET6) {
327 hosts[nhostsbak - 1] = "::1";
333 * Bind to specific IPs if asked to
336 while (nhostsbak > 0) {
339 * XXX - using RPC library internal functions.
341 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
343 if (errno == EPROTONOSUPPORT &&
344 nconf->nc_semantics != NC_TPI_CLTS)
346 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
347 "cannot create socket for %s", nconf->nc_netid);
350 switch (hints.ai_family) {
352 if (inet_pton(AF_INET, hosts[nhostsbak],
354 hints.ai_flags &= AI_NUMERICHOST;
357 * Skip if we have an AF_INET6 address.
359 if (inet_pton(AF_INET6,
360 hosts[nhostsbak], host_addr) == 1) {
367 if (inet_pton(AF_INET6, hosts[nhostsbak],
369 hints.ai_flags &= AI_NUMERICHOST;
372 * Skip if we have an AF_INET address.
374 if (inet_pton(AF_INET, hosts[nhostsbak],
380 if (setsockopt(fd, IPPROTO_IPV6,
381 IPV6_V6ONLY, &on, sizeof on) < 0) {
383 "can't set v6-only binding for "
393 * If no hosts were specified, just bind to INADDR_ANY
395 if (strcmp("*", hosts[nhostsbak]) == 0)
396 hosts[nhostsbak] = NULL;
397 if ((strcmp(nconf->nc_netid, "local") != 0) &&
398 (strcmp(nconf->nc_netid, "unix") != 0)) {
399 if ((aicode = getaddrinfo(hosts[nhostsbak],
400 servname, &hints, &res)) != 0) {
402 "cannot get local address for %s: %s",
403 nconf->nc_netid, gai_strerror(aicode));
406 addrlen = res->ai_addrlen;
407 sa = (struct sockaddr *)res->ai_addr;
409 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
410 if (bind(fd, sa, addrlen) != 0) {
411 syslog(LOG_ERR, "cannot bind %s on %s: %m",
412 (hosts[nhostsbak] == NULL) ? "*" :
413 hosts[nhostsbak], nconf->nc_netid);
421 /* Copy the address */
422 taddr.addr.len = taddr.addr.maxlen = addrlen;
423 taddr.addr.buf = malloc(addrlen);
424 if (taddr.addr.buf == NULL) {
426 "cannot allocate memory for %s address",
432 memcpy(taddr.addr.buf, sa, addrlen);
436 * for debugging print out our universal
443 nb.len = nb.maxlen = sa->sa_len;
444 uaddr = taddr2uaddr(nconf, &nb);
445 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
450 if (nconf->nc_semantics != NC_TPI_CLTS)
451 listen(fd, SOMAXCONN);
453 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
454 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
455 if (my_xprt == (SVCXPRT *)NULL) {
456 syslog(LOG_ERR, "%s: could not create service",
464 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
465 if (bind(fd, sa, addrlen) < 0) {
466 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
473 /* Copy the address */
474 taddr.addr.len = taddr.addr.maxlen = addrlen;
475 taddr.addr.buf = malloc(addrlen);
476 if (taddr.addr.buf == NULL) {
477 syslog(LOG_ERR, "cannot allocate memory for %s address",
483 memcpy(taddr.addr.buf, sa, addrlen);
486 /* for debugging print out our universal address */
491 nb.len = nb.maxlen = sa->sa_len;
492 uaddr = taddr2uaddr(nconf, &nb);
493 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
498 if (nconf->nc_semantics != NC_TPI_CLTS)
499 listen(fd, SOMAXCONN);
501 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
502 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
503 if (my_xprt == (SVCXPRT *)NULL) {
504 syslog(LOG_ERR, "%s: could not create service",
512 * Register both the versions for tcp/ip, udp/ip and local.
514 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
515 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
516 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
517 (strcmp(nconf->nc_netid, "unix") == 0) ||
518 (strcmp(nconf->nc_netid, "local") == 0)) {
519 struct pmaplist *pml;
521 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
523 syslog(LOG_ERR, "could not register on %s",
527 pml = malloc(sizeof (struct pmaplist));
529 syslog(LOG_ERR, "no memory!");
532 pml->pml_map.pm_prog = PMAPPROG;
533 pml->pml_map.pm_vers = PMAPVERS;
534 pml->pml_map.pm_port = PMAPPORT;
535 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
538 "cannot have more than one TCP transport");
541 tcptrans = strdup(nconf->nc_netid);
542 pml->pml_map.pm_prot = IPPROTO_TCP;
544 /* Let's snarf the universal address */
545 /* "h1.h2.h3.h4.p1.p2" */
546 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
547 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
550 "cannot have more than one UDP transport");
553 udptrans = strdup(nconf->nc_netid);
554 pml->pml_map.pm_prot = IPPROTO_UDP;
556 /* Let's snarf the universal address */
557 /* "h1.h2.h3.h4.p1.p2" */
558 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
559 } else if (strcmp(nconf->nc_netid, "local") == 0)
560 pml->pml_map.pm_prot = IPPROTO_ST;
561 else if (strcmp(nconf->nc_netid, "unix") == 0)
562 pml->pml_map.pm_prot = IPPROTO_ST;
563 pml->pml_next = list_pml;
566 /* Add version 3 information */
567 pml = malloc(sizeof (struct pmaplist));
569 syslog(LOG_ERR, "no memory!");
572 pml->pml_map = list_pml->pml_map;
573 pml->pml_map.pm_vers = RPCBVERS;
574 pml->pml_next = list_pml;
577 /* Add version 4 information */
578 pml = malloc (sizeof (struct pmaplist));
580 syslog(LOG_ERR, "no memory!");
583 pml->pml_map = list_pml->pml_map;
584 pml->pml_map.pm_vers = RPCBVERS4;
585 pml->pml_next = list_pml;
588 /* Also add version 2 stuff to rpcbind list */
589 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
593 /* version 3 registration */
594 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
595 syslog(LOG_ERR, "could not register %s version 3",
599 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
601 /* version 4 registration */
602 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
603 syslog(LOG_ERR, "could not register %s version 4",
607 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
609 /* decide if bound checking works for this transport */
610 status = add_bndlist(nconf, &taddr.addr);
614 fprintf(stderr, "Error in finding bind status for %s\n",
616 } else if (status == 0) {
617 fprintf(stderr, "check binding for %s\n",
619 } else if (status > 0) {
620 fprintf(stderr, "No check binding for %s\n",
626 * rmtcall only supported on CLTS transports for now.
628 if (nconf->nc_semantics == NC_TPI_CLTS) {
629 status = create_rmtcall_fd(nconf);
635 "Could not create rmtcall fd for %s\n",
638 fprintf(stderr, "rmtcall fd for %s is %d\n",
639 nconf->nc_netid, status);
651 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
656 rbl = malloc(sizeof (rpcblist));
658 syslog(LOG_ERR, "no memory!");
662 rbl->rpcb_map.r_prog = prog;
663 rbl->rpcb_map.r_vers = vers;
664 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
665 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
666 rbl->rpcb_map.r_owner = strdup(superuser);
667 rbl->rpcb_next = list_rbl; /* Attach to global list */
672 * Catch the signal and die
675 terminate(int dummy __unused)
677 close(rpcbindlockfd);
680 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
681 write_warmstart(); /* Dump yourself */
690 write_warmstart(); /* Dump yourself */
695 /* get command line options */
697 parseargs(int argc, char *argv[])
706 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
712 doabort = 1; /* when debugging, do an abort on */
713 break; /* errors; for rpcbind developers */
720 hosts = realloc(hosts, nhosts * sizeof(char *));
722 errx(1, "Out of memory");
723 hosts[nhosts - 1] = strdup(optarg);
724 if (hosts[nhosts - 1] == NULL)
725 errx(1, "Out of memory");
746 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
751 if (doabort && !debugging) {
753 "-a (abort) specified without -d (debugging) -- ignored.\n");
760 reap(int dummy __unused)
762 int save_errno = errno;
764 while (wait3(NULL, WNOHANG, NULL) > 0)
770 toggle_verboselog(int dummy __unused)
772 verboselog = !verboselog;