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