xref: /onnv-gate/usr/src/lib/libnsl/rpc/rpc_generic.c (revision 3864:2ae506652d11)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3864Sraf  * Common Development and Distribution License (the "License").
6*3864Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
220Sstevel@tonic-gate /*
23*3864Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
261219Sraf 
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
381219Sraf  * Miscl routines for RPC.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "mt.h"
420Sstevel@tonic-gate #include "rpc_mt.h"
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <rpc/rpc.h>
460Sstevel@tonic-gate #include <rpc/nettype.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/mkdev.h>
490Sstevel@tonic-gate #include <sys/stat.h>
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <errno.h>
520Sstevel@tonic-gate #include <sys/resource.h>
530Sstevel@tonic-gate #include <netconfig.h>
540Sstevel@tonic-gate #include <malloc.h>
550Sstevel@tonic-gate #include <syslog.h>
560Sstevel@tonic-gate #include <string.h>
570Sstevel@tonic-gate #include <sys/systeminfo.h>
580Sstevel@tonic-gate #include <netdir.h>
590Sstevel@tonic-gate #include <netdb.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate struct handle {
620Sstevel@tonic-gate 	NCONF_HANDLE *nhandle;
630Sstevel@tonic-gate 	int nflag;		/* Whether NETPATH or NETCONFIG */
640Sstevel@tonic-gate 	int nettype;
650Sstevel@tonic-gate };
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct _rpcnettype {
680Sstevel@tonic-gate 	const char *name;
690Sstevel@tonic-gate 	const int type;
700Sstevel@tonic-gate } _rpctypelist[] = {
710Sstevel@tonic-gate 	"netpath", _RPC_NETPATH,
720Sstevel@tonic-gate 	"visible", _RPC_VISIBLE,
730Sstevel@tonic-gate 	"circuit_v", _RPC_CIRCUIT_V,
740Sstevel@tonic-gate 	"datagram_v", _RPC_DATAGRAM_V,
750Sstevel@tonic-gate 	"circuit_n", _RPC_CIRCUIT_N,
760Sstevel@tonic-gate 	"datagram_n", _RPC_DATAGRAM_N,
770Sstevel@tonic-gate 	"tcp", _RPC_TCP,
780Sstevel@tonic-gate 	"udp", _RPC_UDP,
790Sstevel@tonic-gate 	"local", _RPC_LOCAL,
800Sstevel@tonic-gate 	"door", _RPC_DOOR,
810Sstevel@tonic-gate 	"door_local", _RPC_DOOR_LOCAL,
820Sstevel@tonic-gate 	"door_netpath", _RPC_DOOR_NETPATH,
830Sstevel@tonic-gate 	0, _RPC_NONE
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Cache the result of getrlimit(), so we don't have to do an
880Sstevel@tonic-gate  * expensive call every time. Since many old programs assume
890Sstevel@tonic-gate  * it will not return more than 1024 and use svc_fdset, return
900Sstevel@tonic-gate  * maximum of FD_SETSIZE.
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate int
__rpc_dtbsize(void)93132Srobinson __rpc_dtbsize(void)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	static int tbsize;
960Sstevel@tonic-gate 	struct rlimit rl;
970Sstevel@tonic-gate 
98132Srobinson 	if (tbsize)
990Sstevel@tonic-gate 		return (tbsize);
1000Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
1010Sstevel@tonic-gate 		tbsize = rl.rlim_max;
1020Sstevel@tonic-gate 		/*
1030Sstevel@tonic-gate 		 * backward compatibility; too many places
1040Sstevel@tonic-gate 		 * this function is called assuming it returns
1050Sstevel@tonic-gate 		 * maximum of 1024.
1060Sstevel@tonic-gate 		 */
1070Sstevel@tonic-gate 		if (tbsize > FD_SETSIZE)
1080Sstevel@tonic-gate 			tbsize = FD_SETSIZE;
1090Sstevel@tonic-gate 		return (tbsize);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * Something wrong.  I'll try to save face by returning a
1130Sstevel@tonic-gate 	 * pessimistic number.
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	return (32);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * Find the appropriate buffer size
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate uint_t
__rpc_get_t_size(t_scalar_t size,t_scalar_t bufsize)1220Sstevel@tonic-gate __rpc_get_t_size(
1230Sstevel@tonic-gate 	t_scalar_t size,	/* Size requested */
1240Sstevel@tonic-gate 	t_scalar_t bufsize)	/* Supported by the transport */
1250Sstevel@tonic-gate {
126132Srobinson 	if (bufsize == -2)	/* transfer of data unsupported */
1270Sstevel@tonic-gate 		return ((uint_t)0);
1280Sstevel@tonic-gate 	if (size == 0) {
1290Sstevel@tonic-gate 		if ((bufsize == -1) || (bufsize == 0)) {
1300Sstevel@tonic-gate 			/*
1310Sstevel@tonic-gate 			 * bufsize == -1 : No limit on the size
1320Sstevel@tonic-gate 			 * bufsize == 0 : Concept of tsdu foreign. Choose
1330Sstevel@tonic-gate 			 *			a value.
1340Sstevel@tonic-gate 			 */
1350Sstevel@tonic-gate 			return ((uint_t)RPC_MAXDATASIZE);
1360Sstevel@tonic-gate 		}
137132Srobinson 		return ((uint_t)bufsize);
1380Sstevel@tonic-gate 	}
139132Srobinson 	if ((bufsize == -1) || (bufsize == 0))
1400Sstevel@tonic-gate 		return ((uint_t)size);
1410Sstevel@tonic-gate 	/* Check whether the value is within the upper max limit */
1420Sstevel@tonic-gate 	return (size > bufsize ? (uint_t)bufsize : (uint_t)size);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Find the appropriate address buffer size
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate uint_t
__rpc_get_a_size(t_scalar_t size)1490Sstevel@tonic-gate __rpc_get_a_size(
1500Sstevel@tonic-gate 	t_scalar_t size)	/* normally tinfo.addr */
1510Sstevel@tonic-gate {
152132Srobinson 	if (size >= 0)
1530Sstevel@tonic-gate 		return ((uint_t)size);
154132Srobinson 	if (size <= -2)
1550Sstevel@tonic-gate 		return ((uint_t)0);
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 * (size == -1) No limit on the size. we impose a limit here.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	return ((uint_t)RPC_MAXADDRSIZE);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Returns the type of the network as defined in <rpc/nettype.h>
1640Sstevel@tonic-gate  * If nettype is NULL, it defaults to NETPATH.
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate static int
getnettype(const char * nettype)167132Srobinson getnettype(const char *nettype)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	int i;
1700Sstevel@tonic-gate 
171132Srobinson 	if ((nettype == NULL) || (nettype[0] == NULL))
1720Sstevel@tonic-gate 		return (_RPC_NETPATH);	/* Default */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	for (i = 0; _rpctypelist[i].name; i++)
175132Srobinson 		if (strcasecmp(nettype, _rpctypelist[i].name) == 0)
1760Sstevel@tonic-gate 			return (_rpctypelist[i].type);
1770Sstevel@tonic-gate 	return (_rpctypelist[i].type);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * For the given nettype (tcp or udp only), return the first structure found.
1820Sstevel@tonic-gate  * This should be freed by calling freenetconfigent()
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate struct netconfig *
__rpc_getconfip(char * nettype)1850Sstevel@tonic-gate __rpc_getconfip(char *nettype)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	char *netid;
188*3864Sraf 	char *netid_tcp;
189*3864Sraf 	char *netid_udp;
190*3864Sraf 	static char *netid_tcp_main = NULL;
191*3864Sraf 	static char *netid_udp_main = NULL;
192*3864Sraf 	static pthread_key_t tcp_key = PTHREAD_ONCE_KEY_NP;
193*3864Sraf 	static pthread_key_t udp_key = PTHREAD_ONCE_KEY_NP;
1940Sstevel@tonic-gate 	int main_thread;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if ((main_thread = thr_main())) {
1970Sstevel@tonic-gate 		netid_udp = netid_udp_main;
1980Sstevel@tonic-gate 		netid_tcp = netid_tcp_main;
1990Sstevel@tonic-gate 	} else {
200*3864Sraf 		(void) pthread_key_create_once_np(&tcp_key, free);
2010Sstevel@tonic-gate 		netid_tcp = pthread_getspecific(tcp_key);
202*3864Sraf 		(void) pthread_key_create_once_np(&udp_key, free);
2030Sstevel@tonic-gate 		netid_udp = pthread_getspecific(udp_key);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 	if (!netid_udp && !netid_tcp) {
2060Sstevel@tonic-gate 		struct netconfig *nconf;
2070Sstevel@tonic-gate 		void *confighandle;
2080Sstevel@tonic-gate 
209132Srobinson 		if (!(confighandle = setnetconfig()))
2100Sstevel@tonic-gate 			return (NULL);
2110Sstevel@tonic-gate 		while (nconf = getnetconfig(confighandle)) {
2120Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
2130Sstevel@tonic-gate 				if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
2140Sstevel@tonic-gate 					netid_tcp = strdup(nconf->nc_netid);
2150Sstevel@tonic-gate 					if (netid_tcp == NULL) {
2160Sstevel@tonic-gate 						syslog(LOG_ERR,
2170Sstevel@tonic-gate 							"__rpc_getconfip : "
2180Sstevel@tonic-gate 							"strdup failed");
219132Srobinson 						return (NULL);
2200Sstevel@tonic-gate 					}
2210Sstevel@tonic-gate 					if (main_thread)
2220Sstevel@tonic-gate 						netid_tcp_main = netid_tcp;
2230Sstevel@tonic-gate 					else
224132Srobinson 						(void) pthread_setspecific(
225132Srobinson 							tcp_key,
2260Sstevel@tonic-gate 							(void *)netid_tcp);
2270Sstevel@tonic-gate 				} else
2280Sstevel@tonic-gate 				if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
2290Sstevel@tonic-gate 					netid_udp = strdup(nconf->nc_netid);
2300Sstevel@tonic-gate 					if (netid_udp == NULL) {
2310Sstevel@tonic-gate 						syslog(LOG_ERR,
2320Sstevel@tonic-gate 							"__rpc_getconfip : "
2330Sstevel@tonic-gate 							"strdup failed");
234132Srobinson 						return (NULL);
2350Sstevel@tonic-gate 					}
2360Sstevel@tonic-gate 					if (main_thread)
2370Sstevel@tonic-gate 						netid_udp_main = netid_udp;
2380Sstevel@tonic-gate 					else
239132Srobinson 						(void) pthread_setspecific(
240132Srobinson 							udp_key,
2410Sstevel@tonic-gate 							(void *)netid_udp);
2420Sstevel@tonic-gate 				}
2430Sstevel@tonic-gate 			}
2440Sstevel@tonic-gate 		}
245132Srobinson 		(void) endnetconfig(confighandle);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 	if (strcmp(nettype, "udp") == 0)
2480Sstevel@tonic-gate 		netid = netid_udp;
2490Sstevel@tonic-gate 	else if (strcmp(nettype, "tcp") == 0)
2500Sstevel@tonic-gate 		netid = netid_tcp;
251132Srobinson 	else
252132Srobinson 		return (NULL);
253132Srobinson 	if ((netid == NULL) || (netid[0] == NULL))
254132Srobinson 		return (NULL);
255132Srobinson 	return (getnetconfigent(netid));
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * Returns the type of the nettype, which should then be used with
2610Sstevel@tonic-gate  * __rpc_getconf().
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate void *
__rpc_setconf(char * nettype)2640Sstevel@tonic-gate __rpc_setconf(char *nettype)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	struct handle *handle;
2670Sstevel@tonic-gate 
268132Srobinson 	handle = malloc(sizeof (struct handle));
269132Srobinson 	if (handle == NULL)
2700Sstevel@tonic-gate 		return (NULL);
2710Sstevel@tonic-gate 	switch (handle->nettype = getnettype(nettype)) {
2720Sstevel@tonic-gate 	case _RPC_DOOR_NETPATH:
2730Sstevel@tonic-gate 	case _RPC_NETPATH:
2740Sstevel@tonic-gate 	case _RPC_CIRCUIT_N:
2750Sstevel@tonic-gate 	case _RPC_DATAGRAM_N:
2760Sstevel@tonic-gate 		if (!(handle->nhandle = setnetpath())) {
2770Sstevel@tonic-gate 			free(handle);
2780Sstevel@tonic-gate 			return (NULL);
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 		handle->nflag = TRUE;
2810Sstevel@tonic-gate 		break;
2820Sstevel@tonic-gate 	case _RPC_VISIBLE:
2830Sstevel@tonic-gate 	case _RPC_CIRCUIT_V:
2840Sstevel@tonic-gate 	case _RPC_DATAGRAM_V:
2850Sstevel@tonic-gate 	case _RPC_TCP:
2860Sstevel@tonic-gate 	case _RPC_UDP:
2870Sstevel@tonic-gate 	case _RPC_LOCAL:
2880Sstevel@tonic-gate 	case _RPC_DOOR_LOCAL:
2890Sstevel@tonic-gate 		if (!(handle->nhandle = setnetconfig())) {
2900Sstevel@tonic-gate 			free(handle);
2910Sstevel@tonic-gate 			return (NULL);
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 		handle->nflag = FALSE;
2940Sstevel@tonic-gate 		break;
2950Sstevel@tonic-gate 	default:
2960Sstevel@tonic-gate 		free(handle);
2970Sstevel@tonic-gate 		return (NULL);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	return (handle);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Returns the next netconfig struct for the given "net" type.
3050Sstevel@tonic-gate  * __rpc_setconf() should have been called previously.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate struct netconfig *
__rpc_getconf(void * vhandle)3080Sstevel@tonic-gate __rpc_getconf(void *vhandle)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	struct handle *handle;
3110Sstevel@tonic-gate 	struct netconfig *nconf;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	handle = (struct handle *)vhandle;
314132Srobinson 	if (handle == NULL)
3150Sstevel@tonic-gate 		return (NULL);
316132Srobinson 	for (;;) {
3170Sstevel@tonic-gate 		if (handle->nflag)
3180Sstevel@tonic-gate 			nconf = getnetpath(handle->nhandle);
3190Sstevel@tonic-gate 		else
3200Sstevel@tonic-gate 			nconf = getnetconfig(handle->nhandle);
321132Srobinson 		if (nconf == NULL)
3220Sstevel@tonic-gate 			break;
3230Sstevel@tonic-gate 		if ((nconf->nc_semantics != NC_TPI_CLTS) &&
3240Sstevel@tonic-gate 		    (nconf->nc_semantics != NC_TPI_COTS) &&
3250Sstevel@tonic-gate 		    (nconf->nc_semantics != NC_TPI_COTS_ORD))
3260Sstevel@tonic-gate 			continue;
3270Sstevel@tonic-gate 		switch (handle->nettype) {
3280Sstevel@tonic-gate 		case _RPC_VISIBLE:
3290Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3300Sstevel@tonic-gate 				continue;
3310Sstevel@tonic-gate 			/*FALLTHROUGH*/
3320Sstevel@tonic-gate 		case _RPC_DOOR_NETPATH:
3330Sstevel@tonic-gate 			/*FALLTHROUGH*/
3340Sstevel@tonic-gate 		case _RPC_NETPATH:	/* Be happy */
3350Sstevel@tonic-gate 			break;
3360Sstevel@tonic-gate 		case _RPC_CIRCUIT_V:
3370Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3380Sstevel@tonic-gate 				continue;
3390Sstevel@tonic-gate 			/*FALLTHROUGH*/
3400Sstevel@tonic-gate 		case _RPC_CIRCUIT_N:
3410Sstevel@tonic-gate 			if ((nconf->nc_semantics != NC_TPI_COTS) &&
3420Sstevel@tonic-gate 			    (nconf->nc_semantics != NC_TPI_COTS_ORD))
3430Sstevel@tonic-gate 				continue;
3440Sstevel@tonic-gate 			break;
3450Sstevel@tonic-gate 		case _RPC_DATAGRAM_V:
3460Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3470Sstevel@tonic-gate 				continue;
3480Sstevel@tonic-gate 			/*FALLTHROUGH*/
3490Sstevel@tonic-gate 		case _RPC_DATAGRAM_N:
3500Sstevel@tonic-gate 			if (nconf->nc_semantics != NC_TPI_CLTS)
3510Sstevel@tonic-gate 				continue;
3520Sstevel@tonic-gate 			break;
3530Sstevel@tonic-gate 		case _RPC_TCP:
3540Sstevel@tonic-gate 			if (((nconf->nc_semantics != NC_TPI_COTS) &&
3550Sstevel@tonic-gate 			    (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
3560Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) &&
3570Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6)) ||
3580Sstevel@tonic-gate 			    strcmp(nconf->nc_proto, NC_TCP))
3590Sstevel@tonic-gate 				continue;
3600Sstevel@tonic-gate 			break;
3610Sstevel@tonic-gate 		case _RPC_UDP:
3620Sstevel@tonic-gate 			if ((nconf->nc_semantics != NC_TPI_CLTS) ||
3630Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) &&
3640Sstevel@tonic-gate 			    strcmp(nconf->nc_protofmly, NC_INET6)) ||
3650Sstevel@tonic-gate 			    strcmp(nconf->nc_proto, NC_UDP))
3660Sstevel@tonic-gate 				continue;
3670Sstevel@tonic-gate 			break;
3680Sstevel@tonic-gate 		case _RPC_LOCAL:
3690Sstevel@tonic-gate 		case _RPC_DOOR_LOCAL:
3700Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE))
3710Sstevel@tonic-gate 				continue;
3720Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK))
3730Sstevel@tonic-gate 				continue;
3740Sstevel@tonic-gate 			break;
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		break;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 	return (nconf);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate void
__rpc_endconf(void * vhandle)3820Sstevel@tonic-gate __rpc_endconf(void *vhandle)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	struct handle *handle;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	handle = (struct handle *)vhandle;
387132Srobinson 	if (handle == NULL)
3880Sstevel@tonic-gate 		return;
3890Sstevel@tonic-gate 	if (handle->nflag) {
390132Srobinson 		(void) endnetpath(handle->nhandle);
3910Sstevel@tonic-gate 	} else {
392132Srobinson 		(void) endnetconfig(handle->nhandle);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	free(handle);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Used to ping the NULL procedure for clnt handle.
3990Sstevel@tonic-gate  * Returns NULL if fails, else a non-NULL pointer.
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate void *
rpc_nullproc(CLIENT * clnt)4020Sstevel@tonic-gate rpc_nullproc(CLIENT *clnt)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	struct timeval TIMEOUT = {25, 0};
4050Sstevel@tonic-gate 
406132Srobinson 	if (clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void, NULL,
407132Srobinson 			(xdrproc_t)xdr_void, NULL, TIMEOUT) != RPC_SUCCESS)
408132Srobinson 		return (NULL);
4090Sstevel@tonic-gate 	return ((void *)clnt);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate  * Given a fd, find the transport device it is using and return the
4140Sstevel@tonic-gate  * netconf entry corresponding to it.
4150Sstevel@tonic-gate  * Note: It assumes servtpe parameter is 0 when uninitialized.
4160Sstevel@tonic-gate  *	That is true for xprt->xp_type field.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate struct netconfig *
__rpcfd_to_nconf(int fd,int servtype)4190Sstevel@tonic-gate __rpcfd_to_nconf(int fd, int servtype)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	struct stat statbuf;
4220Sstevel@tonic-gate 	void *hndl;
4230Sstevel@tonic-gate 	struct netconfig *nconf, *newnconf = NULL;
4240Sstevel@tonic-gate 	major_t fdmajor;
4250Sstevel@tonic-gate 	struct t_info tinfo;
4260Sstevel@tonic-gate 
4271219Sraf 	if (fstat(fd, &statbuf) == -1)
4280Sstevel@tonic-gate 		return (NULL);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	fdmajor = major(statbuf.st_rdev);
4310Sstevel@tonic-gate 	if (servtype == 0) {
4320Sstevel@tonic-gate 		if (t_getinfo(fd, &tinfo) == -1) {
4330Sstevel@tonic-gate 			char errorstr[100];
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 			__tli_sys_strerror(errorstr, sizeof (errorstr),
4360Sstevel@tonic-gate 					t_errno, errno);
4370Sstevel@tonic-gate 			(void) syslog(LOG_ERR, "__rpcfd_to_nconf : %s : %s",
4380Sstevel@tonic-gate 					"could not get transport information",
4390Sstevel@tonic-gate 					errorstr);
4400Sstevel@tonic-gate 			return (NULL);
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 		servtype = tinfo.servtype;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	hndl = setnetconfig();
446132Srobinson 	if (hndl == NULL)
4470Sstevel@tonic-gate 		return (NULL);
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * Go through all transports listed in /etc/netconfig looking for
4500Sstevel@tonic-gate 	 *	transport device in use on fd.
4510Sstevel@tonic-gate 	 * - Match on service type first
4520Sstevel@tonic-gate 	 * - if that succeeds, match on major numbers (used for new local
4530Sstevel@tonic-gate 	 *	transport code that is self cloning)
4540Sstevel@tonic-gate 	 * - if that fails, assume transport device uses clone driver
4550Sstevel@tonic-gate 	 *	and try match the fdmajor with minor number of device path
4560Sstevel@tonic-gate 	 *	which will be the major number of transport device since it
4570Sstevel@tonic-gate 	 *	uses the clone driver.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	while (nconf = getnetconfig(hndl)) {
4610Sstevel@tonic-gate 		if (__rpc_matchserv(servtype, nconf->nc_semantics) == TRUE) {
4621219Sraf 			if (!stat(nconf->nc_device, &statbuf)) {
4630Sstevel@tonic-gate 				if (fdmajor == major(statbuf.st_rdev))
4640Sstevel@tonic-gate 					break; /* self cloning driver ? */
4650Sstevel@tonic-gate 				if (fdmajor == minor(statbuf.st_rdev))
4660Sstevel@tonic-gate 					break; /* clone driver! */
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	if (nconf)
4710Sstevel@tonic-gate 		newnconf = getnetconfigent(nconf->nc_netid);
472132Srobinson 	(void) endnetconfig(hndl);
4730Sstevel@tonic-gate 	return (newnconf);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate int
__rpc_matchserv(int servtype,unsigned int nc_semantics)4770Sstevel@tonic-gate __rpc_matchserv(int servtype, unsigned int nc_semantics)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	switch (servtype) {
4800Sstevel@tonic-gate 	case T_COTS:
4810Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_COTS)
4820Sstevel@tonic-gate 			return (TRUE);
4830Sstevel@tonic-gate 		break;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	case T_COTS_ORD:
4860Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_COTS_ORD)
4870Sstevel@tonic-gate 			return (TRUE);
4880Sstevel@tonic-gate 		break;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	case T_CLTS:
4910Sstevel@tonic-gate 		if (nc_semantics == NC_TPI_CLTS)
4920Sstevel@tonic-gate 			return (TRUE);
4930Sstevel@tonic-gate 		break;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	default:
4960Sstevel@tonic-gate 		/* FALSE! */
4970Sstevel@tonic-gate 		break;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 	return (FALSE);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * Routines for RPC/Doors support.
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate 
507132Srobinson extern bool_t __inet_netdir_is_my_host(const char *);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate bool_t
__rpc_is_local_host(const char * host)510132Srobinson __rpc_is_local_host(const char *host)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	char	buf[MAXHOSTNAMELEN + 1];
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (host == NULL || strcmp(host, "localhost") == 0 ||
5150Sstevel@tonic-gate 			strcmp(host, HOST_SELF) == 0 ||
5160Sstevel@tonic-gate 			strcmp(host, HOST_SELF_CONNECT) == 0 ||
5170Sstevel@tonic-gate 			strlen(host) == 0)
5180Sstevel@tonic-gate 		return (TRUE);
5190Sstevel@tonic-gate 	if (sysinfo(SI_HOSTNAME, buf, sizeof (buf)) < 0)
5200Sstevel@tonic-gate 		return (FALSE);
5210Sstevel@tonic-gate 	if (strcmp(host, buf) == 0)
5220Sstevel@tonic-gate 		return (TRUE);
5230Sstevel@tonic-gate 	return (__inet_netdir_is_my_host(host));
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate bool_t
__rpc_try_doors(const char * nettype,bool_t * try_others)527132Srobinson __rpc_try_doors(const char *nettype, bool_t *try_others)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	switch (getnettype(nettype)) {
5300Sstevel@tonic-gate 	case _RPC_DOOR:
5310Sstevel@tonic-gate 		*try_others = FALSE;
5320Sstevel@tonic-gate 		return (TRUE);
5330Sstevel@tonic-gate 	case _RPC_DOOR_LOCAL:
5340Sstevel@tonic-gate 	case _RPC_DOOR_NETPATH:
5350Sstevel@tonic-gate 		*try_others = TRUE;
5360Sstevel@tonic-gate 		return (TRUE);
5370Sstevel@tonic-gate 	default:
5380Sstevel@tonic-gate 		*try_others = TRUE;
5390Sstevel@tonic-gate 		return (FALSE);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate }
542