1 /* 2 * services/localzone.h - 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 42 #ifndef SERVICES_LOCALZONE_H 43 #define SERVICES_LOCALZONE_H 44 #include "util/rbtree.h" 45 #include "util/locks.h" 46 #include "util/storage/dnstree.h" 47 #include "util/module.h" 48 #include "services/view.h" 49 #include "sldns/sbuffer.h" 50 struct packed_rrset_data; 51 struct ub_packed_rrset_key; 52 struct regional; 53 struct config_file; 54 struct edns_data; 55 struct query_info; 56 struct sldns_buffer; 57 struct comm_reply; 58 struct config_strlist; 59 60 /** 61 * Local zone type 62 * This type determines processing for queries that did not match 63 * local-data directly. 64 */ 65 enum localzone_type { 66 /** unset type, used for unset tag_action elements */ 67 local_zone_unset = 0, 68 /** drop query */ 69 local_zone_deny, 70 /** answer with error */ 71 local_zone_refuse, 72 /** answer nxdomain or nodata */ 73 local_zone_static, 74 /** resolve normally */ 75 local_zone_transparent, 76 /** do not block types at localdata names */ 77 local_zone_typetransparent, 78 /** answer with data at zone apex */ 79 local_zone_redirect, 80 /** remove default AS112 blocking contents for zone 81 * nodefault is used in config not during service. */ 82 local_zone_nodefault, 83 /** log client address, but no block (transparent) */ 84 local_zone_inform, 85 /** log client address, and block (drop) */ 86 local_zone_inform_deny, 87 /** log client address, and direct */ 88 local_zone_inform_redirect, 89 /** resolve normally, even when there is local data */ 90 local_zone_always_transparent, 91 /** answer with error, even when there is local data */ 92 local_zone_always_refuse, 93 /** answer with nxdomain, even when there is local data */ 94 local_zone_always_nxdomain, 95 /** answer with noerror/nodata, even when there is local data */ 96 local_zone_always_nodata, 97 /** drop query, even when there is local data */ 98 local_zone_always_deny, 99 /** answer with 0.0.0.0 or ::0 or noerror/nodata, even when there is 100 * local data */ 101 local_zone_always_null, 102 /** answer not from the view, but global or no-answer */ 103 local_zone_noview, 104 /** Invalid type, cannot be used to generate answer */ 105 local_zone_invalid 106 }; 107 108 /** 109 * Authoritative local zones storage, shared. 110 */ 111 struct local_zones { 112 /** lock on the localzone tree */ 113 lock_rw_type lock; 114 /** rbtree of struct local_zone */ 115 rbtree_type ztree; 116 }; 117 118 /** 119 * Local zone. A locally served authoritative zone. 120 */ 121 struct local_zone { 122 /** rbtree node, key is name and class */ 123 rbnode_type node; 124 /** parent zone, if any. */ 125 struct local_zone* parent; 126 127 /** zone name, in uncompressed wireformat */ 128 uint8_t* name; 129 /** length of zone name */ 130 size_t namelen; 131 /** number of labels in zone name */ 132 int namelabs; 133 /** the class of this zone. 134 * uses 'dclass' to not conflict with c++ keyword class. */ 135 uint16_t dclass; 136 137 /** lock on the data in the structure 138 * For the node, parent, name, namelen, namelabs, dclass, you 139 * need to also hold the zones_tree lock to change them (or to 140 * delete this zone) */ 141 lock_rw_type lock; 142 143 /** how to process zone */ 144 enum localzone_type type; 145 /** tag bitlist */ 146 uint8_t* taglist; 147 /** length of the taglist (in bytes) */ 148 size_t taglen; 149 /** netblock addr_tree with struct local_zone_override information 150 * or NULL if there are no override elements */ 151 struct rbtree_type* override_tree; 152 153 /** in this region the zone's data is allocated. 154 * the struct local_zone itself is malloced. */ 155 struct regional* region; 156 /** local data for this zone 157 * rbtree of struct local_data */ 158 rbtree_type data; 159 /** if data contains zone apex SOA data, this is a ptr to it. */ 160 struct ub_packed_rrset_key* soa; 161 /** if data contains zone apex SOA data, this is a ptr to an 162 * artificial negative SOA rrset (TTL is the minimum of the TTL and the 163 * SOA.MINIMUM). */ 164 struct ub_packed_rrset_key* soa_negative; 165 }; 166 167 /** 168 * Local data. One domain name, and the RRs to go with it. 169 */ 170 struct local_data { 171 /** rbtree node, key is name only */ 172 rbnode_type node; 173 /** domain name */ 174 uint8_t* name; 175 /** length of name */ 176 size_t namelen; 177 /** number of labels in name */ 178 int namelabs; 179 /** the data rrsets, with different types, linked list. 180 * If this list is NULL, the node is an empty non-terminal. */ 181 struct local_rrset* rrsets; 182 }; 183 184 /** 185 * A local data RRset 186 */ 187 struct local_rrset { 188 /** next in list */ 189 struct local_rrset* next; 190 /** RRset data item */ 191 struct ub_packed_rrset_key* rrset; 192 }; 193 194 /** 195 * Local zone override information 196 */ 197 struct local_zone_override { 198 /** node in addrtree */ 199 struct addr_tree_node node; 200 /** override for local zone type */ 201 enum localzone_type type; 202 }; 203 204 /** 205 * Create local zones storage 206 * @return new struct or NULL on error. 207 */ 208 struct local_zones* local_zones_create(void); 209 210 /** 211 * Delete local zones storage 212 * @param zones: to delete. 213 */ 214 void local_zones_delete(struct local_zones* zones); 215 216 /** 217 * Apply config settings; setup the local authoritative data. 218 * Takes care of locking. 219 * @param zones: is set up. 220 * @param cfg: config data. 221 * @return false on error. 222 */ 223 int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg); 224 225 /** 226 * Compare two local_zone entries in rbtree. Sort hierarchical but not 227 * canonical 228 * @param z1: zone 1 229 * @param z2: zone 2 230 * @return: -1, 0, +1 comparison value. 231 */ 232 int local_zone_cmp(const void* z1, const void* z2); 233 234 /** 235 * Compare two local_data entries in rbtree. Sort canonical. 236 * @param d1: data 1 237 * @param d2: data 2 238 * @return: -1, 0, +1 comparison value. 239 */ 240 int local_data_cmp(const void* d1, const void* d2); 241 242 /** 243 * Delete one zone 244 * @param z: to delete. 245 */ 246 void local_zone_delete(struct local_zone* z); 247 248 /** 249 * Lookup zone that contains the given name, class and taglist. 250 * User must lock the tree or result zone. 251 * @param zones: the zones tree 252 * @param name: dname to lookup 253 * @param len: length of name. 254 * @param labs: labelcount of name. 255 * @param dclass: class to lookup. 256 * @param dtype: type to lookup, if type DS a zone higher is used for zonecuts. 257 * @param taglist: taglist to lookup. 258 * @param taglen: lenth of taglist. 259 * @param ignoretags: lookup zone by name and class, regardless the 260 * local-zone's tags. 261 * @return closest local_zone or NULL if no covering zone is found. 262 */ 263 struct local_zone* local_zones_tags_lookup(struct local_zones* zones, 264 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype, 265 uint8_t* taglist, size_t taglen, int ignoretags); 266 267 /** 268 * Lookup zone that contains the given name, class. 269 * User must lock the tree or result zone. 270 * @param zones: the zones tree 271 * @param name: dname to lookup 272 * @param len: length of name. 273 * @param labs: labelcount of name. 274 * @param dclass: class to lookup. 275 * @param dtype: type of the record, if type DS then a zone higher up is found 276 * pass 0 to just plain find a zone for a name. 277 * @return closest local_zone or NULL if no covering zone is found. 278 */ 279 struct local_zone* local_zones_lookup(struct local_zones* zones, 280 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype); 281 282 /** 283 * Debug helper. Print all zones 284 * Takes care of locking. 285 * @param zones: the zones tree 286 */ 287 void local_zones_print(struct local_zones* zones); 288 289 /** 290 * Answer authoritatively for local zones. 291 * Takes care of locking. 292 * @param zones: the stored zones (shared, read only). 293 * @param env: the module environment. 294 * @param qinfo: query info (parsed). 295 * @param edns: edns info (parsed). 296 * @param buf: buffer with query ID and flags, also for reply. 297 * @param temp: temporary storage region. 298 * @param repinfo: source address for checks. may be NULL. 299 * @param taglist: taglist for checks. May be NULL. 300 * @param taglen: length of the taglist. 301 * @param tagactions: local zone actions for tags. May be NULL. 302 * @param tagactionssize: length of the tagactions. 303 * @param tag_datas: array per tag of strlist with rdata strings. or NULL. 304 * @param tag_datas_size: size of tag_datas array. 305 * @param tagname: array of tag name strings (for debug output). 306 * @param num_tags: number of items in tagname array. 307 * @param view: answer using this view. May be NULL. 308 * @return true if answer is in buffer. false if query is not answered 309 * by authority data. If the reply should be dropped altogether, the return 310 * value is true, but the buffer is cleared (empty). 311 * It can also return true if a non-exact alias answer is found. In this 312 * case qinfo->local_alias points to the corresponding alias RRset but the 313 * answer is NOT encoded in buffer. It's the caller's responsibility to 314 * complete the alias chain (if needed) and encode the final set of answer. 315 * Data pointed to by qinfo->local_alias is allocated in 'temp' or refers to 316 * configuration data. So the caller will need to make a deep copy of it 317 * if it needs to keep it beyond the lifetime of 'temp' or a dynamic update 318 * to local zone data. 319 */ 320 int local_zones_answer(struct local_zones* zones, struct module_env* env, 321 struct query_info* qinfo, struct edns_data* edns, struct sldns_buffer* buf, 322 struct regional* temp, struct comm_reply* repinfo, uint8_t* taglist, 323 size_t taglen, uint8_t* tagactions, size_t tagactionssize, 324 struct config_strlist** tag_datas, size_t tag_datas_size, 325 char** tagname, int num_tags, struct view* view); 326 327 /** 328 * Answer using the local zone only (not local data used). 329 * @param z: zone for query. 330 * @param env: module environment. 331 * @param qinfo: query. 332 * @param edns: edns from query. 333 * @param repinfo: source address for checks. may be NULL. 334 * @param buf: buffer for answer. 335 * @param temp: temp region for encoding. 336 * @param ld: local data, if NULL, no such name exists in localdata. 337 * @param lz_type: type of the local zone. 338 * @return 1 if a reply is to be sent, 0 if not. 339 */ 340 int 341 local_zones_zone_answer(struct local_zone* z, struct module_env* env, 342 struct query_info* qinfo, struct edns_data* edns, 343 struct comm_reply* repinfo, sldns_buffer* buf, struct regional* temp, 344 struct local_data* ld, enum localzone_type lz_type); 345 346 /** 347 * Parse the string into localzone type. 348 * 349 * @param str: string to parse 350 * @param t: local zone type returned here. 351 * @return 0 on parse error. 352 */ 353 int local_zone_str2type(const char* str, enum localzone_type* t); 354 355 /** 356 * Print localzone type to a string. Pointer to a constant string. 357 * 358 * @param t: local zone type. 359 * @return constant string that describes type. 360 */ 361 const char* local_zone_type2str(enum localzone_type t); 362 363 /** 364 * Find zone that with exactly given name, class. 365 * User must lock the tree or result zone. 366 * @param zones: the zones tree 367 * @param name: dname to lookup 368 * @param len: length of name. 369 * @param labs: labelcount of name. 370 * @param dclass: class to lookup. 371 * @return the exact local_zone or NULL. 372 */ 373 struct local_zone* local_zones_find(struct local_zones* zones, 374 uint8_t* name, size_t len, int labs, uint16_t dclass); 375 376 /** 377 * Find zone that with exactly or smaller name/class 378 * User must lock the tree or result zone. 379 * @param zones: the zones tree 380 * @param name: dname to lookup 381 * @param len: length of name. 382 * @param labs: labelcount of name. 383 * @param dclass: class to lookup. 384 * @param exact: 1 on return is this is an exact match. 385 * @return the exact or smaller local_zone or NULL. 386 */ 387 struct local_zone* 388 local_zones_find_le(struct local_zones* zones, 389 uint8_t* name, size_t len, int labs, uint16_t dclass, 390 int* exact); 391 392 /** 393 * Add a new zone. Caller must hold the zones lock. 394 * Adjusts the other zones as well (parent pointers) after insertion. 395 * The zone must NOT exist (returns NULL and logs error). 396 * @param zones: the zones tree 397 * @param name: dname to add 398 * @param len: length of name. 399 * @param labs: labelcount of name. 400 * @param dclass: class to add. 401 * @param tp: type. 402 * @return local_zone or NULL on error, caller must printout memory error. 403 */ 404 struct local_zone* local_zones_add_zone(struct local_zones* zones, 405 uint8_t* name, size_t len, int labs, uint16_t dclass, 406 enum localzone_type tp); 407 408 /** 409 * Delete a zone. Caller must hold the zones lock. 410 * Adjusts the other zones as well (parent pointers) after insertion. 411 * @param zones: the zones tree 412 * @param zone: the zone to delete from tree. Also deletes zone from memory. 413 */ 414 void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone); 415 416 /** 417 * Add RR data into the localzone data. 418 * Looks up the zone, if no covering zone, a transparent zone with the 419 * name of the RR is created. 420 * @param zones: the zones tree. Not locked by caller. 421 * @param rr: string with on RR. 422 * @return false on failure. 423 */ 424 int local_zones_add_RR(struct local_zones* zones, const char* rr); 425 426 /** 427 * Remove data from domain name in the tree. 428 * All types are removed. No effect if zone or name does not exist. 429 * @param zones: zones tree. 430 * @param name: dname to remove 431 * @param len: length of name. 432 * @param labs: labelcount of name. 433 * @param dclass: class to remove. 434 */ 435 void local_zones_del_data(struct local_zones* zones, 436 uint8_t* name, size_t len, int labs, uint16_t dclass); 437 438 439 /** 440 * Form wireformat from text format domain name. 441 * @param str: the domain name in text "www.example.com" 442 * @param res: resulting wireformat is stored here with malloc. 443 * @param len: length of resulting wireformat. 444 * @param labs: number of labels in resulting wireformat. 445 * @return false on error, syntax or memory. Also logged. 446 */ 447 int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs); 448 449 /** 450 * Find local data tag string match for the given type (in qinfo) in the list. 451 * If found, 'r' will be filled with corresponding rrset information. 452 * @param qinfo: contains name, type, and class for the data 453 * @param list: stores local tag data to be searched 454 * @param r: rrset key to be filled for matched data 455 * @param temp: region to allocate rrset in 'r' 456 * @return 1 if a match is found and rrset is built; otherwise 0 including 457 * errors. 458 */ 459 int local_data_find_tag_datas(const struct query_info* qinfo, 460 struct config_strlist* list, struct ub_packed_rrset_key* r, 461 struct regional* temp); 462 463 /** 464 * See if two sets of tag lists (in the form of bitmap) have the same tag that 465 * has an action. If so, '*tag' will be set to the found tag index, and the 466 * corresponding action will be returned in the form of local zone type. 467 * Otherwise the passed type (lzt) will be returned as the default action. 468 * Pointers except tagactions must not be NULL. 469 * @param taglist: 1st list of tags 470 * @param taglen: size of taglist in bytes 471 * @param taglist2: 2nd list of tags 472 * @param taglen2: size of taglist2 in bytes 473 * @param tagactions: local data actions for tags. May be NULL. 474 * @param tagactionssize: length of the tagactions. 475 * @param lzt: default action (local zone type) if no tag action is found. 476 * @param tag: see above. 477 * @param tagname: array of tag name strings (for debug output). 478 * @param num_tags: number of items in tagname array. 479 * @return found tag action or the default action. 480 */ 481 enum localzone_type local_data_find_tag_action(const uint8_t* taglist, 482 size_t taglen, const uint8_t* taglist2, size_t taglen2, 483 const uint8_t* tagactions, size_t tagactionssize, 484 enum localzone_type lzt, int* tag, char* const* tagname, int num_tags); 485 486 /** 487 * Enter defaults to local zone. 488 * @param zones: to add defaults to 489 * @param cfg: containing list of zones to exclude from default set. 490 * @return 1 on success; 0 otherwise. 491 */ 492 int local_zone_enter_defaults(struct local_zones* zones, 493 struct config_file* cfg); 494 495 /** 496 * Parses resource record string into wire format, also returning its field values. 497 * @param str: input resource record 498 * @param nm: domain name field 499 * @param type: record type field 500 * @param dclass: record class field 501 * @param ttl: ttl field 502 * @param rr: buffer for the parsed rr in wire format 503 * @param len: buffer length 504 * @param rdata: rdata field 505 * @param rdata_len: rdata field length 506 * @return 1 on success; 0 otherwise. 507 */ 508 int rrstr_get_rr_content(const char* str, uint8_t** nm, uint16_t* type, 509 uint16_t* dclass, time_t* ttl, uint8_t* rr, size_t len, 510 uint8_t** rdata, size_t* rdata_len); 511 512 /** 513 * Insert specified rdata into the specified resource record. 514 * @param region: allocator 515 * @param pd: data portion of the destination resource record 516 * @param rdata: source rdata 517 * @param rdata_len: source rdata length 518 * @param ttl: time to live 519 * @param rrstr: resource record in text form (for logging) 520 * @return 1 on success; 0 otherwise. 521 */ 522 int rrset_insert_rr(struct regional* region, struct packed_rrset_data* pd, 523 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr); 524 525 /** 526 * Remove RR from rrset that is created using localzone's rrset_insert_rr. 527 * @param pd: the RRset containing the RR to remove 528 * @param index: index of RR to remove 529 * @return: 1 on success; 0 otherwise. 530 */ 531 int 532 local_rrset_remove_rr(struct packed_rrset_data* pd, size_t index); 533 534 /** 535 * Valid response ip actions for the IP-response-driven-action feature; 536 * defined here instead of in the respip module to enable sharing of enum 537 * values with the localzone_type enum. 538 * Note that these values except 'none' are the same as localzone types of 539 * the 'same semantics'. It's intentional as we use these values via 540 * access-control-tags, which can be shared for both response ip actions and 541 * local zones. 542 */ 543 enum respip_action { 544 /** no respip action */ 545 respip_none = local_zone_unset, 546 /** don't answer */ 547 respip_deny = local_zone_deny, 548 /** redirect as per provided data */ 549 respip_redirect = local_zone_redirect, 550 /** log query source and answer query */ 551 respip_inform = local_zone_inform, 552 /** log query source and don't answer query */ 553 respip_inform_deny = local_zone_inform_deny, 554 /** log query source and redirect */ 555 respip_inform_redirect = local_zone_inform_redirect, 556 /** resolve normally, even when there is response-ip data */ 557 respip_always_transparent = local_zone_always_transparent, 558 /** answer with 'refused' response */ 559 respip_always_refuse = local_zone_always_refuse, 560 /** answer with 'no such domain' response */ 561 respip_always_nxdomain = local_zone_always_nxdomain, 562 /** answer with nodata response */ 563 respip_always_nodata = local_zone_always_nodata, 564 /** answer with nodata response */ 565 respip_always_deny = local_zone_always_deny, 566 567 /* The rest of the values are only possible as 568 * access-control-tag-action */ 569 570 /** serves response data (if any), else, drops queries. */ 571 respip_refuse = local_zone_refuse, 572 /** serves response data, else, nodata answer. */ 573 respip_static = local_zone_static, 574 /** gives response data (if any), else nodata answer. */ 575 respip_transparent = local_zone_transparent, 576 /** gives response data (if any), else nodata answer. */ 577 respip_typetransparent = local_zone_typetransparent, 578 /** type invalid */ 579 respip_invalid = local_zone_invalid, 580 }; 581 582 /** 583 * Get local data from local zone and encode answer. 584 * @param z: local zone to use 585 * @param env: module env 586 * @param qinfo: qinfo 587 * @param edns: edns data, for message encoding 588 * @param repinfo: reply info, for message encoding 589 * @param buf: commpoint buffer 590 * @param temp: scratchpad region 591 * @param labs: number of labels in qname 592 * @param ldp: where to store local data 593 * @param lz_type: type of local zone 594 * @param tag: matching tag index 595 * @param tag_datas: alc specific tag data list 596 * @param tag_datas_size: size of tag_datas 597 * @param tagname: list of names of tags, for logging purpose 598 * @param num_tags: number of tags 599 * @return 1 on success 600 */ 601 int 602 local_data_answer(struct local_zone* z, struct module_env* env, 603 struct query_info* qinfo, struct edns_data* edns, 604 struct comm_reply* repinfo, sldns_buffer* buf, 605 struct regional* temp, int labs, struct local_data** ldp, 606 enum localzone_type lz_type, int tag, struct config_strlist** tag_datas, 607 size_t tag_datas_size, char** tagname, int num_tags); 608 609 /** 610 * Add RR to local zone. 611 * @param z: local zone to add RR to 612 * @param nm: dname of RR 613 * @param nmlen: length of nm 614 * @param nmlabs: number of labels of nm 615 * @param rrtype: RR type 616 * @param rrclass: RR class 617 * @param ttl: TTL of RR to add 618 * @param rdata: RDATA of RR to add 619 * @param rdata_len: length of rdata 620 * @param rrstr: RR in string format, for logging 621 * @return: 1 on success 622 */ 623 int 624 local_zone_enter_rr(struct local_zone* z, uint8_t* nm, size_t nmlen, 625 int nmlabs, uint16_t rrtype, uint16_t rrclass, time_t ttl, 626 uint8_t* rdata, size_t rdata_len, const char* rrstr); 627 628 /** 629 * Find a data node by exact name for a local zone 630 * @param z: local_zone containing data tree 631 * @param nm: name of local-data element to find 632 * @param nmlen: length of nm 633 * @param nmlabs: labs of nm 634 * @return local_data on exact match, NULL otherwise. 635 */ 636 struct local_data* 637 local_zone_find_data(struct local_zone* z, uint8_t* nm, size_t nmlen, int nmlabs); 638 #endif /* SERVICES_LOCALZONE_H */ 639