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