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