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