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