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