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