1 /* 2 * iterator/iter_hints.c - iterative resolver module stub and root hints. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains functions to assist the iterator module. 40 * Keep track of stub and root hints, and read those from config. 41 */ 42 #include "config.h" 43 #include "iterator/iter_hints.h" 44 #include "iterator/iter_delegpt.h" 45 #include "util/log.h" 46 #include "util/config_file.h" 47 #include "util/net_help.h" 48 #include "util/data/dname.h" 49 #include "sldns/rrdef.h" 50 #include "sldns/str2wire.h" 51 #include "sldns/wire2str.h" 52 53 struct iter_hints* 54 hints_create(void) 55 { 56 struct iter_hints* hints = (struct iter_hints*)calloc(1, 57 sizeof(struct iter_hints)); 58 if(!hints) 59 return NULL; 60 return hints; 61 } 62 63 static void hints_stub_free(struct iter_hints_stub* s) 64 { 65 if(!s) return; 66 delegpt_free_mlc(s->dp); 67 free(s); 68 } 69 70 static void delhintnode(rbnode_t* n, void* ATTR_UNUSED(arg)) 71 { 72 struct iter_hints_stub* node = (struct iter_hints_stub*)n; 73 hints_stub_free(node); 74 } 75 76 static void hints_del_tree(struct iter_hints* hints) 77 { 78 traverse_postorder(&hints->tree, &delhintnode, NULL); 79 } 80 81 void 82 hints_delete(struct iter_hints* hints) 83 { 84 if(!hints) 85 return; 86 hints_del_tree(hints); 87 free(hints); 88 } 89 90 /** add hint to delegation hints */ 91 static int 92 ah(struct delegpt* dp, const char* sv, const char* ip) 93 { 94 struct sockaddr_storage addr; 95 socklen_t addrlen; 96 size_t dname_len; 97 uint8_t* dname = sldns_str2wire_dname(sv, &dname_len); 98 if(!dname) { 99 log_err("could not parse %s", sv); 100 return 0; 101 } 102 if(!delegpt_add_ns_mlc(dp, dname, 0) || 103 !extstrtoaddr(ip, &addr, &addrlen) || 104 !delegpt_add_target_mlc(dp, dname, dname_len, 105 &addr, addrlen, 0, 0)) { 106 free(dname); 107 return 0; 108 } 109 free(dname); 110 return 1; 111 } 112 113 /** obtain compiletime provided root hints */ 114 static struct delegpt* 115 compile_time_root_prime(int do_ip4, int do_ip6) 116 { 117 /* from: 118 ; This file is made available by InterNIC 119 ; under anonymous FTP as 120 ; file /domain/named.cache 121 ; on server FTP.INTERNIC.NET 122 ; -OR- RS.INTERNIC.NET 123 ; 124 ; related version of root zone: changes-on-20120103 125 */ 126 struct delegpt* dp = delegpt_create_mlc((uint8_t*)"\000"); 127 if(!dp) 128 return NULL; 129 dp->has_parent_side_NS = 1; 130 if(do_ip4) { 131 if(!ah(dp, "A.ROOT-SERVERS.NET.", "198.41.0.4")) goto failed; 132 if(!ah(dp, "B.ROOT-SERVERS.NET.", "192.228.79.201")) goto failed; 133 if(!ah(dp, "C.ROOT-SERVERS.NET.", "192.33.4.12")) goto failed; 134 if(!ah(dp, "D.ROOT-SERVERS.NET.", "199.7.91.13")) goto failed; 135 if(!ah(dp, "E.ROOT-SERVERS.NET.", "192.203.230.10")) goto failed; 136 if(!ah(dp, "F.ROOT-SERVERS.NET.", "192.5.5.241")) goto failed; 137 if(!ah(dp, "G.ROOT-SERVERS.NET.", "192.112.36.4")) goto failed; 138 if(!ah(dp, "H.ROOT-SERVERS.NET.", "198.97.190.53")) goto failed; 139 if(!ah(dp, "I.ROOT-SERVERS.NET.", "192.36.148.17")) goto failed; 140 if(!ah(dp, "J.ROOT-SERVERS.NET.", "192.58.128.30")) goto failed; 141 if(!ah(dp, "K.ROOT-SERVERS.NET.", "193.0.14.129")) goto failed; 142 if(!ah(dp, "L.ROOT-SERVERS.NET.", "199.7.83.42")) goto failed; 143 if(!ah(dp, "M.ROOT-SERVERS.NET.", "202.12.27.33")) goto failed; 144 } 145 if(do_ip6) { 146 if(!ah(dp, "A.ROOT-SERVERS.NET.", "2001:503:ba3e::2:30")) goto failed; 147 if(!ah(dp, "B.ROOT-SERVERS.NET.", "2001:500:84::b")) goto failed; 148 if(!ah(dp, "C.ROOT-SERVERS.NET.", "2001:500:2::c")) goto failed; 149 if(!ah(dp, "D.ROOT-SERVERS.NET.", "2001:500:2d::d")) goto failed; 150 if(!ah(dp, "E.ROOT-SERVERS.NET.", "2001:500:a8::e")) goto failed; 151 if(!ah(dp, "F.ROOT-SERVERS.NET.", "2001:500:2f::f")) goto failed; 152 if(!ah(dp, "H.ROOT-SERVERS.NET.", "2001:500:1::53")) goto failed; 153 if(!ah(dp, "I.ROOT-SERVERS.NET.", "2001:7fe::53")) goto failed; 154 if(!ah(dp, "J.ROOT-SERVERS.NET.", "2001:503:c27::2:30")) goto failed; 155 if(!ah(dp, "K.ROOT-SERVERS.NET.", "2001:7fd::1")) goto failed; 156 if(!ah(dp, "L.ROOT-SERVERS.NET.", "2001:500:9f::42")) goto failed; 157 if(!ah(dp, "M.ROOT-SERVERS.NET.", "2001:dc3::35")) goto failed; 158 } 159 return dp; 160 failed: 161 delegpt_free_mlc(dp); 162 return 0; 163 } 164 165 /** insert new hint info into hint structure */ 166 static int 167 hints_insert(struct iter_hints* hints, uint16_t c, struct delegpt* dp, 168 int noprime) 169 { 170 struct iter_hints_stub* node = (struct iter_hints_stub*)malloc( 171 sizeof(struct iter_hints_stub)); 172 if(!node) { 173 delegpt_free_mlc(dp); 174 return 0; 175 } 176 node->dp = dp; 177 node->noprime = (uint8_t)noprime; 178 if(!name_tree_insert(&hints->tree, &node->node, dp->name, dp->namelen, 179 dp->namelabs, c)) { 180 char buf[257]; 181 dname_str(dp->name, buf); 182 log_err("second hints for zone %s ignored.", buf); 183 delegpt_free_mlc(dp); 184 free(node); 185 } 186 return 1; 187 } 188 189 /** set stub name */ 190 static struct delegpt* 191 read_stubs_name(struct config_stub* s) 192 { 193 struct delegpt* dp; 194 size_t dname_len; 195 uint8_t* dname; 196 if(!s->name) { 197 log_err("stub zone without a name"); 198 return NULL; 199 } 200 dname = sldns_str2wire_dname(s->name, &dname_len); 201 if(!dname) { 202 log_err("cannot parse stub zone name %s", s->name); 203 return NULL; 204 } 205 if(!(dp=delegpt_create_mlc(dname))) { 206 free(dname); 207 log_err("out of memory"); 208 return NULL; 209 } 210 free(dname); 211 return dp; 212 } 213 214 /** set stub host names */ 215 static int 216 read_stubs_host(struct config_stub* s, struct delegpt* dp) 217 { 218 struct config_strlist* p; 219 size_t dname_len; 220 uint8_t* dname; 221 for(p = s->hosts; p; p = p->next) { 222 log_assert(p->str); 223 dname = sldns_str2wire_dname(p->str, &dname_len); 224 if(!dname) { 225 log_err("cannot parse stub %s nameserver name: '%s'", 226 s->name, p->str); 227 return 0; 228 } 229 if(!delegpt_add_ns_mlc(dp, dname, 0)) { 230 free(dname); 231 log_err("out of memory"); 232 return 0; 233 } 234 free(dname); 235 } 236 return 1; 237 } 238 239 /** set stub server addresses */ 240 static int 241 read_stubs_addr(struct config_stub* s, struct delegpt* dp) 242 { 243 struct config_strlist* p; 244 struct sockaddr_storage addr; 245 socklen_t addrlen; 246 for(p = s->addrs; p; p = p->next) { 247 log_assert(p->str); 248 if(!extstrtoaddr(p->str, &addr, &addrlen)) { 249 log_err("cannot parse stub %s ip address: '%s'", 250 s->name, p->str); 251 return 0; 252 } 253 if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) { 254 log_err("out of memory"); 255 return 0; 256 } 257 } 258 return 1; 259 } 260 261 /** read stubs config */ 262 static int 263 read_stubs(struct iter_hints* hints, struct config_file* cfg) 264 { 265 struct config_stub* s; 266 struct delegpt* dp; 267 for(s = cfg->stubs; s; s = s->next) { 268 if(!(dp=read_stubs_name(s))) 269 return 0; 270 if(!read_stubs_host(s, dp) || !read_stubs_addr(s, dp)) { 271 delegpt_free_mlc(dp); 272 return 0; 273 } 274 /* the flag is turned off for 'stub-first' so that the 275 * last resort will ask for parent-side NS record and thus 276 * fallback to the internet name servers on a failure */ 277 dp->has_parent_side_NS = (uint8_t)!s->isfirst; 278 delegpt_log(VERB_QUERY, dp); 279 if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, !s->isprime)) 280 return 0; 281 } 282 return 1; 283 } 284 285 /** read root hints from file */ 286 static int 287 read_root_hints(struct iter_hints* hints, char* fname) 288 { 289 struct sldns_file_parse_state pstate; 290 struct delegpt* dp; 291 uint8_t rr[LDNS_RR_BUF_SIZE]; 292 size_t rr_len, dname_len; 293 int status; 294 uint16_t c = LDNS_RR_CLASS_IN; 295 FILE* f = fopen(fname, "r"); 296 if(!f) { 297 log_err("could not read root hints %s: %s", 298 fname, strerror(errno)); 299 return 0; 300 } 301 dp = delegpt_create_mlc(NULL); 302 if(!dp) { 303 log_err("out of memory reading root hints"); 304 fclose(f); 305 return 0; 306 } 307 verbose(VERB_QUERY, "Reading root hints from %s", fname); 308 memset(&pstate, 0, sizeof(pstate)); 309 pstate.lineno = 1; 310 dp->has_parent_side_NS = 1; 311 while(!feof(f)) { 312 rr_len = sizeof(rr); 313 dname_len = 0; 314 status = sldns_fp2wire_rr_buf(f, rr, &rr_len, &dname_len, 315 &pstate); 316 if(status != 0) { 317 log_err("reading root hints %s %d:%d: %s", fname, 318 pstate.lineno, LDNS_WIREPARSE_OFFSET(status), 319 sldns_get_errorstr_parse(status)); 320 goto stop_read; 321 } 322 if(rr_len == 0) 323 continue; /* EMPTY line, TTL or ORIGIN */ 324 if(sldns_wirerr_get_type(rr, rr_len, dname_len) 325 == LDNS_RR_TYPE_NS) { 326 if(!delegpt_add_ns_mlc(dp, sldns_wirerr_get_rdata(rr, 327 rr_len, dname_len), 0)) { 328 log_err("out of memory reading root hints"); 329 goto stop_read; 330 } 331 c = sldns_wirerr_get_class(rr, rr_len, dname_len); 332 if(!dp->name) { 333 if(!delegpt_set_name_mlc(dp, rr)) { 334 log_err("out of memory."); 335 goto stop_read; 336 } 337 } 338 } else if(sldns_wirerr_get_type(rr, rr_len, dname_len) 339 == LDNS_RR_TYPE_A && sldns_wirerr_get_rdatalen(rr, 340 rr_len, dname_len) == INET_SIZE) { 341 struct sockaddr_in sa; 342 socklen_t len = (socklen_t)sizeof(sa); 343 memset(&sa, 0, len); 344 sa.sin_family = AF_INET; 345 sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT); 346 memmove(&sa.sin_addr, 347 sldns_wirerr_get_rdata(rr, rr_len, dname_len), 348 INET_SIZE); 349 if(!delegpt_add_target_mlc(dp, rr, dname_len, 350 (struct sockaddr_storage*)&sa, len, 351 0, 0)) { 352 log_err("out of memory reading root hints"); 353 goto stop_read; 354 } 355 } else if(sldns_wirerr_get_type(rr, rr_len, dname_len) 356 == LDNS_RR_TYPE_AAAA && sldns_wirerr_get_rdatalen(rr, 357 rr_len, dname_len) == INET6_SIZE) { 358 struct sockaddr_in6 sa; 359 socklen_t len = (socklen_t)sizeof(sa); 360 memset(&sa, 0, len); 361 sa.sin6_family = AF_INET6; 362 sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT); 363 memmove(&sa.sin6_addr, 364 sldns_wirerr_get_rdata(rr, rr_len, dname_len), 365 INET6_SIZE); 366 if(!delegpt_add_target_mlc(dp, rr, dname_len, 367 (struct sockaddr_storage*)&sa, len, 368 0, 0)) { 369 log_err("out of memory reading root hints"); 370 goto stop_read; 371 } 372 } else { 373 char buf[17]; 374 sldns_wire2str_type_buf(sldns_wirerr_get_type(rr, 375 rr_len, dname_len), buf, sizeof(buf)); 376 log_warn("root hints %s:%d skipping type %s", 377 fname, pstate.lineno, buf); 378 } 379 } 380 fclose(f); 381 if(!dp->name) { 382 log_warn("root hints %s: no NS content", fname); 383 delegpt_free_mlc(dp); 384 return 1; 385 } 386 if(!hints_insert(hints, c, dp, 0)) { 387 return 0; 388 } 389 delegpt_log(VERB_QUERY, dp); 390 return 1; 391 392 stop_read: 393 delegpt_free_mlc(dp); 394 fclose(f); 395 return 0; 396 } 397 398 /** read root hints list */ 399 static int 400 read_root_hints_list(struct iter_hints* hints, struct config_file* cfg) 401 { 402 struct config_strlist* p; 403 for(p = cfg->root_hints; p; p = p->next) { 404 log_assert(p->str); 405 if(p->str && p->str[0]) { 406 char* f = p->str; 407 if(cfg->chrootdir && cfg->chrootdir[0] && 408 strncmp(p->str, cfg->chrootdir, 409 strlen(cfg->chrootdir)) == 0) 410 f += strlen(cfg->chrootdir); 411 if(!read_root_hints(hints, f)) 412 return 0; 413 } 414 } 415 return 1; 416 } 417 418 int 419 hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg) 420 { 421 hints_del_tree(hints); 422 name_tree_init(&hints->tree); 423 424 /* read root hints */ 425 if(!read_root_hints_list(hints, cfg)) 426 return 0; 427 428 /* read stub hints */ 429 if(!read_stubs(hints, cfg)) 430 return 0; 431 432 /* use fallback compiletime root hints */ 433 if(!hints_lookup_root(hints, LDNS_RR_CLASS_IN)) { 434 struct delegpt* dp = compile_time_root_prime(cfg->do_ip4, 435 cfg->do_ip6); 436 verbose(VERB_ALGO, "no config, using builtin root hints."); 437 if(!dp) 438 return 0; 439 if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, 0)) 440 return 0; 441 } 442 443 name_tree_init_parents(&hints->tree); 444 return 1; 445 } 446 447 struct delegpt* 448 hints_lookup_root(struct iter_hints* hints, uint16_t qclass) 449 { 450 uint8_t rootlab = 0; 451 struct iter_hints_stub *stub; 452 stub = (struct iter_hints_stub*)name_tree_find(&hints->tree, 453 &rootlab, 1, 1, qclass); 454 if(!stub) 455 return NULL; 456 return stub->dp; 457 } 458 459 struct iter_hints_stub* 460 hints_lookup_stub(struct iter_hints* hints, uint8_t* qname, 461 uint16_t qclass, struct delegpt* cache_dp) 462 { 463 size_t len; 464 int labs; 465 struct iter_hints_stub *r; 466 467 /* first lookup the stub */ 468 labs = dname_count_size_labels(qname, &len); 469 r = (struct iter_hints_stub*)name_tree_lookup(&hints->tree, qname, 470 len, labs, qclass); 471 if(!r) return NULL; 472 473 /* If there is no cache (root prime situation) */ 474 if(cache_dp == NULL) { 475 if(r->dp->namelabs != 1) 476 return r; /* no cache dp, use any non-root stub */ 477 return NULL; 478 } 479 480 /* 481 * If the stub is same as the delegation we got 482 * And has noprime set, we need to 'prime' to use this stub instead. 483 */ 484 if(r->noprime && query_dname_compare(cache_dp->name, r->dp->name)==0) 485 return r; /* use this stub instead of cached dp */ 486 487 /* 488 * If our cached delegation point is above the hint, we need to prime. 489 */ 490 if(dname_strict_subdomain(r->dp->name, r->dp->namelabs, 491 cache_dp->name, cache_dp->namelabs)) 492 return r; /* need to prime this stub */ 493 return NULL; 494 } 495 496 int hints_next_root(struct iter_hints* hints, uint16_t* qclass) 497 { 498 return name_tree_next_root(&hints->tree, qclass); 499 } 500 501 size_t 502 hints_get_mem(struct iter_hints* hints) 503 { 504 size_t s; 505 struct iter_hints_stub* p; 506 if(!hints) return 0; 507 s = sizeof(*hints); 508 RBTREE_FOR(p, struct iter_hints_stub*, &hints->tree) { 509 s += sizeof(*p) + delegpt_get_mem(p->dp); 510 } 511 return s; 512 } 513 514 int 515 hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp, 516 int noprime) 517 { 518 struct iter_hints_stub *z; 519 if((z=(struct iter_hints_stub*)name_tree_find(&hints->tree, 520 dp->name, dp->namelen, dp->namelabs, c)) != NULL) { 521 (void)rbtree_delete(&hints->tree, &z->node); 522 hints_stub_free(z); 523 } 524 if(!hints_insert(hints, c, dp, noprime)) 525 return 0; 526 name_tree_init_parents(&hints->tree); 527 return 1; 528 } 529 530 void 531 hints_delete_stub(struct iter_hints* hints, uint16_t c, uint8_t* nm) 532 { 533 struct iter_hints_stub *z; 534 size_t len; 535 int labs = dname_count_size_labels(nm, &len); 536 if(!(z=(struct iter_hints_stub*)name_tree_find(&hints->tree, 537 nm, len, labs, c))) 538 return; /* nothing to do */ 539 (void)rbtree_delete(&hints->tree, &z->node); 540 hints_stub_free(z); 541 name_tree_init_parents(&hints->tree); 542 } 543 544