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
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * 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  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23cb620785Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26e8031f0aSraf 
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate  * California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
36e8031f0aSraf  * Miscl routines for RPC.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "mt.h"
407c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
447c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate #include <sys/resource.h>
517c478bd9Sstevel@tonic-gate #include <netconfig.h>
527c478bd9Sstevel@tonic-gate #include <malloc.h>
537c478bd9Sstevel@tonic-gate #include <syslog.h>
547c478bd9Sstevel@tonic-gate #include <string.h>
557c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
567c478bd9Sstevel@tonic-gate #include <netdir.h>
577c478bd9Sstevel@tonic-gate #include <netdb.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct handle {
607c478bd9Sstevel@tonic-gate 	NCONF_HANDLE *nhandle;
617c478bd9Sstevel@tonic-gate 	int nflag;		/* Whether NETPATH or NETCONFIG */
627c478bd9Sstevel@tonic-gate 	int nettype;
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate struct _rpcnettype {
667c478bd9Sstevel@tonic-gate 	const char *name;
677c478bd9Sstevel@tonic-gate 	const int type;
687c478bd9Sstevel@tonic-gate } _rpctypelist[] = {
697c478bd9Sstevel@tonic-gate 	"netpath", _RPC_NETPATH,
707c478bd9Sstevel@tonic-gate 	"visible", _RPC_VISIBLE,
717c478bd9Sstevel@tonic-gate 	"circuit_v", _RPC_CIRCUIT_V,
727c478bd9Sstevel@tonic-gate 	"datagram_v", _RPC_DATAGRAM_V,
737c478bd9Sstevel@tonic-gate 	"circuit_n", _RPC_CIRCUIT_N,
747c478bd9Sstevel@tonic-gate 	"datagram_n", _RPC_DATAGRAM_N,
757c478bd9Sstevel@tonic-gate 	"tcp", _RPC_TCP,
767c478bd9Sstevel@tonic-gate 	"udp", _RPC_UDP,
777c478bd9Sstevel@tonic-gate 	"local", _RPC_LOCAL,
787c478bd9Sstevel@tonic-gate 	"door", _RPC_DOOR,
797c478bd9Sstevel@tonic-gate 	"door_local", _RPC_DOOR_LOCAL,
807c478bd9Sstevel@tonic-gate 	"door_netpath", _RPC_DOOR_NETPATH,
817c478bd9Sstevel@tonic-gate 	0, _RPC_NONE
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Cache the result of getrlimit(), so we don't have to do an
867c478bd9Sstevel@tonic-gate  * expensive call every time. Since many old programs assume
877c478bd9Sstevel@tonic-gate  * it will not return more than 1024 and use svc_fdset, return
887c478bd9Sstevel@tonic-gate  * maximum of FD_SETSIZE.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate int
__rpc_dtbsize(void)9161961e0fSrobinson __rpc_dtbsize(void)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	static int tbsize;
947c478bd9Sstevel@tonic-gate 	struct rlimit rl;
957c478bd9Sstevel@tonic-gate 
9661961e0fSrobinson 	if (tbsize)
977c478bd9Sstevel@tonic-gate 		return (tbsize);
987c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
997c478bd9Sstevel@tonic-gate 		tbsize = rl.rlim_max;
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * backward compatibility; too many places
1027c478bd9Sstevel@tonic-gate 		 * this function is called assuming it returns
1037c478bd9Sstevel@tonic-gate 		 * maximum of 1024.
1047c478bd9Sstevel@tonic-gate 		 */
1057c478bd9Sstevel@tonic-gate 		if (tbsize > FD_SETSIZE)
1067c478bd9Sstevel@tonic-gate 			tbsize = FD_SETSIZE;
1077c478bd9Sstevel@tonic-gate 		return (tbsize);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * Something wrong.  I'll try to save face by returning a
1117c478bd9Sstevel@tonic-gate 	 * pessimistic number.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	return (32);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Find the appropriate buffer size
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate uint_t
__rpc_get_t_size(t_scalar_t size,t_scalar_t bufsize)1207c478bd9Sstevel@tonic-gate __rpc_get_t_size(
1217c478bd9Sstevel@tonic-gate 	t_scalar_t size,	/* Size requested */
1227c478bd9Sstevel@tonic-gate 	t_scalar_t bufsize)	/* Supported by the transport */
1237c478bd9Sstevel@tonic-gate {
12461961e0fSrobinson 	if (bufsize == -2)	/* transfer of data unsupported */
1257c478bd9Sstevel@tonic-gate 		return ((uint_t)0);
1267c478bd9Sstevel@tonic-gate 	if (size == 0) {
1277c478bd9Sstevel@tonic-gate 		if ((bufsize == -1) || (bufsize == 0)) {
1287c478bd9Sstevel@tonic-gate 			/*
1297c478bd9Sstevel@tonic-gate 			 * bufsize == -1 : No limit on the size
1307c478bd9Sstevel@tonic-gate 			 * bufsize == 0 : Concept of tsdu foreign. Choose
1317c478bd9Sstevel@tonic-gate 			 *			a value.
1327c478bd9Sstevel@tonic-gate 			 */
1337c478bd9Sstevel@tonic-gate 			return ((uint_t)RPC_MAXDATASIZE);
1347c478bd9Sstevel@tonic-gate 		}
13561961e0fSrobinson 		return ((uint_t)bufsize);
1367c478bd9Sstevel@tonic-gate 	}
13761961e0fSrobinson 	if ((bufsize == -1) || (bufsize == 0))
1387c478bd9Sstevel@tonic-gate 		return ((uint_t)size);
1397c478bd9Sstevel@tonic-gate 	/* Check whether the value is within the upper max limit */
1407c478bd9Sstevel@tonic-gate 	return (size > bufsize ? (uint_t)bufsize : (uint_t)size);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * Find the appropriate address buffer size
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate uint_t
__rpc_get_a_size(t_scalar_t size)1477c478bd9Sstevel@tonic-gate __rpc_get_a_size(
1487c478bd9Sstevel@tonic-gate 	t_scalar_t size)	/* normally tinfo.addr */
1497c478bd9Sstevel@tonic-gate {
15061961e0fSrobinson 	if (size >= 0)
1517c478bd9Sstevel@tonic-gate 		return ((uint_t)size);
15261961e0fSrobinson 	if (size <= -2)
1537c478bd9Sstevel@tonic-gate 		return ((uint_t)0);
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * (size == -1) No limit on the size. we impose a limit here.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	return ((uint_t)RPC_MAXADDRSIZE);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Returns the type of the network as defined in <rpc/nettype.h>
1627c478bd9Sstevel@tonic-gate  * If nettype is NULL, it defaults to NETPATH.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate static int
getnettype(const char * nettype)16561961e0fSrobinson getnettype(const char *nettype)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	int i;
1687c478bd9Sstevel@tonic-gate 
169*344db6f4SToomas Soome 	if ((nettype == NULL) || (nettype[0] == '\0'))
1707c478bd9Sstevel@tonic-gate 		return (_RPC_NETPATH);	/* Default */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	for (i = 0; _rpctypelist[i].name; i++)
17361961e0fSrobinson 		if (strcasecmp(nettype, _rpctypelist[i].name) == 0)
1747c478bd9Sstevel@tonic-gate 			return (_rpctypelist[i].type);
1757c478bd9Sstevel@tonic-gate 	return (_rpctypelist[i].type);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * For the given nettype (tcp or udp only), return the first structure found.
1807c478bd9Sstevel@tonic-gate  * This should be freed by calling freenetconfigent()
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate struct netconfig *
__rpc_getconfip(char * nettype)1837c478bd9Sstevel@tonic-gate __rpc_getconfip(char *nettype)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	char *netid;
186cb620785Sraf 	char *netid_tcp;
187cb620785Sraf 	char *netid_udp;
188cb620785Sraf 	static char *netid_tcp_main = NULL;
189cb620785Sraf 	static char *netid_udp_main = NULL;
190cb620785Sraf 	static pthread_key_t tcp_key = PTHREAD_ONCE_KEY_NP;
191cb620785Sraf 	static pthread_key_t udp_key = PTHREAD_ONCE_KEY_NP;
1927c478bd9Sstevel@tonic-gate 	int main_thread;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if ((main_thread = thr_main())) {
1957c478bd9Sstevel@tonic-gate 		netid_udp = netid_udp_main;
1967c478bd9Sstevel@tonic-gate 		netid_tcp = netid_tcp_main;
1977c478bd9Sstevel@tonic-gate 	} else {
198cb620785Sraf 		(void) pthread_key_create_once_np(&tcp_key, free);
1997c478bd9Sstevel@tonic-gate 		netid_tcp = pthread_getspecific(tcp_key);
200cb620785Sraf 		(void) pthread_key_create_once_np(&udp_key, free);
2017c478bd9Sstevel@tonic-gate 		netid_udp = pthread_getspecific(udp_key);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	if (!netid_udp && !netid_tcp) {
2047c478bd9Sstevel@tonic-gate 		struct netconfig *nconf;
2057c478bd9Sstevel@tonic-gate 		void *confighandle;
2067c478bd9Sstevel@tonic-gate 
20761961e0fSrobinson 		if (!(confighandle = setnetconfig()))
2087c478bd9Sstevel@tonic-gate 			return (NULL);
2097c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(confighandle)) {
2107c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
2117c478bd9Sstevel@tonic-gate 				if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
2127c478bd9Sstevel@tonic-gate 					netid_tcp = strdup(nconf->nc_netid);
2137c478bd9Sstevel@tonic-gate 					if (netid_tcp == NULL) {
2147c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR,
2157c478bd9Sstevel@tonic-gate 							"__rpc_getconfip : "
2167c478bd9Sstevel@tonic-gate 							"strdup failed");
21761961e0fSrobinson 						return (NULL);
2187c478bd9Sstevel@tonic-gate 					}
2197c478bd9Sstevel@tonic-gate 					if (main_thread)
2207c478bd9Sstevel@tonic-gate 						netid_tcp_main = netid_tcp;
2217c478bd9Sstevel@tonic-gate 					else
22261961e0fSrobinson 						(void) pthread_setspecific(
22361961e0fSrobinson 							tcp_key,
2247c478bd9Sstevel@tonic-gate 							(void *)netid_tcp);
2257c478bd9Sstevel@tonic-gate 				} else
2267c478bd9Sstevel@tonic-gate 				if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
2277c478bd9Sstevel@tonic-gate 					netid_udp = strdup(nconf->nc_netid);
2287c478bd9Sstevel@tonic-gate 					if (netid_udp == NULL) {
2297c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR,
2307c478bd9Sstevel@tonic-gate 							"__rpc_getconfip : "
2317c478bd9Sstevel@tonic-gate 							"strdup failed");
23261961e0fSrobinson 						return (NULL);
2337c478bd9Sstevel@tonic-gate 					}
2347c478bd9Sstevel@tonic-gate 					if (main_thread)
2357c478bd9Sstevel@tonic-gate 						netid_udp_main = netid_udp;
2367c478bd9Sstevel@tonic-gate 					else
23761961e0fSrobinson 						(void) pthread_setspecific(
23861961e0fSrobinson 							udp_key,
2397c478bd9Sstevel@tonic-gate 							(void *)netid_udp);
2407c478bd9Sstevel@tonic-gate 				}
2417c478bd9Sstevel@tonic-gate 			}
2427c478bd9Sstevel@tonic-gate 		}
24361961e0fSrobinson 		(void) endnetconfig(confighandle);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	if (strcmp(nettype, "udp") == 0)
2467c478bd9Sstevel@tonic-gate 		netid = netid_udp;
2477c478bd9Sstevel@tonic-gate 	else if (strcmp(nettype, "tcp") == 0)
2487c478bd9Sstevel@tonic-gate 		netid = netid_tcp;
24961961e0fSrobinson 	else
25061961e0fSrobinson 		return (NULL);
251*344db6f4SToomas Soome 	if ((netid == NULL) || (netid[0] == '\0'))
25261961e0fSrobinson 		return (NULL);
25361961e0fSrobinson 	return (getnetconfigent(netid));
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Returns the type of the nettype, which should then be used with
2597c478bd9Sstevel@tonic-gate  * __rpc_getconf().
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate void *
__rpc_setconf(char * nettype)2627c478bd9Sstevel@tonic-gate __rpc_setconf(char *nettype)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	struct handle *handle;
2657c478bd9Sstevel@tonic-gate 
26661961e0fSrobinson 	handle = malloc(sizeof (struct handle));
26761961e0fSrobinson 	if (handle == NULL)
2687c478bd9Sstevel@tonic-gate 		return (NULL);
2697c478bd9Sstevel@tonic-gate 	switch (handle->nettype = getnettype(nettype)) {
2707c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_NETPATH:
2717c478bd9Sstevel@tonic-gate 	case _RPC_NETPATH:
2727c478bd9Sstevel@tonic-gate 	case _RPC_CIRCUIT_N:
2737c478bd9Sstevel@tonic-gate 	case _RPC_DATAGRAM_N:
2747c478bd9Sstevel@tonic-gate 		if (!(handle->nhandle = setnetpath())) {
2757c478bd9Sstevel@tonic-gate 			free(handle);
2767c478bd9Sstevel@tonic-gate 			return (NULL);
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 		handle->nflag = TRUE;
2797c478bd9Sstevel@tonic-gate 		break;
2807c478bd9Sstevel@tonic-gate 	case _RPC_VISIBLE:
2817c478bd9Sstevel@tonic-gate 	case _RPC_CIRCUIT_V:
2827c478bd9Sstevel@tonic-gate 	case _RPC_DATAGRAM_V:
2837c478bd9Sstevel@tonic-gate 	case _RPC_TCP:
2847c478bd9Sstevel@tonic-gate 	case _RPC_UDP:
2857c478bd9Sstevel@tonic-gate 	case _RPC_LOCAL:
2867c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_LOCAL:
2877c478bd9Sstevel@tonic-gate 		if (!(handle->nhandle = setnetconfig())) {
2887c478bd9Sstevel@tonic-gate 			free(handle);
2897c478bd9Sstevel@tonic-gate 			return (NULL);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		handle->nflag = FALSE;
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 	default:
2947c478bd9Sstevel@tonic-gate 		free(handle);
2957c478bd9Sstevel@tonic-gate 		return (NULL);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (handle);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * Returns the next netconfig struct for the given "net" type.
3037c478bd9Sstevel@tonic-gate  * __rpc_setconf() should have been called previously.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate struct netconfig *
__rpc_getconf(void * vhandle)3067c478bd9Sstevel@tonic-gate __rpc_getconf(void *vhandle)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	struct handle *handle;
3097c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	handle = (struct handle *)vhandle;
31261961e0fSrobinson 	if (handle == NULL)
3137c478bd9Sstevel@tonic-gate 		return (NULL);
31461961e0fSrobinson 	for (;;) {
3157c478bd9Sstevel@tonic-gate 		if (handle->nflag)
3167c478bd9Sstevel@tonic-gate 			nconf = getnetpath(handle->nhandle);
3177c478bd9Sstevel@tonic-gate 		else
3187c478bd9Sstevel@tonic-gate 			nconf = getnetconfig(handle->nhandle);
31961961e0fSrobinson 		if (nconf == NULL)
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		if ((nconf->nc_semantics != NC_TPI_CLTS) &&
3227c478bd9Sstevel@tonic-gate 		    (nconf->nc_semantics != NC_TPI_COTS) &&
3237c478bd9Sstevel@tonic-gate 		    (nconf->nc_semantics != NC_TPI_COTS_ORD))
3247c478bd9Sstevel@tonic-gate 			continue;
3257c478bd9Sstevel@tonic-gate 		switch (handle->nettype) {
3267c478bd9Sstevel@tonic-gate 		case _RPC_VISIBLE:
3277c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3287c478bd9Sstevel@tonic-gate 				continue;
3297c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3307c478bd9Sstevel@tonic-gate 		case _RPC_DOOR_NETPATH:
3317c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3327c478bd9Sstevel@tonic-gate 		case _RPC_NETPATH:	/* Be happy */
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		case _RPC_CIRCUIT_V:
3357c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3367c478bd9Sstevel@tonic-gate 				continue;
3377c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3387c478bd9Sstevel@tonic-gate 		case _RPC_CIRCUIT_N:
3397c478bd9Sstevel@tonic-gate 			if ((nconf->nc_semantics != NC_TPI_COTS) &&
3407c478bd9Sstevel@tonic-gate 			    (nconf->nc_semantics != NC_TPI_COTS_ORD))
3417c478bd9Sstevel@tonic-gate 				continue;
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 		case _RPC_DATAGRAM_V:
3447c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3457c478bd9Sstevel@tonic-gate 				continue;
3467c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3477c478bd9Sstevel@tonic-gate 		case _RPC_DATAGRAM_N:
3487c478bd9Sstevel@tonic-gate 			if (nconf->nc_semantics != NC_TPI_CLTS)
3497c478bd9Sstevel@tonic-gate 				continue;
3507c478bd9Sstevel@tonic-gate 			break;
3517c478bd9Sstevel@tonic-gate 		case _RPC_TCP:
3527c478bd9Sstevel@tonic-gate 			if (((nconf->nc_semantics != NC_TPI_COTS) &&
3537c478bd9Sstevel@tonic-gate 			    (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
3547c478bd9Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) &&
3557c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6)) ||
3567c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_proto, NC_TCP))
3577c478bd9Sstevel@tonic-gate 				continue;
3587c478bd9Sstevel@tonic-gate 			break;
3597c478bd9Sstevel@tonic-gate 		case _RPC_UDP:
3607c478bd9Sstevel@tonic-gate 			if ((nconf->nc_semantics != NC_TPI_CLTS) ||
3617c478bd9Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) &&
3627c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6)) ||
3637c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_proto, NC_UDP))
3647c478bd9Sstevel@tonic-gate 				continue;
3657c478bd9Sstevel@tonic-gate 			break;
3667c478bd9Sstevel@tonic-gate 		case _RPC_LOCAL:
3677c478bd9Sstevel@tonic-gate 		case _RPC_DOOR_LOCAL:
3687c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3697c478bd9Sstevel@tonic-gate 				continue;
3707c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK))
3717c478bd9Sstevel@tonic-gate 				continue;
3727c478bd9Sstevel@tonic-gate 			break;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		break;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	return (nconf);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate void
__rpc_endconf(void * vhandle)3807c478bd9Sstevel@tonic-gate __rpc_endconf(void *vhandle)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	struct handle *handle;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	handle = (struct handle *)vhandle;
38561961e0fSrobinson 	if (handle == NULL)
3867c478bd9Sstevel@tonic-gate 		return;
3877c478bd9Sstevel@tonic-gate 	if (handle->nflag) {
38861961e0fSrobinson 		(void) endnetpath(handle->nhandle);
3897c478bd9Sstevel@tonic-gate 	} else {
39061961e0fSrobinson 		(void) endnetconfig(handle->nhandle);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	free(handle);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Used to ping the NULL procedure for clnt handle.
3977c478bd9Sstevel@tonic-gate  * Returns NULL if fails, else a non-NULL pointer.
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate void *
rpc_nullproc(CLIENT * clnt)4007c478bd9Sstevel@tonic-gate rpc_nullproc(CLIENT *clnt)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	struct timeval TIMEOUT = {25, 0};
4037c478bd9Sstevel@tonic-gate 
40461961e0fSrobinson 	if (clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, NULL,
40561961e0fSrobinson 			(xdrproc_t)xdr_void, NULL, TIMEOUT) != RPC_SUCCESS)
40661961e0fSrobinson 		return (NULL);
4077c478bd9Sstevel@tonic-gate 	return ((void *)clnt);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Given a fd, find the transport device it is using and return the
4127c478bd9Sstevel@tonic-gate  * netconf entry corresponding to it.
4137c478bd9Sstevel@tonic-gate  * Note: It assumes servtpe parameter is 0 when uninitialized.
4147c478bd9Sstevel@tonic-gate  *	That is true for xprt->xp_type field.
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate struct netconfig *
__rpcfd_to_nconf(int fd,int servtype)4177c478bd9Sstevel@tonic-gate __rpcfd_to_nconf(int fd, int servtype)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4207c478bd9Sstevel@tonic-gate 	void *hndl;
4217c478bd9Sstevel@tonic-gate 	struct netconfig *nconf, *newnconf = NULL;
4227c478bd9Sstevel@tonic-gate 	major_t fdmajor;
4237c478bd9Sstevel@tonic-gate 	struct t_info tinfo;
4247c478bd9Sstevel@tonic-gate 
425e8031f0aSraf 	if (fstat(fd, &statbuf) == -1)
4267c478bd9Sstevel@tonic-gate 		return (NULL);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	fdmajor = major(statbuf.st_rdev);
4297c478bd9Sstevel@tonic-gate 	if (servtype == 0) {
4307c478bd9Sstevel@tonic-gate 		if (t_getinfo(fd, &tinfo) == -1) {
4317c478bd9Sstevel@tonic-gate 			char errorstr[100];
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 			__tli_sys_strerror(errorstr, sizeof (errorstr),
4347c478bd9Sstevel@tonic-gate 					t_errno, errno);
4357c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR, "__rpcfd_to_nconf : %s : %s",
4367c478bd9Sstevel@tonic-gate 					"could not get transport information",
4377c478bd9Sstevel@tonic-gate 					errorstr);
4387c478bd9Sstevel@tonic-gate 			return (NULL);
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 		servtype = tinfo.servtype;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	hndl = setnetconfig();
44461961e0fSrobinson 	if (hndl == NULL)
4457c478bd9Sstevel@tonic-gate 		return (NULL);
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * Go through all transports listed in /etc/netconfig looking for
4487c478bd9Sstevel@tonic-gate 	 *	transport device in use on fd.
4497c478bd9Sstevel@tonic-gate 	 * - Match on service type first
4507c478bd9Sstevel@tonic-gate 	 * - if that succeeds, match on major numbers (used for new local
4517c478bd9Sstevel@tonic-gate 	 *	transport code that is self cloning)
4527c478bd9Sstevel@tonic-gate 	 * - if that fails, assume transport device uses clone driver
4537c478bd9Sstevel@tonic-gate 	 *	and try match the fdmajor with minor number of device path
4547c478bd9Sstevel@tonic-gate 	 *	which will be the major number of transport device since it
4557c478bd9Sstevel@tonic-gate 	 *	uses the clone driver.
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	while (nconf = getnetconfig(hndl)) {
4597c478bd9Sstevel@tonic-gate 		if (__rpc_matchserv(servtype, nconf->nc_semantics) == TRUE) {
460e8031f0aSraf 			if (!stat(nconf->nc_device, &statbuf)) {
4617c478bd9Sstevel@tonic-gate 				if (fdmajor == major(statbuf.st_rdev))
4627c478bd9Sstevel@tonic-gate 					break; /* self cloning driver ? */
4637c478bd9Sstevel@tonic-gate 				if (fdmajor == minor(statbuf.st_rdev))
4647c478bd9Sstevel@tonic-gate 					break; /* clone driver! */
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	if (nconf)
4697c478bd9Sstevel@tonic-gate 		newnconf = getnetconfigent(nconf->nc_netid);
47061961e0fSrobinson 	(void) endnetconfig(hndl);
4717c478bd9Sstevel@tonic-gate 	return (newnconf);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate int
__rpc_matchserv(int servtype,unsigned int nc_semantics)4757c478bd9Sstevel@tonic-gate __rpc_matchserv(int servtype, unsigned int nc_semantics)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	switch (servtype) {
4787c478bd9Sstevel@tonic-gate 	case T_COTS:
4797c478bd9Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_COTS)
4807c478bd9Sstevel@tonic-gate 			return (TRUE);
4817c478bd9Sstevel@tonic-gate 		break;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	case T_COTS_ORD:
4847c478bd9Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_COTS_ORD)
4857c478bd9Sstevel@tonic-gate 			return (TRUE);
4867c478bd9Sstevel@tonic-gate 		break;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	case T_CLTS:
4897c478bd9Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_CLTS)
4907c478bd9Sstevel@tonic-gate 			return (TRUE);
4917c478bd9Sstevel@tonic-gate 		break;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	default:
4947c478bd9Sstevel@tonic-gate 		/* FALSE! */
4957c478bd9Sstevel@tonic-gate 		break;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	return (FALSE);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * Routines for RPC/Doors support.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate 
50561961e0fSrobinson extern bool_t __inet_netdir_is_my_host(const char *);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate bool_t
__rpc_is_local_host(const char * host)50861961e0fSrobinson __rpc_is_local_host(const char *host)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	char	buf[MAXHOSTNAMELEN + 1];
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	if (host == NULL || strcmp(host, "localhost") == 0 ||
5137c478bd9Sstevel@tonic-gate 			strcmp(host, HOST_SELF) == 0 ||
5147c478bd9Sstevel@tonic-gate 			strcmp(host, HOST_SELF_CONNECT) == 0 ||
5157c478bd9Sstevel@tonic-gate 			strlen(host) == 0)
5167c478bd9Sstevel@tonic-gate 		return (TRUE);
5177c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, buf, sizeof (buf)) < 0)
5187c478bd9Sstevel@tonic-gate 		return (FALSE);
5197c478bd9Sstevel@tonic-gate 	if (strcmp(host, buf) == 0)
5207c478bd9Sstevel@tonic-gate 		return (TRUE);
5217c478bd9Sstevel@tonic-gate 	return (__inet_netdir_is_my_host(host));
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate bool_t
__rpc_try_doors(const char * nettype,bool_t * try_others)52561961e0fSrobinson __rpc_try_doors(const char *nettype, bool_t *try_others)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	switch (getnettype(nettype)) {
5287c478bd9Sstevel@tonic-gate 	case _RPC_DOOR:
5297c478bd9Sstevel@tonic-gate 		*try_others = FALSE;
5307c478bd9Sstevel@tonic-gate 		return (TRUE);
5317c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_LOCAL:
5327c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_NETPATH:
5337c478bd9Sstevel@tonic-gate 		*try_others = TRUE;
5347c478bd9Sstevel@tonic-gate 		return (TRUE);
5357c478bd9Sstevel@tonic-gate 	default:
5367c478bd9Sstevel@tonic-gate 		*try_others = TRUE;
5377c478bd9Sstevel@tonic-gate 		return (FALSE);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate }
540