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