gethnamaddr.c revision 1.91 1 /* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
2
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/cdefs.h>
55 #if defined(LIBC_SCCS) && !defined(lint)
56 #if 0
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
59 #else
60 __RCSID("$NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $");
61 #endif
62 #endif /* LIBC_SCCS and not lint */
63
64 #if defined(_LIBC)
65 #include "namespace.h"
66 #endif
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <arpa/nameser.h>
72
73 #include <assert.h>
74 #include <ctype.h>
75 #include <errno.h>
76 #include <netdb.h>
77 #include <resolv.h>
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <syslog.h>
81
82 #ifndef LOG_AUTH
83 # define LOG_AUTH 0
84 #endif
85
86 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
87
88 #include <nsswitch.h>
89 #include <stdlib.h>
90 #include <string.h>
91
92 #ifdef YP
93 #include <rpc/rpc.h>
94 #include <rpcsvc/yp_prot.h>
95 #include <rpcsvc/ypclnt.h>
96 #endif
97
98 #include "hostent.h"
99
100 #if defined(_LIBC) && defined(__weak_alias)
101 __weak_alias(gethostbyaddr,_gethostbyaddr)
102 __weak_alias(gethostbyname,_gethostbyname)
103 __weak_alias(gethostent,_gethostent)
104 #endif
105
106 #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
107 (ok)(nm) != 0)
108 #define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
109 #define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
110
111 #define addalias(d, s, arr, siz) do { \
112 if (d >= &arr[siz]) { \
113 char **xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \
114 if (xptr == NULL) \
115 goto nospc; \
116 d = xptr + (d - arr); \
117 arr = xptr; \
118 siz += 10; \
119 } \
120 *d++ = s; \
121 } while (/*CONSTCOND*/0)
122
123 #define setup(arr, siz) do { \
124 arr = malloc((siz = 10) * sizeof(*arr)); \
125 if (arr == NULL) \
126 goto nospc; \
127 } while (/*CONSTCOND*/0)
128
129
130 static const char AskedForGot[] =
131 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
132
133
134 #ifdef YP
135 static char *__ypdomain;
136 #endif
137
138 #define MAXPACKET (64*1024)
139
140 typedef union {
141 HEADER hdr;
142 u_char buf[MAXPACKET];
143 } querybuf;
144
145 typedef union {
146 int32_t al;
147 char ac;
148 } align;
149
150 #ifdef DEBUG
151 static void debugprintf(const char *, res_state, ...)
152 __attribute__((__format__(__printf__, 1, 3)));
153 #endif
154 static struct hostent *getanswer(const querybuf *, int, const char *, int,
155 res_state, struct hostent *, char *, size_t, int *);
156 static void map_v4v6_address(const char *, char *);
157 static void map_v4v6_hostent(struct hostent *, char **, char *);
158 static void addrsort(char **, int, res_state);
159
160 void dns_service(void);
161 #undef dn_skipname
162 int dn_skipname(const u_char *, const u_char *);
163
164 #ifdef YP
165 static struct hostent *_yp_hostent(char *, int, struct getnamaddr *);
166 #endif
167
168 static struct hostent *gethostbyname_internal(const char *, int, res_state,
169 struct hostent *, char *, size_t, int *);
170
171 static const ns_src default_dns_files[] = {
172 { NSSRC_FILES, NS_SUCCESS },
173 { NSSRC_DNS, NS_SUCCESS },
174 { 0, 0 }
175 };
176
177
178 #ifdef DEBUG
179 static void
180 debugprintf(const char *msg, res_state res, ...)
181 {
182 _DIAGASSERT(msg != NULL);
183
184 if (res->options & RES_DEBUG) {
185 int save = errno;
186 va_list ap;
187
188 va_start (ap, res);
189 vprintf(msg, ap);
190 va_end (ap);
191
192 errno = save;
193 }
194 }
195 #else
196 # define debugprintf(msg, res, num) /*nada*/
197 #endif
198
199 #define BOUNDED_INCR(x) \
200 do { \
201 cp += (x); \
202 if (cp > eom) \
203 goto no_recovery; \
204 } while (/*CONSTCOND*/0)
205
206 #define BOUNDS_CHECK(ptr, count) \
207 do { \
208 if ((ptr) + (count) > eom) \
209 goto no_recovery; \
210 } while (/*CONSTCOND*/0)
211
212 static struct hostent *
213 getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
214 res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
215 {
216 const HEADER *hp;
217 const u_char *cp;
218 int n;
219 size_t qlen;
220 const u_char *eom, *erdata;
221 char *bp, **ap, **hap, *ep;
222 int type, class, ancount, qdcount;
223 int haveanswer, had_error;
224 int toobig = 0;
225 char tbuf[MAXDNAME];
226 char **aliases;
227 size_t maxaliases;
228 char *addr_ptrs[MAXADDRS];
229 const char *tname;
230 int (*name_ok)(const char *);
231
232 _DIAGASSERT(answer != NULL);
233 _DIAGASSERT(qname != NULL);
234
235 tname = qname;
236 hent->h_name = NULL;
237 eom = answer->buf + anslen;
238 switch (qtype) {
239 case T_A:
240 case T_AAAA:
241 name_ok = res_hnok;
242 break;
243 case T_PTR:
244 name_ok = res_dnok;
245 break;
246 default:
247 return NULL; /* XXX should be abort(); */
248 }
249
250 setup(aliases, maxaliases);
251 /*
252 * find first satisfactory answer
253 */
254 hp = &answer->hdr;
255 ancount = ntohs(hp->ancount);
256 qdcount = ntohs(hp->qdcount);
257 bp = buf;
258 ep = buf + buflen;
259 cp = answer->buf;
260 BOUNDED_INCR(HFIXEDSZ);
261 if (qdcount != 1)
262 goto no_recovery;
263
264 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
265 if ((n < 0) || !maybe_ok(res, bp, name_ok))
266 goto no_recovery;
267
268 BOUNDED_INCR(n + QFIXEDSZ);
269 if (qtype == T_A || qtype == T_AAAA) {
270 /* res_send() has already verified that the query name is the
271 * same as the one we sent; this just gets the expanded name
272 * (i.e., with the succeeding search-domain tacked on).
273 */
274 n = (int)strlen(bp) + 1; /* for the \0 */
275 if (n >= MAXHOSTNAMELEN)
276 goto no_recovery;
277 hent->h_name = bp;
278 bp += n;
279 /* The qname can be abbreviated, but h_name is now absolute. */
280 qname = hent->h_name;
281 }
282 hent->h_aliases = ap = aliases;
283 hent->h_addr_list = hap = addr_ptrs;
284 *ap = NULL;
285 *hap = NULL;
286 haveanswer = 0;
287 had_error = 0;
288 while (ancount-- > 0 && cp < eom && !had_error) {
289 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
290 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
291 had_error++;
292 continue;
293 }
294 cp += n; /* name */
295 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
296 type = _getshort(cp);
297 cp += INT16SZ; /* type */
298 class = _getshort(cp);
299 cp += INT16SZ + INT32SZ; /* class, TTL */
300 n = _getshort(cp);
301 cp += INT16SZ; /* len */
302 BOUNDS_CHECK(cp, n);
303 erdata = cp + n;
304 if (class != C_IN) {
305 /* XXX - debug? syslog? */
306 cp += n;
307 continue; /* XXX - had_error++ ? */
308 }
309 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
310 n = dn_expand(answer->buf, eom, cp, tbuf,
311 (int)sizeof tbuf);
312 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
313 had_error++;
314 continue;
315 }
316 cp += n;
317 if (cp != erdata)
318 goto no_recovery;
319 /* Store alias. */
320 addalias(ap, bp, aliases, maxaliases);
321 n = (int)strlen(bp) + 1; /* for the \0 */
322 if (n >= MAXHOSTNAMELEN) {
323 had_error++;
324 continue;
325 }
326 bp += n;
327 /* Get canonical name. */
328 n = (int)strlen(tbuf) + 1; /* for the \0 */
329 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
330 had_error++;
331 continue;
332 }
333 strlcpy(bp, tbuf, (size_t)(ep - bp));
334 hent->h_name = bp;
335 bp += n;
336 continue;
337 }
338 if (qtype == T_PTR && type == T_CNAME) {
339 n = dn_expand(answer->buf, eom, cp, tbuf,
340 (int)sizeof tbuf);
341 if (n < 0 || !maybe_dnok(res, tbuf)) {
342 had_error++;
343 continue;
344 }
345 cp += n;
346 if (cp != erdata)
347 goto no_recovery;
348 /* Get canonical name. */
349 n = (int)strlen(tbuf) + 1; /* for the \0 */
350 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
351 had_error++;
352 continue;
353 }
354 strlcpy(bp, tbuf, (size_t)(ep - bp));
355 tname = bp;
356 bp += n;
357 continue;
358 }
359 if (type != qtype) {
360 if (type != T_KEY && type != T_SIG)
361 syslog(LOG_NOTICE|LOG_AUTH,
362 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
363 qname, p_class(C_IN), p_type(qtype),
364 p_type(type));
365 cp += n;
366 continue; /* XXX - had_error++ ? */
367 }
368 switch (type) {
369 case T_PTR:
370 if (strcasecmp(tname, bp) != 0) {
371 syslog(LOG_NOTICE|LOG_AUTH,
372 AskedForGot, qname, bp);
373 cp += n;
374 continue; /* XXX - had_error++ ? */
375 }
376 n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
377 if ((n < 0) || !maybe_hnok(res, bp)) {
378 had_error++;
379 break;
380 }
381 #if MULTI_PTRS_ARE_ALIASES
382 cp += n;
383 if (cp != erdata)
384 goto no_recovery;
385 if (!haveanswer)
386 hent->h_name = bp;
387 else
388 addalias(ap, bp, aliases, maxaliases);
389 if (n != -1) {
390 n = (int)strlen(bp) + 1; /* for the \0 */
391 if (n >= MAXHOSTNAMELEN) {
392 had_error++;
393 break;
394 }
395 bp += n;
396 }
397 break;
398 #else
399 hent->h_name = bp;
400 if (res->options & RES_USE_INET6) {
401 n = strlen(bp) + 1; /* for the \0 */
402 if (n >= MAXHOSTNAMELEN) {
403 had_error++;
404 break;
405 }
406 bp += n;
407 map_v4v6_hostent(hent, &bp, ep);
408 }
409 goto success;
410 #endif
411 case T_A:
412 case T_AAAA:
413 if (strcasecmp(hent->h_name, bp) != 0) {
414 syslog(LOG_NOTICE|LOG_AUTH,
415 AskedForGot, hent->h_name, bp);
416 cp += n;
417 continue; /* XXX - had_error++ ? */
418 }
419 if (n != hent->h_length) {
420 cp += n;
421 continue;
422 }
423 if (type == T_AAAA) {
424 struct in6_addr in6;
425 memcpy(&in6, cp, NS_IN6ADDRSZ);
426 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
427 cp += n;
428 continue;
429 }
430 }
431 if (!haveanswer) {
432 int nn;
433
434 hent->h_name = bp;
435 nn = (int)strlen(bp) + 1; /* for the \0 */
436 bp += nn;
437 }
438
439 bp += sizeof(align) -
440 (size_t)((u_long)bp % sizeof(align));
441
442 if (bp + n >= ep) {
443 debugprintf("size (%d) too big\n", res, n);
444 had_error++;
445 continue;
446 }
447 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
448 if (!toobig++) {
449 debugprintf("Too many addresses (%d)\n",
450 res, MAXADDRS);
451 }
452 cp += n;
453 continue;
454 }
455 (void)memcpy(*hap++ = bp, cp, (size_t)n);
456 bp += n;
457 cp += n;
458 if (cp != erdata)
459 goto no_recovery;
460 break;
461 default:
462 abort();
463 }
464 if (!had_error)
465 haveanswer++;
466 }
467 if (haveanswer) {
468 *ap = NULL;
469 *hap = NULL;
470 /*
471 * Note: we sort even if host can take only one address
472 * in its return structures - should give it the "best"
473 * address in that case, not some random one
474 */
475 if (res->nsort && haveanswer > 1 && qtype == T_A)
476 addrsort(addr_ptrs, haveanswer, res);
477 if (!hent->h_name) {
478 n = (int)strlen(qname) + 1; /* for the \0 */
479 if (n > ep - bp || n >= MAXHOSTNAMELEN)
480 goto no_recovery;
481 strlcpy(bp, qname, (size_t)(ep - bp));
482 hent->h_name = bp;
483 bp += n;
484 }
485 if (res->options & RES_USE_INET6)
486 map_v4v6_hostent(hent, &bp, ep);
487 goto success;
488 }
489 no_recovery:
490 free(aliases);
491 *he = NO_RECOVERY;
492 return NULL;
493 success:
494 bp = (char *)ALIGN(bp);
495 n = (int)(ap - aliases);
496 qlen = (n + 1) * sizeof(*hent->h_aliases);
497 if ((size_t)(ep - bp) < qlen)
498 goto nospc;
499 hent->h_aliases = (void *)bp;
500 memcpy(bp, aliases, qlen);
501 free(aliases);
502 aliases = NULL;
503
504 bp += qlen;
505 n = (int)(hap - addr_ptrs);
506 qlen = (n + 1) * sizeof(*hent->h_addr_list);
507 if ((size_t)(ep - bp) < qlen)
508 goto nospc;
509 hent->h_addr_list = (void *)bp;
510 memcpy(bp, addr_ptrs, qlen);
511 *he = NETDB_SUCCESS;
512 return hent;
513 nospc:
514 free(aliases);
515 errno = ENOSPC;
516 *he = NETDB_INTERNAL;
517 return NULL;
518 }
519
520 struct hostent *
521 gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
522 int *he)
523 {
524 res_state res = __res_get_state();
525
526 if (res == NULL) {
527 *he = NETDB_INTERNAL;
528 return NULL;
529 }
530
531 _DIAGASSERT(name != NULL);
532
533 if (res->options & RES_USE_INET6) {
534 struct hostent *nhp = gethostbyname_internal(name, AF_INET6,
535 res, hp, buf, buflen, he);
536 if (nhp) {
537 __res_put_state(res);
538 return nhp;
539 }
540 }
541 hp = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, he);
542 __res_put_state(res);
543 return hp;
544 }
545
546 struct hostent *
547 gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf,
548 size_t buflen, int *he)
549 {
550 res_state res = __res_get_state();
551
552 if (res == NULL) {
553 *he = NETDB_INTERNAL;
554 return NULL;
555 }
556 hp = gethostbyname_internal(name, af, res, hp, buf, buflen, he);
557 __res_put_state(res);
558 return hp;
559 }
560
561 static struct hostent *
562 gethostbyname_internal(const char *name, int af, res_state res,
563 struct hostent *hp, char *buf, size_t buflen, int *he)
564 {
565 const char *cp;
566 struct getnamaddr info;
567 char hbuf[MAXHOSTNAMELEN];
568 size_t size;
569 static const ns_dtab dtab[] = {
570 NS_FILES_CB(_hf_gethtbyname, NULL)
571 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
572 NS_NIS_CB(_yp_gethtbyname, NULL)
573 NS_NULL_CB
574 };
575
576 _DIAGASSERT(name != NULL);
577
578 switch (af) {
579 case AF_INET:
580 size = NS_INADDRSZ;
581 break;
582 case AF_INET6:
583 size = NS_IN6ADDRSZ;
584 break;
585 default:
586 *he = NETDB_INTERNAL;
587 errno = EAFNOSUPPORT;
588 return NULL;
589 }
590 if (buflen < size)
591 goto nospc;
592
593 hp->h_addrtype = af;
594 hp->h_length = (int)size;
595
596 /*
597 * if there aren't any dots, it could be a user-level alias.
598 * this is also done in res_nquery() since we are not the only
599 * function that looks up host names.
600 */
601 if (!strchr(name, '.') && (cp = res_hostalias(res, name,
602 hbuf, sizeof(hbuf))))
603 name = cp;
604
605 /*
606 * disallow names consisting only of digits/dots, unless
607 * they end in a dot.
608 */
609 if (isdigit((u_char) name[0]))
610 for (cp = name;; ++cp) {
611 if (!*cp) {
612 if (*--cp == '.')
613 break;
614 /*
615 * All-numeric, no dot at the end.
616 * Fake up a hostent as if we'd actually
617 * done a lookup.
618 */
619 goto fake;
620 }
621 if (!isdigit((u_char) *cp) && *cp != '.')
622 break;
623 }
624 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
625 name[0] == ':')
626 for (cp = name;; ++cp) {
627 if (!*cp) {
628 if (*--cp == '.')
629 break;
630 /*
631 * All-IPv6-legal, no dot at the end.
632 * Fake up a hostent as if we'd actually
633 * done a lookup.
634 */
635 goto fake;
636 }
637 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
638 break;
639 }
640
641 *he = NETDB_INTERNAL;
642 info.hp = hp;
643 info.buf = buf;
644 info.buflen = buflen;
645 info.he = he;
646 if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname",
647 default_dns_files, name, strlen(name), af) != NS_SUCCESS)
648 return NULL;
649 *he = NETDB_SUCCESS;
650 return hp;
651 nospc:
652 *he = NETDB_INTERNAL;
653 errno = ENOSPC;
654 return NULL;
655 fake:
656 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
657 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
658
659 hp->h_aliases[0] = NULL;
660 if (size > buflen)
661 goto nospc;
662
663 if (inet_pton(af, name, buf) <= 0) {
664 *he = HOST_NOT_FOUND;
665 return NULL;
666 }
667 hp->h_addr_list[0] = buf;
668 hp->h_addr_list[1] = NULL;
669 buf += size;
670 buflen -= size;
671 HENT_SCOPY(hp->h_name, name, buf, buflen);
672 if (res->options & RES_USE_INET6)
673 map_v4v6_hostent(hp, &buf, buf + buflen);
674 *he = NETDB_SUCCESS;
675 return hp;
676 }
677
678 struct hostent *
679 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
680 char *buf, size_t buflen, int *he)
681 {
682 const u_char *uaddr = (const u_char *)addr;
683 socklen_t size;
684 struct getnamaddr info;
685 static const ns_dtab dtab[] = {
686 NS_FILES_CB(_hf_gethtbyaddr, NULL)
687 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
688 NS_NIS_CB(_yp_gethtbyaddr, NULL)
689 NS_NULL_CB
690 };
691
692 _DIAGASSERT(addr != NULL);
693
694 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
695 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
696 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
697 *he = HOST_NOT_FOUND;
698 return NULL;
699 }
700 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
701 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
702 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
703 /* Unmap. */
704 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
705 addr = uaddr;
706 af = AF_INET;
707 len = NS_INADDRSZ;
708 }
709 switch (af) {
710 case AF_INET:
711 size = NS_INADDRSZ;
712 break;
713 case AF_INET6:
714 size = NS_IN6ADDRSZ;
715 break;
716 default:
717 errno = EAFNOSUPPORT;
718 *he = NETDB_INTERNAL;
719 return NULL;
720 }
721 if (size != len) {
722 errno = EINVAL;
723 *he = NETDB_INTERNAL;
724 return NULL;
725 }
726 info.hp = hp;
727 info.buf = buf;
728 info.buflen = buflen;
729 info.he = he;
730 *he = NETDB_INTERNAL;
731 if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr",
732 default_dns_files, uaddr, len, af) != NS_SUCCESS)
733 return NULL;
734 *he = NETDB_SUCCESS;
735 return hp;
736 }
737
738 struct hostent *
739 gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
740 {
741 char *p, *name;
742 char *cp, **q;
743 int af, len;
744 size_t anum;
745 char **aliases;
746 size_t maxaliases;
747 struct in6_addr host_addr;
748
749 if (hf == NULL) {
750 *he = NETDB_INTERNAL;
751 errno = EINVAL;
752 return NULL;
753 }
754 p = NULL;
755 setup(aliases, maxaliases);
756 for (;;) {
757 free(p);
758 p = fparseln(hf, NULL, NULL, NULL, FPARSELN_UNESCALL);
759 if (p == NULL) {
760 free(aliases);
761 *he = HOST_NOT_FOUND;
762 return NULL;
763 }
764 if (!(cp = strpbrk(p, " \t")))
765 continue;
766 *cp++ = '\0';
767 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
768 af = AF_INET6;
769 len = NS_IN6ADDRSZ;
770 } else {
771 if (inet_pton(AF_INET, p, &host_addr) <= 0)
772 continue;
773
774 res_state res = __res_get_state();
775 if (res == NULL)
776 goto nospc;
777 if (res->options & RES_USE_INET6) {
778 map_v4v6_address(buf, buf);
779 af = AF_INET6;
780 len = NS_IN6ADDRSZ;
781 } else {
782 af = AF_INET;
783 len = NS_INADDRSZ;
784 }
785 __res_put_state(res);
786 }
787
788 /* if this is not something we're looking for, skip it. */
789 if (hent->h_addrtype != 0 && hent->h_addrtype != af)
790 continue;
791 if (hent->h_length != 0 && hent->h_length != len)
792 continue;
793
794 while (*cp == ' ' || *cp == '\t')
795 cp++;
796 if ((cp = strpbrk(name = cp, " \t")) != NULL)
797 *cp++ = '\0';
798 q = aliases;
799 while (cp && *cp) {
800 if (*cp == ' ' || *cp == '\t') {
801 cp++;
802 continue;
803 }
804 addalias(q, cp, aliases, maxaliases);
805 if ((cp = strpbrk(cp, " \t")) != NULL)
806 *cp++ = '\0';
807 }
808 break;
809 }
810 hent->h_length = len;
811 hent->h_addrtype = af;
812 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
813 anum = (size_t)(q - aliases);
814 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
815 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf,
816 buflen);
817 hent->h_addr_list[1] = NULL;
818
819 HENT_SCOPY(hent->h_name, name, buf, buflen);
820 for (size_t i = 0; i < anum; i++)
821 HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
822 hent->h_aliases[anum] = NULL;
823
824 *he = NETDB_SUCCESS;
825 free(p);
826 free(aliases);
827 return hent;
828 nospc:
829 free(p);
830 free(aliases);
831 errno = ENOSPC;
832 *he = NETDB_INTERNAL;
833 return NULL;
834 }
835
836 static void
837 map_v4v6_address(const char *src, char *dst)
838 {
839 u_char *p = (u_char *)dst;
840 char tmp[NS_INADDRSZ];
841 int i;
842
843 _DIAGASSERT(src != NULL);
844 _DIAGASSERT(dst != NULL);
845
846 /* Stash a temporary copy so our caller can update in place. */
847 (void)memcpy(tmp, src, NS_INADDRSZ);
848 /* Mark this ipv6 addr as a mapped ipv4. */
849 for (i = 0; i < 10; i++)
850 *p++ = 0x00;
851 *p++ = 0xff;
852 *p++ = 0xff;
853 /* Retrieve the saved copy and we're done. */
854 (void)memcpy(p, tmp, NS_INADDRSZ);
855 }
856
857 static void
858 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
859 {
860 char **ap;
861
862 _DIAGASSERT(hp != NULL);
863 _DIAGASSERT(bpp != NULL);
864 _DIAGASSERT(ep != NULL);
865
866 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
867 return;
868 hp->h_addrtype = AF_INET6;
869 hp->h_length = NS_IN6ADDRSZ;
870 for (ap = hp->h_addr_list; *ap; ap++) {
871 int i = (int)(sizeof(align) -
872 (size_t)((u_long)*bpp % sizeof(align)));
873
874 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
875 /* Out of memory. Truncate address list here. XXX */
876 *ap = NULL;
877 return;
878 }
879 *bpp += i;
880 map_v4v6_address(*ap, *bpp);
881 *ap = *bpp;
882 *bpp += NS_IN6ADDRSZ;
883 }
884 }
885
886 static void
887 addrsort(char **ap, int num, res_state res)
888 {
889 int i, j;
890 char **p;
891 short aval[MAXADDRS];
892 int needsort = 0;
893
894 _DIAGASSERT(ap != NULL);
895
896 p = ap;
897 for (i = 0; i < num; i++, p++) {
898 for (j = 0 ; (unsigned)j < res->nsort; j++)
899 if (res->sort_list[j].addr.s_addr ==
900 (((struct in_addr *)(void *)(*p))->s_addr &
901 res->sort_list[j].mask))
902 break;
903 aval[i] = j;
904 if (needsort == 0 && i > 0 && j < aval[i-1])
905 needsort = i;
906 }
907 if (!needsort)
908 return;
909
910 while (needsort < num) {
911 for (j = needsort - 1; j >= 0; j--) {
912 if (aval[j] > aval[j+1]) {
913 char *hp;
914
915 i = aval[j];
916 aval[j] = aval[j+1];
917 aval[j+1] = i;
918
919 hp = ap[j];
920 ap[j] = ap[j+1];
921 ap[j+1] = hp;
922 } else
923 break;
924 }
925 needsort++;
926 }
927 }
928
929
930 /*ARGSUSED*/
931 int
932 _dns_gethtbyname(void *rv, void *cb_data, va_list ap)
933 {
934 querybuf *buf;
935 int n, type;
936 struct hostent *hp;
937 const char *name;
938 res_state res;
939 struct getnamaddr *info = rv;
940
941 _DIAGASSERT(rv != NULL);
942
943 name = va_arg(ap, char *);
944 /* NOSTRICT skip string len */(void)va_arg(ap, int);
945 info->hp->h_addrtype = va_arg(ap, int);
946
947 switch (info->hp->h_addrtype) {
948 case AF_INET:
949 info->hp->h_length = NS_INADDRSZ;
950 type = T_A;
951 break;
952 case AF_INET6:
953 info->hp->h_length = NS_IN6ADDRSZ;
954 type = T_AAAA;
955 break;
956 default:
957 return NS_UNAVAIL;
958 }
959 buf = malloc(sizeof(*buf));
960 if (buf == NULL) {
961 *info->he = NETDB_INTERNAL;
962 return NS_NOTFOUND;
963 }
964 res = __res_get_state();
965 if (res == NULL) {
966 free(buf);
967 return NS_NOTFOUND;
968 }
969 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
970 if (n < 0) {
971 free(buf);
972 debugprintf("res_nsearch failed (%d)\n", res, n);
973 __res_put_state(res);
974 return NS_NOTFOUND;
975 }
976 hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
977 info->buflen, info->he);
978 free(buf);
979 __res_put_state(res);
980 if (hp == NULL)
981 switch (h_errno) {
982 case HOST_NOT_FOUND:
983 return NS_NOTFOUND;
984 case TRY_AGAIN:
985 return NS_TRYAGAIN;
986 default:
987 return NS_UNAVAIL;
988 }
989 return NS_SUCCESS;
990 }
991
992 /*ARGSUSED*/
993 int
994 _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
995 {
996 char qbuf[MAXDNAME + 1], *qp, *ep;
997 int n;
998 querybuf *buf;
999 struct hostent *hp;
1000 const unsigned char *uaddr;
1001 int advance;
1002 res_state res;
1003 char *bf;
1004 size_t blen;
1005 struct getnamaddr *info = rv;
1006
1007 _DIAGASSERT(rv != NULL);
1008
1009 uaddr = va_arg(ap, unsigned char *);
1010 info->hp->h_length = va_arg(ap, int);
1011 info->hp->h_addrtype = va_arg(ap, int);
1012
1013 switch (info->hp->h_addrtype) {
1014 case AF_INET:
1015 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1016 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1017 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1018 break;
1019
1020 case AF_INET6:
1021 qp = qbuf;
1022 ep = qbuf + sizeof(qbuf) - 1;
1023 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
1024 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1025 uaddr[n] & 0xf,
1026 ((unsigned int)uaddr[n] >> 4) & 0xf);
1027 if (advance > 0 && qp + advance < ep)
1028 qp += advance;
1029 else {
1030 *info->he = NETDB_INTERNAL;
1031 return NS_NOTFOUND;
1032 }
1033 }
1034 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1035 *info->he = NETDB_INTERNAL;
1036 return NS_NOTFOUND;
1037 }
1038 break;
1039 default:
1040 return NS_UNAVAIL;
1041 }
1042
1043 buf = malloc(sizeof(*buf));
1044 if (buf == NULL) {
1045 *info->he = NETDB_INTERNAL;
1046 return NS_NOTFOUND;
1047 }
1048 res = __res_get_state();
1049 if (res == NULL) {
1050 free(buf);
1051 return NS_NOTFOUND;
1052 }
1053 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
1054 if (n < 0) {
1055 free(buf);
1056 debugprintf("res_nquery failed (%d)\n", res, n);
1057 __res_put_state(res);
1058 return NS_NOTFOUND;
1059 }
1060 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
1061 info->buflen, info->he);
1062 free(buf);
1063 if (hp == NULL) {
1064 __res_put_state(res);
1065 switch (*info->he) {
1066 case HOST_NOT_FOUND:
1067 return NS_NOTFOUND;
1068 case TRY_AGAIN:
1069 return NS_TRYAGAIN;
1070 default:
1071 return NS_UNAVAIL;
1072 }
1073 }
1074
1075 bf = (void *)(hp->h_addr_list + 2);
1076 blen = (size_t)(bf - info->buf);
1077 if (blen + info->hp->h_length > info->buflen)
1078 goto nospc;
1079 hp->h_addr_list[0] = bf;
1080 hp->h_addr_list[1] = NULL;
1081 (void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
1082 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
1083 if (blen + NS_IN6ADDRSZ > info->buflen)
1084 goto nospc;
1085 map_v4v6_address(bf, bf);
1086 hp->h_addrtype = AF_INET6;
1087 hp->h_length = NS_IN6ADDRSZ;
1088 }
1089
1090 __res_put_state(res);
1091 *info->he = NETDB_SUCCESS;
1092 return NS_SUCCESS;
1093 nospc:
1094 *info->he = NETDB_INTERNAL;
1095 return NS_UNAVAIL;
1096 }
1097
1098 #ifdef YP
1099 /*ARGSUSED*/
1100 static struct hostent *
1101 _yp_hostent(char *line, int af, struct getnamaddr *info)
1102 {
1103 struct in6_addr host_addrs[MAXADDRS];
1104 char **aliases;
1105 size_t maxaliases;
1106 char *p = line;
1107 char *cp, **q, *ptr;
1108 size_t len, anum, i;
1109 int addrok;
1110 int more;
1111 size_t naddrs;
1112 struct hostent *hp = info->hp;
1113
1114 _DIAGASSERT(line != NULL);
1115
1116 hp->h_name = NULL;
1117 hp->h_addrtype = af;
1118 switch (af) {
1119 case AF_INET:
1120 hp->h_length = NS_INADDRSZ;
1121 break;
1122 case AF_INET6:
1123 hp->h_length = NS_IN6ADDRSZ;
1124 break;
1125 default:
1126 return NULL;
1127 }
1128 setup(aliases, maxaliases);
1129 naddrs = 0;
1130 q = aliases;
1131
1132 nextline:
1133 /* check for host_addrs overflow */
1134 if (naddrs >= __arraycount(host_addrs))
1135 goto done;
1136
1137 more = 0;
1138 cp = strpbrk(p, " \t");
1139 if (cp == NULL)
1140 goto done;
1141 *cp++ = '\0';
1142
1143 /* p has should have an address */
1144 addrok = inet_pton(af, p, &host_addrs[naddrs]);
1145 if (addrok != 1) {
1146 /* skip to the next line */
1147 while (cp && *cp) {
1148 if (*cp == '\n') {
1149 cp++;
1150 goto nextline;
1151 }
1152 cp++;
1153 }
1154 goto done;
1155 }
1156 naddrs++;
1157
1158 while (*cp == ' ' || *cp == '\t')
1159 cp++;
1160 p = cp;
1161 cp = strpbrk(p, " \t\n");
1162 if (cp != NULL) {
1163 if (*cp == '\n')
1164 more = 1;
1165 *cp++ = '\0';
1166 }
1167 if (!hp->h_name)
1168 hp->h_name = p;
1169 else if (strcmp(hp->h_name, p) == 0)
1170 ;
1171 else
1172 addalias(q, p, aliases, maxaliases);
1173 p = cp;
1174 if (more)
1175 goto nextline;
1176
1177 while (cp && *cp) {
1178 if (*cp == ' ' || *cp == '\t') {
1179 cp++;
1180 continue;
1181 }
1182 if (*cp == '\n') {
1183 cp++;
1184 goto nextline;
1185 }
1186 addalias(q, cp, aliases, maxaliases);
1187 cp = strpbrk(cp, " \t");
1188 if (cp != NULL)
1189 *cp++ = '\0';
1190 }
1191
1192 done:
1193 if (hp->h_name == NULL) {
1194 free(aliases);
1195 return NULL;
1196 }
1197
1198 ptr = info->buf;
1199 len = info->buflen;
1200
1201 anum = (size_t)(q - aliases);
1202 HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
1203 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
1204
1205 for (i = 0; i < naddrs; i++)
1206 HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
1207 ptr, len);
1208 hp->h_addr_list[naddrs] = NULL;
1209
1210 HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
1211
1212 for (i = 0; i < anum; i++)
1213 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
1214 hp->h_aliases[anum] = NULL;
1215 free(aliases);
1216
1217 return hp;
1218 nospc:
1219 free(aliases);
1220 *info->he = NETDB_INTERNAL;
1221 errno = ENOSPC;
1222 return NULL;
1223 }
1224
1225 /*ARGSUSED*/
1226 int
1227 _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
1228 {
1229 struct hostent *hp = NULL;
1230 char *ypcurrent;
1231 int ypcurrentlen, r;
1232 char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1233 const unsigned char *uaddr;
1234 int af;
1235 const char *map;
1236 struct getnamaddr *info = rv;
1237
1238 _DIAGASSERT(rv != NULL);
1239
1240 uaddr = va_arg(ap, unsigned char *);
1241 /* NOSTRICT skip len */(void)va_arg(ap, int);
1242 af = va_arg(ap, int);
1243
1244 if (!__ypdomain) {
1245 if (_yp_check(&__ypdomain) == 0)
1246 return NS_UNAVAIL;
1247 }
1248 /*
1249 * XXX unfortunately, we cannot support IPv6 extended scoped address
1250 * notation here. gethostbyaddr() is not scope-aware. too bad.
1251 */
1252 if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
1253 return NS_UNAVAIL;
1254 switch (af) {
1255 case AF_INET:
1256 map = "hosts.byaddr";
1257 break;
1258 default:
1259 map = "ipnodes.byaddr";
1260 break;
1261 }
1262 ypcurrent = NULL;
1263 r = yp_match(__ypdomain, map, name,
1264 (int)strlen(name), &ypcurrent, &ypcurrentlen);
1265 if (r == 0)
1266 hp = _yp_hostent(ypcurrent, af, info);
1267 else
1268 hp = NULL;
1269 free(ypcurrent);
1270 if (hp == NULL) {
1271 *info->he = HOST_NOT_FOUND;
1272 return NS_NOTFOUND;
1273 }
1274 return NS_SUCCESS;
1275 }
1276
1277 /*ARGSUSED*/
1278 int
1279 _yp_gethtbyname(void *rv, void *cb_data, va_list ap)
1280 {
1281 struct hostent *hp;
1282 char *ypcurrent;
1283 int ypcurrentlen, r;
1284 const char *name;
1285 int af;
1286 const char *map;
1287 struct getnamaddr *info = rv;
1288
1289 _DIAGASSERT(rv != NULL);
1290
1291 name = va_arg(ap, char *);
1292 /* NOSTRICT skip string len */(void)va_arg(ap, int);
1293 af = va_arg(ap, int);
1294
1295 if (!__ypdomain) {
1296 if (_yp_check(&__ypdomain) == 0)
1297 return NS_UNAVAIL;
1298 }
1299 switch (af) {
1300 case AF_INET:
1301 map = "hosts.byname";
1302 break;
1303 default:
1304 map = "ipnodes.byname";
1305 break;
1306 }
1307 ypcurrent = NULL;
1308 r = yp_match(__ypdomain, map, name,
1309 (int)strlen(name), &ypcurrent, &ypcurrentlen);
1310 if (r == 0)
1311 hp = _yp_hostent(ypcurrent, af, info);
1312 else
1313 hp = NULL;
1314 free(ypcurrent);
1315 if (hp == NULL) {
1316 *info->he = HOST_NOT_FOUND;
1317 return NS_NOTFOUND;
1318 }
1319 return NS_SUCCESS;
1320 }
1321 #endif
1322
1323 /*
1324 * Non-reentrant versions.
1325 */
1326 FILE *_h_file;
1327 static struct hostent h_ent;
1328 static char h_buf[16384];
1329
1330 struct hostent *
1331 gethostbyaddr(const void *addr, socklen_t len, int af) {
1332 return gethostbyaddr_r(addr, len, af, &h_ent, h_buf, sizeof(h_buf),
1333 &h_errno);
1334 }
1335
1336 struct hostent *
1337 gethostbyname(const char *name) {
1338 return gethostbyname_r(name, &h_ent, h_buf, sizeof(h_buf), &h_errno);
1339 }
1340
1341 struct hostent *
1342 gethostbyname2(const char *name, int af) {
1343 return gethostbyname2_r(name, af, &h_ent, h_buf, sizeof(h_buf),
1344 &h_errno);
1345 }
1346
1347 struct hostent *
1348 gethostent(void)
1349 {
1350 if (_h_file == NULL) {
1351 sethostent_r(&_h_file);
1352 if (_h_file == NULL) {
1353 h_errno = NETDB_INTERNAL;
1354 return NULL;
1355 }
1356 }
1357 memset(&h_ent, 0, sizeof(h_ent));
1358 return gethostent_r(_h_file, &h_ent, h_buf, sizeof(h_buf), &h_errno);
1359 }
1360
1361