xref: /illumos-gate/usr/src/cmd/rpcbind/rpcbind.c (revision 05360118)
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  */
258f6d9daeSMarcel Telka /*
268f6d9daeSMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
278f6d9daeSMarcel Telka  */
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
418f6d9daeSMarcel Telka  * This is an implementation of RCPBIND according the RFC 1833: Binding
428f6d9daeSMarcel Telka  * Protocols for ONC RPC Version 2.  The RFC specifies three versions of the
438f6d9daeSMarcel Telka  * binding protocol:
448f6d9daeSMarcel Telka  *
458f6d9daeSMarcel Telka  * 1) RPCBIND Version 3 (Section 2.2.1 of the RFC)
468f6d9daeSMarcel Telka  * 2) RPCBIND, Version 4 (Section 2.2.2 of the RFC)
478f6d9daeSMarcel Telka  * 3) Port Mapper Program Protocol (Section 3 of the RFC)
488f6d9daeSMarcel Telka  *
498f6d9daeSMarcel Telka  * Where the "Port Mapper Program Protocol" is refered as Version 2 of the
508f6d9daeSMarcel Telka  * binding protocol.  The implementation of the Version 2 of the binding
518f6d9daeSMarcel Telka  * protocol is compiled in only in a case the PORTMAP macro is defined (by
528f6d9daeSMarcel Telka  * default it is defined).
538f6d9daeSMarcel Telka  *
548f6d9daeSMarcel Telka  * The implementation is based on top of the networking services library -
558f6d9daeSMarcel Telka  * libnsl(3lib) and uses Automatic MT mode (see rcp_control(3nsl) and
568f6d9daeSMarcel Telka  * svc_run(3nsl) for more details).
578f6d9daeSMarcel Telka  *
588f6d9daeSMarcel Telka  * Usually, when a thread handles an RPCBIND procedure (one that arrived from a
598f6d9daeSMarcel Telka  * client), it obtains the data for the response internally, and immediately
608f6d9daeSMarcel Telka  * sends the response back to the client.  The only exception to this rule are
618f6d9daeSMarcel Telka  * remote (aka indirect) RPC calls, for example RPCBPROC_INDIRECT.  Such
628f6d9daeSMarcel Telka  * procedures are designed to forward the RPC request from the client to some
638f6d9daeSMarcel Telka  * other RPC service specified by the client, wait for the result, and forward
648f6d9daeSMarcel Telka  * the result back to the client.  This is implemented in rpcbproc_callit_com().
657c478bd9Sstevel@tonic-gate  *
668f6d9daeSMarcel Telka  * The response from the other (remote) RPC service is handled in
678f6d9daeSMarcel Telka  * handle_reply(), where the thread waiting in rpcbproc_callit_com() is woken
688f6d9daeSMarcel Telka  * up to finish the handling and to send (forward) the response back to the
698f6d9daeSMarcel Telka  * client.
708f6d9daeSMarcel Telka  *
718f6d9daeSMarcel Telka  * The thread implementing the indirect RPC call might be blocked in the
728f6d9daeSMarcel Telka  * rpcbproc_callit_com() waiting for the response from the other RPC service
738f6d9daeSMarcel Telka  * for very long time.  During this time the thread is unable to handle other
748f6d9daeSMarcel Telka  * RPCBIND requests.  To avoid a case when all threads are waiting in
758f6d9daeSMarcel Telka  * rpcbproc_callit_com() and there is no free thread able to handle other
768f6d9daeSMarcel Telka  * RPCBIND requests, the implementation has reserved eight threads to never be
778f6d9daeSMarcel Telka  * used for the remote RPC calls.  The number of active remote RPC calls is in
788f6d9daeSMarcel Telka  * rpcb_rmtcalls, the upper limit of such calls is in rpcb_rmtcalls_max.
798f6d9daeSMarcel Telka  *
808f6d9daeSMarcel Telka  * In addition to the worker threads described above, there are two other
818f6d9daeSMarcel Telka  * threads.  The logthread() thread is responsible for asynchronous logging to
828f6d9daeSMarcel Telka  * syslog.  The terminate() thread is signal handler responsible for reload of
838f6d9daeSMarcel Telka  * the rpcbind configuration (on SIGHUP), or for gracefully shutting down
848f6d9daeSMarcel Telka  * rpcbind (otherwise).
858f6d9daeSMarcel Telka  *
868f6d9daeSMarcel Telka  * There are two global lists used for holding the information about the
878f6d9daeSMarcel Telka  * registered services: list_rbl is for Version 3 and 4 of the binding
888f6d9daeSMarcel Telka  * protocol, and list_pml is for Version 2.  To protect these lists, two global
898f6d9daeSMarcel Telka  * readers/writer locks are defined and heavily used across the rpcbind
908f6d9daeSMarcel Telka  * implementation: list_rbl_lock protecting list_rbl, and list_pml_lock,
918f6d9daeSMarcel Telka  * protecting list_pml.
928f6d9daeSMarcel Telka  *
938f6d9daeSMarcel Telka  * The defined locking order is: list_rbl_lock first, list_pml_lock second.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #include <dlfcn.h>
977c478bd9Sstevel@tonic-gate #include <stdio.h>
98004388ebScasper #include <stdio_ext.h>
997c478bd9Sstevel@tonic-gate #include <sys/types.h>
1007c478bd9Sstevel@tonic-gate #include <unistd.h>
1017c478bd9Sstevel@tonic-gate #include <stdlib.h>
1027c478bd9Sstevel@tonic-gate #include <string.h>
1037c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
1047c478bd9Sstevel@tonic-gate #include <netconfig.h>
1057c478bd9Sstevel@tonic-gate #include <netdir.h>
1067c478bd9Sstevel@tonic-gate #include <errno.h>
1077c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1087c478bd9Sstevel@tonic-gate #include <signal.h>
1097c478bd9Sstevel@tonic-gate #include <string.h>
1107c478bd9Sstevel@tonic-gate #include <stdlib.h>
1117c478bd9Sstevel@tonic-gate #include <thread.h>
1127c478bd9Sstevel@tonic-gate #include <synch.h>
1137c478bd9Sstevel@tonic-gate #include <stdarg.h>
1147c478bd9Sstevel@tonic-gate #ifdef PORTMAP
1157c478bd9Sstevel@tonic-gate #include <netinet/in.h>
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
1187c478bd9Sstevel@tonic-gate #include <sys/termios.h>
1197c478bd9Sstevel@tonic-gate #include "rpcbind.h"
1207c478bd9Sstevel@tonic-gate #include <sys/syslog.h>
1217c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1227c478bd9Sstevel@tonic-gate #include <syslog.h>
1237c478bd9Sstevel@tonic-gate #include <string.h>
1247c478bd9Sstevel@tonic-gate #include <sys/time.h>
1257c478bd9Sstevel@tonic-gate #include <sys/resource.h>
1267c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
1277c478bd9Sstevel@tonic-gate #include <priv_utils.h>
1287c478bd9Sstevel@tonic-gate #include <libscf.h>
1299e293969SRichard Lowe #include <sys/ccompile.h>
1308f6d9daeSMarcel Telka #include <zone.h>
1318f6d9daeSMarcel Telka #include <ctype.h>
1328f6d9daeSMarcel Telka #include <limits.h>
1338f6d9daeSMarcel Telka #include <assert.h>
1347c478bd9Sstevel@tonic-gate 
1358f6d9daeSMarcel Telka static sigset_t sigwaitset;
1367c478bd9Sstevel@tonic-gate 
1378f6d9daeSMarcel Telka static void terminate(void);
1387c478bd9Sstevel@tonic-gate static void detachfromtty(void);
1397c478bd9Sstevel@tonic-gate static void parseargs(int, char *[]);
1407c478bd9Sstevel@tonic-gate static void rbllist_add(ulong_t, ulong_t, struct netconfig *, struct netbuf *);
1417c478bd9Sstevel@tonic-gate static int init_transport(struct netconfig *);
1427c478bd9Sstevel@tonic-gate static int check_netconfig(void);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static boolean_t check_hostserv(struct netconfig *, const char *, const char *);
14545916cd2Sjpk static int setopt_reuseaddr(int);
14645916cd2Sjpk static int setopt_anon_mlp(int);
14745916cd2Sjpk static int setup_callit(int);
1487c478bd9Sstevel@tonic-gate 
1498f6d9daeSMarcel Telka static void rpcb_check_init(void);
1508f6d9daeSMarcel Telka 
1517c478bd9Sstevel@tonic-gate /* Global variables */
1527c478bd9Sstevel@tonic-gate int debugging = 0;	/* Tell me what's going on */
1537c478bd9Sstevel@tonic-gate static int ipv6flag = 0;
1547c478bd9Sstevel@tonic-gate int doabort = 0;	/* When debugging, do an abort on errors */
1558f6d9daeSMarcel Telka static int listen_backlog;
1568f6d9daeSMarcel Telka static const int reserved_threads = 8;
1578f6d9daeSMarcel Telka 
1588f6d9daeSMarcel Telka /*
1598f6d9daeSMarcel Telka  * list_rbl_lock protects list_rbl
1608f6d9daeSMarcel Telka  * lock order: list_rbl_lock, list_pml_lock
1618f6d9daeSMarcel Telka  */
1628f6d9daeSMarcel Telka rwlock_t list_rbl_lock = DEFAULTRWLOCK;
1637c478bd9Sstevel@tonic-gate rpcblist_ptr list_rbl;	/* A list of version 3/4 rpcbind services */
1648f6d9daeSMarcel Telka 
1657c478bd9Sstevel@tonic-gate char *loopback_dg;	/* Datagram loopback transport, for set and unset */
1667c478bd9Sstevel@tonic-gate char *loopback_vc;	/* COTS loopback transport, for set and unset */
1677c478bd9Sstevel@tonic-gate char *loopback_vc_ord;	/* COTS_ORD loopback transport, for set and unset */
1687c478bd9Sstevel@tonic-gate 
1698f6d9daeSMarcel Telka volatile boolean_t verboselog = B_FALSE;
1708f6d9daeSMarcel Telka volatile boolean_t wrap_enabled = B_FALSE;
1718f6d9daeSMarcel Telka volatile boolean_t allow_indirect = B_TRUE;
1728f6d9daeSMarcel Telka volatile boolean_t local_only = B_TRUE;
173064ed339Sjjj 
1747c478bd9Sstevel@tonic-gate /* Local Variable */
1757c478bd9Sstevel@tonic-gate static int warmstart = 0;	/* Grab a old copy of registrations */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #ifdef PORTMAP
1788f6d9daeSMarcel Telka /*
1798f6d9daeSMarcel Telka  * list_pml_lock protects list_pml
1808f6d9daeSMarcel Telka  * lock order: list_rbl_lock, list_pml_lock
1818f6d9daeSMarcel Telka  */
1828f6d9daeSMarcel Telka rwlock_t list_pml_lock = DEFAULTRWLOCK;
1837c478bd9Sstevel@tonic-gate PMAPLIST *list_pml;	/* A list of version 2 rpcbind services */
1848f6d9daeSMarcel Telka 
1857c478bd9Sstevel@tonic-gate char *udptrans;		/* Name of UDP transport */
1867c478bd9Sstevel@tonic-gate char *tcptrans;		/* Name of TCP transport */
1877c478bd9Sstevel@tonic-gate char *udp_uaddr;	/* Universal UDP address */
1887c478bd9Sstevel@tonic-gate char *tcp_uaddr;	/* Universal TCP address */
1897c478bd9Sstevel@tonic-gate #endif
1907c478bd9Sstevel@tonic-gate static char servname[] = "rpcbind";
1917c478bd9Sstevel@tonic-gate static char superuser[] = "superuser";
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static const char daemon_dir[] = DAEMON_DIR;
1947c478bd9Sstevel@tonic-gate 
1958f6d9daeSMarcel Telka static void
block_signals(void)1968f6d9daeSMarcel Telka block_signals(void)
1978f6d9daeSMarcel Telka {
1988f6d9daeSMarcel Telka 	(void) sigemptyset(&sigwaitset);
1998f6d9daeSMarcel Telka 	(void) sigaddset(&sigwaitset, SIGINT);
2008f6d9daeSMarcel Telka 	(void) sigaddset(&sigwaitset, SIGTERM);
2018f6d9daeSMarcel Telka 	(void) sigaddset(&sigwaitset, SIGQUIT);
2028f6d9daeSMarcel Telka 	(void) sigaddset(&sigwaitset, SIGHUP);
2038f6d9daeSMarcel Telka 	(void) sigprocmask(SIG_BLOCK, &sigwaitset, NULL);
2048f6d9daeSMarcel Telka 
2058f6d9daeSMarcel Telka 	/* ignore other signals that could get sent */
2068f6d9daeSMarcel Telka 	(void) signal(SIGUSR1, SIG_IGN);
2078f6d9daeSMarcel Telka 	(void) signal(SIGUSR2, SIG_IGN);
2088f6d9daeSMarcel Telka }
2098f6d9daeSMarcel Telka 
2107c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2117c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
2147c478bd9Sstevel@tonic-gate 	void *nc_handle;	/* Net config handle */
2157c478bd9Sstevel@tonic-gate 	struct rlimit rl;
2168f6d9daeSMarcel Telka 	int rpc_svc_fdunlim = 1;
2178f6d9daeSMarcel Telka 	int rpc_svc_mode = RPC_SVC_MT_AUTO;
2187c478bd9Sstevel@tonic-gate 	int maxrecsz = RPC_MAXDATASIZE;
21945916cd2Sjpk 	boolean_t can_do_mlp;
2207c478bd9Sstevel@tonic-gate 
2218f6d9daeSMarcel Telka 	block_signals();
2227c478bd9Sstevel@tonic-gate 
2238f6d9daeSMarcel Telka 	parseargs(argc, argv);
2247c478bd9Sstevel@tonic-gate 
2258f6d9daeSMarcel Telka 	if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
2268f6d9daeSMarcel Telka 		syslog(LOG_ERR, "getrlimit failed");
2278f6d9daeSMarcel Telka 	} else {
2288f6d9daeSMarcel Telka 		rl.rlim_cur = rl.rlim_max;
2298f6d9daeSMarcel Telka 		if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
2308f6d9daeSMarcel Telka 			syslog(LOG_ERR, "setrlimit failed");
2317c478bd9Sstevel@tonic-gate 	}
232004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
233004388ebScasper 
2347c478bd9Sstevel@tonic-gate 	openlog("rpcbind", LOG_CONS, LOG_DAEMON);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Create the daemon directory in /var/run
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	if (mkdir(daemon_dir, DAEMON_DIR_MODE) == 0 || errno == EEXIST) {
2407c478bd9Sstevel@tonic-gate 		chmod(daemon_dir, DAEMON_DIR_MODE);
2417c478bd9Sstevel@tonic-gate 		chown(daemon_dir, DAEMON_UID, DAEMON_GID);
2427c478bd9Sstevel@tonic-gate 	} else {
2437c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "failed to create \"%s\": %m", daemon_dir);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * These privileges are required for the t_bind check rpcbind uses
2487c478bd9Sstevel@tonic-gate 	 * to determine whether a service is still live or not.
2497c478bd9Sstevel@tonic-gate 	 */
25045916cd2Sjpk 	can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
2517c478bd9Sstevel@tonic-gate 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, DAEMON_UID,
25245916cd2Sjpk 	    DAEMON_GID, PRIV_NET_PRIVADDR, PRIV_SYS_NFS,
25345916cd2Sjpk 	    can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) {
2547c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Insufficient privileges\n");
2557c478bd9Sstevel@tonic-gate 		exit(1);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2588f6d9daeSMarcel Telka 	myzone = getzoneid();
2598f6d9daeSMarcel Telka 
2608f6d9daeSMarcel Telka 	/*
2618f6d9daeSMarcel Telka 	 * Set number of file descriptors to unlimited
2628f6d9daeSMarcel Telka 	 */
2638f6d9daeSMarcel Telka 	if (!rpc_control(RPC_SVC_USE_POLLFD, &rpc_svc_fdunlim)) {
2648f6d9daeSMarcel Telka 		syslog(LOG_INFO, "unable to set number of FD to unlimited");
2658f6d9daeSMarcel Telka 	}
2668f6d9daeSMarcel Telka 
2678f6d9daeSMarcel Telka 	/*
2688f6d9daeSMarcel Telka 	 * Tell RPC that we want automatic thread mode.
2698f6d9daeSMarcel Telka 	 * A new thread will be spawned for each request.
2708f6d9daeSMarcel Telka 	 */
2718f6d9daeSMarcel Telka 	if (!rpc_control(RPC_SVC_MTMODE_SET, &rpc_svc_mode)) {
2728f6d9daeSMarcel Telka 		syslog(LOG_ERR, "unable to set automatic MT mode");
2738f6d9daeSMarcel Telka 		exit(1);
2748f6d9daeSMarcel Telka 	}
2758f6d9daeSMarcel Telka 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Enable non-blocking mode and maximum record size checks for
2787c478bd9Sstevel@tonic-gate 	 * connection oriented transports.
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
2817c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, "unable to set RPC max record size");
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	{
2857c478bd9Sstevel@tonic-gate 		/*
2867c478bd9Sstevel@tonic-gate 		 * rpcbind is the first application to encounter the
2877c478bd9Sstevel@tonic-gate 		 * various netconfig files.  check_netconfig() verifies
2887c478bd9Sstevel@tonic-gate 		 * that they are set up correctly and complains loudly
2897c478bd9Sstevel@tonic-gate 		 * if not.
2907c478bd9Sstevel@tonic-gate 		 */
2917c478bd9Sstevel@tonic-gate 		int trouble;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		trouble = check_netconfig();
2947c478bd9Sstevel@tonic-gate 		if (trouble) {
2958f6d9daeSMarcel Telka 			syslog(LOG_ERR, "%s: found %d errors with network "
2968f6d9daeSMarcel Telka 			    "configuration files. Exiting.", argv[0], trouble);
2978f6d9daeSMarcel Telka 			fprintf(stderr, "%s: found %d errors with network "
2988f6d9daeSMarcel Telka 			    "configuration files. Exiting.\n",
2999e293969SRichard Lowe 			    argv[0], trouble);
3007c478bd9Sstevel@tonic-gate 			exit(1);
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	}
3038f6d9daeSMarcel Telka 
3048f6d9daeSMarcel Telka 	loopback_dg = "";
3058f6d9daeSMarcel Telka 	loopback_vc = "";
3068f6d9daeSMarcel Telka 	loopback_vc_ord = "";
3078f6d9daeSMarcel Telka #ifdef PORTMAP
3088f6d9daeSMarcel Telka 	udptrans = "";
3098f6d9daeSMarcel Telka 	tcptrans = "";
3108f6d9daeSMarcel Telka #endif
3118f6d9daeSMarcel Telka 
3127c478bd9Sstevel@tonic-gate 	ipv6flag = Is_ipv6present();
3137c478bd9Sstevel@tonic-gate 	rpcb_check_init();
3148f6d9daeSMarcel Telka 
315*05360118SToomas Soome 	nc_handle = setnetconfig();	/* open netconfig file */
3168f6d9daeSMarcel Telka 	if (nc_handle == NULL) {
3178f6d9daeSMarcel Telka 		syslog(LOG_ERR, "could not read /etc/netconfig");
3188f6d9daeSMarcel Telka 		exit(1);
3198f6d9daeSMarcel Telka 	}
3208f6d9daeSMarcel Telka 	while ((nconf = getnetconfig(nc_handle)) != NULL) {
3217c478bd9Sstevel@tonic-gate 		if (nconf->nc_flag & NC_VISIBLE)
3227c478bd9Sstevel@tonic-gate 			init_transport(nconf);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	endnetconfig(nc_handle);
3257c478bd9Sstevel@tonic-gate 
326*05360118SToomas Soome 	if ((loopback_dg[0] == '\0') && (loopback_vc[0] == '\0') &&
327*05360118SToomas Soome 	    (loopback_vc_ord[0] == '\0')) {
3287c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not find loopback transports");
3297c478bd9Sstevel@tonic-gate 		exit(1);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (warmstart) {
3337c478bd9Sstevel@tonic-gate 		read_warmstart();
3347c478bd9Sstevel@tonic-gate 	}
3358f6d9daeSMarcel Telka 
3368f6d9daeSMarcel Telka 	/* Create terminate signal handler for graceful exit */
3378f6d9daeSMarcel Telka 	if (thr_create(NULL, 0, (void *(*)(void *))terminate, NULL, 0, NULL)) {
3388f6d9daeSMarcel Telka 		syslog(LOG_ERR, "Failed to create terminate thread");
3398f6d9daeSMarcel Telka 		exit(1);
3408f6d9daeSMarcel Telka 	}
3418f6d9daeSMarcel Telka 
3427c478bd9Sstevel@tonic-gate 	if (debugging) {
3437c478bd9Sstevel@tonic-gate 		printf("rpcbind debugging enabled.");
3447c478bd9Sstevel@tonic-gate 		if (doabort) {
3457c478bd9Sstevel@tonic-gate 			printf("  Will abort on errors!\n");
3467c478bd9Sstevel@tonic-gate 		} else {
3477c478bd9Sstevel@tonic-gate 			printf("\n");
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	} else {
3507c478bd9Sstevel@tonic-gate 		detachfromtty();
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* These are basic privileges we do not need */
3547c478bd9Sstevel@tonic-gate 	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
3557c478bd9Sstevel@tonic-gate 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
3567c478bd9Sstevel@tonic-gate 
3578f6d9daeSMarcel Telka 	svc_run();
3587c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "svc_run returned unexpectedly");
3597c478bd9Sstevel@tonic-gate 	rpcbind_abort();
3607c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * Increments a counter each time a problem is found with the network
3657c478bd9Sstevel@tonic-gate  * configuration information.
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate static int
check_netconfig(void)3687c478bd9Sstevel@tonic-gate check_netconfig(void)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	void	*nc;
3717c478bd9Sstevel@tonic-gate 	void	*dlcookie;
3727c478bd9Sstevel@tonic-gate 	int	busted = 0;
3737c478bd9Sstevel@tonic-gate 	int	i;
3747c478bd9Sstevel@tonic-gate 	int	lo_clts_found = 0, lo_cots_found = 0, lo_cotsord_found = 0;
3757c478bd9Sstevel@tonic-gate 	struct netconfig	*nconf, *np;
3767c478bd9Sstevel@tonic-gate 	struct stat	sb;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	nc = setnetconfig();
3797c478bd9Sstevel@tonic-gate 	if (nc == NULL) {
3807c478bd9Sstevel@tonic-gate 		if (debugging)
3817c478bd9Sstevel@tonic-gate 			fprintf(stderr,
3829e293969SRichard Lowe 			    "setnetconfig() failed:  %s\n", nc_sperror());
3837c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "setnetconfig() failed:  %s", nc_sperror());
3847c478bd9Sstevel@tonic-gate 		return (1);
3857c478bd9Sstevel@tonic-gate 	}
3868f6d9daeSMarcel Telka 	while ((np = getnetconfig(nc)) != NULL) {
3877c478bd9Sstevel@tonic-gate 		if ((np->nc_flag & NC_VISIBLE) == 0)
3887c478bd9Sstevel@tonic-gate 			continue;
3897c478bd9Sstevel@tonic-gate 		if (debugging)
3907c478bd9Sstevel@tonic-gate 			fprintf(stderr, "checking netid \"%s\"\n",
3919e293969SRichard Lowe 			    np->nc_netid);
3927c478bd9Sstevel@tonic-gate 		if (strcmp(np->nc_protofmly, NC_LOOPBACK) == 0)
3937c478bd9Sstevel@tonic-gate 			switch (np->nc_semantics) {
3947c478bd9Sstevel@tonic-gate 			case NC_TPI_CLTS:
3957c478bd9Sstevel@tonic-gate 				lo_clts_found = 1;
3967c478bd9Sstevel@tonic-gate 				break;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 			case NC_TPI_COTS:
3997c478bd9Sstevel@tonic-gate 				lo_cots_found = 1;
4007c478bd9Sstevel@tonic-gate 				break;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 			case NC_TPI_COTS_ORD:
4037c478bd9Sstevel@tonic-gate 				lo_cotsord_found = 1;
4047c478bd9Sstevel@tonic-gate 				break;
4057c478bd9Sstevel@tonic-gate 			}
4067c478bd9Sstevel@tonic-gate 		if (stat(np->nc_device, &sb) == -1 && errno == ENOENT) {
4077c478bd9Sstevel@tonic-gate 			if (debugging)
4087c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\tdevice %s does not exist\n",
4099e293969SRichard Lowe 				    np->nc_device);
4107c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "netid %s:  device %s does not exist",
4119e293969SRichard Lowe 			    np->nc_netid, np->nc_device);
4127c478bd9Sstevel@tonic-gate 			busted++;
4137c478bd9Sstevel@tonic-gate 		} else
4147c478bd9Sstevel@tonic-gate 			if (debugging)
4157c478bd9Sstevel@tonic-gate 				fprintf(stderr, "\tdevice %s present\n",
4169e293969SRichard Lowe 				    np->nc_device);
4177c478bd9Sstevel@tonic-gate 		for (i = 0; i < np->nc_nlookups; i++) {
4187c478bd9Sstevel@tonic-gate 			char	*libname = np->nc_lookups[i];
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			if ((dlcookie = dlopen(libname, RTLD_LAZY)) == NULL) {
4217c478bd9Sstevel@tonic-gate 				char *dlerrstr;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 				dlerrstr = dlerror();
4247c478bd9Sstevel@tonic-gate 				if (debugging) {
4258f6d9daeSMarcel Telka 					fprintf(stderr, "\tnetid %s: dlopen of "
4268f6d9daeSMarcel Telka 					    "name-to-address library %s "
4278f6d9daeSMarcel Telka 					    "failed\ndlerror: %s",
4289e293969SRichard Lowe 					    np->nc_netid, libname,
4299e293969SRichard Lowe 					    dlerrstr ? dlerrstr : "");
4307c478bd9Sstevel@tonic-gate 				}
4318f6d9daeSMarcel Telka 				syslog(LOG_ERR, "netid %s:  dlopen of "
4328f6d9daeSMarcel Telka 				    "name-to-address library %s failed",
4339e293969SRichard Lowe 				    np->nc_netid, libname);
4347c478bd9Sstevel@tonic-gate 				if (dlerrstr)
4357c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "%s", dlerrstr);
4367c478bd9Sstevel@tonic-gate 				busted++;
4377c478bd9Sstevel@tonic-gate 			} else {
4387c478bd9Sstevel@tonic-gate 				if (debugging)
4398f6d9daeSMarcel Telka 					fprintf(stderr, "\tdlopen of "
4408f6d9daeSMarcel Telka 					    "name-to-address library %s "
4418f6d9daeSMarcel Telka 					    "succeeded\n", libname);
4427c478bd9Sstevel@tonic-gate 				(void) dlclose(dlcookie);
4437c478bd9Sstevel@tonic-gate 			}
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 		nconf = getnetconfigent(np->nc_netid);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, ""))
4487c478bd9Sstevel@tonic-gate 			busted++;
4497c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, ""))
4507c478bd9Sstevel@tonic-gate 			busted++;
4517c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF, "rpcbind"))
4527c478bd9Sstevel@tonic-gate 			busted++;
4537c478bd9Sstevel@tonic-gate 		if (!check_hostserv(nconf, HOST_SELF_CONNECT, "rpcbind"))
4547c478bd9Sstevel@tonic-gate 			busted++;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 	endnetconfig(nc);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (lo_clts_found) {
4617c478bd9Sstevel@tonic-gate 		if (debugging)
4627c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found CLTS loopback transport\n");
4637c478bd9Sstevel@tonic-gate 	} else {
4647c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no CLTS loopback transport found\n");
4657c478bd9Sstevel@tonic-gate 		if (debugging)
4667c478bd9Sstevel@tonic-gate 			fprintf(stderr, "no CLTS loopback transport found\n");
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	if (lo_cots_found) {
4697c478bd9Sstevel@tonic-gate 		if (debugging)
4707c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found COTS loopback transport\n");
4717c478bd9Sstevel@tonic-gate 	} else {
4727c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS loopback transport found\n");
4737c478bd9Sstevel@tonic-gate 		if (debugging)
4747c478bd9Sstevel@tonic-gate 			fprintf(stderr, "no COTS loopback transport found\n");
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	if (lo_cotsord_found) {
4777c478bd9Sstevel@tonic-gate 		if (debugging)
4787c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Found COTS ORD loopback transport\n");
4797c478bd9Sstevel@tonic-gate 	} else {
4807c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "no COTS ORD loopback transport found\n");
4817c478bd9Sstevel@tonic-gate 		if (debugging)
4827c478bd9Sstevel@tonic-gate 			fprintf(stderr,
4839e293969SRichard Lowe 			    "no COTS ORD loopback transport found\n");
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	return (busted);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * Adds the entry into the rpcbind database.
4917c478bd9Sstevel@tonic-gate  * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
4927c478bd9Sstevel@tonic-gate  * Returns 0 if succeeds, else fails
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate static int
init_transport(struct netconfig * nconf)4957c478bd9Sstevel@tonic-gate init_transport(struct netconfig *nconf)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	int fd;
4987c478bd9Sstevel@tonic-gate 	struct t_bind *taddr, *baddr;
4997c478bd9Sstevel@tonic-gate 	SVCXPRT	*my_xprt;
5007c478bd9Sstevel@tonic-gate 	struct nd_addrlist *nas;
5017c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
5027c478bd9Sstevel@tonic-gate 	static int msgprt = 0;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
5059e293969SRichard Lowe 	    (nconf->nc_semantics != NC_TPI_COTS) &&
5069e293969SRichard Lowe 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
5077c478bd9Sstevel@tonic-gate 		return (1);	/* not my type */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) && !ipv6flag) {
5107c478bd9Sstevel@tonic-gate 		if (!msgprt)
5118f6d9daeSMarcel Telka 			syslog(LOG_DEBUG, "/etc/netconfig has IPv6 entries but "
5128f6d9daeSMarcel Telka 			    "IPv6 is not configured");
5137c478bd9Sstevel@tonic-gate 		msgprt++;
5147c478bd9Sstevel@tonic-gate 		return (1);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
5187c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot open connection: %s",
5199e293969SRichard Lowe 		    nconf->nc_netid, t_errlist[t_errno]);
5207c478bd9Sstevel@tonic-gate 		return (1);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
52345916cd2Sjpk 	if (is_system_labeled() &&
52445916cd2Sjpk 	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
52545916cd2Sjpk 	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
52645916cd2Sjpk 	    setopt_anon_mlp(fd) == -1) {
52745916cd2Sjpk 		syslog(LOG_ERR, "%s: couldn't set SO_ANON_MLP option",
52845916cd2Sjpk 		    nconf->nc_netid);
52945916cd2Sjpk 	}
53045916cd2Sjpk 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Negotiate for returning the ucred of the caller. This should
5337c478bd9Sstevel@tonic-gate 	 * done before enabling the endpoint for service via
5347c478bd9Sstevel@tonic-gate 	 * t_bind() so that requests to rpcbind contain the uid.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	svc_fd_negotiate_ucred(fd);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	taddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
5397c478bd9Sstevel@tonic-gate 	baddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
5407c478bd9Sstevel@tonic-gate 	if ((baddr == NULL) || (taddr == NULL)) {
5417c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot allocate netbuf: %s",
5429e293969SRichard Lowe 		    nconf->nc_netid, t_errlist[t_errno]);
5437c478bd9Sstevel@tonic-gate 		exit(1);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/* Get rpcbind's address on this transport */
5477c478bd9Sstevel@tonic-gate 	hs.h_host = HOST_SELF;
5487c478bd9Sstevel@tonic-gate 	hs.h_serv = servname;
5497c478bd9Sstevel@tonic-gate 	if (netdir_getbyname(nconf, &hs, &nas))
5507c478bd9Sstevel@tonic-gate 		goto error;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* Copy the address */
5537c478bd9Sstevel@tonic-gate 	taddr->addr.len = nas->n_addrs->len;
5547c478bd9Sstevel@tonic-gate 	memcpy(taddr->addr.buf, nas->n_addrs->buf, (int)nas->n_addrs->len);
5557c478bd9Sstevel@tonic-gate 	netdir_free((char *)nas, ND_ADDRLIST);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (nconf->nc_semantics == NC_TPI_CLTS)
5587c478bd9Sstevel@tonic-gate 		taddr->qlen = 0;
5597c478bd9Sstevel@tonic-gate 	else
5607c478bd9Sstevel@tonic-gate 		taddr->qlen = listen_backlog;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * Sm: If we are running then set SO_REUSEADDR option
5657c478bd9Sstevel@tonic-gate 		 * so that we can bind to our preferred address even if
5667c478bd9Sstevel@tonic-gate 		 * previous connections are in FIN_WAIT state
5677c478bd9Sstevel@tonic-gate 		 */
5687c478bd9Sstevel@tonic-gate 		if (setopt_reuseaddr(fd) == -1) {
5697c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Couldn't set SO_REUSEADDR option");
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if (t_bind(fd, taddr, baddr) != 0) {
5747c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: cannot bind: %s",
5759e293969SRichard Lowe 		    nconf->nc_netid, t_errlist[t_errno]);
5767c478bd9Sstevel@tonic-gate 		goto error;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5798f6d9daeSMarcel Telka 	if (nconf->nc_semantics != NC_TPI_CLTS && taddr->qlen != baddr->qlen)
5808f6d9daeSMarcel Telka 		syslog(LOG_NOTICE, "%s: unable to set listen backlog to %d "
5818f6d9daeSMarcel Telka 		    "(negotiated: %d)", nconf->nc_netid, taddr->qlen,
5828f6d9daeSMarcel Telka 		    baddr->qlen);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (memcmp(taddr->addr.buf, baddr->addr.buf, (int)baddr->addr.len)) {
5857c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: address in use", nconf->nc_netid);
5867c478bd9Sstevel@tonic-gate 		goto error;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5898f6d9daeSMarcel Telka 	my_xprt = svc_tli_create(fd, nconf, baddr, 0, 0);
5908f6d9daeSMarcel Telka 	if (my_xprt == NULL) {
5917c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: could not create service",
5929e293969SRichard Lowe 		    nconf->nc_netid);
5937c478bd9Sstevel@tonic-gate 		goto error;
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	/* set up multicast address for RPC CALL_IT, IPv6 */
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
5997c478bd9Sstevel@tonic-gate 	    (strcmp(nconf->nc_proto, NC_UDP) == 0)) {
6007c478bd9Sstevel@tonic-gate 		if (setup_callit(fd) < 0) {
6018f6d9daeSMarcel Telka 			syslog(LOG_ERR, "Unable to join IPv6 multicast group "
6028f6d9daeSMarcel Telka 			    "for rpc broadcast %s", RPCB_MULTICAST_ADDR);
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
6078f6d9daeSMarcel Telka 		svc_control(my_xprt, SVCSET_KEEPALIVE, (void *) TRUE);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate #ifdef PORTMAP
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Register both the versions for tcp/ip and udp/ip
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	if ((strcmp(nconf->nc_protofmly, NC_INET) == 0) &&
6159e293969SRichard Lowe 	    ((strcmp(nconf->nc_proto, NC_TCP) == 0) ||
6169e293969SRichard Lowe 	    (strcmp(nconf->nc_proto, NC_UDP) == 0))) {
6177c478bd9Sstevel@tonic-gate 		PMAPLIST *pml;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
620*05360118SToomas Soome 		    pmap_service, 0)) {
6217c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "could not register on %s",
6229e293969SRichard Lowe 			    nconf->nc_netid);
6237c478bd9Sstevel@tonic-gate 			goto error;
6247c478bd9Sstevel@tonic-gate 		}
6258f6d9daeSMarcel Telka 		pml = malloc(sizeof (PMAPLIST));
6268f6d9daeSMarcel Telka 		if (pml == NULL) {
6277c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
6287c478bd9Sstevel@tonic-gate 			exit(1);
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_prog = PMAPPROG;
6317c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = PMAPVERS;
6327c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_port = PMAPPORT;
6337c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
6347c478bd9Sstevel@tonic-gate 			if (tcptrans[0]) {
6357c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6367c478bd9Sstevel@tonic-gate 				"cannot have more than one TCP transport");
6377c478bd9Sstevel@tonic-gate 				goto error;
6387c478bd9Sstevel@tonic-gate 			}
6397c478bd9Sstevel@tonic-gate 			tcptrans = strdup(nconf->nc_netid);
6407c478bd9Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_TCP;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 			/* Let's snarf the universal address */
6437c478bd9Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
6447c478bd9Sstevel@tonic-gate 			tcp_uaddr = taddr2uaddr(nconf, &baddr->addr);
6457c478bd9Sstevel@tonic-gate 		} else {
6467c478bd9Sstevel@tonic-gate 			if (udptrans[0]) {
6477c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
6487c478bd9Sstevel@tonic-gate 				"cannot have more than one UDP transport");
6497c478bd9Sstevel@tonic-gate 				goto error;
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate 			udptrans = strdup(nconf->nc_netid);
6527c478bd9Sstevel@tonic-gate 			pml->pml_map.pm_prot = IPPROTO_UDP;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 			/* Let's snarf the universal address */
6557c478bd9Sstevel@tonic-gate 			/* "h1.h2.h3.h4.p1.p2" */
6567c478bd9Sstevel@tonic-gate 			udp_uaddr = taddr2uaddr(nconf, &baddr->addr);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
6597c478bd9Sstevel@tonic-gate 		list_pml = pml;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		/* Add version 3 information */
6628f6d9daeSMarcel Telka 		pml = malloc(sizeof (PMAPLIST));
6638f6d9daeSMarcel Telka 		if (pml == NULL) {
6647c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
6657c478bd9Sstevel@tonic-gate 			exit(1);
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
6687c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS;
6697c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
6707c478bd9Sstevel@tonic-gate 		list_pml = pml;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		/* Add version 4 information */
6738f6d9daeSMarcel Telka 		pml = malloc(sizeof (PMAPLIST));
6748f6d9daeSMarcel Telka 		if (pml == NULL) {
6757c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory!");
6767c478bd9Sstevel@tonic-gate 			exit(1);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 		pml->pml_map = list_pml->pml_map;
6797c478bd9Sstevel@tonic-gate 		pml->pml_map.pm_vers = RPCBVERS4;
6807c478bd9Sstevel@tonic-gate 		pml->pml_next = list_pml;
6817c478bd9Sstevel@tonic-gate 		list_pml = pml;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		/* Also add version 2 stuff to rpcbind list */
6847c478bd9Sstevel@tonic-gate 		rbllist_add(PMAPPROG, PMAPVERS, nconf, &baddr->addr);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate #endif
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/* version 3 registration */
6897c478bd9Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
6907c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 3",
6919e293969SRichard Lowe 		    nconf->nc_netid);
6927c478bd9Sstevel@tonic-gate 		goto error;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS, nconf, &baddr->addr);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/* version 4 registration */
6977c478bd9Sstevel@tonic-gate 	if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
6987c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "could not register %s version 4",
6999e293969SRichard Lowe 		    nconf->nc_netid);
7007c478bd9Sstevel@tonic-gate 		goto error;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	rbllist_add(RPCBPROG, RPCBVERS4, nconf, &baddr->addr);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
7057c478bd9Sstevel@tonic-gate 		if (nconf->nc_semantics == NC_TPI_CLTS)
7067c478bd9Sstevel@tonic-gate 			loopback_dg = strdup(nconf->nc_netid);
7077c478bd9Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS)
7087c478bd9Sstevel@tonic-gate 			loopback_vc = strdup(nconf->nc_netid);
7097c478bd9Sstevel@tonic-gate 		else if (nconf->nc_semantics == NC_TPI_COTS_ORD)
7107c478bd9Sstevel@tonic-gate 			loopback_vc_ord = strdup(nconf->nc_netid);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* decide if bound checking works for this transport */
7148f6d9daeSMarcel Telka 	(void) add_bndlist(nconf, taddr, baddr);
7158f6d9daeSMarcel Telka 
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 * rmtcall only supported on CLTS transports for now.
7187c478bd9Sstevel@tonic-gate 	 */
7198f6d9daeSMarcel Telka 	if (nconf->nc_semantics == NC_TPI_CLTS)
7208f6d9daeSMarcel Telka 		(void) create_rmtcall_fd(nconf);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
7237c478bd9Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
7247c478bd9Sstevel@tonic-gate 	return (0);
7257c478bd9Sstevel@tonic-gate error:
7267c478bd9Sstevel@tonic-gate 	(void) t_free((char *)taddr, T_BIND);
7277c478bd9Sstevel@tonic-gate 	(void) t_free((char *)baddr, T_BIND);
7287c478bd9Sstevel@tonic-gate 	(void) t_close(fd);
7297c478bd9Sstevel@tonic-gate 	return (1);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate static void
rbllist_add(ulong_t prog,ulong_t vers,struct netconfig * nconf,struct netbuf * addr)7337c478bd9Sstevel@tonic-gate rbllist_add(ulong_t prog, ulong_t vers, struct netconfig *nconf,
7348f6d9daeSMarcel Telka     struct netbuf *addr)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	rpcblist_ptr rbl;
7377c478bd9Sstevel@tonic-gate 
7388f6d9daeSMarcel Telka 	rbl = malloc(sizeof (rpcblist));
7398f6d9daeSMarcel Telka 	if (rbl == NULL) {
7407c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory!");
7417c478bd9Sstevel@tonic-gate 		exit(1);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_prog = prog;
7457c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_vers = vers;
7467c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
7477c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
7488f6d9daeSMarcel Telka 	if (rbl->rpcb_map.r_addr == NULL)
7498f6d9daeSMarcel Telka 		rbl->rpcb_map.r_addr = strdup("");
7507c478bd9Sstevel@tonic-gate 	rbl->rpcb_map.r_owner = strdup(superuser);
7518f6d9daeSMarcel Telka 
7528f6d9daeSMarcel Telka 	if (rbl->rpcb_map.r_netid == NULL || rbl->rpcb_map.r_addr == NULL ||
7538f6d9daeSMarcel Telka 	    rbl->rpcb_map.r_owner == NULL) {
7548f6d9daeSMarcel Telka 		syslog(LOG_ERR, "no memory!");
7558f6d9daeSMarcel Telka 		exit(1);
7568f6d9daeSMarcel Telka 	}
7578f6d9daeSMarcel Telka 
7587c478bd9Sstevel@tonic-gate 	rbl->rpcb_next = list_rbl;	/* Attach to global list */
7597c478bd9Sstevel@tonic-gate 	list_rbl = rbl;
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate /*
7638f6d9daeSMarcel Telka  * Catch the signal and die, if not SIGHUP
7647c478bd9Sstevel@tonic-gate  */
7657c478bd9Sstevel@tonic-gate static void
terminate(void)7668f6d9daeSMarcel Telka terminate(void)
7677c478bd9Sstevel@tonic-gate {
7688f6d9daeSMarcel Telka 	int sig;
7698f6d9daeSMarcel Telka 
7708f6d9daeSMarcel Telka 	for (;;) {
7718f6d9daeSMarcel Telka 		sig = sigwait(&sigwaitset);
7728f6d9daeSMarcel Telka 		if (sig == SIGHUP) {
7738f6d9daeSMarcel Telka 			rpcb_check_init();
7748f6d9daeSMarcel Telka 			continue;
7758f6d9daeSMarcel Telka 		}
7768f6d9daeSMarcel Telka 		if (sig != -1 || errno != EINTR)
7778f6d9daeSMarcel Telka 			break;
7788f6d9daeSMarcel Telka 	}
7798f6d9daeSMarcel Telka 
7808f6d9daeSMarcel Telka 	syslog(LOG_ERR, "rpcbind terminating on signal %d.", sig);
7818f6d9daeSMarcel Telka 
7828f6d9daeSMarcel Telka 	rw_wrlock(&list_rbl_lock);
7838f6d9daeSMarcel Telka #ifdef PORTMAP
7848f6d9daeSMarcel Telka 	rw_wrlock(&list_pml_lock);
7858f6d9daeSMarcel Telka #endif
7867c478bd9Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
7877c478bd9Sstevel@tonic-gate 
7888f6d9daeSMarcel Telka 	exit(2);
789064ed339Sjjj }
790064ed339Sjjj 
7917c478bd9Sstevel@tonic-gate void
rpcbind_abort(void)7927c478bd9Sstevel@tonic-gate rpcbind_abort(void)
7937c478bd9Sstevel@tonic-gate {
7948f6d9daeSMarcel Telka 	/*
7958f6d9daeSMarcel Telka 	 * We need to hold write locks to make sure
7968f6d9daeSMarcel Telka 	 * write_warmstart() is executed exactly once
7978f6d9daeSMarcel Telka 	 */
7988f6d9daeSMarcel Telka 	rw_wrlock(&list_rbl_lock);
7998f6d9daeSMarcel Telka #ifdef PORTMAP
8008f6d9daeSMarcel Telka 	rw_wrlock(&list_pml_lock);
8018f6d9daeSMarcel Telka #endif
8027c478bd9Sstevel@tonic-gate 	write_warmstart();	/* Dump yourself */
8038f6d9daeSMarcel Telka 
8047c478bd9Sstevel@tonic-gate 	abort();
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate  * detach from tty
8097c478bd9Sstevel@tonic-gate  */
8107c478bd9Sstevel@tonic-gate static void
detachfromtty(void)8117c478bd9Sstevel@tonic-gate detachfromtty(void)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	close(0);
8147c478bd9Sstevel@tonic-gate 	close(1);
8157c478bd9Sstevel@tonic-gate 	close(2);
8167c478bd9Sstevel@tonic-gate 	switch (forkall()) {
8177c478bd9Sstevel@tonic-gate 	case (pid_t)-1:
8187c478bd9Sstevel@tonic-gate 		perror("fork");
8197c478bd9Sstevel@tonic-gate 		break;
8207c478bd9Sstevel@tonic-gate 	case 0:
8217c478bd9Sstevel@tonic-gate 		break;
8227c478bd9Sstevel@tonic-gate 	default:
8237c478bd9Sstevel@tonic-gate 		exit(0);
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 	setsid();
8267c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
8277c478bd9Sstevel@tonic-gate 	dup(0);
8287c478bd9Sstevel@tonic-gate 	dup(0);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8318f6d9daeSMarcel Telka static int
convert_int(int * val,char * str)8328f6d9daeSMarcel Telka convert_int(int *val, char *str)
8338f6d9daeSMarcel Telka {
8348f6d9daeSMarcel Telka 	long lval;
8358f6d9daeSMarcel Telka 
8368f6d9daeSMarcel Telka 	if (str == NULL || !isdigit(*str))
8378f6d9daeSMarcel Telka 		return (-1);
8388f6d9daeSMarcel Telka 
8398f6d9daeSMarcel Telka 	lval = strtol(str, &str, 10);
8408f6d9daeSMarcel Telka 	if (*str != '\0' || lval > INT_MAX)
8418f6d9daeSMarcel Telka 		return (-2);
8428f6d9daeSMarcel Telka 
8438f6d9daeSMarcel Telka 	*val = (int)lval;
8448f6d9daeSMarcel Telka 	return (0);
8458f6d9daeSMarcel Telka }
8468f6d9daeSMarcel Telka 
8478f6d9daeSMarcel Telka static int get_smf_iprop(const char *, int, int, int);
8488f6d9daeSMarcel Telka 
8497c478bd9Sstevel@tonic-gate /* get command line options */
8507c478bd9Sstevel@tonic-gate static void
parseargs(int argc,char * argv[])8517c478bd9Sstevel@tonic-gate parseargs(int argc, char *argv[])
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	int c;
8547c478bd9Sstevel@tonic-gate 	int tmp;
8557c478bd9Sstevel@tonic-gate 
8568f6d9daeSMarcel Telka 	listen_backlog = get_smf_iprop("listen_backlog", 64, 1, INT_MAX);
8578f6d9daeSMarcel Telka 
8587c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dwal:")) != EOF) {
8597c478bd9Sstevel@tonic-gate 		switch (c) {
8607c478bd9Sstevel@tonic-gate 		case 'd':
8617c478bd9Sstevel@tonic-gate 			debugging = 1;
8627c478bd9Sstevel@tonic-gate 			break;
8637c478bd9Sstevel@tonic-gate 		case 'a':
8647c478bd9Sstevel@tonic-gate 			doabort = 1;	/* when debugging, do an abort on */
8657c478bd9Sstevel@tonic-gate 			break;		/* errors; for rpcbind developers */
8667c478bd9Sstevel@tonic-gate 					/* only! */
8677c478bd9Sstevel@tonic-gate 		case 'w':
8687c478bd9Sstevel@tonic-gate 			warmstart = 1;
8697c478bd9Sstevel@tonic-gate 			break;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		case 'l':
8728f6d9daeSMarcel Telka 			if (convert_int(&tmp, optarg) != 0 || tmp < 1) {
8738f6d9daeSMarcel Telka 				(void) fprintf(stderr, "%s: invalid "
8748f6d9daeSMarcel Telka 				    "listen_backlog option, using defaults\n",
8758f6d9daeSMarcel Telka 				    argv[0]);
8768f6d9daeSMarcel Telka 				break;
8777c478bd9Sstevel@tonic-gate 			}
8788f6d9daeSMarcel Telka 			listen_backlog = tmp;
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 		default:	/* error */
8817c478bd9Sstevel@tonic-gate 			fprintf(stderr,
8828f6d9daeSMarcel Telka 			    "usage: rpcbind [-d] [-w] [-l listen_backlog]\n");
8837c478bd9Sstevel@tonic-gate 			exit(1);
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 	if (doabort && !debugging) {
8879e293969SRichard Lowe 		fprintf(stderr,
8889e293969SRichard Lowe 		    "-a (abort) specified without -d "
8899e293969SRichard Lowe 		    "(debugging) -- ignored.\n");
8909e293969SRichard Lowe 		doabort = 0;
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate static int
setopt_int(int fd,int level,int name,int value)89545916cd2Sjpk setopt_int(int fd, int level, int name, int value)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	struct t_optmgmt req, resp;
89845916cd2Sjpk 	struct {
89945916cd2Sjpk 		struct opthdr opt;
90045916cd2Sjpk 		int value;
90145916cd2Sjpk 	} optdata;
9027c478bd9Sstevel@tonic-gate 
90345916cd2Sjpk 	optdata.opt.level = level;
90445916cd2Sjpk 	optdata.opt.name = name;
90545916cd2Sjpk 	optdata.opt.len = sizeof (int);
9067c478bd9Sstevel@tonic-gate 
90745916cd2Sjpk 	optdata.value = value;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
91045916cd2Sjpk 	req.opt.len = sizeof (optdata);
91145916cd2Sjpk 	req.opt.buf = (char *)&optdata;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	resp.flags = 0;
91445916cd2Sjpk 	resp.opt.buf = (char *)&optdata;
91545916cd2Sjpk 	resp.opt.maxlen = sizeof (optdata);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
9187c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
9197c478bd9Sstevel@tonic-gate 		return (-1);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 	return (0);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
92445916cd2Sjpk static int
setopt_reuseaddr(int fd)92545916cd2Sjpk setopt_reuseaddr(int fd)
92645916cd2Sjpk {
92745916cd2Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_REUSEADDR, 1));
92845916cd2Sjpk }
92945916cd2Sjpk 
93045916cd2Sjpk static int
setopt_anon_mlp(int fd)93145916cd2Sjpk setopt_anon_mlp(int fd)
93245916cd2Sjpk {
93345916cd2Sjpk 	return (setopt_int(fd, SOL_SOCKET, SO_ANON_MLP, 1));
93445916cd2Sjpk }
93545916cd2Sjpk 
9367c478bd9Sstevel@tonic-gate static int
setup_callit(int fd)9377c478bd9Sstevel@tonic-gate setup_callit(int fd)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	struct ipv6_mreq mreq;
9407c478bd9Sstevel@tonic-gate 	struct t_optmgmt req, resp;
9417c478bd9Sstevel@tonic-gate 	struct opthdr *opt;
9427c478bd9Sstevel@tonic-gate 	char reqbuf[ sizeof (struct ipv6_mreq) + 24];
9437c478bd9Sstevel@tonic-gate 	struct ipv6_mreq *pmreq;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	opt = (struct opthdr *)reqbuf;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	opt->level = IPPROTO_IPV6;
9487c478bd9Sstevel@tonic-gate 	opt->name = IPV6_ADD_MEMBERSHIP;
9497c478bd9Sstevel@tonic-gate 	opt->len = sizeof (struct ipv6_mreq);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/* multicast address */
9527c478bd9Sstevel@tonic-gate 	(void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR,
9539e293969SRichard Lowe 	    mreq.ipv6mr_multiaddr.s6_addr);
9547c478bd9Sstevel@tonic-gate 	mreq.ipv6mr_interface = 0;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/* insert it into opt */
9577c478bd9Sstevel@tonic-gate 	pmreq = (struct ipv6_mreq *)&reqbuf[sizeof (struct opthdr)];
9587c478bd9Sstevel@tonic-gate 	memcpy(pmreq, &mreq, sizeof (struct ipv6_mreq));
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	req.flags = T_NEGOTIATE;
9617c478bd9Sstevel@tonic-gate 	req.opt.len = sizeof (struct opthdr) + opt->len;
9627c478bd9Sstevel@tonic-gate 	req.opt.buf = (char *)opt;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	resp.flags = 0;
9657c478bd9Sstevel@tonic-gate 	resp.opt.buf = reqbuf;
9667c478bd9Sstevel@tonic-gate 	resp.opt.maxlen = sizeof (reqbuf);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) {
9697c478bd9Sstevel@tonic-gate 		t_error("t_optmgmt");
9707c478bd9Sstevel@tonic-gate 		return (-1);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 	return (0);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate static boolean_t
check_hostserv(struct netconfig * nconf,const char * host,const char * serv)9767c478bd9Sstevel@tonic-gate check_hostserv(struct netconfig *nconf, const char *host, const char *serv)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	struct nd_hostserv nh;
9797c478bd9Sstevel@tonic-gate 	struct nd_addrlist *na;
9807c478bd9Sstevel@tonic-gate 	const char *hostname = host;
9817c478bd9Sstevel@tonic-gate 	const char *servname = serv;
9827c478bd9Sstevel@tonic-gate 	int retval;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	if (strcmp(host, HOST_SELF) == 0)
9857c478bd9Sstevel@tonic-gate 		hostname = "HOST_SELF";
9867c478bd9Sstevel@tonic-gate 	else if (strcmp(host, HOST_SELF_CONNECT) == 0)
9877c478bd9Sstevel@tonic-gate 		hostname = "HOST_SELF_CONNECT";
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if (serv[0] == '\0')
9907c478bd9Sstevel@tonic-gate 		servname = "<any>";
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	nh.h_host = (char *)host;
9937c478bd9Sstevel@tonic-gate 	nh.h_serv = (char *)serv;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	retval = netdir_getbyname(nconf, &nh, &na);
9967c478bd9Sstevel@tonic-gate 	if (retval != ND_OK || na->n_cnt == 0) {
9977c478bd9Sstevel@tonic-gate 		if (retval == ND_OK)
9987c478bd9Sstevel@tonic-gate 			netdir_free(na, ND_ADDRLIST);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 		syslog(LOG_ALERT, "netid %s: cannot find an address for host "
10017c478bd9Sstevel@tonic-gate 		    "%s, service \"%s\"", nconf->nc_netid, hostname, servname);
10027c478bd9Sstevel@tonic-gate 		if (debugging) {
10037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\tnetdir_getbyname for %s, "
10047c478bd9Sstevel@tonic-gate 			    "service \"%s\" failed\n", hostname, servname);
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		return (B_FALSE);
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 	netdir_free(na, ND_ADDRLIST);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (debugging) {
10117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tnetdir_getbyname for %s, service "
10127c478bd9Sstevel@tonic-gate 		    "service \"%s\" succeeded\n", hostname, servname);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 	return (B_TRUE);
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /* Maximum outstanding syslog requests */
10187c478bd9Sstevel@tonic-gate #define	MAXLOG		100
10197c478bd9Sstevel@tonic-gate /* Maximum length: the messages generated are fairly short; no hostnames. */
10207c478bd9Sstevel@tonic-gate #define	MAXMSG		128
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate typedef struct logmsg {
10237c478bd9Sstevel@tonic-gate 	struct logmsg	*log_next;
10247c478bd9Sstevel@tonic-gate 	int		log_pri;
10257c478bd9Sstevel@tonic-gate 	char		log_msg[MAXMSG];
10267c478bd9Sstevel@tonic-gate } logmsg;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate static logmsg *loghead = NULL;
10297c478bd9Sstevel@tonic-gate static logmsg **logtailp = &loghead;
10307c478bd9Sstevel@tonic-gate static mutex_t logmutex = DEFAULTMUTEX;
10317c478bd9Sstevel@tonic-gate static cond_t logcond = DEFAULTCV;
10327c478bd9Sstevel@tonic-gate static int logcount = 0;
10337c478bd9Sstevel@tonic-gate 
10348f6d9daeSMarcel Telka /* ARGSUSED */
10359e293969SRichard Lowe static void * __NORETURN
logthread(void * arg)10367c478bd9Sstevel@tonic-gate logthread(void *arg)
10377c478bd9Sstevel@tonic-gate {
10388f6d9daeSMarcel Telka 	for (;;) {
10397c478bd9Sstevel@tonic-gate 		logmsg *msg;
10407c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&logmutex);
10417c478bd9Sstevel@tonic-gate 		while ((msg = loghead) == NULL)
10427c478bd9Sstevel@tonic-gate 			(void) cond_wait(&logcond, &logmutex);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		loghead = msg->log_next;
10457c478bd9Sstevel@tonic-gate 		logcount--;
10467c478bd9Sstevel@tonic-gate 		if (loghead == NULL) {
10477c478bd9Sstevel@tonic-gate 			logtailp = &loghead;
10487c478bd9Sstevel@tonic-gate 			logcount = 0;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&logmutex);
10517c478bd9Sstevel@tonic-gate 		syslog(msg->log_pri, "%s", msg->log_msg);
10527c478bd9Sstevel@tonic-gate 		free(msg);
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10570ea5e3a5Sjjj static boolean_t
get_smf_prop(const char * var,boolean_t def_val)10580ea5e3a5Sjjj get_smf_prop(const char *var, boolean_t def_val)
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate 	scf_simple_prop_t *prop;
1061ff904c11SGordon Ross 	uint8_t *val = NULL;
10620ea5e3a5Sjjj 	boolean_t res = def_val;
10637c478bd9Sstevel@tonic-gate 
10640ea5e3a5Sjjj 	prop = scf_simple_prop_get(NULL, NULL, "config", var);
10658f6d9daeSMarcel Telka 	if (prop != NULL) {
10660ea5e3a5Sjjj 		if ((val = scf_simple_prop_next_boolean(prop)) != NULL)
10670ea5e3a5Sjjj 			res = (*val == 0) ? B_FALSE : B_TRUE;
10687c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
10710ea5e3a5Sjjj 	if (prop == NULL || val == NULL) {
10720ea5e3a5Sjjj 		syslog(LOG_ALERT, "no value for config/%s (%s). "
10730ea5e3a5Sjjj 		    "Using default \"%s\"", var, scf_strerror(scf_error()),
10740ea5e3a5Sjjj 		    def_val ? "true" : "false");
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10770ea5e3a5Sjjj 	return (res);
10780ea5e3a5Sjjj }
10790ea5e3a5Sjjj 
10808f6d9daeSMarcel Telka static int
get_smf_iprop(const char * var,int def_val,int min,int max)10818f6d9daeSMarcel Telka get_smf_iprop(const char *var, int def_val, int min, int max)
10828f6d9daeSMarcel Telka {
10838f6d9daeSMarcel Telka 	scf_simple_prop_t *prop;
1084ff904c11SGordon Ross 	int64_t *val = NULL;
10858f6d9daeSMarcel Telka 	int res = def_val;
10868f6d9daeSMarcel Telka 
10878f6d9daeSMarcel Telka 	prop = scf_simple_prop_get(NULL, NULL, "config", var);
10888f6d9daeSMarcel Telka 	if (prop != NULL) {
10898f6d9daeSMarcel Telka 		if ((val = scf_simple_prop_next_integer(prop)) != NULL) {
10908f6d9daeSMarcel Telka 			if (*val < min || *val > max)
10918f6d9daeSMarcel Telka 				syslog(LOG_ALERT, "value for config/%s out of "
10928f6d9daeSMarcel Telka 				    "range. Using default %d", var, def_val);
10938f6d9daeSMarcel Telka 			else
10948f6d9daeSMarcel Telka 				res = (int)*val;
10958f6d9daeSMarcel Telka 		}
10968f6d9daeSMarcel Telka 		scf_simple_prop_free(prop);
10978f6d9daeSMarcel Telka 	}
10988f6d9daeSMarcel Telka 
10998f6d9daeSMarcel Telka 	if (prop == NULL || val == NULL) {
11008f6d9daeSMarcel Telka 		syslog(LOG_ALERT, "no value for config/%s (%s). "
11018f6d9daeSMarcel Telka 		    "Using default %d", var, scf_strerror(scf_error()),
11028f6d9daeSMarcel Telka 		    def_val);
11038f6d9daeSMarcel Telka 	}
11048f6d9daeSMarcel Telka 
11058f6d9daeSMarcel Telka 	return (res);
11068f6d9daeSMarcel Telka }
11078f6d9daeSMarcel Telka 
11080ea5e3a5Sjjj /*
1109064ed339Sjjj  * Initialize: read the configuration parameters from SMF.
1110064ed339Sjjj  * This function must be idempotent because it can be called from the
11118f6d9daeSMarcel Telka  * signal handler.
11120ea5e3a5Sjjj  */
11138f6d9daeSMarcel Telka static void
rpcb_check_init(void)11140ea5e3a5Sjjj rpcb_check_init(void)
11150ea5e3a5Sjjj {
11160ea5e3a5Sjjj 	thread_t tid;
11178f6d9daeSMarcel Telka 	int max_threads;
1118064ed339Sjjj 	static int thr_running;
11190ea5e3a5Sjjj 
11200ea5e3a5Sjjj 	wrap_enabled = get_smf_prop("enable_tcpwrappers", B_FALSE);
11210ea5e3a5Sjjj 	verboselog = get_smf_prop("verbose_logging", B_FALSE);
11220ea5e3a5Sjjj 	allow_indirect = get_smf_prop("allow_indirect", B_TRUE);
11238f6d9daeSMarcel Telka 	local_only = get_smf_prop("local_only", B_TRUE);
11247c478bd9Sstevel@tonic-gate 
1125064ed339Sjjj 	if (wrap_enabled && !thr_running) {
11267c478bd9Sstevel@tonic-gate 		(void) thr_create(NULL, 0, logthread, NULL, THR_DETACHED, &tid);
1127064ed339Sjjj 		thr_running = 1;
1128064ed339Sjjj 	}
11298f6d9daeSMarcel Telka 
11308f6d9daeSMarcel Telka 	/*
11318f6d9daeSMarcel Telka 	 * Set the maximum number of threads.
11328f6d9daeSMarcel Telka 	 */
11338f6d9daeSMarcel Telka 	max_threads = get_smf_iprop("max_threads", 72, 1, INT_MAX);
11348f6d9daeSMarcel Telka 	if (!rpc_control(RPC_SVC_THRMAX_SET, &max_threads)) {
11358f6d9daeSMarcel Telka 		int tmp;
11368f6d9daeSMarcel Telka 
11378f6d9daeSMarcel Telka 		/*
11388f6d9daeSMarcel Telka 		 * The following rpc_control() call cannot fail
11398f6d9daeSMarcel Telka 		 */
11408f6d9daeSMarcel Telka 		if (!rpc_control(RPC_SVC_THRMAX_GET, &tmp))
11418f6d9daeSMarcel Telka 			assert(0);
11428f6d9daeSMarcel Telka 
11438f6d9daeSMarcel Telka 		if (tmp != max_threads) {
11448f6d9daeSMarcel Telka 			syslog(LOG_ERR, "setting max_threads to %d failed, "
11458f6d9daeSMarcel Telka 			    "using %d worker threads", max_threads, tmp);
11468f6d9daeSMarcel Telka 			max_threads = tmp;
11478f6d9daeSMarcel Telka 		}
11488f6d9daeSMarcel Telka 	}
11498f6d9daeSMarcel Telka 
11508f6d9daeSMarcel Telka 	/*
11518f6d9daeSMarcel Telka 	 * Set rpcb_rmtcalls_max.
11528f6d9daeSMarcel Telka 	 */
11538f6d9daeSMarcel Telka 	if (max_threads < reserved_threads)
11548f6d9daeSMarcel Telka 		set_rpcb_rmtcalls_max(0);
11558f6d9daeSMarcel Telka 	else
11568f6d9daeSMarcel Telka 		set_rpcb_rmtcalls_max(max_threads - reserved_threads);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /*
11607c478bd9Sstevel@tonic-gate  * qsyslog() - queue a request for syslog(); if syslog blocks, the other
11617c478bd9Sstevel@tonic-gate  * thread blocks; we make sure we don't run out of memory by allowing
11627c478bd9Sstevel@tonic-gate  * only a limited number of outstandig syslog() requests.
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate void
qsyslog(int pri,const char * fmt,...)11657c478bd9Sstevel@tonic-gate qsyslog(int pri, const char *fmt, ...)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	logmsg *msg = malloc(sizeof (*msg));
11687c478bd9Sstevel@tonic-gate 	va_list ap;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if (msg == NULL)
11717c478bd9Sstevel@tonic-gate 		return;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	msg->log_pri = pri;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
11767c478bd9Sstevel@tonic-gate 	(void) vsnprintf(msg->log_msg, sizeof (msg->log_msg), fmt, ap);
11777c478bd9Sstevel@tonic-gate 	va_end(ap);
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	msg->log_next = NULL;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&logmutex);
11827c478bd9Sstevel@tonic-gate 	if (logcount < MAXLOG) {
11838f6d9daeSMarcel Telka 		if (logcount == 0)
11848f6d9daeSMarcel Telka 			(void) cond_signal(&logcond);
11857c478bd9Sstevel@tonic-gate 		logcount++;
11867c478bd9Sstevel@tonic-gate 		*logtailp = msg;
11877c478bd9Sstevel@tonic-gate 		logtailp = &msg->log_next;
11888f6d9daeSMarcel Telka 		(void) mutex_unlock(&logmutex);
11897c478bd9Sstevel@tonic-gate 	} else {
11908f6d9daeSMarcel Telka 		(void) mutex_unlock(&logmutex);
11917c478bd9Sstevel@tonic-gate 		free(msg);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate }
1194