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