1 /* $OpenBSD: gethostnamadr_async.c,v 1.46 2022/11/17 17:39:41 florian Exp $ */ 2 /* 3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <netinet/in.h> 21 #include <arpa/inet.h> 22 #include <arpa/nameser.h> 23 #include <netdb.h> 24 25 #include <asr.h> 26 #include <ctype.h> 27 #include <errno.h> 28 #include <resolv.h> /* for res_hnok */ 29 #include <stdlib.h> 30 #include <string.h> 31 #include <unistd.h> 32 #include <limits.h> 33 34 #include "asr_private.h" 35 36 #define MAXALIASES 35 37 #define MAXADDRS 35 38 39 struct hostent_ext { 40 struct hostent h; 41 char *aliases[MAXALIASES + 1]; 42 char *addrs[MAXADDRS + 1]; 43 char *end; 44 char *pos; 45 }; 46 47 struct netent_ext { 48 struct netent n; 49 char *aliases[MAXALIASES + 1]; 50 char *end; 51 char *pos; 52 }; 53 54 static int gethostnamadr_async_run(struct asr_query *, struct asr_result *); 55 static struct hostent_ext *hostent_alloc(int); 56 static int hostent_set_cname(struct hostent_ext *, const char *, int); 57 static int hostent_add_alias(struct hostent_ext *, const char *, int); 58 static int hostent_add_addr(struct hostent_ext *, const void *, size_t); 59 static struct hostent_ext *hostent_from_addr(int, const char *, const char *); 60 static struct hostent_ext *hostent_file_match(FILE *, int, int, const char *, 61 int); 62 static struct hostent_ext *hostent_from_packet(int, int, char *, size_t); 63 static void netent_from_hostent(struct asr_result *ar); 64 65 struct asr_query * 66 gethostbyname_async(const char *name, void *asr) 67 { 68 return gethostbyname2_async(name, AF_INET, asr); 69 } 70 DEF_WEAK(gethostbyname_async); 71 72 struct asr_query * 73 gethostbyname2_async(const char *name, int af, void *asr) 74 { 75 struct asr_ctx *ac; 76 struct asr_query *as; 77 78 /* the original segfaults */ 79 if (name == NULL) { 80 errno = EINVAL; 81 return (NULL); 82 } 83 84 ac = _asr_use_resolver(asr); 85 if ((as = _asr_async_new(ac, ASR_GETHOSTBYNAME)) == NULL) 86 goto abort; /* errno set */ 87 as->as_run = gethostnamadr_async_run; 88 89 as->as.hostnamadr.family = af; 90 if (af == AF_INET) 91 as->as.hostnamadr.addrlen = INADDRSZ; 92 else if (af == AF_INET6) 93 as->as.hostnamadr.addrlen = IN6ADDRSZ; 94 as->as.hostnamadr.name = strdup(name); 95 if (as->as.hostnamadr.name == NULL) 96 goto abort; /* errno set */ 97 98 _asr_ctx_unref(ac); 99 return (as); 100 101 abort: 102 if (as) 103 _asr_async_free(as); 104 _asr_ctx_unref(ac); 105 return (NULL); 106 } 107 DEF_WEAK(gethostbyname2_async); 108 109 struct asr_query * 110 gethostbyaddr_async(const void *addr, socklen_t len, int af, void *asr) 111 { 112 struct asr_ctx *ac; 113 struct asr_query *as; 114 115 ac = _asr_use_resolver(asr); 116 as = _gethostbyaddr_async_ctx(addr, len, af, ac); 117 _asr_ctx_unref(ac); 118 119 return (as); 120 } 121 DEF_WEAK(gethostbyaddr_async); 122 123 struct asr_query * 124 _gethostbyaddr_async_ctx(const void *addr, socklen_t len, int af, 125 struct asr_ctx *ac) 126 { 127 struct asr_query *as; 128 129 if ((as = _asr_async_new(ac, ASR_GETHOSTBYADDR)) == NULL) 130 goto abort; /* errno set */ 131 as->as_run = gethostnamadr_async_run; 132 133 as->as.hostnamadr.family = af; 134 as->as.hostnamadr.addrlen = len; 135 if (len > 0) 136 memmove(as->as.hostnamadr.addr, addr, (len > 16) ? 16 : len); 137 138 return (as); 139 140 abort: 141 if (as) 142 _asr_async_free(as); 143 return (NULL); 144 } 145 146 static int 147 gethostnamadr_async_run(struct asr_query *as, struct asr_result *ar) 148 { 149 struct hostent_ext *h; 150 int r, type, saved_errno; 151 FILE *f; 152 char name[MAXDNAME], *data, addr[16], *c; 153 154 next: 155 switch (as->as_state) { 156 157 case ASR_STATE_INIT: 158 159 if (as->as.hostnamadr.family != AF_INET && 160 as->as.hostnamadr.family != AF_INET6) { 161 ar->ar_h_errno = NETDB_INTERNAL; 162 ar->ar_errno = EAFNOSUPPORT; 163 async_set_state(as, ASR_STATE_HALT); 164 break; 165 } 166 167 if ((as->as.hostnamadr.family == AF_INET && 168 as->as.hostnamadr.addrlen != INADDRSZ) || 169 (as->as.hostnamadr.family == AF_INET6 && 170 as->as.hostnamadr.addrlen != IN6ADDRSZ)) { 171 ar->ar_h_errno = NETDB_INTERNAL; 172 ar->ar_errno = EINVAL; 173 async_set_state(as, ASR_STATE_HALT); 174 break; 175 } 176 177 if (as->as_type == ASR_GETHOSTBYNAME) { 178 179 if (as->as.hostnamadr.name[0] == '\0') { 180 ar->ar_h_errno = NO_DATA; 181 async_set_state(as, ASR_STATE_HALT); 182 break; 183 } 184 185 /* Name might be an IP address string */ 186 for (c = as->as.hostnamadr.name; *c; c++) 187 if (!isdigit((unsigned char)*c) && 188 *c != '.' && *c != ':') 189 break; 190 if (*c == 0 && 191 inet_pton(as->as.hostnamadr.family, 192 as->as.hostnamadr.name, addr) == 1) { 193 h = hostent_from_addr(as->as.hostnamadr.family, 194 as->as.hostnamadr.name, addr); 195 if (h == NULL) { 196 ar->ar_errno = errno; 197 ar->ar_h_errno = NETDB_INTERNAL; 198 } 199 else { 200 ar->ar_hostent = &h->h; 201 ar->ar_h_errno = NETDB_SUCCESS; 202 } 203 async_set_state(as, ASR_STATE_HALT); 204 break; 205 } else { 206 if (!hnok_lenient(as->as.hostnamadr.name)) { 207 ar->ar_gai_errno = EAI_FAIL; 208 async_set_state(as, ASR_STATE_HALT); 209 } 210 } 211 } 212 async_set_state(as, ASR_STATE_NEXT_DB); 213 break; 214 215 case ASR_STATE_NEXT_DB: 216 217 if (_asr_iter_db(as) == -1) { 218 async_set_state(as, ASR_STATE_NOT_FOUND); 219 break; 220 } 221 222 switch (AS_DB(as)) { 223 224 case ASR_DB_DNS: 225 226 /* Create a subquery to do the DNS lookup */ 227 228 if (as->as_type == ASR_GETHOSTBYNAME) { 229 type = (as->as.hostnamadr.family == AF_INET) ? 230 T_A : T_AAAA; 231 as->as_subq = _res_search_async_ctx( 232 as->as.hostnamadr.name, 233 C_IN, type, as->as_ctx); 234 } else { 235 _asr_addr_as_fqdn(as->as.hostnamadr.addr, 236 as->as.hostnamadr.family, 237 name, sizeof(name)); 238 as->as_subq = _res_query_async_ctx( 239 name, C_IN, T_PTR, as->as_ctx); 240 } 241 242 if (as->as_subq == NULL) { 243 ar->ar_errno = errno; 244 ar->ar_h_errno = NETDB_INTERNAL; 245 async_set_state(as, ASR_STATE_HALT); 246 break; 247 } 248 249 async_set_state(as, ASR_STATE_SUBQUERY); 250 break; 251 252 case ASR_DB_FILE: 253 254 /* Try to find a match in the host file */ 255 256 if ((f = fopen(_PATH_HOSTS, "re")) == NULL) 257 break; 258 259 if (as->as_type == ASR_GETHOSTBYNAME) 260 data = as->as.hostnamadr.name; 261 else 262 data = as->as.hostnamadr.addr; 263 264 h = hostent_file_match(f, as->as_type, 265 as->as.hostnamadr.family, data, 266 as->as.hostnamadr.addrlen); 267 saved_errno = errno; 268 fclose(f); 269 errno = saved_errno; 270 271 if (h == NULL) { 272 if (errno) { 273 ar->ar_errno = errno; 274 ar->ar_h_errno = NETDB_INTERNAL; 275 async_set_state(as, ASR_STATE_HALT); 276 } 277 /* otherwise not found */ 278 break; 279 } 280 ar->ar_hostent = &h->h; 281 ar->ar_h_errno = NETDB_SUCCESS; 282 async_set_state(as, ASR_STATE_HALT); 283 break; 284 } 285 break; 286 287 case ASR_STATE_SUBQUERY: 288 289 /* Run the DNS subquery. */ 290 291 if ((r = asr_run(as->as_subq, ar)) == ASYNC_COND) 292 return (ASYNC_COND); 293 294 /* Done. */ 295 as->as_subq = NULL; 296 297 /* 298 * We either got no packet or a packet without an answer. 299 * Saveguard the h_errno and use the next DB. 300 */ 301 if (ar->ar_count == 0) { 302 free(ar->ar_data); 303 as->as.hostnamadr.subq_h_errno = ar->ar_h_errno; 304 async_set_state(as, ASR_STATE_NEXT_DB); 305 break; 306 } 307 308 /* Read the hostent from the packet. */ 309 310 h = hostent_from_packet(as->as_type, 311 as->as.hostnamadr.family, ar->ar_data, ar->ar_datalen); 312 free(ar->ar_data); 313 if (h == NULL) { 314 ar->ar_errno = errno; 315 ar->ar_h_errno = NETDB_INTERNAL; 316 async_set_state(as, ASR_STATE_HALT); 317 break; 318 } 319 320 if (as->as_type == ASR_GETHOSTBYADDR) { 321 if (hostent_add_addr(h, as->as.hostnamadr.addr, 322 as->as.hostnamadr.addrlen) == -1) { 323 free(h); 324 ar->ar_errno = errno; 325 ar->ar_h_errno = NETDB_INTERNAL; 326 async_set_state(as, ASR_STATE_HALT); 327 break; 328 } 329 } 330 331 /* 332 * No valid hostname or address found in the dns packet. 333 * Ignore it. 334 */ 335 if ((as->as_type == ASR_GETHOSTBYNAME && 336 h->h.h_addr_list[0] == NULL) || 337 h->h.h_name == NULL) { 338 free(h); 339 async_set_state(as, ASR_STATE_NEXT_DB); 340 break; 341 } 342 343 ar->ar_hostent = &h->h; 344 ar->ar_h_errno = NETDB_SUCCESS; 345 async_set_state(as, ASR_STATE_HALT); 346 break; 347 348 case ASR_STATE_NOT_FOUND: 349 ar->ar_errno = 0; 350 if (as->as.hostnamadr.subq_h_errno) 351 ar->ar_h_errno = as->as.hostnamadr.subq_h_errno; 352 else 353 ar->ar_h_errno = HOST_NOT_FOUND; 354 async_set_state(as, ASR_STATE_HALT); 355 break; 356 357 case ASR_STATE_HALT: 358 if (ar->ar_h_errno == NETDB_SUCCESS && 359 as->as_flags & ASYNC_GETNET) 360 netent_from_hostent(ar); 361 if (ar->ar_h_errno) { 362 ar->ar_hostent = NULL; 363 ar->ar_netent = NULL; 364 } else 365 ar->ar_errno = 0; 366 return (ASYNC_DONE); 367 368 default: 369 ar->ar_errno = EOPNOTSUPP; 370 ar->ar_h_errno = NETDB_INTERNAL; 371 ar->ar_gai_errno = EAI_SYSTEM; 372 async_set_state(as, ASR_STATE_HALT); 373 break; 374 } 375 goto next; 376 } 377 378 /* 379 * Create a hostent from a numeric address string. 380 */ 381 static struct hostent_ext * 382 hostent_from_addr(int family, const char *name, const char *addr) 383 { 384 struct hostent_ext *h; 385 386 if ((h = hostent_alloc(family)) == NULL) 387 return (NULL); 388 if (hostent_set_cname(h, name, 0) == -1) 389 goto fail; 390 if (hostent_add_addr(h, addr, h->h.h_length) == -1) 391 goto fail; 392 return (h); 393 fail: 394 free(h); 395 return (NULL); 396 } 397 398 /* 399 * Lookup the first matching entry in the hostfile, either by address or by 400 * name depending on reqtype, and build a hostent from the line. 401 */ 402 static struct hostent_ext * 403 hostent_file_match(FILE *f, int reqtype, int family, const char *data, 404 int datalen) 405 { 406 char *tokens[MAXTOKEN], addr[16], buf[BUFSIZ + 1]; 407 struct hostent_ext *h; 408 int n, i; 409 410 for (;;) { 411 n = _asr_parse_namedb_line(f, tokens, MAXTOKEN, buf, sizeof(buf)); 412 if (n == -1) { 413 errno = 0; /* ignore errors reading the file */ 414 return (NULL); 415 } 416 417 /* there must be an address and at least one name */ 418 if (n < 2) 419 continue; 420 421 if (reqtype == ASR_GETHOSTBYNAME) { 422 for (i = 1; i < n; i++) { 423 if (strcasecmp(data, tokens[i])) 424 continue; 425 if (inet_pton(family, tokens[0], addr) == 1) 426 goto found; 427 } 428 } else { 429 if (inet_pton(family, tokens[0], addr) == 1 && 430 memcmp(addr, data, datalen) == 0) 431 goto found; 432 } 433 } 434 435 found: 436 if ((h = hostent_alloc(family)) == NULL) 437 return (NULL); 438 if (hostent_set_cname(h, tokens[1], 0) == -1) 439 goto fail; 440 for (i = 2; i < n; i ++) 441 if (hostent_add_alias(h, tokens[i], 0) == -1) 442 goto fail; 443 if (hostent_add_addr(h, addr, h->h.h_length) == -1) 444 goto fail; 445 return (h); 446 fail: 447 free(h); 448 return (NULL); 449 } 450 451 /* 452 * Fill the hostent from the given DNS packet. 453 */ 454 static struct hostent_ext * 455 hostent_from_packet(int reqtype, int family, char *pkt, size_t pktlen) 456 { 457 struct hostent_ext *h; 458 struct asr_unpack p; 459 struct asr_dns_header hdr; 460 struct asr_dns_query q; 461 struct asr_dns_rr rr; 462 char dname[MAXDNAME]; 463 464 if ((h = hostent_alloc(family)) == NULL) 465 return (NULL); 466 467 _asr_unpack_init(&p, pkt, pktlen); 468 _asr_unpack_header(&p, &hdr); 469 for (; hdr.qdcount; hdr.qdcount--) 470 _asr_unpack_query(&p, &q); 471 strlcpy(dname, q.q_dname, sizeof(dname)); 472 473 for (; hdr.ancount; hdr.ancount--) { 474 _asr_unpack_rr(&p, &rr); 475 if (rr.rr_class != C_IN) 476 continue; 477 switch (rr.rr_type) { 478 479 case T_CNAME: 480 if (reqtype == ASR_GETHOSTBYNAME) { 481 if (hostent_add_alias(h, rr.rr_dname, 1) == -1) 482 goto fail; 483 } else { 484 if (strcasecmp(rr.rr_dname, dname) == 0) 485 strlcpy(dname, rr.rr.cname.cname, 486 sizeof(dname)); 487 } 488 break; 489 490 case T_PTR: 491 if (reqtype != ASR_GETHOSTBYADDR) 492 break; 493 if (strcasecmp(rr.rr_dname, dname) != 0) 494 continue; 495 if (hostent_set_cname(h, rr.rr.ptr.ptrname, 1) == -1) 496 hostent_add_alias(h, rr.rr.ptr.ptrname, 1); 497 break; 498 499 case T_A: 500 if (reqtype != ASR_GETHOSTBYNAME) 501 break; 502 if (family != AF_INET) 503 break; 504 if (hostent_set_cname(h, rr.rr_dname, 1) == -1) 505 ; 506 if (hostent_add_addr(h, &rr.rr.in_a.addr, 4) == -1) 507 goto fail; 508 break; 509 510 case T_AAAA: 511 if (reqtype != ASR_GETHOSTBYNAME) 512 break; 513 if (family != AF_INET6) 514 break; 515 if (hostent_set_cname(h, rr.rr_dname, 1) == -1) 516 ; 517 if (hostent_add_addr(h, &rr.rr.in_aaaa.addr6, 16) == -1) 518 goto fail; 519 break; 520 } 521 } 522 523 return (h); 524 fail: 525 free(h); 526 return (NULL); 527 } 528 529 static struct hostent_ext * 530 hostent_alloc(int family) 531 { 532 struct hostent_ext *h; 533 size_t alloc; 534 535 alloc = sizeof(*h) + 1024; 536 if ((h = calloc(1, alloc)) == NULL) 537 return (NULL); 538 539 h->h.h_addrtype = family; 540 h->h.h_length = (family == AF_INET) ? 4 : 16; 541 h->h.h_aliases = h->aliases; 542 h->h.h_addr_list = h->addrs; 543 h->pos = (char *)(h) + sizeof(*h); 544 h->end = h->pos + 1024; 545 546 return (h); 547 } 548 549 static int 550 hostent_set_cname(struct hostent_ext *h, const char *name, int isdname) 551 { 552 char buf[MAXDNAME]; 553 size_t n; 554 555 if (h->h.h_name) 556 return (-1); 557 558 if (isdname) { 559 _asr_strdname(name, buf, sizeof buf); 560 buf[strlen(buf) - 1] = '\0'; 561 if (!res_hnok(buf)) 562 return (-1); 563 name = buf; 564 } 565 566 n = strlen(name) + 1; 567 if (h->pos + n >= h->end) 568 return (-1); 569 570 h->h.h_name = h->pos; 571 memmove(h->pos, name, n); 572 h->pos += n; 573 return (0); 574 } 575 576 static int 577 hostent_add_alias(struct hostent_ext *h, const char *name, int isdname) 578 { 579 char buf[MAXDNAME]; 580 size_t i, n; 581 582 for (i = 0; i < MAXALIASES; i++) 583 if (h->aliases[i] == NULL) 584 break; 585 if (i == MAXALIASES) 586 return (0); 587 588 if (isdname) { 589 _asr_strdname(name, buf, sizeof buf); 590 buf[strlen(buf)-1] = '\0'; 591 if (!res_hnok(buf)) 592 return (-1); 593 name = buf; 594 } 595 596 n = strlen(name) + 1; 597 if (h->pos + n >= h->end) 598 return (0); 599 600 h->aliases[i] = h->pos; 601 memmove(h->pos, name, n); 602 h->pos += n; 603 return (0); 604 } 605 606 static int 607 hostent_add_addr(struct hostent_ext *h, const void *addr, size_t size) 608 { 609 int i; 610 611 for (i = 0; i < MAXADDRS; i++) 612 if (h->addrs[i] == NULL) 613 break; 614 if (i == MAXADDRS) 615 return (0); 616 617 if (h->pos + size >= h->end) 618 return (0); 619 620 h->addrs[i] = h->pos; 621 memmove(h->pos, addr, size); 622 h->pos += size; 623 return (0); 624 } 625 626 static void 627 netent_from_hostent(struct asr_result *ar) 628 { 629 struct in_addr *addr; 630 struct netent_ext *n; 631 struct hostent_ext *h; 632 char **na, **ha; 633 size_t sz; 634 635 /* Allocate and initialize the output. */ 636 if ((n = calloc(1, sizeof(*n) + 1024)) == NULL) { 637 ar->ar_h_errno = NETDB_INTERNAL; 638 ar->ar_errno = errno; 639 goto out; 640 } 641 n->pos = (char *)(n) + sizeof(*n); 642 n->end = n->pos + 1024; 643 n->n.n_name = n->pos; 644 n->n.n_aliases = n->aliases; 645 646 /* Copy the fixed-size data. */ 647 h = (struct hostent_ext *)ar->ar_hostent; 648 addr = (struct in_addr *)h->h.h_addr; 649 n->n.n_net = ntohl(addr->s_addr); 650 n->n.n_addrtype = h->h.h_addrtype; 651 652 /* Copy the network name. */ 653 sz = strlen(h->h.h_name) + 1; 654 memcpy(n->pos, h->h.h_name, sz); 655 n->pos += sz; 656 657 /* 658 * Copy the aliases. 659 * No overflow check is needed because we are merely copying 660 * a part of the data from a structure of the same size. 661 */ 662 na = n->aliases; 663 for (ha = h->aliases; *ha != NULL; ha++) { 664 sz = strlen(*ha) + 1; 665 memcpy(n->pos, *ha, sz); 666 *na++ = n->pos; 667 n->pos += sz; 668 } 669 *na = NULL; 670 671 /* Handle the return values. */ 672 ar->ar_netent = &n->n; 673 out: 674 free(ar->ar_hostent); 675 ar->ar_hostent = NULL; 676 } 677