xref: /freebsd/lib/libc/net/gethostbydns.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1 /*
2  * ++Copyright++ 1985, 1988, 1993
3  * -
4  * Copyright (c) 1985, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  * -
31  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32  *
33  * Permission to use, copy, modify, and distribute this software for any
34  * purpose with or without fee is hereby granted, provided that the above
35  * copyright notice and this permission notice appear in all copies, and that
36  * the name of Digital Equipment Corporation not be used in advertising or
37  * publicity pertaining to distribution of the document or software without
38  * specific, written prior permission.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47  * SOFTWARE.
48  * -
49  * --Copyright--
50  */
51 
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
54 static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
55 #endif /* LIBC_SCCS and not lint */
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58 
59 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <arpa/nameser.h>
64 
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68 #include <string.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <errno.h>
73 #include <syslog.h>
74 #include <stdarg.h>
75 #include <nsswitch.h>
76 
77 #include "netdb_private.h"
78 #include "res_config.h"
79 
80 #define SPRINTF(x) ((size_t)sprintf x)
81 
82 static const char AskedForGot[] =
83 		"gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
84 
85 #ifdef RESOLVSORT
86 static void addrsort(char **, int, res_state);
87 #endif
88 
89 #ifdef DEBUG
90 static void dbg_printf(char *, int, res_state) __printflike(1, 0);
91 #endif
92 
93 #define MAXPACKET	(64*1024)
94 
95 typedef union {
96     HEADER hdr;
97     u_char buf[MAXPACKET];
98 } querybuf;
99 
100 typedef union {
101     int32_t al;
102     char ac;
103 } align;
104 
105 int _dns_ttl_;
106 
107 #ifdef DEBUG
108 static void
109 dbg_printf(char *msg, int num, res_state res)
110 {
111 	if (res->options & RES_DEBUG) {
112 		int save = errno;
113 
114 		printf(msg, num);
115 		errno = save;
116 	}
117 }
118 #else
119 # define dbg_printf(msg, num, res) /*nada*/
120 #endif
121 
122 #define BOUNDED_INCR(x) \
123 	do { \
124 		cp += x; \
125 		if (cp > eom) { \
126 			RES_SET_H_ERRNO(statp, NO_RECOVERY); \
127 			return (-1); \
128 		} \
129 	} while (0)
130 
131 #define BOUNDS_CHECK(ptr, count) \
132 	do { \
133 		if ((ptr) + (count) > eom) { \
134 			RES_SET_H_ERRNO(statp, NO_RECOVERY); \
135 			return (-1); \
136 		} \
137 	} while (0)
138 
139 static int
140 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
141     struct hostent *he, struct hostent_data *hed, res_state statp)
142 {
143 	const HEADER *hp;
144 	const u_char *cp;
145 	int n;
146 	const u_char *eom, *erdata;
147 	char *bp, *ep, **ap, **hap;
148 	int type, class, ancount, qdcount;
149 	int haveanswer, had_error;
150 	int toobig = 0;
151 	char tbuf[MAXDNAME];
152 	const char *tname;
153 	int (*name_ok)(const char *);
154 
155 	tname = qname;
156 	he->h_name = NULL;
157 	eom = answer->buf + anslen;
158 	switch (qtype) {
159 	case T_A:
160 	case T_AAAA:
161 		name_ok = res_hnok;
162 		break;
163 	case T_PTR:
164 		name_ok = res_dnok;
165 		break;
166 	default:
167 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
168 		return (-1);	/* XXX should be abort(); */
169 	}
170 	/*
171 	 * find first satisfactory answer
172 	 */
173 	hp = &answer->hdr;
174 	ancount = ntohs(hp->ancount);
175 	qdcount = ntohs(hp->qdcount);
176 	bp = hed->hostbuf;
177 	ep = hed->hostbuf + sizeof hed->hostbuf;
178 	cp = answer->buf;
179 	BOUNDED_INCR(HFIXEDSZ);
180 	if (qdcount != 1) {
181 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
182 		return (-1);
183 	}
184 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
185 	if ((n < 0) || !(*name_ok)(bp)) {
186 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
187 		return (-1);
188 	}
189 	BOUNDED_INCR(n + QFIXEDSZ);
190 	if (qtype == T_A || qtype == T_AAAA) {
191 		/* res_send() has already verified that the query name is the
192 		 * same as the one we sent; this just gets the expanded name
193 		 * (i.e., with the succeeding search-domain tacked on).
194 		 */
195 		n = strlen(bp) + 1;		/* for the \0 */
196 		if (n >= MAXHOSTNAMELEN) {
197 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
198 			return (-1);
199 		}
200 		he->h_name = bp;
201 		bp += n;
202 		/* The qname can be abbreviated, but h_name is now absolute. */
203 		qname = he->h_name;
204 	}
205 	ap = hed->host_aliases;
206 	*ap = NULL;
207 	he->h_aliases = hed->host_aliases;
208 	hap = hed->h_addr_ptrs;
209 	*hap = NULL;
210 	he->h_addr_list = hed->h_addr_ptrs;
211 	haveanswer = 0;
212 	had_error = 0;
213 	_dns_ttl_ = -1;
214 	while (ancount-- > 0 && cp < eom && !had_error) {
215 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
216 		if ((n < 0) || !(*name_ok)(bp)) {
217 			had_error++;
218 			continue;
219 		}
220 		cp += n;			/* name */
221 		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
222 		type = _getshort(cp);
223  		cp += INT16SZ;			/* type */
224 		class = _getshort(cp);
225  		cp += INT16SZ;			/* class */
226 		if (qtype == T_A  && type == T_A)
227 			_dns_ttl_ = _getlong(cp);
228 		cp += INT32SZ;			/* TTL */
229 		n = _getshort(cp);
230 		cp += INT16SZ;			/* len */
231 		BOUNDS_CHECK(cp, n);
232 		erdata = cp + n;
233 		if (class != C_IN) {
234 			/* XXX - debug? syslog? */
235 			cp += n;
236 			continue;		/* XXX - had_error++ ? */
237 		}
238 		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
239 			if (ap >= &hed->host_aliases[_MAXALIASES-1])
240 				continue;
241 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
242 			if ((n < 0) || !(*name_ok)(tbuf)) {
243 				had_error++;
244 				continue;
245 			}
246 			cp += n;
247 			if (cp != erdata) {
248 				RES_SET_H_ERRNO(statp, NO_RECOVERY);
249 				return (-1);
250 			}
251 			/* Store alias. */
252 			*ap++ = bp;
253 			n = strlen(bp) + 1;	/* for the \0 */
254 			if (n >= MAXHOSTNAMELEN) {
255 				had_error++;
256 				continue;
257 			}
258 			bp += n;
259 			/* Get canonical name. */
260 			n = strlen(tbuf) + 1;	/* for the \0 */
261 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
262 				had_error++;
263 				continue;
264 			}
265 			strcpy(bp, tbuf);
266 			he->h_name = bp;
267 			bp += n;
268 			continue;
269 		}
270 		if (qtype == T_PTR && type == T_CNAME) {
271 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
272 			if (n < 0 || !res_dnok(tbuf)) {
273 				had_error++;
274 				continue;
275 			}
276 			cp += n;
277 			if (cp != erdata) {
278 				RES_SET_H_ERRNO(statp, NO_RECOVERY);
279 				return (-1);
280 			}
281 			/* Get canonical name. */
282 			n = strlen(tbuf) + 1;	/* for the \0 */
283 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
284 				had_error++;
285 				continue;
286 			}
287 			strcpy(bp, tbuf);
288 			tname = bp;
289 			bp += n;
290 			continue;
291 		}
292 		if (type != qtype) {
293 			if (type != T_SIG && type != ns_t_dname)
294 				syslog(LOG_NOTICE|LOG_AUTH,
295 	"gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
296 				       qname, p_class(C_IN), p_type(qtype),
297 				       p_type(type));
298 			cp += n;
299 			continue;		/* XXX - had_error++ ? */
300 		}
301 		switch (type) {
302 		case T_PTR:
303 			if (strcasecmp(tname, bp) != 0) {
304 				syslog(LOG_NOTICE|LOG_AUTH,
305 				       AskedForGot, qname, bp);
306 				cp += n;
307 				continue;	/* XXX - had_error++ ? */
308 			}
309 			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
310 			if ((n < 0) || !res_hnok(bp)) {
311 				had_error++;
312 				break;
313 			}
314 #if MULTI_PTRS_ARE_ALIASES
315 			cp += n;
316 			if (cp != erdata) {
317 				RES_SET_H_ERRNO(statp, NO_RECOVERY);
318 				return (-1);
319 			}
320 			if (!haveanswer)
321 				he->h_name = bp;
322 			else if (ap < &hed->host_aliases[_MAXALIASES-1])
323 				*ap++ = bp;
324 			else
325 				n = -1;
326 			if (n != -1) {
327 				n = strlen(bp) + 1;	/* for the \0 */
328 				if (n >= MAXHOSTNAMELEN) {
329 					had_error++;
330 					break;
331 				}
332 				bp += n;
333 			}
334 			break;
335 #else
336 			he->h_name = bp;
337 			if (statp->options & RES_USE_INET6) {
338 				n = strlen(bp) + 1;	/* for the \0 */
339 				if (n >= MAXHOSTNAMELEN) {
340 					had_error++;
341 					break;
342 				}
343 				bp += n;
344 				_map_v4v6_hostent(he, &bp, ep);
345 			}
346 			RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
347 			return (0);
348 #endif
349 		case T_A:
350 		case T_AAAA:
351 			if (strcasecmp(he->h_name, bp) != 0) {
352 				syslog(LOG_NOTICE|LOG_AUTH,
353 				       AskedForGot, he->h_name, bp);
354 				cp += n;
355 				continue;	/* XXX - had_error++ ? */
356 			}
357 			if (n != he->h_length) {
358 				cp += n;
359 				continue;
360 			}
361 			if (!haveanswer) {
362 				int nn;
363 
364 				he->h_name = bp;
365 				nn = strlen(bp) + 1;	/* for the \0 */
366 				bp += nn;
367 			}
368 
369 			bp += sizeof(align) - ((u_long)bp % sizeof(align));
370 
371 			if (bp + n >= ep) {
372 				dbg_printf("size (%d) too big\n", n, statp);
373 				had_error++;
374 				continue;
375 			}
376 			if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
377 				if (!toobig++)
378 					dbg_printf("Too many addresses (%d)\n",
379 						_MAXADDRS, statp);
380 				cp += n;
381 				continue;
382 			}
383 			memcpy(*hap++ = bp, cp, n);
384 			bp += n;
385 			cp += n;
386 			if (cp != erdata) {
387 				RES_SET_H_ERRNO(statp, NO_RECOVERY);
388 				return (-1);
389 			}
390 			break;
391 		default:
392 			dbg_printf("Impossible condition (type=%d)\n", type,
393 			    statp);
394 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
395 			return (-1);
396 			/* BIND has abort() here, too risky on bad data */
397 		}
398 		if (!had_error)
399 			haveanswer++;
400 	}
401 	if (haveanswer) {
402 		*ap = NULL;
403 		*hap = NULL;
404 # if defined(RESOLVSORT)
405 		/*
406 		 * Note: we sort even if host can take only one address
407 		 * in its return structures - should give it the "best"
408 		 * address in that case, not some random one
409 		 */
410 		if (statp->nsort && haveanswer > 1 && qtype == T_A)
411 			addrsort(hed->h_addr_ptrs, haveanswer, statp);
412 # endif /*RESOLVSORT*/
413 		if (!he->h_name) {
414 			n = strlen(qname) + 1;	/* for the \0 */
415 			if (n > ep - bp || n >= MAXHOSTNAMELEN)
416 				goto no_recovery;
417 			strcpy(bp, qname);
418 			he->h_name = bp;
419 			bp += n;
420 		}
421 		if (statp->options & RES_USE_INET6)
422 			_map_v4v6_hostent(he, &bp, ep);
423 		RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
424 		return (0);
425 	}
426  no_recovery:
427 	RES_SET_H_ERRNO(statp, NO_RECOVERY);
428 	return (-1);
429 }
430 
431 /* XXX: for async DNS resolver in ypserv */
432 struct hostent *
433 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
434 {
435 	struct hostent *he;
436 	struct hostent_data *hed;
437 	int error;
438 	res_state statp;
439 
440 	statp = __res_state();
441 	if ((he = __hostent_init()) == NULL ||
442 	    (hed = __hostent_data_init()) == NULL) {
443 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
444 		return (NULL);
445 	}
446 	switch (qtype) {
447 	case T_AAAA:
448 		he->h_addrtype = AF_INET6;
449 		he->h_length = NS_IN6ADDRSZ;
450 		break;
451 	case T_A:
452 	default:
453 		he->h_addrtype = AF_INET;
454 		he->h_length = NS_INADDRSZ;
455 		break;
456 	}
457 
458 	error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
459 	    he, hed, statp);
460 	return (error == 0) ? he : NULL;
461 }
462 
463 int
464 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
465 {
466 	const char *name;
467 	int af;
468 	char *buffer;
469 	size_t buflen;
470 	int *errnop, *h_errnop;
471 	struct hostent *hptr, he;
472 	struct hostent_data *hed;
473 	querybuf *buf;
474 	int n, type, error;
475 	res_state statp;
476 
477 	name = va_arg(ap, const char *);
478 	af = va_arg(ap, int);
479 	hptr = va_arg(ap, struct hostent *);
480 	buffer = va_arg(ap, char *);
481 	buflen = va_arg(ap, size_t);
482 	errnop = va_arg(ap, int *);
483 	h_errnop = va_arg(ap, int *);
484 
485 	*((struct hostent **)rval) = NULL;
486 
487 	statp = __res_state();
488 	if ((hed = __hostent_data_init()) == NULL) {
489 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
490 		*h_errnop = statp->res_h_errno;
491 		return (NS_NOTFOUND);
492 	}
493 
494 	he.h_addrtype = af;
495 	switch (af) {
496 	case AF_INET:
497 		he.h_length = NS_INADDRSZ;
498 		type = T_A;
499 		break;
500 	case AF_INET6:
501 		he.h_length = NS_IN6ADDRSZ;
502 		type = T_AAAA;
503 		break;
504 	default:
505 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
506 		*h_errnop = statp->res_h_errno;
507 		errno = EAFNOSUPPORT;
508 		return (NS_UNAVAIL);
509 	}
510 
511 	if ((buf = malloc(sizeof(*buf))) == NULL) {
512 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
513 		*h_errnop = statp->res_h_errno;
514 		return (NS_NOTFOUND);
515 	}
516 	n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
517 	if (n < 0) {
518 		free(buf);
519 		dbg_printf("res_nsearch failed (%d)\n", n, statp);
520 		*h_errnop = statp->res_h_errno;
521 		return (NS_NOTFOUND);
522 	} else if (n > sizeof(buf->buf)) {
523 		free(buf);
524 		dbg_printf("static buffer is too small (%d)\n", n, statp);
525 		*h_errnop = statp->res_h_errno;
526 		return (NS_UNAVAIL);
527 	}
528 	error = gethostanswer(buf, n, name, type, &he, hed, statp);
529 	free(buf);
530 	if (error != 0) {
531 		*h_errnop = statp->res_h_errno;
532 		switch (statp->res_h_errno) {
533 		case HOST_NOT_FOUND:
534 			return (NS_NOTFOUND);
535 		case TRY_AGAIN:
536 			return (NS_TRYAGAIN);
537 		default:
538 			return (NS_UNAVAIL);
539 		}
540 		/*NOTREACHED*/
541 	}
542 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
543 		*errnop = errno;
544 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
545 		*h_errnop = statp->res_h_errno;
546 		return (NS_RETURN);
547 	}
548 	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
549 	*((struct hostent **)rval) = hptr;
550 	return (NS_SUCCESS);
551 }
552 
553 int
554 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
555 {
556 	const void *addr;
557 	socklen_t len;
558 	int af;
559 	char *buffer;
560 	size_t buflen;
561 	int *errnop, *h_errnop;
562 	const u_char *uaddr;
563 	struct hostent *hptr, he;
564 	struct hostent_data *hed;
565 	int n;
566 	querybuf *buf;
567 	char qbuf[MAXDNAME+1], *qp;
568 	res_state statp;
569 #ifdef SUNSECURITY
570 	struct hostdata rhd;
571 	struct hostent *rhe;
572 	char **haddr;
573 	u_long old_options;
574 	char hname2[MAXDNAME+1], numaddr[46];
575 	int ret_h_error;
576 #endif /*SUNSECURITY*/
577 
578 	addr = va_arg(ap, const void *);
579 	len = va_arg(ap, socklen_t);
580 	af = va_arg(ap, int);
581 	hptr = va_arg(ap, struct hostent *);
582 	buffer = va_arg(ap, char *);
583 	buflen = va_arg(ap, size_t);
584 	errnop = va_arg(ap, int *);
585 	h_errnop = va_arg(ap, int *);
586 	uaddr = (const u_char *)addr;
587 
588 	*((struct hostent **)rval) = NULL;
589 
590 	statp = __res_state();
591 	if ((hed = __hostent_data_init()) == NULL) {
592 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
593 		*h_errnop = statp->res_h_errno;
594 		return (NS_NOTFOUND);
595 	}
596 
597 	switch (af) {
598 	case AF_INET:
599 		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
600 			       (uaddr[3] & 0xff),
601 			       (uaddr[2] & 0xff),
602 			       (uaddr[1] & 0xff),
603 			       (uaddr[0] & 0xff));
604 		break;
605 	case AF_INET6:
606 		qp = qbuf;
607 		for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
608 			qp += SPRINTF((qp, "%x.%x.",
609 				       uaddr[n] & 0xf,
610 				       (uaddr[n] >> 4) & 0xf));
611 		}
612 		strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
613 		break;
614 	default:
615 		abort();
616 	}
617 	if ((buf = malloc(sizeof(*buf))) == NULL) {
618 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
619 		*h_errnop = statp->res_h_errno;
620 		return NS_NOTFOUND;
621 	}
622 	n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
623 	    sizeof buf->buf);
624 	if (n < 0) {
625 		free(buf);
626 		dbg_printf("res_nquery failed (%d)\n", n, statp);
627 		*h_errnop = statp->res_h_errno;
628 		return (NS_UNAVAIL);
629 	}
630 	if (n > sizeof buf->buf) {
631 		free(buf);
632 		dbg_printf("static buffer is too small (%d)\n", n, statp);
633 		*h_errnop = statp->res_h_errno;
634 		return (NS_UNAVAIL);
635 	}
636 	if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
637 		free(buf);
638 		*h_errnop = statp->res_h_errno;
639 		switch (statp->res_h_errno) {
640 		case HOST_NOT_FOUND:
641 			return (NS_NOTFOUND);
642 		case TRY_AGAIN:
643 			return (NS_TRYAGAIN);
644 		default:
645 			return (NS_UNAVAIL);
646 		}
647 		/*NOTREACHED*/
648 	}
649 	free(buf);
650 #ifdef SUNSECURITY
651 	if (af == AF_INET) {
652 	    /*
653 	     * turn off search as the name should be absolute,
654 	     * 'localhost' should be matched by defnames
655 	     */
656 	    strncpy(hname2, he.h_name, MAXDNAME);
657 	    hname2[MAXDNAME] = '\0';
658 	    old_options = statp->options;
659 	    statp->options &= ~RES_DNSRCH;
660 	    statp->options |= RES_DEFNAMES;
661 	    memset(&rhd, 0, sizeof rhd);
662 	    rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
663 	        sizeof(rhd.data), &ret_h_error);
664 	    if (rhe == NULL) {
665 		if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
666 		    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
667 		syslog(LOG_NOTICE|LOG_AUTH,
668 		       "gethostbyaddr: No A record for %s (verifying [%s])",
669 		       hname2, numaddr);
670 		statp->options = old_options;
671 		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
672 		*h_errnop = statp->res_h_errno;
673 		return (NS_NOTFOUND);
674 	    }
675 	    statp->options = old_options;
676 	    for (haddr = rhe->h_addr_list; *haddr; haddr++)
677 		if (!memcmp(*haddr, addr, NS_INADDRSZ))
678 			break;
679 	    if (!*haddr) {
680 		if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
681 		    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
682 		syslog(LOG_NOTICE|LOG_AUTH,
683 		       "gethostbyaddr: A record of %s != PTR record [%s]",
684 		       hname2, numaddr);
685 		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
686 		*h_errnop = statp->res_h_errno;
687 		return (NS_NOTFOUND);
688 	    }
689 	}
690 #endif /*SUNSECURITY*/
691 	he.h_addrtype = af;
692 	he.h_length = len;
693 	memcpy(hed->host_addr, uaddr, len);
694 	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
695 	hed->h_addr_ptrs[1] = NULL;
696 	if (af == AF_INET && (statp->options & RES_USE_INET6)) {
697 		_map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
698 		he.h_addrtype = AF_INET6;
699 		he.h_length = NS_IN6ADDRSZ;
700 	}
701 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
702 		*errnop = errno;
703 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
704 		*h_errnop = statp->res_h_errno;
705 		return (NS_RETURN);
706 	}
707 	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
708 	*((struct hostent **)rval) = hptr;
709 	return (NS_SUCCESS);
710 }
711 
712 #ifdef RESOLVSORT
713 static void
714 addrsort(char **ap, int num, res_state res)
715 {
716 	int i, j;
717 	char **p;
718 	short aval[_MAXADDRS];
719 	int needsort = 0;
720 
721 	p = ap;
722 	for (i = 0; i < num; i++, p++) {
723 	    for (j = 0 ; (unsigned)j < res->nsort; j++)
724 		if (res->sort_list[j].addr.s_addr ==
725 		    (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
726 			break;
727 	    aval[i] = j;
728 	    if (needsort == 0 && i > 0 && j < aval[i-1])
729 		needsort = i;
730 	}
731 	if (!needsort)
732 	    return;
733 
734 	while (needsort < num) {
735 	    for (j = needsort - 1; j >= 0; j--) {
736 		if (aval[j] > aval[j+1]) {
737 		    char *hp;
738 
739 		    i = aval[j];
740 		    aval[j] = aval[j+1];
741 		    aval[j+1] = i;
742 
743 		    hp = ap[j];
744 		    ap[j] = ap[j+1];
745 		    ap[j+1] = hp;
746 
747 		} else
748 		    break;
749 	    }
750 	    needsort++;
751 	}
752 }
753 #endif
754 
755 void
756 _sethostdnsent(int stayopen)
757 {
758 	res_state statp;
759 
760 	statp = __res_state();
761 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
762 		return;
763 	if (stayopen)
764 		statp->options |= RES_STAYOPEN | RES_USEVC;
765 }
766 
767 void
768 _endhostdnsent(void)
769 {
770 	res_state statp;
771 
772 	statp = __res_state();
773 	statp->options &= ~(RES_STAYOPEN | RES_USEVC);
774 	res_nclose(statp);
775 }
776