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