2 * services/localzone.h - local zones authority service.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions to enable local zone authority service.
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 struct ub_packed_rrset_key;
54 struct config_strlist;
58 * This type determines processing for queries that did not match
59 * local-data directly.
64 /** answer with error */
66 /** answer nxdomain or nodata */
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 */
74 /** remove default AS112 blocking contents for zone
75 * nodefault is used in config not during service. */
77 /** log client address, but no block (transparent) */
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
90 * Authoritative local zones storage, shared.
93 /** lock on the localzone tree */
95 /** rbtree of struct local_zone */
100 * Local zone. A locally served authoritative zone.
103 /** rbtree node, key is name and class */
105 /** parent zone, if any. */
106 struct local_zone* parent;
108 /** zone name, in uncompressed wireformat */
110 /** length of zone name */
112 /** number of labels in zone name */
114 /** the class of this zone.
115 * uses 'dclass' to not conflict with c++ keyword class. */
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) */
124 /** how to process zone */
125 enum localzone_type type;
128 /** length of the taglist (in bytes) */
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;
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 */
140 /** if data contains zone apex SOA data, this is a ptr to it. */
141 struct ub_packed_rrset_key* soa;
145 * Local data. One domain name, and the RRs to go with it.
148 /** rbtree node, key is name only */
152 /** length of name */
154 /** number of labels in name */
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;
166 struct local_rrset* next;
167 /** RRset data item */
168 struct ub_packed_rrset_key* rrset;
172 * Local zone override information
174 struct local_zone_override {
175 /** node in addrtree */
176 struct addr_tree_node node;
177 /** override for local zone type */
178 enum localzone_type type;
182 * Create local zones storage
183 * @return new struct or NULL on error.
185 struct local_zones* local_zones_create(void);
188 * Delete local zones storage
189 * @param zones: to delete.
191 void local_zones_delete(struct local_zones* zones);
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.
200 int local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg);
203 * Compare two local_zone entries in rbtree. Sort hierarchical but not
207 * @return: -1, 0, +1 comparison value.
209 int local_zone_cmp(const void* z1, const void* z2);
212 * Compare two local_data entries in rbtree. Sort canonical.
215 * @return: -1, 0, +1 comparison value.
217 int local_data_cmp(const void* d1, const void* d2);
221 * @param z: to delete.
223 void local_zone_delete(struct local_zone* z);
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
237 * @return closest local_zone or NULL if no covering zone is found.
239 struct 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);
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.
253 struct local_zone* local_zones_lookup(struct local_zones* zones,
254 uint8_t* name, size_t len, int labs, uint16_t dclass);
257 * Debug helper. Print all zones
258 * Takes care of locking.
259 * @param zones: the zones tree
261 void local_zones_print(struct local_zones* zones);
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).
284 int 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);
292 * Parse the string into localzone type.
294 * @param str: string to parse
295 * @param t: local zone type returned here.
296 * @return 0 on parse error.
298 int local_zone_str2type(const char* str, enum localzone_type* t);
301 * Print localzone type to a string. Pointer to a constant string.
303 * @param t: local zone type.
304 * @return constant string that describes type.
306 const char* local_zone_type2str(enum localzone_type t);
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.
318 struct local_zone* local_zones_find(struct local_zones* zones,
319 uint8_t* name, size_t len, int labs, uint16_t dclass);
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.
331 * @return local_zone or NULL on error, caller must printout memory error.
333 struct 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);
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.
343 void local_zones_del_zone(struct local_zones* zones, struct local_zone* zone);
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.
353 int local_zones_add_RR(struct local_zones* zones, const char* rr);
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.
364 void local_zones_del_data(struct local_zones* zones,
365 uint8_t* name, size_t len, int labs, uint16_t dclass);
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.
376 int parse_dname(const char* str, uint8_t** res, size_t* len, int* labs);
378 #endif /* SERVICES_LOCALZONE_H */