2 * services/localzone.h - local zones authority service.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions to enable local zone authority service.
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 struct packed_rrset_data;
50 struct ub_packed_rrset_key;
57 struct config_strlist;
61 * This type determines processing for queries that did not match
62 * local-data directly.
65 /** unset type, used for unset tag_action elements */
69 /** answer with error */
71 /** answer nxdomain or nodata */
73 /** resolve normally */
74 local_zone_transparent,
75 /** do not block types at localdata names */
76 local_zone_typetransparent,
77 /** answer with data at zone apex */
79 /** remove default AS112 blocking contents for zone
80 * nodefault is used in config not during service. */
82 /** log client address, but no block (transparent) */
84 /** log client address, and block (drop) */
85 local_zone_inform_deny,
86 /** resolve normally, even when there is local data */
87 local_zone_always_transparent,
88 /** answer with error, even when there is local data */
89 local_zone_always_refuse,
90 /** answer with nxdomain, even when there is local data */
91 local_zone_always_nxdomain
95 * Authoritative local zones storage, shared.
98 /** lock on the localzone tree */
100 /** rbtree of struct local_zone */
105 * Local zone. A locally served authoritative zone.
108 /** rbtree node, key is name and class */
110 /** parent zone, if any. */
111 struct local_zone* parent;
113 /** zone name, in uncompressed wireformat */
115 /** length of zone name */
117 /** number of labels in zone name */
119 /** the class of this zone.
120 * uses 'dclass' to not conflict with c++ keyword class. */
123 /** lock on the data in the structure
124 * For the node, parent, name, namelen, namelabs, dclass, you
125 * need to also hold the zones_tree lock to change them (or to
126 * delete this zone) */
129 /** how to process zone */
130 enum localzone_type type;
133 /** length of the taglist (in bytes) */
135 /** netblock addr_tree with struct local_zone_override information
136 * or NULL if there are no override elements */
137 struct rbtree_type* override_tree;
139 /** in this region the zone's data is allocated.
140 * the struct local_zone itself is malloced. */
141 struct regional* region;
142 /** local data for this zone
143 * rbtree of struct local_data */
145 /** if data contains zone apex SOA data, this is a ptr to it. */
146 struct ub_packed_rrset_key* soa;
150 * Local data. One domain name, and the RRs to go with it.
153 /** rbtree node, key is name only */
157 /** length of name */
159 /** number of labels in name */
161 /** the data rrsets, with different types, linked list.
162 * If this list is NULL, the node is an empty non-terminal. */
163 struct local_rrset* rrsets;
171 struct local_rrset* next;
172 /** RRset data item */
173 struct ub_packed_rrset_key* rrset;
177 * Local zone override information
179 struct local_zone_override {
180 /** node in addrtree */
181 struct addr_tree_node node;
182 /** override for local zone type */
183 enum localzone_type type;
187 * Create local zones storage
188 * @return new struct or NULL on error.
190 struct local_zones* local_zones_create(void);
193 * Delete local zones storage
194 * @param zones: to delete.
196 void local_zones_delete(struct local_zones* zones);
199 * Apply config settings; setup the local authoritative data.
200 * Takes care of locking.
201 * @param zones: is set up.
202 * @param cfg: config data.
203 * @return false on error.
205 int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
208 * Compare two local_zone entries in rbtree. Sort hierarchical but not
212 * @return: -1, 0, +1 comparison value.
214 int local_zone_cmp(const void* z1, const void* z2);
217 * Compare two local_data entries in rbtree. Sort canonical.
220 * @return: -1, 0, +1 comparison value.
222 int local_data_cmp(const void* d1, const void* d2);
226 * @param z: to delete.
228 void local_zone_delete(struct local_zone* z);
231 * Lookup zone that contains the given name, class and taglist.
232 * User must lock the tree or result zone.
233 * @param zones: the zones tree
234 * @param name: dname to lookup
235 * @param len: length of name.
236 * @param labs: labelcount of name.
237 * @param dclass: class to lookup.
238 * @param taglist: taglist to lookup.
239 * @param taglen: lenth of taglist.
240 * @param ignoretags: lookup zone by name and class, regardless the
242 * @return closest local_zone or NULL if no covering zone is found.
244 struct local_zone* local_zones_tags_lookup(struct local_zones* zones,
245 uint8_t* name, size_t len, int labs, uint16_t dclass,
246 uint8_t* taglist, size_t taglen, int ignoretags);
249 * Lookup zone that contains the given name, class.
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 * @return closest local_zone or NULL if no covering zone is found.
258 struct local_zone* local_zones_lookup(struct local_zones* zones,
259 uint8_t* name, size_t len, int labs, uint16_t dclass);
262 * Debug helper. Print all zones
263 * Takes care of locking.
264 * @param zones: the zones tree
266 void local_zones_print(struct local_zones* zones);
269 * Answer authoritatively for local zones.
270 * Takes care of locking.
271 * @param zones: the stored zones (shared, read only).
272 * @param env: the module environment.
273 * @param qinfo: query info (parsed).
274 * @param edns: edns info (parsed).
275 * @param buf: buffer with query ID and flags, also for reply.
276 * @param temp: temporary storage region.
277 * @param repinfo: source address for checks. may be NULL.
278 * @param taglist: taglist for checks. May be NULL.
279 * @param taglen: length of the taglist.
280 * @param tagactions: local zone actions for tags. May be NULL.
281 * @param tagactionssize: length of the tagactions.
282 * @param tag_datas: array per tag of strlist with rdata strings. or NULL.
283 * @param tag_datas_size: size of tag_datas array.
284 * @param tagname: array of tag name strings (for debug output).
285 * @param num_tags: number of items in tagname array.
286 * @param view: answer using this view. May be NULL.
287 * @return true if answer is in buffer. false if query is not answered
288 * by authority data. If the reply should be dropped altogether, the return
289 * value is true, but the buffer is cleared (empty).
290 * It can also return true if a non-exact alias answer is found. In this
291 * case qinfo->local_alias points to the corresponding alias RRset but the
292 * answer is NOT encoded in buffer. It's the caller's responsibility to
293 * complete the alias chain (if needed) and encode the final set of answer.
294 * Data pointed to by qinfo->local_alias is allocated in 'temp' or refers to
295 * configuration data. So the caller will need to make a deep copy of it
296 * if it needs to keep it beyond the lifetime of 'temp' or a dynamic update
297 * to local zone data.
299 int local_zones_answer(struct local_zones* zones, struct module_env* env,
300 struct query_info* qinfo, struct edns_data* edns, struct sldns_buffer* buf,
301 struct regional* temp, struct comm_reply* repinfo, uint8_t* taglist,
302 size_t taglen, uint8_t* tagactions, size_t tagactionssize,
303 struct config_strlist** tag_datas, size_t tag_datas_size,
304 char** tagname, int num_tags, struct view* view);
307 * Parse the string into localzone type.
309 * @param str: string to parse
310 * @param t: local zone type returned here.
311 * @return 0 on parse error.
313 int local_zone_str2type(const char* str, enum localzone_type* t);
316 * Print localzone type to a string. Pointer to a constant string.
318 * @param t: local zone type.
319 * @return constant string that describes type.
321 const char* local_zone_type2str(enum localzone_type t);
324 * Find zone that with exactly given name, class.
325 * User must lock the tree or result zone.
326 * @param zones: the zones tree
327 * @param name: dname to lookup
328 * @param len: length of name.
329 * @param labs: labelcount of name.
330 * @param dclass: class to lookup.
331 * @return the exact local_zone or NULL.
333 struct local_zone* local_zones_find(struct local_zones* zones,
334 uint8_t* name, size_t len, int labs, uint16_t dclass);
337 * Add a new zone. Caller must hold the zones lock.
338 * Adjusts the other zones as well (parent pointers) after insertion.
339 * The zone must NOT exist (returns NULL and logs error).
340 * @param zones: the zones tree
341 * @param name: dname to add
342 * @param len: length of name.
343 * @param labs: labelcount of name.
344 * @param dclass: class to add.
346 * @return local_zone or NULL on error, caller must printout memory error.
348 struct local_zone* local_zones_add_zone(struct local_zones* zones,
349 uint8_t* name, size_t len, int labs, uint16_t dclass,
350 enum localzone_type tp);
353 * Delete a zone. Caller must hold the zones lock.
354 * Adjusts the other zones as well (parent pointers) after insertion.
355 * @param zones: the zones tree
356 * @param zone: the zone to delete from tree. Also deletes zone from memory.
358 void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
361 * Add RR data into the localzone data.
362 * Looks up the zone, if no covering zone, a transparent zone with the
363 * name of the RR is created.
364 * @param zones: the zones tree. Not locked by caller.
365 * @param rr: string with on RR.
366 * @return false on failure.
368 int local_zones_add_RR(struct local_zones* zones, const char* rr);
371 * Remove data from domain name in the tree.
372 * All types are removed. No effect if zone or name does not exist.
373 * @param zones: zones tree.
374 * @param name: dname to remove
375 * @param len: length of name.
376 * @param labs: labelcount of name.
377 * @param dclass: class to remove.
379 void local_zones_del_data(struct local_zones* zones,
380 uint8_t* name, size_t len, int labs, uint16_t dclass);
384 * Form wireformat from text format domain name.
385 * @param str: the domain name in text "www.example.com"
386 * @param res: resulting wireformat is stored here with malloc.
387 * @param len: length of resulting wireformat.
388 * @param labs: number of labels in resulting wireformat.
389 * @return false on error, syntax or memory. Also logged.
391 int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
394 * Find local data tag string match for the given type (in qinfo) in the list.
395 * If found, 'r' will be filled with corresponding rrset information.
396 * @param qinfo: contains name, type, and class for the data
397 * @param list: stores local tag data to be searched
398 * @param r: rrset key to be filled for matched data
399 * @param temp: region to allocate rrset in 'r'
400 * @return 1 if a match is found and rrset is built; otherwise 0 including
403 int local_data_find_tag_datas(const struct query_info* qinfo,
404 struct config_strlist* list, struct ub_packed_rrset_key* r,
405 struct regional* temp);
408 * See if two sets of tag lists (in the form of bitmap) have the same tag that
409 * has an action. If so, '*tag' will be set to the found tag index, and the
410 * corresponding action will be returned in the form of local zone type.
411 * Otherwise the passed type (lzt) will be returned as the default action.
412 * Pointers except tagactions must not be NULL.
413 * @param taglist: 1st list of tags
414 * @param taglen: size of taglist in bytes
415 * @param taglist2: 2nd list of tags
416 * @param taglen2: size of taglist2 in bytes
417 * @param tagactions: local data actions for tags. May be NULL.
418 * @param tagactionssize: length of the tagactions.
419 * @param lzt: default action (local zone type) if no tag action is found.
420 * @param tag: see above.
421 * @param tagname: array of tag name strings (for debug output).
422 * @param num_tags: number of items in tagname array.
423 * @return found tag action or the default action.
425 enum localzone_type local_data_find_tag_action(const uint8_t* taglist,
426 size_t taglen, const uint8_t* taglist2, size_t taglen2,
427 const uint8_t* tagactions, size_t tagactionssize,
428 enum localzone_type lzt, int* tag, char* const* tagname, int num_tags);
431 * Parses resource record string into wire format, also returning its field values.
432 * @param str: input resource record
433 * @param nm: domain name field
434 * @param type: record type field
435 * @param dclass: record class field
436 * @param ttl: ttl field
437 * @param rr: buffer for the parsed rr in wire format
438 * @param len: buffer length
439 * @param rdata: rdata field
440 * @param rdata_len: rdata field length
441 * @return 1 on success; 0 otherwise.
443 int rrstr_get_rr_content(const char* str, uint8_t** nm, uint16_t* type,
444 uint16_t* dclass, time_t* ttl, uint8_t* rr, size_t len,
445 uint8_t** rdata, size_t* rdata_len);
448 * Insert specified rdata into the specified resource record.
449 * @param region: allocator
450 * @param pd: data portion of the destination resource record
451 * @param rdata: source rdata
452 * @param rdata_len: source rdata length
453 * @param ttl: time to live
454 * @param rrstr: resource record in text form (for logging)
455 * @return 1 on success; 0 otherwise.
457 int rrset_insert_rr(struct regional* region, struct packed_rrset_data* pd,
458 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr);
461 * Valid response ip actions for the IP-response-driven-action feature;
462 * defined here instead of in the respip module to enable sharing of enum
463 * values with the localzone_type enum.
464 * Note that these values except 'none' are the same as localzone types of
465 * the 'same semantics'. It's intentional as we use these values via
466 * access-control-tags, which can be shared for both response ip actions and
470 /** no respip action */
471 respip_none = local_zone_unset,
473 respip_deny = local_zone_deny,
474 /** redirect as per provided data */
475 respip_redirect = local_zone_redirect,
476 /** log query source and answer query */
477 respip_inform = local_zone_inform,
478 /** log query source and don't answer query */
479 respip_inform_deny = local_zone_inform_deny,
480 /** resolve normally, even when there is response-ip data */
481 respip_always_transparent = local_zone_always_transparent,
482 /** answer with 'refused' response */
483 respip_always_refuse = local_zone_always_refuse,
484 /** answer with 'no such domain' response */
485 respip_always_nxdomain = local_zone_always_nxdomain,
487 /* The rest of the values are only possible as
488 * access-control-tag-action */
490 /** serves response data (if any), else, drops queries. */
491 respip_refuse = local_zone_refuse,
492 /** serves response data, else, nodata answer. */
493 respip_static = local_zone_static,
494 /** gives response data (if any), else nodata answer. */
495 respip_transparent = local_zone_transparent,
496 /** gives response data (if any), else nodata answer. */
497 respip_typetransparent = local_zone_typetransparent,
500 #endif /* SERVICES_LOCALZONE_H */