localzone.h revision 307729
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"
47struct ub_packed_rrset_key;
48struct regional;
49struct config_file;
50struct edns_data;
51struct query_info;
52struct sldns_buffer;
53struct comm_reply;
54struct config_strlist;
55
56/**
57 * Local zone type
58 * This type determines processing for queries that did not match
59 * local-data directly.
60 */
61enum localzone_type {
62	/** drop query */
63	local_zone_deny = 0,
64	/** answer with error */
65	local_zone_refuse,
66	/** answer nxdomain or nodata */
67	local_zone_static,
68	/** resolve normally */
69	local_zone_transparent,
70	/** do not block types at localdata names */
71	local_zone_typetransparent,
72	/** answer with data at zone apex */
73	local_zone_redirect,
74	/** remove default AS112 blocking contents for zone
75	 * nodefault is used in config not during service. */
76	local_zone_nodefault,
77	/** log client address, but no block (transparent) */
78	local_zone_inform,
79	/** log client address, and block (drop) */
80	local_zone_inform_deny,
81	/** resolve normally, even when there is local data */
82	local_zone_always_transparent,
83	/** answer with error, even when there is local data */
84	local_zone_always_refuse,
85	/** answer with nxdomain, even when there is local data */
86	local_zone_always_nxdomain
87};
88
89/**
90 * Authoritative local zones storage, shared.
91 */
92struct local_zones {
93	/** lock on the localzone tree */
94	lock_rw_t lock;
95	/** rbtree of struct local_zone */
96	rbtree_t ztree;
97};
98
99/**
100 * Local zone. A locally served authoritative zone.
101 */
102struct local_zone {
103	/** rbtree node, key is name and class */
104	rbnode_t node;
105	/** parent zone, if any. */
106	struct local_zone* parent;
107
108	/** zone name, in uncompressed wireformat */
109	uint8_t* name;
110	/** length of zone name */
111	size_t namelen;
112	/** number of labels in zone name */
113	int namelabs;
114	/** the class of this zone.
115	 * uses 'dclass' to not conflict with c++ keyword class. */
116	uint16_t dclass;
117
118	/** lock on the data in the structure
119	 * For the node, parent, name, namelen, namelabs, dclass, you
120	 * need to also hold the zones_tree lock to change them (or to
121	 * delete this zone) */
122	lock_rw_t lock;
123
124	/** how to process zone */
125	enum localzone_type type;
126	/** tag bitlist */
127	uint8_t* taglist;
128	/** length of the taglist (in bytes) */
129	size_t taglen;
130	/** netblock addr_tree with struct local_zone_override information
131	 * or NULL if there are no override elements */
132	struct rbtree_t* override_tree;
133
134	/** in this region the zone's data is allocated.
135	 * the struct local_zone itself is malloced. */
136	struct regional* region;
137	/** local data for this zone
138	 * rbtree of struct local_data */
139	rbtree_t data;
140	/** if data contains zone apex SOA data, this is a ptr to it. */
141	struct ub_packed_rrset_key* soa;
142};
143
144/**
145 * Local data. One domain name, and the RRs to go with it.
146 */
147struct local_data {
148	/** rbtree node, key is name only */
149	rbnode_t node;
150	/** domain name */
151	uint8_t* name;
152	/** length of name */
153	size_t namelen;
154	/** number of labels in name */
155	int namelabs;
156	/** the data rrsets, with different types, linked list.
157	 * If this list is NULL, the node is an empty non-terminal. */
158	struct local_rrset* rrsets;
159};
160
161/**
162 * A local data RRset
163 */
164struct local_rrset {
165	/** next in list */
166	struct local_rrset* next;
167	/** RRset data item */
168	struct ub_packed_rrset_key* rrset;
169};
170
171/**
172 * Local zone override information
173 */
174struct local_zone_override {
175	/** node in addrtree */
176	struct addr_tree_node node;
177	/** override for local zone type */
178	enum localzone_type type;
179};
180
181/**
182 * Create local zones storage
183 * @return new struct or NULL on error.
184 */
185struct local_zones* local_zones_create(void);
186
187/**
188 * Delete local zones storage
189 * @param zones: to delete.
190 */
191void local_zones_delete(struct local_zones* zones);
192
193/**
194 * Apply config settings; setup the local authoritative data.
195 * Takes care of locking.
196 * @param zones: is set up.
197 * @param cfg: config data.
198 * @return false on error.
199 */
200int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
201
202/**
203 * Compare two local_zone entries in rbtree. Sort hierarchical but not
204 * canonical
205 * @param z1: zone 1
206 * @param z2: zone 2
207 * @return: -1, 0, +1 comparison value.
208 */
209int local_zone_cmp(const void* z1, const void* z2);
210
211/**
212 * Compare two local_data entries in rbtree. Sort canonical.
213 * @param d1: data 1
214 * @param d2: data 2
215 * @return: -1, 0, +1 comparison value.
216 */
217int local_data_cmp(const void* d1, const void* d2);
218
219/**
220 * Delete one zone
221 * @param z: to delete.
222 */
223void local_zone_delete(struct local_zone* z);
224
225/**
226 * Lookup zone that contains the given name, class and taglist.
227 * User must lock the tree or result zone.
228 * @param zones: the zones tree
229 * @param name: dname to lookup
230 * @param len: length of name.
231 * @param labs: labelcount of name.
232 * @param dclass: class to lookup.
233 * @param taglist: taglist to lookup.
234 * @param taglen: lenth of taglist.
235 * @param ignoretags: lookup zone by name and class, regardless the
236 * local-zone's tags.
237 * @return closest local_zone or NULL if no covering zone is found.
238 */
239struct local_zone* local_zones_tags_lookup(struct local_zones* zones,
240	uint8_t* name, size_t len, int labs, uint16_t dclass,
241	uint8_t* taglist, size_t taglen, int ignoretags);
242
243/**
244 * Lookup zone that contains the given name, class.
245 * User must lock the tree or result zone.
246 * @param zones: the zones tree
247 * @param name: dname to lookup
248 * @param len: length of name.
249 * @param labs: labelcount of name.
250 * @param dclass: class to lookup.
251 * @return closest local_zone or NULL if no covering zone is found.
252 */
253struct local_zone* local_zones_lookup(struct local_zones* zones,
254	uint8_t* name, size_t len, int labs, uint16_t dclass);
255
256/**
257 * Debug helper. Print all zones
258 * Takes care of locking.
259 * @param zones: the zones tree
260 */
261void local_zones_print(struct local_zones* zones);
262
263/**
264 * Answer authoritatively for local zones.
265 * Takes care of locking.
266 * @param zones: the stored zones (shared, read only).
267 * @param qinfo: query info (parsed).
268 * @param edns: edns info (parsed).
269 * @param buf: buffer with query ID and flags, also for reply.
270 * @param temp: temporary storage region.
271 * @param repinfo: source address for checks. may be NULL.
272 * @param taglist: taglist for checks. May be NULL.
273 * @param taglen: length of the taglist.
274 * @param tagactions: local zone actions for tags. May be NULL.
275 * @param tagactionssize: length of the tagactions.
276 * @param tag_datas: array per tag of strlist with rdata strings. or NULL.
277 * @param tag_datas_size: size of tag_datas array.
278 * @param tagname: array of tag name strings (for debug output).
279 * @param num_tags: number of items in tagname array.
280 * @return true if answer is in buffer. false if query is not answered
281 * by authority data. If the reply should be dropped altogether, the return
282 * value is true, but the buffer is cleared (empty).
283 */
284int local_zones_answer(struct local_zones* zones, struct query_info* qinfo,
285	struct edns_data* edns, struct sldns_buffer* buf, struct regional* temp,
286	struct comm_reply* repinfo, uint8_t* taglist, size_t taglen,
287	uint8_t* tagactions, size_t tagactionssize,
288	struct config_strlist** tag_datas, size_t tag_datas_size,
289	char** tagname, int num_tags);
290
291/**
292 * Parse the string into localzone type.
293 *
294 * @param str: string to parse
295 * @param t: local zone type returned here.
296 * @return 0 on parse error.
297 */
298int local_zone_str2type(const char* str, enum localzone_type* t);
299
300/**
301 * Print localzone type to a string.  Pointer to a constant string.
302 *
303 * @param t: local zone type.
304 * @return constant string that describes type.
305 */
306const char* local_zone_type2str(enum localzone_type t);
307
308/**
309 * Find zone that with exactly given name, class.
310 * User must lock the tree or result zone.
311 * @param zones: the zones tree
312 * @param name: dname to lookup
313 * @param len: length of name.
314 * @param labs: labelcount of name.
315 * @param dclass: class to lookup.
316 * @return the exact local_zone or NULL.
317 */
318struct local_zone* local_zones_find(struct local_zones* zones,
319	uint8_t* name, size_t len, int labs, uint16_t dclass);
320
321/**
322 * Add a new zone. Caller must hold the zones lock.
323 * Adjusts the other zones as well (parent pointers) after insertion.
324 * The zone must NOT exist (returns NULL and logs error).
325 * @param zones: the zones tree
326 * @param name: dname to add
327 * @param len: length of name.
328 * @param labs: labelcount of name.
329 * @param dclass: class to add.
330 * @param tp: type.
331 * @return local_zone or NULL on error, caller must printout memory error.
332 */
333struct local_zone* local_zones_add_zone(struct local_zones* zones,
334	uint8_t* name, size_t len, int labs, uint16_t dclass,
335	enum localzone_type tp);
336
337/**
338 * Delete a zone. Caller must hold the zones lock.
339 * Adjusts the other zones as well (parent pointers) after insertion.
340 * @param zones: the zones tree
341 * @param zone: the zone to delete from tree. Also deletes zone from memory.
342 */
343void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
344
345/**
346 * Add RR data into the localzone data.
347 * Looks up the zone, if no covering zone, a transparent zone with the
348 * name of the RR is created.
349 * @param zones: the zones tree. Not locked by caller.
350 * @param rr: string with on RR.
351 * @return false on failure.
352 */
353int local_zones_add_RR(struct local_zones* zones, const char* rr);
354
355/**
356 * Remove data from domain name in the tree.
357 * All types are removed. No effect if zone or name does not exist.
358 * @param zones: zones tree.
359 * @param name: dname to remove
360 * @param len: length of name.
361 * @param labs: labelcount of name.
362 * @param dclass: class to remove.
363 */
364void local_zones_del_data(struct local_zones* zones,
365	uint8_t* name, size_t len, int labs, uint16_t dclass);
366
367
368/**
369 * Form wireformat from text format domain name.
370 * @param str: the domain name in text "www.example.com"
371 * @param res: resulting wireformat is stored here with malloc.
372 * @param len: length of resulting wireformat.
373 * @param labs: number of labels in resulting wireformat.
374 * @return false on error, syntax or memory. Also logged.
375 */
376int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
377
378#endif /* SERVICES_LOCALZONE_H */
379