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: stable/10/lib/libc/net/gethostbydns.c 211340 2010-08-15 16:28:08Z ume $");
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
dprintf(msg,num,res)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
gethostanswer(const querybuf * answer,int anslen,const char * qname,int qtype,struct hostent * he,struct hostent_data * hed,res_state statp)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 *
__dns_getanswer(const char * answer,int anslen,const char * qname,int qtype)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
_dns_gethostbyname(void * rval,void * cb_data,va_list ap)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 (NS_NOTFOUND);
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 (NS_UNAVAIL);
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 switch (statp->res_h_errno) {
537 case HOST_NOT_FOUND:
538 return (NS_NOTFOUND);
539 case TRY_AGAIN:
540 return (NS_TRYAGAIN);
541 default:
542 return (NS_UNAVAIL);
543 }
544 /*NOTREACHED*/
545 }
546 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
547 *errnop = errno;
548 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
549 *h_errnop = statp->res_h_errno;
550 return (NS_RETURN);
551 }
552 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
553 *((struct hostent **)rval) = hptr;
554 return (NS_SUCCESS);
555 }
556
557 int
_dns_gethostbyaddr(void * rval,void * cb_data,va_list ap)558 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
559 {
560 const void *addr;
561 socklen_t len;
562 int af;
563 char *buffer;
564 size_t buflen;
565 int *errnop, *h_errnop;
566 const u_char *uaddr;
567 struct hostent *hptr, he;
568 struct hostent_data *hed;
569 int n;
570 querybuf *buf;
571 char qbuf[MAXDNAME+1], *qp;
572 res_state statp;
573 #ifdef SUNSECURITY
574 struct hostdata rhd;
575 struct hostent *rhe;
576 char **haddr;
577 u_long old_options;
578 char hname2[MAXDNAME+1], numaddr[46];
579 int ret_h_error;
580 #endif /*SUNSECURITY*/
581
582 addr = va_arg(ap, const void *);
583 len = va_arg(ap, socklen_t);
584 af = va_arg(ap, int);
585 hptr = va_arg(ap, struct hostent *);
586 buffer = va_arg(ap, char *);
587 buflen = va_arg(ap, size_t);
588 errnop = va_arg(ap, int *);
589 h_errnop = va_arg(ap, int *);
590 uaddr = (const u_char *)addr;
591
592 *((struct hostent **)rval) = NULL;
593
594 statp = __res_state();
595 if ((hed = __hostent_data_init()) == NULL) {
596 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
597 *h_errnop = statp->res_h_errno;
598 return (NS_NOTFOUND);
599 }
600
601 switch (af) {
602 case AF_INET:
603 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
604 (uaddr[3] & 0xff),
605 (uaddr[2] & 0xff),
606 (uaddr[1] & 0xff),
607 (uaddr[0] & 0xff));
608 break;
609 case AF_INET6:
610 qp = qbuf;
611 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
612 qp += SPRINTF((qp, "%x.%x.",
613 uaddr[n] & 0xf,
614 (uaddr[n] >> 4) & 0xf));
615 }
616 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
617 break;
618 default:
619 abort();
620 }
621 if ((buf = malloc(sizeof(*buf))) == NULL) {
622 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
623 *h_errnop = statp->res_h_errno;
624 return NS_NOTFOUND;
625 }
626 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
627 sizeof buf->buf);
628 if (n < 0) {
629 free(buf);
630 dprintf("res_nquery failed (%d)\n", n, statp);
631 *h_errnop = statp->res_h_errno;
632 return (NS_UNAVAIL);
633 }
634 if (n > sizeof buf->buf) {
635 free(buf);
636 dprintf("static buffer is too small (%d)\n", n, statp);
637 *h_errnop = statp->res_h_errno;
638 return (NS_UNAVAIL);
639 }
640 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
641 free(buf);
642 *h_errnop = statp->res_h_errno;
643 switch (statp->res_h_errno) {
644 case HOST_NOT_FOUND:
645 return (NS_NOTFOUND);
646 case TRY_AGAIN:
647 return (NS_TRYAGAIN);
648 default:
649 return (NS_UNAVAIL);
650 }
651 /*NOTREACHED*/
652 }
653 free(buf);
654 #ifdef SUNSECURITY
655 if (af == AF_INET) {
656 /*
657 * turn off search as the name should be absolute,
658 * 'localhost' should be matched by defnames
659 */
660 strncpy(hname2, he.h_name, MAXDNAME);
661 hname2[MAXDNAME] = '\0';
662 old_options = statp->options;
663 statp->options &= ~RES_DNSRCH;
664 statp->options |= RES_DEFNAMES;
665 memset(&rhd, 0, sizeof rhd);
666 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
667 sizeof(rhd.data), &ret_h_error);
668 if (rhe == NULL) {
669 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
670 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
671 syslog(LOG_NOTICE|LOG_AUTH,
672 "gethostbyaddr: No A record for %s (verifying [%s])",
673 hname2, numaddr);
674 statp->options = old_options;
675 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
676 *h_errnop = statp->res_h_errno;
677 return (NS_NOTFOUND);
678 }
679 statp->options = old_options;
680 for (haddr = rhe->h_addr_list; *haddr; haddr++)
681 if (!memcmp(*haddr, addr, NS_INADDRSZ))
682 break;
683 if (!*haddr) {
684 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
685 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
686 syslog(LOG_NOTICE|LOG_AUTH,
687 "gethostbyaddr: A record of %s != PTR record [%s]",
688 hname2, numaddr);
689 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
690 *h_errnop = statp->res_h_errno;
691 return (NS_NOTFOUND);
692 }
693 }
694 #endif /*SUNSECURITY*/
695 he.h_addrtype = af;
696 he.h_length = len;
697 memcpy(hed->host_addr, uaddr, len);
698 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
699 hed->h_addr_ptrs[1] = NULL;
700 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
701 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
702 he.h_addrtype = AF_INET6;
703 he.h_length = NS_IN6ADDRSZ;
704 }
705 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
706 *errnop = errno;
707 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
708 *h_errnop = statp->res_h_errno;
709 return (NS_RETURN);
710 }
711 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
712 *((struct hostent **)rval) = hptr;
713 return (NS_SUCCESS);
714 }
715
716 #ifdef RESOLVSORT
717 static void
addrsort(char ** ap,int num,res_state res)718 addrsort(char **ap, int num, res_state res)
719 {
720 int i, j;
721 char **p;
722 short aval[_MAXADDRS];
723 int needsort = 0;
724
725 p = ap;
726 for (i = 0; i < num; i++, p++) {
727 for (j = 0 ; (unsigned)j < res->nsort; j++)
728 if (res->sort_list[j].addr.s_addr ==
729 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
730 break;
731 aval[i] = j;
732 if (needsort == 0 && i > 0 && j < aval[i-1])
733 needsort = i;
734 }
735 if (!needsort)
736 return;
737
738 while (needsort < num) {
739 for (j = needsort - 1; j >= 0; j--) {
740 if (aval[j] > aval[j+1]) {
741 char *hp;
742
743 i = aval[j];
744 aval[j] = aval[j+1];
745 aval[j+1] = i;
746
747 hp = ap[j];
748 ap[j] = ap[j+1];
749 ap[j+1] = hp;
750
751 } else
752 break;
753 }
754 needsort++;
755 }
756 }
757 #endif
758
759 void
_sethostdnsent(int stayopen)760 _sethostdnsent(int stayopen)
761 {
762 res_state statp;
763
764 statp = __res_state();
765 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
766 return;
767 if (stayopen)
768 statp->options |= RES_STAYOPEN | RES_USEVC;
769 }
770
771 void
_endhostdnsent()772 _endhostdnsent()
773 {
774 res_state statp;
775
776 statp = __res_state();
777 statp->options &= ~(RES_STAYOPEN | RES_USEVC);
778 res_nclose(statp);
779 }
780