1 /* 2 * Copyright (c) 1995 - 2006 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 #endif 37 #include "roken.h" 38 #ifdef HAVE_ARPA_NAMESER_H 39 #include <arpa/nameser.h> 40 #endif 41 #ifdef HAVE_RESOLV_H 42 #include <resolv.h> 43 #endif 44 #include "resolve.h" 45 46 #include <assert.h> 47 48 RCSID("$Id: resolve.c 19869 2007-01-12 16:03:14Z lha $"); 49 50 #ifdef _AIX /* AIX have broken res_nsearch() in 5.1 (5.0 also ?) */ 51 #undef HAVE_RES_NSEARCH 52 #endif 53 54 #define DECL(X) {#X, rk_ns_t_##X} 55 56 static struct stot{ 57 const char *name; 58 int type; 59 }stot[] = { 60 DECL(a), 61 DECL(aaaa), 62 DECL(ns), 63 DECL(cname), 64 DECL(soa), 65 DECL(ptr), 66 DECL(mx), 67 DECL(txt), 68 DECL(afsdb), 69 DECL(sig), 70 DECL(key), 71 DECL(srv), 72 DECL(naptr), 73 DECL(sshfp), 74 DECL(ds), 75 {NULL, 0} 76 }; 77 78 int _resolve_debug = 0; 79 80 int ROKEN_LIB_FUNCTION 81 dns_string_to_type(const char *name) 82 { 83 struct stot *p = stot; 84 for(p = stot; p->name; p++) 85 if(strcasecmp(name, p->name) == 0) 86 return p->type; 87 return -1; 88 } 89 90 const char * ROKEN_LIB_FUNCTION 91 dns_type_to_string(int type) 92 { 93 struct stot *p = stot; 94 for(p = stot; p->name; p++) 95 if(type == p->type) 96 return p->name; 97 return NULL; 98 } 99 100 #if (defined(HAVE_RES_SEARCH) || defined(HAVE_RES_NSEARCH)) && defined(HAVE_DN_EXPAND) 101 102 static void 103 dns_free_rr(struct resource_record *rr) 104 { 105 if(rr->domain) 106 free(rr->domain); 107 if(rr->u.data) 108 free(rr->u.data); 109 free(rr); 110 } 111 112 void ROKEN_LIB_FUNCTION 113 dns_free_data(struct dns_reply *r) 114 { 115 struct resource_record *rr; 116 if(r->q.domain) 117 free(r->q.domain); 118 for(rr = r->head; rr;){ 119 struct resource_record *tmp = rr; 120 rr = rr->next; 121 dns_free_rr(tmp); 122 } 123 free (r); 124 } 125 126 static int 127 parse_record(const unsigned char *data, const unsigned char *end_data, 128 const unsigned char **pp, struct resource_record **ret_rr) 129 { 130 struct resource_record *rr; 131 int type, class, ttl, size; 132 int status; 133 char host[MAXDNAME]; 134 const unsigned char *p = *pp; 135 136 *ret_rr = NULL; 137 138 status = dn_expand(data, end_data, p, host, sizeof(host)); 139 if(status < 0) 140 return -1; 141 if (p + status + 10 > end_data) 142 return -1; 143 144 p += status; 145 type = (p[0] << 8) | p[1]; 146 p += 2; 147 class = (p[0] << 8) | p[1]; 148 p += 2; 149 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; 150 p += 4; 151 size = (p[0] << 8) | p[1]; 152 p += 2; 153 154 if (p + size > end_data) 155 return -1; 156 157 rr = calloc(1, sizeof(*rr)); 158 if(rr == NULL) 159 return -1; 160 rr->domain = strdup(host); 161 if(rr->domain == NULL) { 162 dns_free_rr(rr); 163 return -1; 164 } 165 rr->type = type; 166 rr->class = class; 167 rr->ttl = ttl; 168 rr->size = size; 169 switch(type){ 170 case rk_ns_t_ns: 171 case rk_ns_t_cname: 172 case rk_ns_t_ptr: 173 status = dn_expand(data, end_data, p, host, sizeof(host)); 174 if(status < 0) { 175 dns_free_rr(rr); 176 return -1; 177 } 178 rr->u.txt = strdup(host); 179 if(rr->u.txt == NULL) { 180 dns_free_rr(rr); 181 return -1; 182 } 183 break; 184 case rk_ns_t_mx: 185 case rk_ns_t_afsdb:{ 186 size_t hostlen; 187 188 status = dn_expand(data, end_data, p + 2, host, sizeof(host)); 189 if(status < 0){ 190 dns_free_rr(rr); 191 return -1; 192 } 193 if (status + 2 > size) { 194 dns_free_rr(rr); 195 return -1; 196 } 197 198 hostlen = strlen(host); 199 rr->u.mx = (struct mx_record*)malloc(sizeof(struct mx_record) + 200 hostlen); 201 if(rr->u.mx == NULL) { 202 dns_free_rr(rr); 203 return -1; 204 } 205 rr->u.mx->preference = (p[0] << 8) | p[1]; 206 strlcpy(rr->u.mx->domain, host, hostlen + 1); 207 break; 208 } 209 case rk_ns_t_srv:{ 210 size_t hostlen; 211 status = dn_expand(data, end_data, p + 6, host, sizeof(host)); 212 if(status < 0){ 213 dns_free_rr(rr); 214 return -1; 215 } 216 if (status + 6 > size) { 217 dns_free_rr(rr); 218 return -1; 219 } 220 221 hostlen = strlen(host); 222 rr->u.srv = 223 (struct srv_record*)malloc(sizeof(struct srv_record) + 224 hostlen); 225 if(rr->u.srv == NULL) { 226 dns_free_rr(rr); 227 return -1; 228 } 229 rr->u.srv->priority = (p[0] << 8) | p[1]; 230 rr->u.srv->weight = (p[2] << 8) | p[3]; 231 rr->u.srv->port = (p[4] << 8) | p[5]; 232 strlcpy(rr->u.srv->target, host, hostlen + 1); 233 break; 234 } 235 case rk_ns_t_txt:{ 236 if(size == 0 || size < *p + 1) { 237 dns_free_rr(rr); 238 return -1; 239 } 240 rr->u.txt = (char*)malloc(*p + 1); 241 if(rr->u.txt == NULL) { 242 dns_free_rr(rr); 243 return -1; 244 } 245 strncpy(rr->u.txt, (const char*)(p + 1), *p); 246 rr->u.txt[*p] = '\0'; 247 break; 248 } 249 case rk_ns_t_key : { 250 size_t key_len; 251 252 if (size < 4) { 253 dns_free_rr(rr); 254 return -1; 255 } 256 257 key_len = size - 4; 258 rr->u.key = malloc (sizeof(*rr->u.key) + key_len - 1); 259 if (rr->u.key == NULL) { 260 dns_free_rr(rr); 261 return -1; 262 } 263 264 rr->u.key->flags = (p[0] << 8) | p[1]; 265 rr->u.key->protocol = p[2]; 266 rr->u.key->algorithm = p[3]; 267 rr->u.key->key_len = key_len; 268 memcpy (rr->u.key->key_data, p + 4, key_len); 269 break; 270 } 271 case rk_ns_t_sig : { 272 size_t sig_len, hostlen; 273 274 if(size <= 18) { 275 dns_free_rr(rr); 276 return -1; 277 } 278 status = dn_expand (data, end_data, p + 18, host, sizeof(host)); 279 if (status < 0) { 280 dns_free_rr(rr); 281 return -1; 282 } 283 if (status + 18 > size) { 284 dns_free_rr(rr); 285 return -1; 286 } 287 288 /* the signer name is placed after the sig_data, to make it 289 easy to free this structure; the size calculation below 290 includes the zero-termination if the structure itself. 291 don't you just love C? 292 */ 293 sig_len = size - 18 - status; 294 hostlen = strlen(host); 295 rr->u.sig = malloc(sizeof(*rr->u.sig) 296 + hostlen + sig_len); 297 if (rr->u.sig == NULL) { 298 dns_free_rr(rr); 299 return -1; 300 } 301 rr->u.sig->type = (p[0] << 8) | p[1]; 302 rr->u.sig->algorithm = p[2]; 303 rr->u.sig->labels = p[3]; 304 rr->u.sig->orig_ttl = (p[4] << 24) | (p[5] << 16) 305 | (p[6] << 8) | p[7]; 306 rr->u.sig->sig_expiration = (p[8] << 24) | (p[9] << 16) 307 | (p[10] << 8) | p[11]; 308 rr->u.sig->sig_inception = (p[12] << 24) | (p[13] << 16) 309 | (p[14] << 8) | p[15]; 310 rr->u.sig->key_tag = (p[16] << 8) | p[17]; 311 rr->u.sig->sig_len = sig_len; 312 memcpy (rr->u.sig->sig_data, p + 18 + status, sig_len); 313 rr->u.sig->signer = &rr->u.sig->sig_data[sig_len]; 314 strlcpy(rr->u.sig->signer, host, hostlen + 1); 315 break; 316 } 317 318 case rk_ns_t_cert : { 319 size_t cert_len; 320 321 if (size < 5) { 322 dns_free_rr(rr); 323 return -1; 324 } 325 326 cert_len = size - 5; 327 rr->u.cert = malloc (sizeof(*rr->u.cert) + cert_len - 1); 328 if (rr->u.cert == NULL) { 329 dns_free_rr(rr); 330 return -1; 331 } 332 333 rr->u.cert->type = (p[0] << 8) | p[1]; 334 rr->u.cert->tag = (p[2] << 8) | p[3]; 335 rr->u.cert->algorithm = p[4]; 336 rr->u.cert->cert_len = cert_len; 337 memcpy (rr->u.cert->cert_data, p + 5, cert_len); 338 break; 339 } 340 case rk_ns_t_sshfp : { 341 size_t sshfp_len; 342 343 if (size < 2) { 344 dns_free_rr(rr); 345 return -1; 346 } 347 348 sshfp_len = size - 2; 349 350 rr->u.sshfp = malloc (sizeof(*rr->u.sshfp) + sshfp_len - 1); 351 if (rr->u.sshfp == NULL) { 352 dns_free_rr(rr); 353 return -1; 354 } 355 356 rr->u.sshfp->algorithm = p[0]; 357 rr->u.sshfp->type = p[1]; 358 rr->u.sshfp->sshfp_len = sshfp_len; 359 memcpy (rr->u.sshfp->sshfp_data, p + 2, sshfp_len); 360 break; 361 } 362 case rk_ns_t_ds: { 363 size_t digest_len; 364 365 if (size < 4) { 366 dns_free_rr(rr); 367 return -1; 368 } 369 370 digest_len = size - 4; 371 372 rr->u.ds = malloc (sizeof(*rr->u.ds) + digest_len - 1); 373 if (rr->u.ds == NULL) { 374 dns_free_rr(rr); 375 return -1; 376 } 377 378 rr->u.ds->key_tag = (p[0] << 8) | p[1]; 379 rr->u.ds->algorithm = p[2]; 380 rr->u.ds->digest_type = p[3]; 381 rr->u.ds->digest_len = digest_len; 382 memcpy (rr->u.ds->digest_data, p + 4, digest_len); 383 break; 384 } 385 default: 386 rr->u.data = (unsigned char*)malloc(size); 387 if(size != 0 && rr->u.data == NULL) { 388 dns_free_rr(rr); 389 return -1; 390 } 391 if (size) 392 memcpy(rr->u.data, p, size); 393 } 394 *pp = p + size; 395 *ret_rr = rr; 396 397 return 0; 398 } 399 400 #ifndef TEST_RESOLVE 401 static 402 #endif 403 struct dns_reply* 404 parse_reply(const unsigned char *data, size_t len) 405 { 406 const unsigned char *p; 407 int status; 408 int i; 409 char host[MAXDNAME]; 410 const unsigned char *end_data = data + len; 411 struct dns_reply *r; 412 struct resource_record **rr; 413 414 r = calloc(1, sizeof(*r)); 415 if (r == NULL) 416 return NULL; 417 418 p = data; 419 420 r->h.id = (p[0] << 8) | p[1]; 421 r->h.flags = 0; 422 if (p[2] & 0x01) 423 r->h.flags |= rk_DNS_HEADER_RESPONSE_FLAG; 424 r->h.opcode = (p[2] >> 1) & 0xf; 425 if (p[2] & 0x20) 426 r->h.flags |= rk_DNS_HEADER_AUTHORITIVE_ANSWER; 427 if (p[2] & 0x40) 428 r->h.flags |= rk_DNS_HEADER_TRUNCATED_MESSAGE; 429 if (p[2] & 0x80) 430 r->h.flags |= rk_DNS_HEADER_RECURSION_DESIRED; 431 if (p[3] & 0x01) 432 r->h.flags |= rk_DNS_HEADER_RECURSION_AVAILABLE; 433 if (p[3] & 0x04) 434 r->h.flags |= rk_DNS_HEADER_AUTHORITIVE_ANSWER; 435 if (p[3] & 0x08) 436 r->h.flags |= rk_DNS_HEADER_CHECKING_DISABLED; 437 r->h.response_code = (p[3] >> 4) & 0xf; 438 r->h.qdcount = (p[4] << 8) | p[5]; 439 r->h.ancount = (p[6] << 8) | p[7]; 440 r->h.nscount = (p[8] << 8) | p[9]; 441 r->h.arcount = (p[10] << 8) | p[11]; 442 443 p += 12; 444 445 if(r->h.qdcount != 1) { 446 free(r); 447 return NULL; 448 } 449 status = dn_expand(data, end_data, p, host, sizeof(host)); 450 if(status < 0){ 451 dns_free_data(r); 452 return NULL; 453 } 454 r->q.domain = strdup(host); 455 if(r->q.domain == NULL) { 456 dns_free_data(r); 457 return NULL; 458 } 459 if (p + status + 4 > end_data) { 460 dns_free_data(r); 461 return NULL; 462 } 463 p += status; 464 r->q.type = (p[0] << 8 | p[1]); 465 p += 2; 466 r->q.class = (p[0] << 8 | p[1]); 467 p += 2; 468 469 rr = &r->head; 470 for(i = 0; i < r->h.ancount; i++) { 471 if(parse_record(data, end_data, &p, rr) != 0) { 472 dns_free_data(r); 473 return NULL; 474 } 475 rr = &(*rr)->next; 476 } 477 for(i = 0; i < r->h.nscount; i++) { 478 if(parse_record(data, end_data, &p, rr) != 0) { 479 dns_free_data(r); 480 return NULL; 481 } 482 rr = &(*rr)->next; 483 } 484 for(i = 0; i < r->h.arcount; i++) { 485 if(parse_record(data, end_data, &p, rr) != 0) { 486 dns_free_data(r); 487 return NULL; 488 } 489 rr = &(*rr)->next; 490 } 491 *rr = NULL; 492 return r; 493 } 494 495 #ifdef HAVE_RES_NSEARCH 496 #ifdef HAVE_RES_NDESTROY 497 #define rk_res_free(x) res_ndestroy(x) 498 #else 499 #define rk_res_free(x) res_nclose(x) 500 #endif 501 #endif 502 503 static struct dns_reply * 504 dns_lookup_int(const char *domain, int rr_class, int rr_type) 505 { 506 struct dns_reply *r; 507 unsigned char *reply = NULL; 508 int size; 509 int len; 510 #ifdef HAVE_RES_NSEARCH 511 struct __res_state state; 512 memset(&state, 0, sizeof(state)); 513 if(res_ninit(&state)) 514 return NULL; /* is this the best we can do? */ 515 #elif defined(HAVE__RES) 516 u_long old_options = 0; 517 #endif 518 519 size = 0; 520 len = 1000; 521 do { 522 if (reply) { 523 free(reply); 524 reply = NULL; 525 } 526 if (size <= len) 527 size = len; 528 if (_resolve_debug) { 529 #ifdef HAVE_RES_NSEARCH 530 state.options |= RES_DEBUG; 531 #elif defined(HAVE__RES) 532 old_options = _res.options; 533 _res.options |= RES_DEBUG; 534 #endif 535 fprintf(stderr, "dns_lookup(%s, %d, %s), buffer size %d\n", domain, 536 rr_class, dns_type_to_string(rr_type), size); 537 } 538 reply = malloc(size); 539 if (reply == NULL) { 540 #ifdef HAVE_RES_NSEARCH 541 rk_res_free(&state); 542 #endif 543 return NULL; 544 } 545 #ifdef HAVE_RES_NSEARCH 546 len = res_nsearch(&state, domain, rr_class, rr_type, reply, size); 547 #else 548 len = res_search(domain, rr_class, rr_type, reply, size); 549 #endif 550 if (_resolve_debug) { 551 #if defined(HAVE__RES) && !defined(HAVE_RES_NSEARCH) 552 _res.options = old_options; 553 #endif 554 fprintf(stderr, "dns_lookup(%s, %d, %s) --> %d\n", 555 domain, rr_class, dns_type_to_string(rr_type), len); 556 } 557 if (len < 0) { 558 #ifdef HAVE_RES_NSEARCH 559 rk_res_free(&state); 560 #endif 561 free(reply); 562 return NULL; 563 } 564 } while (size < len && len < rk_DNS_MAX_PACKET_SIZE); 565 #ifdef HAVE_RES_NSEARCH 566 rk_res_free(&state); 567 #endif 568 569 len = min(len, size); 570 r = parse_reply(reply, len); 571 free(reply); 572 return r; 573 } 574 575 struct dns_reply * ROKEN_LIB_FUNCTION 576 dns_lookup(const char *domain, const char *type_name) 577 { 578 int type; 579 580 type = dns_string_to_type(type_name); 581 if(type == -1) { 582 if(_resolve_debug) 583 fprintf(stderr, "dns_lookup: unknown resource type: `%s'\n", 584 type_name); 585 return NULL; 586 } 587 return dns_lookup_int(domain, C_IN, type); 588 } 589 590 static int 591 compare_srv(const void *a, const void *b) 592 { 593 const struct resource_record *const* aa = a, *const* bb = b; 594 595 if((*aa)->u.srv->priority == (*bb)->u.srv->priority) 596 return ((*aa)->u.srv->weight - (*bb)->u.srv->weight); 597 return ((*aa)->u.srv->priority - (*bb)->u.srv->priority); 598 } 599 600 #ifndef HAVE_RANDOM 601 #define random() rand() 602 #endif 603 604 /* try to rearrange the srv-records by the algorithm in RFC2782 */ 605 void ROKEN_LIB_FUNCTION 606 dns_srv_order(struct dns_reply *r) 607 { 608 struct resource_record **srvs, **ss, **headp; 609 struct resource_record *rr; 610 int num_srv = 0; 611 612 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE) 613 int state[256 / sizeof(int)]; 614 char *oldstate; 615 #endif 616 617 for(rr = r->head; rr; rr = rr->next) 618 if(rr->type == rk_ns_t_srv) 619 num_srv++; 620 621 if(num_srv == 0) 622 return; 623 624 srvs = malloc(num_srv * sizeof(*srvs)); 625 if(srvs == NULL) 626 return; /* XXX not much to do here */ 627 628 /* unlink all srv-records from the linked list and put them in 629 a vector */ 630 for(ss = srvs, headp = &r->head; *headp; ) 631 if((*headp)->type == rk_ns_t_srv) { 632 *ss = *headp; 633 *headp = (*headp)->next; 634 (*ss)->next = NULL; 635 ss++; 636 } else 637 headp = &(*headp)->next; 638 639 /* sort them by priority and weight */ 640 qsort(srvs, num_srv, sizeof(*srvs), compare_srv); 641 642 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE) 643 oldstate = initstate(time(NULL), (char*)state, sizeof(state)); 644 #endif 645 646 headp = &r->head; 647 648 for(ss = srvs; ss < srvs + num_srv; ) { 649 int sum, rnd, count; 650 struct resource_record **ee, **tt; 651 /* find the last record with the same priority and count the 652 sum of all weights */ 653 for(sum = 0, tt = ss; tt < srvs + num_srv; tt++) { 654 assert(*tt != NULL); 655 if((*tt)->u.srv->priority != (*ss)->u.srv->priority) 656 break; 657 sum += (*tt)->u.srv->weight; 658 } 659 ee = tt; 660 /* ss is now the first record of this priority and ee is the 661 first of the next */ 662 while(ss < ee) { 663 rnd = random() % (sum + 1); 664 for(count = 0, tt = ss; ; tt++) { 665 if(*tt == NULL) 666 continue; 667 count += (*tt)->u.srv->weight; 668 if(count >= rnd) 669 break; 670 } 671 672 assert(tt < ee); 673 674 /* insert the selected record at the tail (of the head) of 675 the list */ 676 (*tt)->next = *headp; 677 *headp = *tt; 678 headp = &(*tt)->next; 679 sum -= (*tt)->u.srv->weight; 680 *tt = NULL; 681 while(ss < ee && *ss == NULL) 682 ss++; 683 } 684 } 685 686 #if defined(HAVE_INITSTATE) && defined(HAVE_SETSTATE) 687 setstate(oldstate); 688 #endif 689 free(srvs); 690 return; 691 } 692 693 #else /* NOT defined(HAVE_RES_SEARCH) && defined(HAVE_DN_EXPAND) */ 694 695 struct dns_reply * ROKEN_LIB_FUNCTION 696 dns_lookup(const char *domain, const char *type_name) 697 { 698 return NULL; 699 } 700 701 void ROKEN_LIB_FUNCTION 702 dns_free_data(struct dns_reply *r) 703 { 704 } 705 706 void ROKEN_LIB_FUNCTION 707 dns_srv_order(struct dns_reply *r) 708 { 709 } 710 711 #endif 712