Home | History | Annotate | Line # | Download | only in rpcbind
rpcbind.c revision 1.21
      1 /*	$NetBSD: rpcbind.c,v 1.21 2015/05/09 18:22:37 dholland Exp $	*/
      2 
      3 /*
      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.
     10  *
     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.
     14  *
     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.
     18  *
     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.
     22  *
     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.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 /*
     32  * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
     33  */
     34 
     35 /* #ident	"@(#)rpcbind.c	1.19	94/04/25 SMI" */
     36 
     37 #if 0
     38 #ifndef lint
     39 static	char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
     40 #endif
     41 #endif
     42 
     43 /*
     44  * rpcbind.c
     45  * Implements the program, version to address mapping for rpc.
     46  *
     47  */
     48 
     49 #include <sys/types.h>
     50 #include <sys/stat.h>
     51 #include <sys/errno.h>
     52 #include <sys/time.h>
     53 #include <sys/resource.h>
     54 #include <sys/wait.h>
     55 #include <sys/signal.h>
     56 #include <sys/socket.h>
     57 #include <sys/un.h>
     58 #include <rpc/rpc.h>
     59 #ifdef PORTMAP
     60 #include <netinet/in.h>
     61 #endif
     62 #include <netdb.h>
     63 #include <stdio.h>
     64 #include <netconfig.h>
     65 #include <stdlib.h>
     66 #include <unistd.h>
     67 #include <syslog.h>
     68 #include <err.h>
     69 #include <util.h>
     70 #include <pwd.h>
     71 #include <string.h>
     72 #include <errno.h>
     73 #include "rpcbind.h"
     74 
     75 /* Global variables */
     76 int debugging = 0;	/* Tell me what's going on */
     77 int doabort = 0;	/* When debugging, do an abort on errors */
     78 rpcblist_ptr list_rbl;	/* A list of version 3/4 rpcbind services */
     79 
     80 /* who to suid to if -s is given */
     81 #define RUN_AS  "daemon"
     82 
     83 int runasdaemon = 0;
     84 int insecure = 0;
     85 int oldstyle_local = 0;
     86 int verboselog = 0;
     87 
     88 #ifdef WARMSTART
     89 /* Local Variable */
     90 static int warmstart = 0;	/* Grab a old copy of registrations */
     91 #endif
     92 
     93 #ifdef PORTMAP
     94 struct pmaplist *list_pml;	/* A list of version 2 rpcbind services */
     95 const char *udptrans;		/* Name of UDP transport */
     96 const char *tcptrans;		/* Name of TCP transport */
     97 const char *udp_uaddr;		/* Universal UDP address */
     98 const char *tcp_uaddr;		/* Universal TCP address */
     99 #endif
    100 static const char servname[] = "sunrpc";
    101 
    102 const char rpcbind_superuser[] = "superuser";
    103 const char rpcbind_unknown[] = "unknown";
    104 
    105 static int init_transport(struct netconfig *);
    106 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
    107     struct netbuf *);
    108 __dead static void terminate(int);
    109 static void parseargs(int, char *[]);
    110 
    111 int
    112 main(int argc, char *argv[])
    113 {
    114 	struct netconfig *nconf;
    115 	void *nc_handle;	/* Net config handle */
    116 	struct rlimit rl;
    117 	int maxrec = RPC_MAXDATASIZE;
    118 
    119 	parseargs(argc, argv);
    120 
    121 	if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && rl.rlim_cur < 128) {
    122 		if (rl.rlim_max <= 128)
    123 			rl.rlim_cur = rl.rlim_max;
    124 		else
    125 			rl.rlim_cur = 128;
    126 		if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
    127 			err(EXIT_FAILURE, "setrlimit(RLIMIT_NOFILE)");
    128 		}
    129 	}
    130 	nc_handle = setnetconfig(); 	/* open netconfig file */
    131 	if (nc_handle == NULL)
    132 		errx(1, "could not read /etc/netconfig");
    133 #ifdef PORTMAP
    134 	udptrans = "";
    135 	tcptrans = "";
    136 #endif
    137 
    138 	nconf = getnetconfigent("local");
    139 	if (nconf == NULL)
    140 		errx(1, "can't find local transport");
    141 
    142 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
    143 
    144 	init_transport(nconf);
    145 
    146 	while ((nconf = getnetconfig(nc_handle))) {
    147 		if (nconf->nc_flag & NC_VISIBLE)
    148 			init_transport(nconf);
    149 	}
    150 	endnetconfig(nc_handle);
    151 
    152 	/* catch the usual termination signals for graceful exit */
    153 	(void) signal(SIGCHLD, reap);
    154 	(void) signal(SIGINT, terminate);
    155 	(void) signal(SIGTERM, terminate);
    156 	(void) signal(SIGQUIT, terminate);
    157 	/* ignore others that could get sent */
    158 	(void) signal(SIGPIPE, SIG_IGN);
    159 	(void) signal(SIGHUP, SIG_IGN);
    160 	(void) signal(SIGUSR1, SIG_IGN);
    161 	(void) signal(SIGUSR2, SIG_IGN);
    162 #ifdef WARMSTART
    163 	if (warmstart) {
    164 		read_warmstart();
    165 	}
    166 #endif
    167 	if (debugging) {
    168 		printf("rpcbind debugging enabled.");
    169 		if (doabort) {
    170 			printf("  Will abort on errors!\n");
    171 		} else {
    172 			printf("\n");
    173 		}
    174 	} else {
    175 		if (daemon(0, 0))
    176 			err(1, "fork failed");
    177 	}
    178 
    179 	openlog("rpcbind", 0, LOG_DAEMON);
    180 	pidfile(NULL);
    181 
    182 	if (runasdaemon) {
    183 		struct passwd *p;
    184 
    185 		if((p = getpwnam(RUN_AS)) == NULL) {
    186 			syslog(LOG_ERR, "cannot get uid of daemon: %m");
    187 			exit(1);
    188 		}
    189 		if (setuid(p->pw_uid) == -1) {
    190 			syslog(LOG_ERR, "setuid to daemon failed: %m");
    191 			exit(1);
    192 		}
    193 	}
    194 
    195 	network_init();
    196 
    197 	my_svc_run();
    198 	syslog(LOG_ERR, "svc_run returned unexpectedly");
    199 	rpcbind_abort();
    200 	/* NOTREACHED */
    201 
    202 	return 0;
    203 }
    204 
    205 /*
    206  * Adds the entry into the rpcbind database.
    207  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
    208  * Returns 0 if succeeds, else fails
    209  */
    210 static int
    211 init_transport(struct netconfig *nconf)
    212 {
    213 	int fd;
    214 	struct t_bind taddr;
    215 	struct addrinfo hints, *res = NULL;
    216 	struct __rpc_sockinfo si;
    217 	SVCXPRT	*my_xprt;
    218 	int status;	/* bound checking ? */
    219 	int aicode;
    220 	int addrlen;
    221 	struct sockaddr *sa;
    222 	struct sockaddr_un sun;
    223 	const int one = 1;
    224 
    225 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
    226 		(nconf->nc_semantics != NC_TPI_COTS) &&
    227 		(nconf->nc_semantics != NC_TPI_COTS_ORD))
    228 		return 1;	/* not my type */
    229 #ifdef RPCBIND_DEBUG
    230 	if (debugging) {
    231 		int i;
    232 		char **s;
    233 
    234 		(void)fprintf(stderr, "%s: %ld lookup routines :\n",
    235 		    nconf->nc_netid, nconf->nc_nlookups);
    236 		for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
    237 		     i++, s++)
    238 			(void)fprintf(stderr, "[%d] - %s\n", i, *s);
    239 	}
    240 #endif
    241 
    242 	/*
    243 	 * XXX - using RPC library internal functions.
    244 	 */
    245 	if ((fd = __rpc_nconf2fd(nconf)) < 0) {
    246 		if (errno == EAFNOSUPPORT)
    247 			return 1;
    248 		warn("Cannot create socket for `%s'", nconf->nc_netid);
    249 		return 1;
    250 	}
    251 
    252 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
    253 		warnx("Cannot get information for `%s'", nconf->nc_netid);
    254 		return 1;
    255 	}
    256 
    257 	if (si.si_af == AF_INET6) {
    258 		/*
    259 		 * We're doing host-based access checks here, so don't allow
    260 		 * v4-in-v6 to confuse things.
    261 		 */
    262 		if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
    263 		    sizeof one) < 0) {
    264 			warn("Can't make socket ipv6 only");
    265 			return 1;
    266 		}
    267 	}
    268 
    269 
    270 	if (!strcmp(nconf->nc_netid, "local")) {
    271 		(void)memset(&sun, 0, sizeof sun);
    272 		sun.sun_family = AF_LOCAL;
    273 		(void)unlink(_PATH_RPCBINDSOCK);
    274 		(void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
    275 		    sizeof(sun.sun_path));
    276 		sun.sun_len = SUN_LEN(&sun);
    277 		addrlen = sizeof(struct sockaddr_un);
    278 		sa = (struct sockaddr *)&sun;
    279 	} else {
    280 		/* Get rpcbind's address on this transport */
    281 
    282 		(void)memset(&hints, 0, sizeof hints);
    283 		hints.ai_flags = AI_PASSIVE;
    284 		hints.ai_family = si.si_af;
    285 		hints.ai_socktype = si.si_socktype;
    286 		hints.ai_protocol = si.si_proto;
    287 		if ((aicode = getaddrinfo(NULL, servname, &hints, &res)) != 0) {
    288 			warnx("Cannot get local address for `%s' (%s)",
    289 			    nconf->nc_netid, gai_strerror(aicode));
    290 			return 1;
    291 		}
    292 		addrlen = res->ai_addrlen;
    293 		sa = (struct sockaddr *)res->ai_addr;
    294 	}
    295 
    296 	if (bind(fd, sa, addrlen) < 0) {
    297 		warn("Cannot bind `%s'", nconf->nc_netid);
    298 		if (res != NULL)
    299 			freeaddrinfo(res);
    300 		return 1;
    301 	}
    302 	if (sa->sa_family == AF_LOCAL)
    303 		if (chmod(sun.sun_path, S_IRWXU|S_IRWXG|S_IRWXO) == -1)
    304 			warn("Cannot chmod `%s'", sun.sun_path);
    305 
    306 	/* Copy the address */
    307 	taddr.addr.len = taddr.addr.maxlen = addrlen;
    308 	taddr.addr.buf = malloc(addrlen);
    309 	if (taddr.addr.buf == NULL) {
    310 		warn("Cannot allocate memory for `%s' address",
    311 		    nconf->nc_netid);
    312 		if (res != NULL)
    313 			freeaddrinfo(res);
    314 		return 1;
    315 	}
    316 	(void)memcpy(taddr.addr.buf, sa, addrlen);
    317 #ifdef RPCBIND_DEBUG
    318 	if (debugging) {
    319 		/* for debugging print out our universal address */
    320 		char *uaddr;
    321 		struct netbuf nb;
    322 
    323 		nb.buf = sa;
    324 		nb.len = nb.maxlen = sa->sa_len;
    325 		uaddr = taddr2uaddr(nconf, &nb);
    326 		(void)fprintf(stderr, "rpcbind: my address is %s\n", uaddr);
    327 		(void)free(uaddr);
    328 	}
    329 #endif
    330 
    331 	if (res != NULL)
    332 		freeaddrinfo(res);
    333 
    334 	if (nconf->nc_semantics != NC_TPI_CLTS)
    335 		listen(fd, SOMAXCONN);
    336 
    337 	my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE,
    338 	    RPC_MAXDATASIZE);
    339 	if (my_xprt == NULL) {
    340 		warnx("Could not create service for `%s'", nconf->nc_netid);
    341 		goto error;
    342 	}
    343 
    344 #ifdef PORTMAP
    345 	/*
    346 	 * Register both the versions for tcp/ip, udp/ip and local.
    347 	 */
    348 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
    349 		(strcmp(nconf->nc_proto, NC_TCP) == 0 ||
    350 		strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
    351 		strcmp(nconf->nc_netid, "local") == 0) {
    352 		struct pmaplist *pml;
    353 
    354 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
    355 			pmap_service, 0)) {
    356 			warn("Could not register on `%s'", nconf->nc_netid);
    357 			goto error;
    358 		}
    359 		pml = malloc(sizeof (struct pmaplist));
    360 		if (pml == NULL) {
    361 			warn("Cannot allocate memory");
    362 			goto error;
    363 		}
    364 		pml->pml_map.pm_prog = PMAPPROG;
    365 		pml->pml_map.pm_vers = PMAPVERS;
    366 		pml->pml_map.pm_port = PMAPPORT;
    367 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
    368 			if (tcptrans[0]) {
    369 				warnx(
    370 				    "Cannot have more than one TCP transport");
    371 				free(pml);
    372 				goto error;
    373 			}
    374 			tcptrans = strdup(nconf->nc_netid);
    375 			if (tcptrans == NULL) {
    376 				free(pml);
    377 				warn("Cannot allocate memory");
    378 				goto error;
    379 			}
    380 			pml->pml_map.pm_prot = IPPROTO_TCP;
    381 
    382 			/* Let's snarf the universal address */
    383 			/* "h1.h2.h3.h4.p1.p2" */
    384 			tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
    385 		} else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
    386 			if (udptrans[0]) {
    387 				free(pml);
    388 				warnx(
    389 				"Cannot have more than one UDP transport");
    390 				goto error;
    391 			}
    392 			udptrans = strdup(nconf->nc_netid);
    393 			if (udptrans == NULL) {
    394 				free(pml);
    395 				warn("Cannot allocate memory");
    396 				goto error;
    397 			}
    398 			pml->pml_map.pm_prot = IPPROTO_UDP;
    399 
    400 			/* Let's snarf the universal address */
    401 			/* "h1.h2.h3.h4.p1.p2" */
    402 			udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
    403 		}
    404 		pml->pml_next = list_pml;
    405 		list_pml = pml;
    406 
    407 		/* Add version 3 information */
    408 		pml = malloc(sizeof (struct pmaplist));
    409 		if (pml == NULL) {
    410 			warn("Cannot allocate memory");
    411 			goto error;
    412 		}
    413 		pml->pml_map = list_pml->pml_map;
    414 		pml->pml_map.pm_vers = RPCBVERS;
    415 		pml->pml_next = list_pml;
    416 		list_pml = pml;
    417 
    418 		/* Add version 4 information */
    419 		pml = malloc(sizeof (struct pmaplist));
    420 		if (pml == NULL) {
    421 			warn("Cannot allocate memory");
    422 			goto error;
    423 		}
    424 		pml->pml_map = list_pml->pml_map;
    425 		pml->pml_map.pm_vers = RPCBVERS4;
    426 		pml->pml_next = list_pml;
    427 		list_pml = pml;
    428 
    429 		/* Also add version 2 stuff to rpcbind list */
    430 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
    431 	}
    432 #endif
    433 
    434 	/* version 3 registration */
    435 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
    436 		warn("Could not register %s version 3", nconf->nc_netid);
    437 		goto error;
    438 	}
    439 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
    440 
    441 	/* version 4 registration */
    442 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
    443 		warn("Could not register %s version 4", nconf->nc_netid);
    444 		goto error;
    445 	}
    446 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
    447 
    448 	/* decide if bound checking works for this transport */
    449 	status = add_bndlist(nconf, &taddr.addr);
    450 #ifdef RPCBIND_DEBUG
    451 	if (debugging) {
    452 		if (status < 0) {
    453 			fprintf(stderr, "Error in finding bind status for %s\n",
    454 				nconf->nc_netid);
    455 		} else if (status == 0) {
    456 			fprintf(stderr, "check binding for %s\n",
    457 				nconf->nc_netid);
    458 		} else if (status > 0) {
    459 			fprintf(stderr, "No check binding for %s\n",
    460 				nconf->nc_netid);
    461 		}
    462 	}
    463 #else
    464 	__USE(status);
    465 #endif
    466 	/*
    467 	 * rmtcall only supported on CLTS transports for now.
    468 	 */
    469 	if (nconf->nc_semantics == NC_TPI_CLTS) {
    470 		status = create_rmtcall_fd(nconf);
    471 
    472 #ifdef RPCBIND_DEBUG
    473 		if (debugging) {
    474 			if (status < 0) {
    475 				fprintf(stderr,
    476 				    "Could not create rmtcall fd for %s\n",
    477 					nconf->nc_netid);
    478 			} else {
    479 				fprintf(stderr, "rmtcall fd for %s is %d\n",
    480 					nconf->nc_netid, status);
    481 			}
    482 		}
    483 #endif
    484 	}
    485 	return (0);
    486 error:
    487 	(void)close(fd);
    488 	return (1);
    489 }
    490 
    491 static void
    492 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
    493 	    struct netbuf *addr)
    494 {
    495 	rpcblist_ptr rbl;
    496 
    497 	rbl = malloc(sizeof(rpcblist));
    498 	if (rbl == NULL) {
    499 		warn("Out of memory");
    500 		return;
    501 	}
    502 
    503 	rbl->rpcb_map.r_prog = prog;
    504 	rbl->rpcb_map.r_vers = vers;
    505 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
    506 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
    507 	rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
    508 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
    509 	list_rbl = rbl;
    510 }
    511 
    512 /*
    513  * Catch the signal and die
    514  */
    515 static void
    516 terminate(int dummy)
    517 {
    518 #ifdef WARMSTART
    519 	syslog(LOG_ERR,
    520 		"rpcbind terminating on signal. Restart with \"rpcbind -w\"");
    521 	write_warmstart();	/* Dump yourself */
    522 #endif
    523 	exit(2);
    524 }
    525 
    526 void
    527 rpcbind_abort()
    528 {
    529 #ifdef WARMSTART
    530 	write_warmstart();	/* Dump yourself */
    531 #endif
    532 	abort();
    533 }
    534 
    535 /* get command line options */
    536 static void
    537 parseargs(int argc, char *argv[])
    538 {
    539 	int c;
    540 
    541 	while ((c = getopt(argc, argv, "dwailLs")) != -1) {
    542 		switch (c) {
    543 		case 'a':
    544 			doabort = 1;	/* when debugging, do an abort on */
    545 			break;		/* errors; for rpcbind developers */
    546 					/* only! */
    547 		case 'd':
    548 			debugging = 1;
    549 			break;
    550 		case 'i':
    551 			insecure = 1;
    552 			break;
    553 		case 'L':
    554 			oldstyle_local = 1;
    555 			break;
    556 		case 'l':
    557 			verboselog = 1;
    558 			break;
    559 		case 's':
    560 			runasdaemon = 1;
    561 			break;
    562 #ifdef WARMSTART
    563 		case 'w':
    564 			warmstart = 1;
    565 			break;
    566 #endif
    567 		default:	/* error */
    568 			fprintf(stderr,	"usage: rpcbind [-Idwils]\n");
    569 			exit (1);
    570 		}
    571 	}
    572 	if (doabort && !debugging) {
    573 	    fprintf(stderr,
    574 		"-a (abort) specified without -d (debugging) -- ignored.\n");
    575 	    doabort = 0;
    576 	}
    577 }
    578 
    579 void
    580 reap(int dummy)
    581 {
    582 	int save_errno = errno;
    583 
    584 	while (wait3(NULL, WNOHANG, NULL) > 0)
    585 		;
    586 	errno = save_errno;
    587 }
    588 
    589 void
    590 toggle_verboselog(int dummy)
    591 {
    592 	verboselog = !verboselog;
    593 }
    594