xref: /onnv-gate/usr/src/cmd/rpcbind/rpcbind.c (revision 2128:6403b9743e73)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
290Sstevel@tonic-gate  * The Regents of the University of California
300Sstevel@tonic-gate  * All Rights Reserved
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
330Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
340Sstevel@tonic-gate  * contributors.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * rpcbind.c
410Sstevel@tonic-gate  * Implements the program, version to address mapping for rpc.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <dlfcn.h>
460Sstevel@tonic-gate #include <stdio.h>
471914Scasper #include <stdio_ext.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <unistd.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <rpc/rpc.h>
530Sstevel@tonic-gate #include <netconfig.h>
540Sstevel@tonic-gate #include <netdir.h>
550Sstevel@tonic-gate #include <errno.h>
560Sstevel@tonic-gate #include <sys/wait.h>
570Sstevel@tonic-gate #include <signal.h>
580Sstevel@tonic-gate #include <string.h>
590Sstevel@tonic-gate #include <stdlib.h>
600Sstevel@tonic-gate #include <thread.h>
610Sstevel@tonic-gate #include <synch.h>
620Sstevel@tonic-gate #include <stdarg.h>
630Sstevel@tonic-gate #ifdef PORTMAP
640Sstevel@tonic-gate #include <netinet/in.h>
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate #include <arpa/inet.h>
670Sstevel@tonic-gate #include <sys/termios.h>
680Sstevel@tonic-gate #include "rpcbind.h"
690Sstevel@tonic-gate #include <sys/syslog.h>
700Sstevel@tonic-gate #include <sys/stat.h>
710Sstevel@tonic-gate #include <syslog.h>
720Sstevel@tonic-gate #include <string.h>
730Sstevel@tonic-gate #include <sys/time.h>
740Sstevel@tonic-gate #include <sys/resource.h>
750Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
760Sstevel@tonic-gate #include <priv_utils.h>
770Sstevel@tonic-gate #include <libscf.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #ifdef PORTMAP
800Sstevel@tonic-gate extern void pmap_service(struct svc_req *, SVCXPRT *xprt);
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate extern void rpcb_service_3(struct svc_req *, SVCXPRT *xprt);
830Sstevel@tonic-gate extern void rpcb_service_4(struct svc_req *, SVCXPRT *xprt);
840Sstevel@tonic-gate extern void read_warmstart(void);
850Sstevel@tonic-gate extern void write_warmstart(void);
860Sstevel@tonic-gate extern int Is_ipv6present(void);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	MAX_FILEDESC_LIMIT	1023
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static void terminate(int);
91*2128Sjjj static void note_refresh(int);
920Sstevel@tonic-gate static void detachfromtty(void);
930Sstevel@tonic-gate static void parseargs(int, char *[]);
940Sstevel@tonic-gate static void rbllist_add(ulong_t, ulong_t, struct netconfig *, struct netbuf *);
950Sstevel@tonic-gate static int init_transport(struct netconfig *);
960Sstevel@tonic-gate static int check_netconfig(void);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static boolean_t check_hostserv(struct netconfig *, const char *, const char *);
991676Sjpk static int setopt_reuseaddr(int);
1001676Sjpk static int setopt_anon_mlp(int);
1011676Sjpk static int setup_callit(int);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* Global variables */
1040Sstevel@tonic-gate #ifdef ND_DEBUG
1050Sstevel@tonic-gate int debugging = 1;	/* Tell me what's going on */
1060Sstevel@tonic-gate #else
1070Sstevel@tonic-gate int debugging = 0;	/* Tell me what's going on */
1080Sstevel@tonic-gate #endif
1090Sstevel@tonic-gate static int ipv6flag = 0;
1100Sstevel@tonic-gate int doabort = 0;	/* When debugging, do an abort on errors */
1110Sstevel@tonic-gate static int listen_backlog = 64;
1120Sstevel@tonic-gate rpcblist_ptr list_rbl;	/* A list of version 3/4 rpcbind services */
1130Sstevel@tonic-gate char *loopback_dg;	/* Datagram loopback transport, for set and unset */
1140Sstevel@tonic-gate char *loopback_vc;	/* COTS loopback transport, for set and unset */
1150Sstevel@tonic-gate char *loopback_vc_ord;	/* COTS_ORD loopback transport, for set and unset */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate boolean_t verboselog = B_FALSE;
1180Sstevel@tonic-gate boolean_t wrap_enabled = B_FALSE;
1190Sstevel@tonic-gate boolean_t allow_indirect = B_TRUE;
1202104Sjjj boolean_t local_only = B_FALSE;
1210Sstevel@tonic-gate 
122*2128Sjjj volatile sig_atomic_t sigrefresh;
123*2128Sjjj 
1240Sstevel@tonic-gate /* Local Variable */
1250Sstevel@tonic-gate static int warmstart = 0;	/* Grab a old copy of registrations */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #ifdef PORTMAP
1280Sstevel@tonic-gate PMAPLIST *list_pml;	/* A list of version 2 rpcbind services */
1290Sstevel@tonic-gate char *udptrans;		/* Name of UDP transport */
1300Sstevel@tonic-gate char *tcptrans;		/* Name of TCP transport */
1310Sstevel@tonic-gate char *udp_uaddr;	/* Universal UDP address */
1320Sstevel@tonic-gate char *tcp_uaddr;	/* Universal TCP address */
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate static char servname[] = "rpcbind";
1350Sstevel@tonic-gate static char superuser[] = "superuser";
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static const char daemon_dir[] = DAEMON_DIR;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
main(int argc,char * argv[])1400Sstevel@tonic-gate main(int argc, char *argv[])
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	struct netconfig *nconf;
1430Sstevel@tonic-gate 	void *nc_handle;	/* Net config handle */
1440Sstevel@tonic-gate 	struct rlimit rl;
1450Sstevel@tonic-gate 	int maxrecsz = RPC_MAXDATASIZE;
1461676Sjpk 	boolean_t can_do_mlp;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	parseargs(argc, argv);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	getrlimit(RLIMIT_NOFILE, &rl);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (rl.rlim_cur < MAX_FILEDESC_LIMIT) {
1530Sstevel@tonic-gate 		if (rl.rlim_max <= MAX_FILEDESC_LIMIT)
1540Sstevel@tonic-gate 			rl.rlim_cur = rl.rlim_max;
1550Sstevel@tonic-gate 		else
1560Sstevel@tonic-gate 			rl.rlim_cur = MAX_FILEDESC_LIMIT;
1570Sstevel@tonic-gate 		setrlimit(RLIMIT_NOFILE, &rl);
1580Sstevel@tonic-gate 	}
1591914Scasper 	(void) enable_extended_FILE_stdio(-1, -1);
1601914Scasper 
1610Sstevel@tonic-gate 	openlog("rpcbind", LOG_CONS, LOG_DAEMON);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	/*
1640Sstevel@tonic-gate 	 * Create the daemon directory in /var/run
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	if (mkdir(daemon_dir, DAEMON_DIR_MODE) == 0 || errno == EEXIST) {
1670Sstevel@tonic-gate 		chmod(daemon_dir, DAEMON_DIR_MODE);
1680Sstevel@tonic-gate 		chown(daemon_dir, DAEMON_UID, DAEMON_GID);
1690Sstevel@tonic-gate 	} else {
1700Sstevel@tonic-gate 		syslog(LOG_ERR, "failed to create \"%s\": %m", daemon_dir);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * These privileges are required for the t_bind check rpcbind uses
1750Sstevel@tonic-gate 	 * to determine whether a service is still live or not.
1760Sstevel@tonic-gate 	 */
1771676Sjpk 	can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
1780Sstevel@tonic-gate 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, DAEMON_UID,
1791676Sjpk 	    DAEMON_GID, PRIV_NET_PRIVADDR, PRIV_SYS_NFS,
1801676Sjpk 	    can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) {
1810Sstevel@tonic-gate 		fprintf(stderr, "Insufficient privileges\n");
1820Sstevel@tonic-gate 		exit(1);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Enable non-blocking mode and maximum record size checks for
1870Sstevel@tonic-gate 	 * connection oriented transports.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
1900Sstevel@tonic-gate 		syslog(LOG_INFO, "unable to set RPC max record size");
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	nc_handle = setnetconfig(); 	/* open netconfig file */
1940Sstevel@tonic-gate 	if (nc_handle == NULL) {
1950Sstevel@tonic-gate 		syslog(LOG_ERR, "could not read /etc/netconfig");
1960Sstevel@tonic-gate 		exit(1);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	loopback_dg = "";
1990Sstevel@tonic-gate 	loopback_vc = "";
2000Sstevel@tonic-gate 	loopback_vc_ord = "";
2010Sstevel@tonic-gate #ifdef PORTMAP
2020Sstevel@tonic-gate 	udptrans = "";
2030Sstevel@tonic-gate 	tcptrans = "";
2040Sstevel@tonic-gate #endif
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	{
2070Sstevel@tonic-gate 		/*
2080Sstevel@tonic-gate 		 * rpcbind is the first application to encounter the
2090Sstevel@tonic-gate 		 * various netconfig files.  check_netconfig() verifies
2100Sstevel@tonic-gate 		 * that they are set up correctly and complains loudly
2110Sstevel@tonic-gate 		 * if not.
2120Sstevel@tonic-gate 		 */
2130Sstevel@tonic-gate 		int trouble;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 		trouble = check_netconfig();
2160Sstevel@tonic-gate 		if (trouble) {
2170Sstevel@tonic-gate 			syslog(LOG_ERR,
2180Sstevel@tonic-gate 	"%s: found %d errors with network configuration files. Exiting.",
2190Sstevel@tonic-gate 				argv[0], trouble);
2200Sstevel@tonic-gate 			fprintf(stderr,
2210Sstevel@tonic-gate 	"%s: found %d errors with network configuration files. Exiting.\n",
2220Sstevel@tonic-gate 				argv[0], trouble);
2230Sstevel@tonic-gate 			exit(1);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	ipv6flag = Is_ipv6present();
2270Sstevel@tonic-gate 	rpcb_check_init();
2280Sstevel@tonic-gate 	while (nconf = getnetconfig(nc_handle)) {
2290Sstevel@tonic-gate 		if (nconf->nc_flag & NC_VISIBLE)
2300Sstevel@tonic-gate 			init_transport(nconf);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	endnetconfig(nc_handle);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if ((loopback_dg[0] == NULL) && (loopback_vc[0] == NULL) &&
2350Sstevel@tonic-gate 		(loopback_vc_ord[0] == NULL)) {
2360Sstevel@tonic-gate 		syslog(LOG_ERR, "could not find loopback transports");
2370Sstevel@tonic-gate 		exit(1);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* catch the usual termination signals for graceful exit */
2410Sstevel@tonic-gate 	(void) signal(SIGINT, terminate);
2420Sstevel@tonic-gate 	(void) signal(SIGTERM, terminate);
2430Sstevel@tonic-gate 	(void) signal(SIGQUIT, terminate);
2440Sstevel@tonic-gate 	/* ignore others that could get sent */
245*2128Sjjj 	(void) sigset(SIGHUP, note_refresh);
2460Sstevel@tonic-gate 	(void) signal(SIGUSR1, SIG_IGN);
2470Sstevel@tonic-gate 	(void) signal(SIGUSR2, SIG_IGN);
2480Sstevel@tonic-gate 	if (warmstart) {
2490Sstevel@tonic-gate 		read_warmstart();
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	if (debugging) {
2520Sstevel@tonic-gate 		printf("rpcbind debugging enabled.");
2530Sstevel@tonic-gate 		if (doabort) {
2540Sstevel@tonic-gate 			printf("  Will abort on errors!\n");
2550Sstevel@tonic-gate 		} else {
2560Sstevel@tonic-gate 			printf("\n");
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	} else {
2590Sstevel@tonic-gate 		detachfromtty();
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* These are basic privileges we do not need */
2630Sstevel@tonic-gate 	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
2640Sstevel@tonic-gate 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	my_svc_run();
2670Sstevel@tonic-gate 	syslog(LOG_ERR, "svc_run returned unexpectedly");
2680Sstevel@tonic-gate 	rpcbind_abort();
2690Sstevel@tonic-gate 	/* NOTREACHED */
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * Increments a counter each time a problem is found with the network
2740Sstevel@tonic-gate  * configuration information.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate static int
check_netconfig(void)2770Sstevel@tonic-gate check_netconfig(void)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	void	*nc;
2800Sstevel@tonic-gate 	void	*dlcookie;
2810Sstevel@tonic-gate 	int	busted = 0;
2820Sstevel@tonic-gate 	int	i;
2830Sstevel@tonic-gate 	int	lo_clts_found = 0, lo_cots_found = 0, lo_cotsord_found = 0;
2840Sstevel@tonic-gate 	struct netconfig	*nconf, *np;
2850Sstevel@tonic-gate 	struct stat	sb;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	nc = setnetconfig();
2880Sstevel@tonic-gate 	if (nc == NULL) {
2890Sstevel@tonic-gate 		if (debugging)
2900Sstevel@tonic-gate 			fprintf(stderr,
2910Sstevel@tonic-gate 				"setnetconfig() failed:  %s\n", nc_sperror());
2920Sstevel@tonic-gate 		syslog(LOG_ALERT, "setnetconfig() failed:  %s", nc_sperror());
2930Sstevel@tonic-gate 		return (1);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 	while (np = getnetconfig(nc)) {
2960Sstevel@tonic-gate 		if ((np->nc_flag & NC_VISIBLE) == 0)
2970Sstevel@tonic-gate 			continue;
2980Sstevel@tonic-gate 		if (debugging)
2990Sstevel@tonic-gate 			fprintf(stderr, "checking netid \"%s\"\n",
3000Sstevel@tonic-gate 				np->nc_netid);
3010Sstevel@tonic-gate 		if (strcmp(np->nc_protofmly, NC_LOOPBACK) == 0)
3020Sstevel@tonic-gate 			switch (np->nc_semantics) {
3030Sstevel@tonic-gate 			case NC_TPI_CLTS:
3040Sstevel@tonic-gate 				lo_clts_found = 1;
3050Sstevel@tonic-gate 				break;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 			case NC_TPI_COTS:
3080Sstevel@tonic-gate 				lo_cots_found = 1;
3090Sstevel@tonic-gate 				break;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 			case NC_TPI_COTS_ORD:
3120Sstevel@tonic-gate 				lo_cotsord_found = 1;
3130Sstevel@tonic-gate 				break;
3140Sstevel@tonic-gate 			}
3150Sstevel@tonic-gate 		if (stat(np->nc_device, &sb) == -1 && errno == ENOENT) {
3160Sstevel@tonic-gate 			if (debugging)
3170Sstevel@tonic-gate 				fprintf(stderr, "\tdevice %s does not exist\n",
3180Sstevel@tonic-gate 					np->nc_device);
3190Sstevel@tonic-gate 			syslog(LOG_ERR, "netid %s:  device %s does not exist",
3200Sstevel@tonic-gate 				np->nc_netid, np->nc_device);
3210Sstevel@tonic-gate 			busted++;
3220Sstevel@tonic-gate 		} else
3230Sstevel@tonic-gate 			if (debugging)
3240Sstevel@tonic-gate 				fprintf(stderr, "\tdevice %s present\n",
3250Sstevel@tonic-gate 					np->nc_device);
3260Sstevel@tonic-gate 		for (i = 0; i < np->nc_nlookups; i++) {
3270Sstevel@tonic-gate 			char	*libname = np->nc_lookups[i];
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 			if ((dlcookie = dlopen(libname, RTLD_LAZY)) == NULL) {
3300Sstevel@tonic-gate 				char *dlerrstr;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 				dlerrstr = dlerror();
3330Sstevel@tonic-gate 				if (debugging) {
3340Sstevel@tonic-gate 					fprintf(stderr,
3350Sstevel@tonic-gate 	"\tnetid %s: dlopen of name-to-address library %s failed\ndlerror: %s",
3360Sstevel@tonic-gate 	np->nc_netid, libname, dlerrstr ? dlerrstr : "");
3370Sstevel@tonic-gate 				}
3380Sstevel@tonic-gate 				syslog(LOG_ERR,
3390Sstevel@tonic-gate 	"netid %s:  dlopen of name-to-address library %s failed",
3400Sstevel@tonic-gate 						np->nc_netid, libname);
3410Sstevel@tonic-gate 				if (dlerrstr)
3420Sstevel@tonic-gate 					syslog(LOG_ERR, "%s", dlerrstr);
3430Sstevel@tonic-gate 				busted++;
3440Sstevel@tonic-gate 			} else {
3450Sstevel@tonic-gate 				if (debugging)
3460Sstevel@tonic-gate 					fprintf(stderr,
3470Sstevel@tonic-gate 	"\tdlopen of name-to-address library %s succeeded\n", libname);
3480Sstevel@tonic-gate 				(void) dlclose(dlcookie);
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 		nconf = getnetconfigent(np->nc_netid);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, ""))
3540Sstevel@tonic-gate 			busted++;
3550Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, ""))
3560Sstevel@tonic-gate 			busted++;
3570Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, "rpcbind"))
3580Sstevel@tonic-gate 			busted++;
3590Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, "rpcbind"))
3600Sstevel@tonic-gate 			busted++;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		freenetconfigent(nconf);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	endnetconfig(nc);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (lo_clts_found) {
3670Sstevel@tonic-gate 		if (debugging)
3680Sstevel@tonic-gate 			fprintf(stderr, "Found CLTS loopback transport\n");
3690Sstevel@tonic-gate 	} else {
3700Sstevel@tonic-gate 		syslog(LOG_ALERT, "no CLTS loopback transport found\n");
3710Sstevel@tonic-gate 		if (debugging)
3720Sstevel@tonic-gate 			fprintf(stderr, "no CLTS loopback transport found\n");
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	if (lo_cots_found) {
3750Sstevel@tonic-gate 		if (debugging)
3760Sstevel@tonic-gate 			fprintf(stderr, "Found COTS loopback transport\n");
3770Sstevel@tonic-gate 	} else {
3780Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS loopback transport found\n");
3790Sstevel@tonic-gate 		if (debugging)
3800Sstevel@tonic-gate 			fprintf(stderr, "no COTS loopback transport found\n");
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 	if (lo_cotsord_found) {
3830Sstevel@tonic-gate 		if (debugging)
3840Sstevel@tonic-gate 			fprintf(stderr, "Found COTS ORD loopback transport\n");
3850Sstevel@tonic-gate 	} else {
3860Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS ORD loopback transport found\n");
3870Sstevel@tonic-gate 		if (debugging)
3880Sstevel@tonic-gate 			fprintf(stderr,
3890Sstevel@tonic-gate 				"no COTS ORD loopback transport found\n");
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (busted);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * Adds the entry into the rpcbind database.
3970Sstevel@tonic-gate  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
3980Sstevel@tonic-gate  * Returns 0 if succeeds, else fails
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate static int
init_transport(struct netconfig * nconf)4010Sstevel@tonic-gate init_transport(struct netconfig *nconf)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	int fd;
4040Sstevel@tonic-gate 	struct t_bind *taddr, *baddr;
4050Sstevel@tonic-gate 	SVCXPRT	*my_xprt;
4060Sstevel@tonic-gate 	struct nd_addrlist *nas;
4070Sstevel@tonic-gate 	struct nd_hostserv hs;
4080Sstevel@tonic-gate 	int status;	/* bound checking ? */
4090Sstevel@tonic-gate 	static int msgprt = 0;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
4120Sstevel@tonic-gate 		(nconf->nc_semantics != NC_TPI_COTS) &&
4130Sstevel@tonic-gate 		(nconf->nc_semantics != NC_TPI_COTS_ORD))
4140Sstevel@tonic-gate 		return (1);	/* not my type */
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) && !ipv6flag) {
4170Sstevel@tonic-gate 		if (!msgprt)
4180Sstevel@tonic-gate 			syslog(LOG_DEBUG,
4190Sstevel@tonic-gate "/etc/netconfig has IPv6 entries but IPv6 is not configured");
4200Sstevel@tonic-gate 		msgprt++;
4210Sstevel@tonic-gate 		return (1);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate #ifdef ND_DEBUG
4240Sstevel@tonic-gate 	{
4250Sstevel@tonic-gate 	int i;
4260Sstevel@tonic-gate 	char **s;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %d lookup routines :\n",
4290Sstevel@tonic-gate 		nconf->nc_netid, nconf->nc_nlookups);
4300Sstevel@tonic-gate 	for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups; i++, s++)
4310Sstevel@tonic-gate 		fprintf(stderr, "[%d] - %s\n", i, *s);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate #endif
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
4360Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot open connection: %s",
4370Sstevel@tonic-gate 				nconf->nc_netid, t_errlist[t_errno]);
4380Sstevel@tonic-gate 		return (1);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4411676Sjpk 	if (is_system_labeled() &&
4421676Sjpk 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
4431676Sjpk 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
4441676Sjpk 	    setopt_anon_mlp(fd) == -1) {
4451676Sjpk 		syslog(LOG_ERR, "%s: couldn't set SO_ANON_MLP option",
4461676Sjpk 		    nconf->nc_netid);
4471676Sjpk 	}
4481676Sjpk 
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * Negotiate for returning the ucred of the caller. This should
4510Sstevel@tonic-gate 	 * done before enabling the endpoint for service via
4520Sstevel@tonic-gate 	 * t_bind() so that requests to rpcbind contain the uid.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	svc_fd_negotiate_ucred(fd);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	taddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4570Sstevel@tonic-gate 	baddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4580Sstevel@tonic-gate 	if ((baddr == NULL) || (taddr == NULL)) {
4590Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot allocate netbuf: %s",
4600Sstevel@tonic-gate 				nconf->nc_netid, t_errlist[t_errno]);
4610Sstevel@tonic-gate 		exit(1);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/* Get rpcbind's address on this transport */
4650Sstevel@tonic-gate 	hs.h_host = HOST_SELF;
4660Sstevel@tonic-gate 	hs.h_serv = servname;
4670Sstevel@tonic-gate 	if (netdir_getbyname(nconf, &hs, &nas))
4680Sstevel@tonic-gate 		goto error;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* Copy the address */
4710Sstevel@tonic-gate 	taddr->addr.len = nas->n_addrs->len;
4720Sstevel@tonic-gate 	memcpy(taddr->addr.buf, nas->n_addrs->buf, (int)nas->n_addrs->len);
4730Sstevel@tonic-gate #ifdef ND_DEBUG
4740Sstevel@tonic-gate 	{
4750Sstevel@tonic-gate 	/* for debugging print out our universal address */
4760Sstevel@tonic-gate 	char *uaddr;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	uaddr = taddr2uaddr(nconf, nas->n_addrs);
4790Sstevel@tonic-gate 	(void) fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
4800Sstevel@tonic-gate 	(void) free(uaddr);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate #endif
4830Sstevel@tonic-gate 	netdir_free((char *)nas, ND_ADDRLIST);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	if (nconf->nc_semantics == NC_TPI_CLTS)
4860Sstevel@tonic-gate 		taddr->qlen = 0;
4870Sstevel@tonic-gate 	else
4880Sstevel@tonic-gate 		taddr->qlen = listen_backlog;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
4910Sstevel@tonic-gate 		/*
4920Sstevel@tonic-gate 		 * Sm: If we are running then set SO_REUSEADDR option
4930Sstevel@tonic-gate 		 * so that we can bind to our preferred address even if
4940Sstevel@tonic-gate 		 * previous connections are in FIN_WAIT state
4950Sstevel@tonic-gate 		 */
4960Sstevel@tonic-gate #ifdef ND_DEBUG
4970Sstevel@tonic-gate 		fprintf(stdout, "Setting SO_REUSEADDR.\n");
4980Sstevel@tonic-gate #endif
4990Sstevel@tonic-gate 		if (setopt_reuseaddr(fd) == -1) {
5000Sstevel@tonic-gate 			syslog(LOG_ERR, "Couldn't set SO_REUSEADDR option");
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if (t_bind(fd, taddr, baddr) != 0) {
5050Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot bind: %s",
5060Sstevel@tonic-gate 			nconf->nc_netid, t_errlist[t_errno]);
5070Sstevel@tonic-gate 		goto error;
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (memcmp(taddr->addr.buf, baddr->addr.buf, (int)baddr->addr.len)) {
5120Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: address in use", nconf->nc_netid);
5130Sstevel@tonic-gate 		goto error;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, baddr, 0, 0);
5170Sstevel@tonic-gate 	if (my_xprt == (SVCXPRT *)NULL) {
5180Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: could not create service",
5190Sstevel@tonic-gate 				nconf->nc_netid);
5200Sstevel@tonic-gate 		goto error;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/* set up multicast address for RPC CALL_IT, IPv6 */
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
5260Sstevel@tonic-gate 	    (strcmp(nconf->nc_proto, NC_UDP) == 0)) {
5270Sstevel@tonic-gate 		if (setup_callit(fd) < 0) {
5280Sstevel@tonic-gate 			syslog(LOG_ERR, "Unable to join IPv6 multicast group \
5290Sstevel@tonic-gate for rpc broadcast %s", RPCB_MULTICAST_ADDR);
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5340Sstevel@tonic-gate 			svc_control(my_xprt, SVCSET_KEEPALIVE, (void *) TRUE);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate #ifdef PORTMAP
5380Sstevel@tonic-gate 	/*
5390Sstevel@tonic-gate 	 * Register both the versions for tcp/ip and udp/ip
5400Sstevel@tonic-gate 	 */
5410Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
5420Sstevel@tonic-gate 		((strcmp(nconf->nc_proto, NC_TCP) == 0) ||
5430Sstevel@tonic-gate 		(strcmp(nconf->nc_proto, NC_UDP) == 0))) {
5440Sstevel@tonic-gate 		PMAPLIST *pml;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
5470Sstevel@tonic-gate 			pmap_service, NULL)) {
5480Sstevel@tonic-gate 			syslog(LOG_ERR, "could not register on %s",
5490Sstevel@tonic-gate 					nconf->nc_netid);
5500Sstevel@tonic-gate 			goto error;
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5530Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
5540Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
5550Sstevel@tonic-gate 			exit(1);
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		pml->pml_map.pm_prog = PMAPPROG;
5580Sstevel@tonic-gate 		pml->pml_map.pm_vers = PMAPVERS;
5590Sstevel@tonic-gate 		pml->pml_map.pm_port = PMAPPORT;
5600Sstevel@tonic-gate 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5610Sstevel@tonic-gate 			if (tcptrans[0]) {
5620Sstevel@tonic-gate 				syslog(LOG_ERR,
5630Sstevel@tonic-gate 				"cannot have more than one TCP transport");
5640Sstevel@tonic-gate 				goto error;
5650Sstevel@tonic-gate 			}
5660Sstevel@tonic-gate 			tcptrans = strdup(nconf->nc_netid);
5670Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_TCP;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 			/* Let's snarf the universal address */
5700Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
5710Sstevel@tonic-gate 			tcp_uaddr = taddr2uaddr(nconf, &baddr->addr);
5720Sstevel@tonic-gate 		} else {
5730Sstevel@tonic-gate 			if (udptrans[0]) {
5740Sstevel@tonic-gate 				syslog(LOG_ERR,
5750Sstevel@tonic-gate 				"cannot have more than one UDP transport");
5760Sstevel@tonic-gate 				goto error;
5770Sstevel@tonic-gate 			}
5780Sstevel@tonic-gate 			udptrans = strdup(nconf->nc_netid);
5790Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_UDP;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 			/* Let's snarf the universal address */
5820Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
5830Sstevel@tonic-gate 			udp_uaddr = taddr2uaddr(nconf, &baddr->addr);
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 		pml->pml_next = list_pml;
5860Sstevel@tonic-gate 		list_pml = pml;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 		/* Add version 3 information */
5890Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5900Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
5910Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
5920Sstevel@tonic-gate 			exit(1);
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
5950Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS;
5960Sstevel@tonic-gate 		pml->pml_next = list_pml;
5970Sstevel@tonic-gate 		list_pml = pml;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		/* Add version 4 information */
6000Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
6010Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
6020Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
6030Sstevel@tonic-gate 			exit(1);
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
6060Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS4;
6070Sstevel@tonic-gate 		pml->pml_next = list_pml;
6080Sstevel@tonic-gate 		list_pml = pml;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 		/* Also add version 2 stuff to rpcbind list */
6110Sstevel@tonic-gate 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &baddr->addr);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate #endif
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* version 3 registration */
6160Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
6170Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 3",
6180Sstevel@tonic-gate 				nconf->nc_netid);
6190Sstevel@tonic-gate 		goto error;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &baddr->addr);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/* version 4 registration */
6240Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
6250Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 4",
6260Sstevel@tonic-gate 				nconf->nc_netid);
6270Sstevel@tonic-gate 		goto error;
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &baddr->addr);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
6320Sstevel@tonic-gate 		if (nconf->nc_semantics == NC_TPI_CLTS)
6330Sstevel@tonic-gate 			loopback_dg = strdup(nconf->nc_netid);
6340Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS)
6350Sstevel@tonic-gate 			loopback_vc = strdup(nconf->nc_netid);
6360Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS_ORD)
6370Sstevel@tonic-gate 			loopback_vc_ord = strdup(nconf->nc_netid);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	/* decide if bound checking works for this transport */
6410Sstevel@tonic-gate 	status = add_bndlist(nconf, taddr, baddr);
6420Sstevel@tonic-gate #ifdef BIND_DEBUG
6430Sstevel@tonic-gate 	if (status < 0) {
6440Sstevel@tonic-gate 		fprintf(stderr, "Error in finding bind status for %s\n",
6450Sstevel@tonic-gate 			nconf->nc_netid);
6460Sstevel@tonic-gate 	} else if (status == 0) {
6470Sstevel@tonic-gate 		fprintf(stderr, "check binding for %s\n",
6480Sstevel@tonic-gate 			nconf->nc_netid);
6490Sstevel@tonic-gate 	} else if (status > 0) {
6500Sstevel@tonic-gate 		fprintf(stderr, "No check binding for %s\n",
6510Sstevel@tonic-gate 			nconf->nc_netid);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate #endif
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * rmtcall only supported on CLTS transports for now.
6560Sstevel@tonic-gate 	 * only needed when we are allowing indirect calls
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	if (allow_indirect && nconf->nc_semantics == NC_TPI_CLTS) {
6590Sstevel@tonic-gate 		status = create_rmtcall_fd(nconf);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate #ifdef BIND_DEBUG
6620Sstevel@tonic-gate 		if (status < 0) {
6630Sstevel@tonic-gate 			fprintf(stderr, "Could not create rmtcall fd for %s\n",
6640Sstevel@tonic-gate 				nconf->nc_netid);
6650Sstevel@tonic-gate 		} else {
6660Sstevel@tonic-gate 			fprintf(stderr, "rmtcall fd for %s is %d\n",
6670Sstevel@tonic-gate 				nconf->nc_netid, status);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate #endif
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
6720Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
6730Sstevel@tonic-gate 	return (0);
6740Sstevel@tonic-gate error:
6750Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
6760Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
6770Sstevel@tonic-gate 	(void) t_close(fd);
6780Sstevel@tonic-gate 	return (1);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate static void
rbllist_add(ulong_t prog,ulong_t vers,struct netconfig * nconf,struct netbuf * addr)6820Sstevel@tonic-gate rbllist_add(ulong_t prog, ulong_t vers, struct netconfig *nconf,
6830Sstevel@tonic-gate 	struct netbuf *addr)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	rpcblist_ptr rbl;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	rbl = (rpcblist_ptr)malloc((uint_t)sizeof (rpcblist));
6880Sstevel@tonic-gate 	if (rbl == (rpcblist_ptr)NULL) {
6890Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory!");
6900Sstevel@tonic-gate 		exit(1);
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	rbl->rpcb_map.r_prog = prog;
6940Sstevel@tonic-gate 	rbl->rpcb_map.r_vers = vers;
6950Sstevel@tonic-gate 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
6960Sstevel@tonic-gate 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
6970Sstevel@tonic-gate 	rbl->rpcb_map.r_owner = strdup(superuser);
6980Sstevel@tonic-gate 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
6990Sstevel@tonic-gate 	list_rbl = rbl;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate  * Catch the signal and die
7040Sstevel@tonic-gate  */
7050Sstevel@tonic-gate /* ARGSUSED */
7060Sstevel@tonic-gate static void
terminate(int sig)7070Sstevel@tonic-gate terminate(int sig)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	syslog(LOG_ERR, "rpcbind terminating on signal.");
7100Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7110Sstevel@tonic-gate 	exit(2);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
714*2128Sjjj /* ARGSUSED */
715*2128Sjjj static void
note_refresh(int sig)716*2128Sjjj note_refresh(int sig)
717*2128Sjjj {
718*2128Sjjj 	sigrefresh = 1;
719*2128Sjjj }
720*2128Sjjj 
7210Sstevel@tonic-gate void
rpcbind_abort(void)7220Sstevel@tonic-gate rpcbind_abort(void)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7250Sstevel@tonic-gate 	abort();
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate /*
7290Sstevel@tonic-gate  * detach from tty
7300Sstevel@tonic-gate  */
7310Sstevel@tonic-gate static void
detachfromtty(void)7320Sstevel@tonic-gate detachfromtty(void)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	close(0);
7350Sstevel@tonic-gate 	close(1);
7360Sstevel@tonic-gate 	close(2);
7370Sstevel@tonic-gate 	switch (forkall()) {
7380Sstevel@tonic-gate 	case (pid_t)-1:
7390Sstevel@tonic-gate 		perror("fork");
7400Sstevel@tonic-gate 		break;
7410Sstevel@tonic-gate 	case 0:
7420Sstevel@tonic-gate 		break;
7430Sstevel@tonic-gate 	default:
7440Sstevel@tonic-gate 		exit(0);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 	setsid();
7470Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
7480Sstevel@tonic-gate 	dup(0);
7490Sstevel@tonic-gate 	dup(0);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate /* get command line options */
7530Sstevel@tonic-gate static void
parseargs(int argc,char * argv[])7540Sstevel@tonic-gate parseargs(int argc, char *argv[])
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	int c;
7570Sstevel@tonic-gate 	int tmp;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dwal:")) != EOF) {
7600Sstevel@tonic-gate 		switch (c) {
7610Sstevel@tonic-gate 		case 'd':
7620Sstevel@tonic-gate 			debugging = 1;
7630Sstevel@tonic-gate 			break;
7640Sstevel@tonic-gate 		case 'a':
7650Sstevel@tonic-gate 			doabort = 1;	/* when debugging, do an abort on */
7660Sstevel@tonic-gate 			break;		/* errors; for rpcbind developers */
7670Sstevel@tonic-gate 					/* only! */
7680Sstevel@tonic-gate 		case 'w':
7690Sstevel@tonic-gate 			warmstart = 1;
7700Sstevel@tonic-gate 			break;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		case 'l':
7730Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &tmp)) {
7740Sstevel@tonic-gate 				if (tmp > listen_backlog) {
7750Sstevel@tonic-gate 					listen_backlog = tmp;
7760Sstevel@tonic-gate 				}
7770Sstevel@tonic-gate 			}
7780Sstevel@tonic-gate 			break;
7790Sstevel@tonic-gate 		default:	/* error */
7800Sstevel@tonic-gate 			fprintf(stderr,
7810Sstevel@tonic-gate 			"usage: rpcbind [-d] [-w] [-l listen_backlog]\n");
7820Sstevel@tonic-gate 			exit(1);
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 	if (doabort && !debugging) {
7860Sstevel@tonic-gate 	    fprintf(stderr,
7870Sstevel@tonic-gate 		"-a (abort) specified without -d (debugging) -- ignored.\n");
7880Sstevel@tonic-gate 	    doabort = 0;
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate static int
setopt_int(int fd,int level,int name,int value)7931676Sjpk setopt_int(int fd, int level, int name, int value)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	struct t_optmgmt req, resp;
7961676Sjpk 	struct {
7971676Sjpk 		struct opthdr opt;
7981676Sjpk 		int value;
7991676Sjpk 	} optdata;
8000Sstevel@tonic-gate 
8011676Sjpk 	optdata.opt.level = level;
8021676Sjpk 	optdata.opt.name = name;
8031676Sjpk 	optdata.opt.len = sizeof (int);
8040Sstevel@tonic-gate 
8051676Sjpk 	optdata.value = value;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
8081676Sjpk 	req.opt.len = sizeof (optdata);
8091676Sjpk 	req.opt.buf = (char *)&optdata;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	resp.flags = 0;
8121676Sjpk 	resp.opt.buf = (char *)&optdata;
8131676Sjpk 	resp.opt.maxlen = sizeof (optdata);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
8160Sstevel@tonic-gate 		t_error("t_optmgmt");
8170Sstevel@tonic-gate 		return (-1);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	return (0);
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate static int
setopt_reuseaddr(int fd)8231676Sjpk setopt_reuseaddr(int fd)
8241676Sjpk {
8251676Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_REUSEADDR, 1));
8261676Sjpk }
8271676Sjpk 
8281676Sjpk static int
setopt_anon_mlp(int fd)8291676Sjpk setopt_anon_mlp(int fd)
8301676Sjpk {
8311676Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_ANON_MLP, 1));
8321676Sjpk }
8331676Sjpk 
8341676Sjpk static int
setup_callit(int fd)8350Sstevel@tonic-gate setup_callit(int fd)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate 	struct ipv6_mreq mreq;
8380Sstevel@tonic-gate 	struct t_optmgmt req, resp;
8390Sstevel@tonic-gate 	struct opthdr *opt;
8400Sstevel@tonic-gate 	char reqbuf[ sizeof (struct ipv6_mreq) + 24];
8410Sstevel@tonic-gate 	struct ipv6_mreq *pmreq;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	opt = (struct opthdr *)reqbuf;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	opt->level = IPPROTO_IPV6;
8460Sstevel@tonic-gate 	opt->name = IPV6_ADD_MEMBERSHIP;
8470Sstevel@tonic-gate 	opt->len = sizeof (struct ipv6_mreq);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/* multicast address */
8500Sstevel@tonic-gate 	(void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR,
8510Sstevel@tonic-gate 		mreq.ipv6mr_multiaddr.s6_addr);
8520Sstevel@tonic-gate 	mreq.ipv6mr_interface = 0;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	/* insert it into opt */
8550Sstevel@tonic-gate 	pmreq = (struct ipv6_mreq *)&reqbuf[sizeof (struct opthdr)];
8560Sstevel@tonic-gate 	memcpy(pmreq, &mreq, sizeof (struct ipv6_mreq));
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
8590Sstevel@tonic-gate 	req.opt.len = sizeof (struct opthdr) + opt->len;
8600Sstevel@tonic-gate 	req.opt.buf = (char *)opt;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	resp.flags = 0;
8630Sstevel@tonic-gate 	resp.opt.buf = reqbuf;
8640Sstevel@tonic-gate 	resp.opt.maxlen = sizeof (reqbuf);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
8670Sstevel@tonic-gate 		t_error("t_optmgmt");
8680Sstevel@tonic-gate 		return (-1);
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 	return (0);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static boolean_t
check_hostserv(struct netconfig * nconf,const char * host,const char * serv)8740Sstevel@tonic-gate check_hostserv(struct netconfig *nconf, const char *host, const char *serv)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	struct nd_hostserv nh;
8770Sstevel@tonic-gate 	struct nd_addrlist *na;
8780Sstevel@tonic-gate 	const char *hostname = host;
8790Sstevel@tonic-gate 	const char *servname = serv;
8800Sstevel@tonic-gate 	int retval;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	if (strcmp(host, HOST_SELF) == 0)
8830Sstevel@tonic-gate 		hostname = "HOST_SELF";
8840Sstevel@tonic-gate 	else if (strcmp(host, HOST_SELF_CONNECT) == 0)
8850Sstevel@tonic-gate 		hostname = "HOST_SELF_CONNECT";
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if (serv[0] == '\0')
8880Sstevel@tonic-gate 		servname = "<any>";
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	nh.h_host = (char *)host;
8910Sstevel@tonic-gate 	nh.h_serv = (char *)serv;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	retval = netdir_getbyname(nconf, &nh, &na);
8940Sstevel@tonic-gate 	if (retval != ND_OK || na->n_cnt == 0) {
8950Sstevel@tonic-gate 		if (retval == ND_OK)
8960Sstevel@tonic-gate 			netdir_free(na, ND_ADDRLIST);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 		syslog(LOG_ALERT, "netid %s: cannot find an address for host "
8990Sstevel@tonic-gate 		    "%s, service \"%s\"", nconf->nc_netid, hostname, servname);
9000Sstevel@tonic-gate 		if (debugging) {
9010Sstevel@tonic-gate 			(void) fprintf(stderr, "\tnetdir_getbyname for %s, "
9020Sstevel@tonic-gate 			    "service \"%s\" failed\n", hostname, servname);
9030Sstevel@tonic-gate 		}
9040Sstevel@tonic-gate 		return (B_FALSE);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	netdir_free(na, ND_ADDRLIST);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	if (debugging) {
9090Sstevel@tonic-gate 		(void) fprintf(stderr, "\tnetdir_getbyname for %s, service "
9100Sstevel@tonic-gate 		    "service \"%s\" succeeded\n", hostname, servname);
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 	return (B_TRUE);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate /* Maximum outstanding syslog requests */
9160Sstevel@tonic-gate #define	MAXLOG		100
9170Sstevel@tonic-gate /* Maximum length: the messages generated are fairly short; no hostnames. */
9180Sstevel@tonic-gate #define	MAXMSG		128
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate typedef struct logmsg {
9210Sstevel@tonic-gate 	struct logmsg	*log_next;
9220Sstevel@tonic-gate 	int		log_pri;
9230Sstevel@tonic-gate 	char		log_msg[MAXMSG];
9240Sstevel@tonic-gate } logmsg;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate static logmsg *loghead = NULL;
9270Sstevel@tonic-gate static logmsg **logtailp = &loghead;
9280Sstevel@tonic-gate static mutex_t logmutex = DEFAULTMUTEX;
9290Sstevel@tonic-gate static cond_t logcond = DEFAULTCV;
9300Sstevel@tonic-gate static int logcount = 0;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*ARGSUSED*/
9330Sstevel@tonic-gate static void *
logthread(void * arg)9340Sstevel@tonic-gate logthread(void *arg)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	while (1) {
9370Sstevel@tonic-gate 		logmsg *msg;
9380Sstevel@tonic-gate 		(void) mutex_lock(&logmutex);
9390Sstevel@tonic-gate 		while ((msg = loghead) == NULL)
9400Sstevel@tonic-gate 			(void) cond_wait(&logcond, &logmutex);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		loghead = msg->log_next;
9430Sstevel@tonic-gate 		logcount--;
9440Sstevel@tonic-gate 		if (loghead == NULL) {
9450Sstevel@tonic-gate 			logtailp = &loghead;
9460Sstevel@tonic-gate 			logcount = 0;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 		(void) mutex_unlock(&logmutex);
9490Sstevel@tonic-gate 		syslog(msg->log_pri, "%s", msg->log_msg);
9500Sstevel@tonic-gate 		free(msg);
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	/* NOTREACHED */
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9552104Sjjj static boolean_t
get_smf_prop(const char * var,boolean_t def_val)9562104Sjjj get_smf_prop(const char *var, boolean_t def_val)
9572104Sjjj {
9582104Sjjj 	scf_simple_prop_t *prop;
9592104Sjjj 	uint8_t *val;
9602104Sjjj 	boolean_t res = def_val;
9612104Sjjj 
9622104Sjjj 	prop = scf_simple_prop_get(NULL, NULL, "config", var);
9632104Sjjj 	if (prop) {
9642104Sjjj 		if ((val = scf_simple_prop_next_boolean(prop)) != NULL)
9652104Sjjj 			res = (*val == 0) ? B_FALSE : B_TRUE;
9662104Sjjj 		scf_simple_prop_free(prop);
9672104Sjjj 	}
9682104Sjjj 
9692104Sjjj 	if (prop == NULL || val == NULL) {
9702104Sjjj 		syslog(LOG_ALERT, "no value for config/%s (%s). "
9712104Sjjj 		    "Using default \"%s\"", var, scf_strerror(scf_error()),
9722104Sjjj 		    def_val ? "true" : "false");
9732104Sjjj 	}
9742104Sjjj 
9752104Sjjj 	return (res);
9762104Sjjj }
9772104Sjjj 
9780Sstevel@tonic-gate /*
979*2128Sjjj  * Initialize: read the configuration parameters from SMF.
980*2128Sjjj  * This function must be idempotent because it can be called from the
981*2128Sjjj  * main poll() loop in my_svc_run().
9820Sstevel@tonic-gate  */
983*2128Sjjj void
rpcb_check_init(void)9840Sstevel@tonic-gate rpcb_check_init(void)
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate 	thread_t tid;
987*2128Sjjj 	static int thr_running;
9880Sstevel@tonic-gate 
9892104Sjjj 	wrap_enabled = get_smf_prop("enable_tcpwrappers", B_FALSE);
9902104Sjjj 	verboselog = get_smf_prop("verbose_logging", B_FALSE);
9912104Sjjj 	allow_indirect = get_smf_prop("allow_indirect", B_TRUE);
9922104Sjjj 	local_only = get_smf_prop("local_only", B_FALSE);
9930Sstevel@tonic-gate 
994*2128Sjjj 	if (wrap_enabled && !thr_running) {
9950Sstevel@tonic-gate 		(void) thr_create(NULL, 0, logthread, NULL, THR_DETACHED, &tid);
996*2128Sjjj 		thr_running = 1;
997*2128Sjjj 	}
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate  * qsyslog() - queue a request for syslog(); if syslog blocks, the other
10020Sstevel@tonic-gate  * thread blocks; we make sure we don't run out of memory by allowing
10030Sstevel@tonic-gate  * only a limited number of outstandig syslog() requests.
10040Sstevel@tonic-gate  */
10050Sstevel@tonic-gate void
qsyslog(int pri,const char * fmt,...)10060Sstevel@tonic-gate qsyslog(int pri, const char *fmt, ...)
10070Sstevel@tonic-gate {
10080Sstevel@tonic-gate 	logmsg *msg = malloc(sizeof (*msg));
10090Sstevel@tonic-gate 	int oldcount;
10100Sstevel@tonic-gate 	va_list ap;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	if (msg == NULL)
10130Sstevel@tonic-gate 		return;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	msg->log_pri = pri;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	va_start(ap, fmt);
10180Sstevel@tonic-gate 	(void) vsnprintf(msg->log_msg, sizeof (msg->log_msg), fmt, ap);
10190Sstevel@tonic-gate 	va_end(ap);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	msg->log_next = NULL;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	(void) mutex_lock(&logmutex);
10240Sstevel@tonic-gate 	oldcount = logcount;
10250Sstevel@tonic-gate 	if (logcount < MAXLOG) {
10260Sstevel@tonic-gate 		logcount++;
10270Sstevel@tonic-gate 		*logtailp = msg;
10280Sstevel@tonic-gate 		logtailp = &msg->log_next;
10290Sstevel@tonic-gate 	} else {
10300Sstevel@tonic-gate 		free(msg);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 	(void) mutex_unlock(&logmutex);
10330Sstevel@tonic-gate 	if (oldcount == 0)
10340Sstevel@tonic-gate 		(void) cond_signal(&logcond);
10350Sstevel@tonic-gate }
1036