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