1 /* $NetBSD: gethnamaddr.c,v 1.78 2012/03/13 21:13:40 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.78 2012/03/13 21:13:40 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 debugprintf(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, 0 } 172 }; 173 174 175 #ifdef DEBUG 176 static void 177 debugprintf(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 debugprintf(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, (int)(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 = (int)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, (int)(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, (int)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 = (int)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 = (int)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, (int)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 = (int)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, (int)(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 = (int)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 = (int)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 debugprintf("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 debugprintf("Too many addresses (%d)\n", 460 res, MAXADDRS); 461 } 462 cp += n; 463 continue; 464 } 465 (void)memcpy(*hap++ = bp, cp, (size_t)n); 466 bp += n; 467 cp += n; 468 if (cp != erdata) { 469 h_errno = NO_RECOVERY; 470 return NULL; 471 } 472 break; 473 default: 474 abort(); 475 } 476 if (!had_error) 477 haveanswer++; 478 } 479 if (haveanswer) { 480 *ap = NULL; 481 *hap = NULL; 482 /* 483 * Note: we sort even if host can take only one address 484 * in its return structures - should give it the "best" 485 * address in that case, not some random one 486 */ 487 if (res->nsort && haveanswer > 1 && qtype == T_A) 488 addrsort(h_addr_ptrs, haveanswer, res); 489 if (!host.h_name) { 490 n = (int)strlen(qname) + 1; /* for the \0 */ 491 if (n > ep - bp || n >= MAXHOSTNAMELEN) 492 goto no_recovery; 493 strlcpy(bp, qname, (size_t)(ep - bp)); 494 host.h_name = bp; 495 bp += n; 496 } 497 if (res->options & RES_USE_INET6) 498 map_v4v6_hostent(&host, &bp, ep); 499 h_errno = NETDB_SUCCESS; 500 return &host; 501 } 502 no_recovery: 503 h_errno = NO_RECOVERY; 504 return NULL; 505 } 506 507 struct hostent * 508 gethostbyname(const char *name) 509 { 510 struct hostent *hp; 511 res_state res = __res_get_state(); 512 513 if (res == NULL) 514 return NULL; 515 516 _DIAGASSERT(name != NULL); 517 518 if (res->options & RES_USE_INET6) { 519 hp = gethostbyname_internal(name, AF_INET6, res); 520 if (hp) { 521 __res_put_state(res); 522 return hp; 523 } 524 } 525 hp = gethostbyname_internal(name, AF_INET, res); 526 __res_put_state(res); 527 return hp; 528 } 529 530 struct hostent * 531 gethostbyname2(const char *name, int af) 532 { 533 struct hostent *hp; 534 res_state res = __res_get_state(); 535 536 if (res == NULL) 537 return NULL; 538 hp = gethostbyname_internal(name, af, res); 539 __res_put_state(res); 540 return hp; 541 } 542 543 static struct hostent * 544 gethostbyname_internal(const char *name, int af, res_state res) 545 { 546 const char *cp; 547 char *bp, *ep; 548 int size; 549 struct hostent *hp; 550 static const ns_dtab dtab[] = { 551 NS_FILES_CB(_gethtbyname, NULL) 552 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */ 553 NS_NIS_CB(_yp_gethtbyname, NULL) 554 NS_NULL_CB 555 }; 556 557 _DIAGASSERT(name != NULL); 558 559 switch (af) { 560 case AF_INET: 561 size = INADDRSZ; 562 break; 563 case AF_INET6: 564 size = IN6ADDRSZ; 565 break; 566 default: 567 h_errno = NETDB_INTERNAL; 568 errno = EAFNOSUPPORT; 569 return NULL; 570 } 571 572 host.h_addrtype = af; 573 host.h_length = size; 574 575 /* 576 * if there aren't any dots, it could be a user-level alias. 577 * this is also done in res_nquery() since we are not the only 578 * function that looks up host names. 579 */ 580 if (!strchr(name, '.') && (cp = __hostalias(name))) 581 name = cp; 582 583 /* 584 * disallow names consisting only of digits/dots, unless 585 * they end in a dot. 586 */ 587 if (isdigit((u_char) name[0])) 588 for (cp = name;; ++cp) { 589 if (!*cp) { 590 if (*--cp == '.') 591 break; 592 /* 593 * All-numeric, no dot at the end. 594 * Fake up a hostent as if we'd actually 595 * done a lookup. 596 */ 597 if (inet_pton(af, name, 598 (char *)(void *)host_addr) <= 0) { 599 h_errno = HOST_NOT_FOUND; 600 return NULL; 601 } 602 strncpy(hostbuf, name, MAXDNAME); 603 hostbuf[MAXDNAME] = '\0'; 604 bp = hostbuf + MAXDNAME; 605 ep = hostbuf + sizeof hostbuf; 606 host.h_name = hostbuf; 607 host.h_aliases = host_aliases; 608 host_aliases[0] = NULL; 609 h_addr_ptrs[0] = (char *)(void *)host_addr; 610 h_addr_ptrs[1] = NULL; 611 host.h_addr_list = h_addr_ptrs; 612 if (res->options & RES_USE_INET6) 613 map_v4v6_hostent(&host, &bp, ep); 614 h_errno = NETDB_SUCCESS; 615 return &host; 616 } 617 if (!isdigit((u_char) *cp) && *cp != '.') 618 break; 619 } 620 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || 621 name[0] == ':') 622 for (cp = name;; ++cp) { 623 if (!*cp) { 624 if (*--cp == '.') 625 break; 626 /* 627 * All-IPv6-legal, no dot at the end. 628 * Fake up a hostent as if we'd actually 629 * done a lookup. 630 */ 631 if (inet_pton(af, name, 632 (char *)(void *)host_addr) <= 0) { 633 h_errno = HOST_NOT_FOUND; 634 return NULL; 635 } 636 strncpy(hostbuf, name, MAXDNAME); 637 hostbuf[MAXDNAME] = '\0'; 638 bp = hostbuf + MAXDNAME; 639 ep = hostbuf + sizeof hostbuf; 640 host.h_name = hostbuf; 641 host.h_aliases = host_aliases; 642 host_aliases[0] = NULL; 643 h_addr_ptrs[0] = (char *)(void *)host_addr; 644 h_addr_ptrs[1] = NULL; 645 host.h_addr_list = h_addr_ptrs; 646 h_errno = NETDB_SUCCESS; 647 return &host; 648 } 649 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') 650 break; 651 } 652 653 hp = NULL; 654 h_errno = NETDB_INTERNAL; 655 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname", 656 default_dns_files, name, strlen(name), af) != NS_SUCCESS) 657 return NULL; 658 h_errno = NETDB_SUCCESS; 659 return hp; 660 } 661 662 struct hostent * 663 gethostbyaddr(const char *addr, /* XXX should have been def'd as u_char! */ 664 socklen_t len, int af) 665 { 666 const u_char *uaddr = (const u_char *)addr; 667 socklen_t size; 668 struct hostent *hp; 669 static const ns_dtab dtab[] = { 670 NS_FILES_CB(_gethtbyaddr, NULL) 671 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */ 672 NS_NIS_CB(_yp_gethtbyaddr, NULL) 673 NS_NULL_CB 674 }; 675 676 _DIAGASSERT(addr != NULL); 677 678 if (af == AF_INET6 && len == IN6ADDRSZ && 679 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) || 680 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) { 681 h_errno = HOST_NOT_FOUND; 682 return NULL; 683 } 684 if (af == AF_INET6 && len == IN6ADDRSZ && 685 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) || 686 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) { 687 /* Unmap. */ 688 addr += IN6ADDRSZ - INADDRSZ; 689 uaddr += IN6ADDRSZ - INADDRSZ; 690 af = AF_INET; 691 len = INADDRSZ; 692 } 693 switch (af) { 694 case AF_INET: 695 size = INADDRSZ; 696 break; 697 case AF_INET6: 698 size = IN6ADDRSZ; 699 break; 700 default: 701 errno = EAFNOSUPPORT; 702 h_errno = NETDB_INTERNAL; 703 return NULL; 704 } 705 if (size != len) { 706 errno = EINVAL; 707 h_errno = NETDB_INTERNAL; 708 return NULL; 709 } 710 hp = NULL; 711 h_errno = NETDB_INTERNAL; 712 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr", 713 default_dns_files, uaddr, len, af) != NS_SUCCESS) 714 return NULL; 715 h_errno = NETDB_SUCCESS; 716 return hp; 717 } 718 719 void 720 _sethtent(int f) 721 { 722 if (!hostf) 723 hostf = fopen(_PATH_HOSTS, "re"); 724 else 725 rewind(hostf); 726 stayopen = f; 727 } 728 729 void 730 _endhtent(void) 731 { 732 if (hostf && !stayopen) { 733 (void) fclose(hostf); 734 hostf = NULL; 735 } 736 } 737 738 struct hostent * 739 _gethtent(void) 740 { 741 char *p; 742 char *cp, **q; 743 int af, len; 744 745 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "re"))) { 746 h_errno = NETDB_INTERNAL; 747 return NULL; 748 } 749 again: 750 if (!(p = fgets(hostbuf, (int)sizeof hostbuf, hostf))) { 751 h_errno = HOST_NOT_FOUND; 752 return NULL; 753 } 754 if (*p == '#') 755 goto again; 756 if (!(cp = strpbrk(p, "#\n"))) 757 goto again; 758 *cp = '\0'; 759 if (!(cp = strpbrk(p, " \t"))) 760 goto again; 761 *cp++ = '\0'; 762 if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) { 763 af = AF_INET6; 764 len = IN6ADDRSZ; 765 } else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) { 766 res_state res = __res_get_state(); 767 if (res == NULL) 768 return NULL; 769 if (res->options & RES_USE_INET6) { 770 map_v4v6_address((char *)(void *)host_addr, 771 (char *)(void *)host_addr); 772 af = AF_INET6; 773 len = IN6ADDRSZ; 774 } else { 775 af = AF_INET; 776 len = INADDRSZ; 777 } 778 __res_put_state(res); 779 } else { 780 goto again; 781 } 782 /* if this is not something we're looking for, skip it. */ 783 if (host.h_addrtype != 0 && host.h_addrtype != af) 784 goto again; 785 if (host.h_length != 0 && host.h_length != len) 786 goto again; 787 h_addr_ptrs[0] = (char *)(void *)host_addr; 788 h_addr_ptrs[1] = NULL; 789 host.h_addr_list = h_addr_ptrs; 790 host.h_length = len; 791 host.h_addrtype = af; 792 while (*cp == ' ' || *cp == '\t') 793 cp++; 794 host.h_name = cp; 795 q = host.h_aliases = host_aliases; 796 if ((cp = strpbrk(cp, " \t")) != NULL) 797 *cp++ = '\0'; 798 while (cp && *cp) { 799 if (*cp == ' ' || *cp == '\t') { 800 cp++; 801 continue; 802 } 803 if (q < &host_aliases[MAXALIASES - 1]) 804 *q++ = cp; 805 if ((cp = strpbrk(cp, " \t")) != NULL) 806 *cp++ = '\0'; 807 } 808 *q = NULL; 809 h_errno = NETDB_SUCCESS; 810 return &host; 811 } 812 813 /*ARGSUSED*/ 814 int 815 _gethtbyname(void *rv, void *cb_data, va_list ap) 816 { 817 struct hostent *hp; 818 const char *name; 819 int af; 820 821 _DIAGASSERT(rv != NULL); 822 823 name = va_arg(ap, char *); 824 /* NOSTRICT skip len */(void)va_arg(ap, int); 825 af = va_arg(ap, int); 826 827 hp = NULL; 828 #if 0 829 { 830 res_state res = __res_get_state(); 831 if (res == NULL) 832 return NS_NOTFOUND; 833 if (res->options & RES_USE_INET6) 834 hp = _gethtbyname2(name, AF_INET6); 835 if (hp==NULL) 836 hp = _gethtbyname2(name, AF_INET); 837 __res_put_state(res); 838 } 839 #else 840 hp = _gethtbyname2(name, af); 841 #endif 842 *((struct hostent **)rv) = hp; 843 if (hp == NULL) { 844 h_errno = HOST_NOT_FOUND; 845 return NS_NOTFOUND; 846 } 847 return NS_SUCCESS; 848 } 849 850 struct hostent * 851 _gethtbyname2(const char *name, int af) 852 { 853 struct hostent *p; 854 char *tmpbuf, *ptr, **cp; 855 int num; 856 size_t len; 857 858 _DIAGASSERT(name != NULL); 859 860 _sethtent(stayopen); 861 ptr = tmpbuf = NULL; 862 num = 0; 863 while ((p = _gethtent()) != NULL && num < MAXADDRS) { 864 if (p->h_addrtype != af) 865 continue; 866 if (strcasecmp(p->h_name, name) != 0) { 867 for (cp = p->h_aliases; *cp != NULL; cp++) 868 if (strcasecmp(*cp, name) == 0) 869 break; 870 if (*cp == NULL) continue; 871 } 872 873 if (num == 0) { 874 size_t bufsize; 875 char *src; 876 877 bufsize = strlen(p->h_name) + 2 + 878 MAXADDRS * p->h_length + 879 ALIGNBYTES; 880 for (cp = p->h_aliases; *cp != NULL; cp++) 881 bufsize += strlen(*cp) + 1; 882 883 if ((tmpbuf = malloc(bufsize)) == NULL) { 884 h_errno = NETDB_INTERNAL; 885 return NULL; 886 } 887 888 ptr = tmpbuf; 889 src = p->h_name; 890 while ((*ptr++ = *src++) != '\0'); 891 for (cp = p->h_aliases; *cp != NULL; cp++) { 892 src = *cp; 893 while ((*ptr++ = *src++) != '\0'); 894 } 895 *ptr++ = '\0'; 896 897 ptr = (char *)(void *)ALIGN(ptr); 898 } 899 900 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length); 901 ptr += p->h_length; 902 num++; 903 } 904 _endhtent(); 905 if (num == 0) return NULL; 906 907 len = ptr - tmpbuf; 908 if (len > (sizeof(hostbuf) - ALIGNBYTES)) { 909 free(tmpbuf); 910 errno = ENOSPC; 911 h_errno = NETDB_INTERNAL; 912 return NULL; 913 } 914 ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len); 915 free(tmpbuf); 916 917 host.h_name = ptr; 918 while (*ptr++); 919 920 cp = host_aliases; 921 while (*ptr) { 922 *cp++ = ptr; 923 while (*ptr++); 924 } 925 ptr++; 926 *cp = NULL; 927 928 ptr = (char *)(void *)ALIGN(ptr); 929 cp = h_addr_ptrs; 930 while (num--) { 931 *cp++ = ptr; 932 ptr += host.h_length; 933 } 934 *cp = NULL; 935 936 return &host; 937 } 938 939 /*ARGSUSED*/ 940 int 941 _gethtbyaddr(void *rv, void *cb_data, va_list ap) 942 { 943 struct hostent *p; 944 const unsigned char *addr; 945 int len, af; 946 947 _DIAGASSERT(rv != NULL); 948 949 addr = va_arg(ap, unsigned char *); 950 len = va_arg(ap, int); 951 af = va_arg(ap, int); 952 953 host.h_length = len; 954 host.h_addrtype = af; 955 956 _sethtent(stayopen); 957 while ((p = _gethtent()) != NULL) 958 if (p->h_addrtype == af && !memcmp(p->h_addr, addr, 959 (size_t)len)) 960 break; 961 _endhtent(); 962 *((struct hostent **)rv) = p; 963 if (p==NULL) { 964 h_errno = HOST_NOT_FOUND; 965 return NS_NOTFOUND; 966 } 967 return NS_SUCCESS; 968 } 969 970 static void 971 map_v4v6_address(const char *src, char *dst) 972 { 973 u_char *p = (u_char *)dst; 974 char tmp[INADDRSZ]; 975 int i; 976 977 _DIAGASSERT(src != NULL); 978 _DIAGASSERT(dst != NULL); 979 980 /* Stash a temporary copy so our caller can update in place. */ 981 (void)memcpy(tmp, src, INADDRSZ); 982 /* Mark this ipv6 addr as a mapped ipv4. */ 983 for (i = 0; i < 10; i++) 984 *p++ = 0x00; 985 *p++ = 0xff; 986 *p++ = 0xff; 987 /* Retrieve the saved copy and we're done. */ 988 (void)memcpy((void *)p, tmp, INADDRSZ); 989 } 990 991 static void 992 map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) 993 { 994 char **ap; 995 996 _DIAGASSERT(hp != NULL); 997 _DIAGASSERT(bpp != NULL); 998 _DIAGASSERT(ep != NULL); 999 1000 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) 1001 return; 1002 hp->h_addrtype = AF_INET6; 1003 hp->h_length = IN6ADDRSZ; 1004 for (ap = hp->h_addr_list; *ap; ap++) { 1005 int i = (int)(sizeof(align) - 1006 (size_t)((u_long)*bpp % sizeof(align))); 1007 1008 if (ep - *bpp < (i + IN6ADDRSZ)) { 1009 /* Out of memory. Truncate address list here. XXX */ 1010 *ap = NULL; 1011 return; 1012 } 1013 *bpp += i; 1014 map_v4v6_address(*ap, *bpp); 1015 *ap = *bpp; 1016 *bpp += IN6ADDRSZ; 1017 } 1018 } 1019 1020 static void 1021 addrsort(char **ap, int num, res_state res) 1022 { 1023 int i, j; 1024 char **p; 1025 short aval[MAXADDRS]; 1026 int needsort = 0; 1027 1028 _DIAGASSERT(ap != NULL); 1029 1030 p = ap; 1031 for (i = 0; i < num; i++, p++) { 1032 for (j = 0 ; (unsigned)j < res->nsort; j++) 1033 if (res->sort_list[j].addr.s_addr == 1034 (((struct in_addr *)(void *)(*p))->s_addr & 1035 res->sort_list[j].mask)) 1036 break; 1037 aval[i] = j; 1038 if (needsort == 0 && i > 0 && j < aval[i-1]) 1039 needsort = i; 1040 } 1041 if (!needsort) 1042 return; 1043 1044 while (needsort < num) { 1045 for (j = needsort - 1; j >= 0; j--) { 1046 if (aval[j] > aval[j+1]) { 1047 char *hp; 1048 1049 i = aval[j]; 1050 aval[j] = aval[j+1]; 1051 aval[j+1] = i; 1052 1053 hp = ap[j]; 1054 ap[j] = ap[j+1]; 1055 ap[j+1] = hp; 1056 } else 1057 break; 1058 } 1059 needsort++; 1060 } 1061 } 1062 1063 struct hostent * 1064 gethostent(void) 1065 { 1066 host.h_addrtype = 0; 1067 host.h_length = 0; 1068 return _gethtent(); 1069 } 1070 1071 /*ARGSUSED*/ 1072 int 1073 _dns_gethtbyname(void *rv, void *cb_data, va_list ap) 1074 { 1075 querybuf *buf; 1076 int n, type; 1077 struct hostent *hp; 1078 const char *name; 1079 int af; 1080 res_state res; 1081 1082 _DIAGASSERT(rv != NULL); 1083 1084 name = va_arg(ap, char *); 1085 /* NOSTRICT skip len */(void)va_arg(ap, int); 1086 af = va_arg(ap, int); 1087 1088 switch (af) { 1089 case AF_INET: 1090 type = T_A; 1091 break; 1092 case AF_INET6: 1093 type = T_AAAA; 1094 break; 1095 default: 1096 return NS_UNAVAIL; 1097 } 1098 buf = malloc(sizeof(*buf)); 1099 if (buf == NULL) { 1100 h_errno = NETDB_INTERNAL; 1101 return NS_NOTFOUND; 1102 } 1103 res = __res_get_state(); 1104 if (res == NULL) { 1105 free(buf); 1106 return NS_NOTFOUND; 1107 } 1108 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf)); 1109 if (n < 0) { 1110 free(buf); 1111 debugprintf("res_nsearch failed (%d)\n", res, n); 1112 __res_put_state(res); 1113 return NS_NOTFOUND; 1114 } 1115 hp = getanswer(buf, n, name, type, res); 1116 free(buf); 1117 __res_put_state(res); 1118 if (hp == NULL) 1119 switch (h_errno) { 1120 case HOST_NOT_FOUND: 1121 return NS_NOTFOUND; 1122 case TRY_AGAIN: 1123 return NS_TRYAGAIN; 1124 default: 1125 return NS_UNAVAIL; 1126 } 1127 *((struct hostent **)rv) = hp; 1128 return NS_SUCCESS; 1129 } 1130 1131 /*ARGSUSED*/ 1132 int 1133 _dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) 1134 { 1135 char qbuf[MAXDNAME + 1], *qp, *ep; 1136 int n; 1137 querybuf *buf; 1138 struct hostent *hp; 1139 const unsigned char *uaddr; 1140 int len, af, advance; 1141 res_state res; 1142 1143 _DIAGASSERT(rv != NULL); 1144 1145 uaddr = va_arg(ap, unsigned char *); 1146 len = va_arg(ap, int); 1147 af = va_arg(ap, int); 1148 1149 switch (af) { 1150 case AF_INET: 1151 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", 1152 (uaddr[3] & 0xff), (uaddr[2] & 0xff), 1153 (uaddr[1] & 0xff), (uaddr[0] & 0xff)); 1154 break; 1155 1156 case AF_INET6: 1157 qp = qbuf; 1158 ep = qbuf + sizeof(qbuf) - 1; 1159 for (n = IN6ADDRSZ - 1; n >= 0; n--) { 1160 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", 1161 uaddr[n] & 0xf, 1162 ((unsigned int)uaddr[n] >> 4) & 0xf); 1163 if (advance > 0 && qp + advance < ep) 1164 qp += advance; 1165 else { 1166 h_errno = NETDB_INTERNAL; 1167 return NS_NOTFOUND; 1168 } 1169 } 1170 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { 1171 h_errno = NETDB_INTERNAL; 1172 return NS_NOTFOUND; 1173 } 1174 break; 1175 default: 1176 abort(); 1177 } 1178 1179 buf = malloc(sizeof(*buf)); 1180 if (buf == NULL) { 1181 h_errno = NETDB_INTERNAL; 1182 return NS_NOTFOUND; 1183 } 1184 res = __res_get_state(); 1185 if (res == NULL) { 1186 free(buf); 1187 return NS_NOTFOUND; 1188 } 1189 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf)); 1190 if (n < 0) { 1191 free(buf); 1192 debugprintf("res_nquery failed (%d)\n", res, n); 1193 __res_put_state(res); 1194 return NS_NOTFOUND; 1195 } 1196 hp = getanswer(buf, n, qbuf, T_PTR, res); 1197 free(buf); 1198 if (hp == NULL) { 1199 __res_put_state(res); 1200 switch (h_errno) { 1201 case HOST_NOT_FOUND: 1202 return NS_NOTFOUND; 1203 case TRY_AGAIN: 1204 return NS_TRYAGAIN; 1205 default: 1206 return NS_UNAVAIL; 1207 } 1208 } 1209 hp->h_addrtype = af; 1210 hp->h_length = len; 1211 (void)memcpy(host_addr, uaddr, (size_t)len); 1212 h_addr_ptrs[0] = (char *)(void *)host_addr; 1213 h_addr_ptrs[1] = NULL; 1214 if (af == AF_INET && (res->options & RES_USE_INET6)) { 1215 map_v4v6_address((char *)(void *)host_addr, 1216 (char *)(void *)host_addr); 1217 hp->h_addrtype = AF_INET6; 1218 hp->h_length = IN6ADDRSZ; 1219 } 1220 1221 __res_put_state(res); 1222 *((struct hostent **)rv) = hp; 1223 h_errno = NETDB_SUCCESS; 1224 return NS_SUCCESS; 1225 } 1226 1227 #ifdef YP 1228 /*ARGSUSED*/ 1229 struct hostent * 1230 _yphostent(char *line, int af) 1231 { 1232 static struct in_addr host_addrs[MAXADDRS]; 1233 static struct in6_addr host6_addrs[MAXADDRS]; 1234 char *p = line; 1235 char *cp, **q; 1236 char **hap; 1237 int addrok; 1238 int more; 1239 size_t naddrs; 1240 1241 _DIAGASSERT(line != NULL); 1242 1243 host.h_name = NULL; 1244 host.h_addr_list = h_addr_ptrs; 1245 host.h_addrtype = af; 1246 switch (af) { 1247 case AF_INET: 1248 host.h_length = INADDRSZ; 1249 break; 1250 case AF_INET6: 1251 host.h_length = IN6ADDRSZ; 1252 break; 1253 default: 1254 return NULL; 1255 } 1256 hap = h_addr_ptrs; 1257 q = host.h_aliases = host_aliases; 1258 naddrs = 0; 1259 1260 nextline: 1261 /* check for host_addrs overflow */ 1262 if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0])) 1263 goto done; 1264 if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0])) 1265 goto done; 1266 1267 more = 0; 1268 cp = strpbrk(p, " \t"); 1269 if (cp == NULL) 1270 goto done; 1271 *cp++ = '\0'; 1272 1273 /* p has should have an address */ 1274 addrok = 0; 1275 switch (af) { 1276 case AF_INET: 1277 addrok = inet_aton(p, &host_addrs[naddrs]); 1278 break; 1279 case AF_INET6: 1280 addrok = inet_pton(af, p, &host6_addrs[naddrs]); 1281 break; 1282 } 1283 if (addrok != 1) { 1284 /* skip to the next line */ 1285 while (cp && *cp) { 1286 if (*cp == '\n') { 1287 cp++; 1288 goto nextline; 1289 } 1290 cp++; 1291 } 1292 1293 goto done; 1294 } 1295 1296 switch (af) { 1297 case AF_INET: 1298 *hap++ = (char *)(void *)&host_addrs[naddrs++]; 1299 break; 1300 case AF_INET6: 1301 *hap++ = (char *)(void *)&host6_addrs[naddrs++]; 1302 break; 1303 } 1304 1305 while (*cp == ' ' || *cp == '\t') 1306 cp++; 1307 p = cp; 1308 cp = strpbrk(p, " \t\n"); 1309 if (cp != NULL) { 1310 if (*cp == '\n') 1311 more = 1; 1312 *cp++ = '\0'; 1313 } 1314 if (!host.h_name) 1315 host.h_name = p; 1316 else if (strcmp(host.h_name, p)==0) 1317 ; 1318 else if (q < &host_aliases[MAXALIASES - 1]) 1319 *q++ = p; 1320 p = cp; 1321 if (more) 1322 goto nextline; 1323 1324 while (cp && *cp) { 1325 if (*cp == ' ' || *cp == '\t') { 1326 cp++; 1327 continue; 1328 } 1329 if (*cp == '\n') { 1330 cp++; 1331 goto nextline; 1332 } 1333 if (q < &host_aliases[MAXALIASES - 1]) 1334 *q++ = cp; 1335 cp = strpbrk(cp, " \t"); 1336 if (cp != NULL) 1337 *cp++ = '\0'; 1338 } 1339 1340 done: 1341 if (host.h_name == NULL) 1342 return NULL; 1343 *q = NULL; 1344 *hap = NULL; 1345 return &host; 1346 } 1347 1348 /*ARGSUSED*/ 1349 int 1350 _yp_gethtbyaddr(void *rv, void *cb_data, va_list ap) 1351 { 1352 struct hostent *hp = NULL; 1353 static char *__ypcurrent; 1354 int __ypcurrentlen, r; 1355 char name[INET6_ADDRSTRLEN]; /* XXX enough? */ 1356 const unsigned char *uaddr; 1357 int af; 1358 const char *map; 1359 1360 _DIAGASSERT(rv != NULL); 1361 1362 uaddr = va_arg(ap, unsigned char *); 1363 /* NOSTRICT skip len */(void)va_arg(ap, int); 1364 af = va_arg(ap, int); 1365 1366 if (!__ypdomain) { 1367 if (_yp_check(&__ypdomain) == 0) 1368 return NS_UNAVAIL; 1369 } 1370 /* 1371 * XXX unfortunately, we cannot support IPv6 extended scoped address 1372 * notation here. gethostbyaddr() is not scope-aware. too bad. 1373 */ 1374 if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL) 1375 return NS_UNAVAIL; 1376 if (__ypcurrent) 1377 free(__ypcurrent); 1378 __ypcurrent = NULL; 1379 switch (af) { 1380 case AF_INET: 1381 map = "hosts.byaddr"; 1382 break; 1383 default: 1384 map = "ipnodes.byaddr"; 1385 break; 1386 } 1387 r = yp_match(__ypdomain, map, name, 1388 (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1389 if (r == 0) 1390 hp = _yphostent(__ypcurrent, af); 1391 if (hp == NULL) { 1392 h_errno = HOST_NOT_FOUND; 1393 return NS_NOTFOUND; 1394 } 1395 *((struct hostent **)rv) = hp; 1396 return NS_SUCCESS; 1397 } 1398 1399 /*ARGSUSED*/ 1400 int 1401 _yp_gethtbyname(void *rv, void *cb_data, va_list ap) 1402 { 1403 struct hostent *hp = NULL; 1404 static char *__ypcurrent; 1405 int __ypcurrentlen, r; 1406 const char *name; 1407 int af; 1408 const char *map; 1409 1410 _DIAGASSERT(rv != NULL); 1411 1412 name = va_arg(ap, char *); 1413 /* NOSTRICT skip len */(void)va_arg(ap, int); 1414 af = va_arg(ap, int); 1415 1416 if (!__ypdomain) { 1417 if (_yp_check(&__ypdomain) == 0) 1418 return NS_UNAVAIL; 1419 } 1420 if (__ypcurrent) 1421 free(__ypcurrent); 1422 __ypcurrent = NULL; 1423 switch (af) { 1424 case AF_INET: 1425 map = "hosts.byname"; 1426 break; 1427 default: 1428 map = "ipnodes.byname"; 1429 break; 1430 } 1431 r = yp_match(__ypdomain, map, name, 1432 (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1433 if (r == 0) 1434 hp = _yphostent(__ypcurrent, af); 1435 if (hp == NULL) { 1436 h_errno = HOST_NOT_FOUND; 1437 return NS_NOTFOUND; 1438 } 1439 *((struct hostent **)rv) = hp; 1440 return NS_SUCCESS; 1441 } 1442 #endif 1443