xref: /titanic_54/usr/src/cmd/rpcbind/rpcbind.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /* All Rights Reserved */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
297c478bd9Sstevel@tonic-gate  * The Regents of the University of California
307c478bd9Sstevel@tonic-gate  * All Rights Reserved
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
337c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
347c478bd9Sstevel@tonic-gate  * contributors.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * rpcbind.c
417c478bd9Sstevel@tonic-gate  * Implements the program, version to address mapping for rpc.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <dlfcn.h>
467c478bd9Sstevel@tonic-gate #include <stdio.h>
47*004388ebScasper #include <stdio_ext.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
537c478bd9Sstevel@tonic-gate #include <netconfig.h>
547c478bd9Sstevel@tonic-gate #include <netdir.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
567c478bd9Sstevel@tonic-gate #include <sys/wait.h>
577c478bd9Sstevel@tonic-gate #include <signal.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <stdlib.h>
607c478bd9Sstevel@tonic-gate #include <thread.h>
617c478bd9Sstevel@tonic-gate #include <synch.h>
627c478bd9Sstevel@tonic-gate #include <deflt.h>
637c478bd9Sstevel@tonic-gate #include <stdarg.h>
647c478bd9Sstevel@tonic-gate #ifdef PORTMAP
657c478bd9Sstevel@tonic-gate #include <netinet/in.h>
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
687c478bd9Sstevel@tonic-gate #include <sys/termios.h>
697c478bd9Sstevel@tonic-gate #include "rpcbind.h"
707c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
717c478bd9Sstevel@tonic-gate #include <sys/stat.h>
727c478bd9Sstevel@tonic-gate #include <syslog.h>
737c478bd9Sstevel@tonic-gate #include <string.h>
747c478bd9Sstevel@tonic-gate #include <sys/time.h>
757c478bd9Sstevel@tonic-gate #include <sys/resource.h>
767c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
777c478bd9Sstevel@tonic-gate #include <priv_utils.h>
787c478bd9Sstevel@tonic-gate #include <libscf.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef PORTMAP
817c478bd9Sstevel@tonic-gate extern void pmap_service(struct svc_req *, SVCXPRT *xprt);
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate extern void rpcb_service_3(struct svc_req *, SVCXPRT *xprt);
847c478bd9Sstevel@tonic-gate extern void rpcb_service_4(struct svc_req *, SVCXPRT *xprt);
857c478bd9Sstevel@tonic-gate extern void read_warmstart(void);
867c478bd9Sstevel@tonic-gate extern void write_warmstart(void);
877c478bd9Sstevel@tonic-gate extern int Is_ipv6present(void);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	MAX_FILEDESC_LIMIT	1023
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static void terminate(int);
927c478bd9Sstevel@tonic-gate static void detachfromtty(void);
937c478bd9Sstevel@tonic-gate static void parseargs(int, char *[]);
947c478bd9Sstevel@tonic-gate static void rbllist_add(ulong_t, ulong_t, struct netconfig *, struct netbuf *);
957c478bd9Sstevel@tonic-gate static int init_transport(struct netconfig *);
967c478bd9Sstevel@tonic-gate static int check_netconfig(void);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static boolean_t check_hostserv(struct netconfig *, const char *, const char *);
997c478bd9Sstevel@tonic-gate static void rpcb_check_init(void);
10045916cd2Sjpk static int setopt_reuseaddr(int);
10145916cd2Sjpk static int setopt_anon_mlp(int);
10245916cd2Sjpk static int setup_callit(int);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* Global variables */
1057c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
1067c478bd9Sstevel@tonic-gate int debugging = 1;	/* Tell me what's going on */
1077c478bd9Sstevel@tonic-gate #else
1087c478bd9Sstevel@tonic-gate int debugging = 0;	/* Tell me what's going on */
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate static int ipv6flag = 0;
1117c478bd9Sstevel@tonic-gate int doabort = 0;	/* When debugging, do an abort on errors */
1127c478bd9Sstevel@tonic-gate static int listen_backlog = 64;
1137c478bd9Sstevel@tonic-gate rpcblist_ptr list_rbl;	/* A list of version 3/4 rpcbind services */
1147c478bd9Sstevel@tonic-gate char *loopback_dg;	/* Datagram loopback transport, for set and unset */
1157c478bd9Sstevel@tonic-gate char *loopback_vc;	/* COTS loopback transport, for set and unset */
1167c478bd9Sstevel@tonic-gate char *loopback_vc_ord;	/* COTS_ORD loopback transport, for set and unset */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate boolean_t verboselog = B_FALSE;
1197c478bd9Sstevel@tonic-gate boolean_t wrap_enabled = B_FALSE;
1207c478bd9Sstevel@tonic-gate boolean_t allow_indirect = B_TRUE;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* Local Variable */
1237c478bd9Sstevel@tonic-gate static int warmstart = 0;	/* Grab a old copy of registrations */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #ifdef PORTMAP
1267c478bd9Sstevel@tonic-gate PMAPLIST *list_pml;	/* A list of version 2 rpcbind services */
1277c478bd9Sstevel@tonic-gate char *udptrans;		/* Name of UDP transport */
1287c478bd9Sstevel@tonic-gate char *tcptrans;		/* Name of TCP transport */
1297c478bd9Sstevel@tonic-gate char *udp_uaddr;	/* Universal UDP address */
1307c478bd9Sstevel@tonic-gate char *tcp_uaddr;	/* Universal TCP address */
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate static char servname[] = "rpcbind";
1337c478bd9Sstevel@tonic-gate static char superuser[] = "superuser";
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static const char daemon_dir[] = DAEMON_DIR;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
1387c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1417c478bd9Sstevel@tonic-gate 	void *nc_handle;	/* Net config handle */
1427c478bd9Sstevel@tonic-gate 	struct rlimit rl;
1437c478bd9Sstevel@tonic-gate 	int maxrecsz = RPC_MAXDATASIZE;
14445916cd2Sjpk 	boolean_t can_do_mlp;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	parseargs(argc, argv);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	getrlimit(RLIMIT_NOFILE, &rl);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (rl.rlim_cur < MAX_FILEDESC_LIMIT) {
1517c478bd9Sstevel@tonic-gate 		if (rl.rlim_max <= MAX_FILEDESC_LIMIT)
1527c478bd9Sstevel@tonic-gate 			rl.rlim_cur = rl.rlim_max;
1537c478bd9Sstevel@tonic-gate 		else
1547c478bd9Sstevel@tonic-gate 			rl.rlim_cur = MAX_FILEDESC_LIMIT;
1557c478bd9Sstevel@tonic-gate 		setrlimit(RLIMIT_NOFILE, &rl);
1567c478bd9Sstevel@tonic-gate 	}
157*004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
158*004388ebScasper 
1597c478bd9Sstevel@tonic-gate 	openlog("rpcbind", LOG_CONS, LOG_DAEMON);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Create the daemon directory in /var/run
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	if (mkdir(daemon_dir, DAEMON_DIR_MODE) == 0 || errno == EEXIST) {
1657c478bd9Sstevel@tonic-gate 		chmod(daemon_dir, DAEMON_DIR_MODE);
1667c478bd9Sstevel@tonic-gate 		chown(daemon_dir, DAEMON_UID, DAEMON_GID);
1677c478bd9Sstevel@tonic-gate 	} else {
1687c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "failed to create \"%s\": %m", daemon_dir);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * These privileges are required for the t_bind check rpcbind uses
1737c478bd9Sstevel@tonic-gate 	 * to determine whether a service is still live or not.
1747c478bd9Sstevel@tonic-gate 	 */
17545916cd2Sjpk 	can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
1767c478bd9Sstevel@tonic-gate 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, DAEMON_UID,
17745916cd2Sjpk 	    DAEMON_GID, PRIV_NET_PRIVADDR, PRIV_SYS_NFS,
17845916cd2Sjpk 	    can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) {
1797c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Insufficient privileges\n");
1807c478bd9Sstevel@tonic-gate 		exit(1);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Enable non-blocking mode and maximum record size checks for
1857c478bd9Sstevel@tonic-gate 	 * connection oriented transports.
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
1887c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, "unable to set RPC max record size");
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	nc_handle = setnetconfig(); 	/* open netconfig file */
1927c478bd9Sstevel@tonic-gate 	if (nc_handle == NULL) {
1937c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not read /etc/netconfig");
1947c478bd9Sstevel@tonic-gate 		exit(1);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	loopback_dg = "";
1977c478bd9Sstevel@tonic-gate 	loopback_vc = "";
1987c478bd9Sstevel@tonic-gate 	loopback_vc_ord = "";
1997c478bd9Sstevel@tonic-gate #ifdef PORTMAP
2007c478bd9Sstevel@tonic-gate 	udptrans = "";
2017c478bd9Sstevel@tonic-gate 	tcptrans = "";
2027c478bd9Sstevel@tonic-gate #endif
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	{
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		 * rpcbind is the first application to encounter the
2077c478bd9Sstevel@tonic-gate 		 * various netconfig files.  check_netconfig() verifies
2087c478bd9Sstevel@tonic-gate 		 * that they are set up correctly and complains loudly
2097c478bd9Sstevel@tonic-gate 		 * if not.
2107c478bd9Sstevel@tonic-gate 		 */
2117c478bd9Sstevel@tonic-gate 		int trouble;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		trouble = check_netconfig();
2147c478bd9Sstevel@tonic-gate 		if (trouble) {
2157c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
2167c478bd9Sstevel@tonic-gate 	"%s: found %d errors with network configuration files. Exiting.",
2177c478bd9Sstevel@tonic-gate 				argv[0], trouble);
2187c478bd9Sstevel@tonic-gate 			fprintf(stderr,
2197c478bd9Sstevel@tonic-gate 	"%s: found %d errors with network configuration files. Exiting.\n",
2207c478bd9Sstevel@tonic-gate 				argv[0], trouble);
2217c478bd9Sstevel@tonic-gate 			exit(1);
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	ipv6flag = Is_ipv6present();
2257c478bd9Sstevel@tonic-gate 	rpcb_check_init();
2267c478bd9Sstevel@tonic-gate 	while (nconf = getnetconfig(nc_handle)) {
2277c478bd9Sstevel@tonic-gate 		if (nconf->nc_flag & NC_VISIBLE)
2287c478bd9Sstevel@tonic-gate 			init_transport(nconf);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	endnetconfig(nc_handle);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if ((loopback_dg[0] == NULL) && (loopback_vc[0] == NULL) &&
2337c478bd9Sstevel@tonic-gate 		(loopback_vc_ord[0] == NULL)) {
2347c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not find loopback transports");
2357c478bd9Sstevel@tonic-gate 		exit(1);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/* catch the usual termination signals for graceful exit */
2397c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, terminate);
2407c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, terminate);
2417c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, terminate);
2427c478bd9Sstevel@tonic-gate 	/* ignore others that could get sent */
2437c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);
2447c478bd9Sstevel@tonic-gate 	(void) signal(SIGUSR1, SIG_IGN);
2457c478bd9Sstevel@tonic-gate 	(void) signal(SIGUSR2, SIG_IGN);
2467c478bd9Sstevel@tonic-gate 	if (warmstart) {
2477c478bd9Sstevel@tonic-gate 		read_warmstart();
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	if (debugging) {
2507c478bd9Sstevel@tonic-gate 		printf("rpcbind debugging enabled.");
2517c478bd9Sstevel@tonic-gate 		if (doabort) {
2527c478bd9Sstevel@tonic-gate 			printf("  Will abort on errors!\n");
2537c478bd9Sstevel@tonic-gate 		} else {
2547c478bd9Sstevel@tonic-gate 			printf("\n");
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	} else {
2577c478bd9Sstevel@tonic-gate 		detachfromtty();
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/* These are basic privileges we do not need */
2617c478bd9Sstevel@tonic-gate 	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
2627c478bd9Sstevel@tonic-gate 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	my_svc_run();
2657c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "svc_run returned unexpectedly");
2667c478bd9Sstevel@tonic-gate 	rpcbind_abort();
2677c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * Increments a counter each time a problem is found with the network
2727c478bd9Sstevel@tonic-gate  * configuration information.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static int
2757c478bd9Sstevel@tonic-gate check_netconfig(void)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	void	*nc;
2787c478bd9Sstevel@tonic-gate 	void	*dlcookie;
2797c478bd9Sstevel@tonic-gate 	int	busted = 0;
2807c478bd9Sstevel@tonic-gate 	int	i;
2817c478bd9Sstevel@tonic-gate 	int	lo_clts_found = 0, lo_cots_found = 0, lo_cotsord_found = 0;
2827c478bd9Sstevel@tonic-gate 	struct netconfig	*nconf, *np;
2837c478bd9Sstevel@tonic-gate 	struct stat	sb;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	nc = setnetconfig();
2867c478bd9Sstevel@tonic-gate 	if (nc == NULL) {
2877c478bd9Sstevel@tonic-gate 		if (debugging)
2887c478bd9Sstevel@tonic-gate 			fprintf(stderr,
2897c478bd9Sstevel@tonic-gate 				"setnetconfig() failed:  %s\n", nc_sperror());
2907c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "setnetconfig() failed:  %s", nc_sperror());
2917c478bd9Sstevel@tonic-gate 		return (1);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	while (np = getnetconfig(nc)) {
2947c478bd9Sstevel@tonic-gate 		if ((np->nc_flag & NC_VISIBLE) == 0)
2957c478bd9Sstevel@tonic-gate 			continue;
2967c478bd9Sstevel@tonic-gate 		if (debugging)
2977c478bd9Sstevel@tonic-gate 			fprintf(stderr, "checking netid \"%s\"\n",
2987c478bd9Sstevel@tonic-gate 				np->nc_netid);
2997c478bd9Sstevel@tonic-gate 		if (strcmp(np->nc_protofmly, NC_LOOPBACK) == 0)
3007c478bd9Sstevel@tonic-gate 			switch (np->nc_semantics) {
3017c478bd9Sstevel@tonic-gate 			case NC_TPI_CLTS:
3027c478bd9Sstevel@tonic-gate 				lo_clts_found = 1;
3037c478bd9Sstevel@tonic-gate 				break;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			case NC_TPI_COTS:
3067c478bd9Sstevel@tonic-gate 				lo_cots_found = 1;
3077c478bd9Sstevel@tonic-gate 				break;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 			case NC_TPI_COTS_ORD:
3107c478bd9Sstevel@tonic-gate 				lo_cotsord_found = 1;
3117c478bd9Sstevel@tonic-gate 				break;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 		if (stat(np->nc_device, &sb) == -1 && errno == ENOENT) {
3147c478bd9Sstevel@tonic-gate 			if (debugging)
3157c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\tdevice %s does not exist\n",
3167c478bd9Sstevel@tonic-gate 					np->nc_device);
3177c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "netid %s:  device %s does not exist",
3187c478bd9Sstevel@tonic-gate 				np->nc_netid, np->nc_device);
3197c478bd9Sstevel@tonic-gate 			busted++;
3207c478bd9Sstevel@tonic-gate 		} else
3217c478bd9Sstevel@tonic-gate 			if (debugging)
3227c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\tdevice %s present\n",
3237c478bd9Sstevel@tonic-gate 					np->nc_device);
3247c478bd9Sstevel@tonic-gate 		for (i = 0; i < np->nc_nlookups; i++) {
3257c478bd9Sstevel@tonic-gate 			char	*libname = np->nc_lookups[i];
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			if ((dlcookie = dlopen(libname, RTLD_LAZY)) == NULL) {
3287c478bd9Sstevel@tonic-gate 				char *dlerrstr;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 				dlerrstr = dlerror();
3317c478bd9Sstevel@tonic-gate 				if (debugging) {
3327c478bd9Sstevel@tonic-gate 					fprintf(stderr,
3337c478bd9Sstevel@tonic-gate 	"\tnetid %s: dlopen of name-to-address library %s failed\ndlerror: %s",
3347c478bd9Sstevel@tonic-gate 	np->nc_netid, libname, dlerrstr ? dlerrstr : "");
3357c478bd9Sstevel@tonic-gate 				}
3367c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3377c478bd9Sstevel@tonic-gate 	"netid %s:  dlopen of name-to-address library %s failed",
3387c478bd9Sstevel@tonic-gate 						np->nc_netid, libname);
3397c478bd9Sstevel@tonic-gate 				if (dlerrstr)
3407c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "%s", dlerrstr);
3417c478bd9Sstevel@tonic-gate 				busted++;
3427c478bd9Sstevel@tonic-gate 			} else {
3437c478bd9Sstevel@tonic-gate 				if (debugging)
3447c478bd9Sstevel@tonic-gate 					fprintf(stderr,
3457c478bd9Sstevel@tonic-gate 	"\tdlopen of name-to-address library %s succeeded\n", libname);
3467c478bd9Sstevel@tonic-gate 				(void) dlclose(dlcookie);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 		nconf = getnetconfigent(np->nc_netid);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, ""))
3527c478bd9Sstevel@tonic-gate 			busted++;
3537c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, ""))
3547c478bd9Sstevel@tonic-gate 			busted++;
3557c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, "rpcbind"))
3567c478bd9Sstevel@tonic-gate 			busted++;
3577c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, "rpcbind"))
3587c478bd9Sstevel@tonic-gate 			busted++;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	endnetconfig(nc);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (lo_clts_found) {
3657c478bd9Sstevel@tonic-gate 		if (debugging)
3667c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found CLTS loopback transport\n");
3677c478bd9Sstevel@tonic-gate 	} else {
3687c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no CLTS loopback transport found\n");
3697c478bd9Sstevel@tonic-gate 		if (debugging)
3707c478bd9Sstevel@tonic-gate 			fprintf(stderr, "no CLTS loopback transport found\n");
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	if (lo_cots_found) {
3737c478bd9Sstevel@tonic-gate 		if (debugging)
3747c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found COTS loopback transport\n");
3757c478bd9Sstevel@tonic-gate 	} else {
3767c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS loopback transport found\n");
3777c478bd9Sstevel@tonic-gate 		if (debugging)
3787c478bd9Sstevel@tonic-gate 			fprintf(stderr, "no COTS loopback transport found\n");
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	if (lo_cotsord_found) {
3817c478bd9Sstevel@tonic-gate 		if (debugging)
3827c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found COTS ORD loopback transport\n");
3837c478bd9Sstevel@tonic-gate 	} else {
3847c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS ORD loopback transport found\n");
3857c478bd9Sstevel@tonic-gate 		if (debugging)
3867c478bd9Sstevel@tonic-gate 			fprintf(stderr,
3877c478bd9Sstevel@tonic-gate 				"no COTS ORD loopback transport found\n");
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (busted);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * Adds the entry into the rpcbind database.
3957c478bd9Sstevel@tonic-gate  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
3967c478bd9Sstevel@tonic-gate  * Returns 0 if succeeds, else fails
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate static int
3997c478bd9Sstevel@tonic-gate init_transport(struct netconfig *nconf)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	int fd;
4027c478bd9Sstevel@tonic-gate 	struct t_bind *taddr, *baddr;
4037c478bd9Sstevel@tonic-gate 	SVCXPRT	*my_xprt;
4047c478bd9Sstevel@tonic-gate 	struct nd_addrlist *nas;
4057c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
4067c478bd9Sstevel@tonic-gate 	int status;	/* bound checking ? */
4077c478bd9Sstevel@tonic-gate 	static int msgprt = 0;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
4107c478bd9Sstevel@tonic-gate 		(nconf->nc_semantics != NC_TPI_COTS) &&
4117c478bd9Sstevel@tonic-gate 		(nconf->nc_semantics != NC_TPI_COTS_ORD))
4127c478bd9Sstevel@tonic-gate 		return (1);	/* not my type */
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) && !ipv6flag) {
4157c478bd9Sstevel@tonic-gate 		if (!msgprt)
4167c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG,
4177c478bd9Sstevel@tonic-gate "/etc/netconfig has IPv6 entries but IPv6 is not configured");
4187c478bd9Sstevel@tonic-gate 		msgprt++;
4197c478bd9Sstevel@tonic-gate 		return (1);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
4227c478bd9Sstevel@tonic-gate 	{
4237c478bd9Sstevel@tonic-gate 	int i;
4247c478bd9Sstevel@tonic-gate 	char **s;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %d lookup routines :\n",
4277c478bd9Sstevel@tonic-gate 		nconf->nc_netid, nconf->nc_nlookups);
4287c478bd9Sstevel@tonic-gate 	for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups; i++, s++)
4297c478bd9Sstevel@tonic-gate 		fprintf(stderr, "[%d] - %s\n", i, *s);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate #endif
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
4347c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot open connection: %s",
4357c478bd9Sstevel@tonic-gate 				nconf->nc_netid, t_errlist[t_errno]);
4367c478bd9Sstevel@tonic-gate 		return (1);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
43945916cd2Sjpk 	if (is_system_labeled() &&
44045916cd2Sjpk 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
44145916cd2Sjpk 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
44245916cd2Sjpk 	    setopt_anon_mlp(fd) == -1) {
44345916cd2Sjpk 		syslog(LOG_ERR, "%s: couldn't set SO_ANON_MLP option",
44445916cd2Sjpk 		    nconf->nc_netid);
44545916cd2Sjpk 	}
44645916cd2Sjpk 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * Negotiate for returning the ucred of the caller. This should
4497c478bd9Sstevel@tonic-gate 	 * done before enabling the endpoint for service via
4507c478bd9Sstevel@tonic-gate 	 * t_bind() so that requests to rpcbind contain the uid.
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 	svc_fd_negotiate_ucred(fd);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	taddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4557c478bd9Sstevel@tonic-gate 	baddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4567c478bd9Sstevel@tonic-gate 	if ((baddr == NULL) || (taddr == NULL)) {
4577c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot allocate netbuf: %s",
4587c478bd9Sstevel@tonic-gate 				nconf->nc_netid, t_errlist[t_errno]);
4597c478bd9Sstevel@tonic-gate 		exit(1);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/* Get rpcbind's address on this transport */
4637c478bd9Sstevel@tonic-gate 	hs.h_host = HOST_SELF;
4647c478bd9Sstevel@tonic-gate 	hs.h_serv = servname;
4657c478bd9Sstevel@tonic-gate 	if (netdir_getbyname(nconf, &hs, &nas))
4667c478bd9Sstevel@tonic-gate 		goto error;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/* Copy the address */
4697c478bd9Sstevel@tonic-gate 	taddr->addr.len = nas->n_addrs->len;
4707c478bd9Sstevel@tonic-gate 	memcpy(taddr->addr.buf, nas->n_addrs->buf, (int)nas->n_addrs->len);
4717c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
4727c478bd9Sstevel@tonic-gate 	{
4737c478bd9Sstevel@tonic-gate 	/* for debugging print out our universal address */
4747c478bd9Sstevel@tonic-gate 	char *uaddr;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	uaddr = taddr2uaddr(nconf, nas->n_addrs);
4777c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
4787c478bd9Sstevel@tonic-gate 	(void) free(uaddr);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate #endif
4817c478bd9Sstevel@tonic-gate 	netdir_free((char *)nas, ND_ADDRLIST);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (nconf->nc_semantics == NC_TPI_CLTS)
4847c478bd9Sstevel@tonic-gate 		taddr->qlen = 0;
4857c478bd9Sstevel@tonic-gate 	else
4867c478bd9Sstevel@tonic-gate 		taddr->qlen = listen_backlog;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
4897c478bd9Sstevel@tonic-gate 		/*
4907c478bd9Sstevel@tonic-gate 		 * Sm: If we are running then set SO_REUSEADDR option
4917c478bd9Sstevel@tonic-gate 		 * so that we can bind to our preferred address even if
4927c478bd9Sstevel@tonic-gate 		 * previous connections are in FIN_WAIT state
4937c478bd9Sstevel@tonic-gate 		 */
4947c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
4957c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Setting SO_REUSEADDR.\n");
4967c478bd9Sstevel@tonic-gate #endif
4977c478bd9Sstevel@tonic-gate 		if (setopt_reuseaddr(fd) == -1) {
4987c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Couldn't set SO_REUSEADDR option");
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (t_bind(fd, taddr, baddr) != 0) {
5037c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot bind: %s",
5047c478bd9Sstevel@tonic-gate 			nconf->nc_netid, t_errlist[t_errno]);
5057c478bd9Sstevel@tonic-gate 		goto error;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (memcmp(taddr->addr.buf, baddr->addr.buf, (int)baddr->addr.len)) {
5107c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: address in use", nconf->nc_netid);
5117c478bd9Sstevel@tonic-gate 		goto error;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, baddr, 0, 0);
5157c478bd9Sstevel@tonic-gate 	if (my_xprt == (SVCXPRT *)NULL) {
5167c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: could not create service",
5177c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
5187c478bd9Sstevel@tonic-gate 		goto error;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* set up multicast address for RPC CALL_IT, IPv6 */
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
5247c478bd9Sstevel@tonic-gate 	    (strcmp(nconf->nc_proto, NC_UDP) == 0)) {
5257c478bd9Sstevel@tonic-gate 		if (setup_callit(fd) < 0) {
5267c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Unable to join IPv6 multicast group \
5277c478bd9Sstevel@tonic-gate for rpc broadcast %s", RPCB_MULTICAST_ADDR);
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5327c478bd9Sstevel@tonic-gate 			svc_control(my_xprt, SVCSET_KEEPALIVE, (void *) TRUE);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate #ifdef PORTMAP
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * Register both the versions for tcp/ip and udp/ip
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
5407c478bd9Sstevel@tonic-gate 		((strcmp(nconf->nc_proto, NC_TCP) == 0) ||
5417c478bd9Sstevel@tonic-gate 		(strcmp(nconf->nc_proto, NC_UDP) == 0))) {
5427c478bd9Sstevel@tonic-gate 		PMAPLIST *pml;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
5457c478bd9Sstevel@tonic-gate 			pmap_service, NULL)) {
5467c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "could not register on %s",
5477c478bd9Sstevel@tonic-gate 					nconf->nc_netid);
5487c478bd9Sstevel@tonic-gate 			goto error;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5517c478bd9Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
5527c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
5537c478bd9Sstevel@tonic-gate 			exit(1);
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_prog = PMAPPROG;
5567c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = PMAPVERS;
5577c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_port = PMAPPORT;
5587c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5597c478bd9Sstevel@tonic-gate 			if (tcptrans[0]) {
5607c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
5617c478bd9Sstevel@tonic-gate 				"cannot have more than one TCP transport");
5627c478bd9Sstevel@tonic-gate 				goto error;
5637c478bd9Sstevel@tonic-gate 			}
5647c478bd9Sstevel@tonic-gate 			tcptrans = strdup(nconf->nc_netid);
5657c478bd9Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_TCP;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 			/* Let's snarf the universal address */
5687c478bd9Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
5697c478bd9Sstevel@tonic-gate 			tcp_uaddr = taddr2uaddr(nconf, &baddr->addr);
5707c478bd9Sstevel@tonic-gate 		} else {
5717c478bd9Sstevel@tonic-gate 			if (udptrans[0]) {
5727c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
5737c478bd9Sstevel@tonic-gate 				"cannot have more than one UDP transport");
5747c478bd9Sstevel@tonic-gate 				goto error;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 			udptrans = strdup(nconf->nc_netid);
5777c478bd9Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_UDP;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 			/* Let's snarf the universal address */
5807c478bd9Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
5817c478bd9Sstevel@tonic-gate 			udp_uaddr = taddr2uaddr(nconf, &baddr->addr);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
5847c478bd9Sstevel@tonic-gate 		list_pml = pml;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		/* Add version 3 information */
5877c478bd9Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5887c478bd9Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
5897c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
5907c478bd9Sstevel@tonic-gate 			exit(1);
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
5937c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS;
5947c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
5957c478bd9Sstevel@tonic-gate 		list_pml = pml;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 		/* Add version 4 information */
5987c478bd9Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5997c478bd9Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
6007c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
6017c478bd9Sstevel@tonic-gate 			exit(1);
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
6047c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS4;
6057c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
6067c478bd9Sstevel@tonic-gate 		list_pml = pml;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		/* Also add version 2 stuff to rpcbind list */
6097c478bd9Sstevel@tonic-gate 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &baddr->addr);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate #endif
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	/* version 3 registration */
6147c478bd9Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
6157c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 3",
6167c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
6177c478bd9Sstevel@tonic-gate 		goto error;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &baddr->addr);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* version 4 registration */
6227c478bd9Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
6237c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 4",
6247c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
6257c478bd9Sstevel@tonic-gate 		goto error;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &baddr->addr);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
6307c478bd9Sstevel@tonic-gate 		if (nconf->nc_semantics == NC_TPI_CLTS)
6317c478bd9Sstevel@tonic-gate 			loopback_dg = strdup(nconf->nc_netid);
6327c478bd9Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS)
6337c478bd9Sstevel@tonic-gate 			loopback_vc = strdup(nconf->nc_netid);
6347c478bd9Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS_ORD)
6357c478bd9Sstevel@tonic-gate 			loopback_vc_ord = strdup(nconf->nc_netid);
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/* decide if bound checking works for this transport */
6397c478bd9Sstevel@tonic-gate 	status = add_bndlist(nconf, taddr, baddr);
6407c478bd9Sstevel@tonic-gate #ifdef BIND_DEBUG
6417c478bd9Sstevel@tonic-gate 	if (status < 0) {
6427c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Error in finding bind status for %s\n",
6437c478bd9Sstevel@tonic-gate 			nconf->nc_netid);
6447c478bd9Sstevel@tonic-gate 	} else if (status == 0) {
6457c478bd9Sstevel@tonic-gate 		fprintf(stderr, "check binding for %s\n",
6467c478bd9Sstevel@tonic-gate 			nconf->nc_netid);
6477c478bd9Sstevel@tonic-gate 	} else if (status > 0) {
6487c478bd9Sstevel@tonic-gate 		fprintf(stderr, "No check binding for %s\n",
6497c478bd9Sstevel@tonic-gate 			nconf->nc_netid);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate #endif
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * rmtcall only supported on CLTS transports for now.
6547c478bd9Sstevel@tonic-gate 	 * only needed when we are allowing indirect calls
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	if (allow_indirect && nconf->nc_semantics == NC_TPI_CLTS) {
6577c478bd9Sstevel@tonic-gate 		status = create_rmtcall_fd(nconf);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate #ifdef BIND_DEBUG
6607c478bd9Sstevel@tonic-gate 		if (status < 0) {
6617c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not create rmtcall fd for %s\n",
6627c478bd9Sstevel@tonic-gate 				nconf->nc_netid);
6637c478bd9Sstevel@tonic-gate 		} else {
6647c478bd9Sstevel@tonic-gate 			fprintf(stderr, "rmtcall fd for %s is %d\n",
6657c478bd9Sstevel@tonic-gate 				nconf->nc_netid, status);
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate #endif
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
6707c478bd9Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
6717c478bd9Sstevel@tonic-gate 	return (0);
6727c478bd9Sstevel@tonic-gate error:
6737c478bd9Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
6747c478bd9Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
6757c478bd9Sstevel@tonic-gate 	(void) t_close(fd);
6767c478bd9Sstevel@tonic-gate 	return (1);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static void
6807c478bd9Sstevel@tonic-gate rbllist_add(ulong_t prog, ulong_t vers, struct netconfig *nconf,
6817c478bd9Sstevel@tonic-gate 	struct netbuf *addr)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	rpcblist_ptr rbl;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	rbl = (rpcblist_ptr)malloc((uint_t)sizeof (rpcblist));
6867c478bd9Sstevel@tonic-gate 	if (rbl == (rpcblist_ptr)NULL) {
6877c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory!");
6887c478bd9Sstevel@tonic-gate 		exit(1);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_prog = prog;
6927c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_vers = vers;
6937c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
6947c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
6957c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_owner = strdup(superuser);
6967c478bd9Sstevel@tonic-gate 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
6977c478bd9Sstevel@tonic-gate 	list_rbl = rbl;
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * Catch the signal and die
7027c478bd9Sstevel@tonic-gate  */
7037c478bd9Sstevel@tonic-gate /* ARGSUSED */
7047c478bd9Sstevel@tonic-gate static void
7057c478bd9Sstevel@tonic-gate terminate(int sig)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "rpcbind terminating on signal.");
7087c478bd9Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7097c478bd9Sstevel@tonic-gate 	exit(2);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate void
7137c478bd9Sstevel@tonic-gate rpcbind_abort(void)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7167c478bd9Sstevel@tonic-gate 	abort();
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * detach from tty
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate static void
7237c478bd9Sstevel@tonic-gate detachfromtty(void)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	close(0);
7267c478bd9Sstevel@tonic-gate 	close(1);
7277c478bd9Sstevel@tonic-gate 	close(2);
7287c478bd9Sstevel@tonic-gate 	switch (forkall()) {
7297c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
7307c478bd9Sstevel@tonic-gate 		perror("fork");
7317c478bd9Sstevel@tonic-gate 		break;
7327c478bd9Sstevel@tonic-gate 	case 0:
7337c478bd9Sstevel@tonic-gate 		break;
7347c478bd9Sstevel@tonic-gate 	default:
7357c478bd9Sstevel@tonic-gate 		exit(0);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	setsid();
7387c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
7397c478bd9Sstevel@tonic-gate 	dup(0);
7407c478bd9Sstevel@tonic-gate 	dup(0);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate /* get command line options */
7447c478bd9Sstevel@tonic-gate static void
7457c478bd9Sstevel@tonic-gate parseargs(int argc, char *argv[])
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	int c;
7487c478bd9Sstevel@tonic-gate 	int tmp;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dwal:")) != EOF) {
7517c478bd9Sstevel@tonic-gate 		switch (c) {
7527c478bd9Sstevel@tonic-gate 		case 'd':
7537c478bd9Sstevel@tonic-gate 			debugging = 1;
7547c478bd9Sstevel@tonic-gate 			break;
7557c478bd9Sstevel@tonic-gate 		case 'a':
7567c478bd9Sstevel@tonic-gate 			doabort = 1;	/* when debugging, do an abort on */
7577c478bd9Sstevel@tonic-gate 			break;		/* errors; for rpcbind developers */
7587c478bd9Sstevel@tonic-gate 					/* only! */
7597c478bd9Sstevel@tonic-gate 		case 'w':
7607c478bd9Sstevel@tonic-gate 			warmstart = 1;
7617c478bd9Sstevel@tonic-gate 			break;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		case 'l':
7647c478bd9Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &tmp)) {
7657c478bd9Sstevel@tonic-gate 				if (tmp > listen_backlog) {
7667c478bd9Sstevel@tonic-gate 					listen_backlog = tmp;
7677c478bd9Sstevel@tonic-gate 				}
7687c478bd9Sstevel@tonic-gate 			}
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		default:	/* error */
7717c478bd9Sstevel@tonic-gate 			fprintf(stderr,
7727c478bd9Sstevel@tonic-gate 			"usage: rpcbind [-d] [-w] [-l listen_backlog]\n");
7737c478bd9Sstevel@tonic-gate 			exit(1);
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	if (doabort && !debugging) {
7777c478bd9Sstevel@tonic-gate 	    fprintf(stderr,
7787c478bd9Sstevel@tonic-gate 		"-a (abort) specified without -d (debugging) -- ignored.\n");
7797c478bd9Sstevel@tonic-gate 	    doabort = 0;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate static int
78445916cd2Sjpk setopt_int(int fd, int level, int name, int value)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	struct t_optmgmt req, resp;
78745916cd2Sjpk 	struct {
78845916cd2Sjpk 		struct opthdr opt;
78945916cd2Sjpk 		int value;
79045916cd2Sjpk 	} optdata;
7917c478bd9Sstevel@tonic-gate 
79245916cd2Sjpk 	optdata.opt.level = level;
79345916cd2Sjpk 	optdata.opt.name = name;
79445916cd2Sjpk 	optdata.opt.len = sizeof (int);
7957c478bd9Sstevel@tonic-gate 
79645916cd2Sjpk 	optdata.value = value;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
79945916cd2Sjpk 	req.opt.len = sizeof (optdata);
80045916cd2Sjpk 	req.opt.buf = (char *)&optdata;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	resp.flags = 0;
80345916cd2Sjpk 	resp.opt.buf = (char *)&optdata;
80445916cd2Sjpk 	resp.opt.maxlen = sizeof (optdata);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
8077c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
8087c478bd9Sstevel@tonic-gate 		return (-1);
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 	return (0);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate static int
81445916cd2Sjpk setopt_reuseaddr(int fd)
81545916cd2Sjpk {
81645916cd2Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_REUSEADDR, 1));
81745916cd2Sjpk }
81845916cd2Sjpk 
81945916cd2Sjpk static int
82045916cd2Sjpk setopt_anon_mlp(int fd)
82145916cd2Sjpk {
82245916cd2Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_ANON_MLP, 1));
82345916cd2Sjpk }
82445916cd2Sjpk 
82545916cd2Sjpk static int
8267c478bd9Sstevel@tonic-gate setup_callit(int fd)
8277c478bd9Sstevel@tonic-gate {
8287c478bd9Sstevel@tonic-gate 	struct ipv6_mreq mreq;
8297c478bd9Sstevel@tonic-gate 	struct t_optmgmt req, resp;
8307c478bd9Sstevel@tonic-gate 	struct opthdr *opt;
8317c478bd9Sstevel@tonic-gate 	char reqbuf[ sizeof (struct ipv6_mreq) + 24];
8327c478bd9Sstevel@tonic-gate 	struct ipv6_mreq *pmreq;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	opt = (struct opthdr *)reqbuf;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	opt->level = IPPROTO_IPV6;
8377c478bd9Sstevel@tonic-gate 	opt->name = IPV6_ADD_MEMBERSHIP;
8387c478bd9Sstevel@tonic-gate 	opt->len = sizeof (struct ipv6_mreq);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	/* multicast address */
8417c478bd9Sstevel@tonic-gate 	(void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR,
8427c478bd9Sstevel@tonic-gate 		mreq.ipv6mr_multiaddr.s6_addr);
8437c478bd9Sstevel@tonic-gate 	mreq.ipv6mr_interface = 0;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	/* insert it into opt */
8467c478bd9Sstevel@tonic-gate 	pmreq = (struct ipv6_mreq *)&reqbuf[sizeof (struct opthdr)];
8477c478bd9Sstevel@tonic-gate 	memcpy(pmreq, &mreq, sizeof (struct ipv6_mreq));
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
8507c478bd9Sstevel@tonic-gate 	req.opt.len = sizeof (struct opthdr) + opt->len;
8517c478bd9Sstevel@tonic-gate 	req.opt.buf = (char *)opt;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	resp.flags = 0;
8547c478bd9Sstevel@tonic-gate 	resp.opt.buf = reqbuf;
8557c478bd9Sstevel@tonic-gate 	resp.opt.maxlen = sizeof (reqbuf);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
8587c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
8597c478bd9Sstevel@tonic-gate 		return (-1);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 	return (0);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate static boolean_t
8657c478bd9Sstevel@tonic-gate check_hostserv(struct netconfig *nconf, const char *host, const char *serv)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	struct nd_hostserv nh;
8687c478bd9Sstevel@tonic-gate 	struct nd_addrlist *na;
8697c478bd9Sstevel@tonic-gate 	const char *hostname = host;
8707c478bd9Sstevel@tonic-gate 	const char *servname = serv;
8717c478bd9Sstevel@tonic-gate 	int retval;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (strcmp(host, HOST_SELF) == 0)
8747c478bd9Sstevel@tonic-gate 		hostname = "HOST_SELF";
8757c478bd9Sstevel@tonic-gate 	else if (strcmp(host, HOST_SELF_CONNECT) == 0)
8767c478bd9Sstevel@tonic-gate 		hostname = "HOST_SELF_CONNECT";
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (serv[0] == '\0')
8797c478bd9Sstevel@tonic-gate 		servname = "<any>";
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	nh.h_host = (char *)host;
8827c478bd9Sstevel@tonic-gate 	nh.h_serv = (char *)serv;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	retval = netdir_getbyname(nconf, &nh, &na);
8857c478bd9Sstevel@tonic-gate 	if (retval != ND_OK || na->n_cnt == 0) {
8867c478bd9Sstevel@tonic-gate 		if (retval == ND_OK)
8877c478bd9Sstevel@tonic-gate 			netdir_free(na, ND_ADDRLIST);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "netid %s: cannot find an address for host "
8907c478bd9Sstevel@tonic-gate 		    "%s, service \"%s\"", nconf->nc_netid, hostname, servname);
8917c478bd9Sstevel@tonic-gate 		if (debugging) {
8927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\tnetdir_getbyname for %s, "
8937c478bd9Sstevel@tonic-gate 			    "service \"%s\" failed\n", hostname, servname);
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 		return (B_FALSE);
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	netdir_free(na, ND_ADDRLIST);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (debugging) {
9007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tnetdir_getbyname for %s, service "
9017c478bd9Sstevel@tonic-gate 		    "service \"%s\" succeeded\n", hostname, servname);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 	return (B_TRUE);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate #define	DEFRPCBIND	"/etc/default/rpcbind"
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /* Maximum outstanding syslog requests */
9097c478bd9Sstevel@tonic-gate #define	MAXLOG		100
9107c478bd9Sstevel@tonic-gate /* Maximum length: the messages generated are fairly short; no hostnames. */
9117c478bd9Sstevel@tonic-gate #define	MAXMSG		128
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate typedef struct logmsg {
9147c478bd9Sstevel@tonic-gate 	struct logmsg	*log_next;
9157c478bd9Sstevel@tonic-gate 	int		log_pri;
9167c478bd9Sstevel@tonic-gate 	char		log_msg[MAXMSG];
9177c478bd9Sstevel@tonic-gate } logmsg;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate static logmsg *loghead = NULL;
9207c478bd9Sstevel@tonic-gate static logmsg **logtailp = &loghead;
9217c478bd9Sstevel@tonic-gate static mutex_t logmutex = DEFAULTMUTEX;
9227c478bd9Sstevel@tonic-gate static cond_t logcond = DEFAULTCV;
9237c478bd9Sstevel@tonic-gate static int logcount = 0;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9267c478bd9Sstevel@tonic-gate static void *
9277c478bd9Sstevel@tonic-gate logthread(void *arg)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	while (1) {
9307c478bd9Sstevel@tonic-gate 		logmsg *msg;
9317c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&logmutex);
9327c478bd9Sstevel@tonic-gate 		while ((msg = loghead) == NULL)
9337c478bd9Sstevel@tonic-gate 			(void) cond_wait(&logcond, &logmutex);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		loghead = msg->log_next;
9367c478bd9Sstevel@tonic-gate 		logcount--;
9377c478bd9Sstevel@tonic-gate 		if (loghead == NULL) {
9387c478bd9Sstevel@tonic-gate 			logtailp = &loghead;
9397c478bd9Sstevel@tonic-gate 			logcount = 0;
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&logmutex);
9427c478bd9Sstevel@tonic-gate 		syslog(msg->log_pri, "%s", msg->log_msg);
9437c478bd9Sstevel@tonic-gate 		free(msg);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * Initialize: read the configuration parameters from the default file.
9507c478bd9Sstevel@tonic-gate  */
9517c478bd9Sstevel@tonic-gate static void
9527c478bd9Sstevel@tonic-gate rpcb_check_init(void)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	thread_t tid;
9557c478bd9Sstevel@tonic-gate 	scf_simple_prop_t *prop;
9567c478bd9Sstevel@tonic-gate 	uint8_t	*bool;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, NULL, "config",
9597c478bd9Sstevel@tonic-gate 	    "enable_tcpwrappers")) != NULL) {
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		if ((bool = scf_simple_prop_next_boolean(prop)) != NULL) {
9627c478bd9Sstevel@tonic-gate 			wrap_enabled = (*bool == 0) ? B_FALSE : B_TRUE;
9637c478bd9Sstevel@tonic-gate 		} else {
9647c478bd9Sstevel@tonic-gate 			syslog(LOG_ALERT, "enable_tcpwrappers no value %s",
9657c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
9687c478bd9Sstevel@tonic-gate 	} else {
9697c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "unable to get enable_tcpwrappers %s",
9707c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, NULL, "config",
9737c478bd9Sstevel@tonic-gate 	    "verbose_logging")) != NULL) {
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		if ((bool = scf_simple_prop_next_boolean(prop)) != NULL) {
9767c478bd9Sstevel@tonic-gate 			verboselog = (*bool == 0) ? B_FALSE : B_TRUE;
9777c478bd9Sstevel@tonic-gate 		} else {
9787c478bd9Sstevel@tonic-gate 			syslog(LOG_ALERT, "verboselog no value %s",
9797c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
9827c478bd9Sstevel@tonic-gate 	} else {
9837c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "unable to get verbose_logging %s",
9847c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, NULL, "config",
9877c478bd9Sstevel@tonic-gate 	    "allow_indirect")) != NULL) {
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		if ((bool = scf_simple_prop_next_boolean(prop)) != NULL) {
9907c478bd9Sstevel@tonic-gate 			allow_indirect = (*bool == 0) ? B_FALSE : B_TRUE;
9917c478bd9Sstevel@tonic-gate 		} else {
9927c478bd9Sstevel@tonic-gate 			syslog(LOG_ALERT, "allow_indirect no value %s",
9937c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
9967c478bd9Sstevel@tonic-gate 	} else {
9977c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "unable to get allow_indirect %s",
9987c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (wrap_enabled)
10027c478bd9Sstevel@tonic-gate 		(void) thr_create(NULL, 0, logthread, NULL, THR_DETACHED, &tid);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * qsyslog() - queue a request for syslog(); if syslog blocks, the other
10077c478bd9Sstevel@tonic-gate  * thread blocks; we make sure we don't run out of memory by allowing
10087c478bd9Sstevel@tonic-gate  * only a limited number of outstandig syslog() requests.
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate void
10117c478bd9Sstevel@tonic-gate qsyslog(int pri, const char *fmt, ...)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	logmsg *msg = malloc(sizeof (*msg));
10147c478bd9Sstevel@tonic-gate 	int oldcount;
10157c478bd9Sstevel@tonic-gate 	va_list ap;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (msg == NULL)
10187c478bd9Sstevel@tonic-gate 		return;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	msg->log_pri = pri;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
10237c478bd9Sstevel@tonic-gate 	(void) vsnprintf(msg->log_msg, sizeof (msg->log_msg), fmt, ap);
10247c478bd9Sstevel@tonic-gate 	va_end(ap);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	msg->log_next = NULL;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&logmutex);
10297c478bd9Sstevel@tonic-gate 	oldcount = logcount;
10307c478bd9Sstevel@tonic-gate 	if (logcount < MAXLOG) {
10317c478bd9Sstevel@tonic-gate 		logcount++;
10327c478bd9Sstevel@tonic-gate 		*logtailp = msg;
10337c478bd9Sstevel@tonic-gate 		logtailp = &msg->log_next;
10347c478bd9Sstevel@tonic-gate 	} else {
10357c478bd9Sstevel@tonic-gate 		free(msg);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&logmutex);
10387c478bd9Sstevel@tonic-gate 	if (oldcount == 0)
10397c478bd9Sstevel@tonic-gate 		(void) cond_signal(&logcond);
10407c478bd9Sstevel@tonic-gate }
1041