1 /* 2 * services/localzone.c - local zones authority service. 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 enable local zone authority service. 40 */ 41 #include "config.h" 42 #include "services/localzone.h" 43 #include "sldns/str2wire.h" 44 #include "util/regional.h" 45 #include "util/config_file.h" 46 #include "util/data/dname.h" 47 #include "util/data/packed_rrset.h" 48 #include "util/data/msgencode.h" 49 #include "util/net_help.h" 50 #include "util/netevent.h" 51 #include "util/data/msgreply.h" 52 #include "util/data/msgparse.h" 53 #include "util/as112.h" 54 55 /* maximum RRs in an RRset, to cap possible 'endless' list RRs. 56 * with 16 bytes for an A record, a 64K packet has about 4000 max */ 57 #define LOCALZONE_RRSET_COUNT_MAX 4096 58 59 /** print all RRsets in local zone */ 60 static void 61 local_zone_out(struct local_zone* z) 62 { 63 struct local_data* d; 64 struct local_rrset* p; 65 RBTREE_FOR(d, struct local_data*, &z->data) { 66 for(p = d->rrsets; p; p = p->next) { 67 log_nametypeclass(NO_VERBOSE, "rrset", d->name, 68 ntohs(p->rrset->rk.type), 69 ntohs(p->rrset->rk.rrset_class)); 70 } 71 } 72 } 73 74 static void 75 local_zone_print(struct local_zone* z) 76 { 77 char buf[64]; 78 lock_rw_rdlock(&z->lock); 79 snprintf(buf, sizeof(buf), "%s zone", 80 local_zone_type2str(z->type)); 81 log_nametypeclass(NO_VERBOSE, buf, z->name, 0, z->dclass); 82 local_zone_out(z); 83 lock_rw_unlock(&z->lock); 84 } 85 86 void local_zones_print(struct local_zones* zones) 87 { 88 struct local_zone* z; 89 lock_rw_rdlock(&zones->lock); 90 log_info("number of auth zones %u", (unsigned)zones->ztree.count); 91 RBTREE_FOR(z, struct local_zone*, &zones->ztree) { 92 local_zone_print(z); 93 } 94 lock_rw_unlock(&zones->lock); 95 } 96 97 struct local_zones* 98 local_zones_create(void) 99 { 100 struct local_zones* zones = (struct local_zones*)calloc(1, 101 sizeof(*zones)); 102 if(!zones) 103 return NULL; 104 rbtree_init(&zones->ztree, &local_zone_cmp); 105 lock_rw_init(&zones->lock); 106 lock_protect(&zones->lock, &zones->ztree, sizeof(zones->ztree)); 107 /* also lock protects the rbnode's in struct local_zone */ 108 return zones; 109 } 110 111 /** helper traverse to delete zones */ 112 static void 113 lzdel(rbnode_type* n, void* ATTR_UNUSED(arg)) 114 { 115 struct local_zone* z = (struct local_zone*)n->key; 116 local_zone_delete(z); 117 } 118 119 void 120 local_zones_delete(struct local_zones* zones) 121 { 122 if(!zones) 123 return; 124 lock_rw_destroy(&zones->lock); 125 /* walk through zones and delete them all */ 126 traverse_postorder(&zones->ztree, lzdel, NULL); 127 free(zones); 128 } 129 130 void 131 local_zone_delete(struct local_zone* z) 132 { 133 if(!z) 134 return; 135 lock_rw_destroy(&z->lock); 136 regional_destroy(z->region); 137 free(z->name); 138 free(z->taglist); 139 free(z); 140 } 141 142 int 143 local_zone_cmp(const void* z1, const void* z2) 144 { 145 /* first sort on class, so that hierarchy can be maintained within 146 * a class */ 147 struct local_zone* a = (struct local_zone*)z1; 148 struct local_zone* b = (struct local_zone*)z2; 149 int m; 150 if(a->dclass != b->dclass) { 151 if(a->dclass < b->dclass) 152 return -1; 153 return 1; 154 } 155 return dname_lab_cmp(a->name, a->namelabs, b->name, b->namelabs, &m); 156 } 157 158 int 159 local_data_cmp(const void* d1, const void* d2) 160 { 161 struct local_data* a = (struct local_data*)d1; 162 struct local_data* b = (struct local_data*)d2; 163 int m; 164 return dname_canon_lab_cmp(a->name, a->namelabs, b->name, 165 b->namelabs, &m); 166 } 167 168 /* form wireformat from text format domain name */ 169 int 170 parse_dname(const char* str, uint8_t** res, size_t* len, int* labs) 171 { 172 *res = sldns_str2wire_dname(str, len); 173 *labs = 0; 174 if(!*res) { 175 log_err("cannot parse name %s", str); 176 return 0; 177 } 178 *labs = dname_count_size_labels(*res, len); 179 return 1; 180 } 181 182 /** create a new localzone */ 183 static struct local_zone* 184 local_zone_create(uint8_t* nm, size_t len, int labs, 185 enum localzone_type t, uint16_t dclass) 186 { 187 struct local_zone* z = (struct local_zone*)calloc(1, sizeof(*z)); 188 if(!z) { 189 return NULL; 190 } 191 z->node.key = z; 192 z->dclass = dclass; 193 z->type = t; 194 z->name = nm; 195 z->namelen = len; 196 z->namelabs = labs; 197 lock_rw_init(&z->lock); 198 z->region = regional_create_nochunk(sizeof(struct regional)); 199 if(!z->region) { 200 free(z); 201 return NULL; 202 } 203 rbtree_init(&z->data, &local_data_cmp); 204 lock_protect(&z->lock, &z->parent, sizeof(*z)-sizeof(rbnode_type)); 205 /* also the zones->lock protects node, parent, name*, class */ 206 return z; 207 } 208 209 /** enter a new zone with allocated dname returns with WRlock */ 210 static struct local_zone* 211 lz_enter_zone_dname(struct local_zones* zones, uint8_t* nm, size_t len, 212 int labs, enum localzone_type t, uint16_t c) 213 { 214 struct local_zone* z = local_zone_create(nm, len, labs, t, c); 215 if(!z) { 216 free(nm); 217 log_err("out of memory"); 218 return NULL; 219 } 220 221 /* add to rbtree */ 222 lock_rw_wrlock(&zones->lock); 223 lock_rw_wrlock(&z->lock); 224 if(!rbtree_insert(&zones->ztree, &z->node)) { 225 struct local_zone* oldz; 226 char str[256]; 227 dname_str(nm, str); 228 log_warn("duplicate local-zone %s", str); 229 lock_rw_unlock(&z->lock); 230 /* save zone name locally before deallocation, 231 * otherwise, nm is gone if we zone_delete now. */ 232 oldz = z; 233 /* find the correct zone, so not an error for duplicate */ 234 z = local_zones_find(zones, nm, len, labs, c); 235 lock_rw_wrlock(&z->lock); 236 lock_rw_unlock(&zones->lock); 237 local_zone_delete(oldz); 238 return z; 239 } 240 lock_rw_unlock(&zones->lock); 241 return z; 242 } 243 244 /** enter a new zone */ 245 static struct local_zone* 246 lz_enter_zone(struct local_zones* zones, const char* name, const char* type, 247 uint16_t dclass) 248 { 249 struct local_zone* z; 250 enum localzone_type t; 251 uint8_t* nm; 252 size_t len; 253 int labs; 254 if(!parse_dname(name, &nm, &len, &labs)) { 255 log_err("bad zone name %s %s", name, type); 256 return NULL; 257 } 258 if(!local_zone_str2type(type, &t)) { 259 log_err("bad lz_enter_zone type %s %s", name, type); 260 free(nm); 261 return NULL; 262 } 263 if(!(z=lz_enter_zone_dname(zones, nm, len, labs, t, dclass))) { 264 log_err("could not enter zone %s %s", name, type); 265 return NULL; 266 } 267 return z; 268 } 269 270 int 271 rrstr_get_rr_content(const char* str, uint8_t** nm, uint16_t* type, 272 uint16_t* dclass, time_t* ttl, uint8_t* rr, size_t len, 273 uint8_t** rdata, size_t* rdata_len) 274 { 275 size_t dname_len = 0; 276 int e = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600, 277 NULL, 0, NULL, 0); 278 if(e) { 279 log_err("error parsing local-data at %d: '%s': %s", 280 LDNS_WIREPARSE_OFFSET(e), str, 281 sldns_get_errorstr_parse(e)); 282 return 0; 283 } 284 *nm = memdup(rr, dname_len); 285 if(!*nm) { 286 log_err("out of memory"); 287 return 0; 288 } 289 *dclass = sldns_wirerr_get_class(rr, len, dname_len); 290 *type = sldns_wirerr_get_type(rr, len, dname_len); 291 *ttl = (time_t)sldns_wirerr_get_ttl(rr, len, dname_len); 292 *rdata = sldns_wirerr_get_rdatawl(rr, len, dname_len); 293 *rdata_len = sldns_wirerr_get_rdatalen(rr, len, dname_len)+2; 294 return 1; 295 } 296 297 /** return name and class of rr; parses string */ 298 static int 299 get_rr_nameclass(const char* str, uint8_t** nm, uint16_t* dclass, 300 uint16_t* dtype) 301 { 302 uint8_t rr[LDNS_RR_BUF_SIZE]; 303 size_t len = sizeof(rr), dname_len = 0; 304 int s = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600, 305 NULL, 0, NULL, 0); 306 if(s != 0) { 307 log_err("error parsing local-data at %d '%s': %s", 308 LDNS_WIREPARSE_OFFSET(s), str, 309 sldns_get_errorstr_parse(s)); 310 return 0; 311 } 312 *nm = memdup(rr, dname_len); 313 *dclass = sldns_wirerr_get_class(rr, len, dname_len); 314 *dtype = sldns_wirerr_get_type(rr, len, dname_len); 315 if(!*nm) { 316 log_err("out of memory"); 317 return 0; 318 } 319 return 1; 320 } 321 322 /** 323 * Find an rrset in local data structure. 324 * @param data: local data domain name structure. 325 * @param type: type to look for (host order). 326 * @param alias_ok: 1 if matching a non-exact, alias type such as CNAME is 327 * allowed. otherwise 0. 328 * @return rrset pointer or NULL if not found. 329 */ 330 static struct local_rrset* 331 local_data_find_type(struct local_data* data, uint16_t type, int alias_ok) 332 { 333 struct local_rrset* p, *cname = NULL; 334 type = htons(type); 335 for(p = data->rrsets; p; p = p->next) { 336 if(p->rrset->rk.type == type) 337 return p; 338 if(alias_ok && p->rrset->rk.type == htons(LDNS_RR_TYPE_CNAME)) 339 cname = p; 340 } 341 if(alias_ok) 342 return cname; 343 return NULL; 344 } 345 346 /** check for RR duplicates */ 347 static int 348 rr_is_duplicate(struct packed_rrset_data* pd, uint8_t* rdata, size_t rdata_len) 349 { 350 size_t i; 351 for(i=0; i<pd->count; i++) { 352 if(pd->rr_len[i] == rdata_len && 353 memcmp(pd->rr_data[i], rdata, rdata_len) == 0) 354 return 1; 355 } 356 return 0; 357 } 358 359 /** new local_rrset */ 360 static struct local_rrset* 361 new_local_rrset(struct regional* region, struct local_data* node, 362 uint16_t rrtype, uint16_t rrclass) 363 { 364 struct packed_rrset_data* pd; 365 struct local_rrset* rrset = (struct local_rrset*) 366 regional_alloc_zero(region, sizeof(*rrset)); 367 if(!rrset) { 368 log_err("out of memory"); 369 return NULL; 370 } 371 rrset->next = node->rrsets; 372 node->rrsets = rrset; 373 rrset->rrset = (struct ub_packed_rrset_key*) 374 regional_alloc_zero(region, sizeof(*rrset->rrset)); 375 if(!rrset->rrset) { 376 log_err("out of memory"); 377 return NULL; 378 } 379 rrset->rrset->entry.key = rrset->rrset; 380 pd = (struct packed_rrset_data*)regional_alloc_zero(region, 381 sizeof(*pd)); 382 if(!pd) { 383 log_err("out of memory"); 384 return NULL; 385 } 386 pd->trust = rrset_trust_prim_noglue; 387 pd->security = sec_status_insecure; 388 rrset->rrset->entry.data = pd; 389 rrset->rrset->rk.dname = node->name; 390 rrset->rrset->rk.dname_len = node->namelen; 391 rrset->rrset->rk.type = htons(rrtype); 392 rrset->rrset->rk.rrset_class = htons(rrclass); 393 return rrset; 394 } 395 396 /** insert RR into RRset data structure; Wastes a couple of bytes */ 397 int 398 rrset_insert_rr(struct regional* region, struct packed_rrset_data* pd, 399 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr) 400 { 401 size_t* oldlen = pd->rr_len; 402 time_t* oldttl = pd->rr_ttl; 403 uint8_t** olddata = pd->rr_data; 404 405 /* add RR to rrset */ 406 if(pd->count > LOCALZONE_RRSET_COUNT_MAX) { 407 log_warn("RRset '%s' has more than %d records, record ignored", 408 rrstr, LOCALZONE_RRSET_COUNT_MAX); 409 return 1; 410 } 411 pd->count++; 412 pd->rr_len = regional_alloc(region, sizeof(*pd->rr_len)*pd->count); 413 pd->rr_ttl = regional_alloc(region, sizeof(*pd->rr_ttl)*pd->count); 414 pd->rr_data = regional_alloc(region, sizeof(*pd->rr_data)*pd->count); 415 if(!pd->rr_len || !pd->rr_ttl || !pd->rr_data) { 416 log_err("out of memory"); 417 return 0; 418 } 419 if(pd->count > 1) { 420 memcpy(pd->rr_len+1, oldlen, 421 sizeof(*pd->rr_len)*(pd->count-1)); 422 memcpy(pd->rr_ttl+1, oldttl, 423 sizeof(*pd->rr_ttl)*(pd->count-1)); 424 memcpy(pd->rr_data+1, olddata, 425 sizeof(*pd->rr_data)*(pd->count-1)); 426 } 427 pd->rr_len[0] = rdata_len; 428 pd->rr_ttl[0] = ttl; 429 pd->rr_data[0] = regional_alloc_init(region, rdata, rdata_len); 430 if(!pd->rr_data[0]) { 431 log_err("out of memory"); 432 return 0; 433 } 434 return 1; 435 } 436 437 /** Delete RR from local-zone RRset, wastes memory as the deleted RRs cannot be 438 * free'd (regionally alloc'd) */ 439 int 440 local_rrset_remove_rr(struct packed_rrset_data* pd, size_t index) 441 { 442 log_assert(pd->count > 0); 443 if(index >= pd->count) { 444 log_warn("Trying to remove RR with out of bound index"); 445 return 0; 446 } 447 if(index + 1 < pd->count) { 448 /* not removing last element */ 449 size_t nexti = index + 1; 450 size_t num = pd->count - nexti; 451 memmove(pd->rr_len+index, pd->rr_len+nexti, sizeof(*pd->rr_len)*num); 452 memmove(pd->rr_ttl+index, pd->rr_ttl+nexti, sizeof(*pd->rr_ttl)*num); 453 memmove(pd->rr_data+index, pd->rr_data+nexti, sizeof(*pd->rr_data)*num); 454 } 455 pd->count--; 456 return 1; 457 } 458 459 struct local_data* 460 local_zone_find_data(struct local_zone* z, uint8_t* nm, size_t nmlen, int nmlabs) 461 { 462 struct local_data key; 463 key.node.key = &key; 464 key.name = nm; 465 key.namelen = nmlen; 466 key.namelabs = nmlabs; 467 return (struct local_data*)rbtree_search(&z->data, &key.node); 468 } 469 470 /** find a node, create it if not and all its empty nonterminal parents */ 471 static int 472 lz_find_create_node(struct local_zone* z, uint8_t* nm, size_t nmlen, 473 int nmlabs, struct local_data** res) 474 { 475 struct local_data* ld = local_zone_find_data(z, nm, nmlen, nmlabs); 476 if(!ld) { 477 /* create a domain name to store rr. */ 478 ld = (struct local_data*)regional_alloc_zero(z->region, 479 sizeof(*ld)); 480 if(!ld) { 481 log_err("out of memory adding local data"); 482 return 0; 483 } 484 ld->node.key = ld; 485 ld->name = regional_alloc_init(z->region, nm, nmlen); 486 if(!ld->name) { 487 log_err("out of memory"); 488 return 0; 489 } 490 ld->namelen = nmlen; 491 ld->namelabs = nmlabs; 492 if(!rbtree_insert(&z->data, &ld->node)) { 493 log_assert(0); /* duplicate name */ 494 } 495 /* see if empty nonterminals need to be created */ 496 if(nmlabs > z->namelabs) { 497 dname_remove_label(&nm, &nmlen); 498 if(!lz_find_create_node(z, nm, nmlen, nmlabs-1, res)) 499 return 0; 500 } 501 } 502 *res = ld; 503 return 1; 504 } 505 506 /* Mark the SOA record for the zone. This only marks the SOA rrset; the data 507 * for the RR is entered later on local_zone_enter_rr() as with the other 508 * records. An artificial soa_negative record with a modified TTL (minimum of 509 * the TTL and the SOA.MINIMUM) is also created and marked for usage with 510 * negative answers and to avoid allocations during those answers. */ 511 static int 512 lz_mark_soa_for_zone(struct local_zone* z, struct ub_packed_rrset_key* soa_rrset, 513 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr) 514 { 515 struct packed_rrset_data* pd = (struct packed_rrset_data*) 516 regional_alloc_zero(z->region, sizeof(*pd)); 517 struct ub_packed_rrset_key* rrset_negative = (struct ub_packed_rrset_key*) 518 regional_alloc_zero(z->region, sizeof(*rrset_negative)); 519 time_t minimum; 520 if(!rrset_negative||!pd) { 521 log_err("out of memory"); 522 return 0; 523 } 524 /* Mark the original SOA record and then continue with the negative one. */ 525 z->soa = soa_rrset; 526 rrset_negative->entry.key = rrset_negative; 527 pd->trust = rrset_trust_prim_noglue; 528 pd->security = sec_status_insecure; 529 rrset_negative->entry.data = pd; 530 rrset_negative->rk.dname = soa_rrset->rk.dname; 531 rrset_negative->rk.dname_len = soa_rrset->rk.dname_len; 532 rrset_negative->rk.type = soa_rrset->rk.type; 533 rrset_negative->rk.rrset_class = soa_rrset->rk.rrset_class; 534 if(!rrset_insert_rr(z->region, pd, rdata, rdata_len, ttl, rrstr)) 535 return 0; 536 /* last 4 bytes are minimum ttl in network format */ 537 if(pd->count == 0 || pd->rr_len[0] < 2+4) 538 return 0; 539 minimum = (time_t)sldns_read_uint32(pd->rr_data[0]+(pd->rr_len[0]-4)); 540 minimum = ttl<minimum?ttl:minimum; 541 pd->ttl = minimum; 542 pd->rr_ttl[0] = minimum; 543 544 z->soa_negative = rrset_negative; 545 return 1; 546 } 547 548 int 549 local_zone_enter_rr(struct local_zone* z, uint8_t* nm, size_t nmlen, 550 int nmlabs, uint16_t rrtype, uint16_t rrclass, time_t ttl, 551 uint8_t* rdata, size_t rdata_len, const char* rrstr) 552 { 553 struct local_data* node; 554 struct local_rrset* rrset; 555 struct packed_rrset_data* pd; 556 557 if(!lz_find_create_node(z, nm, nmlen, nmlabs, &node)) { 558 return 0; 559 } 560 log_assert(node); 561 562 /* Reject it if we would end up having CNAME and other data (including 563 * another CNAME) for a redirect zone. */ 564 if((z->type == local_zone_redirect || 565 z->type == local_zone_inform_redirect) && node->rrsets) { 566 const char* othertype = NULL; 567 if (rrtype == LDNS_RR_TYPE_CNAME) 568 othertype = "other"; 569 else if (node->rrsets->rrset->rk.type == 570 htons(LDNS_RR_TYPE_CNAME)) { 571 othertype = "CNAME"; 572 } 573 if(othertype) { 574 log_err("local-data '%s' in redirect zone must not " 575 "coexist with %s local-data", rrstr, othertype); 576 return 0; 577 } 578 } 579 rrset = local_data_find_type(node, rrtype, 0); 580 if(!rrset) { 581 rrset = new_local_rrset(z->region, node, rrtype, rrclass); 582 if(!rrset) 583 return 0; 584 if(query_dname_compare(node->name, z->name) == 0) { 585 if(rrtype == LDNS_RR_TYPE_NSEC) 586 rrset->rrset->rk.flags = PACKED_RRSET_NSEC_AT_APEX; 587 if(rrtype == LDNS_RR_TYPE_SOA && 588 !lz_mark_soa_for_zone(z, rrset->rrset, rdata, rdata_len, ttl, 589 rrstr)) 590 return 0; 591 } 592 } 593 pd = (struct packed_rrset_data*)rrset->rrset->entry.data; 594 log_assert(rrset && pd); 595 596 /* check for duplicate RR */ 597 if(rr_is_duplicate(pd, rdata, rdata_len)) { 598 verbose(VERB_ALGO, "ignoring duplicate RR: %s", rrstr); 599 return 1; 600 } 601 return rrset_insert_rr(z->region, pd, rdata, rdata_len, ttl, rrstr); 602 } 603 604 /** enter data RR into auth zone */ 605 static int 606 lz_enter_rr_into_zone(struct local_zone* z, const char* rrstr) 607 { 608 uint8_t* nm; 609 size_t nmlen; 610 int nmlabs, ret; 611 uint16_t rrtype = 0, rrclass = 0; 612 time_t ttl = 0; 613 uint8_t rr[LDNS_RR_BUF_SIZE]; 614 uint8_t* rdata; 615 size_t rdata_len; 616 if(!rrstr_get_rr_content(rrstr, &nm, &rrtype, &rrclass, &ttl, rr, 617 sizeof(rr), &rdata, &rdata_len)) { 618 log_err("bad local-data: %s", rrstr); 619 return 0; 620 } 621 log_assert(z->dclass == rrclass); 622 if((z->type == local_zone_redirect || 623 z->type == local_zone_inform_redirect) && 624 query_dname_compare(z->name, nm) != 0) { 625 log_err("local-data in redirect zone must reside at top of zone" 626 ", not at %s", rrstr); 627 free(nm); 628 return 0; 629 } 630 nmlabs = dname_count_size_labels(nm, &nmlen); 631 ret = local_zone_enter_rr(z, nm, nmlen, nmlabs, rrtype, rrclass, ttl, 632 rdata, rdata_len, rrstr); 633 free(nm); 634 return ret; 635 } 636 637 /** enter a data RR into auth data; a zone for it must exist */ 638 static int 639 lz_enter_rr_str(struct local_zones* zones, const char* rr) 640 { 641 uint8_t* rr_name; 642 uint16_t rr_class, rr_type; 643 size_t len; 644 int labs; 645 struct local_zone* z; 646 int r; 647 if(!get_rr_nameclass(rr, &rr_name, &rr_class, &rr_type)) { 648 log_err("bad rr %s", rr); 649 return 0; 650 } 651 labs = dname_count_size_labels(rr_name, &len); 652 lock_rw_rdlock(&zones->lock); 653 z = local_zones_lookup(zones, rr_name, len, labs, rr_class, rr_type); 654 if(!z) { 655 lock_rw_unlock(&zones->lock); 656 fatal_exit("internal error: no zone for rr %s", rr); 657 } 658 lock_rw_wrlock(&z->lock); 659 lock_rw_unlock(&zones->lock); 660 free(rr_name); 661 r = lz_enter_rr_into_zone(z, rr); 662 lock_rw_unlock(&z->lock); 663 return r; 664 } 665 666 /** enter tagstring into zone */ 667 static int 668 lz_enter_zone_tag(struct local_zones* zones, char* zname, uint8_t* list, 669 size_t len, uint16_t rr_class) 670 { 671 uint8_t dname[LDNS_MAX_DOMAINLEN+1]; 672 size_t dname_len = sizeof(dname); 673 int dname_labs, r = 0; 674 struct local_zone* z; 675 676 if(sldns_str2wire_dname_buf(zname, dname, &dname_len) != 0) { 677 log_err("cannot parse zone name in local-zone-tag: %s", zname); 678 return 0; 679 } 680 dname_labs = dname_count_labels(dname); 681 682 lock_rw_rdlock(&zones->lock); 683 z = local_zones_find(zones, dname, dname_len, dname_labs, rr_class); 684 if(!z) { 685 lock_rw_unlock(&zones->lock); 686 log_err("no local-zone for tag %s", zname); 687 return 0; 688 } 689 lock_rw_wrlock(&z->lock); 690 lock_rw_unlock(&zones->lock); 691 free(z->taglist); 692 z->taglist = memdup(list, len); 693 z->taglen = len; 694 if(z->taglist) 695 r = 1; 696 lock_rw_unlock(&z->lock); 697 return r; 698 } 699 700 /** enter override into zone */ 701 static int 702 lz_enter_override(struct local_zones* zones, char* zname, char* netblock, 703 char* type, uint16_t rr_class) 704 { 705 uint8_t dname[LDNS_MAX_DOMAINLEN+1]; 706 size_t dname_len = sizeof(dname); 707 int dname_labs; 708 struct sockaddr_storage addr; 709 int net; 710 socklen_t addrlen; 711 struct local_zone* z; 712 enum localzone_type t; 713 714 /* parse zone name */ 715 if(sldns_str2wire_dname_buf(zname, dname, &dname_len) != 0) { 716 log_err("cannot parse zone name in local-zone-override: %s %s", 717 zname, netblock); 718 return 0; 719 } 720 dname_labs = dname_count_labels(dname); 721 722 /* parse netblock */ 723 if(!netblockstrtoaddr(netblock, UNBOUND_DNS_PORT, &addr, &addrlen, 724 &net)) { 725 log_err("cannot parse netblock in local-zone-override: %s %s", 726 zname, netblock); 727 return 0; 728 } 729 730 /* parse zone type */ 731 if(!local_zone_str2type(type, &t)) { 732 log_err("cannot parse type in local-zone-override: %s %s %s", 733 zname, netblock, type); 734 return 0; 735 } 736 737 /* find localzone entry */ 738 lock_rw_rdlock(&zones->lock); 739 z = local_zones_find(zones, dname, dname_len, dname_labs, rr_class); 740 if(!z) { 741 lock_rw_unlock(&zones->lock); 742 log_err("no local-zone for local-zone-override %s", zname); 743 return 0; 744 } 745 lock_rw_wrlock(&z->lock); 746 lock_rw_unlock(&zones->lock); 747 748 /* create netblock addr_tree if not present yet */ 749 if(!z->override_tree) { 750 z->override_tree = (struct rbtree_type*)regional_alloc_zero( 751 z->region, sizeof(*z->override_tree)); 752 if(!z->override_tree) { 753 lock_rw_unlock(&z->lock); 754 log_err("out of memory"); 755 return 0; 756 } 757 addr_tree_init(z->override_tree); 758 } 759 /* add new elem to tree */ 760 if(z->override_tree) { 761 struct local_zone_override* n; 762 n = (struct local_zone_override*)regional_alloc_zero( 763 z->region, sizeof(*n)); 764 if(!n) { 765 lock_rw_unlock(&z->lock); 766 log_err("out of memory"); 767 return 0; 768 } 769 n->type = t; 770 if(!addr_tree_insert(z->override_tree, 771 (struct addr_tree_node*)n, &addr, addrlen, net)) { 772 lock_rw_unlock(&z->lock); 773 log_err("duplicate local-zone-override %s %s", 774 zname, netblock); 775 return 1; 776 } 777 } 778 779 lock_rw_unlock(&z->lock); 780 return 1; 781 } 782 783 /** parse local-zone: statements */ 784 static int 785 lz_enter_zones(struct local_zones* zones, struct config_file* cfg) 786 { 787 struct config_str2list* p; 788 #ifndef THREADS_DISABLED 789 struct local_zone* z; 790 #endif 791 for(p = cfg->local_zones; p; p = p->next) { 792 if(!( 793 #ifndef THREADS_DISABLED 794 z= 795 #endif 796 lz_enter_zone(zones, p->str, p->str2, 797 LDNS_RR_CLASS_IN))) 798 return 0; 799 lock_rw_unlock(&z->lock); 800 } 801 return 1; 802 } 803 804 /** lookup a zone in rbtree; exact match only; SLOW due to parse */ 805 static int 806 lz_exists(struct local_zones* zones, const char* name) 807 { 808 struct local_zone z; 809 z.node.key = &z; 810 z.dclass = LDNS_RR_CLASS_IN; 811 if(!parse_dname(name, &z.name, &z.namelen, &z.namelabs)) { 812 log_err("bad name %s", name); 813 return 0; 814 } 815 lock_rw_rdlock(&zones->lock); 816 if(rbtree_search(&zones->ztree, &z.node)) { 817 lock_rw_unlock(&zones->lock); 818 free(z.name); 819 return 1; 820 } 821 lock_rw_unlock(&zones->lock); 822 free(z.name); 823 return 0; 824 } 825 826 /** lookup a zone in cfg->nodefault list */ 827 static int 828 lz_nodefault(struct config_file* cfg, const char* name) 829 { 830 struct config_strlist* p; 831 size_t len = strlen(name); 832 if(len == 0) return 0; 833 if(name[len-1] == '.') len--; 834 835 for(p = cfg->local_zones_nodefault; p; p = p->next) { 836 /* compare zone name, lowercase, compare without ending . */ 837 if(strncasecmp(p->str, name, len) == 0 && 838 (strlen(p->str) == len || (strlen(p->str)==len+1 && 839 p->str[len] == '.'))) 840 return 1; 841 } 842 return 0; 843 } 844 845 /** enter (AS112) empty default zone */ 846 static int 847 add_empty_default(struct local_zones* zones, struct config_file* cfg, 848 const char* name) 849 { 850 struct local_zone* z; 851 char str[1024]; /* known long enough */ 852 if(lz_exists(zones, name) || lz_nodefault(cfg, name)) 853 return 1; /* do not enter default content */ 854 if(!(z=lz_enter_zone(zones, name, "static", LDNS_RR_CLASS_IN))) 855 return 0; 856 snprintf(str, sizeof(str), "%s 10800 IN SOA localhost. " 857 "nobody.invalid. 1 3600 1200 604800 10800", name); 858 if(!lz_enter_rr_into_zone(z, str)) { 859 lock_rw_unlock(&z->lock); 860 return 0; 861 } 862 snprintf(str, sizeof(str), "%s 10800 IN NS localhost. ", name); 863 if(!lz_enter_rr_into_zone(z, str)) { 864 lock_rw_unlock(&z->lock); 865 return 0; 866 } 867 lock_rw_unlock(&z->lock); 868 return 1; 869 } 870 871 /** enter default zones */ 872 int local_zone_enter_defaults(struct local_zones* zones, struct config_file* cfg) 873 { 874 struct local_zone* z; 875 const char** zstr; 876 877 /* Do not add any default */ 878 if(cfg->local_zones_disable_default) 879 return 1; 880 881 /* this list of zones is from RFC 6303 and RFC 7686 */ 882 883 /* block localhost level zones first, then onion and later the LAN zones */ 884 885 /* localhost. zone */ 886 if(!lz_exists(zones, "localhost.") && 887 !lz_nodefault(cfg, "localhost.")) { 888 if(!(z=lz_enter_zone(zones, "localhost.", "redirect", 889 LDNS_RR_CLASS_IN)) || 890 !lz_enter_rr_into_zone(z, 891 "localhost. 10800 IN NS localhost.") || 892 !lz_enter_rr_into_zone(z, 893 "localhost. 10800 IN SOA localhost. nobody.invalid. " 894 "1 3600 1200 604800 10800") || 895 !lz_enter_rr_into_zone(z, 896 "localhost. 10800 IN A 127.0.0.1") || 897 !lz_enter_rr_into_zone(z, 898 "localhost. 10800 IN AAAA ::1")) { 899 log_err("out of memory adding default zone"); 900 if(z) { lock_rw_unlock(&z->lock); } 901 return 0; 902 } 903 lock_rw_unlock(&z->lock); 904 } 905 /* reverse ip4 zone */ 906 if(!lz_exists(zones, "127.in-addr.arpa.") && 907 !lz_nodefault(cfg, "127.in-addr.arpa.")) { 908 if(!(z=lz_enter_zone(zones, "127.in-addr.arpa.", "static", 909 LDNS_RR_CLASS_IN)) || 910 !lz_enter_rr_into_zone(z, 911 "127.in-addr.arpa. 10800 IN NS localhost.") || 912 !lz_enter_rr_into_zone(z, 913 "127.in-addr.arpa. 10800 IN SOA localhost. " 914 "nobody.invalid. 1 3600 1200 604800 10800") || 915 !lz_enter_rr_into_zone(z, 916 "1.0.0.127.in-addr.arpa. 10800 IN PTR localhost.")) { 917 log_err("out of memory adding default zone"); 918 if(z) { lock_rw_unlock(&z->lock); } 919 return 0; 920 } 921 lock_rw_unlock(&z->lock); 922 } 923 /* reverse ip6 zone */ 924 if(!lz_exists(zones, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.") && 925 !lz_nodefault(cfg, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.")) { 926 if(!(z=lz_enter_zone(zones, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", "static", 927 LDNS_RR_CLASS_IN)) || 928 !lz_enter_rr_into_zone(z, 929 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. 10800 IN NS localhost.") || 930 !lz_enter_rr_into_zone(z, 931 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. 10800 IN SOA localhost. " 932 "nobody.invalid. 1 3600 1200 604800 10800") || 933 !lz_enter_rr_into_zone(z, 934 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. 10800 IN PTR localhost.")) { 935 log_err("out of memory adding default zone"); 936 if(z) { lock_rw_unlock(&z->lock); } 937 return 0; 938 } 939 lock_rw_unlock(&z->lock); 940 } 941 /* home.arpa. zone (RFC 8375) */ 942 if(!add_empty_default(zones, cfg, "home.arpa.")) { 943 log_err("out of memory adding default zone"); 944 return 0; 945 } 946 /* onion. zone (RFC 7686) */ 947 if(!add_empty_default(zones, cfg, "onion.")) { 948 log_err("out of memory adding default zone"); 949 return 0; 950 } 951 /* test. zone (RFC 6761) */ 952 if(!add_empty_default(zones, cfg, "test.")) { 953 log_err("out of memory adding default zone"); 954 return 0; 955 } 956 /* invalid. zone (RFC 6761) */ 957 if(!add_empty_default(zones, cfg, "invalid.")) { 958 log_err("out of memory adding default zone"); 959 return 0; 960 } 961 /* block AS112 zones, unless asked not to */ 962 if(!cfg->unblock_lan_zones) { 963 for(zstr = as112_zones; *zstr; zstr++) { 964 if(!add_empty_default(zones, cfg, *zstr)) { 965 log_err("out of memory adding default zone"); 966 return 0; 967 } 968 } 969 } 970 return 1; 971 } 972 973 /** parse local-zone-override: statements */ 974 static int 975 lz_enter_overrides(struct local_zones* zones, struct config_file* cfg) 976 { 977 struct config_str3list* p; 978 for(p = cfg->local_zone_overrides; p; p = p->next) { 979 if(!lz_enter_override(zones, p->str, p->str2, p->str3, 980 LDNS_RR_CLASS_IN)) 981 return 0; 982 } 983 return 1; 984 } 985 986 /** setup parent pointers, so that a lookup can be done for closest match */ 987 static void 988 init_parents(struct local_zones* zones) 989 { 990 struct local_zone* node, *prev = NULL, *p; 991 int m; 992 lock_rw_wrlock(&zones->lock); 993 RBTREE_FOR(node, struct local_zone*, &zones->ztree) { 994 lock_rw_wrlock(&node->lock); 995 node->parent = NULL; 996 if(!prev || prev->dclass != node->dclass) { 997 prev = node; 998 lock_rw_unlock(&node->lock); 999 continue; 1000 } 1001 (void)dname_lab_cmp(prev->name, prev->namelabs, node->name, 1002 node->namelabs, &m); /* we know prev is smaller */ 1003 /* sort order like: . com. bla.com. zwb.com. net. */ 1004 /* find the previous, or parent-parent-parent */ 1005 for(p = prev; p; p = p->parent) 1006 /* looking for name with few labels, a parent */ 1007 if(p->namelabs <= m) { 1008 /* ==: since prev matched m, this is closest*/ 1009 /* <: prev matches more, but is not a parent, 1010 * this one is a (grand)parent */ 1011 node->parent = p; 1012 break; 1013 } 1014 prev = node; 1015 1016 if(node->override_tree) 1017 addr_tree_init_parents(node->override_tree); 1018 lock_rw_unlock(&node->lock); 1019 } 1020 lock_rw_unlock(&zones->lock); 1021 } 1022 1023 /** enter implicit transparent zone for local-data: without local-zone: */ 1024 static int 1025 lz_setup_implicit(struct local_zones* zones, struct config_file* cfg) 1026 { 1027 /* walk over all items that have no parent zone and find 1028 * the name that covers them all (could be the root) and 1029 * add that as a transparent zone */ 1030 struct config_strlist* p; 1031 int have_name = 0; 1032 int have_other_classes = 0; 1033 uint16_t dclass = 0; 1034 uint8_t* nm = 0; 1035 size_t nmlen = 0; 1036 int nmlabs = 0; 1037 int match = 0; /* number of labels match count */ 1038 1039 init_parents(zones); /* to enable local_zones_lookup() */ 1040 for(p = cfg->local_data; p; p = p->next) { 1041 uint8_t* rr_name; 1042 uint16_t rr_class, rr_type; 1043 size_t len; 1044 int labs; 1045 if(!get_rr_nameclass(p->str, &rr_name, &rr_class, &rr_type)) { 1046 log_err("Bad local-data RR %s", p->str); 1047 return 0; 1048 } 1049 labs = dname_count_size_labels(rr_name, &len); 1050 lock_rw_rdlock(&zones->lock); 1051 if(!local_zones_lookup(zones, rr_name, len, labs, rr_class, 1052 rr_type)) { 1053 /* Check if there is a zone that this could go 1054 * under but for different class; created zones are 1055 * always for LDNS_RR_CLASS_IN. Create the zone with 1056 * a different class but the same configured 1057 * local_zone_type. */ 1058 struct local_zone* z = local_zones_lookup(zones, 1059 rr_name, len, labs, LDNS_RR_CLASS_IN, rr_type); 1060 if(z) { 1061 uint8_t* name = memdup(z->name, z->namelen); 1062 size_t znamelen = z->namelen; 1063 int znamelabs = z->namelabs; 1064 enum localzone_type ztype = z->type; 1065 lock_rw_unlock(&zones->lock); 1066 if(!name) { 1067 log_err("out of memory"); 1068 free(rr_name); 1069 return 0; 1070 } 1071 if(!( 1072 #ifndef THREADS_DISABLED 1073 z = 1074 #endif 1075 lz_enter_zone_dname(zones, name, 1076 znamelen, znamelabs, 1077 ztype, rr_class))) { 1078 free(rr_name); 1079 return 0; 1080 } 1081 lock_rw_unlock(&z->lock); 1082 free(rr_name); 1083 continue; 1084 } 1085 if(!have_name) { 1086 dclass = rr_class; 1087 nm = rr_name; 1088 nmlen = len; 1089 nmlabs = labs; 1090 match = labs; 1091 have_name = 1; 1092 } else { 1093 int m; 1094 if(rr_class != dclass) { 1095 /* process other classes later */ 1096 free(rr_name); 1097 have_other_classes = 1; 1098 lock_rw_unlock(&zones->lock); 1099 continue; 1100 } 1101 /* find smallest shared topdomain */ 1102 (void)dname_lab_cmp(nm, nmlabs, 1103 rr_name, labs, &m); 1104 free(rr_name); 1105 if(m < match) 1106 match = m; 1107 } 1108 } else free(rr_name); 1109 lock_rw_unlock(&zones->lock); 1110 } 1111 if(have_name) { 1112 uint8_t* n2; 1113 #ifndef THREADS_DISABLED 1114 struct local_zone* z; 1115 #endif 1116 /* allocate zone of smallest shared topdomain to contain em */ 1117 n2 = nm; 1118 dname_remove_labels(&n2, &nmlen, nmlabs - match); 1119 n2 = memdup(n2, nmlen); 1120 free(nm); 1121 if(!n2) { 1122 log_err("out of memory"); 1123 return 0; 1124 } 1125 log_nametypeclass(VERB_ALGO, "implicit transparent local-zone", 1126 n2, 0, dclass); 1127 if(!( 1128 #ifndef THREADS_DISABLED 1129 z= 1130 #endif 1131 lz_enter_zone_dname(zones, n2, nmlen, match, 1132 local_zone_transparent, dclass))) { 1133 return 0; 1134 } 1135 lock_rw_unlock(&z->lock); 1136 } 1137 if(have_other_classes) { 1138 /* restart to setup other class */ 1139 return lz_setup_implicit(zones, cfg); 1140 } 1141 return 1; 1142 } 1143 1144 /** enter local-zone-tag info */ 1145 static int 1146 lz_enter_zone_tags(struct local_zones* zones, struct config_file* cfg) 1147 { 1148 struct config_strbytelist* p; 1149 int c = 0; 1150 for(p = cfg->local_zone_tags; p; p = p->next) { 1151 if(!lz_enter_zone_tag(zones, p->str, p->str2, p->str2len, 1152 LDNS_RR_CLASS_IN)) 1153 return 0; 1154 c++; 1155 } 1156 if(c) verbose(VERB_ALGO, "applied tags to %d local zones", c); 1157 return 1; 1158 } 1159 1160 /** enter auth data */ 1161 static int 1162 lz_enter_data(struct local_zones* zones, struct config_file* cfg) 1163 { 1164 struct config_strlist* p; 1165 for(p = cfg->local_data; p; p = p->next) { 1166 if(!lz_enter_rr_str(zones, p->str)) 1167 return 0; 1168 } 1169 return 1; 1170 } 1171 1172 /** free memory from config */ 1173 static void 1174 lz_freeup_cfg(struct config_file* cfg) 1175 { 1176 config_deldblstrlist(cfg->local_zones); 1177 cfg->local_zones = NULL; 1178 config_delstrlist(cfg->local_zones_nodefault); 1179 cfg->local_zones_nodefault = NULL; 1180 config_delstrlist(cfg->local_data); 1181 cfg->local_data = NULL; 1182 } 1183 1184 int 1185 local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg) 1186 { 1187 /* create zones from zone statements. */ 1188 if(!lz_enter_zones(zones, cfg)) { 1189 return 0; 1190 } 1191 /* apply default zones+content (unless disabled, or overridden) */ 1192 if(!local_zone_enter_defaults(zones, cfg)) { 1193 return 0; 1194 } 1195 /* enter local zone overrides */ 1196 if(!lz_enter_overrides(zones, cfg)) { 1197 return 0; 1198 } 1199 /* create implicit transparent zone from data. */ 1200 if(!lz_setup_implicit(zones, cfg)) { 1201 return 0; 1202 } 1203 1204 /* setup parent ptrs for lookup during data entry */ 1205 init_parents(zones); 1206 /* insert local zone tags */ 1207 if(!lz_enter_zone_tags(zones, cfg)) { 1208 return 0; 1209 } 1210 /* insert local data */ 1211 if(!lz_enter_data(zones, cfg)) { 1212 return 0; 1213 } 1214 /* freeup memory from cfg struct. */ 1215 lz_freeup_cfg(cfg); 1216 return 1; 1217 } 1218 1219 struct local_zone* 1220 local_zones_lookup(struct local_zones* zones, 1221 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype) 1222 { 1223 return local_zones_tags_lookup(zones, name, len, labs, 1224 dclass, dtype, NULL, 0, 1); 1225 } 1226 1227 struct local_zone* 1228 local_zones_tags_lookup(struct local_zones* zones, 1229 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype, 1230 uint8_t* taglist, size_t taglen, int ignoretags) 1231 { 1232 rbnode_type* res = NULL; 1233 struct local_zone *result; 1234 struct local_zone key; 1235 int m; 1236 /* for type DS use a zone higher when on a zonecut */ 1237 if(dtype == LDNS_RR_TYPE_DS && !dname_is_root(name)) { 1238 dname_remove_label(&name, &len); 1239 labs--; 1240 } 1241 key.node.key = &key; 1242 key.dclass = dclass; 1243 key.name = name; 1244 key.namelen = len; 1245 key.namelabs = labs; 1246 rbtree_find_less_equal(&zones->ztree, &key, &res); 1247 result = (struct local_zone*)res; 1248 /* exact or smaller element (or no element) */ 1249 if(!result || result->dclass != dclass) 1250 return NULL; 1251 /* count number of labels matched */ 1252 (void)dname_lab_cmp(result->name, result->namelabs, key.name, 1253 key.namelabs, &m); 1254 while(result) { /* go up until qname is zone or subdomain of zone */ 1255 if(result->namelabs <= m) 1256 if(ignoretags || !result->taglist || 1257 taglist_intersect(result->taglist, 1258 result->taglen, taglist, taglen)) 1259 break; 1260 result = result->parent; 1261 } 1262 return result; 1263 } 1264 1265 struct local_zone* 1266 local_zones_find(struct local_zones* zones, 1267 uint8_t* name, size_t len, int labs, uint16_t dclass) 1268 { 1269 struct local_zone key; 1270 key.node.key = &key; 1271 key.dclass = dclass; 1272 key.name = name; 1273 key.namelen = len; 1274 key.namelabs = labs; 1275 /* exact */ 1276 return (struct local_zone*)rbtree_search(&zones->ztree, &key); 1277 } 1278 1279 struct local_zone* 1280 local_zones_find_le(struct local_zones* zones, 1281 uint8_t* name, size_t len, int labs, uint16_t dclass, 1282 int* exact) 1283 { 1284 struct local_zone key; 1285 rbnode_type *node; 1286 key.node.key = &key; 1287 key.dclass = dclass; 1288 key.name = name; 1289 key.namelen = len; 1290 key.namelabs = labs; 1291 *exact = rbtree_find_less_equal(&zones->ztree, &key, &node); 1292 return (struct local_zone*)node; 1293 } 1294 1295 /** encode answer consisting of 1 rrset */ 1296 static int 1297 local_encode(struct query_info* qinfo, struct module_env* env, 1298 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf, 1299 struct regional* temp, struct ub_packed_rrset_key* rrset, int ansec, 1300 int rcode) 1301 { 1302 struct reply_info rep; 1303 uint16_t udpsize; 1304 /* make answer with time=0 for fixed TTL values */ 1305 memset(&rep, 0, sizeof(rep)); 1306 rep.flags = (uint16_t)((BIT_QR | BIT_AA | BIT_RA) | rcode); 1307 rep.qdcount = 1; 1308 if(ansec) 1309 rep.an_numrrsets = 1; 1310 else rep.ns_numrrsets = 1; 1311 rep.rrset_count = 1; 1312 rep.rrsets = &rrset; 1313 rep.reason_bogus = LDNS_EDE_NONE; 1314 udpsize = edns->udp_size; 1315 edns->edns_version = EDNS_ADVERTISED_VERSION; 1316 edns->udp_size = EDNS_ADVERTISED_SIZE; 1317 edns->ext_rcode = 0; 1318 edns->bits &= EDNS_DO; 1319 if(!inplace_cb_reply_local_call(env, qinfo, NULL, &rep, rcode, edns, 1320 repinfo, temp, env->now_tv) || !reply_info_answer_encode(qinfo, &rep, 1321 *(uint16_t*)sldns_buffer_begin(buf), sldns_buffer_read_u16_at(buf, 2), 1322 buf, 0, 0, temp, udpsize, edns, (int)(edns->bits&EDNS_DO), 0)) { 1323 error_encode(buf, (LDNS_RCODE_SERVFAIL|BIT_AA), qinfo, 1324 *(uint16_t*)sldns_buffer_begin(buf), 1325 sldns_buffer_read_u16_at(buf, 2), edns); 1326 } 1327 return 1; 1328 } 1329 1330 /** encode local error answer */ 1331 static void 1332 local_error_encode(struct query_info* qinfo, struct module_env* env, 1333 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf, 1334 struct regional* temp, int rcode, int r, int ede_code, 1335 const char* ede_txt) 1336 { 1337 edns->edns_version = EDNS_ADVERTISED_VERSION; 1338 edns->udp_size = EDNS_ADVERTISED_SIZE; 1339 edns->ext_rcode = 0; 1340 edns->bits &= EDNS_DO; 1341 1342 if(!inplace_cb_reply_local_call(env, qinfo, NULL, NULL, 1343 rcode, edns, repinfo, temp, env->now_tv)) 1344 edns->opt_list_inplace_cb_out = NULL; 1345 1346 if(ede_code != LDNS_EDE_NONE && env->cfg->ede) { 1347 edns_opt_list_append_ede(&edns->opt_list_out, temp, 1348 ede_code, ede_txt); 1349 } 1350 1351 error_encode(buf, r, qinfo, *(uint16_t*)sldns_buffer_begin(buf), 1352 sldns_buffer_read_u16_at(buf, 2), edns); 1353 } 1354 1355 /** find local data tag string match for the given type in the list */ 1356 int 1357 local_data_find_tag_datas(const struct query_info* qinfo, 1358 struct config_strlist* list, struct ub_packed_rrset_key* r, 1359 struct regional* temp) 1360 { 1361 struct config_strlist* p; 1362 char buf[65536]; 1363 uint8_t rr[LDNS_RR_BUF_SIZE]; 1364 size_t len; 1365 int res; 1366 struct packed_rrset_data* d; 1367 for(p=list; p; p=p->next) { 1368 uint16_t rdr_type; 1369 1370 len = sizeof(rr); 1371 /* does this element match the type? */ 1372 snprintf(buf, sizeof(buf), ". %s", p->str); 1373 res = sldns_str2wire_rr_buf(buf, rr, &len, NULL, 3600, 1374 NULL, 0, NULL, 0); 1375 if(res != 0) 1376 /* parse errors are already checked before, in 1377 * acllist check_data, skip this for robustness */ 1378 continue; 1379 if(len < 1 /* . */ + 8 /* typeclassttl*/ + 2 /*rdatalen*/) 1380 continue; 1381 rdr_type = sldns_wirerr_get_type(rr, len, 1); 1382 if(rdr_type != qinfo->qtype && rdr_type != LDNS_RR_TYPE_CNAME) 1383 continue; 1384 1385 /* do we have entries already? if not setup key */ 1386 if(r->rk.dname == NULL) { 1387 r->entry.key = r; 1388 r->rk.dname = qinfo->qname; 1389 r->rk.dname_len = qinfo->qname_len; 1390 r->rk.type = htons(rdr_type); 1391 r->rk.rrset_class = htons(qinfo->qclass); 1392 r->rk.flags = 0; 1393 d = (struct packed_rrset_data*)regional_alloc_zero( 1394 temp, sizeof(struct packed_rrset_data) 1395 + sizeof(size_t) + sizeof(uint8_t*) + 1396 sizeof(time_t)); 1397 if(!d) return 0; /* out of memory */ 1398 r->entry.data = d; 1399 d->ttl = sldns_wirerr_get_ttl(rr, len, 1); 1400 d->rr_len = (size_t*)((uint8_t*)d + 1401 sizeof(struct packed_rrset_data)); 1402 d->rr_data = (uint8_t**)&(d->rr_len[1]); 1403 d->rr_ttl = (time_t*)&(d->rr_data[1]); 1404 } 1405 d = (struct packed_rrset_data*)r->entry.data; 1406 /* add entry to the data */ 1407 if(d->count != 0) { 1408 size_t* oldlen = d->rr_len; 1409 uint8_t** olddata = d->rr_data; 1410 time_t* oldttl = d->rr_ttl; 1411 /* increase arrays for lookup */ 1412 /* this is of course slow for very many records, 1413 * but most redirects are expected with few records */ 1414 d->rr_len = (size_t*)regional_alloc_zero(temp, 1415 (d->count+1)*sizeof(size_t)); 1416 d->rr_data = (uint8_t**)regional_alloc_zero(temp, 1417 (d->count+1)*sizeof(uint8_t*)); 1418 d->rr_ttl = (time_t*)regional_alloc_zero(temp, 1419 (d->count+1)*sizeof(time_t)); 1420 if(!d->rr_len || !d->rr_data || !d->rr_ttl) 1421 return 0; /* out of memory */ 1422 /* first one was allocated after struct d, but new 1423 * ones get their own array increment alloc, so 1424 * copy old content */ 1425 memmove(d->rr_len, oldlen, d->count*sizeof(size_t)); 1426 memmove(d->rr_data, olddata, d->count*sizeof(uint8_t*)); 1427 memmove(d->rr_ttl, oldttl, d->count*sizeof(time_t)); 1428 } 1429 1430 d->rr_len[d->count] = sldns_wirerr_get_rdatalen(rr, len, 1)+2; 1431 d->rr_ttl[d->count] = sldns_wirerr_get_ttl(rr, len, 1); 1432 d->rr_data[d->count] = regional_alloc_init(temp, 1433 sldns_wirerr_get_rdatawl(rr, len, 1), 1434 d->rr_len[d->count]); 1435 if(!d->rr_data[d->count]) 1436 return 0; /* out of memory */ 1437 d->count++; 1438 } 1439 if(r->rk.dname) 1440 return 1; 1441 return 0; 1442 } 1443 1444 static int 1445 find_tag_datas(struct query_info* qinfo, struct config_strlist* list, 1446 struct ub_packed_rrset_key* r, struct regional* temp) 1447 { 1448 int result = local_data_find_tag_datas(qinfo, list, r, temp); 1449 1450 /* If we've found a non-exact alias type of local data, make a shallow 1451 * copy of the RRset and remember it in qinfo to complete the alias 1452 * chain later. */ 1453 if(result && qinfo->qtype != LDNS_RR_TYPE_CNAME && 1454 r->rk.type == htons(LDNS_RR_TYPE_CNAME)) { 1455 qinfo->local_alias = 1456 regional_alloc_zero(temp, sizeof(struct local_rrset)); 1457 if(!qinfo->local_alias) 1458 return 0; /* out of memory */ 1459 qinfo->local_alias->rrset = 1460 regional_alloc_init(temp, r, sizeof(*r)); 1461 if(!qinfo->local_alias->rrset) 1462 return 0; /* out of memory */ 1463 } 1464 return result; 1465 } 1466 1467 int 1468 local_data_answer(struct local_zone* z, struct module_env* env, 1469 struct query_info* qinfo, struct edns_data* edns, 1470 struct comm_reply* repinfo, sldns_buffer* buf, 1471 struct regional* temp, int labs, struct local_data** ldp, 1472 enum localzone_type lz_type, int tag, struct config_strlist** tag_datas, 1473 size_t tag_datas_size, char** tagname, int num_tags) 1474 { 1475 struct local_data key; 1476 struct local_data* ld; 1477 struct local_rrset* lr; 1478 key.node.key = &key; 1479 key.name = qinfo->qname; 1480 key.namelen = qinfo->qname_len; 1481 key.namelabs = labs; 1482 if(lz_type == local_zone_redirect || 1483 lz_type == local_zone_inform_redirect) { 1484 key.name = z->name; 1485 key.namelen = z->namelen; 1486 key.namelabs = z->namelabs; 1487 if(tag != -1 && (size_t)tag<tag_datas_size && tag_datas[tag]) { 1488 struct ub_packed_rrset_key r; 1489 memset(&r, 0, sizeof(r)); 1490 if(find_tag_datas(qinfo, tag_datas[tag], &r, temp)) { 1491 verbose(VERB_ALGO, "redirect with tag data [%d] %s", 1492 tag, (tag<num_tags?tagname[tag]:"null")); 1493 1494 /* If we found a matching alias, we should 1495 * use it as part of the answer, but we can't 1496 * encode it until we complete the alias 1497 * chain. */ 1498 if(qinfo->local_alias) 1499 return 1; 1500 return local_encode(qinfo, env, edns, repinfo, buf, temp, 1501 &r, 1, LDNS_RCODE_NOERROR); 1502 } 1503 } 1504 } 1505 ld = (struct local_data*)rbtree_search(&z->data, &key.node); 1506 *ldp = ld; 1507 if(!ld) { 1508 return 0; 1509 } 1510 lr = local_data_find_type(ld, qinfo->qtype, 1); 1511 if(!lr) 1512 return 0; 1513 1514 /* Special case for alias matching. See local_data_answer(). */ 1515 if((lz_type == local_zone_redirect || 1516 lz_type == local_zone_inform_redirect) && 1517 qinfo->qtype != LDNS_RR_TYPE_CNAME && 1518 lr->rrset->rk.type == htons(LDNS_RR_TYPE_CNAME)) { 1519 uint8_t* ctarget; 1520 size_t ctargetlen = 0; 1521 1522 qinfo->local_alias = 1523 regional_alloc_zero(temp, sizeof(struct local_rrset)); 1524 if(!qinfo->local_alias) 1525 return 0; /* out of memory */ 1526 qinfo->local_alias->rrset = regional_alloc_init( 1527 temp, lr->rrset, sizeof(*lr->rrset)); 1528 if(!qinfo->local_alias->rrset) 1529 return 0; /* out of memory */ 1530 qinfo->local_alias->rrset->rk.dname = qinfo->qname; 1531 qinfo->local_alias->rrset->rk.dname_len = qinfo->qname_len; 1532 get_cname_target(lr->rrset, &ctarget, &ctargetlen); 1533 if(!ctargetlen) 1534 return 0; /* invalid cname */ 1535 if(dname_is_wild(ctarget)) { 1536 /* synthesize cname target */ 1537 struct packed_rrset_data* d, *lr_d; 1538 /* -3 for wildcard label and root label from qname */ 1539 size_t newtargetlen = qinfo->qname_len + ctargetlen - 3; 1540 1541 log_assert(ctargetlen >= 3); 1542 log_assert(qinfo->qname_len >= 1); 1543 1544 if(newtargetlen > LDNS_MAX_DOMAINLEN) { 1545 qinfo->local_alias = NULL; 1546 local_error_encode(qinfo, env, edns, repinfo, 1547 buf, temp, LDNS_RCODE_YXDOMAIN, 1548 (LDNS_RCODE_YXDOMAIN|BIT_AA), 1549 LDNS_EDE_OTHER, 1550 "DNAME expansion became too large"); 1551 return 1; 1552 } 1553 memset(&qinfo->local_alias->rrset->entry, 0, 1554 sizeof(qinfo->local_alias->rrset->entry)); 1555 qinfo->local_alias->rrset->entry.key = 1556 qinfo->local_alias->rrset; 1557 qinfo->local_alias->rrset->entry.hash = 1558 rrset_key_hash(&qinfo->local_alias->rrset->rk); 1559 d = (struct packed_rrset_data*)regional_alloc_zero(temp, 1560 sizeof(struct packed_rrset_data) + sizeof(size_t) + 1561 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t) 1562 + newtargetlen); 1563 if(!d) 1564 return 0; /* out of memory */ 1565 lr_d = (struct packed_rrset_data*)lr->rrset->entry.data; 1566 qinfo->local_alias->rrset->entry.data = d; 1567 d->ttl = lr_d->rr_ttl[0]; /* RFC6672-like behavior: 1568 synth CNAME TTL uses original TTL*/ 1569 d->count = 1; 1570 d->rrsig_count = 0; 1571 d->trust = rrset_trust_ans_noAA; 1572 d->rr_len = (size_t*)((uint8_t*)d + 1573 sizeof(struct packed_rrset_data)); 1574 d->rr_len[0] = newtargetlen + sizeof(uint16_t); 1575 packed_rrset_ptr_fixup(d); 1576 d->rr_ttl[0] = d->ttl; 1577 sldns_write_uint16(d->rr_data[0], newtargetlen); 1578 /* write qname */ 1579 memmove(d->rr_data[0] + sizeof(uint16_t), qinfo->qname, 1580 qinfo->qname_len - 1); 1581 /* write cname target wildcard label */ 1582 memmove(d->rr_data[0] + sizeof(uint16_t) + 1583 qinfo->qname_len - 1, ctarget + 2, 1584 ctargetlen - 2); 1585 } 1586 return 1; 1587 } 1588 if(lz_type == local_zone_redirect || 1589 lz_type == local_zone_inform_redirect) { 1590 /* convert rrset name to query name; like a wildcard */ 1591 struct ub_packed_rrset_key r = *lr->rrset; 1592 r.rk.dname = qinfo->qname; 1593 r.rk.dname_len = qinfo->qname_len; 1594 return local_encode(qinfo, env, edns, repinfo, buf, temp, &r, 1, 1595 LDNS_RCODE_NOERROR); 1596 } 1597 return local_encode(qinfo, env, edns, repinfo, buf, temp, lr->rrset, 1, 1598 LDNS_RCODE_NOERROR); 1599 } 1600 1601 /** 1602 * See if the local zone does not cover the name, eg. the name is not 1603 * in the zone and the zone is transparent */ 1604 static int 1605 local_zone_does_not_cover(struct local_zone* z, struct query_info* qinfo, 1606 int labs) 1607 { 1608 struct local_data key; 1609 struct local_data* ld = NULL; 1610 struct local_rrset* lr = NULL; 1611 if(z->type == local_zone_always_transparent || z->type == local_zone_block_a) 1612 return 1; 1613 if(z->type != local_zone_transparent 1614 && z->type != local_zone_typetransparent 1615 && z->type != local_zone_inform) 1616 return 0; 1617 key.node.key = &key; 1618 key.name = qinfo->qname; 1619 key.namelen = qinfo->qname_len; 1620 key.namelabs = labs; 1621 ld = (struct local_data*)rbtree_search(&z->data, &key.node); 1622 if(z->type == local_zone_transparent || z->type == local_zone_inform) 1623 return (ld == NULL); 1624 if(ld) 1625 lr = local_data_find_type(ld, qinfo->qtype, 1); 1626 /* local_zone_typetransparent */ 1627 return (lr == NULL); 1628 } 1629 1630 static inline int 1631 local_zone_is_udp_query(struct comm_reply* repinfo) { 1632 return repinfo != NULL 1633 ? (repinfo->c != NULL 1634 ? repinfo->c->type == comm_udp 1635 : 0) 1636 : 0; 1637 } 1638 1639 int 1640 local_zones_zone_answer(struct local_zone* z, struct module_env* env, 1641 struct query_info* qinfo, struct edns_data* edns, 1642 struct comm_reply* repinfo, sldns_buffer* buf, struct regional* temp, 1643 struct local_data* ld, enum localzone_type lz_type) 1644 { 1645 if(lz_type == local_zone_deny || 1646 lz_type == local_zone_always_deny || 1647 lz_type == local_zone_inform_deny) { 1648 /** no reply at all, signal caller by clearing buffer. */ 1649 sldns_buffer_clear(buf); 1650 sldns_buffer_flip(buf); 1651 return 1; 1652 } else if(lz_type == local_zone_refuse 1653 || lz_type == local_zone_always_refuse) { 1654 local_error_encode(qinfo, env, edns, repinfo, buf, temp, 1655 LDNS_RCODE_REFUSED, (LDNS_RCODE_REFUSED|BIT_AA), 1656 LDNS_EDE_NONE, NULL); 1657 return 1; 1658 } else if(lz_type == local_zone_static || 1659 lz_type == local_zone_redirect || 1660 lz_type == local_zone_inform_redirect || 1661 lz_type == local_zone_always_nxdomain || 1662 lz_type == local_zone_always_nodata || 1663 (lz_type == local_zone_truncate 1664 && local_zone_is_udp_query(repinfo))) { 1665 /* for static, reply nodata or nxdomain 1666 * for redirect, reply nodata */ 1667 /* no additional section processing, 1668 * cname, dname or wildcard processing, 1669 * or using closest match for NSEC. 1670 * or using closest match for returning delegation downwards 1671 */ 1672 int rcode = (ld || lz_type == local_zone_redirect || 1673 lz_type == local_zone_inform_redirect || 1674 lz_type == local_zone_always_nodata || 1675 lz_type == local_zone_truncate)? 1676 LDNS_RCODE_NOERROR:LDNS_RCODE_NXDOMAIN; 1677 rcode = (lz_type == local_zone_truncate ? (rcode|BIT_TC) : rcode); 1678 if(z != NULL && z->soa && z->soa_negative) 1679 return local_encode(qinfo, env, edns, repinfo, buf, temp, 1680 z->soa_negative, 0, rcode); 1681 local_error_encode(qinfo, env, edns, repinfo, buf, temp, 1682 rcode, (rcode|BIT_AA), LDNS_EDE_NONE, NULL); 1683 return 1; 1684 } else if(lz_type == local_zone_typetransparent 1685 || lz_type == local_zone_always_transparent) { 1686 /* no NODATA or NXDOMAINS for this zone type */ 1687 return 0; 1688 } else if(lz_type == local_zone_block_a) { 1689 /* Return NODATA for all A queries */ 1690 if(qinfo->qtype == LDNS_RR_TYPE_A) { 1691 local_error_encode(qinfo, env, edns, repinfo, buf, temp, 1692 LDNS_RCODE_NOERROR, (LDNS_RCODE_NOERROR|BIT_AA), 1693 LDNS_EDE_NONE, NULL); 1694 return 1; 1695 } 1696 1697 return 0; 1698 } else if(lz_type == local_zone_always_null) { 1699 /* 0.0.0.0 or ::0 or noerror/nodata for this zone type, 1700 * used for blocklists. */ 1701 if(qinfo->qtype == LDNS_RR_TYPE_A || 1702 qinfo->qtype == LDNS_RR_TYPE_AAAA) { 1703 struct ub_packed_rrset_key lrr; 1704 struct packed_rrset_data d; 1705 time_t rr_ttl = 3600; 1706 size_t rr_len = 0; 1707 uint8_t rr_data[2+16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 1708 uint8_t* rr_datas = rr_data; 1709 memset(&lrr, 0, sizeof(lrr)); 1710 memset(&d, 0, sizeof(d)); 1711 lrr.entry.data = &d; 1712 lrr.rk.dname = qinfo->qname; 1713 lrr.rk.dname_len = qinfo->qname_len; 1714 lrr.rk.type = htons(qinfo->qtype); 1715 lrr.rk.rrset_class = htons(qinfo->qclass); 1716 if(qinfo->qtype == LDNS_RR_TYPE_A) { 1717 rr_len = 4; 1718 sldns_write_uint16(rr_data, rr_len); 1719 rr_len += 2; 1720 } else { 1721 rr_len = 16; 1722 sldns_write_uint16(rr_data, rr_len); 1723 rr_len += 2; 1724 } 1725 d.ttl = rr_ttl; 1726 d.count = 1; 1727 d.rr_len = &rr_len; 1728 d.rr_data = &rr_datas; 1729 d.rr_ttl = &rr_ttl; 1730 return local_encode(qinfo, env, edns, repinfo, buf, temp, 1731 &lrr, 1, LDNS_RCODE_NOERROR); 1732 } else { 1733 /* NODATA: No EDE needed */ 1734 local_error_encode(qinfo, env, edns, repinfo, buf, 1735 temp, LDNS_RCODE_NOERROR, 1736 (LDNS_RCODE_NOERROR|BIT_AA), -1, NULL); 1737 } 1738 return 1; 1739 } 1740 /* else lz_type == local_zone_transparent */ 1741 1742 /* if the zone is transparent and the name exists, but the type 1743 * does not, then we should make this noerror/nodata */ 1744 if(ld && ld->rrsets) { 1745 int rcode = LDNS_RCODE_NOERROR; 1746 if(z != NULL && z->soa && z->soa_negative) 1747 return local_encode(qinfo, env, edns, repinfo, buf, temp, 1748 z->soa_negative, 0, rcode); 1749 /* NODATA: No EDE needed */ 1750 local_error_encode(qinfo, env, edns, repinfo, buf, temp, rcode, 1751 (rcode|BIT_AA), LDNS_EDE_NONE, NULL); 1752 return 1; 1753 } 1754 1755 /* stop here, and resolve further on */ 1756 return 0; 1757 } 1758 1759 /** print log information for an inform zone query */ 1760 static void 1761 lz_inform_print(struct local_zone* z, struct query_info* qinfo, 1762 struct sockaddr_storage* addr, socklen_t addrlen) 1763 { 1764 char ip[128], txt[512]; 1765 char zname[LDNS_MAX_DOMAINLEN+1]; 1766 uint16_t port = ntohs(((struct sockaddr_in*)addr)->sin_port); 1767 dname_str(z->name, zname); 1768 addr_to_str(addr, addrlen, ip, sizeof(ip)); 1769 snprintf(txt, sizeof(txt), "%s %s %s@%u", zname, local_zone_type2str(z->type), ip, 1770 (unsigned)port); 1771 log_nametypeclass(NO_VERBOSE, txt, qinfo->qname, qinfo->qtype, qinfo->qclass); 1772 } 1773 1774 static enum localzone_type 1775 lz_type(uint8_t *taglist, size_t taglen, uint8_t *taglist2, size_t taglen2, 1776 uint8_t *tagactions, size_t tagactionssize, enum localzone_type lzt, 1777 struct comm_reply* repinfo, struct rbtree_type* override_tree, 1778 int* tag, char** tagname, int num_tags) 1779 { 1780 struct local_zone_override* lzo; 1781 if(repinfo && override_tree) { 1782 lzo = (struct local_zone_override*)addr_tree_lookup( 1783 override_tree, &repinfo->client_addr, 1784 repinfo->client_addrlen); 1785 if(lzo && lzo->type) { 1786 verbose(VERB_ALGO, "local zone override to type %s", 1787 local_zone_type2str(lzo->type)); 1788 return lzo->type; 1789 } 1790 } 1791 if(!taglist || !taglist2) 1792 return lzt; 1793 return local_data_find_tag_action(taglist, taglen, taglist2, taglen2, 1794 tagactions, tagactionssize, lzt, tag, tagname, num_tags); 1795 } 1796 1797 enum localzone_type 1798 local_data_find_tag_action(const uint8_t* taglist, size_t taglen, 1799 const uint8_t* taglist2, size_t taglen2, const uint8_t* tagactions, 1800 size_t tagactionssize, enum localzone_type lzt, int* tag, 1801 char* const* tagname, int num_tags) 1802 { 1803 size_t i, j; 1804 uint8_t tagmatch; 1805 1806 for(i=0; i<taglen && i<taglen2; i++) { 1807 tagmatch = (taglist[i] & taglist2[i]); 1808 for(j=0; j<8 && tagmatch>0; j++) { 1809 if((tagmatch & 0x1)) { 1810 *tag = (int)(i*8+j); 1811 verbose(VERB_ALGO, "matched tag [%d] %s", 1812 *tag, (*tag<num_tags?tagname[*tag]:"null")); 1813 /* does this tag have a tag action? */ 1814 if(i*8+j < tagactionssize && tagactions 1815 && tagactions[i*8+j] != 0) { 1816 verbose(VERB_ALGO, "tag action [%d] %s to type %s", 1817 *tag, (*tag<num_tags?tagname[*tag]:"null"), 1818 local_zone_type2str( 1819 (enum localzone_type) 1820 tagactions[i*8+j])); 1821 return (enum localzone_type)tagactions[i*8+j]; 1822 } 1823 return lzt; 1824 } 1825 tagmatch >>= 1; 1826 } 1827 } 1828 return lzt; 1829 } 1830 1831 int 1832 local_zones_answer(struct local_zones* zones, struct module_env* env, 1833 struct query_info* qinfo, struct edns_data* edns, sldns_buffer* buf, 1834 struct regional* temp, struct comm_reply* repinfo, uint8_t* taglist, 1835 size_t taglen, uint8_t* tagactions, size_t tagactionssize, 1836 struct config_strlist** tag_datas, size_t tag_datas_size, 1837 char** tagname, int num_tags, struct view* view) 1838 { 1839 /* see if query is covered by a zone, 1840 * if so: - try to match (exact) local data 1841 * - look at zone type for negative response. */ 1842 int labs = dname_count_labels(qinfo->qname); 1843 struct local_data* ld = NULL; 1844 struct local_zone* z = NULL; 1845 enum localzone_type lzt = local_zone_transparent; 1846 int r, tag = -1; 1847 1848 if(view) { 1849 lock_rw_rdlock(&view->lock); 1850 if(view->local_zones && 1851 (z = local_zones_lookup(view->local_zones, 1852 qinfo->qname, qinfo->qname_len, labs, 1853 qinfo->qclass, qinfo->qtype))) { 1854 lock_rw_rdlock(&z->lock); 1855 lzt = z->type; 1856 } 1857 if(lzt == local_zone_noview) { 1858 lock_rw_unlock(&z->lock); 1859 z = NULL; 1860 } 1861 if(z && (lzt == local_zone_transparent || 1862 lzt == local_zone_typetransparent || 1863 lzt == local_zone_inform || 1864 lzt == local_zone_always_transparent || 1865 lzt == local_zone_block_a) && 1866 local_zone_does_not_cover(z, qinfo, labs)) { 1867 lock_rw_unlock(&z->lock); 1868 z = NULL; 1869 } 1870 if(view->local_zones && !z && !view->isfirst){ 1871 lock_rw_unlock(&view->lock); 1872 return 0; 1873 } 1874 if(z && verbosity >= VERB_ALGO) { 1875 char zname[255+1]; 1876 dname_str(z->name, zname); 1877 verbose(VERB_ALGO, "using localzone %s %s from view %s", 1878 zname, local_zone_type2str(lzt), view->name); 1879 } 1880 lock_rw_unlock(&view->lock); 1881 } 1882 if(!z) { 1883 /* try global local_zones tree */ 1884 lock_rw_rdlock(&zones->lock); 1885 if(!(z = local_zones_tags_lookup(zones, qinfo->qname, 1886 qinfo->qname_len, labs, qinfo->qclass, qinfo->qtype, 1887 taglist, taglen, 0))) { 1888 lock_rw_unlock(&zones->lock); 1889 return 0; 1890 } 1891 lock_rw_rdlock(&z->lock); 1892 lzt = lz_type(taglist, taglen, z->taglist, z->taglen, 1893 tagactions, tagactionssize, z->type, repinfo, 1894 z->override_tree, &tag, tagname, num_tags); 1895 lock_rw_unlock(&zones->lock); 1896 if(z && verbosity >= VERB_ALGO) { 1897 char zname[255+1]; 1898 dname_str(z->name, zname); 1899 verbose(VERB_ALGO, "using localzone %s %s", zname, 1900 local_zone_type2str(lzt)); 1901 } 1902 } 1903 if((env->cfg->log_local_actions || 1904 lzt == local_zone_inform || 1905 lzt == local_zone_inform_deny || 1906 lzt == local_zone_inform_redirect) 1907 && repinfo) 1908 lz_inform_print(z, qinfo, &repinfo->client_addr, 1909 repinfo->client_addrlen); 1910 1911 if(lzt != local_zone_always_refuse 1912 && lzt != local_zone_always_transparent 1913 && lzt != local_zone_block_a 1914 && lzt != local_zone_always_nxdomain 1915 && lzt != local_zone_always_nodata 1916 && lzt != local_zone_always_deny 1917 && local_data_answer(z, env, qinfo, edns, repinfo, buf, temp, labs, 1918 &ld, lzt, tag, tag_datas, tag_datas_size, tagname, num_tags)) { 1919 lock_rw_unlock(&z->lock); 1920 /* We should tell the caller that encode is deferred if we found 1921 * a local alias. */ 1922 return !qinfo->local_alias; 1923 } 1924 r = local_zones_zone_answer(z, env, qinfo, edns, repinfo, buf, temp, ld, lzt); 1925 lock_rw_unlock(&z->lock); 1926 return r && !qinfo->local_alias; /* see above */ 1927 } 1928 1929 const char* local_zone_type2str(enum localzone_type t) 1930 { 1931 switch(t) { 1932 case local_zone_unset: return "unset"; 1933 case local_zone_deny: return "deny"; 1934 case local_zone_refuse: return "refuse"; 1935 case local_zone_redirect: return "redirect"; 1936 case local_zone_transparent: return "transparent"; 1937 case local_zone_typetransparent: return "typetransparent"; 1938 case local_zone_static: return "static"; 1939 case local_zone_nodefault: return "nodefault"; 1940 case local_zone_inform: return "inform"; 1941 case local_zone_inform_deny: return "inform_deny"; 1942 case local_zone_inform_redirect: return "inform_redirect"; 1943 case local_zone_always_transparent: return "always_transparent"; 1944 case local_zone_block_a: return "block_a"; 1945 case local_zone_always_refuse: return "always_refuse"; 1946 case local_zone_always_nxdomain: return "always_nxdomain"; 1947 case local_zone_always_nodata: return "always_nodata"; 1948 case local_zone_always_deny: return "always_deny"; 1949 case local_zone_always_null: return "always_null"; 1950 case local_zone_noview: return "noview"; 1951 case local_zone_truncate: return "truncate"; 1952 case local_zone_invalid: return "invalid"; 1953 } 1954 return "badtyped"; 1955 } 1956 1957 int local_zone_str2type(const char* type, enum localzone_type* t) 1958 { 1959 if(strcmp(type, "deny") == 0) 1960 *t = local_zone_deny; 1961 else if(strcmp(type, "refuse") == 0) 1962 *t = local_zone_refuse; 1963 else if(strcmp(type, "static") == 0) 1964 *t = local_zone_static; 1965 else if(strcmp(type, "transparent") == 0) 1966 *t = local_zone_transparent; 1967 else if(strcmp(type, "typetransparent") == 0) 1968 *t = local_zone_typetransparent; 1969 else if(strcmp(type, "redirect") == 0) 1970 *t = local_zone_redirect; 1971 else if(strcmp(type, "inform") == 0) 1972 *t = local_zone_inform; 1973 else if(strcmp(type, "inform_deny") == 0) 1974 *t = local_zone_inform_deny; 1975 else if(strcmp(type, "inform_redirect") == 0) 1976 *t = local_zone_inform_redirect; 1977 else if(strcmp(type, "always_transparent") == 0) 1978 *t = local_zone_always_transparent; 1979 else if(strcmp(type, "block_a") == 0) 1980 *t = local_zone_block_a; 1981 else if(strcmp(type, "always_refuse") == 0) 1982 *t = local_zone_always_refuse; 1983 else if(strcmp(type, "always_nxdomain") == 0) 1984 *t = local_zone_always_nxdomain; 1985 else if(strcmp(type, "always_nodata") == 0) 1986 *t = local_zone_always_nodata; 1987 else if(strcmp(type, "always_deny") == 0) 1988 *t = local_zone_always_deny; 1989 else if(strcmp(type, "always_null") == 0) 1990 *t = local_zone_always_null; 1991 else if(strcmp(type, "noview") == 0) 1992 *t = local_zone_noview; 1993 else if(strcmp(type, "truncate") == 0) 1994 *t = local_zone_truncate; 1995 else if(strcmp(type, "nodefault") == 0) 1996 *t = local_zone_nodefault; 1997 else return 0; 1998 return 1; 1999 } 2000 2001 /** iterate over the kiddies of the given name and set their parent ptr */ 2002 static void 2003 set_kiddo_parents(struct local_zone* z, struct local_zone* match, 2004 struct local_zone* newp) 2005 { 2006 /* both zones and z are locked already */ 2007 /* in the sorted rbtree, the kiddies of z are located after z */ 2008 /* z must be present in the tree */ 2009 struct local_zone* p = z; 2010 p = (struct local_zone*)rbtree_next(&p->node); 2011 while(p!=(struct local_zone*)RBTREE_NULL && 2012 p->dclass == z->dclass && dname_strict_subdomain(p->name, 2013 p->namelabs, z->name, z->namelabs)) { 2014 /* update parent ptr */ 2015 /* only when matches with existing parent pointer, so that 2016 * deeper child structures are not touched, i.e. 2017 * update of x, and a.x, b.x, f.b.x, g.b.x, c.x, y 2018 * gets to update a.x, b.x and c.x */ 2019 lock_rw_wrlock(&p->lock); 2020 if(p->parent == match) 2021 p->parent = newp; 2022 lock_rw_unlock(&p->lock); 2023 p = (struct local_zone*)rbtree_next(&p->node); 2024 } 2025 } 2026 2027 struct local_zone* local_zones_add_zone(struct local_zones* zones, 2028 uint8_t* name, size_t len, int labs, uint16_t dclass, 2029 enum localzone_type tp) 2030 { 2031 /* create */ 2032 struct local_zone* z = local_zone_create(name, len, labs, tp, dclass); 2033 if(!z) { 2034 free(name); 2035 return NULL; 2036 } 2037 lock_rw_wrlock(&z->lock); 2038 2039 /* find the closest parent */ 2040 z->parent = local_zones_find(zones, name, len, labs, dclass); 2041 2042 /* insert into the tree */ 2043 if(!rbtree_insert(&zones->ztree, &z->node)) { 2044 /* duplicate entry! */ 2045 lock_rw_unlock(&z->lock); 2046 local_zone_delete(z); 2047 log_err("internal: duplicate entry in local_zones_add_zone"); 2048 return NULL; 2049 } 2050 2051 /* set parent pointers right */ 2052 set_kiddo_parents(z, z->parent, z); 2053 2054 lock_rw_unlock(&z->lock); 2055 return z; 2056 } 2057 2058 void local_zones_del_zone(struct local_zones* zones, struct local_zone* z) 2059 { 2060 /* fix up parents in tree */ 2061 lock_rw_wrlock(&z->lock); 2062 set_kiddo_parents(z, z, z->parent); 2063 2064 /* remove from tree */ 2065 (void)rbtree_delete(&zones->ztree, z); 2066 2067 /* delete the zone */ 2068 lock_rw_unlock(&z->lock); 2069 local_zone_delete(z); 2070 } 2071 2072 int 2073 local_zones_add_RR(struct local_zones* zones, const char* rr) 2074 { 2075 uint8_t* rr_name; 2076 uint16_t rr_class, rr_type; 2077 size_t len; 2078 int labs; 2079 struct local_zone* z; 2080 int r; 2081 if(!get_rr_nameclass(rr, &rr_name, &rr_class, &rr_type)) { 2082 return 0; 2083 } 2084 labs = dname_count_size_labels(rr_name, &len); 2085 /* could first try readlock then get writelock if zone does not exist, 2086 * but we do not add enough RRs (from multiple threads) to optimize */ 2087 lock_rw_wrlock(&zones->lock); 2088 z = local_zones_lookup(zones, rr_name, len, labs, rr_class, rr_type); 2089 if(!z) { 2090 z = local_zones_add_zone(zones, rr_name, len, labs, rr_class, 2091 local_zone_transparent); 2092 if(!z) { 2093 lock_rw_unlock(&zones->lock); 2094 return 0; 2095 } 2096 } else { 2097 free(rr_name); 2098 } 2099 lock_rw_wrlock(&z->lock); 2100 lock_rw_unlock(&zones->lock); 2101 r = lz_enter_rr_into_zone(z, rr); 2102 lock_rw_unlock(&z->lock); 2103 return r; 2104 } 2105 2106 /** returns true if the node is terminal so no deeper domain names exist */ 2107 static int 2108 is_terminal(struct local_data* d) 2109 { 2110 /* for empty nonterminals, the deeper domain names are sorted 2111 * right after them, so simply check the next name in the tree 2112 */ 2113 struct local_data* n = (struct local_data*)rbtree_next(&d->node); 2114 if(n == (struct local_data*)RBTREE_NULL) 2115 return 1; /* last in tree, no deeper node */ 2116 if(dname_strict_subdomain(n->name, n->namelabs, d->name, d->namelabs)) 2117 return 0; /* there is a deeper node */ 2118 return 1; 2119 } 2120 2121 /** delete empty terminals from tree when final data is deleted */ 2122 static void 2123 del_empty_term(struct local_zone* z, struct local_data* d, 2124 uint8_t* name, size_t len, int labs) 2125 { 2126 while(d && d->rrsets == NULL && is_terminal(d)) { 2127 /* is this empty nonterminal? delete */ 2128 /* note, no memory recycling in zone region */ 2129 (void)rbtree_delete(&z->data, d); 2130 2131 /* go up and to the next label */ 2132 if(dname_is_root(name)) 2133 return; 2134 dname_remove_label(&name, &len); 2135 labs--; 2136 d = local_zone_find_data(z, name, len, labs); 2137 } 2138 } 2139 2140 /** find and remove type from list in domain struct */ 2141 static void 2142 del_local_rrset(struct local_data* d, uint16_t dtype) 2143 { 2144 struct local_rrset* prev=NULL, *p=d->rrsets; 2145 while(p && ntohs(p->rrset->rk.type) != dtype) { 2146 prev = p; 2147 p = p->next; 2148 } 2149 if(!p) 2150 return; /* rrset type not found */ 2151 /* unlink it */ 2152 if(prev) prev->next = p->next; 2153 else d->rrsets = p->next; 2154 /* no memory recycling for zone deletions ... */ 2155 } 2156 2157 void local_zones_del_data(struct local_zones* zones, 2158 uint8_t* name, size_t len, int labs, uint16_t dclass) 2159 { 2160 /* find zone */ 2161 struct local_zone* z; 2162 struct local_data* d; 2163 2164 /* remove DS */ 2165 lock_rw_rdlock(&zones->lock); 2166 z = local_zones_lookup(zones, name, len, labs, dclass, LDNS_RR_TYPE_DS); 2167 if(z) { 2168 lock_rw_wrlock(&z->lock); 2169 d = local_zone_find_data(z, name, len, labs); 2170 if(d) { 2171 del_local_rrset(d, LDNS_RR_TYPE_DS); 2172 del_empty_term(z, d, name, len, labs); 2173 } 2174 lock_rw_unlock(&z->lock); 2175 } 2176 lock_rw_unlock(&zones->lock); 2177 2178 /* remove other types */ 2179 lock_rw_rdlock(&zones->lock); 2180 z = local_zones_lookup(zones, name, len, labs, dclass, 0); 2181 if(!z) { 2182 /* no such zone, we're done */ 2183 lock_rw_unlock(&zones->lock); 2184 return; 2185 } 2186 lock_rw_wrlock(&z->lock); 2187 lock_rw_unlock(&zones->lock); 2188 2189 /* find the domain */ 2190 d = local_zone_find_data(z, name, len, labs); 2191 if(d) { 2192 /* no memory recycling for zone deletions ... */ 2193 d->rrsets = NULL; 2194 /* did we delete the soa record ? */ 2195 if(query_dname_compare(d->name, z->name) == 0) { 2196 z->soa = NULL; 2197 z->soa_negative = NULL; 2198 } 2199 2200 /* cleanup the empty nonterminals for this name */ 2201 del_empty_term(z, d, name, len, labs); 2202 } 2203 2204 lock_rw_unlock(&z->lock); 2205 } 2206