xref: /illumos-gate/usr/src/cmd/ypcmd/rpc_bootstrap.c (revision 19c77476)
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*19c77476Spwernau  * Common Development and Distribution License (the "License").
6*19c77476Spwernau  * 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  *
21*19c77476Spwernau  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
307c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
317c478bd9Sstevel@tonic-gate  * California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <ctype.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <tiuser.h>
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
447c478bd9Sstevel@tonic-gate #include <sys/socket.h>
457c478bd9Sstevel@tonic-gate #include <netdir.h>
467c478bd9Sstevel@tonic-gate #include <netdb.h>
477c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
487c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
497c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate CLIENT *__clnt_tp_create_bootstrap();
527c478bd9Sstevel@tonic-gate int __rpcb_getaddr_bootstrap();
53*19c77476Spwernau struct hostent *__files_gethostbyname(char *, sa_family_t);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern int hostNotKnownLocally;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static char *__map_addr();
58*19c77476Spwernau static struct hostent host;
59*19c77476Spwernau static char hostaddr[sizeof (struct in6_addr)];
60*19c77476Spwernau static char *host_aliases[MAXALIASES];
61*19c77476Spwernau static char *host_addrs[] = {
62*19c77476Spwernau 	hostaddr,
63*19c77476Spwernau 	NULL
64*19c77476Spwernau };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * __clnt_tp_create_bootstrap()
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * This routine is NOT TRANSPORT INDEPENDENT.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * It relies on the local /etc/hosts file for hostname to address
727c478bd9Sstevel@tonic-gate  * translation and does it itself instead of calling netdir_getbyname
73*19c77476Spwernau  * thereby avoids recursion.  Secondarily, it will use a validated
74*19c77476Spwernau  * IP address directly.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate CLIENT *
__clnt_tp_create_bootstrap(hostname,prog,vers,nconf)777c478bd9Sstevel@tonic-gate __clnt_tp_create_bootstrap(hostname, prog, vers, nconf)
787c478bd9Sstevel@tonic-gate 	char *hostname;
79*19c77476Spwernau 	ulong_t prog, vers;
807c478bd9Sstevel@tonic-gate 	struct netconfig    *nconf;
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	CLIENT *cl;
837c478bd9Sstevel@tonic-gate 	struct netbuf	*svc_taddr;
847c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sa;
857c478bd9Sstevel@tonic-gate 	int fd;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (nconf == (struct netconfig *)NULL) {
887c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
897c478bd9Sstevel@tonic-gate 		return (NULL);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
927c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
937c478bd9Sstevel@tonic-gate 		return (NULL);
947c478bd9Sstevel@tonic-gate 	}
95*19c77476Spwernau 	svc_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
967c478bd9Sstevel@tonic-gate 	if (! svc_taddr) {
977c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
987c478bd9Sstevel@tonic-gate 		t_close(fd);
997c478bd9Sstevel@tonic-gate 		return (NULL);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	sa = (struct sockaddr_in6 *)calloc(1, sizeof (*sa));
1027c478bd9Sstevel@tonic-gate 	if (! sa) {
1037c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1047c478bd9Sstevel@tonic-gate 		t_close(fd);
1057c478bd9Sstevel@tonic-gate 		free(svc_taddr);
1067c478bd9Sstevel@tonic-gate 		return (NULL);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	svc_taddr->maxlen = svc_taddr->len = sizeof (*sa);
1097c478bd9Sstevel@tonic-gate 	svc_taddr->buf = (char *)sa;
1107c478bd9Sstevel@tonic-gate 	if (__rpcb_getaddr_bootstrap(prog,
1117c478bd9Sstevel@tonic-gate 		vers, nconf, svc_taddr, hostname) == FALSE) {
1127c478bd9Sstevel@tonic-gate 		t_close(fd);
1137c478bd9Sstevel@tonic-gate 		free(svc_taddr);
1147c478bd9Sstevel@tonic-gate 		free(sa);
1157c478bd9Sstevel@tonic-gate 		return (NULL);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
1187c478bd9Sstevel@tonic-gate 	cl = __nis_clnt_create(fd, nconf, 0, svc_taddr, 0, prog, vers, 0, 0);
1197c478bd9Sstevel@tonic-gate 	if (cl == 0) {
1207c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_SUCCESS)
1217c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
1227c478bd9Sstevel@tonic-gate 		t_close(fd);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	free(svc_taddr);
1257c478bd9Sstevel@tonic-gate 	free(sa);
1267c478bd9Sstevel@tonic-gate 	return (cl);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * __rpcb_getaddr_bootstrap()
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * This is our internal function that replaces rpcb_getaddr(). We
1337c478bd9Sstevel@tonic-gate  * build our own to prevent calling netdir_getbyname() which could
1347c478bd9Sstevel@tonic-gate  * recurse to the nameservice.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate int
__rpcb_getaddr_bootstrap(program,version,nconf,address,hostname)1377c478bd9Sstevel@tonic-gate __rpcb_getaddr_bootstrap(program, version, nconf, address, hostname)
138*19c77476Spwernau 	ulong_t program;
139*19c77476Spwernau 	ulong_t version;
1407c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1417c478bd9Sstevel@tonic-gate 	struct netbuf *address; /* populate with the taddr of the service */
1427c478bd9Sstevel@tonic-gate 	char *hostname;
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	char *svc_uaddr;
145*19c77476Spwernau 	struct hostent *hent, tmphent;
1467c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sa;
1477c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sa6;
1487c478bd9Sstevel@tonic-gate 	struct netbuf rpcb_taddr;
1497c478bd9Sstevel@tonic-gate 	struct sockaddr_in local_sa;
1507c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 local_sa6;
1517c478bd9Sstevel@tonic-gate 	in_port_t inport;
1527c478bd9Sstevel@tonic-gate 	int p1, p2;
1537c478bd9Sstevel@tonic-gate 	char *ipaddr, *port;
1547c478bd9Sstevel@tonic-gate 	int i, ipaddrlen;
155*19c77476Spwernau 	sa_family_t type;
156*19c77476Spwernau 	char addr[sizeof (in6_addr_t)];
157*19c77476Spwernau 	char *tmphost_addrs[2];
158*19c77476Spwernau 
159*19c77476Spwernau 	if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
160*19c77476Spwernau 		type = AF_INET6;
161*19c77476Spwernau 	} else if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
162*19c77476Spwernau 		type = AF_INET;
163*19c77476Spwernau 	} else {
1647c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1657c478bd9Sstevel@tonic-gate 		return (FALSE);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* Get the address of the RPCBIND at hostname */
169*19c77476Spwernau 	hent = __files_gethostbyname(hostname, type);
1707c478bd9Sstevel@tonic-gate 	if (hent == (struct hostent *)NULL) {
171*19c77476Spwernau 		/* Make sure this is not an IP address before giving up */
172*19c77476Spwernau 		if (inet_pton(type, hostname, addr) == 1) {
173*19c77476Spwernau 			/* This is a numeric address, fill in the blanks */
174*19c77476Spwernau 			hent = &tmphent;
175*19c77476Spwernau 			memset(&tmphent, 0, sizeof (struct hostent));
176*19c77476Spwernau 			hent->h_addrtype = type;
177*19c77476Spwernau 			hent->h_length = (type == AF_INET6) ?
178*19c77476Spwernau 			    sizeof (in6_addr_t) : sizeof (in_addr_t);
179*19c77476Spwernau 			hent->h_addr_list = tmphost_addrs;
180*19c77476Spwernau 			tmphost_addrs[0] = addr;
181*19c77476Spwernau 			tmphost_addrs[1] = NULL;
182*19c77476Spwernau 		} else {
183*19c77476Spwernau 			rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
184*19c77476Spwernau 			hostNotKnownLocally = 1;
185*19c77476Spwernau 			return (FALSE);
186*19c77476Spwernau 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	switch (hent->h_addrtype) {
1907c478bd9Sstevel@tonic-gate 	case AF_INET:
1917c478bd9Sstevel@tonic-gate 		local_sa.sin_family = AF_INET;
1927c478bd9Sstevel@tonic-gate 		local_sa.sin_port = htons(111); /* RPCBIND port */
1937c478bd9Sstevel@tonic-gate 		memcpy((char *)&(local_sa.sin_addr.s_addr),
194*19c77476Spwernau 		    hent->h_addr_list[0], hent->h_length);
1957c478bd9Sstevel@tonic-gate 		rpcb_taddr.buf = (char *)&local_sa;
1967c478bd9Sstevel@tonic-gate 		rpcb_taddr.maxlen = sizeof (local_sa);
1977c478bd9Sstevel@tonic-gate 		rpcb_taddr.len = rpcb_taddr.maxlen;
1987c478bd9Sstevel@tonic-gate 		break;
1997c478bd9Sstevel@tonic-gate 	case AF_INET6:
2007c478bd9Sstevel@tonic-gate 		local_sa6.sin6_family = AF_INET6;
2017c478bd9Sstevel@tonic-gate 		local_sa6.sin6_port = htons(111); /* RPCBIND port */
2027c478bd9Sstevel@tonic-gate 		memcpy((char *)&(local_sa6.sin6_addr.s6_addr),
203*19c77476Spwernau 		    hent->h_addr_list[0], hent->h_length);
2047c478bd9Sstevel@tonic-gate 		rpcb_taddr.buf = (char *)&local_sa6;
2057c478bd9Sstevel@tonic-gate 		rpcb_taddr.maxlen = sizeof (local_sa6);
2067c478bd9Sstevel@tonic-gate 		rpcb_taddr.len = rpcb_taddr.maxlen;
2077c478bd9Sstevel@tonic-gate 		break;
2087c478bd9Sstevel@tonic-gate 	default:
2097c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2107c478bd9Sstevel@tonic-gate 		return (FALSE);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	svc_uaddr = __map_addr(nconf, &rpcb_taddr, program, version);
2147c478bd9Sstevel@tonic-gate 	if (! svc_uaddr)
2157c478bd9Sstevel@tonic-gate 		return (FALSE);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /* do a local uaddr2taddr and stuff in the memory supplied by the caller */
2187c478bd9Sstevel@tonic-gate 	ipaddr = svc_uaddr;
2197c478bd9Sstevel@tonic-gate 	ipaddrlen = strlen(ipaddr);
2207c478bd9Sstevel@tonic-gate 	/* Look for the first '.' starting from the end */
2217c478bd9Sstevel@tonic-gate 	for (i = ipaddrlen-1; i >= 0; i--)
222*19c77476Spwernau 		if (ipaddr[i] == '.')
223*19c77476Spwernau 			break;
2247c478bd9Sstevel@tonic-gate 	/* Find the second dot (still counting from the end) */
2257c478bd9Sstevel@tonic-gate 	for (i--; i >= 0; i--)
226*19c77476Spwernau 		if (ipaddr[i] == '.')
227*19c77476Spwernau 			break;
2287c478bd9Sstevel@tonic-gate 	/* If we didn't find it, the uaddr has a syntax error */
2297c478bd9Sstevel@tonic-gate 	if (i < 0) {
2307c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2317c478bd9Sstevel@tonic-gate 		return (FALSE);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	port = &ipaddr[i+1];
2347c478bd9Sstevel@tonic-gate 	ipaddr[i] = '\0';
2357c478bd9Sstevel@tonic-gate 	sscanf(port, "%d.%d", &p1, &p2);
2367c478bd9Sstevel@tonic-gate 	inport = (p1 << 8) + p2;
2377c478bd9Sstevel@tonic-gate 	if (hent->h_addrtype == AF_INET) {
2387c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr_in *)address->buf;
2397c478bd9Sstevel@tonic-gate 		address->len = sizeof (*sa);
2407c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET, ipaddr, &sa->sin_addr) != 1) {
2417c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2427c478bd9Sstevel@tonic-gate 			return (FALSE);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		sa->sin_port = htons(inport);
2457c478bd9Sstevel@tonic-gate 		sa->sin_family = AF_INET;
2467c478bd9Sstevel@tonic-gate 	} else {
2477c478bd9Sstevel@tonic-gate 		sa6 = (struct sockaddr_in6 *)address->buf;
2487c478bd9Sstevel@tonic-gate 		address->len = sizeof (*sa6);
2497c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET6, ipaddr, &sa6->sin6_addr) != 1) {
2507c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2517c478bd9Sstevel@tonic-gate 			return (FALSE);
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		sa6->sin6_port = htons(inport);
2547c478bd9Sstevel@tonic-gate 		sa6->sin6_family = AF_INET6;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	return (TRUE);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * __map_addr()
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate static char *
__map_addr(nc,rpcb_taddr,prog,ver)2647c478bd9Sstevel@tonic-gate __map_addr(nc, rpcb_taddr, prog, ver)
2657c478bd9Sstevel@tonic-gate 	struct netconfig	*nc;		/* Our transport	*/
266*19c77476Spwernau 	struct netbuf		*rpcb_taddr;	/* RPCBIND address */
267*19c77476Spwernau 	ulong_t			prog, ver;	/* Name service Prog/vers */
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	register CLIENT *client;
2707c478bd9Sstevel@tonic-gate 	RPCB 		parms;		/* Parameters for RPC binder	  */
2717c478bd9Sstevel@tonic-gate 	enum clnt_stat	clnt_st;	/* Result from the rpc call	  */
2727c478bd9Sstevel@tonic-gate 	int		fd;		/* Stream file descriptor	  */
2737c478bd9Sstevel@tonic-gate 	char 		*ua = NULL;	/* Universal address of service	  */
2747c478bd9Sstevel@tonic-gate 	struct timeval	tv;		/* Timeout for our rpcb call	  */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * First we open a connection to the remote rpcbind process.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nc->nc_device, O_RDWR, NULL)) == -1) {
2807c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
2817c478bd9Sstevel@tonic-gate 		return (NULL);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	client = __nis_clnt_create(fd, nc, 0, rpcb_taddr, 0,
285*19c77476Spwernau 	    RPCBPROG, RPCBVERS, 0, 0);
286*19c77476Spwernau 	if (!client) {
2877c478bd9Sstevel@tonic-gate 		t_close(fd);
2887c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
2897c478bd9Sstevel@tonic-gate 		return (NULL);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Now make the call to get the NIS service address.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	tv.tv_sec = 10;
2967c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
2977c478bd9Sstevel@tonic-gate 	parms.r_prog = prog;
2987c478bd9Sstevel@tonic-gate 	parms.r_vers = ver;
2997c478bd9Sstevel@tonic-gate 	parms.r_netid = nc->nc_netid;	/* not needed */
3007c478bd9Sstevel@tonic-gate 	parms.r_addr = "";	/* not needed; just for xdring */
3017c478bd9Sstevel@tonic-gate 	parms.r_owner = "";	/* not needed; just for xdring */
3027c478bd9Sstevel@tonic-gate 	clnt_st = clnt_call(client, RPCBPROC_GETADDR, xdr_rpcb, (char *)&parms,
303*19c77476Spwernau 	    xdr_wrapstring, (char *)&ua, tv);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = clnt_st;
3067c478bd9Sstevel@tonic-gate 	if (clnt_st == RPC_SUCCESS) {
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
3097c478bd9Sstevel@tonic-gate 		t_close(fd);
3107c478bd9Sstevel@tonic-gate 		if (*ua == '\0') {
3117c478bd9Sstevel@tonic-gate 			xdr_free(xdr_wrapstring, (char *)&ua);
3127c478bd9Sstevel@tonic-gate 			return (NULL);
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		return (ua);
3157c478bd9Sstevel@tonic-gate 	} else if (((clnt_st == RPC_PROGVERSMISMATCH) ||
316*19c77476Spwernau 	    (clnt_st == RPC_PROGUNAVAIL) ||
317*19c77476Spwernau 	    (clnt_st == RPC_TIMEDOUT)) &&
318*19c77476Spwernau 	    (strcmp(nc->nc_protofmly, NC_INET) == 0)) {
3197c478bd9Sstevel@tonic-gate 		/*
3207c478bd9Sstevel@tonic-gate 		 * version 3 not available. Try version 2
3217c478bd9Sstevel@tonic-gate 		 * The assumption here is that the netbuf
3227c478bd9Sstevel@tonic-gate 		 * is arranged in the sockaddr_in
3237c478bd9Sstevel@tonic-gate 		 * style for IP cases.
3247c478bd9Sstevel@tonic-gate 		 */
325*19c77476Spwernau 		ushort_t	port;
3267c478bd9Sstevel@tonic-gate 		struct sockaddr_in	*sa;
3277c478bd9Sstevel@tonic-gate 		struct netbuf 		remote;
3287c478bd9Sstevel@tonic-gate 		int		protocol;
3297c478bd9Sstevel@tonic-gate 		char	buf[32];
3307c478bd9Sstevel@tonic-gate 		char	*res;
3317c478bd9Sstevel@tonic-gate 
332*19c77476Spwernau 		clnt_control(client, CLGET_SVC_ADDR, (char *)&remote);
3337c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr_in *)(remote.buf);
334*19c77476Spwernau 		protocol = strcmp(nc->nc_proto, NC_TCP) ? IPPROTO_UDP :
335*19c77476Spwernau 		    IPPROTO_TCP;
336*19c77476Spwernau 		port = (ushort_t)pmap_getport(sa, prog, ver, protocol);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (port != 0) {
3397c478bd9Sstevel@tonic-gate 			/* print s_addr (and port) in host byte order */
3407c478bd9Sstevel@tonic-gate 			sa->sin_addr.s_addr = ntohl(sa->sin_addr.s_addr);
3417c478bd9Sstevel@tonic-gate 			sprintf(buf, "%d.%d.%d.%d.%d.%d",
342*19c77476Spwernau 			    (sa->sin_addr.s_addr >> 24) & 0xff,
343*19c77476Spwernau 			    (sa->sin_addr.s_addr >> 16) & 0xff,
344*19c77476Spwernau 			    (sa->sin_addr.s_addr >>  8) & 0xff,
345*19c77476Spwernau 			    (sa->sin_addr.s_addr) & 0xff,
346*19c77476Spwernau 			    (port >> 8) & 0xff,
347*19c77476Spwernau 			    port & 0xff);
3487c478bd9Sstevel@tonic-gate 			res = strdup(buf);
3497c478bd9Sstevel@tonic-gate 			if (res != 0) {
3507c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_SUCCESS;
3517c478bd9Sstevel@tonic-gate 			} else {
3527c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 		} else {
3557c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
3567c478bd9Sstevel@tonic-gate 			res = NULL;
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
3597c478bd9Sstevel@tonic-gate 		t_close(fd);
3607c478bd9Sstevel@tonic-gate 		return (res);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
3637c478bd9Sstevel@tonic-gate 	t_close(fd);
3647c478bd9Sstevel@tonic-gate 	return (NULL);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate #define	bcmp(s1, s2, len)	memcmp(s1, s2, len)
3687c478bd9Sstevel@tonic-gate #define	bcopy(s1, s2, len)	memcpy(s2, s1, len)
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #define	MAXALIASES	35
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate static char line[BUFSIZ+1];
3737c478bd9Sstevel@tonic-gate 
374*19c77476Spwernau static char *_hosts4_6[] = { "/etc/inet/hosts", "/etc/inet/ipnodes", 0 };
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static char *any();
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static struct hostent *__files_gethostent();
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate struct hostent *
__files_gethostbyname(char * nam,sa_family_t af)381*19c77476Spwernau __files_gethostbyname(char *nam, sa_family_t af)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	register struct hostent *hp;
3847c478bd9Sstevel@tonic-gate 	register char **cp;
385*19c77476Spwernau 	char **file = _hosts4_6;
3867c478bd9Sstevel@tonic-gate 	FILE *hostf;
3877c478bd9Sstevel@tonic-gate 
388*19c77476Spwernau 	if ((af != AF_INET) && (af != AF_INET6))
3897c478bd9Sstevel@tonic-gate 		return (0);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	for (; *file != 0; file++) {
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		if ((hostf = fopen(*file, "r")) == 0)
3947c478bd9Sstevel@tonic-gate 			continue;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		while (hp = __files_gethostent(hostf)) {
3977c478bd9Sstevel@tonic-gate 			if (hp->h_addrtype != af)
3987c478bd9Sstevel@tonic-gate 				continue;
3997c478bd9Sstevel@tonic-gate 			if (strcasecmp(hp->h_name, nam) == 0) {
4007c478bd9Sstevel@tonic-gate 				(void) fclose(hostf);
4017c478bd9Sstevel@tonic-gate 				return (hp);
4027c478bd9Sstevel@tonic-gate 			}
4037c478bd9Sstevel@tonic-gate 			for (cp = hp->h_aliases; cp != 0 && *cp != 0; cp++)
4047c478bd9Sstevel@tonic-gate 				if (strcasecmp(*cp, nam) == 0) {
4057c478bd9Sstevel@tonic-gate 					(void) fclose(hostf);
4067c478bd9Sstevel@tonic-gate 					return (hp);
4077c478bd9Sstevel@tonic-gate 				}
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		(void) fclose(hostf);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	return (0);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate #define	isV6Addr(s)	(strchr(s, (int)':') != 0)
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate static struct hostent *
__files_gethostent(FILE * hostf)4197c478bd9Sstevel@tonic-gate __files_gethostent(FILE *hostf)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	char *p;
4227c478bd9Sstevel@tonic-gate 	register char *cp, **q;
4237c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
4247c478bd9Sstevel@tonic-gate 	struct in_addr in4;
4257c478bd9Sstevel@tonic-gate 	void *addr;
4267c478bd9Sstevel@tonic-gate 	sa_family_t af;
4277c478bd9Sstevel@tonic-gate 	int len;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (hostf == NULL)
4307c478bd9Sstevel@tonic-gate 		return (NULL);
4317c478bd9Sstevel@tonic-gate again:
4327c478bd9Sstevel@tonic-gate 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
4337c478bd9Sstevel@tonic-gate 		return (NULL);
4347c478bd9Sstevel@tonic-gate 	if (*p == '#')
4357c478bd9Sstevel@tonic-gate 		goto again;
4367c478bd9Sstevel@tonic-gate 	cp = any(p, "#\n");
4377c478bd9Sstevel@tonic-gate 	if (cp == NULL)
4387c478bd9Sstevel@tonic-gate 		goto again;
4397c478bd9Sstevel@tonic-gate 	*cp = '\0';
4407c478bd9Sstevel@tonic-gate 	cp = any(p, " \t");
4417c478bd9Sstevel@tonic-gate 	if (cp == NULL)
4427c478bd9Sstevel@tonic-gate 		goto again;
4437c478bd9Sstevel@tonic-gate 	*cp++ = '\0';
4447c478bd9Sstevel@tonic-gate 	/* THIS STUFF IS INTERNET SPECIFIC */
4457c478bd9Sstevel@tonic-gate 	host.h_addr_list = host_addrs;
4467c478bd9Sstevel@tonic-gate 	if (isV6Addr(p)) {
4477c478bd9Sstevel@tonic-gate 		af = AF_INET6;
4487c478bd9Sstevel@tonic-gate 		addr = (void *)&in6;
4497c478bd9Sstevel@tonic-gate 		len = sizeof (in6);
4507c478bd9Sstevel@tonic-gate 	} else {
4517c478bd9Sstevel@tonic-gate 		af = AF_INET;
4527c478bd9Sstevel@tonic-gate 		addr = (void *)&in4;
4537c478bd9Sstevel@tonic-gate 		len = sizeof (in4);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	if (inet_pton(af, p, addr) != 1)
4567c478bd9Sstevel@tonic-gate 		goto again;
4577c478bd9Sstevel@tonic-gate 	bcopy(addr, host.h_addr_list[0], len);
4587c478bd9Sstevel@tonic-gate 	host.h_length = len;
4597c478bd9Sstevel@tonic-gate 	host.h_addrtype = af;
4607c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
4617c478bd9Sstevel@tonic-gate 		cp++;
4627c478bd9Sstevel@tonic-gate 	host.h_name = cp;
4637c478bd9Sstevel@tonic-gate 	q = host.h_aliases = host_aliases;
4647c478bd9Sstevel@tonic-gate 	cp = any(cp, " \t");
4657c478bd9Sstevel@tonic-gate 	if (cp != NULL)
4667c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
4677c478bd9Sstevel@tonic-gate 	while (cp && *cp) {
4687c478bd9Sstevel@tonic-gate 		if (*cp == ' ' || *cp == '\t') {
4697c478bd9Sstevel@tonic-gate 			cp++;
4707c478bd9Sstevel@tonic-gate 			continue;
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 		if (q < &host_aliases[MAXALIASES - 1])
4737c478bd9Sstevel@tonic-gate 			*q++ = cp;
4747c478bd9Sstevel@tonic-gate 		cp = any(cp, " \t");
4757c478bd9Sstevel@tonic-gate 		if (cp != NULL)
4767c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	*q = NULL;
4797c478bd9Sstevel@tonic-gate 	return (&host);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static char *
any(cp,match)4837c478bd9Sstevel@tonic-gate any(cp, match)
4847c478bd9Sstevel@tonic-gate 	register char *cp;
4857c478bd9Sstevel@tonic-gate 	char *match;
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	register char *mp, c;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	while (c = *cp) {
4907c478bd9Sstevel@tonic-gate 		for (mp = match; *mp; mp++)
4917c478bd9Sstevel@tonic-gate 			if (*mp == c)
4927c478bd9Sstevel@tonic-gate 				return (cp);
4937c478bd9Sstevel@tonic-gate 		cp++;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	return ((char *)0);
4967c478bd9Sstevel@tonic-gate }
497