1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1985, 1988 Regents of the University of California.
3*7c478bd9Sstevel@tonic-gate  * All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
6*7c478bd9Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
7*7c478bd9Sstevel@tonic-gate  * to the University of California at Berkeley. The name of the University
8*7c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
9*7c478bd9Sstevel@tonic-gate  * software without specific prior written permission. This software
10*7c478bd9Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  */
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" /* SunOS 1.11; UCB 6.32 */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
19*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
20*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
21*7c478bd9Sstevel@tonic-gate #include <ctype.h>
22*7c478bd9Sstevel@tonic-gate #include <netdb.h>
23*7c478bd9Sstevel@tonic-gate #include <stdio.h>
24*7c478bd9Sstevel@tonic-gate #include <errno.h>
25*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
26*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
27*7c478bd9Sstevel@tonic-gate #include <resolv.h>
28*7c478bd9Sstevel@tonic-gate #include <syslog.h>
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * When the name service switch calls libresolv, it doesn't want fallback
32*7c478bd9Sstevel@tonic-gate  * to /etc/hosts, so we provide a method to turn it off.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate static int	no_hosts_fallback = 0;
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate void
37*7c478bd9Sstevel@tonic-gate __res_set_no_hosts_fallback(void) {
38*7c478bd9Sstevel@tonic-gate 	no_hosts_fallback = 1;
39*7c478bd9Sstevel@tonic-gate }
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int
42*7c478bd9Sstevel@tonic-gate __res_no_hosts_fallback(void) {
43*7c478bd9Sstevel@tonic-gate 	return(no_hosts_fallback);
44*7c478bd9Sstevel@tonic-gate }
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static char *h_addr_ptrs[MAXADDRS + 1];
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static struct hostent host;
49*7c478bd9Sstevel@tonic-gate static char *host_aliases[MAXALIASES];
50*7c478bd9Sstevel@tonic-gate static char hostbuf[BUFSIZ+1];
51*7c478bd9Sstevel@tonic-gate static struct in_addr host_addr;
52*7c478bd9Sstevel@tonic-gate static char HOSTDB[] = "/etc/hosts";
53*7c478bd9Sstevel@tonic-gate static FILE *hostf = NULL;
54*7c478bd9Sstevel@tonic-gate static char hostaddr[MAXADDRS];
55*7c478bd9Sstevel@tonic-gate static char *host_addrs[2];
56*7c478bd9Sstevel@tonic-gate static int stayopen = 0;
57*7c478bd9Sstevel@tonic-gate static char *any();
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #if PACKETSZ > 1024
60*7c478bd9Sstevel@tonic-gate #define	MAXPACKET	PACKETSZ
61*7c478bd9Sstevel@tonic-gate #else
62*7c478bd9Sstevel@tonic-gate #define	MAXPACKET	1024
63*7c478bd9Sstevel@tonic-gate #endif
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate typedef union {
66*7c478bd9Sstevel@tonic-gate 	HEADER hdr;
67*7c478bd9Sstevel@tonic-gate 	u_char buf[MAXPACKET];
68*7c478bd9Sstevel@tonic-gate } querybuf;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static union {
71*7c478bd9Sstevel@tonic-gate 	long al;
72*7c478bd9Sstevel@tonic-gate 	char ac;
73*7c478bd9Sstevel@tonic-gate } align;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate int h_errno;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate static struct hostent *
79*7c478bd9Sstevel@tonic-gate getanswer(answer, anslen, iquery)
80*7c478bd9Sstevel@tonic-gate 	querybuf *answer;
81*7c478bd9Sstevel@tonic-gate 	int anslen;
82*7c478bd9Sstevel@tonic-gate 	int iquery;
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	register HEADER *hp;
85*7c478bd9Sstevel@tonic-gate 	register u_char *cp;
86*7c478bd9Sstevel@tonic-gate 	register int n;
87*7c478bd9Sstevel@tonic-gate 	u_char *eom;
88*7c478bd9Sstevel@tonic-gate 	char *bp, **ap;
89*7c478bd9Sstevel@tonic-gate 	int type, class, buflen, ancount, qdcount;
90*7c478bd9Sstevel@tonic-gate 	int haveanswer, getclass = C_ANY;
91*7c478bd9Sstevel@tonic-gate 	char **hap;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	eom = answer->buf + anslen;
94*7c478bd9Sstevel@tonic-gate 	/*
95*7c478bd9Sstevel@tonic-gate 	 * find first satisfactory answer
96*7c478bd9Sstevel@tonic-gate 	 */
97*7c478bd9Sstevel@tonic-gate 	hp = &answer->hdr;
98*7c478bd9Sstevel@tonic-gate 	ancount = ntohs(hp->ancount);
99*7c478bd9Sstevel@tonic-gate 	qdcount = ntohs(hp->qdcount);
100*7c478bd9Sstevel@tonic-gate 	bp = hostbuf;
101*7c478bd9Sstevel@tonic-gate 	buflen = sizeof (hostbuf);
102*7c478bd9Sstevel@tonic-gate 	cp = answer->buf + sizeof (HEADER);
103*7c478bd9Sstevel@tonic-gate 	if (qdcount) {
104*7c478bd9Sstevel@tonic-gate 		if (iquery) {
105*7c478bd9Sstevel@tonic-gate 			if ((n = dn_expand((char *)answer->buf, eom,
106*7c478bd9Sstevel@tonic-gate 						cp, bp, buflen)) < 0) {
107*7c478bd9Sstevel@tonic-gate 				h_errno = NO_RECOVERY;
108*7c478bd9Sstevel@tonic-gate 				return ((struct hostent *) NULL);
109*7c478bd9Sstevel@tonic-gate 			}
110*7c478bd9Sstevel@tonic-gate 			cp += n + QFIXEDSZ;
111*7c478bd9Sstevel@tonic-gate 			host.h_name = bp;
112*7c478bd9Sstevel@tonic-gate 			n = strlen(bp) + 1;
113*7c478bd9Sstevel@tonic-gate 			bp += n;
114*7c478bd9Sstevel@tonic-gate 			buflen -= n;
115*7c478bd9Sstevel@tonic-gate 		} else
116*7c478bd9Sstevel@tonic-gate 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
117*7c478bd9Sstevel@tonic-gate 		while (--qdcount > 0)
118*7c478bd9Sstevel@tonic-gate 			cp += dn_skipname(cp, eom) + QFIXEDSZ;
119*7c478bd9Sstevel@tonic-gate 	} else if (iquery) {
120*7c478bd9Sstevel@tonic-gate 		if (hp->aa)
121*7c478bd9Sstevel@tonic-gate 			h_errno = HOST_NOT_FOUND;
122*7c478bd9Sstevel@tonic-gate 		else
123*7c478bd9Sstevel@tonic-gate 			h_errno = TRY_AGAIN;
124*7c478bd9Sstevel@tonic-gate 		return ((struct hostent *) NULL);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 	ap = host_aliases;
127*7c478bd9Sstevel@tonic-gate 	host.h_aliases = host_aliases;
128*7c478bd9Sstevel@tonic-gate 	hap = h_addr_ptrs;
129*7c478bd9Sstevel@tonic-gate #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
130*7c478bd9Sstevel@tonic-gate 	host.h_addr_list = h_addr_ptrs;
131*7c478bd9Sstevel@tonic-gate #endif
132*7c478bd9Sstevel@tonic-gate 	haveanswer = 0;
133*7c478bd9Sstevel@tonic-gate 	while (--ancount >= 0 && cp < eom && haveanswer < MAXADDRS) {
134*7c478bd9Sstevel@tonic-gate 		if ((n = dn_expand((char *)answer->buf, eom,
135*7c478bd9Sstevel@tonic-gate 						cp, bp, buflen)) < 0)
136*7c478bd9Sstevel@tonic-gate 			break;
137*7c478bd9Sstevel@tonic-gate 		cp += n;
138*7c478bd9Sstevel@tonic-gate 		type = _getshort(cp);
139*7c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
140*7c478bd9Sstevel@tonic-gate 		class = _getshort(cp);
141*7c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short) + sizeof (u_long);
142*7c478bd9Sstevel@tonic-gate 		n = _getshort(cp);
143*7c478bd9Sstevel@tonic-gate 		cp += sizeof (u_short);
144*7c478bd9Sstevel@tonic-gate 		if (type == T_CNAME) {
145*7c478bd9Sstevel@tonic-gate 			cp += n;
146*7c478bd9Sstevel@tonic-gate 			if (ap >= &host_aliases[MAXALIASES-1])
147*7c478bd9Sstevel@tonic-gate 				continue;
148*7c478bd9Sstevel@tonic-gate 			*ap++ = bp;
149*7c478bd9Sstevel@tonic-gate 			n = strlen(bp) + 1;
150*7c478bd9Sstevel@tonic-gate 			bp += n;
151*7c478bd9Sstevel@tonic-gate 			buflen -= n;
152*7c478bd9Sstevel@tonic-gate 			continue;
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 		if (iquery && type == T_PTR) {
155*7c478bd9Sstevel@tonic-gate 			if ((n = dn_expand((char *)answer->buf, eom,
156*7c478bd9Sstevel@tonic-gate 					cp, bp, buflen)) < 0) {
157*7c478bd9Sstevel@tonic-gate 				cp += n;
158*7c478bd9Sstevel@tonic-gate 				continue;
159*7c478bd9Sstevel@tonic-gate 			}
160*7c478bd9Sstevel@tonic-gate 			cp += n;
161*7c478bd9Sstevel@tonic-gate 			host.h_name = bp;
162*7c478bd9Sstevel@tonic-gate 			return (&host);
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 		if (iquery || type != T_A) {
165*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
166*7c478bd9Sstevel@tonic-gate 			if (_res.options & RES_DEBUG)
167*7c478bd9Sstevel@tonic-gate 				printf("unexpected answer type %d, size %d\n",
168*7c478bd9Sstevel@tonic-gate 					type, n);
169*7c478bd9Sstevel@tonic-gate #endif
170*7c478bd9Sstevel@tonic-gate 			cp += n;
171*7c478bd9Sstevel@tonic-gate 			continue;
172*7c478bd9Sstevel@tonic-gate 		}
173*7c478bd9Sstevel@tonic-gate 		if (haveanswer) {
174*7c478bd9Sstevel@tonic-gate 			if (n != host.h_length) {
175*7c478bd9Sstevel@tonic-gate 				cp += n;
176*7c478bd9Sstevel@tonic-gate 				continue;
177*7c478bd9Sstevel@tonic-gate 			}
178*7c478bd9Sstevel@tonic-gate 			if (class != getclass) {
179*7c478bd9Sstevel@tonic-gate 				cp += n;
180*7c478bd9Sstevel@tonic-gate 				continue;
181*7c478bd9Sstevel@tonic-gate 			}
182*7c478bd9Sstevel@tonic-gate 		} else {
183*7c478bd9Sstevel@tonic-gate 			host.h_length = n;
184*7c478bd9Sstevel@tonic-gate 			getclass = class;
185*7c478bd9Sstevel@tonic-gate 			host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
186*7c478bd9Sstevel@tonic-gate 			if (!iquery) {
187*7c478bd9Sstevel@tonic-gate 				host.h_name = bp;
188*7c478bd9Sstevel@tonic-gate 				bp += strlen(bp) + 1;
189*7c478bd9Sstevel@tonic-gate 			}
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		bp += sizeof (align) - ((u_long)bp % sizeof (align));
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 		if (bp + n >= &hostbuf[sizeof (hostbuf)]) {
195*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
196*7c478bd9Sstevel@tonic-gate 			if (_res.options & RES_DEBUG)
197*7c478bd9Sstevel@tonic-gate 				printf("size (%d) too big\n", n);
198*7c478bd9Sstevel@tonic-gate #endif
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 		}
201*7c478bd9Sstevel@tonic-gate #ifdef SYSV
202*7c478bd9Sstevel@tonic-gate 		memcpy((void *)(*hap++ = bp), (void *)cp, n);
203*7c478bd9Sstevel@tonic-gate #else
204*7c478bd9Sstevel@tonic-gate 		bcopy(cp, *hap++ = bp, n);
205*7c478bd9Sstevel@tonic-gate #endif
206*7c478bd9Sstevel@tonic-gate 		bp += n;
207*7c478bd9Sstevel@tonic-gate 		cp += n;
208*7c478bd9Sstevel@tonic-gate 		haveanswer++;
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	if (haveanswer) {
211*7c478bd9Sstevel@tonic-gate 		*ap = NULL;
212*7c478bd9Sstevel@tonic-gate #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
213*7c478bd9Sstevel@tonic-gate 		*hap = NULL;
214*7c478bd9Sstevel@tonic-gate #else
215*7c478bd9Sstevel@tonic-gate 		host.h_addr = h_addr_ptrs[0];
216*7c478bd9Sstevel@tonic-gate #endif
217*7c478bd9Sstevel@tonic-gate 		return (&host);
218*7c478bd9Sstevel@tonic-gate 	} else {
219*7c478bd9Sstevel@tonic-gate 		h_errno = TRY_AGAIN;
220*7c478bd9Sstevel@tonic-gate 		return ((struct hostent *) NULL);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate struct hostent *
225*7c478bd9Sstevel@tonic-gate res_gethostbyname(name)
226*7c478bd9Sstevel@tonic-gate 	char *name;
227*7c478bd9Sstevel@tonic-gate {
228*7c478bd9Sstevel@tonic-gate 	querybuf buf;
229*7c478bd9Sstevel@tonic-gate 	register char *cp;
230*7c478bd9Sstevel@tonic-gate 	int n;
231*7c478bd9Sstevel@tonic-gate 	struct hostent *hp, *gethostdomain();
232*7c478bd9Sstevel@tonic-gate 	static struct hostent *_gethtbyname();
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	/*
235*7c478bd9Sstevel@tonic-gate 	 * disallow names consisting only of digits/dots, unless
236*7c478bd9Sstevel@tonic-gate 	 * they end in a dot.
237*7c478bd9Sstevel@tonic-gate 	 */
238*7c478bd9Sstevel@tonic-gate 	if (isdigit(name[0]))
239*7c478bd9Sstevel@tonic-gate 		for (cp = name; /*EMPTY*/; ++cp) {
240*7c478bd9Sstevel@tonic-gate 			if (!*cp) {
241*7c478bd9Sstevel@tonic-gate 				if (*--cp == '.')
242*7c478bd9Sstevel@tonic-gate 					break;
243*7c478bd9Sstevel@tonic-gate 				h_errno = HOST_NOT_FOUND;
244*7c478bd9Sstevel@tonic-gate 				return ((struct hostent *) NULL);
245*7c478bd9Sstevel@tonic-gate 			}
246*7c478bd9Sstevel@tonic-gate 			if (!isdigit(*cp) && *cp != '.')
247*7c478bd9Sstevel@tonic-gate 				break;
248*7c478bd9Sstevel@tonic-gate 		}
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof (buf))) < 0) {
251*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
252*7c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
253*7c478bd9Sstevel@tonic-gate 			printf("res_search failed\n");
254*7c478bd9Sstevel@tonic-gate #endif
255*7c478bd9Sstevel@tonic-gate 		if (errno == ECONNREFUSED)
256*7c478bd9Sstevel@tonic-gate 			return (_gethtbyname(name));
257*7c478bd9Sstevel@tonic-gate 		else
258*7c478bd9Sstevel@tonic-gate 			return ((struct hostent *) NULL);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	return (getanswer(&buf, n, 0));
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate static struct hostent *
264*7c478bd9Sstevel@tonic-gate _getrhbyaddr(addr, len, type)
265*7c478bd9Sstevel@tonic-gate 	char *addr;
266*7c478bd9Sstevel@tonic-gate 	int len, type;
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	int n;
269*7c478bd9Sstevel@tonic-gate 	querybuf buf;
270*7c478bd9Sstevel@tonic-gate 	register struct hostent *hp;
271*7c478bd9Sstevel@tonic-gate 	char qbuf[MAXDNAME];
272*7c478bd9Sstevel@tonic-gate 	static struct hostent *_gethtbyaddr();
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	if (type != AF_INET)
275*7c478bd9Sstevel@tonic-gate 		return ((struct hostent *) NULL);
276*7c478bd9Sstevel@tonic-gate 	(void) sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa",
277*7c478bd9Sstevel@tonic-gate 		((unsigned)addr[3] & 0xff),
278*7c478bd9Sstevel@tonic-gate 		((unsigned)addr[2] & 0xff),
279*7c478bd9Sstevel@tonic-gate 		((unsigned)addr[1] & 0xff),
280*7c478bd9Sstevel@tonic-gate 		((unsigned)addr[0] & 0xff));
281*7c478bd9Sstevel@tonic-gate 	n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof (buf));
282*7c478bd9Sstevel@tonic-gate 	if (n < 0) {
283*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
284*7c478bd9Sstevel@tonic-gate 		if (_res.options & RES_DEBUG)
285*7c478bd9Sstevel@tonic-gate 			printf("res_query failed\n");
286*7c478bd9Sstevel@tonic-gate #endif
287*7c478bd9Sstevel@tonic-gate 		if (errno == ECONNREFUSED)
288*7c478bd9Sstevel@tonic-gate 			return (_gethtbyaddr(addr, len, type));
289*7c478bd9Sstevel@tonic-gate 		return ((struct hostent *) NULL);
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 	hp = getanswer(&buf, n, 1);
292*7c478bd9Sstevel@tonic-gate 	if (hp == NULL)
293*7c478bd9Sstevel@tonic-gate 		return ((struct hostent *) NULL);
294*7c478bd9Sstevel@tonic-gate 	hp->h_addrtype = type;
295*7c478bd9Sstevel@tonic-gate 	hp->h_length = len;
296*7c478bd9Sstevel@tonic-gate 	h_addr_ptrs[0] = (char *)&host_addr;
297*7c478bd9Sstevel@tonic-gate 	h_addr_ptrs[1] = (char *)0;
298*7c478bd9Sstevel@tonic-gate 	host_addr = *(struct in_addr *)addr;
299*7c478bd9Sstevel@tonic-gate 	return (hp);
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate /*
303*7c478bd9Sstevel@tonic-gate  * First we get what the PTR record says, but do an extra call
304*7c478bd9Sstevel@tonic-gate  * to gethostbyname() to make sure that someone is not trying to
305*7c478bd9Sstevel@tonic-gate  * spoof us.  Hopefully this is not done that often, so good
306*7c478bd9Sstevel@tonic-gate  * performance is not really an issue.
307*7c478bd9Sstevel@tonic-gate  */
308*7c478bd9Sstevel@tonic-gate struct hostent *
309*7c478bd9Sstevel@tonic-gate res_gethostbyaddr(addr, len, type)
310*7c478bd9Sstevel@tonic-gate 	char	*addr;
311*7c478bd9Sstevel@tonic-gate 	int	len;
312*7c478bd9Sstevel@tonic-gate 	int	type;
313*7c478bd9Sstevel@tonic-gate {
314*7c478bd9Sstevel@tonic-gate 	char		**a, hbuf[MAXHOSTNAMELEN];
315*7c478bd9Sstevel@tonic-gate 	struct hostent	*hp, *hp2;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	if ((hp = _getrhbyaddr(addr, len, type)) == (struct hostent *)NULL)
318*7c478bd9Sstevel@tonic-gate 		return ((struct hostent *)NULL);
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	/* hang on to what we got as an answer */
321*7c478bd9Sstevel@tonic-gate 	(void) strcpy(hbuf, hp->h_name);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	/* check to make sure by doing a forward query */
324*7c478bd9Sstevel@tonic-gate 	if ((hp2 = res_gethostbyname(hbuf)) != (struct hostent *)NULL)
325*7c478bd9Sstevel@tonic-gate 		for (a = hp2->h_addr_list; *a; a++)
326*7c478bd9Sstevel@tonic-gate #ifdef SYSV
327*7c478bd9Sstevel@tonic-gate 			if (memcmp(*a, addr, hp2->h_length) == 0)
328*7c478bd9Sstevel@tonic-gate #else
329*7c478bd9Sstevel@tonic-gate 			if (bcmp(*a, addr, hp2->h_length) == 0)
330*7c478bd9Sstevel@tonic-gate #endif
331*7c478bd9Sstevel@tonic-gate 				return (hp2);
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	/*
334*7c478bd9Sstevel@tonic-gate 	 * we've been spoofed, make sure to log it.
335*7c478bd9Sstevel@tonic-gate 	 * XXX - syslog needs a security priority level.
336*7c478bd9Sstevel@tonic-gate 	 */
337*7c478bd9Sstevel@tonic-gate 	syslog(LOG_NOTICE, "gethostbyaddr: %s != %s", hbuf,
338*7c478bd9Sstevel@tonic-gate 		inet_ntoa(*(struct in_addr *)addr));
339*7c478bd9Sstevel@tonic-gate 	return ((struct hostent *)NULL);
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate static
343*7c478bd9Sstevel@tonic-gate _sethtent(f)
344*7c478bd9Sstevel@tonic-gate 	int f;
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) return(0);
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	if (hostf == NULL)
349*7c478bd9Sstevel@tonic-gate 		hostf = fopen(HOSTDB, "r");
350*7c478bd9Sstevel@tonic-gate 	else
351*7c478bd9Sstevel@tonic-gate 		rewind(hostf);
352*7c478bd9Sstevel@tonic-gate 	stayopen |= f;
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate static
356*7c478bd9Sstevel@tonic-gate _endhtent()
357*7c478bd9Sstevel@tonic-gate {
358*7c478bd9Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) return(0);
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	if (hostf && !stayopen) {
361*7c478bd9Sstevel@tonic-gate 		(void) fclose(hostf);
362*7c478bd9Sstevel@tonic-gate 		hostf = NULL;
363*7c478bd9Sstevel@tonic-gate 	}
364*7c478bd9Sstevel@tonic-gate }
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate static struct hostent *
367*7c478bd9Sstevel@tonic-gate _gethtent()
368*7c478bd9Sstevel@tonic-gate {
369*7c478bd9Sstevel@tonic-gate 	char *p;
370*7c478bd9Sstevel@tonic-gate 	register char *cp, **q;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	if (__res_no_hosts_fallback()) return(NULL);
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r")) == NULL)
375*7c478bd9Sstevel@tonic-gate 		return (NULL);
376*7c478bd9Sstevel@tonic-gate again:
377*7c478bd9Sstevel@tonic-gate 	if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
378*7c478bd9Sstevel@tonic-gate 		return (NULL);
379*7c478bd9Sstevel@tonic-gate 	if (*p == '#')
380*7c478bd9Sstevel@tonic-gate 		goto again;
381*7c478bd9Sstevel@tonic-gate 	cp = any(p, "#\n");
382*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
383*7c478bd9Sstevel@tonic-gate 		goto again;
384*7c478bd9Sstevel@tonic-gate 	*cp = '\0';
385*7c478bd9Sstevel@tonic-gate 	cp = any(p, " \t");
386*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
387*7c478bd9Sstevel@tonic-gate 		goto again;
388*7c478bd9Sstevel@tonic-gate 	*cp++ = '\0';
389*7c478bd9Sstevel@tonic-gate 	/* THIS STUFF IS INTERNET SPECIFIC */
390*7c478bd9Sstevel@tonic-gate #if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */
391*7c478bd9Sstevel@tonic-gate 	host.h_addr_list = host_addrs;
392*7c478bd9Sstevel@tonic-gate #endif
393*7c478bd9Sstevel@tonic-gate 	host.h_addr = hostaddr;
394*7c478bd9Sstevel@tonic-gate 	*((u_long *)host.h_addr) = inet_addr(p);
395*7c478bd9Sstevel@tonic-gate 	host.h_length = sizeof (u_long);
396*7c478bd9Sstevel@tonic-gate 	host.h_addrtype = AF_INET;
397*7c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
398*7c478bd9Sstevel@tonic-gate 		cp++;
399*7c478bd9Sstevel@tonic-gate 	host.h_name = cp;
400*7c478bd9Sstevel@tonic-gate 	q = host.h_aliases = host_aliases;
401*7c478bd9Sstevel@tonic-gate 	cp = any(cp, " \t");
402*7c478bd9Sstevel@tonic-gate 	if (cp != NULL)
403*7c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
404*7c478bd9Sstevel@tonic-gate 	while (cp && *cp) {
405*7c478bd9Sstevel@tonic-gate 		if (*cp == ' ' || *cp == '\t') {
406*7c478bd9Sstevel@tonic-gate 			cp++;
407*7c478bd9Sstevel@tonic-gate 			continue;
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 		if (q < &host_aliases[MAXALIASES - 1])
410*7c478bd9Sstevel@tonic-gate 			*q++ = cp;
411*7c478bd9Sstevel@tonic-gate 		cp = any(cp, " \t");
412*7c478bd9Sstevel@tonic-gate 		if (cp != NULL)
413*7c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
414*7c478bd9Sstevel@tonic-gate 	}
415*7c478bd9Sstevel@tonic-gate 	*q = NULL;
416*7c478bd9Sstevel@tonic-gate 	return (&host);
417*7c478bd9Sstevel@tonic-gate }
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate static char *
420*7c478bd9Sstevel@tonic-gate any(cp, match)
421*7c478bd9Sstevel@tonic-gate 	register char *cp;
422*7c478bd9Sstevel@tonic-gate 	char *match;
423*7c478bd9Sstevel@tonic-gate {
424*7c478bd9Sstevel@tonic-gate 	register char *mp, c;
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	while (c = *cp) {
427*7c478bd9Sstevel@tonic-gate 		for (mp = match; *mp; mp++)
428*7c478bd9Sstevel@tonic-gate 			if (*mp == c)
429*7c478bd9Sstevel@tonic-gate 				return (cp);
430*7c478bd9Sstevel@tonic-gate 		cp++;
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 	return ((char *)0);
433*7c478bd9Sstevel@tonic-gate }
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate static struct hostent *
436*7c478bd9Sstevel@tonic-gate _gethtbyname(name)
437*7c478bd9Sstevel@tonic-gate 	char *name;
438*7c478bd9Sstevel@tonic-gate {
439*7c478bd9Sstevel@tonic-gate 	register struct hostent *p;
440*7c478bd9Sstevel@tonic-gate 	register char **cp;
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	_sethtent(0);
443*7c478bd9Sstevel@tonic-gate 	while (p = _gethtent()) {
444*7c478bd9Sstevel@tonic-gate 		if (strcasecmp(p->h_name, name) == 0)
445*7c478bd9Sstevel@tonic-gate 			break;
446*7c478bd9Sstevel@tonic-gate 		for (cp = p->h_aliases; *cp != 0; cp++)
447*7c478bd9Sstevel@tonic-gate 			if (strcasecmp(*cp, name) == 0)
448*7c478bd9Sstevel@tonic-gate 				goto found;
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate found:
451*7c478bd9Sstevel@tonic-gate 	_endhtent();
452*7c478bd9Sstevel@tonic-gate 	return (p);
453*7c478bd9Sstevel@tonic-gate }
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate static struct hostent *
456*7c478bd9Sstevel@tonic-gate _gethtbyaddr(addr, len, type)
457*7c478bd9Sstevel@tonic-gate 	char *addr;
458*7c478bd9Sstevel@tonic-gate 	int len, type;
459*7c478bd9Sstevel@tonic-gate {
460*7c478bd9Sstevel@tonic-gate 	register struct hostent *p;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	_sethtent(0);
463*7c478bd9Sstevel@tonic-gate 	while (p = _gethtent())
464*7c478bd9Sstevel@tonic-gate #ifdef SYSV
465*7c478bd9Sstevel@tonic-gate 		if (p->h_addrtype == type && !memcmp(p->h_addr, addr, len))
466*7c478bd9Sstevel@tonic-gate #else
467*7c478bd9Sstevel@tonic-gate 		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
468*7c478bd9Sstevel@tonic-gate #endif
469*7c478bd9Sstevel@tonic-gate 			break;
470*7c478bd9Sstevel@tonic-gate 	_endhtent();
471*7c478bd9Sstevel@tonic-gate 	return (p);
472*7c478bd9Sstevel@tonic-gate }
473