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