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