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