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