xref: /titanic_53/usr/src/cmd/rpcbind/rpcbind.c (revision 9e293969c29a9c274758e70e5a7349223cef86c1)
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 /*
387c478bd9Sstevel@tonic-gate  * rpcbind.c
397c478bd9Sstevel@tonic-gate  * Implements the program, version to address mapping for rpc.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <dlfcn.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
45004388ebScasper #include <stdio_ext.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
517c478bd9Sstevel@tonic-gate #include <netconfig.h>
527c478bd9Sstevel@tonic-gate #include <netdir.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate #include <sys/wait.h>
557c478bd9Sstevel@tonic-gate #include <signal.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <thread.h>
597c478bd9Sstevel@tonic-gate #include <synch.h>
607c478bd9Sstevel@tonic-gate #include <stdarg.h>
617c478bd9Sstevel@tonic-gate #ifdef PORTMAP
627c478bd9Sstevel@tonic-gate #include <netinet/in.h>
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
657c478bd9Sstevel@tonic-gate #include <sys/termios.h>
667c478bd9Sstevel@tonic-gate #include "rpcbind.h"
677c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
687c478bd9Sstevel@tonic-gate #include <sys/stat.h>
697c478bd9Sstevel@tonic-gate #include <syslog.h>
707c478bd9Sstevel@tonic-gate #include <string.h>
717c478bd9Sstevel@tonic-gate #include <sys/time.h>
727c478bd9Sstevel@tonic-gate #include <sys/resource.h>
737c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
747c478bd9Sstevel@tonic-gate #include <priv_utils.h>
757c478bd9Sstevel@tonic-gate #include <libscf.h>
76*9e293969SRichard Lowe #include <sys/ccompile.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #ifdef PORTMAP
797c478bd9Sstevel@tonic-gate extern void pmap_service(struct svc_req *, SVCXPRT *xprt);
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate extern void rpcb_service_3(struct svc_req *, SVCXPRT *xprt);
827c478bd9Sstevel@tonic-gate extern void rpcb_service_4(struct svc_req *, SVCXPRT *xprt);
837c478bd9Sstevel@tonic-gate extern void read_warmstart(void);
847c478bd9Sstevel@tonic-gate extern void write_warmstart(void);
857c478bd9Sstevel@tonic-gate extern int Is_ipv6present(void);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	MAX_FILEDESC_LIMIT	1023
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static void terminate(int);
90064ed339Sjjj static void note_refresh(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 *);
9845916cd2Sjpk static int setopt_reuseaddr(int);
9945916cd2Sjpk static int setopt_anon_mlp(int);
10045916cd2Sjpk static int setup_callit(int);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* Global variables */
1037c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
1047c478bd9Sstevel@tonic-gate int debugging = 1;	/* Tell me what's going on */
1057c478bd9Sstevel@tonic-gate #else
1067c478bd9Sstevel@tonic-gate int debugging = 0;	/* Tell me what's going on */
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate static int ipv6flag = 0;
1097c478bd9Sstevel@tonic-gate int doabort = 0;	/* When debugging, do an abort on errors */
1107c478bd9Sstevel@tonic-gate static int listen_backlog = 64;
1117c478bd9Sstevel@tonic-gate rpcblist_ptr list_rbl;	/* A list of version 3/4 rpcbind services */
1127c478bd9Sstevel@tonic-gate char *loopback_dg;	/* Datagram loopback transport, for set and unset */
1137c478bd9Sstevel@tonic-gate char *loopback_vc;	/* COTS loopback transport, for set and unset */
1147c478bd9Sstevel@tonic-gate char *loopback_vc_ord;	/* COTS_ORD loopback transport, for set and unset */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate boolean_t verboselog = B_FALSE;
1177c478bd9Sstevel@tonic-gate boolean_t wrap_enabled = B_FALSE;
1187c478bd9Sstevel@tonic-gate boolean_t allow_indirect = B_TRUE;
1190ea5e3a5Sjjj boolean_t local_only = B_FALSE;
1207c478bd9Sstevel@tonic-gate 
121064ed339Sjjj volatile sig_atomic_t sigrefresh;
122064ed339Sjjj 
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 */
244064ed339Sjjj 	(void) sigset(SIGHUP, note_refresh);
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",
335*9e293969SRichard Lowe 					    np->nc_netid, libname,
336*9e293969SRichard Lowe 					    dlerrstr ? dlerrstr : "");
3377c478bd9Sstevel@tonic-gate 				}
3387c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3397c478bd9Sstevel@tonic-gate 	"netid %s:  dlopen of name-to-address library %s failed",
3407c478bd9Sstevel@tonic-gate 				    np->nc_netid, libname);
3417c478bd9Sstevel@tonic-gate 				if (dlerrstr)
3427c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "%s", dlerrstr);
3437c478bd9Sstevel@tonic-gate 				busted++;
3447c478bd9Sstevel@tonic-gate 			} else {
3457c478bd9Sstevel@tonic-gate 				if (debugging)
3467c478bd9Sstevel@tonic-gate 					fprintf(stderr,
3477c478bd9Sstevel@tonic-gate 	"\tdlopen of name-to-address library %s succeeded\n", libname);
3487c478bd9Sstevel@tonic-gate 				(void) dlclose(dlcookie);
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		nconf = getnetconfigent(np->nc_netid);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, ""))
3547c478bd9Sstevel@tonic-gate 			busted++;
3557c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, ""))
3567c478bd9Sstevel@tonic-gate 			busted++;
3577c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, "rpcbind"))
3587c478bd9Sstevel@tonic-gate 			busted++;
3597c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, "rpcbind"))
3607c478bd9Sstevel@tonic-gate 			busted++;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	endnetconfig(nc);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (lo_clts_found) {
3677c478bd9Sstevel@tonic-gate 		if (debugging)
3687c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found CLTS loopback transport\n");
3697c478bd9Sstevel@tonic-gate 	} else {
3707c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no CLTS loopback transport found\n");
3717c478bd9Sstevel@tonic-gate 		if (debugging)
3727c478bd9Sstevel@tonic-gate 			fprintf(stderr, "no CLTS loopback transport found\n");
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	if (lo_cots_found) {
3757c478bd9Sstevel@tonic-gate 		if (debugging)
3767c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found COTS loopback transport\n");
3777c478bd9Sstevel@tonic-gate 	} else {
3787c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS loopback transport found\n");
3797c478bd9Sstevel@tonic-gate 		if (debugging)
3807c478bd9Sstevel@tonic-gate 			fprintf(stderr, "no COTS loopback transport found\n");
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	if (lo_cotsord_found) {
3837c478bd9Sstevel@tonic-gate 		if (debugging)
3847c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found COTS ORD loopback transport\n");
3857c478bd9Sstevel@tonic-gate 	} else {
3867c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS ORD loopback transport found\n");
3877c478bd9Sstevel@tonic-gate 		if (debugging)
3887c478bd9Sstevel@tonic-gate 			fprintf(stderr,
3897c478bd9Sstevel@tonic-gate 			    "no COTS ORD loopback transport found\n");
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	return (busted);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Adds the entry into the rpcbind database.
3977c478bd9Sstevel@tonic-gate  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
3987c478bd9Sstevel@tonic-gate  * Returns 0 if succeeds, else fails
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate init_transport(struct netconfig *nconf)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	int fd;
4047c478bd9Sstevel@tonic-gate 	struct t_bind *taddr, *baddr;
4057c478bd9Sstevel@tonic-gate 	SVCXPRT	*my_xprt;
4067c478bd9Sstevel@tonic-gate 	struct nd_addrlist *nas;
4077c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
4087c478bd9Sstevel@tonic-gate 	int status;	/* bound checking ? */
4097c478bd9Sstevel@tonic-gate 	static int msgprt = 0;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
4127c478bd9Sstevel@tonic-gate 	    (nconf->nc_semantics != NC_TPI_COTS) &&
4137c478bd9Sstevel@tonic-gate 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
4147c478bd9Sstevel@tonic-gate 		return (1);	/* not my type */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) && !ipv6flag) {
4177c478bd9Sstevel@tonic-gate 		if (!msgprt)
4187c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG,
4197c478bd9Sstevel@tonic-gate "/etc/netconfig has IPv6 entries but IPv6 is not configured");
4207c478bd9Sstevel@tonic-gate 		msgprt++;
4217c478bd9Sstevel@tonic-gate 		return (1);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
4247c478bd9Sstevel@tonic-gate 	{
4257c478bd9Sstevel@tonic-gate 	int i;
4267c478bd9Sstevel@tonic-gate 	char **s;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %d lookup routines :\n",
4297c478bd9Sstevel@tonic-gate 	    nconf->nc_netid, nconf->nc_nlookups);
4307c478bd9Sstevel@tonic-gate 	for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups; i++, s++)
4317c478bd9Sstevel@tonic-gate 		fprintf(stderr, "[%d] - %s\n", i, *s);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate #endif
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
4367c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot open connection: %s",
4377c478bd9Sstevel@tonic-gate 		    nconf->nc_netid, t_errlist[t_errno]);
4387c478bd9Sstevel@tonic-gate 		return (1);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
44145916cd2Sjpk 	if (is_system_labeled() &&
44245916cd2Sjpk 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
44345916cd2Sjpk 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
44445916cd2Sjpk 	    setopt_anon_mlp(fd) == -1) {
44545916cd2Sjpk 		syslog(LOG_ERR, "%s: couldn't set SO_ANON_MLP option",
44645916cd2Sjpk 		    nconf->nc_netid);
44745916cd2Sjpk 	}
44845916cd2Sjpk 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Negotiate for returning the ucred of the caller. This should
4517c478bd9Sstevel@tonic-gate 	 * done before enabling the endpoint for service via
4527c478bd9Sstevel@tonic-gate 	 * t_bind() so that requests to rpcbind contain the uid.
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	svc_fd_negotiate_ucred(fd);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	taddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4577c478bd9Sstevel@tonic-gate 	baddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
4587c478bd9Sstevel@tonic-gate 	if ((baddr == NULL) || (taddr == NULL)) {
4597c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot allocate netbuf: %s",
4607c478bd9Sstevel@tonic-gate 		    nconf->nc_netid, t_errlist[t_errno]);
4617c478bd9Sstevel@tonic-gate 		exit(1);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/* Get rpcbind's address on this transport */
4657c478bd9Sstevel@tonic-gate 	hs.h_host = HOST_SELF;
4667c478bd9Sstevel@tonic-gate 	hs.h_serv = servname;
4677c478bd9Sstevel@tonic-gate 	if (netdir_getbyname(nconf, &hs, &nas))
4687c478bd9Sstevel@tonic-gate 		goto error;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/* Copy the address */
4717c478bd9Sstevel@tonic-gate 	taddr->addr.len = nas->n_addrs->len;
4727c478bd9Sstevel@tonic-gate 	memcpy(taddr->addr.buf, nas->n_addrs->buf, (int)nas->n_addrs->len);
4737c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
4747c478bd9Sstevel@tonic-gate 	{
4757c478bd9Sstevel@tonic-gate 	/* for debugging print out our universal address */
4767c478bd9Sstevel@tonic-gate 	char *uaddr;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	uaddr = taddr2uaddr(nconf, nas->n_addrs);
4797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "rpcbind : my address is %s\n", uaddr);
4807c478bd9Sstevel@tonic-gate 	(void) free(uaddr);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate #endif
4837c478bd9Sstevel@tonic-gate 	netdir_free((char *)nas, ND_ADDRLIST);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (nconf->nc_semantics == NC_TPI_CLTS)
4867c478bd9Sstevel@tonic-gate 		taddr->qlen = 0;
4877c478bd9Sstevel@tonic-gate 	else
4887c478bd9Sstevel@tonic-gate 		taddr->qlen = listen_backlog;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
4917c478bd9Sstevel@tonic-gate 		/*
4927c478bd9Sstevel@tonic-gate 		 * Sm: If we are running then set SO_REUSEADDR option
4937c478bd9Sstevel@tonic-gate 		 * so that we can bind to our preferred address even if
4947c478bd9Sstevel@tonic-gate 		 * previous connections are in FIN_WAIT state
4957c478bd9Sstevel@tonic-gate 		 */
4967c478bd9Sstevel@tonic-gate #ifdef ND_DEBUG
4977c478bd9Sstevel@tonic-gate 		fprintf(stdout, "Setting SO_REUSEADDR.\n");
4987c478bd9Sstevel@tonic-gate #endif
4997c478bd9Sstevel@tonic-gate 		if (setopt_reuseaddr(fd) == -1) {
5007c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Couldn't set SO_REUSEADDR option");
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (t_bind(fd, taddr, baddr) != 0) {
5057c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot bind: %s",
5067c478bd9Sstevel@tonic-gate 		    nconf->nc_netid, t_errlist[t_errno]);
5077c478bd9Sstevel@tonic-gate 		goto error;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (memcmp(taddr->addr.buf, baddr->addr.buf, (int)baddr->addr.len)) {
5127c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: address in use", nconf->nc_netid);
5137c478bd9Sstevel@tonic-gate 		goto error;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, baddr, 0, 0);
5177c478bd9Sstevel@tonic-gate 	if (my_xprt == (SVCXPRT *)NULL) {
5187c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: could not create service",
5197c478bd9Sstevel@tonic-gate 		    nconf->nc_netid);
5207c478bd9Sstevel@tonic-gate 		goto error;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* set up multicast address for RPC CALL_IT, IPv6 */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
5267c478bd9Sstevel@tonic-gate 	    (strcmp(nconf->nc_proto, NC_UDP) == 0)) {
5277c478bd9Sstevel@tonic-gate 		if (setup_callit(fd) < 0) {
5287c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Unable to join IPv6 multicast group \
5297c478bd9Sstevel@tonic-gate for rpc broadcast %s", RPCB_MULTICAST_ADDR);
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5347c478bd9Sstevel@tonic-gate 			svc_control(my_xprt, SVCSET_KEEPALIVE, (void *) TRUE);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate #ifdef PORTMAP
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * Register both the versions for tcp/ip and udp/ip
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
5427c478bd9Sstevel@tonic-gate 	    ((strcmp(nconf->nc_proto, NC_TCP) == 0) ||
5437c478bd9Sstevel@tonic-gate 	    (strcmp(nconf->nc_proto, NC_UDP) == 0))) {
5447c478bd9Sstevel@tonic-gate 		PMAPLIST *pml;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
5477c478bd9Sstevel@tonic-gate 		    pmap_service, NULL)) {
5487c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "could not register on %s",
5497c478bd9Sstevel@tonic-gate 			    nconf->nc_netid);
5507c478bd9Sstevel@tonic-gate 			goto error;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5537c478bd9Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
5547c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
5557c478bd9Sstevel@tonic-gate 			exit(1);
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_prog = PMAPPROG;
5587c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = PMAPVERS;
5597c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_port = PMAPPORT;
5607c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5617c478bd9Sstevel@tonic-gate 			if (tcptrans[0]) {
5627c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
5637c478bd9Sstevel@tonic-gate 				"cannot have more than one TCP transport");
5647c478bd9Sstevel@tonic-gate 				goto error;
5657c478bd9Sstevel@tonic-gate 			}
5667c478bd9Sstevel@tonic-gate 			tcptrans = strdup(nconf->nc_netid);
5677c478bd9Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_TCP;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 			/* Let's snarf the universal address */
5707c478bd9Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
5717c478bd9Sstevel@tonic-gate 			tcp_uaddr = taddr2uaddr(nconf, &baddr->addr);
5727c478bd9Sstevel@tonic-gate 		} else {
5737c478bd9Sstevel@tonic-gate 			if (udptrans[0]) {
5747c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
5757c478bd9Sstevel@tonic-gate 				"cannot have more than one UDP transport");
5767c478bd9Sstevel@tonic-gate 				goto error;
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 			udptrans = strdup(nconf->nc_netid);
5797c478bd9Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_UDP;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 			/* Let's snarf the universal address */
5827c478bd9Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
5837c478bd9Sstevel@tonic-gate 			udp_uaddr = taddr2uaddr(nconf, &baddr->addr);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
5867c478bd9Sstevel@tonic-gate 		list_pml = pml;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		/* Add version 3 information */
5897c478bd9Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
5907c478bd9Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
5917c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
5927c478bd9Sstevel@tonic-gate 			exit(1);
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
5957c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS;
5967c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
5977c478bd9Sstevel@tonic-gate 		list_pml = pml;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		/* Add version 4 information */
6007c478bd9Sstevel@tonic-gate 		pml = (PMAPLIST *)malloc((uint_t)sizeof (PMAPLIST));
6017c478bd9Sstevel@tonic-gate 		if (pml == (PMAPLIST *)NULL) {
6027c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
6037c478bd9Sstevel@tonic-gate 			exit(1);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
6067c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS4;
6077c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
6087c478bd9Sstevel@tonic-gate 		list_pml = pml;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/* Also add version 2 stuff to rpcbind list */
6117c478bd9Sstevel@tonic-gate 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &baddr->addr);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate #endif
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/* version 3 registration */
6167c478bd9Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
6177c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 3",
6187c478bd9Sstevel@tonic-gate 		    nconf->nc_netid);
6197c478bd9Sstevel@tonic-gate 		goto error;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &baddr->addr);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/* version 4 registration */
6247c478bd9Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
6257c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 4",
6267c478bd9Sstevel@tonic-gate 		    nconf->nc_netid);
6277c478bd9Sstevel@tonic-gate 		goto error;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &baddr->addr);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
6327c478bd9Sstevel@tonic-gate 		if (nconf->nc_semantics == NC_TPI_CLTS)
6337c478bd9Sstevel@tonic-gate 			loopback_dg = strdup(nconf->nc_netid);
6347c478bd9Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS)
6357c478bd9Sstevel@tonic-gate 			loopback_vc = strdup(nconf->nc_netid);
6367c478bd9Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS_ORD)
6377c478bd9Sstevel@tonic-gate 			loopback_vc_ord = strdup(nconf->nc_netid);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	/* decide if bound checking works for this transport */
6417c478bd9Sstevel@tonic-gate 	status = add_bndlist(nconf, taddr, baddr);
6427c478bd9Sstevel@tonic-gate #ifdef BIND_DEBUG
6437c478bd9Sstevel@tonic-gate 	if (status < 0) {
6447c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Error in finding bind status for %s\n",
6457c478bd9Sstevel@tonic-gate 		    nconf->nc_netid);
6467c478bd9Sstevel@tonic-gate 	} else if (status == 0) {
6477c478bd9Sstevel@tonic-gate 		fprintf(stderr, "check binding for %s\n",
6487c478bd9Sstevel@tonic-gate 		    nconf->nc_netid);
6497c478bd9Sstevel@tonic-gate 	} else if (status > 0) {
6507c478bd9Sstevel@tonic-gate 		fprintf(stderr, "No check binding for %s\n",
6517c478bd9Sstevel@tonic-gate 		    nconf->nc_netid);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate #endif
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * rmtcall only supported on CLTS transports for now.
6567c478bd9Sstevel@tonic-gate 	 * only needed when we are allowing indirect calls
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	if (allow_indirect && nconf->nc_semantics == NC_TPI_CLTS) {
6597c478bd9Sstevel@tonic-gate 		status = create_rmtcall_fd(nconf);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate #ifdef BIND_DEBUG
6627c478bd9Sstevel@tonic-gate 		if (status < 0) {
6637c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Could not create rmtcall fd for %s\n",
6647c478bd9Sstevel@tonic-gate 			    nconf->nc_netid);
6657c478bd9Sstevel@tonic-gate 		} else {
6667c478bd9Sstevel@tonic-gate 			fprintf(stderr, "rmtcall fd for %s is %d\n",
6677c478bd9Sstevel@tonic-gate 			    nconf->nc_netid, status);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate #endif
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
6727c478bd9Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
6737c478bd9Sstevel@tonic-gate 	return (0);
6747c478bd9Sstevel@tonic-gate error:
6757c478bd9Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
6767c478bd9Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
6777c478bd9Sstevel@tonic-gate 	(void) t_close(fd);
6787c478bd9Sstevel@tonic-gate 	return (1);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate static void
6827c478bd9Sstevel@tonic-gate rbllist_add(ulong_t prog, ulong_t vers, struct netconfig *nconf,
6837c478bd9Sstevel@tonic-gate 	struct netbuf *addr)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	rpcblist_ptr rbl;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	rbl = (rpcblist_ptr)malloc((uint_t)sizeof (rpcblist));
6887c478bd9Sstevel@tonic-gate 	if (rbl == (rpcblist_ptr)NULL) {
6897c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory!");
6907c478bd9Sstevel@tonic-gate 		exit(1);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_prog = prog;
6947c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_vers = vers;
6957c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
6967c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
6977c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_owner = strdup(superuser);
6987c478bd9Sstevel@tonic-gate 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
6997c478bd9Sstevel@tonic-gate 	list_rbl = rbl;
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate  * Catch the signal and die
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate /* ARGSUSED */
7067c478bd9Sstevel@tonic-gate static void
7077c478bd9Sstevel@tonic-gate terminate(int sig)
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "rpcbind terminating on signal.");
7107c478bd9Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7117c478bd9Sstevel@tonic-gate 	exit(2);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
714064ed339Sjjj /* ARGSUSED */
715064ed339Sjjj static void
716064ed339Sjjj note_refresh(int sig)
717064ed339Sjjj {
718064ed339Sjjj 	sigrefresh = 1;
719064ed339Sjjj }
720064ed339Sjjj 
7217c478bd9Sstevel@tonic-gate void
7227c478bd9Sstevel@tonic-gate rpcbind_abort(void)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7257c478bd9Sstevel@tonic-gate 	abort();
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate /*
7297c478bd9Sstevel@tonic-gate  * detach from tty
7307c478bd9Sstevel@tonic-gate  */
7317c478bd9Sstevel@tonic-gate static void
7327c478bd9Sstevel@tonic-gate detachfromtty(void)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	close(0);
7357c478bd9Sstevel@tonic-gate 	close(1);
7367c478bd9Sstevel@tonic-gate 	close(2);
7377c478bd9Sstevel@tonic-gate 	switch (forkall()) {
7387c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
7397c478bd9Sstevel@tonic-gate 		perror("fork");
7407c478bd9Sstevel@tonic-gate 		break;
7417c478bd9Sstevel@tonic-gate 	case 0:
7427c478bd9Sstevel@tonic-gate 		break;
7437c478bd9Sstevel@tonic-gate 	default:
7447c478bd9Sstevel@tonic-gate 		exit(0);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	setsid();
7477c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
7487c478bd9Sstevel@tonic-gate 	dup(0);
7497c478bd9Sstevel@tonic-gate 	dup(0);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /* get command line options */
7537c478bd9Sstevel@tonic-gate static void
7547c478bd9Sstevel@tonic-gate parseargs(int argc, char *argv[])
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	int c;
7577c478bd9Sstevel@tonic-gate 	int tmp;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dwal:")) != EOF) {
7607c478bd9Sstevel@tonic-gate 		switch (c) {
7617c478bd9Sstevel@tonic-gate 		case 'd':
7627c478bd9Sstevel@tonic-gate 			debugging = 1;
7637c478bd9Sstevel@tonic-gate 			break;
7647c478bd9Sstevel@tonic-gate 		case 'a':
7657c478bd9Sstevel@tonic-gate 			doabort = 1;	/* when debugging, do an abort on */
7667c478bd9Sstevel@tonic-gate 			break;		/* errors; for rpcbind developers */
7677c478bd9Sstevel@tonic-gate 					/* only! */
7687c478bd9Sstevel@tonic-gate 		case 'w':
7697c478bd9Sstevel@tonic-gate 			warmstart = 1;
7707c478bd9Sstevel@tonic-gate 			break;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		case 'l':
7737c478bd9Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &tmp)) {
7747c478bd9Sstevel@tonic-gate 				if (tmp > listen_backlog) {
7757c478bd9Sstevel@tonic-gate 					listen_backlog = tmp;
7767c478bd9Sstevel@tonic-gate 				}
7777c478bd9Sstevel@tonic-gate 			}
7787c478bd9Sstevel@tonic-gate 			break;
7797c478bd9Sstevel@tonic-gate 		default:	/* error */
7807c478bd9Sstevel@tonic-gate 			fprintf(stderr,
7817c478bd9Sstevel@tonic-gate 			"usage: rpcbind [-d] [-w] [-l listen_backlog]\n");
7827c478bd9Sstevel@tonic-gate 			exit(1);
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	if (doabort && !debugging) {
7867c478bd9Sstevel@tonic-gate 		fprintf(stderr,
787*9e293969SRichard Lowe 		    "-a (abort) specified without -d "
788*9e293969SRichard Lowe 		    "(debugging) -- ignored.\n");
7897c478bd9Sstevel@tonic-gate 		doabort = 0;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate static int
79445916cd2Sjpk setopt_int(int fd, int level, int name, int value)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	struct t_optmgmt req, resp;
79745916cd2Sjpk 	struct {
79845916cd2Sjpk 		struct opthdr opt;
79945916cd2Sjpk 		int value;
80045916cd2Sjpk 	} optdata;
8017c478bd9Sstevel@tonic-gate 
80245916cd2Sjpk 	optdata.opt.level = level;
80345916cd2Sjpk 	optdata.opt.name = name;
80445916cd2Sjpk 	optdata.opt.len = sizeof (int);
8057c478bd9Sstevel@tonic-gate 
80645916cd2Sjpk 	optdata.value = value;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
80945916cd2Sjpk 	req.opt.len = sizeof (optdata);
81045916cd2Sjpk 	req.opt.buf = (char *)&optdata;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	resp.flags = 0;
81345916cd2Sjpk 	resp.opt.buf = (char *)&optdata;
81445916cd2Sjpk 	resp.opt.maxlen = sizeof (optdata);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
8177c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
8187c478bd9Sstevel@tonic-gate 		return (-1);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 	return (0);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate static int
82445916cd2Sjpk setopt_reuseaddr(int fd)
82545916cd2Sjpk {
82645916cd2Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_REUSEADDR, 1));
82745916cd2Sjpk }
82845916cd2Sjpk 
82945916cd2Sjpk static int
83045916cd2Sjpk setopt_anon_mlp(int fd)
83145916cd2Sjpk {
83245916cd2Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_ANON_MLP, 1));
83345916cd2Sjpk }
83445916cd2Sjpk 
83545916cd2Sjpk static int
8367c478bd9Sstevel@tonic-gate setup_callit(int fd)
8377c478bd9Sstevel@tonic-gate {
8387c478bd9Sstevel@tonic-gate 	struct ipv6_mreq mreq;
8397c478bd9Sstevel@tonic-gate 	struct t_optmgmt req, resp;
8407c478bd9Sstevel@tonic-gate 	struct opthdr *opt;
8417c478bd9Sstevel@tonic-gate 	char reqbuf[ sizeof (struct ipv6_mreq) + 24];
8427c478bd9Sstevel@tonic-gate 	struct ipv6_mreq *pmreq;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	opt = (struct opthdr *)reqbuf;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	opt->level = IPPROTO_IPV6;
8477c478bd9Sstevel@tonic-gate 	opt->name = IPV6_ADD_MEMBERSHIP;
8487c478bd9Sstevel@tonic-gate 	opt->len = sizeof (struct ipv6_mreq);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* multicast address */
8517c478bd9Sstevel@tonic-gate 	(void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR,
8527c478bd9Sstevel@tonic-gate 	    mreq.ipv6mr_multiaddr.s6_addr);
8537c478bd9Sstevel@tonic-gate 	mreq.ipv6mr_interface = 0;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/* insert it into opt */
8567c478bd9Sstevel@tonic-gate 	pmreq = (struct ipv6_mreq *)&reqbuf[sizeof (struct opthdr)];
8577c478bd9Sstevel@tonic-gate 	memcpy(pmreq, &mreq, sizeof (struct ipv6_mreq));
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
8607c478bd9Sstevel@tonic-gate 	req.opt.len = sizeof (struct opthdr) + opt->len;
8617c478bd9Sstevel@tonic-gate 	req.opt.buf = (char *)opt;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	resp.flags = 0;
8647c478bd9Sstevel@tonic-gate 	resp.opt.buf = reqbuf;
8657c478bd9Sstevel@tonic-gate 	resp.opt.maxlen = sizeof (reqbuf);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
8687c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
8697c478bd9Sstevel@tonic-gate 		return (-1);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	return (0);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate static boolean_t
8757c478bd9Sstevel@tonic-gate check_hostserv(struct netconfig *nconf, const char *host, const char *serv)
8767c478bd9Sstevel@tonic-gate {
8777c478bd9Sstevel@tonic-gate 	struct nd_hostserv nh;
8787c478bd9Sstevel@tonic-gate 	struct nd_addrlist *na;
8797c478bd9Sstevel@tonic-gate 	const char *hostname = host;
8807c478bd9Sstevel@tonic-gate 	const char *servname = serv;
8817c478bd9Sstevel@tonic-gate 	int retval;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	if (strcmp(host, HOST_SELF) == 0)
8847c478bd9Sstevel@tonic-gate 		hostname = "HOST_SELF";
8857c478bd9Sstevel@tonic-gate 	else if (strcmp(host, HOST_SELF_CONNECT) == 0)
8867c478bd9Sstevel@tonic-gate 		hostname = "HOST_SELF_CONNECT";
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (serv[0] == '\0')
8897c478bd9Sstevel@tonic-gate 		servname = "<any>";
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	nh.h_host = (char *)host;
8927c478bd9Sstevel@tonic-gate 	nh.h_serv = (char *)serv;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	retval = netdir_getbyname(nconf, &nh, &na);
8957c478bd9Sstevel@tonic-gate 	if (retval != ND_OK || na->n_cnt == 0) {
8967c478bd9Sstevel@tonic-gate 		if (retval == ND_OK)
8977c478bd9Sstevel@tonic-gate 			netdir_free(na, ND_ADDRLIST);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "netid %s: cannot find an address for host "
9007c478bd9Sstevel@tonic-gate 		    "%s, service \"%s\"", nconf->nc_netid, hostname, servname);
9017c478bd9Sstevel@tonic-gate 		if (debugging) {
9027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\tnetdir_getbyname for %s, "
9037c478bd9Sstevel@tonic-gate 			    "service \"%s\" failed\n", hostname, servname);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		return (B_FALSE);
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 	netdir_free(na, ND_ADDRLIST);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (debugging) {
9107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tnetdir_getbyname for %s, service "
9117c478bd9Sstevel@tonic-gate 		    "service \"%s\" succeeded\n", hostname, servname);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	return (B_TRUE);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate /* Maximum outstanding syslog requests */
9177c478bd9Sstevel@tonic-gate #define	MAXLOG		100
9187c478bd9Sstevel@tonic-gate /* Maximum length: the messages generated are fairly short; no hostnames. */
9197c478bd9Sstevel@tonic-gate #define	MAXMSG		128
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate typedef struct logmsg {
9227c478bd9Sstevel@tonic-gate 	struct logmsg	*log_next;
9237c478bd9Sstevel@tonic-gate 	int		log_pri;
9247c478bd9Sstevel@tonic-gate 	char		log_msg[MAXMSG];
9257c478bd9Sstevel@tonic-gate } logmsg;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate static logmsg *loghead = NULL;
9287c478bd9Sstevel@tonic-gate static logmsg **logtailp = &loghead;
9297c478bd9Sstevel@tonic-gate static mutex_t logmutex = DEFAULTMUTEX;
9307c478bd9Sstevel@tonic-gate static cond_t logcond = DEFAULTCV;
9317c478bd9Sstevel@tonic-gate static int logcount = 0;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
934*9e293969SRichard Lowe static void * __NORETURN
9357c478bd9Sstevel@tonic-gate logthread(void *arg)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	while (1) {
9387c478bd9Sstevel@tonic-gate 		logmsg *msg;
9397c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&logmutex);
9407c478bd9Sstevel@tonic-gate 		while ((msg = loghead) == NULL)
9417c478bd9Sstevel@tonic-gate 			(void) cond_wait(&logcond, &logmutex);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 		loghead = msg->log_next;
9447c478bd9Sstevel@tonic-gate 		logcount--;
9457c478bd9Sstevel@tonic-gate 		if (loghead == NULL) {
9467c478bd9Sstevel@tonic-gate 			logtailp = &loghead;
9477c478bd9Sstevel@tonic-gate 			logcount = 0;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&logmutex);
9507c478bd9Sstevel@tonic-gate 		syslog(msg->log_pri, "%s", msg->log_msg);
9517c478bd9Sstevel@tonic-gate 		free(msg);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate 
9560ea5e3a5Sjjj static boolean_t
9570ea5e3a5Sjjj get_smf_prop(const char *var, boolean_t def_val)
9580ea5e3a5Sjjj {
9590ea5e3a5Sjjj 	scf_simple_prop_t *prop;
9600ea5e3a5Sjjj 	uint8_t *val;
9610ea5e3a5Sjjj 	boolean_t res = def_val;
9620ea5e3a5Sjjj 
9630ea5e3a5Sjjj 	prop = scf_simple_prop_get(NULL, NULL, "config", var);
9640ea5e3a5Sjjj 	if (prop) {
9650ea5e3a5Sjjj 		if ((val = scf_simple_prop_next_boolean(prop)) != NULL)
9660ea5e3a5Sjjj 			res = (*val == 0) ? B_FALSE : B_TRUE;
9670ea5e3a5Sjjj 		scf_simple_prop_free(prop);
9680ea5e3a5Sjjj 	}
9690ea5e3a5Sjjj 
9700ea5e3a5Sjjj 	if (prop == NULL || val == NULL) {
9710ea5e3a5Sjjj 		syslog(LOG_ALERT, "no value for config/%s (%s). "
9720ea5e3a5Sjjj 		    "Using default \"%s\"", var, scf_strerror(scf_error()),
9730ea5e3a5Sjjj 		    def_val ? "true" : "false");
9740ea5e3a5Sjjj 	}
9750ea5e3a5Sjjj 
9760ea5e3a5Sjjj 	return (res);
9770ea5e3a5Sjjj }
9780ea5e3a5Sjjj 
9797c478bd9Sstevel@tonic-gate /*
980064ed339Sjjj  * Initialize: read the configuration parameters from SMF.
981064ed339Sjjj  * This function must be idempotent because it can be called from the
982064ed339Sjjj  * main poll() loop in my_svc_run().
9837c478bd9Sstevel@tonic-gate  */
984064ed339Sjjj void
9857c478bd9Sstevel@tonic-gate rpcb_check_init(void)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	thread_t tid;
988064ed339Sjjj 	static int thr_running;
9897c478bd9Sstevel@tonic-gate 
9900ea5e3a5Sjjj 	wrap_enabled = get_smf_prop("enable_tcpwrappers", B_FALSE);
9910ea5e3a5Sjjj 	verboselog = get_smf_prop("verbose_logging", B_FALSE);
9920ea5e3a5Sjjj 	allow_indirect = get_smf_prop("allow_indirect", B_TRUE);
9930ea5e3a5Sjjj 	local_only = get_smf_prop("local_only", B_FALSE);
9947c478bd9Sstevel@tonic-gate 
995064ed339Sjjj 	if (wrap_enabled && !thr_running) {
9967c478bd9Sstevel@tonic-gate 		(void) thr_create(NULL, 0, logthread, NULL, THR_DETACHED, &tid);
997064ed339Sjjj 		thr_running = 1;
998064ed339Sjjj 	}
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * qsyslog() - queue a request for syslog(); if syslog blocks, the other
10037c478bd9Sstevel@tonic-gate  * thread blocks; we make sure we don't run out of memory by allowing
10047c478bd9Sstevel@tonic-gate  * only a limited number of outstandig syslog() requests.
10057c478bd9Sstevel@tonic-gate  */
10067c478bd9Sstevel@tonic-gate void
10077c478bd9Sstevel@tonic-gate qsyslog(int pri, const char *fmt, ...)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	logmsg *msg = malloc(sizeof (*msg));
10107c478bd9Sstevel@tonic-gate 	int oldcount;
10117c478bd9Sstevel@tonic-gate 	va_list ap;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (msg == NULL)
10147c478bd9Sstevel@tonic-gate 		return;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	msg->log_pri = pri;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
10197c478bd9Sstevel@tonic-gate 	(void) vsnprintf(msg->log_msg, sizeof (msg->log_msg), fmt, ap);
10207c478bd9Sstevel@tonic-gate 	va_end(ap);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	msg->log_next = NULL;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&logmutex);
10257c478bd9Sstevel@tonic-gate 	oldcount = logcount;
10267c478bd9Sstevel@tonic-gate 	if (logcount < MAXLOG) {
10277c478bd9Sstevel@tonic-gate 		logcount++;
10287c478bd9Sstevel@tonic-gate 		*logtailp = msg;
10297c478bd9Sstevel@tonic-gate 		logtailp = &msg->log_next;
10307c478bd9Sstevel@tonic-gate 	} else {
10317c478bd9Sstevel@tonic-gate 		free(msg);
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&logmutex);
10347c478bd9Sstevel@tonic-gate 	if (oldcount == 0)
10357c478bd9Sstevel@tonic-gate 		(void) cond_signal(&logcond);
10367c478bd9Sstevel@tonic-gate }
1037