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