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