xref: /titanic_50/usr/src/lib/libnsl/rpc/rpc_generic.c (revision cb6207858a9fcc2feaee22e626912fba281ac969)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb620785Sraf  * Common Development and Distribution License (the "License").
6*cb620785Sraf  * 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 /*
23*cb620785Sraf  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
38e8031f0aSraf  * Miscl routines for RPC.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "mt.h"
427c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
467c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
497c478bd9Sstevel@tonic-gate #include <sys/stat.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/resource.h>
537c478bd9Sstevel@tonic-gate #include <netconfig.h>
547c478bd9Sstevel@tonic-gate #include <malloc.h>
557c478bd9Sstevel@tonic-gate #include <syslog.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
587c478bd9Sstevel@tonic-gate #include <netdir.h>
597c478bd9Sstevel@tonic-gate #include <netdb.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct handle {
627c478bd9Sstevel@tonic-gate 	NCONF_HANDLE *nhandle;
637c478bd9Sstevel@tonic-gate 	int nflag;		/* Whether NETPATH or NETCONFIG */
647c478bd9Sstevel@tonic-gate 	int nettype;
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate struct _rpcnettype {
687c478bd9Sstevel@tonic-gate 	const char *name;
697c478bd9Sstevel@tonic-gate 	const int type;
707c478bd9Sstevel@tonic-gate } _rpctypelist[] = {
717c478bd9Sstevel@tonic-gate 	"netpath", _RPC_NETPATH,
727c478bd9Sstevel@tonic-gate 	"visible", _RPC_VISIBLE,
737c478bd9Sstevel@tonic-gate 	"circuit_v", _RPC_CIRCUIT_V,
747c478bd9Sstevel@tonic-gate 	"datagram_v", _RPC_DATAGRAM_V,
757c478bd9Sstevel@tonic-gate 	"circuit_n", _RPC_CIRCUIT_N,
767c478bd9Sstevel@tonic-gate 	"datagram_n", _RPC_DATAGRAM_N,
777c478bd9Sstevel@tonic-gate 	"tcp", _RPC_TCP,
787c478bd9Sstevel@tonic-gate 	"udp", _RPC_UDP,
797c478bd9Sstevel@tonic-gate 	"local", _RPC_LOCAL,
807c478bd9Sstevel@tonic-gate 	"door", _RPC_DOOR,
817c478bd9Sstevel@tonic-gate 	"door_local", _RPC_DOOR_LOCAL,
827c478bd9Sstevel@tonic-gate 	"door_netpath", _RPC_DOOR_NETPATH,
837c478bd9Sstevel@tonic-gate 	0, _RPC_NONE
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Cache the result of getrlimit(), so we don't have to do an
887c478bd9Sstevel@tonic-gate  * expensive call every time. Since many old programs assume
897c478bd9Sstevel@tonic-gate  * it will not return more than 1024 and use svc_fdset, return
907c478bd9Sstevel@tonic-gate  * maximum of FD_SETSIZE.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
__rpc_dtbsize(void)9361961e0fSrobinson __rpc_dtbsize(void)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	static int tbsize;
967c478bd9Sstevel@tonic-gate 	struct rlimit rl;
977c478bd9Sstevel@tonic-gate 
9861961e0fSrobinson 	if (tbsize)
997c478bd9Sstevel@tonic-gate 		return (tbsize);
1007c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
1017c478bd9Sstevel@tonic-gate 		tbsize = rl.rlim_max;
1027c478bd9Sstevel@tonic-gate 		/*
1037c478bd9Sstevel@tonic-gate 		 * backward compatibility; too many places
1047c478bd9Sstevel@tonic-gate 		 * this function is called assuming it returns
1057c478bd9Sstevel@tonic-gate 		 * maximum of 1024.
1067c478bd9Sstevel@tonic-gate 		 */
1077c478bd9Sstevel@tonic-gate 		if (tbsize > FD_SETSIZE)
1087c478bd9Sstevel@tonic-gate 			tbsize = FD_SETSIZE;
1097c478bd9Sstevel@tonic-gate 		return (tbsize);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * Something wrong.  I'll try to save face by returning a
1137c478bd9Sstevel@tonic-gate 	 * pessimistic number.
1147c478bd9Sstevel@tonic-gate 	 */
1157c478bd9Sstevel@tonic-gate 	return (32);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Find the appropriate buffer size
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate uint_t
__rpc_get_t_size(t_scalar_t size,t_scalar_t bufsize)1227c478bd9Sstevel@tonic-gate __rpc_get_t_size(
1237c478bd9Sstevel@tonic-gate 	t_scalar_t size,	/* Size requested */
1247c478bd9Sstevel@tonic-gate 	t_scalar_t bufsize)	/* Supported by the transport */
1257c478bd9Sstevel@tonic-gate {
12661961e0fSrobinson 	if (bufsize == -2)	/* transfer of data unsupported */
1277c478bd9Sstevel@tonic-gate 		return ((uint_t)0);
1287c478bd9Sstevel@tonic-gate 	if (size == 0) {
1297c478bd9Sstevel@tonic-gate 		if ((bufsize == -1) || (bufsize == 0)) {
1307c478bd9Sstevel@tonic-gate 			/*
1317c478bd9Sstevel@tonic-gate 			 * bufsize == -1 : No limit on the size
1327c478bd9Sstevel@tonic-gate 			 * bufsize == 0 : Concept of tsdu foreign. Choose
1337c478bd9Sstevel@tonic-gate 			 *			a value.
1347c478bd9Sstevel@tonic-gate 			 */
1357c478bd9Sstevel@tonic-gate 			return ((uint_t)RPC_MAXDATASIZE);
13661961e0fSrobinson 		}
1377c478bd9Sstevel@tonic-gate 		return ((uint_t)bufsize);
1387c478bd9Sstevel@tonic-gate 	}
13961961e0fSrobinson 	if ((bufsize == -1) || (bufsize == 0))
1407c478bd9Sstevel@tonic-gate 		return ((uint_t)size);
1417c478bd9Sstevel@tonic-gate 	/* Check whether the value is within the upper max limit */
1427c478bd9Sstevel@tonic-gate 	return (size > bufsize ? (uint_t)bufsize : (uint_t)size);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Find the appropriate address buffer size
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate uint_t
__rpc_get_a_size(t_scalar_t size)1497c478bd9Sstevel@tonic-gate __rpc_get_a_size(
1507c478bd9Sstevel@tonic-gate 	t_scalar_t size)	/* normally tinfo.addr */
1517c478bd9Sstevel@tonic-gate {
15261961e0fSrobinson 	if (size >= 0)
1537c478bd9Sstevel@tonic-gate 		return ((uint_t)size);
15461961e0fSrobinson 	if (size <= -2)
1557c478bd9Sstevel@tonic-gate 		return ((uint_t)0);
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * (size == -1) No limit on the size. we impose a limit here.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	return ((uint_t)RPC_MAXADDRSIZE);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Returns the type of the network as defined in <rpc/nettype.h>
1647c478bd9Sstevel@tonic-gate  * If nettype is NULL, it defaults to NETPATH.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate static int
getnettype(const char * nettype)16761961e0fSrobinson getnettype(const char *nettype)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	int i;
1707c478bd9Sstevel@tonic-gate 
17161961e0fSrobinson 	if ((nettype == NULL) || (nettype[0] == NULL))
1727c478bd9Sstevel@tonic-gate 		return (_RPC_NETPATH);	/* Default */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	for (i = 0; _rpctypelist[i].name; i++)
17561961e0fSrobinson 		if (strcasecmp(nettype, _rpctypelist[i].name) == 0)
1767c478bd9Sstevel@tonic-gate 			return (_rpctypelist[i].type);
1777c478bd9Sstevel@tonic-gate 	return (_rpctypelist[i].type);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * For the given nettype (tcp or udp only), return the first structure found.
1827c478bd9Sstevel@tonic-gate  * This should be freed by calling freenetconfigent()
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate struct netconfig *
__rpc_getconfip(char * nettype)1857c478bd9Sstevel@tonic-gate __rpc_getconfip(char *nettype)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	char *netid;
188*cb620785Sraf 	char *netid_tcp;
189*cb620785Sraf 	char *netid_udp;
190*cb620785Sraf 	static char *netid_tcp_main = NULL;
191*cb620785Sraf 	static char *netid_udp_main = NULL;
192*cb620785Sraf 	static pthread_key_t tcp_key = PTHREAD_ONCE_KEY_NP;
193*cb620785Sraf 	static pthread_key_t udp_key = PTHREAD_ONCE_KEY_NP;
1947c478bd9Sstevel@tonic-gate 	int main_thread;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if ((main_thread = thr_main())) {
1977c478bd9Sstevel@tonic-gate 		netid_udp = netid_udp_main;
1987c478bd9Sstevel@tonic-gate 		netid_tcp = netid_tcp_main;
1997c478bd9Sstevel@tonic-gate 	} else {
200*cb620785Sraf 		(void) pthread_key_create_once_np(&tcp_key, free);
2017c478bd9Sstevel@tonic-gate 		netid_tcp = pthread_getspecific(tcp_key);
202*cb620785Sraf 		(void) pthread_key_create_once_np(&udp_key, free);
2037c478bd9Sstevel@tonic-gate 		netid_udp = pthread_getspecific(udp_key);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	if (!netid_udp && !netid_tcp) {
2067c478bd9Sstevel@tonic-gate 		struct netconfig *nconf;
2077c478bd9Sstevel@tonic-gate 		void *confighandle;
2087c478bd9Sstevel@tonic-gate 
20961961e0fSrobinson 		if (!(confighandle = setnetconfig()))
2107c478bd9Sstevel@tonic-gate 			return (NULL);
2117c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(confighandle)) {
2127c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
2137c478bd9Sstevel@tonic-gate 				if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
2147c478bd9Sstevel@tonic-gate 					netid_tcp = strdup(nconf->nc_netid);
2157c478bd9Sstevel@tonic-gate 					if (netid_tcp == NULL) {
2167c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR,
2177c478bd9Sstevel@tonic-gate 							"__rpc_getconfip : "
2187c478bd9Sstevel@tonic-gate 							"strdup failed");
21961961e0fSrobinson 						return (NULL);
2207c478bd9Sstevel@tonic-gate 					}
2217c478bd9Sstevel@tonic-gate 					if (main_thread)
2227c478bd9Sstevel@tonic-gate 						netid_tcp_main = netid_tcp;
2237c478bd9Sstevel@tonic-gate 					else
22461961e0fSrobinson 						(void) pthread_setspecific(
22561961e0fSrobinson 							tcp_key,
2267c478bd9Sstevel@tonic-gate 							(void *)netid_tcp);
2277c478bd9Sstevel@tonic-gate 				} else
2287c478bd9Sstevel@tonic-gate 				if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
2297c478bd9Sstevel@tonic-gate 					netid_udp = strdup(nconf->nc_netid);
2307c478bd9Sstevel@tonic-gate 					if (netid_udp == NULL) {
2317c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR,
2327c478bd9Sstevel@tonic-gate 							"__rpc_getconfip : "
2337c478bd9Sstevel@tonic-gate 							"strdup failed");
23461961e0fSrobinson 						return (NULL);
2357c478bd9Sstevel@tonic-gate 					}
2367c478bd9Sstevel@tonic-gate 					if (main_thread)
2377c478bd9Sstevel@tonic-gate 						netid_udp_main = netid_udp;
2387c478bd9Sstevel@tonic-gate 					else
23961961e0fSrobinson 						(void) pthread_setspecific(
24061961e0fSrobinson 							udp_key,
2417c478bd9Sstevel@tonic-gate 							(void *)netid_udp);
2427c478bd9Sstevel@tonic-gate 				}
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 		}
24561961e0fSrobinson 		(void) endnetconfig(confighandle);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	if (strcmp(nettype, "udp") == 0)
2487c478bd9Sstevel@tonic-gate 		netid = netid_udp;
2497c478bd9Sstevel@tonic-gate 	else if (strcmp(nettype, "tcp") == 0)
2507c478bd9Sstevel@tonic-gate 		netid = netid_tcp;
25161961e0fSrobinson 	else
25261961e0fSrobinson 		return (NULL);
25361961e0fSrobinson 	if ((netid == NULL) || (netid[0] == NULL))
25461961e0fSrobinson 		return (NULL);
25561961e0fSrobinson 	return (getnetconfigent(netid));
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * Returns the type of the nettype, which should then be used with
2617c478bd9Sstevel@tonic-gate  * __rpc_getconf().
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate void *
__rpc_setconf(char * nettype)2647c478bd9Sstevel@tonic-gate __rpc_setconf(char *nettype)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	struct handle *handle;
2677c478bd9Sstevel@tonic-gate 
26861961e0fSrobinson 	handle = malloc(sizeof (struct handle));
26961961e0fSrobinson 	if (handle == NULL)
2707c478bd9Sstevel@tonic-gate 		return (NULL);
2717c478bd9Sstevel@tonic-gate 	switch (handle->nettype = getnettype(nettype)) {
2727c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_NETPATH:
2737c478bd9Sstevel@tonic-gate 	case _RPC_NETPATH:
2747c478bd9Sstevel@tonic-gate 	case _RPC_CIRCUIT_N:
2757c478bd9Sstevel@tonic-gate 	case _RPC_DATAGRAM_N:
2767c478bd9Sstevel@tonic-gate 		if (!(handle->nhandle = setnetpath())) {
2777c478bd9Sstevel@tonic-gate 			free(handle);
2787c478bd9Sstevel@tonic-gate 			return (NULL);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		handle->nflag = TRUE;
2817c478bd9Sstevel@tonic-gate 		break;
2827c478bd9Sstevel@tonic-gate 	case _RPC_VISIBLE:
2837c478bd9Sstevel@tonic-gate 	case _RPC_CIRCUIT_V:
2847c478bd9Sstevel@tonic-gate 	case _RPC_DATAGRAM_V:
2857c478bd9Sstevel@tonic-gate 	case _RPC_TCP:
2867c478bd9Sstevel@tonic-gate 	case _RPC_UDP:
2877c478bd9Sstevel@tonic-gate 	case _RPC_LOCAL:
2887c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_LOCAL:
2897c478bd9Sstevel@tonic-gate 		if (!(handle->nhandle = setnetconfig())) {
2907c478bd9Sstevel@tonic-gate 			free(handle);
2917c478bd9Sstevel@tonic-gate 			return (NULL);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		handle->nflag = FALSE;
2947c478bd9Sstevel@tonic-gate 		break;
2957c478bd9Sstevel@tonic-gate 	default:
2967c478bd9Sstevel@tonic-gate 		free(handle);
2977c478bd9Sstevel@tonic-gate 		return (NULL);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return (handle);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * Returns the next netconfig struct for the given "net" type.
3057c478bd9Sstevel@tonic-gate  * __rpc_setconf() should have been called previously.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate struct netconfig *
__rpc_getconf(void * vhandle)3087c478bd9Sstevel@tonic-gate __rpc_getconf(void *vhandle)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	struct handle *handle;
3117c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	handle = (struct handle *)vhandle;
31461961e0fSrobinson 	if (handle == NULL)
3157c478bd9Sstevel@tonic-gate 		return (NULL);
31661961e0fSrobinson 	for (;;) {
3177c478bd9Sstevel@tonic-gate 		if (handle->nflag)
3187c478bd9Sstevel@tonic-gate 			nconf = getnetpath(handle->nhandle);
3197c478bd9Sstevel@tonic-gate 		else
3207c478bd9Sstevel@tonic-gate 			nconf = getnetconfig(handle->nhandle);
32161961e0fSrobinson 		if (nconf == NULL)
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 		if ((nconf->nc_semantics != NC_TPI_CLTS) &&
3247c478bd9Sstevel@tonic-gate 		    (nconf->nc_semantics != NC_TPI_COTS) &&
3257c478bd9Sstevel@tonic-gate 		    (nconf->nc_semantics != NC_TPI_COTS_ORD))
3267c478bd9Sstevel@tonic-gate 			continue;
3277c478bd9Sstevel@tonic-gate 		switch (handle->nettype) {
3287c478bd9Sstevel@tonic-gate 		case _RPC_VISIBLE:
3297c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3307c478bd9Sstevel@tonic-gate 				continue;
3317c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3327c478bd9Sstevel@tonic-gate 		case _RPC_DOOR_NETPATH:
3337c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3347c478bd9Sstevel@tonic-gate 		case _RPC_NETPATH:	/* Be happy */
3357c478bd9Sstevel@tonic-gate 			break;
3367c478bd9Sstevel@tonic-gate 		case _RPC_CIRCUIT_V:
3377c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3387c478bd9Sstevel@tonic-gate 				continue;
3397c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3407c478bd9Sstevel@tonic-gate 		case _RPC_CIRCUIT_N:
3417c478bd9Sstevel@tonic-gate 			if ((nconf->nc_semantics != NC_TPI_COTS) &&
3427c478bd9Sstevel@tonic-gate 			    (nconf->nc_semantics != NC_TPI_COTS_ORD))
3437c478bd9Sstevel@tonic-gate 				continue;
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 		case _RPC_DATAGRAM_V:
3467c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3477c478bd9Sstevel@tonic-gate 				continue;
3487c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
3497c478bd9Sstevel@tonic-gate 		case _RPC_DATAGRAM_N:
3507c478bd9Sstevel@tonic-gate 			if (nconf->nc_semantics != NC_TPI_CLTS)
3517c478bd9Sstevel@tonic-gate 				continue;
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 		case _RPC_TCP:
3547c478bd9Sstevel@tonic-gate 			if (((nconf->nc_semantics != NC_TPI_COTS) &&
3557c478bd9Sstevel@tonic-gate 			    (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
3567c478bd9Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) &&
3577c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6)) ||
3587c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_proto, NC_TCP))
3597c478bd9Sstevel@tonic-gate 				continue;
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		case _RPC_UDP:
3627c478bd9Sstevel@tonic-gate 			if ((nconf->nc_semantics != NC_TPI_CLTS) ||
3637c478bd9Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) &&
3647c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6)) ||
3657c478bd9Sstevel@tonic-gate 			    strcmp(nconf->nc_proto, NC_UDP))
3667c478bd9Sstevel@tonic-gate 				continue;
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 		case _RPC_LOCAL:
3697c478bd9Sstevel@tonic-gate 		case _RPC_DOOR_LOCAL:
3707c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3717c478bd9Sstevel@tonic-gate 				continue;
3727c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK))
3737c478bd9Sstevel@tonic-gate 				continue;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		break;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	return (nconf);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate void
__rpc_endconf(void * vhandle)3827c478bd9Sstevel@tonic-gate __rpc_endconf(void *vhandle)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	struct handle *handle;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	handle = (struct handle *)vhandle;
38761961e0fSrobinson 	if (handle == NULL)
3887c478bd9Sstevel@tonic-gate 		return;
3897c478bd9Sstevel@tonic-gate 	if (handle->nflag) {
39061961e0fSrobinson 		(void) endnetpath(handle->nhandle);
3917c478bd9Sstevel@tonic-gate 	} else {
39261961e0fSrobinson 		(void) endnetconfig(handle->nhandle);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	free(handle);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * Used to ping the NULL procedure for clnt handle.
3997c478bd9Sstevel@tonic-gate  * Returns NULL if fails, else a non-NULL pointer.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate void *
rpc_nullproc(CLIENT * clnt)4027c478bd9Sstevel@tonic-gate rpc_nullproc(CLIENT *clnt)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	struct timeval TIMEOUT = {25, 0};
4057c478bd9Sstevel@tonic-gate 
40661961e0fSrobinson 	if (clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, NULL,
40761961e0fSrobinson 			(xdrproc_t)xdr_void, NULL, TIMEOUT) != RPC_SUCCESS)
40861961e0fSrobinson 		return (NULL);
4097c478bd9Sstevel@tonic-gate 	return ((void *)clnt);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Given a fd, find the transport device it is using and return the
4147c478bd9Sstevel@tonic-gate  * netconf entry corresponding to it.
4157c478bd9Sstevel@tonic-gate  * Note: It assumes servtpe parameter is 0 when uninitialized.
4167c478bd9Sstevel@tonic-gate  *	That is true for xprt->xp_type field.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate struct netconfig *
__rpcfd_to_nconf(int fd,int servtype)4197c478bd9Sstevel@tonic-gate __rpcfd_to_nconf(int fd, int servtype)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	struct stat statbuf;
4227c478bd9Sstevel@tonic-gate 	void *hndl;
4237c478bd9Sstevel@tonic-gate 	struct netconfig *nconf, *newnconf = NULL;
4247c478bd9Sstevel@tonic-gate 	major_t fdmajor;
4257c478bd9Sstevel@tonic-gate 	struct t_info tinfo;
4267c478bd9Sstevel@tonic-gate 
427e8031f0aSraf 	if (fstat(fd, &statbuf) == -1)
4287c478bd9Sstevel@tonic-gate 		return (NULL);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	fdmajor = major(statbuf.st_rdev);
4317c478bd9Sstevel@tonic-gate 	if (servtype == 0) {
4327c478bd9Sstevel@tonic-gate 		if (t_getinfo(fd, &tinfo) == -1) {
4337c478bd9Sstevel@tonic-gate 			char errorstr[100];
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 			__tli_sys_strerror(errorstr, sizeof (errorstr),
4367c478bd9Sstevel@tonic-gate 					t_errno, errno);
4377c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR, "__rpcfd_to_nconf : %s : %s",
4387c478bd9Sstevel@tonic-gate 					"could not get transport information",
4397c478bd9Sstevel@tonic-gate 					errorstr);
4407c478bd9Sstevel@tonic-gate 			return (NULL);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		servtype = tinfo.servtype;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	hndl = setnetconfig();
44661961e0fSrobinson 	if (hndl == NULL)
4477c478bd9Sstevel@tonic-gate 		return (NULL);
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Go through all transports listed in /etc/netconfig looking for
4507c478bd9Sstevel@tonic-gate 	 *	transport device in use on fd.
4517c478bd9Sstevel@tonic-gate 	 * - Match on service type first
4527c478bd9Sstevel@tonic-gate 	 * - if that succeeds, match on major numbers (used for new local
4537c478bd9Sstevel@tonic-gate 	 *	transport code that is self cloning)
4547c478bd9Sstevel@tonic-gate 	 * - if that fails, assume transport device uses clone driver
4557c478bd9Sstevel@tonic-gate 	 *	and try match the fdmajor with minor number of device path
4567c478bd9Sstevel@tonic-gate 	 *	which will be the major number of transport device since it
4577c478bd9Sstevel@tonic-gate 	 *	uses the clone driver.
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	while (nconf = getnetconfig(hndl)) {
4617c478bd9Sstevel@tonic-gate 		if (__rpc_matchserv(servtype, nconf->nc_semantics) == TRUE) {
462e8031f0aSraf 			if (!stat(nconf->nc_device, &statbuf)) {
4637c478bd9Sstevel@tonic-gate 				if (fdmajor == major(statbuf.st_rdev))
4647c478bd9Sstevel@tonic-gate 					break; /* self cloning driver ? */
4657c478bd9Sstevel@tonic-gate 				if (fdmajor == minor(statbuf.st_rdev))
4667c478bd9Sstevel@tonic-gate 					break; /* clone driver! */
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	if (nconf)
4717c478bd9Sstevel@tonic-gate 		newnconf = getnetconfigent(nconf->nc_netid);
47261961e0fSrobinson 	(void) endnetconfig(hndl);
4737c478bd9Sstevel@tonic-gate 	return (newnconf);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate int
__rpc_matchserv(int servtype,unsigned int nc_semantics)4777c478bd9Sstevel@tonic-gate __rpc_matchserv(int servtype, unsigned int nc_semantics)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	switch (servtype) {
4807c478bd9Sstevel@tonic-gate 	case T_COTS:
4817c478bd9Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_COTS)
4827c478bd9Sstevel@tonic-gate 			return (TRUE);
4837c478bd9Sstevel@tonic-gate 		break;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	case T_COTS_ORD:
4867c478bd9Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_COTS_ORD)
4877c478bd9Sstevel@tonic-gate 			return (TRUE);
4887c478bd9Sstevel@tonic-gate 		break;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	case T_CLTS:
4917c478bd9Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_CLTS)
4927c478bd9Sstevel@tonic-gate 			return (TRUE);
4937c478bd9Sstevel@tonic-gate 		break;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	default:
4967c478bd9Sstevel@tonic-gate 		/* FALSE! */
4977c478bd9Sstevel@tonic-gate 		break;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	return (FALSE);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Routines for RPC/Doors support.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate 
50761961e0fSrobinson extern bool_t __inet_netdir_is_my_host(const char *);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate bool_t
__rpc_is_local_host(const char * host)51061961e0fSrobinson __rpc_is_local_host(const char *host)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	char	buf[MAXHOSTNAMELEN + 1];
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (host == NULL || strcmp(host, "localhost") == 0 ||
5157c478bd9Sstevel@tonic-gate 			strcmp(host, HOST_SELF) == 0 ||
5167c478bd9Sstevel@tonic-gate 			strcmp(host, HOST_SELF_CONNECT) == 0 ||
5177c478bd9Sstevel@tonic-gate 			strlen(host) == 0)
5187c478bd9Sstevel@tonic-gate 		return (TRUE);
5197c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, buf, sizeof (buf)) < 0)
5207c478bd9Sstevel@tonic-gate 		return (FALSE);
5217c478bd9Sstevel@tonic-gate 	if (strcmp(host, buf) == 0)
5227c478bd9Sstevel@tonic-gate 		return (TRUE);
5237c478bd9Sstevel@tonic-gate 	return (__inet_netdir_is_my_host(host));
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate bool_t
__rpc_try_doors(const char * nettype,bool_t * try_others)52761961e0fSrobinson __rpc_try_doors(const char *nettype, bool_t *try_others)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	switch (getnettype(nettype)) {
5307c478bd9Sstevel@tonic-gate 	case _RPC_DOOR:
5317c478bd9Sstevel@tonic-gate 		*try_others = FALSE;
5327c478bd9Sstevel@tonic-gate 		return (TRUE);
5337c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_LOCAL:
5347c478bd9Sstevel@tonic-gate 	case _RPC_DOOR_NETPATH:
5357c478bd9Sstevel@tonic-gate 		*try_others = TRUE;
5367c478bd9Sstevel@tonic-gate 		return (TRUE);
5377c478bd9Sstevel@tonic-gate 	default:
5387c478bd9Sstevel@tonic-gate 		*try_others = TRUE;
5397c478bd9Sstevel@tonic-gate 		return (FALSE);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate }
542