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