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