xref: /illumos-gate/usr/src/cmd/ypcmd/rpc_bootstrap.c (revision 2a8bcb4e)
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 #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/file.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <tiuser.h>
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
427c478bd9Sstevel@tonic-gate #include <sys/socket.h>
437c478bd9Sstevel@tonic-gate #include <netdir.h>
447c478bd9Sstevel@tonic-gate #include <netdb.h>
457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
467c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
477c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate CLIENT *__clnt_tp_create_bootstrap();
507c478bd9Sstevel@tonic-gate int __rpcb_getaddr_bootstrap();
51*19c77476Spwernau struct hostent *__files_gethostbyname(char *, sa_family_t);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern int hostNotKnownLocally;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static char *__map_addr();
56*19c77476Spwernau static struct hostent host;
57*19c77476Spwernau static char hostaddr[sizeof (struct in6_addr)];
58*19c77476Spwernau static char *host_aliases[MAXALIASES];
59*19c77476Spwernau static char *host_addrs[] = {
60*19c77476Spwernau 	hostaddr,
61*19c77476Spwernau 	NULL
62*19c77476Spwernau };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * __clnt_tp_create_bootstrap()
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * This routine is NOT TRANSPORT INDEPENDENT.
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * It relies on the local /etc/hosts file for hostname to address
707c478bd9Sstevel@tonic-gate  * translation and does it itself instead of calling netdir_getbyname
71*19c77476Spwernau  * thereby avoids recursion.  Secondarily, it will use a validated
72*19c77476Spwernau  * IP address directly.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate CLIENT *
__clnt_tp_create_bootstrap(hostname,prog,vers,nconf)757c478bd9Sstevel@tonic-gate __clnt_tp_create_bootstrap(hostname, prog, vers, nconf)
767c478bd9Sstevel@tonic-gate 	char *hostname;
77*19c77476Spwernau 	ulong_t prog, vers;
787c478bd9Sstevel@tonic-gate 	struct netconfig    *nconf;
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	CLIENT *cl;
817c478bd9Sstevel@tonic-gate 	struct netbuf	*svc_taddr;
827c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sa;
837c478bd9Sstevel@tonic-gate 	int fd;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (nconf == (struct netconfig *)NULL) {
867c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
877c478bd9Sstevel@tonic-gate 		return (NULL);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
907c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
917c478bd9Sstevel@tonic-gate 		return (NULL);
927c478bd9Sstevel@tonic-gate 	}
93*19c77476Spwernau 	svc_taddr = (struct netbuf *)malloc(sizeof (struct netbuf));
947c478bd9Sstevel@tonic-gate 	if (! svc_taddr) {
957c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
967c478bd9Sstevel@tonic-gate 		t_close(fd);
977c478bd9Sstevel@tonic-gate 		return (NULL);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	sa = (struct sockaddr_in6 *)calloc(1, sizeof (*sa));
1007c478bd9Sstevel@tonic-gate 	if (! sa) {
1017c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1027c478bd9Sstevel@tonic-gate 		t_close(fd);
1037c478bd9Sstevel@tonic-gate 		free(svc_taddr);
1047c478bd9Sstevel@tonic-gate 		return (NULL);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	svc_taddr->maxlen = svc_taddr->len = sizeof (*sa);
1077c478bd9Sstevel@tonic-gate 	svc_taddr->buf = (char *)sa;
1087c478bd9Sstevel@tonic-gate 	if (__rpcb_getaddr_bootstrap(prog,
1097c478bd9Sstevel@tonic-gate 		vers, nconf, svc_taddr, hostname) == FALSE) {
1107c478bd9Sstevel@tonic-gate 		t_close(fd);
1117c478bd9Sstevel@tonic-gate 		free(svc_taddr);
1127c478bd9Sstevel@tonic-gate 		free(sa);
1137c478bd9Sstevel@tonic-gate 		return (NULL);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = RPC_SUCCESS;
1167c478bd9Sstevel@tonic-gate 	cl = __nis_clnt_create(fd, nconf, 0, svc_taddr, 0, prog, vers, 0, 0);
1177c478bd9Sstevel@tonic-gate 	if (cl == 0) {
1187c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_SUCCESS)
1197c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_TLIERROR;
1207c478bd9Sstevel@tonic-gate 		t_close(fd);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	free(svc_taddr);
1237c478bd9Sstevel@tonic-gate 	free(sa);
1247c478bd9Sstevel@tonic-gate 	return (cl);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * __rpcb_getaddr_bootstrap()
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * This is our internal function that replaces rpcb_getaddr(). We
1317c478bd9Sstevel@tonic-gate  * build our own to prevent calling netdir_getbyname() which could
1327c478bd9Sstevel@tonic-gate  * recurse to the nameservice.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate int
__rpcb_getaddr_bootstrap(program,version,nconf,address,hostname)1357c478bd9Sstevel@tonic-gate __rpcb_getaddr_bootstrap(program, version, nconf, address, hostname)
136*19c77476Spwernau 	ulong_t program;
137*19c77476Spwernau 	ulong_t version;
1387c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1397c478bd9Sstevel@tonic-gate 	struct netbuf *address; /* populate with the taddr of the service */
1407c478bd9Sstevel@tonic-gate 	char *hostname;
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	char *svc_uaddr;
143*19c77476Spwernau 	struct hostent *hent, tmphent;
1447c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sa;
1457c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sa6;
1467c478bd9Sstevel@tonic-gate 	struct netbuf rpcb_taddr;
1477c478bd9Sstevel@tonic-gate 	struct sockaddr_in local_sa;
1487c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 local_sa6;
1497c478bd9Sstevel@tonic-gate 	in_port_t inport;
1507c478bd9Sstevel@tonic-gate 	int p1, p2;
1517c478bd9Sstevel@tonic-gate 	char *ipaddr, *port;
1527c478bd9Sstevel@tonic-gate 	int i, ipaddrlen;
153*19c77476Spwernau 	sa_family_t type;
154*19c77476Spwernau 	char addr[sizeof (in6_addr_t)];
155*19c77476Spwernau 	char *tmphost_addrs[2];
156*19c77476Spwernau 
157*19c77476Spwernau 	if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
158*19c77476Spwernau 		type = AF_INET6;
159*19c77476Spwernau 	} else if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
160*19c77476Spwernau 		type = AF_INET;
161*19c77476Spwernau 	} else {
1627c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1637c478bd9Sstevel@tonic-gate 		return (FALSE);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* Get the address of the RPCBIND at hostname */
167*19c77476Spwernau 	hent = __files_gethostbyname(hostname, type);
1687c478bd9Sstevel@tonic-gate 	if (hent == (struct hostent *)NULL) {
169*19c77476Spwernau 		/* Make sure this is not an IP address before giving up */
170*19c77476Spwernau 		if (inet_pton(type, hostname, addr) == 1) {
171*19c77476Spwernau 			/* This is a numeric address, fill in the blanks */
172*19c77476Spwernau 			hent = &tmphent;
173*19c77476Spwernau 			memset(&tmphent, 0, sizeof (struct hostent));
174*19c77476Spwernau 			hent->h_addrtype = type;
175*19c77476Spwernau 			hent->h_length = (type == AF_INET6) ?
176*19c77476Spwernau 			    sizeof (in6_addr_t) : sizeof (in_addr_t);
177*19c77476Spwernau 			hent->h_addr_list = tmphost_addrs;
178*19c77476Spwernau 			tmphost_addrs[0] = addr;
179*19c77476Spwernau 			tmphost_addrs[1] = NULL;
180*19c77476Spwernau 		} else {
181*19c77476Spwernau 			rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
182*19c77476Spwernau 			hostNotKnownLocally = 1;
183*19c77476Spwernau 			return (FALSE);
184*19c77476Spwernau 		}
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	switch (hent->h_addrtype) {
1887c478bd9Sstevel@tonic-gate 	case AF_INET:
1897c478bd9Sstevel@tonic-gate 		local_sa.sin_family = AF_INET;
1907c478bd9Sstevel@tonic-gate 		local_sa.sin_port = htons(111); /* RPCBIND port */
1917c478bd9Sstevel@tonic-gate 		memcpy((char *)&(local_sa.sin_addr.s_addr),
192*19c77476Spwernau 		    hent->h_addr_list[0], hent->h_length);
1937c478bd9Sstevel@tonic-gate 		rpcb_taddr.buf = (char *)&local_sa;
1947c478bd9Sstevel@tonic-gate 		rpcb_taddr.maxlen = sizeof (local_sa);
1957c478bd9Sstevel@tonic-gate 		rpcb_taddr.len = rpcb_taddr.maxlen;
1967c478bd9Sstevel@tonic-gate 		break;
1977c478bd9Sstevel@tonic-gate 	case AF_INET6:
1987c478bd9Sstevel@tonic-gate 		local_sa6.sin6_family = AF_INET6;
1997c478bd9Sstevel@tonic-gate 		local_sa6.sin6_port = htons(111); /* RPCBIND port */
2007c478bd9Sstevel@tonic-gate 		memcpy((char *)&(local_sa6.sin6_addr.s6_addr),
201*19c77476Spwernau 		    hent->h_addr_list[0], hent->h_length);
2027c478bd9Sstevel@tonic-gate 		rpcb_taddr.buf = (char *)&local_sa6;
2037c478bd9Sstevel@tonic-gate 		rpcb_taddr.maxlen = sizeof (local_sa6);
2047c478bd9Sstevel@tonic-gate 		rpcb_taddr.len = rpcb_taddr.maxlen;
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	default:
2077c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2087c478bd9Sstevel@tonic-gate 		return (FALSE);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	svc_uaddr = __map_addr(nconf, &rpcb_taddr, program, version);
2127c478bd9Sstevel@tonic-gate 	if (! svc_uaddr)
2137c478bd9Sstevel@tonic-gate 		return (FALSE);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /* do a local uaddr2taddr and stuff in the memory supplied by the caller */
2167c478bd9Sstevel@tonic-gate 	ipaddr = svc_uaddr;
2177c478bd9Sstevel@tonic-gate 	ipaddrlen = strlen(ipaddr);
2187c478bd9Sstevel@tonic-gate 	/* Look for the first '.' starting from the end */
2197c478bd9Sstevel@tonic-gate 	for (i = ipaddrlen-1; i >= 0; i--)
220*19c77476Spwernau 		if (ipaddr[i] == '.')
221*19c77476Spwernau 			break;
2227c478bd9Sstevel@tonic-gate 	/* Find the second dot (still counting from the end) */
2237c478bd9Sstevel@tonic-gate 	for (i--; i >= 0; i--)
224*19c77476Spwernau 		if (ipaddr[i] == '.')
225*19c77476Spwernau 			break;
2267c478bd9Sstevel@tonic-gate 	/* If we didn't find it, the uaddr has a syntax error */
2277c478bd9Sstevel@tonic-gate 	if (i < 0) {
2287c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2297c478bd9Sstevel@tonic-gate 		return (FALSE);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	port = &ipaddr[i+1];
2327c478bd9Sstevel@tonic-gate 	ipaddr[i] = '\0';
2337c478bd9Sstevel@tonic-gate 	sscanf(port, "%d.%d", &p1, &p2);
2347c478bd9Sstevel@tonic-gate 	inport = (p1 << 8) + p2;
2357c478bd9Sstevel@tonic-gate 	if (hent->h_addrtype == AF_INET) {
2367c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr_in *)address->buf;
2377c478bd9Sstevel@tonic-gate 		address->len = sizeof (*sa);
2387c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET, ipaddr, &sa->sin_addr) != 1) {
2397c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2407c478bd9Sstevel@tonic-gate 			return (FALSE);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		sa->sin_port = htons(inport);
2437c478bd9Sstevel@tonic-gate 		sa->sin_family = AF_INET;
2447c478bd9Sstevel@tonic-gate 	} else {
2457c478bd9Sstevel@tonic-gate 		sa6 = (struct sockaddr_in6 *)address->buf;
2467c478bd9Sstevel@tonic-gate 		address->len = sizeof (*sa6);
2477c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET6, ipaddr, &sa6->sin6_addr) != 1) {
2487c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
2497c478bd9Sstevel@tonic-gate 			return (FALSE);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		sa6->sin6_port = htons(inport);
2527c478bd9Sstevel@tonic-gate 		sa6->sin6_family = AF_INET6;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	return (TRUE);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * __map_addr()
2597c478bd9Sstevel@tonic-gate  *
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate static char *
__map_addr(nc,rpcb_taddr,prog,ver)2627c478bd9Sstevel@tonic-gate __map_addr(nc, rpcb_taddr, prog, ver)
2637c478bd9Sstevel@tonic-gate 	struct netconfig	*nc;		/* Our transport	*/
264*19c77476Spwernau 	struct netbuf		*rpcb_taddr;	/* RPCBIND address */
265*19c77476Spwernau 	ulong_t			prog, ver;	/* Name service Prog/vers */
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	register CLIENT *client;
2687c478bd9Sstevel@tonic-gate 	RPCB 		parms;		/* Parameters for RPC binder	  */
2697c478bd9Sstevel@tonic-gate 	enum clnt_stat	clnt_st;	/* Result from the rpc call	  */
2707c478bd9Sstevel@tonic-gate 	int		fd;		/* Stream file descriptor	  */
2717c478bd9Sstevel@tonic-gate 	char 		*ua = NULL;	/* Universal address of service	  */
2727c478bd9Sstevel@tonic-gate 	struct timeval	tv;		/* Timeout for our rpcb call	  */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * First we open a connection to the remote rpcbind process.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	if ((fd = t_open(nc->nc_device, O_RDWR, NULL)) == -1) {
2787c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
2797c478bd9Sstevel@tonic-gate 		return (NULL);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	client = __nis_clnt_create(fd, nc, 0, rpcb_taddr, 0,
283*19c77476Spwernau 	    RPCBPROG, RPCBVERS, 0, 0);
284*19c77476Spwernau 	if (!client) {
2857c478bd9Sstevel@tonic-gate 		t_close(fd);
2867c478bd9Sstevel@tonic-gate 		rpc_createerr.cf_stat = RPC_TLIERROR;
2877c478bd9Sstevel@tonic-gate 		return (NULL);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Now make the call to get the NIS service address.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	tv.tv_sec = 10;
2947c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
2957c478bd9Sstevel@tonic-gate 	parms.r_prog = prog;
2967c478bd9Sstevel@tonic-gate 	parms.r_vers = ver;
2977c478bd9Sstevel@tonic-gate 	parms.r_netid = nc->nc_netid;	/* not needed */
2987c478bd9Sstevel@tonic-gate 	parms.r_addr = "";	/* not needed; just for xdring */
2997c478bd9Sstevel@tonic-gate 	parms.r_owner = "";	/* not needed; just for xdring */
3007c478bd9Sstevel@tonic-gate 	clnt_st = clnt_call(client, RPCBPROC_GETADDR, xdr_rpcb, (char *)&parms,
301*19c77476Spwernau 	    xdr_wrapstring, (char *)&ua, tv);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	rpc_createerr.cf_stat = clnt_st;
3047c478bd9Sstevel@tonic-gate 	if (clnt_st == RPC_SUCCESS) {
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
3077c478bd9Sstevel@tonic-gate 		t_close(fd);
3087c478bd9Sstevel@tonic-gate 		if (*ua == '\0') {
3097c478bd9Sstevel@tonic-gate 			xdr_free(xdr_wrapstring, (char *)&ua);
3107c478bd9Sstevel@tonic-gate 			return (NULL);
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		return (ua);
3137c478bd9Sstevel@tonic-gate 	} else if (((clnt_st == RPC_PROGVERSMISMATCH) ||
314*19c77476Spwernau 	    (clnt_st == RPC_PROGUNAVAIL) ||
315*19c77476Spwernau 	    (clnt_st == RPC_TIMEDOUT)) &&
316*19c77476Spwernau 	    (strcmp(nc->nc_protofmly, NC_INET) == 0)) {
3177c478bd9Sstevel@tonic-gate 		/*
3187c478bd9Sstevel@tonic-gate 		 * version 3 not available. Try version 2
3197c478bd9Sstevel@tonic-gate 		 * The assumption here is that the netbuf
3207c478bd9Sstevel@tonic-gate 		 * is arranged in the sockaddr_in
3217c478bd9Sstevel@tonic-gate 		 * style for IP cases.
3227c478bd9Sstevel@tonic-gate 		 */
323*19c77476Spwernau 		ushort_t	port;
3247c478bd9Sstevel@tonic-gate 		struct sockaddr_in	*sa;
3257c478bd9Sstevel@tonic-gate 		struct netbuf 		remote;
3267c478bd9Sstevel@tonic-gate 		int		protocol;
3277c478bd9Sstevel@tonic-gate 		char	buf[32];
3287c478bd9Sstevel@tonic-gate 		char	*res;
3297c478bd9Sstevel@tonic-gate 
330*19c77476Spwernau 		clnt_control(client, CLGET_SVC_ADDR, (char *)&remote);
3317c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr_in *)(remote.buf);
332*19c77476Spwernau 		protocol = strcmp(nc->nc_proto, NC_TCP) ? IPPROTO_UDP :
333*19c77476Spwernau 		    IPPROTO_TCP;
334*19c77476Spwernau 		port = (ushort_t)pmap_getport(sa, prog, ver, protocol);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		if (port != 0) {
3377c478bd9Sstevel@tonic-gate 			/* print s_addr (and port) in host byte order */
3387c478bd9Sstevel@tonic-gate 			sa->sin_addr.s_addr = ntohl(sa->sin_addr.s_addr);
3397c478bd9Sstevel@tonic-gate 			sprintf(buf, "%d.%d.%d.%d.%d.%d",
340*19c77476Spwernau 			    (sa->sin_addr.s_addr >> 24) & 0xff,
341*19c77476Spwernau 			    (sa->sin_addr.s_addr >> 16) & 0xff,
342*19c77476Spwernau 			    (sa->sin_addr.s_addr >>  8) & 0xff,
343*19c77476Spwernau 			    (sa->sin_addr.s_addr) & 0xff,
344*19c77476Spwernau 			    (port >> 8) & 0xff,
345*19c77476Spwernau 			    port & 0xff);
3467c478bd9Sstevel@tonic-gate 			res = strdup(buf);
3477c478bd9Sstevel@tonic-gate 			if (res != 0) {
3487c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_SUCCESS;
3497c478bd9Sstevel@tonic-gate 			} else {
3507c478bd9Sstevel@tonic-gate 				rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3517c478bd9Sstevel@tonic-gate 			}
3527c478bd9Sstevel@tonic-gate 		} else {
3537c478bd9Sstevel@tonic-gate 			rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
3547c478bd9Sstevel@tonic-gate 			res = NULL;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		clnt_destroy(client);
3577c478bd9Sstevel@tonic-gate 		t_close(fd);
3587c478bd9Sstevel@tonic-gate 		return (res);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	clnt_destroy(client);
3617c478bd9Sstevel@tonic-gate 	t_close(fd);
3627c478bd9Sstevel@tonic-gate 	return (NULL);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate #define	bcmp(s1, s2, len)	memcmp(s1, s2, len)
3667c478bd9Sstevel@tonic-gate #define	bcopy(s1, s2, len)	memcpy(s2, s1, len)
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #define	MAXALIASES	35
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static char line[BUFSIZ+1];
3717c478bd9Sstevel@tonic-gate 
372*19c77476Spwernau static char *_hosts4_6[] = { "/etc/inet/hosts", "/etc/inet/ipnodes", 0 };
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static char *any();
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static struct hostent *__files_gethostent();
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate struct hostent *
__files_gethostbyname(char * nam,sa_family_t af)379*19c77476Spwernau __files_gethostbyname(char *nam, sa_family_t af)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	register struct hostent *hp;
3827c478bd9Sstevel@tonic-gate 	register char **cp;
383*19c77476Spwernau 	char **file = _hosts4_6;
3847c478bd9Sstevel@tonic-gate 	FILE *hostf;
3857c478bd9Sstevel@tonic-gate 
386*19c77476Spwernau 	if ((af != AF_INET) && (af != AF_INET6))
3877c478bd9Sstevel@tonic-gate 		return (0);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	for (; *file != 0; file++) {
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		if ((hostf = fopen(*file, "r")) == 0)
3927c478bd9Sstevel@tonic-gate 			continue;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		while (hp = __files_gethostent(hostf)) {
3957c478bd9Sstevel@tonic-gate 			if (hp->h_addrtype != af)
3967c478bd9Sstevel@tonic-gate 				continue;
3977c478bd9Sstevel@tonic-gate 			if (strcasecmp(hp->h_name, nam) == 0) {
3987c478bd9Sstevel@tonic-gate 				(void) fclose(hostf);
3997c478bd9Sstevel@tonic-gate 				return (hp);
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 			for (cp = hp->h_aliases; cp != 0 && *cp != 0; cp++)
4027c478bd9Sstevel@tonic-gate 				if (strcasecmp(*cp, nam) == 0) {
4037c478bd9Sstevel@tonic-gate 					(void) fclose(hostf);
4047c478bd9Sstevel@tonic-gate 					return (hp);
4057c478bd9Sstevel@tonic-gate 				}
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		(void) fclose(hostf);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return (0);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate #define	isV6Addr(s)	(strchr(s, (int)':') != 0)
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate static struct hostent *
__files_gethostent(FILE * hostf)4177c478bd9Sstevel@tonic-gate __files_gethostent(FILE *hostf)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	char *p;
4207c478bd9Sstevel@tonic-gate 	register char *cp, **q;
4217c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
4227c478bd9Sstevel@tonic-gate 	struct in_addr in4;
4237c478bd9Sstevel@tonic-gate 	void *addr;
4247c478bd9Sstevel@tonic-gate 	sa_family_t af;
4257c478bd9Sstevel@tonic-gate 	int len;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (hostf == NULL)
4287c478bd9Sstevel@tonic-gate 		return (NULL);
4297c478bd9Sstevel@tonic-gate again:
4307c478bd9Sstevel@tonic-gate 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
4317c478bd9Sstevel@tonic-gate 		return (NULL);
4327c478bd9Sstevel@tonic-gate 	if (*p == '#')
4337c478bd9Sstevel@tonic-gate 		goto again;
4347c478bd9Sstevel@tonic-gate 	cp = any(p, "#\n");
4357c478bd9Sstevel@tonic-gate 	if (cp == NULL)
4367c478bd9Sstevel@tonic-gate 		goto again;
4377c478bd9Sstevel@tonic-gate 	*cp = '\0';
4387c478bd9Sstevel@tonic-gate 	cp = any(p, " \t");
4397c478bd9Sstevel@tonic-gate 	if (cp == NULL)
4407c478bd9Sstevel@tonic-gate 		goto again;
4417c478bd9Sstevel@tonic-gate 	*cp++ = '\0';
4427c478bd9Sstevel@tonic-gate 	/* THIS STUFF IS INTERNET SPECIFIC */
4437c478bd9Sstevel@tonic-gate 	host.h_addr_list = host_addrs;
4447c478bd9Sstevel@tonic-gate 	if (isV6Addr(p)) {
4457c478bd9Sstevel@tonic-gate 		af = AF_INET6;
4467c478bd9Sstevel@tonic-gate 		addr = (void *)&in6;
4477c478bd9Sstevel@tonic-gate 		len = sizeof (in6);
4487c478bd9Sstevel@tonic-gate 	} else {
4497c478bd9Sstevel@tonic-gate 		af = AF_INET;
4507c478bd9Sstevel@tonic-gate 		addr = (void *)&in4;
4517c478bd9Sstevel@tonic-gate 		len = sizeof (in4);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	if (inet_pton(af, p, addr) != 1)
4547c478bd9Sstevel@tonic-gate 		goto again;
4557c478bd9Sstevel@tonic-gate 	bcopy(addr, host.h_addr_list[0], len);
4567c478bd9Sstevel@tonic-gate 	host.h_length = len;
4577c478bd9Sstevel@tonic-gate 	host.h_addrtype = af;
4587c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
4597c478bd9Sstevel@tonic-gate 		cp++;
4607c478bd9Sstevel@tonic-gate 	host.h_name = cp;
4617c478bd9Sstevel@tonic-gate 	q = host.h_aliases = host_aliases;
4627c478bd9Sstevel@tonic-gate 	cp = any(cp, " \t");
4637c478bd9Sstevel@tonic-gate 	if (cp != NULL)
4647c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
4657c478bd9Sstevel@tonic-gate 	while (cp && *cp) {
4667c478bd9Sstevel@tonic-gate 		if (*cp == ' ' || *cp == '\t') {
4677c478bd9Sstevel@tonic-gate 			cp++;
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		if (q < &host_aliases[MAXALIASES - 1])
4717c478bd9Sstevel@tonic-gate 			*q++ = cp;
4727c478bd9Sstevel@tonic-gate 		cp = any(cp, " \t");
4737c478bd9Sstevel@tonic-gate 		if (cp != NULL)
4747c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	*q = NULL;
4777c478bd9Sstevel@tonic-gate 	return (&host);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static char *
any(cp,match)4817c478bd9Sstevel@tonic-gate any(cp, match)
4827c478bd9Sstevel@tonic-gate 	register char *cp;
4837c478bd9Sstevel@tonic-gate 	char *match;
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	register char *mp, c;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	while (c = *cp) {
4887c478bd9Sstevel@tonic-gate 		for (mp = match; *mp; mp++)
4897c478bd9Sstevel@tonic-gate 			if (*mp == c)
4907c478bd9Sstevel@tonic-gate 				return (cp);
4917c478bd9Sstevel@tonic-gate 		cp++;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 	return ((char *)0);
4947c478bd9Sstevel@tonic-gate }
495