1 /* $NetBSD: rpcbind.c,v 1.4 2013/10/19 17:45:00 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
39 static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
45 * Implements the program, version to address mapping for rpc.
49 #include <sys/types.h>
51 #include <sys/errno.h>
53 #include <sys/resource.h>
55 #include <sys/signal.h>
56 #include <sys/socket.h>
60 #include <netinet/in.h>
64 #include <netconfig.h>
71 #include <semaphore.h>
76 #include <rump/rump.h>
77 #include <rump/rump_syscalls.h>
79 /* Global variables */
80 int debugging = 1; /* 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 #include "svc_fdset.h"
86 /* who to suid to if -s is given */
87 #define RUN_AS "daemon"
91 int oldstyle_local = 0;
96 static int warmstart = 0; /* Grab a old copy of registrations */
100 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
101 const char *udptrans; /* Name of UDP transport */
102 const char *tcptrans; /* Name of TCP transport */
103 const char *udp_uaddr; /* Universal UDP address */
104 const char *tcp_uaddr; /* Universal TCP address */
106 static const char servname[] = "sunrpc";
108 const char rpcbind_superuser[] = "superuser";
109 const char rpcbind_unknown[] = "unknown";
111 static int init_transport(struct netconfig *);
112 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
114 static void terminate(int);
116 static void parseargs(int, char *[]);
119 int rpcbind_main(void *);
121 rpcbind_main(void *arg)
123 struct netconfig *nconf;
124 void *nc_handle; /* Net config handle */
126 int maxrec = RPC_MAXDATASIZE;
130 parseargs(argc, argv);
135 getrlimit(RLIMIT_NOFILE, &rl);
136 if (rl.rlim_cur < 128) {
137 if (rl.rlim_max <= 128)
138 rl.rlim_cur = rl.rlim_max;
141 setrlimit(RLIMIT_NOFILE, &rl);
144 if (geteuid()) /* This command allowed only to root */
145 errx(1, "Sorry. You are not superuser");
147 nc_handle = setnetconfig(); /* open netconfig file */
148 if (nc_handle == NULL)
149 errx(1, "could not read /etc/netconfig");
155 nconf = getnetconfigent("local");
157 errx(1, "can't find local transport");
159 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
161 init_transport(nconf);
163 while ((nconf = getnetconfig(nc_handle))) {
164 if (nconf->nc_flag & NC_VISIBLE)
165 init_transport(nconf);
167 endnetconfig(nc_handle);
169 /* catch the usual termination signals for graceful exit */
170 (void) signal(SIGCHLD, reap);
171 (void) signal(SIGINT, terminate);
172 (void) signal(SIGTERM, terminate);
173 (void) signal(SIGQUIT, terminate);
174 /* ignore others that could get sent */
175 (void) signal(SIGPIPE, SIG_IGN);
176 //(void) signal(SIGHUP, SIG_IGN); used by mountd
177 (void) signal(SIGUSR1, SIG_IGN);
178 (void) signal(SIGUSR2, SIG_IGN);
185 printf("rpcbind debugging enabled.");
187 printf(" Will abort on errors!\n");
193 err(1, "fork failed");
196 openlog("rpcbind", 0, LOG_DAEMON);
202 if((p = getpwnam(RUN_AS)) == NULL) {
203 syslog(LOG_ERR, "cannot get uid of daemon: %m");
206 if (setuid(p->pw_uid) == -1) {
207 syslog(LOG_ERR, "setuid to daemon failed: %m");
216 syslog(LOG_ERR, "svc_run returned unexpectedly");
224 * Adds the entry into the rpcbind database.
225 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
226 * Returns 0 if succeeds, else fails
229 init_transport(struct netconfig *nconf)
233 struct addrinfo hints, *res = NULL;
234 struct __rpc_sockinfo si;
236 int aicode, status, addrlen;
238 struct sockaddr_un sun;
241 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
242 (nconf->nc_semantics != NC_TPI_COTS) &&
243 (nconf->nc_semantics != NC_TPI_COTS_ORD))
244 return 1; /* not my type */
250 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
251 nconf->nc_netid, nconf->nc_nlookups);
252 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
254 (void)fprintf(stderr, "[%d] - %s\n", i, *s);
259 * XXX - using RPC library internal functions.
261 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
262 if (errno == EAFNOSUPPORT)
264 warn("Cannot create socket for `%s'", nconf->nc_netid);
268 if (!__rpc_nconf2sockinfo(nconf, &si)) {
269 warnx("Cannot get information for `%s'", nconf->nc_netid);
273 if (si.si_af == AF_INET6) {
275 * We're doing host-based access checks here, so don't allow
276 * v4-in-v6 to confuse things.
278 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
280 warn("Can't make socket ipv6 only");
286 if (!strcmp(nconf->nc_netid, "local")) {
287 (void)memset(&sun, 0, sizeof sun);
288 sun.sun_family = AF_LOCAL;
289 (void)rump_sys_unlink(_PATH_RPCBINDSOCK);
290 (void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
291 sizeof(sun.sun_path));
292 sun.sun_len = SUN_LEN(&sun);
293 addrlen = sizeof(struct sockaddr_un);
294 sa = (struct sockaddr *)&sun;
296 /* Get rpcbind's address on this transport */
298 (void)memset(&hints, 0, sizeof hints);
299 hints.ai_flags = AI_PASSIVE;
300 hints.ai_family = si.si_af;
301 hints.ai_socktype = si.si_socktype;
302 hints.ai_protocol = si.si_proto;
303 if ((aicode = getaddrinfo(NULL, servname, &hints, &res)) != 0) {
304 warnx("Cannot get local address for `%s' (%s)",
305 nconf->nc_netid, gai_strerror(aicode));
308 addrlen = res->ai_addrlen;
309 sa = (struct sockaddr *)res->ai_addr;
312 if (bind(fd, sa, addrlen) < 0) {
313 warn("Cannot bind `%s'", nconf->nc_netid);
319 if (sa->sa_family == AF_LOCAL)
320 if (rump_sys_chmod(sun.sun_path, S_IRWXU|S_IRWXG|S_IRWXO) == -1)
321 warn("Cannot chmod `%s'", sun.sun_path);
324 /* Copy the address */
325 taddr.addr.len = taddr.addr.maxlen = addrlen;
326 taddr.addr.buf = malloc(addrlen);
327 if (taddr.addr.buf == NULL) {
328 warn("Cannot allocate memory for `%s' address",
334 (void)memcpy(taddr.addr.buf, sa, addrlen);
337 /* for debugging print out our universal address */
342 nb.len = nb.maxlen = sa->sa_len;
343 uaddr = taddr2uaddr(nconf, &nb);
344 (void)fprintf(stderr, "rpcbind: my address is %s\n", uaddr);
352 if (nconf->nc_semantics != NC_TPI_CLTS)
353 listen(fd, SOMAXCONN);
355 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE,
357 if (my_xprt == NULL) {
358 warnx("Could not create service for `%s'", nconf->nc_netid);
364 * Register both the versions for tcp/ip, udp/ip and local.
366 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
367 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
368 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
369 strcmp(nconf->nc_netid, "local") == 0) {
370 struct pmaplist *pml;
372 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
374 warn("Could not register on `%s'", nconf->nc_netid);
377 pml = malloc(sizeof (struct pmaplist));
379 warn("Cannot allocate memory");
382 pml->pml_map.pm_prog = PMAPPROG;
383 pml->pml_map.pm_vers = PMAPVERS;
384 pml->pml_map.pm_port = PMAPPORT;
385 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
388 "Cannot have more than one TCP transport");
392 tcptrans = strdup(nconf->nc_netid);
393 if (tcptrans == NULL) {
395 warn("Cannot allocate memory");
398 pml->pml_map.pm_prot = IPPROTO_TCP;
400 /* Let's snarf the universal address */
401 /* "h1.h2.h3.h4.p1.p2" */
402 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
403 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
407 "Cannot have more than one UDP transport");
410 udptrans = strdup(nconf->nc_netid);
411 if (udptrans == NULL) {
413 warn("Cannot allocate memory");
416 pml->pml_map.pm_prot = IPPROTO_UDP;
418 /* Let's snarf the universal address */
419 /* "h1.h2.h3.h4.p1.p2" */
420 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
422 pml->pml_next = list_pml;
425 /* Add version 3 information */
426 pml = malloc(sizeof (struct pmaplist));
428 warn("Cannot allocate memory");
431 pml->pml_map = list_pml->pml_map;
432 pml->pml_map.pm_vers = RPCBVERS;
433 pml->pml_next = list_pml;
436 /* Add version 4 information */
437 pml = malloc(sizeof (struct pmaplist));
439 warn("Cannot allocate memory");
442 pml->pml_map = list_pml->pml_map;
443 pml->pml_map.pm_vers = RPCBVERS4;
444 pml->pml_next = list_pml;
447 /* Also add version 2 stuff to rpcbind list */
448 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
452 /* version 3 registration */
453 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
454 warn("Could not register %s version 3", nconf->nc_netid);
457 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
459 /* version 4 registration */
460 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
461 warn("Could not register %s version 4", nconf->nc_netid);
464 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
466 /* decide if bound checking works for this transport */
467 status = add_bndlist(nconf, &taddr.addr);
471 fprintf(stderr, "Error in finding bind status for %s\n",
473 } else if (status == 0) {
474 fprintf(stderr, "check binding for %s\n",
476 } else if (status > 0) {
477 fprintf(stderr, "No check binding for %s\n",
485 * rmtcall only supported on CLTS transports for now.
487 if (nconf->nc_semantics == NC_TPI_CLTS) {
488 status = create_rmtcall_fd(nconf);
494 "Could not create rmtcall fd for %s\n",
497 fprintf(stderr, "rmtcall fd for %s is %d\n",
498 nconf->nc_netid, status);
505 (void)rump_sys_close(fd);
510 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
515 rbl = malloc(sizeof(rpcblist));
517 warn("Out of memory");
521 rbl->rpcb_map.r_prog = prog;
522 rbl->rpcb_map.r_vers = vers;
523 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
524 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
525 rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
526 rbl->rpcb_next = list_rbl; /* Attach to global list */
531 * Catch the signal and die
538 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
539 write_warmstart(); /* Dump yourself */
548 write_warmstart(); /* Dump yourself */
554 /* get command line options */
556 parseargs(int argc, char *argv[])
560 while ((c = getopt(argc, argv, "dwailLs")) != -1) {
563 doabort = 1; /* when debugging, do an abort on */
564 break; /* errors; for rpcbind developers */
587 fprintf(stderr, "usage: rpcbind [-Idwils]\n");
591 if (doabort && !debugging) {
593 "-a (abort) specified without -d (debugging) -- ignored.\n");
602 int save_errno = errno;
604 while (wait3(NULL, WNOHANG, NULL) > 0)
610 toggle_verboselog(int dummy)
612 verboselog = !verboselog;