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 $
34 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
39 * Implements the program, version to address mapping for rpc.
43 #include <sys/types.h>
45 #include <sys/errno.h>
47 #include <sys/resource.h>
49 #include <sys/signal.h>
50 #include <sys/socket.h>
53 #include <rpc/rpc_com.h>
55 #include <netinet/in.h>
57 #include <arpa/inet.h>
61 #include <netconfig.h>
72 /* Global variables */
73 int debugging = 0; /* Tell me what's going on */
74 int doabort = 0; /* When debugging, do an abort on errors */
75 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
77 /* who to suid to if -s is given */
78 #define RUN_AS "daemon"
80 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
84 int oldstyle_local = 0;
95 static int warmstart = 0; /* Grab an 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 static int init_transport(struct netconfig *);
109 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
111 static void terminate(int);
112 static void parseargs(int, char *[]);
115 main(int argc, char *argv[])
117 struct netconfig *nconf;
118 void *nc_handle; /* Net config handle */
120 int maxrec = RPC_MAXDATASIZE;
122 parseargs(argc, argv);
124 /* Check that another rpcbind isn't already running. */
125 if ((rpcbindlockfd = (open(RPCBINDDLOCK,
126 O_RDONLY|O_CREAT, 0444))) == -1)
127 err(1, "%s", RPCBINDDLOCK);
129 if(flock(rpcbindlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
130 errx(1, "another rpcbind is already running. Aborting");
132 getrlimit(RLIMIT_NOFILE, &rl);
133 if (rl.rlim_cur < 128) {
134 if (rl.rlim_max <= 128)
135 rl.rlim_cur = rl.rlim_max;
138 setrlimit(RLIMIT_NOFILE, &rl);
140 openlog("rpcbind", LOG_CONS, LOG_DAEMON);
141 if (geteuid()) { /* This command allowed only to root */
142 fprintf(stderr, "Sorry. You are not superuser\n");
145 nc_handle = setnetconfig(); /* open netconfig file */
146 if (nc_handle == NULL) {
147 syslog(LOG_ERR, "could not read /etc/netconfig");
155 nconf = getnetconfigent("local");
157 nconf = getnetconfigent("unix");
159 syslog(LOG_ERR, "%s: can't find local transport\n", argv[0]);
163 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
165 init_transport(nconf);
167 while ((nconf = getnetconfig(nc_handle))) {
168 if (nconf->nc_flag & NC_VISIBLE)
169 if (ipv6_only == 1 && strcmp(nconf->nc_protofmly,
173 init_transport(nconf);
175 endnetconfig(nc_handle);
177 /* catch the usual termination signals for graceful exit */
178 signal(SIGCHLD, reap);
179 signal(SIGINT, terminate);
180 signal(SIGTERM, terminate);
181 signal(SIGQUIT, terminate);
182 /* ignore others that could get sent */
183 signal(SIGPIPE, SIG_IGN);
184 signal(SIGHUP, SIG_IGN);
185 signal(SIGUSR1, SIG_IGN);
186 signal(SIGUSR2, SIG_IGN);
193 printf("rpcbind debugging enabled.");
195 printf(" Will abort on errors!\n");
201 err(1, "fork failed");
207 if((p = getpwnam(RUN_AS)) == NULL) {
208 syslog(LOG_ERR, "cannot get uid of daemon: %m");
211 if (setuid(p->pw_uid) == -1) {
212 syslog(LOG_ERR, "setuid to daemon failed: %m");
220 syslog(LOG_ERR, "svc_run returned unexpectedly");
228 * Adds the entry into the rpcbind database.
229 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
230 * Returns 0 if succeeds, else fails
233 init_transport(struct netconfig *nconf)
237 struct addrinfo hints, *res = NULL;
238 struct __rpc_sockinfo si;
240 int status; /* bound checking ? */
246 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
247 struct sockaddr_un sun;
250 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
251 (nconf->nc_semantics != NC_TPI_COTS) &&
252 (nconf->nc_semantics != NC_TPI_COTS_ORD))
253 return (1); /* not my type */
259 fprintf(stderr, "%s: %ld lookup routines :\n",
260 nconf->nc_netid, nconf->nc_nlookups);
261 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
263 fprintf(stderr, "[%d] - %s\n", i, *s);
268 * XXX - using RPC library internal functions.
270 if ((strcmp(nconf->nc_netid, "local") == 0) ||
271 (strcmp(nconf->nc_netid, "unix") == 0)) {
273 * For other transports we call this later, for each socket we
276 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
278 if (errno == EPROTONOSUPPORT)
280 syslog(non_fatal?LOG_DEBUG:LOG_ERR, "cannot create socket for %s",
286 if (!__rpc_nconf2sockinfo(nconf, &si)) {
287 syslog(LOG_ERR, "cannot get information for %s",
292 if ((strcmp(nconf->nc_netid, "local") == 0) ||
293 (strcmp(nconf->nc_netid, "unix") == 0)) {
294 memset(&sun, 0, sizeof sun);
295 sun.sun_family = AF_LOCAL;
296 unlink(_PATH_RPCBINDSOCK);
297 strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
298 sun.sun_len = SUN_LEN(&sun);
299 addrlen = sizeof (struct sockaddr_un);
300 sa = (struct sockaddr *)&sun;
302 /* Get rpcbind's address on this transport */
304 memset(&hints, 0, sizeof hints);
305 hints.ai_flags = AI_PASSIVE;
306 hints.ai_family = si.si_af;
307 hints.ai_socktype = si.si_socktype;
308 hints.ai_protocol = si.si_proto;
311 if ((strcmp(nconf->nc_netid, "local") != 0) &&
312 (strcmp(nconf->nc_netid, "unix") != 0)) {
314 * If no hosts were specified, just bind to INADDR_ANY.
315 * Otherwise make sure 127.0.0.1 is added to the list.
319 hosts = realloc(hosts, nhostsbak * sizeof(char *));
323 if (hints.ai_family == AF_INET) {
324 hosts[nhostsbak - 1] = "127.0.0.1";
325 } else if (hints.ai_family == AF_INET6) {
326 hosts[nhostsbak - 1] = "::1";
332 * Bind to specific IPs if asked to
335 while (nhostsbak > 0) {
338 * XXX - using RPC library internal functions.
340 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
342 if (errno == EPROTONOSUPPORT &&
343 nconf->nc_semantics != NC_TPI_CLTS)
345 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
346 "cannot create socket for %s", nconf->nc_netid);
349 switch (hints.ai_family) {
351 if (inet_pton(AF_INET, hosts[nhostsbak],
353 hints.ai_flags &= AI_NUMERICHOST;
356 * Skip if we have an AF_INET6 address.
358 if (inet_pton(AF_INET6,
359 hosts[nhostsbak], host_addr) == 1) {
366 if (inet_pton(AF_INET6, hosts[nhostsbak],
368 hints.ai_flags &= AI_NUMERICHOST;
371 * Skip if we have an AF_INET address.
373 if (inet_pton(AF_INET, hosts[nhostsbak],
379 if (setsockopt(fd, IPPROTO_IPV6,
380 IPV6_V6ONLY, &on, sizeof on) < 0) {
382 "can't set v6-only binding for "
392 * If no hosts were specified, just bind to INADDR_ANY
394 if (strcmp("*", hosts[nhostsbak]) == 0)
395 hosts[nhostsbak] = NULL;
396 if ((strcmp(nconf->nc_netid, "local") != 0) &&
397 (strcmp(nconf->nc_netid, "unix") != 0)) {
398 if ((aicode = getaddrinfo(hosts[nhostsbak],
399 servname, &hints, &res)) != 0) {
401 "cannot get local address for %s: %s",
402 nconf->nc_netid, gai_strerror(aicode));
405 addrlen = res->ai_addrlen;
406 sa = (struct sockaddr *)res->ai_addr;
408 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
409 if (bind(fd, sa, addrlen) != 0) {
410 syslog(LOG_ERR, "cannot bind %s on %s: %m",
411 (hosts[nhostsbak] == NULL) ? "*" :
412 hosts[nhostsbak], nconf->nc_netid);
420 /* Copy the address */
421 taddr.addr.len = taddr.addr.maxlen = addrlen;
422 taddr.addr.buf = malloc(addrlen);
423 if (taddr.addr.buf == NULL) {
425 "cannot allocate memory for %s address",
431 memcpy(taddr.addr.buf, sa, addrlen);
435 * for debugging print out our universal
442 nb.len = nb.maxlen = sa->sa_len;
443 uaddr = taddr2uaddr(nconf, &nb);
444 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
449 if (nconf->nc_semantics != NC_TPI_CLTS)
450 listen(fd, SOMAXCONN);
452 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
453 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
454 if (my_xprt == NULL) {
455 syslog(LOG_ERR, "%s: could not create service",
463 oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
464 if (bind(fd, sa, addrlen) < 0) {
465 syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
472 /* Copy the address */
473 taddr.addr.len = taddr.addr.maxlen = addrlen;
474 taddr.addr.buf = malloc(addrlen);
475 if (taddr.addr.buf == NULL) {
476 syslog(LOG_ERR, "cannot allocate memory for %s address",
482 memcpy(taddr.addr.buf, sa, addrlen);
485 /* for debugging print out our universal address */
490 nb.len = nb.maxlen = sa->sa_len;
491 uaddr = taddr2uaddr(nconf, &nb);
492 fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
497 if (nconf->nc_semantics != NC_TPI_CLTS)
498 listen(fd, SOMAXCONN);
500 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr,
501 RPC_MAXDATASIZE, RPC_MAXDATASIZE);
502 if (my_xprt == NULL) {
503 syslog(LOG_ERR, "%s: could not create service",
511 * Register both the versions for tcp/ip, udp/ip and local.
513 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
514 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
515 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
516 (strcmp(nconf->nc_netid, "unix") == 0) ||
517 (strcmp(nconf->nc_netid, "local") == 0)) {
518 struct pmaplist *pml;
520 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
522 syslog(LOG_ERR, "could not register on %s",
526 pml = malloc(sizeof (struct pmaplist));
528 syslog(LOG_ERR, "no memory!");
531 pml->pml_map.pm_prog = PMAPPROG;
532 pml->pml_map.pm_vers = PMAPVERS;
533 pml->pml_map.pm_port = PMAPPORT;
534 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
537 "cannot have more than one TCP transport");
540 tcptrans = strdup(nconf->nc_netid);
541 pml->pml_map.pm_prot = IPPROTO_TCP;
543 /* Let's snarf the universal address */
544 /* "h1.h2.h3.h4.p1.p2" */
545 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
546 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
549 "cannot have more than one UDP transport");
552 udptrans = strdup(nconf->nc_netid);
553 pml->pml_map.pm_prot = IPPROTO_UDP;
555 /* Let's snarf the universal address */
556 /* "h1.h2.h3.h4.p1.p2" */
557 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
558 } else if (strcmp(nconf->nc_netid, "local") == 0)
559 pml->pml_map.pm_prot = IPPROTO_ST;
560 else if (strcmp(nconf->nc_netid, "unix") == 0)
561 pml->pml_map.pm_prot = IPPROTO_ST;
562 pml->pml_next = list_pml;
565 /* Add version 3 information */
566 pml = malloc(sizeof (struct pmaplist));
568 syslog(LOG_ERR, "no memory!");
571 pml->pml_map = list_pml->pml_map;
572 pml->pml_map.pm_vers = RPCBVERS;
573 pml->pml_next = list_pml;
576 /* Add version 4 information */
577 pml = malloc (sizeof (struct pmaplist));
579 syslog(LOG_ERR, "no memory!");
582 pml->pml_map = list_pml->pml_map;
583 pml->pml_map.pm_vers = RPCBVERS4;
584 pml->pml_next = list_pml;
587 /* Also add version 2 stuff to rpcbind list */
588 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
592 /* version 3 registration */
593 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
594 syslog(LOG_ERR, "could not register %s version 3",
598 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
600 /* version 4 registration */
601 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
602 syslog(LOG_ERR, "could not register %s version 4",
606 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
608 /* decide if bound checking works for this transport */
609 status = add_bndlist(nconf, &taddr.addr);
613 fprintf(stderr, "Error in finding bind status for %s\n",
615 } else if (status == 0) {
616 fprintf(stderr, "check binding for %s\n",
618 } else if (status > 0) {
619 fprintf(stderr, "No check binding for %s\n",
625 * rmtcall only supported on CLTS transports for now.
627 if (nconf->nc_semantics == NC_TPI_CLTS) {
628 status = create_rmtcall_fd(nconf);
634 "Could not create rmtcall fd for %s\n",
637 fprintf(stderr, "rmtcall fd for %s is %d\n",
638 nconf->nc_netid, status);
650 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
655 rbl = malloc(sizeof (rpcblist));
657 syslog(LOG_ERR, "no memory!");
661 rbl->rpcb_map.r_prog = prog;
662 rbl->rpcb_map.r_vers = vers;
663 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
664 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
665 rbl->rpcb_map.r_owner = strdup(superuser);
666 rbl->rpcb_next = list_rbl; /* Attach to global list */
671 * Catch the signal and die
674 terminate(int dummy __unused)
676 close(rpcbindlockfd);
679 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
680 write_warmstart(); /* Dump yourself */
689 write_warmstart(); /* Dump yourself */
694 /* get command line options */
696 parseargs(int argc, char *argv[])
705 while ((c = getopt(argc, argv, "6adh:iLls" WSOP)) != -1) {
711 doabort = 1; /* when debugging, do an abort on */
712 break; /* errors; for rpcbind developers */
719 hosts = realloc(hosts, nhosts * sizeof(char *));
721 errx(1, "Out of memory");
722 hosts[nhosts - 1] = strdup(optarg);
723 if (hosts[nhosts - 1] == NULL)
724 errx(1, "Out of memory");
745 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
750 if (doabort && !debugging) {
752 "-a (abort) specified without -d (debugging) -- ignored.\n");
759 reap(int dummy __unused)
761 int save_errno = errno;
763 while (wait3(NULL, WNOHANG, NULL) > 0)
769 toggle_verboselog(int dummy __unused)
771 verboselog = !verboselog;