1 /*
2 * services/localzone.c - 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 #include "config.h"
42 #include "services/localzone.h"
43 #include "sldns/str2wire.h"
44 #include "util/regional.h"
45 #include "util/config_file.h"
46 #include "util/data/dname.h"
47 #include "util/data/packed_rrset.h"
48 #include "util/data/msgencode.h"
49 #include "util/net_help.h"
50 #include "util/netevent.h"
51 #include "util/data/msgreply.h"
52 #include "util/data/msgparse.h"
53 #include "util/as112.h"
54
55 /* maximum RRs in an RRset, to cap possible 'endless' list RRs.
56 * with 16 bytes for an A record, a 64K packet has about 4000 max */
57 #define LOCALZONE_RRSET_COUNT_MAX 4096
58
59 /** print all RRsets in local zone */
60 static void
local_zone_out(struct local_zone * z)61 local_zone_out(struct local_zone* z)
62 {
63 struct local_data* d;
64 struct local_rrset* p;
65 RBTREE_FOR(d, struct local_data*, &z->data) {
66 for(p = d->rrsets; p; p = p->next) {
67 log_nametypeclass(NO_VERBOSE, "rrset", d->name,
68 ntohs(p->rrset->rk.type),
69 ntohs(p->rrset->rk.rrset_class));
70 }
71 }
72 }
73
74 static void
local_zone_print(struct local_zone * z)75 local_zone_print(struct local_zone* z)
76 {
77 char buf[64];
78 lock_rw_rdlock(&z->lock);
79 snprintf(buf, sizeof(buf), "%s zone",
80 local_zone_type2str(z->type));
81 log_nametypeclass(NO_VERBOSE, buf, z->name, 0, z->dclass);
82 local_zone_out(z);
83 lock_rw_unlock(&z->lock);
84 }
85
local_zones_print(struct local_zones * zones)86 void local_zones_print(struct local_zones* zones)
87 {
88 struct local_zone* z;
89 lock_rw_rdlock(&zones->lock);
90 log_info("number of auth zones %u", (unsigned)zones->ztree.count);
91 RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
92 local_zone_print(z);
93 }
94 lock_rw_unlock(&zones->lock);
95 }
96
97 struct local_zones*
local_zones_create(void)98 local_zones_create(void)
99 {
100 struct local_zones* zones = (struct local_zones*)calloc(1,
101 sizeof(*zones));
102 if(!zones)
103 return NULL;
104 rbtree_init(&zones->ztree, &local_zone_cmp);
105 lock_rw_init(&zones->lock);
106 lock_protect(&zones->lock, &zones->ztree, sizeof(zones->ztree));
107 /* also lock protects the rbnode's in struct local_zone */
108 return zones;
109 }
110
111 /** helper traverse to delete zones */
112 static void
lzdel(rbnode_type * n,void * ATTR_UNUSED (arg))113 lzdel(rbnode_type* n, void* ATTR_UNUSED(arg))
114 {
115 struct local_zone* z = (struct local_zone*)n->key;
116 local_zone_delete(z);
117 }
118
119 void
local_zones_delete(struct local_zones * zones)120 local_zones_delete(struct local_zones* zones)
121 {
122 if(!zones)
123 return;
124 lock_rw_destroy(&zones->lock);
125 /* walk through zones and delete them all */
126 traverse_postorder(&zones->ztree, lzdel, NULL);
127 free(zones);
128 }
129
130 void
local_zone_delete(struct local_zone * z)131 local_zone_delete(struct local_zone* z)
132 {
133 if(!z)
134 return;
135 lock_rw_destroy(&z->lock);
136 regional_destroy(z->region);
137 free(z->name);
138 free(z->taglist);
139 free(z);
140 }
141
142 int
local_zone_cmp(const void * z1,const void * z2)143 local_zone_cmp(const void* z1, const void* z2)
144 {
145 /* first sort on class, so that hierarchy can be maintained within
146 * a class */
147 struct local_zone* a = (struct local_zone*)z1;
148 struct local_zone* b = (struct local_zone*)z2;
149 int m;
150 if(a->dclass != b->dclass) {
151 if(a->dclass < b->dclass)
152 return -1;
153 return 1;
154 }
155 return dname_lab_cmp(a->name, a->namelabs, b->name, b->namelabs, &m);
156 }
157
158 int
local_data_cmp(const void * d1,const void * d2)159 local_data_cmp(const void* d1, const void* d2)
160 {
161 struct local_data* a = (struct local_data*)d1;
162 struct local_data* b = (struct local_data*)d2;
163 int m;
164 return dname_canon_lab_cmp(a->name, a->namelabs, b->name,
165 b->namelabs, &m);
166 }
167
168 /* form wireformat from text format domain name */
169 int
parse_dname(const char * str,uint8_t ** res,size_t * len,int * labs)170 parse_dname(const char* str, uint8_t** res, size_t* len, int* labs)
171 {
172 *res = sldns_str2wire_dname(str, len);
173 *labs = 0;
174 if(!*res) {
175 log_err("cannot parse name %s", str);
176 return 0;
177 }
178 *labs = dname_count_size_labels(*res, len);
179 return 1;
180 }
181
182 /** create a new localzone */
183 static struct local_zone*
local_zone_create(uint8_t * nm,size_t len,int labs,enum localzone_type t,uint16_t dclass)184 local_zone_create(uint8_t* nm, size_t len, int labs,
185 enum localzone_type t, uint16_t dclass)
186 {
187 struct local_zone* z = (struct local_zone*)calloc(1, sizeof(*z));
188 if(!z) {
189 return NULL;
190 }
191 z->node.key = z;
192 z->dclass = dclass;
193 z->type = t;
194 z->name = nm;
195 z->namelen = len;
196 z->namelabs = labs;
197 lock_rw_init(&z->lock);
198 z->region = regional_create_nochunk(sizeof(struct regional));
199 if(!z->region) {
200 free(z);
201 return NULL;
202 }
203 rbtree_init(&z->data, &local_data_cmp);
204 lock_protect(&z->lock, &z->parent, sizeof(*z)-sizeof(rbnode_type));
205 /* also the zones->lock protects node, parent, name*, class */
206 return z;
207 }
208
209 /** enter a new zone with allocated dname returns with WRlock */
210 static struct local_zone*
lz_enter_zone_dname(struct local_zones * zones,uint8_t * nm,size_t len,int labs,enum localzone_type t,uint16_t c)211 lz_enter_zone_dname(struct local_zones* zones, uint8_t* nm, size_t len,
212 int labs, enum localzone_type t, uint16_t c)
213 {
214 struct local_zone* z = local_zone_create(nm, len, labs, t, c);
215 if(!z) {
216 free(nm);
217 log_err("out of memory");
218 return NULL;
219 }
220
221 /* add to rbtree */
222 lock_rw_wrlock(&zones->lock);
223 lock_rw_wrlock(&z->lock);
224 if(!rbtree_insert(&zones->ztree, &z->node)) {
225 struct local_zone* oldz;
226 char str[256];
227 dname_str(nm, str);
228 log_warn("duplicate local-zone %s", str);
229 lock_rw_unlock(&z->lock);
230 /* save zone name locally before deallocation,
231 * otherwise, nm is gone if we zone_delete now. */
232 oldz = z;
233 /* find the correct zone, so not an error for duplicate */
234 z = local_zones_find(zones, nm, len, labs, c);
235 lock_rw_wrlock(&z->lock);
236 lock_rw_unlock(&zones->lock);
237 local_zone_delete(oldz);
238 return z;
239 }
240 lock_rw_unlock(&zones->lock);
241 return z;
242 }
243
244 /** enter a new zone */
245 struct local_zone*
lz_enter_zone(struct local_zones * zones,const char * name,const char * type,uint16_t dclass)246 lz_enter_zone(struct local_zones* zones, const char* name, const char* type,
247 uint16_t dclass)
248 {
249 struct local_zone* z;
250 enum localzone_type t;
251 uint8_t* nm;
252 size_t len;
253 int labs;
254 if(!parse_dname(name, &nm, &len, &labs)) {
255 log_err("bad zone name %s %s", name, type);
256 return NULL;
257 }
258 if(!local_zone_str2type(type, &t)) {
259 log_err("bad lz_enter_zone type %s %s", name, type);
260 free(nm);
261 return NULL;
262 }
263 if(!(z=lz_enter_zone_dname(zones, nm, len, labs, t, dclass))) {
264 log_err("could not enter zone %s %s", name, type);
265 return NULL;
266 }
267 return z;
268 }
269
270 int
rrstr_get_rr_content(const char * str,uint8_t ** nm,uint16_t * type,uint16_t * dclass,time_t * ttl,uint8_t * rr,size_t len,uint8_t ** rdata,size_t * rdata_len)271 rrstr_get_rr_content(const char* str, uint8_t** nm, uint16_t* type,
272 uint16_t* dclass, time_t* ttl, uint8_t* rr, size_t len,
273 uint8_t** rdata, size_t* rdata_len)
274 {
275 size_t dname_len = 0;
276 int e = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600,
277 NULL, 0, NULL, 0);
278 if(e) {
279 log_err("error parsing local-data at %d: '%s': %s",
280 LDNS_WIREPARSE_OFFSET(e), str,
281 sldns_get_errorstr_parse(e));
282 return 0;
283 }
284 *nm = memdup(rr, dname_len);
285 if(!*nm) {
286 log_err("out of memory");
287 return 0;
288 }
289 *dclass = sldns_wirerr_get_class(rr, len, dname_len);
290 *type = sldns_wirerr_get_type(rr, len, dname_len);
291 *ttl = (time_t)sldns_wirerr_get_ttl(rr, len, dname_len);
292 *rdata = sldns_wirerr_get_rdatawl(rr, len, dname_len);
293 *rdata_len = sldns_wirerr_get_rdatalen(rr, len, dname_len)+2;
294 return 1;
295 }
296
297 /** return name and class of rr; parses string */
298 static int
get_rr_nameclass(const char * str,uint8_t ** nm,uint16_t * dclass,uint16_t * dtype)299 get_rr_nameclass(const char* str, uint8_t** nm, uint16_t* dclass,
300 uint16_t* dtype)
301 {
302 uint8_t rr[LDNS_RR_BUF_SIZE];
303 size_t len = sizeof(rr), dname_len = 0;
304 int s = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600,
305 NULL, 0, NULL, 0);
306 if(s != 0) {
307 log_err("error parsing local-data at %d '%s': %s",
308 LDNS_WIREPARSE_OFFSET(s), str,
309 sldns_get_errorstr_parse(s));
310 return 0;
311 }
312 *nm = memdup(rr, dname_len);
313 *dclass = sldns_wirerr_get_class(rr, len, dname_len);
314 *dtype = sldns_wirerr_get_type(rr, len, dname_len);
315 if(!*nm) {
316 log_err("out of memory");
317 return 0;
318 }
319 return 1;
320 }
321
322 /**
323 * Find an rrset in local data structure.
324 * @param data: local data domain name structure.
325 * @param type: type to look for (host order).
326 * @param alias_ok: 1 if matching a non-exact, alias type such as CNAME is
327 * allowed. otherwise 0.
328 * @return rrset pointer or NULL if not found.
329 */
330 static struct local_rrset*
local_data_find_type(struct local_data * data,uint16_t type,int alias_ok)331 local_data_find_type(struct local_data* data, uint16_t type, int alias_ok)
332 {
333 struct local_rrset* p, *cname = NULL;
334 type = htons(type);
335 for(p = data->rrsets; p; p = p->next) {
336 if(p->rrset->rk.type == type)
337 return p;
338 if(alias_ok && p->rrset->rk.type == htons(LDNS_RR_TYPE_CNAME))
339 cname = p;
340 }
341 if(alias_ok)
342 return cname;
343 return NULL;
344 }
345
346 /** check for RR duplicates */
347 static int
rr_is_duplicate(struct packed_rrset_data * pd,uint8_t * rdata,size_t rdata_len)348 rr_is_duplicate(struct packed_rrset_data* pd, uint8_t* rdata, size_t rdata_len)
349 {
350 size_t i;
351 for(i=0; i<pd->count; i++) {
352 if(pd->rr_len[i] == rdata_len &&
353 memcmp(pd->rr_data[i], rdata, rdata_len) == 0)
354 return 1;
355 }
356 return 0;
357 }
358
359 /** new local_rrset */
360 static struct local_rrset*
new_local_rrset(struct regional * region,struct local_data * node,uint16_t rrtype,uint16_t rrclass)361 new_local_rrset(struct regional* region, struct local_data* node,
362 uint16_t rrtype, uint16_t rrclass)
363 {
364 struct packed_rrset_data* pd;
365 struct local_rrset* rrset = (struct local_rrset*)
366 regional_alloc_zero(region, sizeof(*rrset));
367 if(!rrset) {
368 log_err("out of memory");
369 return NULL;
370 }
371 rrset->next = node->rrsets;
372 node->rrsets = rrset;
373 rrset->rrset = (struct ub_packed_rrset_key*)
374 regional_alloc_zero(region, sizeof(*rrset->rrset));
375 if(!rrset->rrset) {
376 log_err("out of memory");
377 return NULL;
378 }
379 rrset->rrset->entry.key = rrset->rrset;
380 pd = (struct packed_rrset_data*)regional_alloc_zero(region,
381 sizeof(*pd));
382 if(!pd) {
383 log_err("out of memory");
384 return NULL;
385 }
386 pd->trust = rrset_trust_prim_noglue;
387 pd->security = sec_status_insecure;
388 rrset->rrset->entry.data = pd;
389 rrset->rrset->rk.dname = node->name;
390 rrset->rrset->rk.dname_len = node->namelen;
391 rrset->rrset->rk.type = htons(rrtype);
392 rrset->rrset->rk.rrset_class = htons(rrclass);
393 return rrset;
394 }
395
396 /** insert RR into RRset data structure; Wastes a couple of bytes */
397 int
rrset_insert_rr(struct regional * region,struct packed_rrset_data * pd,uint8_t * rdata,size_t rdata_len,time_t ttl,const char * rrstr)398 rrset_insert_rr(struct regional* region, struct packed_rrset_data* pd,
399 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr)
400 {
401 size_t* oldlen = pd->rr_len;
402 time_t* oldttl = pd->rr_ttl;
403 uint8_t** olddata = pd->rr_data;
404
405 /* add RR to rrset */
406 if(pd->count > LOCALZONE_RRSET_COUNT_MAX) {
407 log_warn("RRset '%s' has more than %d records, record ignored",
408 rrstr, LOCALZONE_RRSET_COUNT_MAX);
409 return 1;
410 }
411 pd->count++;
412 pd->rr_len = regional_alloc(region, sizeof(*pd->rr_len)*pd->count);
413 pd->rr_ttl = regional_alloc(region, sizeof(*pd->rr_ttl)*pd->count);
414 pd->rr_data = regional_alloc(region, sizeof(*pd->rr_data)*pd->count);
415 if(!pd->rr_len || !pd->rr_ttl || !pd->rr_data) {
416 log_err("out of memory");
417 return 0;
418 }
419 if(pd->count > 1) {
420 memcpy(pd->rr_len+1, oldlen,
421 sizeof(*pd->rr_len)*(pd->count-1));
422 memcpy(pd->rr_ttl+1, oldttl,
423 sizeof(*pd->rr_ttl)*(pd->count-1));
424 memcpy(pd->rr_data+1, olddata,
425 sizeof(*pd->rr_data)*(pd->count-1));
426 }
427 pd->rr_len[0] = rdata_len;
428 pd->rr_ttl[0] = ttl;
429 pd->rr_data[0] = regional_alloc_init(region, rdata, rdata_len);
430 if(!pd->rr_data[0]) {
431 log_err("out of memory");
432 return 0;
433 }
434 return 1;
435 }
436
437 /** Delete RR from local-zone RRset, wastes memory as the deleted RRs cannot be
438 * free'd (regionally alloc'd) */
439 int
local_rrset_remove_rr(struct packed_rrset_data * pd,size_t index)440 local_rrset_remove_rr(struct packed_rrset_data* pd, size_t index)
441 {
442 log_assert(pd->count > 0);
443 if(index >= pd->count) {
444 log_warn("Trying to remove RR with out of bound index");
445 return 0;
446 }
447 if(index + 1 < pd->count) {
448 /* not removing last element */
449 size_t nexti = index + 1;
450 size_t num = pd->count - nexti;
451 memmove(pd->rr_len+index, pd->rr_len+nexti, sizeof(*pd->rr_len)*num);
452 memmove(pd->rr_ttl+index, pd->rr_ttl+nexti, sizeof(*pd->rr_ttl)*num);
453 memmove(pd->rr_data+index, pd->rr_data+nexti, sizeof(*pd->rr_data)*num);
454 }
455 pd->count--;
456 return 1;
457 }
458
459 struct local_data*
local_zone_find_data(struct local_zone * z,uint8_t * nm,size_t nmlen,int nmlabs)460 local_zone_find_data(struct local_zone* z, uint8_t* nm, size_t nmlen, int nmlabs)
461 {
462 struct local_data key;
463 key.node.key = &key;
464 key.name = nm;
465 key.namelen = nmlen;
466 key.namelabs = nmlabs;
467 return (struct local_data*)rbtree_search(&z->data, &key.node);
468 }
469
470 /** find a node, create it if not and all its empty nonterminal parents */
471 static int
lz_find_create_node(struct local_zone * z,uint8_t * nm,size_t nmlen,int nmlabs,struct local_data ** res)472 lz_find_create_node(struct local_zone* z, uint8_t* nm, size_t nmlen,
473 int nmlabs, struct local_data** res)
474 {
475 struct local_data* ld = local_zone_find_data(z, nm, nmlen, nmlabs);
476 if(!ld) {
477 /* create a domain name to store rr. */
478 ld = (struct local_data*)regional_alloc_zero(z->region,
479 sizeof(*ld));
480 if(!ld) {
481 log_err("out of memory adding local data");
482 return 0;
483 }
484 ld->node.key = ld;
485 ld->name = regional_alloc_init(z->region, nm, nmlen);
486 if(!ld->name) {
487 log_err("out of memory");
488 return 0;
489 }
490 ld->namelen = nmlen;
491 ld->namelabs = nmlabs;
492 if(!rbtree_insert(&z->data, &ld->node)) {
493 log_assert(0); /* duplicate name */
494 }
495 /* see if empty nonterminals need to be created */
496 if(nmlabs > z->namelabs) {
497 dname_remove_label(&nm, &nmlen);
498 if(!lz_find_create_node(z, nm, nmlen, nmlabs-1, res))
499 return 0;
500 }
501 }
502 *res = ld;
503 return 1;
504 }
505
506 /* Mark the SOA record for the zone. This only marks the SOA rrset; the data
507 * for the RR is entered later on local_zone_enter_rr() as with the other
508 * records. An artificial soa_negative record with a modified TTL (minimum of
509 * the TTL and the SOA.MINIMUM) is also created and marked for usage with
510 * negative answers and to avoid allocations during those answers. */
511 static int
lz_mark_soa_for_zone(struct local_zone * z,struct ub_packed_rrset_key * soa_rrset,uint8_t * rdata,size_t rdata_len,time_t ttl,const char * rrstr)512 lz_mark_soa_for_zone(struct local_zone* z, struct ub_packed_rrset_key* soa_rrset,
513 uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr)
514 {
515 struct packed_rrset_data* pd = (struct packed_rrset_data*)
516 regional_alloc_zero(z->region, sizeof(*pd));
517 struct ub_packed_rrset_key* rrset_negative = (struct ub_packed_rrset_key*)
518 regional_alloc_zero(z->region, sizeof(*rrset_negative));
519 time_t minimum;
520 if(!rrset_negative||!pd) {
521 log_err("out of memory");
522 return 0;
523 }
524 /* Mark the original SOA record and then continue with the negative one. */
525 z->soa = soa_rrset;
526 rrset_negative->entry.key = rrset_negative;
527 pd->trust = rrset_trust_prim_noglue;
528 pd->security = sec_status_insecure;
529 rrset_negative->entry.data = pd;
530 rrset_negative->rk.dname = soa_rrset->rk.dname;
531 rrset_negative->rk.dname_len = soa_rrset->rk.dname_len;
532 rrset_negative->rk.type = soa_rrset->rk.type;
533 rrset_negative->rk.rrset_class = soa_rrset->rk.rrset_class;
534 if(!rrset_insert_rr(z->region, pd, rdata, rdata_len, ttl, rrstr))
535 return 0;
536 /* last 4 bytes are minimum ttl in network format */
537 if(pd->count == 0 || pd->rr_len[0] < 2+4)
538 return 0;
539 minimum = (time_t)sldns_read_uint32(pd->rr_data[0]+(pd->rr_len[0]-4));
540 minimum = ttl<minimum?ttl:minimum;
541 pd->ttl = minimum;
542 pd->rr_ttl[0] = minimum;
543
544 z->soa_negative = rrset_negative;
545 return 1;
546 }
547
548 int
local_zone_enter_rr(struct local_zone * z,uint8_t * nm,size_t nmlen,int nmlabs,uint16_t rrtype,uint16_t rrclass,time_t ttl,uint8_t * rdata,size_t rdata_len,const char * rrstr)549 local_zone_enter_rr(struct local_zone* z, uint8_t* nm, size_t nmlen,
550 int nmlabs, uint16_t rrtype, uint16_t rrclass, time_t ttl,
551 uint8_t* rdata, size_t rdata_len, const char* rrstr)
552 {
553 struct local_data* node;
554 struct local_rrset* rrset;
555 struct packed_rrset_data* pd;
556
557 if(!lz_find_create_node(z, nm, nmlen, nmlabs, &node)) {
558 return 0;
559 }
560 log_assert(node);
561
562 /* Reject it if we would end up having CNAME and other data (including
563 * another CNAME) for a redirect zone. */
564 if((z->type == local_zone_redirect ||
565 z->type == local_zone_inform_redirect) && node->rrsets) {
566 const char* othertype = NULL;
567 if (rrtype == LDNS_RR_TYPE_CNAME)
568 othertype = "other";
569 else if (node->rrsets->rrset->rk.type ==
570 htons(LDNS_RR_TYPE_CNAME)) {
571 othertype = "CNAME";
572 }
573 if(othertype) {
574 log_err("local-data '%s' in redirect zone must not "
575 "coexist with %s local-data", rrstr, othertype);
576 return 0;
577 }
578 }
579 rrset = local_data_find_type(node, rrtype, 0);
580 if(!rrset) {
581 rrset = new_local_rrset(z->region, node, rrtype, rrclass);
582 if(!rrset)
583 return 0;
584 if(query_dname_compare(node->name, z->name) == 0) {
585 if(rrtype == LDNS_RR_TYPE_NSEC)
586 rrset->rrset->rk.flags = PACKED_RRSET_NSEC_AT_APEX;
587 if(rrtype == LDNS_RR_TYPE_SOA &&
588 !lz_mark_soa_for_zone(z, rrset->rrset, rdata, rdata_len, ttl,
589 rrstr))
590 return 0;
591 }
592 }
593 pd = (struct packed_rrset_data*)rrset->rrset->entry.data;
594 log_assert(rrset && pd);
595
596 /* check for duplicate RR */
597 if(rr_is_duplicate(pd, rdata, rdata_len)) {
598 verbose(VERB_ALGO, "ignoring duplicate RR: %s", rrstr);
599 return 1;
600 }
601 return rrset_insert_rr(z->region, pd, rdata, rdata_len, ttl, rrstr);
602 }
603
604 /** enter data RR into auth zone */
605 static int
lz_enter_rr_into_zone(struct local_zone * z,const char * rrstr)606 lz_enter_rr_into_zone(struct local_zone* z, const char* rrstr)
607 {
608 uint8_t* nm;
609 size_t nmlen;
610 int nmlabs, ret;
611 uint16_t rrtype = 0, rrclass = 0;
612 time_t ttl = 0;
613 uint8_t rr[LDNS_RR_BUF_SIZE];
614 uint8_t* rdata;
615 size_t rdata_len;
616 if(!rrstr_get_rr_content(rrstr, &nm, &rrtype, &rrclass, &ttl, rr,
617 sizeof(rr), &rdata, &rdata_len)) {
618 log_err("bad local-data: %s", rrstr);
619 return 0;
620 }
621 log_assert(z->dclass == rrclass);
622 if((z->type == local_zone_redirect ||
623 z->type == local_zone_inform_redirect) &&
624 query_dname_compare(z->name, nm) != 0) {
625 log_err("local-data in redirect zone must reside at top of zone"
626 ", not at %s", rrstr);
627 free(nm);
628 return 0;
629 }
630 nmlabs = dname_count_size_labels(nm, &nmlen);
631 ret = local_zone_enter_rr(z, nm, nmlen, nmlabs, rrtype, rrclass, ttl,
632 rdata, rdata_len, rrstr);
633 free(nm);
634 return ret;
635 }
636
637 /** enter a data RR into auth data; a zone for it must exist */
638 static int
lz_enter_rr_str(struct local_zones * zones,const char * rr)639 lz_enter_rr_str(struct local_zones* zones, const char* rr)
640 {
641 uint8_t* rr_name;
642 uint16_t rr_class, rr_type;
643 size_t len;
644 int labs;
645 struct local_zone* z;
646 int r;
647 if(!get_rr_nameclass(rr, &rr_name, &rr_class, &rr_type)) {
648 log_err("bad rr %s", rr);
649 return 0;
650 }
651 labs = dname_count_size_labels(rr_name, &len);
652 lock_rw_rdlock(&zones->lock);
653 z = local_zones_lookup(zones, rr_name, len, labs, rr_class, rr_type);
654 if(!z) {
655 lock_rw_unlock(&zones->lock);
656 fatal_exit("internal error: no zone for rr %s", rr);
657 }
658 lock_rw_wrlock(&z->lock);
659 lock_rw_unlock(&zones->lock);
660 free(rr_name);
661 r = lz_enter_rr_into_zone(z, rr);
662 lock_rw_unlock(&z->lock);
663 return r;
664 }
665
666 /** enter tagstring into zone */
667 static int
lz_enter_zone_tag(struct local_zones * zones,char * zname,uint8_t * list,size_t len,uint16_t rr_class)668 lz_enter_zone_tag(struct local_zones* zones, char* zname, uint8_t* list,
669 size_t len, uint16_t rr_class)
670 {
671 uint8_t dname[LDNS_MAX_DOMAINLEN+1];
672 size_t dname_len = sizeof(dname);
673 int dname_labs, r = 0;
674 struct local_zone* z;
675
676 if(sldns_str2wire_dname_buf(zname, dname, &dname_len) != 0) {
677 log_err("cannot parse zone name in local-zone-tag: %s", zname);
678 return 0;
679 }
680 dname_labs = dname_count_labels(dname);
681
682 lock_rw_rdlock(&zones->lock);
683 z = local_zones_find(zones, dname, dname_len, dname_labs, rr_class);
684 if(!z) {
685 lock_rw_unlock(&zones->lock);
686 log_err("no local-zone for tag %s", zname);
687 return 0;
688 }
689 lock_rw_wrlock(&z->lock);
690 lock_rw_unlock(&zones->lock);
691 free(z->taglist);
692 z->taglist = memdup(list, len);
693 z->taglen = len;
694 if(z->taglist)
695 r = 1;
696 lock_rw_unlock(&z->lock);
697 return r;
698 }
699
700 /** enter override into zone */
701 static int
lz_enter_override(struct local_zones * zones,char * zname,char * netblock,char * type,uint16_t rr_class)702 lz_enter_override(struct local_zones* zones, char* zname, char* netblock,
703 char* type, uint16_t rr_class)
704 {
705 uint8_t dname[LDNS_MAX_DOMAINLEN+1];
706 size_t dname_len = sizeof(dname);
707 int dname_labs;
708 struct sockaddr_storage addr;
709 int net;
710 socklen_t addrlen;
711 struct local_zone* z;
712 enum localzone_type t;
713
714 /* parse zone name */
715 if(sldns_str2wire_dname_buf(zname, dname, &dname_len) != 0) {
716 log_err("cannot parse zone name in local-zone-override: %s %s",
717 zname, netblock);
718 return 0;
719 }
720 dname_labs = dname_count_labels(dname);
721
722 /* parse netblock */
723 if(!netblockstrtoaddr(netblock, UNBOUND_DNS_PORT, &addr, &addrlen,
724 &net)) {
725 log_err("cannot parse netblock in local-zone-override: %s %s",
726 zname, netblock);
727 return 0;
728 }
729
730 /* parse zone type */
731 if(!local_zone_str2type(type, &t)) {
732 log_err("cannot parse type in local-zone-override: %s %s %s",
733 zname, netblock, type);
734 return 0;
735 }
736
737 /* find localzone entry */
738 lock_rw_rdlock(&zones->lock);
739 z = local_zones_find(zones, dname, dname_len, dname_labs, rr_class);
740 if(!z) {
741 lock_rw_unlock(&zones->lock);
742 log_err("no local-zone for local-zone-override %s", zname);
743 return 0;
744 }
745 lock_rw_wrlock(&z->lock);
746 lock_rw_unlock(&zones->lock);
747
748 /* create netblock addr_tree if not present yet */
749 if(!z->override_tree) {
750 z->override_tree = (struct rbtree_type*)regional_alloc_zero(
751 z->region, sizeof(*z->override_tree));
752 if(!z->override_tree) {
753 lock_rw_unlock(&z->lock);
754 log_err("out of memory");
755 return 0;
756 }
757 addr_tree_init(z->override_tree);
758 }
759 /* add new elem to tree */
760 if(z->override_tree) {
761 struct local_zone_override* n;
762 n = (struct local_zone_override*)regional_alloc_zero(
763 z->region, sizeof(*n));
764 if(!n) {
765 lock_rw_unlock(&z->lock);
766 log_err("out of memory");
767 return 0;
768 }
769 n->type = t;
770 if(!addr_tree_insert(z->override_tree,
771 (struct addr_tree_node*)n, &addr, addrlen, net)) {
772 lock_rw_unlock(&z->lock);
773 log_err("duplicate local-zone-override %s %s",
774 zname, netblock);
775 return 1;
776 }
777 }
778
779 lock_rw_unlock(&z->lock);
780 return 1;
781 }
782
783 /** parse local-zone: statements */
784 static int
lz_enter_zones(struct local_zones * zones,struct config_file * cfg)785 lz_enter_zones(struct local_zones* zones, struct config_file* cfg)
786 {
787 struct config_str2list* p;
788 #ifndef THREADS_DISABLED
789 struct local_zone* z;
790 #endif
791 for(p = cfg->local_zones; p; p = p->next) {
792 if(!(
793 #ifndef THREADS_DISABLED
794 z=
795 #endif
796 lz_enter_zone(zones, p->str, p->str2,
797 LDNS_RR_CLASS_IN)))
798 return 0;
799 lock_rw_unlock(&z->lock);
800 }
801 return 1;
802 }
803
804 /** lookup a zone in rbtree; exact match only; SLOW due to parse */
805 static int
lz_exists(struct local_zones * zones,const char * name)806 lz_exists(struct local_zones* zones, const char* name)
807 {
808 struct local_zone z;
809 z.node.key = &z;
810 z.dclass = LDNS_RR_CLASS_IN;
811 if(!parse_dname(name, &z.name, &z.namelen, &z.namelabs)) {
812 log_err("bad name %s", name);
813 return 0;
814 }
815 lock_rw_rdlock(&zones->lock);
816 if(rbtree_search(&zones->ztree, &z.node)) {
817 lock_rw_unlock(&zones->lock);
818 free(z.name);
819 return 1;
820 }
821 lock_rw_unlock(&zones->lock);
822 free(z.name);
823 return 0;
824 }
825
826 /** lookup a zone in cfg->nodefault list */
827 static int
lz_nodefault(struct config_file * cfg,const char * name)828 lz_nodefault(struct config_file* cfg, const char* name)
829 {
830 struct config_strlist* p;
831 size_t len = strlen(name);
832 if(len == 0) return 0;
833 if(name[len-1] == '.') len--;
834
835 for(p = cfg->local_zones_nodefault; p; p = p->next) {
836 /* compare zone name, lowercase, compare without ending . */
837 if(strncasecmp(p->str, name, len) == 0 &&
838 (strlen(p->str) == len || (strlen(p->str)==len+1 &&
839 p->str[len] == '.')))
840 return 1;
841 }
842 return 0;
843 }
844
845 /** enter (AS112) empty default zone */
846 static int
add_empty_default(struct local_zones * zones,struct config_file * cfg,const char * name)847 add_empty_default(struct local_zones* zones, struct config_file* cfg,
848 const char* name)
849 {
850 struct local_zone* z;
851 char str[1024]; /* known long enough */
852 if(lz_exists(zones, name) || lz_nodefault(cfg, name))
853 return 1; /* do not enter default content */
854 if(!(z=lz_enter_zone(zones, name, "static", LDNS_RR_CLASS_IN)))
855 return 0;
856 snprintf(str, sizeof(str), "%s 10800 IN SOA localhost. "
857 "nobody.invalid. 1 3600 1200 604800 10800", name);
858 if(!lz_enter_rr_into_zone(z, str)) {
859 lock_rw_unlock(&z->lock);
860 return 0;
861 }
862 snprintf(str, sizeof(str), "%s 10800 IN NS localhost. ", name);
863 if(!lz_enter_rr_into_zone(z, str)) {
864 lock_rw_unlock(&z->lock);
865 return 0;
866 }
867 lock_rw_unlock(&z->lock);
868 return 1;
869 }
870
871 /** enter default zones */
local_zone_enter_defaults(struct local_zones * zones,struct config_file * cfg)872 int local_zone_enter_defaults(struct local_zones* zones, struct config_file* cfg)
873 {
874 struct local_zone* z;
875 const char** zstr;
876
877 /* Do not add any default */
878 if(cfg->local_zones_disable_default)
879 return 1;
880
881 /* this list of zones is from RFC 6303 and RFC 7686 */
882
883 /* block localhost level zones first, then onion and later the LAN zones */
884
885 /* localhost. zone */
886 if(!lz_exists(zones, "localhost.") &&
887 !lz_nodefault(cfg, "localhost.")) {
888 if(!(z=lz_enter_zone(zones, "localhost.", "redirect",
889 LDNS_RR_CLASS_IN)) ||
890 !lz_enter_rr_into_zone(z,
891 "localhost. 10800 IN NS localhost.") ||
892 !lz_enter_rr_into_zone(z,
893 "localhost. 10800 IN SOA localhost. nobody.invalid. "
894 "1 3600 1200 604800 10800") ||
895 !lz_enter_rr_into_zone(z,
896 "localhost. 10800 IN A 127.0.0.1") ||
897 !lz_enter_rr_into_zone(z,
898 "localhost. 10800 IN AAAA ::1")) {
899 log_err("out of memory adding default zone");
900 if(z) { lock_rw_unlock(&z->lock); }
901 return 0;
902 }
903 lock_rw_unlock(&z->lock);
904 }
905 /* reverse ip4 zone */
906 if(!lz_exists(zones, "127.in-addr.arpa.") &&
907 !lz_nodefault(cfg, "127.in-addr.arpa.")) {
908 if(!(z=lz_enter_zone(zones, "127.in-addr.arpa.", "static",
909 LDNS_RR_CLASS_IN)) ||
910 !lz_enter_rr_into_zone(z,
911 "127.in-addr.arpa. 10800 IN NS localhost.") ||
912 !lz_enter_rr_into_zone(z,
913 "127.in-addr.arpa. 10800 IN SOA localhost. "
914 "nobody.invalid. 1 3600 1200 604800 10800") ||
915 !lz_enter_rr_into_zone(z,
916 "1.0.0.127.in-addr.arpa. 10800 IN PTR localhost.")) {
917 log_err("out of memory adding default zone");
918 if(z) { lock_rw_unlock(&z->lock); }
919 return 0;
920 }
921 lock_rw_unlock(&z->lock);
922 }
923 /* reverse ip6 zone */
924 if(!lz_exists(zones, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.") &&
925 !lz_nodefault(cfg, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.")) {
926 if(!(z=lz_enter_zone(zones, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", "static",
927 LDNS_RR_CLASS_IN)) ||
928 !lz_enter_rr_into_zone(z,
929 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. 10800 IN NS localhost.") ||
930 !lz_enter_rr_into_zone(z,
931 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. 10800 IN SOA localhost. "
932 "nobody.invalid. 1 3600 1200 604800 10800") ||
933 !lz_enter_rr_into_zone(z,
934 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa. 10800 IN PTR localhost.")) {
935 log_err("out of memory adding default zone");
936 if(z) { lock_rw_unlock(&z->lock); }
937 return 0;
938 }
939 lock_rw_unlock(&z->lock);
940 }
941 /* home.arpa. zone (RFC 8375) */
942 if(!add_empty_default(zones, cfg, "home.arpa.")) {
943 log_err("out of memory adding default zone");
944 return 0;
945 }
946 /* onion. zone (RFC 7686) */
947 if(!add_empty_default(zones, cfg, "onion.")) {
948 log_err("out of memory adding default zone");
949 return 0;
950 }
951 /* test. zone (RFC 6761) */
952 if(!add_empty_default(zones, cfg, "test.")) {
953 log_err("out of memory adding default zone");
954 return 0;
955 }
956 /* invalid. zone (RFC 6761) */
957 if(!add_empty_default(zones, cfg, "invalid.")) {
958 log_err("out of memory adding default zone");
959 return 0;
960 }
961 /* block AS112 zones, unless asked not to */
962 if(!cfg->unblock_lan_zones) {
963 for(zstr = as112_zones; *zstr; zstr++) {
964 if(!add_empty_default(zones, cfg, *zstr)) {
965 log_err("out of memory adding default zone");
966 return 0;
967 }
968 }
969 }
970 return 1;
971 }
972
973 /** parse local-zone-override: statements */
974 static int
lz_enter_overrides(struct local_zones * zones,struct config_file * cfg)975 lz_enter_overrides(struct local_zones* zones, struct config_file* cfg)
976 {
977 struct config_str3list* p;
978 for(p = cfg->local_zone_overrides; p; p = p->next) {
979 if(!lz_enter_override(zones, p->str, p->str2, p->str3,
980 LDNS_RR_CLASS_IN))
981 return 0;
982 }
983 return 1;
984 }
985
986 /* return closest parent in the tree, NULL if none */
find_closest_parent(struct local_zone * curr,struct local_zone * prev)987 static struct local_zone* find_closest_parent(struct local_zone* curr,
988 struct local_zone* prev)
989 {
990 struct local_zone* p;
991 int m;
992 if(!prev || prev->dclass != curr->dclass) return NULL;
993 (void)dname_lab_cmp(prev->name, prev->namelabs, curr->name,
994 curr->namelabs, &m); /* we know prev is smaller */
995 /* sort order like: . com. bla.com. zwb.com. net. */
996 /* find the previous, or parent-parent-parent */
997 for(p = prev; p; p = p->parent) {
998 /* looking for name with few labels, a parent */
999 if(p->namelabs <= m) {
1000 /* ==: since prev matched m, this is closest*/
1001 /* <: prev matches more, but is not a parent,
1002 * this one is a (grand)parent */
1003 return p;
1004 }
1005 }
1006 return NULL;
1007 }
1008
1009 /** setup parent pointers, so that a lookup can be done for closest match */
1010 void
lz_init_parents(struct local_zones * zones)1011 lz_init_parents(struct local_zones* zones)
1012 {
1013 struct local_zone* node, *prev = NULL;
1014 lock_rw_wrlock(&zones->lock);
1015 RBTREE_FOR(node, struct local_zone*, &zones->ztree) {
1016 lock_rw_wrlock(&node->lock);
1017 node->parent = find_closest_parent(node, prev);
1018 prev = node;
1019 if(node->override_tree)
1020 addr_tree_init_parents(node->override_tree);
1021 lock_rw_unlock(&node->lock);
1022 }
1023 lock_rw_unlock(&zones->lock);
1024 }
1025
1026 /** enter implicit transparent zone for local-data: without local-zone: */
1027 static int
lz_setup_implicit(struct local_zones * zones,struct config_file * cfg)1028 lz_setup_implicit(struct local_zones* zones, struct config_file* cfg)
1029 {
1030 /* walk over all items that have no parent zone and find
1031 * the name that covers them all (could be the root) and
1032 * add that as a transparent zone */
1033 struct config_strlist* p;
1034 int have_name = 0;
1035 int have_other_classes = 0;
1036 uint16_t dclass = 0;
1037 uint8_t* nm = 0;
1038 size_t nmlen = 0;
1039 int nmlabs = 0;
1040 int match = 0; /* number of labels match count */
1041
1042 lz_init_parents(zones); /* to enable local_zones_lookup() */
1043 for(p = cfg->local_data; p; p = p->next) {
1044 uint8_t* rr_name;
1045 uint16_t rr_class, rr_type;
1046 size_t len;
1047 int labs;
1048 if(!get_rr_nameclass(p->str, &rr_name, &rr_class, &rr_type)) {
1049 log_err("Bad local-data RR %s", p->str);
1050 return 0;
1051 }
1052 labs = dname_count_size_labels(rr_name, &len);
1053 lock_rw_rdlock(&zones->lock);
1054 if(!local_zones_lookup(zones, rr_name, len, labs, rr_class,
1055 rr_type)) {
1056 /* Check if there is a zone that this could go
1057 * under but for different class; created zones are
1058 * always for LDNS_RR_CLASS_IN. Create the zone with
1059 * a different class but the same configured
1060 * local_zone_type. */
1061 struct local_zone* z = local_zones_lookup(zones,
1062 rr_name, len, labs, LDNS_RR_CLASS_IN, rr_type);
1063 if(z) {
1064 uint8_t* name = memdup(z->name, z->namelen);
1065 size_t znamelen = z->namelen;
1066 int znamelabs = z->namelabs;
1067 enum localzone_type ztype = z->type;
1068 lock_rw_unlock(&zones->lock);
1069 if(!name) {
1070 log_err("out of memory");
1071 free(rr_name);
1072 return 0;
1073 }
1074 if(!(
1075 #ifndef THREADS_DISABLED
1076 z =
1077 #endif
1078 lz_enter_zone_dname(zones, name,
1079 znamelen, znamelabs,
1080 ztype, rr_class))) {
1081 free(rr_name);
1082 return 0;
1083 }
1084 lock_rw_unlock(&z->lock);
1085 free(rr_name);
1086 continue;
1087 }
1088 if(!have_name) {
1089 dclass = rr_class;
1090 nm = rr_name;
1091 nmlen = len;
1092 nmlabs = labs;
1093 match = labs;
1094 have_name = 1;
1095 } else {
1096 int m;
1097 if(rr_class != dclass) {
1098 /* process other classes later */
1099 free(rr_name);
1100 have_other_classes = 1;
1101 lock_rw_unlock(&zones->lock);
1102 continue;
1103 }
1104 /* find smallest shared topdomain */
1105 (void)dname_lab_cmp(nm, nmlabs,
1106 rr_name, labs, &m);
1107 free(rr_name);
1108 if(m < match)
1109 match = m;
1110 }
1111 } else free(rr_name);
1112 lock_rw_unlock(&zones->lock);
1113 }
1114 if(have_name) {
1115 uint8_t* n2;
1116 #ifndef THREADS_DISABLED
1117 struct local_zone* z;
1118 #endif
1119 /* allocate zone of smallest shared topdomain to contain em */
1120 n2 = nm;
1121 dname_remove_labels(&n2, &nmlen, nmlabs - match);
1122 n2 = memdup(n2, nmlen);
1123 free(nm);
1124 if(!n2) {
1125 log_err("out of memory");
1126 return 0;
1127 }
1128 log_nametypeclass(VERB_ALGO, "implicit transparent local-zone",
1129 n2, 0, dclass);
1130 if(!(
1131 #ifndef THREADS_DISABLED
1132 z=
1133 #endif
1134 lz_enter_zone_dname(zones, n2, nmlen, match,
1135 local_zone_transparent, dclass))) {
1136 return 0;
1137 }
1138 lock_rw_unlock(&z->lock);
1139 }
1140 if(have_other_classes) {
1141 /* restart to setup other class */
1142 return lz_setup_implicit(zones, cfg);
1143 }
1144 return 1;
1145 }
1146
1147 /** enter local-zone-tag info */
1148 static int
lz_enter_zone_tags(struct local_zones * zones,struct config_file * cfg)1149 lz_enter_zone_tags(struct local_zones* zones, struct config_file* cfg)
1150 {
1151 struct config_strbytelist* p;
1152 int c = 0;
1153 for(p = cfg->local_zone_tags; p; p = p->next) {
1154 if(!lz_enter_zone_tag(zones, p->str, p->str2, p->str2len,
1155 LDNS_RR_CLASS_IN))
1156 return 0;
1157 c++;
1158 }
1159 if(c) verbose(VERB_ALGO, "applied tags to %d local zones", c);
1160 return 1;
1161 }
1162
1163 /** enter auth data */
1164 static int
lz_enter_data(struct local_zones * zones,struct config_file * cfg)1165 lz_enter_data(struct local_zones* zones, struct config_file* cfg)
1166 {
1167 struct config_strlist* p;
1168 for(p = cfg->local_data; p; p = p->next) {
1169 if(!lz_enter_rr_str(zones, p->str))
1170 return 0;
1171 }
1172 return 1;
1173 }
1174
1175 /** free memory from config */
1176 static void
lz_freeup_cfg(struct config_file * cfg)1177 lz_freeup_cfg(struct config_file* cfg)
1178 {
1179 config_deldblstrlist(cfg->local_zones);
1180 cfg->local_zones = NULL;
1181 config_delstrlist(cfg->local_zones_nodefault);
1182 cfg->local_zones_nodefault = NULL;
1183 config_delstrlist(cfg->local_data);
1184 cfg->local_data = NULL;
1185 }
1186
1187 int
local_zones_apply_cfg(struct local_zones * zones,struct config_file * cfg)1188 local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg)
1189 {
1190 /* create zones from zone statements. */
1191 if(!lz_enter_zones(zones, cfg)) {
1192 return 0;
1193 }
1194 /* apply default zones+content (unless disabled, or overridden) */
1195 if(!local_zone_enter_defaults(zones, cfg)) {
1196 return 0;
1197 }
1198 /* enter local zone overrides */
1199 if(!lz_enter_overrides(zones, cfg)) {
1200 return 0;
1201 }
1202 /* create implicit transparent zone from data. */
1203 if(!lz_setup_implicit(zones, cfg)) {
1204 return 0;
1205 }
1206
1207 /* setup parent ptrs for lookup during data entry */
1208 lz_init_parents(zones);
1209 /* insert local zone tags */
1210 if(!lz_enter_zone_tags(zones, cfg)) {
1211 return 0;
1212 }
1213 /* insert local data */
1214 if(!lz_enter_data(zones, cfg)) {
1215 return 0;
1216 }
1217 /* freeup memory from cfg struct. */
1218 lz_freeup_cfg(cfg);
1219 return 1;
1220 }
1221
1222 struct local_zone*
local_zones_lookup(struct local_zones * zones,uint8_t * name,size_t len,int labs,uint16_t dclass,uint16_t dtype)1223 local_zones_lookup(struct local_zones* zones,
1224 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype)
1225 {
1226 return local_zones_tags_lookup(zones, name, len, labs,
1227 dclass, dtype, NULL, 0, 1);
1228 }
1229
1230 struct local_zone*
local_zones_tags_lookup(struct local_zones * zones,uint8_t * name,size_t len,int labs,uint16_t dclass,uint16_t dtype,uint8_t * taglist,size_t taglen,int ignoretags)1231 local_zones_tags_lookup(struct local_zones* zones,
1232 uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype,
1233 uint8_t* taglist, size_t taglen, int ignoretags)
1234 {
1235 rbnode_type* res = NULL;
1236 struct local_zone *result;
1237 struct local_zone key;
1238 int m;
1239 /* for type DS use a zone higher when on a zonecut */
1240 if(dtype == LDNS_RR_TYPE_DS && !dname_is_root(name)) {
1241 dname_remove_label(&name, &len);
1242 labs--;
1243 }
1244 key.node.key = &key;
1245 key.dclass = dclass;
1246 key.name = name;
1247 key.namelen = len;
1248 key.namelabs = labs;
1249 rbtree_find_less_equal(&zones->ztree, &key, &res);
1250 result = (struct local_zone*)res;
1251 /* exact or smaller element (or no element) */
1252 if(!result || result->dclass != dclass)
1253 return NULL;
1254 /* count number of labels matched */
1255 (void)dname_lab_cmp(result->name, result->namelabs, key.name,
1256 key.namelabs, &m);
1257 while(result) { /* go up until qname is zone or subdomain of zone */
1258 if(result->namelabs <= m)
1259 if(ignoretags || !result->taglist ||
1260 taglist_intersect(result->taglist,
1261 result->taglen, taglist, taglen))
1262 break;
1263 result = result->parent;
1264 }
1265 return result;
1266 }
1267
1268 struct local_zone*
local_zones_find(struct local_zones * zones,uint8_t * name,size_t len,int labs,uint16_t dclass)1269 local_zones_find(struct local_zones* zones,
1270 uint8_t* name, size_t len, int labs, uint16_t dclass)
1271 {
1272 struct local_zone key;
1273 key.node.key = &key;
1274 key.dclass = dclass;
1275 key.name = name;
1276 key.namelen = len;
1277 key.namelabs = labs;
1278 /* exact */
1279 return (struct local_zone*)rbtree_search(&zones->ztree, &key);
1280 }
1281
1282 struct local_zone*
local_zones_find_le(struct local_zones * zones,uint8_t * name,size_t len,int labs,uint16_t dclass,int * exact)1283 local_zones_find_le(struct local_zones* zones,
1284 uint8_t* name, size_t len, int labs, uint16_t dclass,
1285 int* exact)
1286 {
1287 struct local_zone key;
1288 rbnode_type *node;
1289 key.node.key = &key;
1290 key.dclass = dclass;
1291 key.name = name;
1292 key.namelen = len;
1293 key.namelabs = labs;
1294 *exact = rbtree_find_less_equal(&zones->ztree, &key, &node);
1295 return (struct local_zone*)node;
1296 }
1297
1298 /** encode answer consisting of 1 rrset */
1299 static int
local_encode(struct query_info * qinfo,struct module_env * env,struct edns_data * edns,struct comm_reply * repinfo,sldns_buffer * buf,struct regional * temp,struct ub_packed_rrset_key * rrset,int ansec,int rcode)1300 local_encode(struct query_info* qinfo, struct module_env* env,
1301 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf,
1302 struct regional* temp, struct ub_packed_rrset_key* rrset, int ansec,
1303 int rcode)
1304 {
1305 struct reply_info rep;
1306 uint16_t udpsize;
1307 /* make answer with time=0 for fixed TTL values */
1308 memset(&rep, 0, sizeof(rep));
1309 rep.flags = (uint16_t)((BIT_QR | BIT_AA | BIT_RA) | rcode);
1310 rep.qdcount = 1;
1311 if(ansec)
1312 rep.an_numrrsets = 1;
1313 else rep.ns_numrrsets = 1;
1314 rep.rrset_count = 1;
1315 rep.rrsets = &rrset;
1316 rep.reason_bogus = LDNS_EDE_NONE;
1317 udpsize = edns->udp_size;
1318 edns->edns_version = EDNS_ADVERTISED_VERSION;
1319 edns->udp_size = EDNS_ADVERTISED_SIZE;
1320 edns->ext_rcode = 0;
1321 edns->bits &= EDNS_DO;
1322 if(!inplace_cb_reply_local_call(env, qinfo, NULL, &rep, rcode, edns,
1323 repinfo, temp, env->now_tv) || !reply_info_answer_encode(qinfo, &rep,
1324 *(uint16_t*)sldns_buffer_begin(buf), sldns_buffer_read_u16_at(buf, 2),
1325 buf, 0, 0, temp, udpsize, edns, (int)(edns->bits&EDNS_DO), 0)) {
1326 error_encode(buf, (LDNS_RCODE_SERVFAIL|BIT_AA), qinfo,
1327 *(uint16_t*)sldns_buffer_begin(buf),
1328 sldns_buffer_read_u16_at(buf, 2), edns);
1329 }
1330 return 1;
1331 }
1332
1333 /** encode local error answer */
1334 static void
local_error_encode(struct query_info * qinfo,struct module_env * env,struct edns_data * edns,struct comm_reply * repinfo,sldns_buffer * buf,struct regional * temp,int rcode,int r,int ede_code,const char * ede_txt)1335 local_error_encode(struct query_info* qinfo, struct module_env* env,
1336 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf,
1337 struct regional* temp, int rcode, int r, int ede_code,
1338 const char* ede_txt)
1339 {
1340 edns->edns_version = EDNS_ADVERTISED_VERSION;
1341 edns->udp_size = EDNS_ADVERTISED_SIZE;
1342 edns->ext_rcode = 0;
1343 edns->bits &= EDNS_DO;
1344
1345 if(!inplace_cb_reply_local_call(env, qinfo, NULL, NULL,
1346 rcode, edns, repinfo, temp, env->now_tv))
1347 edns->opt_list_inplace_cb_out = NULL;
1348
1349 if(ede_code != LDNS_EDE_NONE && env->cfg->ede) {
1350 edns_opt_list_append_ede(&edns->opt_list_out, temp,
1351 ede_code, ede_txt);
1352 }
1353
1354 error_encode(buf, r, qinfo, *(uint16_t*)sldns_buffer_begin(buf),
1355 sldns_buffer_read_u16_at(buf, 2), edns);
1356 }
1357
1358 /** find local data tag string match for the given type in the list */
1359 int
local_data_find_tag_datas(const struct query_info * qinfo,struct config_strlist * list,struct ub_packed_rrset_key * r,struct regional * temp)1360 local_data_find_tag_datas(const struct query_info* qinfo,
1361 struct config_strlist* list, struct ub_packed_rrset_key* r,
1362 struct regional* temp)
1363 {
1364 struct config_strlist* p;
1365 char buf[65536];
1366 uint8_t rr[LDNS_RR_BUF_SIZE];
1367 size_t len;
1368 int res;
1369 struct packed_rrset_data* d;
1370 for(p=list; p; p=p->next) {
1371 uint16_t rdr_type;
1372
1373 len = sizeof(rr);
1374 /* does this element match the type? */
1375 snprintf(buf, sizeof(buf), ". %s", p->str);
1376 res = sldns_str2wire_rr_buf(buf, rr, &len, NULL, 3600,
1377 NULL, 0, NULL, 0);
1378 if(res != 0)
1379 /* parse errors are already checked before, in
1380 * acllist check_data, skip this for robustness */
1381 continue;
1382 if(len < 1 /* . */ + 8 /* typeclassttl*/ + 2 /*rdatalen*/)
1383 continue;
1384 rdr_type = sldns_wirerr_get_type(rr, len, 1);
1385 if(rdr_type != qinfo->qtype && rdr_type != LDNS_RR_TYPE_CNAME)
1386 continue;
1387
1388 /* do we have entries already? if not setup key */
1389 if(r->rk.dname == NULL) {
1390 r->entry.key = r;
1391 r->rk.dname = qinfo->qname;
1392 r->rk.dname_len = qinfo->qname_len;
1393 r->rk.type = htons(rdr_type);
1394 r->rk.rrset_class = htons(qinfo->qclass);
1395 r->rk.flags = 0;
1396 d = (struct packed_rrset_data*)regional_alloc_zero(
1397 temp, sizeof(struct packed_rrset_data)
1398 + sizeof(size_t) + sizeof(uint8_t*) +
1399 sizeof(time_t));
1400 if(!d) return 0; /* out of memory */
1401 r->entry.data = d;
1402 d->ttl = sldns_wirerr_get_ttl(rr, len, 1);
1403 d->rr_len = (size_t*)((uint8_t*)d +
1404 sizeof(struct packed_rrset_data));
1405 d->rr_data = (uint8_t**)&(d->rr_len[1]);
1406 d->rr_ttl = (time_t*)&(d->rr_data[1]);
1407 }
1408 d = (struct packed_rrset_data*)r->entry.data;
1409 /* add entry to the data */
1410 if(d->count != 0) {
1411 size_t* oldlen = d->rr_len;
1412 uint8_t** olddata = d->rr_data;
1413 time_t* oldttl = d->rr_ttl;
1414 /* increase arrays for lookup */
1415 /* this is of course slow for very many records,
1416 * but most redirects are expected with few records */
1417 d->rr_len = (size_t*)regional_alloc_zero(temp,
1418 (d->count+1)*sizeof(size_t));
1419 d->rr_data = (uint8_t**)regional_alloc_zero(temp,
1420 (d->count+1)*sizeof(uint8_t*));
1421 d->rr_ttl = (time_t*)regional_alloc_zero(temp,
1422 (d->count+1)*sizeof(time_t));
1423 if(!d->rr_len || !d->rr_data || !d->rr_ttl)
1424 return 0; /* out of memory */
1425 /* first one was allocated after struct d, but new
1426 * ones get their own array increment alloc, so
1427 * copy old content */
1428 memmove(d->rr_len, oldlen, d->count*sizeof(size_t));
1429 memmove(d->rr_data, olddata, d->count*sizeof(uint8_t*));
1430 memmove(d->rr_ttl, oldttl, d->count*sizeof(time_t));
1431 }
1432
1433 d->rr_len[d->count] = sldns_wirerr_get_rdatalen(rr, len, 1)+2;
1434 d->rr_ttl[d->count] = sldns_wirerr_get_ttl(rr, len, 1);
1435 d->rr_data[d->count] = regional_alloc_init(temp,
1436 sldns_wirerr_get_rdatawl(rr, len, 1),
1437 d->rr_len[d->count]);
1438 if(!d->rr_data[d->count])
1439 return 0; /* out of memory */
1440 d->count++;
1441 }
1442 if(r->rk.dname)
1443 return 1;
1444 return 0;
1445 }
1446
1447 static int
find_tag_datas(struct query_info * qinfo,struct config_strlist * list,struct ub_packed_rrset_key * r,struct regional * temp)1448 find_tag_datas(struct query_info* qinfo, struct config_strlist* list,
1449 struct ub_packed_rrset_key* r, struct regional* temp)
1450 {
1451 int result = local_data_find_tag_datas(qinfo, list, r, temp);
1452
1453 /* If we've found a non-exact alias type of local data, make a shallow
1454 * copy of the RRset and remember it in qinfo to complete the alias
1455 * chain later. */
1456 if(result && qinfo->qtype != LDNS_RR_TYPE_CNAME &&
1457 r->rk.type == htons(LDNS_RR_TYPE_CNAME)) {
1458 qinfo->local_alias =
1459 regional_alloc_zero(temp, sizeof(struct local_rrset));
1460 if(!qinfo->local_alias)
1461 return 0; /* out of memory */
1462 qinfo->local_alias->rrset =
1463 regional_alloc_init(temp, r, sizeof(*r));
1464 if(!qinfo->local_alias->rrset)
1465 return 0; /* out of memory */
1466 }
1467 return result;
1468 }
1469
1470 int
local_data_answer(struct local_zone * z,struct module_env * env,struct query_info * qinfo,struct edns_data * edns,struct comm_reply * repinfo,sldns_buffer * buf,struct regional * temp,int labs,struct local_data ** ldp,enum localzone_type lz_type,int tag,struct config_strlist ** tag_datas,size_t tag_datas_size,char ** tagname,int num_tags)1471 local_data_answer(struct local_zone* z, struct module_env* env,
1472 struct query_info* qinfo, struct edns_data* edns,
1473 struct comm_reply* repinfo, sldns_buffer* buf,
1474 struct regional* temp, int labs, struct local_data** ldp,
1475 enum localzone_type lz_type, int tag, struct config_strlist** tag_datas,
1476 size_t tag_datas_size, char** tagname, int num_tags)
1477 {
1478 struct local_data key;
1479 struct local_data* ld;
1480 struct local_rrset* lr;
1481 key.node.key = &key;
1482 key.name = qinfo->qname;
1483 key.namelen = qinfo->qname_len;
1484 key.namelabs = labs;
1485 if(lz_type == local_zone_redirect ||
1486 lz_type == local_zone_inform_redirect) {
1487 key.name = z->name;
1488 key.namelen = z->namelen;
1489 key.namelabs = z->namelabs;
1490 if(tag != -1 && (size_t)tag<tag_datas_size && tag_datas[tag]) {
1491 struct ub_packed_rrset_key r;
1492 memset(&r, 0, sizeof(r));
1493 if(find_tag_datas(qinfo, tag_datas[tag], &r, temp)) {
1494 verbose(VERB_ALGO, "redirect with tag data [%d] %s",
1495 tag, (tag<num_tags?tagname[tag]:"null"));
1496
1497 /* If we found a matching alias, we should
1498 * use it as part of the answer, but we can't
1499 * encode it until we complete the alias
1500 * chain. */
1501 if(qinfo->local_alias)
1502 return 1;
1503 return local_encode(qinfo, env, edns, repinfo, buf, temp,
1504 &r, 1, LDNS_RCODE_NOERROR);
1505 }
1506 }
1507 }
1508 ld = (struct local_data*)rbtree_search(&z->data, &key.node);
1509 *ldp = ld;
1510 if(!ld) {
1511 return 0;
1512 }
1513 lr = local_data_find_type(ld, qinfo->qtype, 1);
1514 if(!lr)
1515 return 0;
1516
1517 /* Special case for alias matching. See local_data_answer(). */
1518 if((lz_type == local_zone_redirect ||
1519 lz_type == local_zone_inform_redirect) &&
1520 qinfo->qtype != LDNS_RR_TYPE_CNAME &&
1521 lr->rrset->rk.type == htons(LDNS_RR_TYPE_CNAME)) {
1522 uint8_t* ctarget;
1523 size_t ctargetlen = 0;
1524
1525 qinfo->local_alias =
1526 regional_alloc_zero(temp, sizeof(struct local_rrset));
1527 if(!qinfo->local_alias)
1528 return 0; /* out of memory */
1529 qinfo->local_alias->rrset = regional_alloc_init(
1530 temp, lr->rrset, sizeof(*lr->rrset));
1531 if(!qinfo->local_alias->rrset)
1532 return 0; /* out of memory */
1533 qinfo->local_alias->rrset->rk.dname = qinfo->qname;
1534 qinfo->local_alias->rrset->rk.dname_len = qinfo->qname_len;
1535 get_cname_target(lr->rrset, &ctarget, &ctargetlen);
1536 if(!ctargetlen)
1537 return 0; /* invalid cname */
1538 if(dname_is_wild(ctarget)) {
1539 /* synthesize cname target */
1540 struct packed_rrset_data* d, *lr_d;
1541 /* -3 for wildcard label and root label from qname */
1542 size_t newtargetlen = qinfo->qname_len + ctargetlen - 3;
1543
1544 log_assert(ctargetlen >= 3);
1545 log_assert(qinfo->qname_len >= 1);
1546
1547 if(newtargetlen > LDNS_MAX_DOMAINLEN) {
1548 qinfo->local_alias = NULL;
1549 local_error_encode(qinfo, env, edns, repinfo,
1550 buf, temp, LDNS_RCODE_YXDOMAIN,
1551 (LDNS_RCODE_YXDOMAIN|BIT_AA),
1552 LDNS_EDE_OTHER,
1553 "DNAME expansion became too large");
1554 return 1;
1555 }
1556 memset(&qinfo->local_alias->rrset->entry, 0,
1557 sizeof(qinfo->local_alias->rrset->entry));
1558 qinfo->local_alias->rrset->entry.key =
1559 qinfo->local_alias->rrset;
1560 qinfo->local_alias->rrset->entry.hash =
1561 rrset_key_hash(&qinfo->local_alias->rrset->rk);
1562 d = (struct packed_rrset_data*)regional_alloc_zero(temp,
1563 sizeof(struct packed_rrset_data) + sizeof(size_t) +
1564 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)
1565 + newtargetlen);
1566 if(!d)
1567 return 0; /* out of memory */
1568 lr_d = (struct packed_rrset_data*)lr->rrset->entry.data;
1569 qinfo->local_alias->rrset->entry.data = d;
1570 d->ttl = lr_d->rr_ttl[0]; /* RFC6672-like behavior:
1571 synth CNAME TTL uses original TTL*/
1572 d->count = 1;
1573 d->rrsig_count = 0;
1574 d->trust = rrset_trust_ans_noAA;
1575 d->rr_len = (size_t*)((uint8_t*)d +
1576 sizeof(struct packed_rrset_data));
1577 d->rr_len[0] = newtargetlen + sizeof(uint16_t);
1578 packed_rrset_ptr_fixup(d);
1579 d->rr_ttl[0] = d->ttl;
1580 sldns_write_uint16(d->rr_data[0], newtargetlen);
1581 /* write qname */
1582 memmove(d->rr_data[0] + sizeof(uint16_t), qinfo->qname,
1583 qinfo->qname_len - 1);
1584 /* write cname target wildcard label */
1585 memmove(d->rr_data[0] + sizeof(uint16_t) +
1586 qinfo->qname_len - 1, ctarget + 2,
1587 ctargetlen - 2);
1588 }
1589 return 1;
1590 }
1591 if(lz_type == local_zone_redirect ||
1592 lz_type == local_zone_inform_redirect) {
1593 /* convert rrset name to query name; like a wildcard */
1594 struct ub_packed_rrset_key r = *lr->rrset;
1595 r.rk.dname = qinfo->qname;
1596 r.rk.dname_len = qinfo->qname_len;
1597 return local_encode(qinfo, env, edns, repinfo, buf, temp, &r, 1,
1598 LDNS_RCODE_NOERROR);
1599 }
1600 return local_encode(qinfo, env, edns, repinfo, buf, temp, lr->rrset, 1,
1601 LDNS_RCODE_NOERROR);
1602 }
1603
1604 /**
1605 * See if the local zone does not cover the name, eg. the name is not
1606 * in the zone and the zone is transparent */
1607 static int
local_zone_does_not_cover(struct local_zone * z,struct query_info * qinfo,int labs)1608 local_zone_does_not_cover(struct local_zone* z, struct query_info* qinfo,
1609 int labs)
1610 {
1611 struct local_data key;
1612 struct local_data* ld = NULL;
1613 struct local_rrset* lr = NULL;
1614 if(z->type == local_zone_always_transparent || z->type == local_zone_block_a)
1615 return 1;
1616 if(z->type != local_zone_transparent
1617 && z->type != local_zone_typetransparent
1618 && z->type != local_zone_inform)
1619 return 0;
1620 key.node.key = &key;
1621 key.name = qinfo->qname;
1622 key.namelen = qinfo->qname_len;
1623 key.namelabs = labs;
1624 ld = (struct local_data*)rbtree_search(&z->data, &key.node);
1625 if(z->type == local_zone_transparent || z->type == local_zone_inform)
1626 return (ld == NULL);
1627 if(ld)
1628 lr = local_data_find_type(ld, qinfo->qtype, 1);
1629 /* local_zone_typetransparent */
1630 return (lr == NULL);
1631 }
1632
1633 static inline int
local_zone_is_udp_query(struct comm_reply * repinfo)1634 local_zone_is_udp_query(struct comm_reply* repinfo) {
1635 return repinfo != NULL
1636 ? (repinfo->c != NULL
1637 ? repinfo->c->type == comm_udp
1638 : 0)
1639 : 0;
1640 }
1641
1642 int
local_zones_zone_answer(struct local_zone * z,struct module_env * env,struct query_info * qinfo,struct edns_data * edns,struct comm_reply * repinfo,sldns_buffer * buf,struct regional * temp,struct local_data * ld,enum localzone_type lz_type)1643 local_zones_zone_answer(struct local_zone* z, struct module_env* env,
1644 struct query_info* qinfo, struct edns_data* edns,
1645 struct comm_reply* repinfo, sldns_buffer* buf, struct regional* temp,
1646 struct local_data* ld, enum localzone_type lz_type)
1647 {
1648 if(lz_type == local_zone_deny ||
1649 lz_type == local_zone_always_deny ||
1650 lz_type == local_zone_inform_deny) {
1651 /** no reply at all, signal caller by clearing buffer. */
1652 sldns_buffer_clear(buf);
1653 sldns_buffer_flip(buf);
1654 return 1;
1655 } else if(lz_type == local_zone_refuse
1656 || lz_type == local_zone_always_refuse) {
1657 local_error_encode(qinfo, env, edns, repinfo, buf, temp,
1658 LDNS_RCODE_REFUSED, (LDNS_RCODE_REFUSED|BIT_AA),
1659 LDNS_EDE_NONE, NULL);
1660 return 1;
1661 } else if(lz_type == local_zone_static ||
1662 lz_type == local_zone_redirect ||
1663 lz_type == local_zone_inform_redirect ||
1664 lz_type == local_zone_always_nxdomain ||
1665 lz_type == local_zone_always_nodata ||
1666 (lz_type == local_zone_truncate
1667 && local_zone_is_udp_query(repinfo))) {
1668 /* for static, reply nodata or nxdomain
1669 * for redirect, reply nodata */
1670 /* no additional section processing,
1671 * cname, dname or wildcard processing,
1672 * or using closest match for NSEC.
1673 * or using closest match for returning delegation downwards
1674 */
1675 int rcode = (ld || lz_type == local_zone_redirect ||
1676 lz_type == local_zone_inform_redirect ||
1677 lz_type == local_zone_always_nodata ||
1678 lz_type == local_zone_truncate)?
1679 LDNS_RCODE_NOERROR:LDNS_RCODE_NXDOMAIN;
1680 rcode = (lz_type == local_zone_truncate ? (rcode|BIT_TC) : rcode);
1681 if(z != NULL && z->soa && z->soa_negative)
1682 return local_encode(qinfo, env, edns, repinfo, buf, temp,
1683 z->soa_negative, 0, rcode);
1684 local_error_encode(qinfo, env, edns, repinfo, buf, temp,
1685 rcode, (rcode|BIT_AA), LDNS_EDE_NONE, NULL);
1686 return 1;
1687 } else if(lz_type == local_zone_typetransparent
1688 || lz_type == local_zone_always_transparent) {
1689 /* no NODATA or NXDOMAINS for this zone type */
1690 return 0;
1691 } else if(lz_type == local_zone_block_a) {
1692 /* Return NODATA for all A queries */
1693 if(qinfo->qtype == LDNS_RR_TYPE_A) {
1694 local_error_encode(qinfo, env, edns, repinfo, buf, temp,
1695 LDNS_RCODE_NOERROR, (LDNS_RCODE_NOERROR|BIT_AA),
1696 LDNS_EDE_NONE, NULL);
1697 return 1;
1698 }
1699
1700 return 0;
1701 } else if(lz_type == local_zone_always_null) {
1702 /* 0.0.0.0 or ::0 or noerror/nodata for this zone type,
1703 * used for blocklists. */
1704 if(qinfo->qtype == LDNS_RR_TYPE_A ||
1705 qinfo->qtype == LDNS_RR_TYPE_AAAA) {
1706 struct ub_packed_rrset_key lrr;
1707 struct packed_rrset_data d;
1708 time_t rr_ttl = 3600;
1709 size_t rr_len = 0;
1710 uint8_t rr_data[2+16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1711 uint8_t* rr_datas = rr_data;
1712 memset(&lrr, 0, sizeof(lrr));
1713 memset(&d, 0, sizeof(d));
1714 lrr.entry.data = &d;
1715 lrr.rk.dname = qinfo->qname;
1716 lrr.rk.dname_len = qinfo->qname_len;
1717 lrr.rk.type = htons(qinfo->qtype);
1718 lrr.rk.rrset_class = htons(qinfo->qclass);
1719 if(qinfo->qtype == LDNS_RR_TYPE_A) {
1720 rr_len = 4;
1721 sldns_write_uint16(rr_data, rr_len);
1722 rr_len += 2;
1723 } else {
1724 rr_len = 16;
1725 sldns_write_uint16(rr_data, rr_len);
1726 rr_len += 2;
1727 }
1728 d.ttl = rr_ttl;
1729 d.count = 1;
1730 d.rr_len = &rr_len;
1731 d.rr_data = &rr_datas;
1732 d.rr_ttl = &rr_ttl;
1733 return local_encode(qinfo, env, edns, repinfo, buf, temp,
1734 &lrr, 1, LDNS_RCODE_NOERROR);
1735 } else {
1736 /* NODATA: No EDE needed */
1737 local_error_encode(qinfo, env, edns, repinfo, buf,
1738 temp, LDNS_RCODE_NOERROR,
1739 (LDNS_RCODE_NOERROR|BIT_AA), -1, NULL);
1740 }
1741 return 1;
1742 }
1743 /* else lz_type == local_zone_transparent */
1744
1745 /* if the zone is transparent and the name exists, but the type
1746 * does not, then we should make this noerror/nodata */
1747 if(ld && ld->rrsets) {
1748 int rcode = LDNS_RCODE_NOERROR;
1749 if(z != NULL && z->soa && z->soa_negative)
1750 return local_encode(qinfo, env, edns, repinfo, buf, temp,
1751 z->soa_negative, 0, rcode);
1752 /* NODATA: No EDE needed */
1753 local_error_encode(qinfo, env, edns, repinfo, buf, temp, rcode,
1754 (rcode|BIT_AA), LDNS_EDE_NONE, NULL);
1755 return 1;
1756 }
1757
1758 /* stop here, and resolve further on */
1759 return 0;
1760 }
1761
1762 /** print log information for an inform zone query */
1763 static void
lz_inform_print(struct local_zone * z,struct query_info * qinfo,struct sockaddr_storage * addr,socklen_t addrlen)1764 lz_inform_print(struct local_zone* z, struct query_info* qinfo,
1765 struct sockaddr_storage* addr, socklen_t addrlen)
1766 {
1767 char ip[128], txt[512];
1768 char zname[LDNS_MAX_DOMAINLEN+1];
1769 uint16_t port = ntohs(((struct sockaddr_in*)addr)->sin_port);
1770 dname_str(z->name, zname);
1771 addr_to_str(addr, addrlen, ip, sizeof(ip));
1772 snprintf(txt, sizeof(txt), "%s %s %s@%u", zname, local_zone_type2str(z->type), ip,
1773 (unsigned)port);
1774 log_nametypeclass(NO_VERBOSE, txt, qinfo->qname, qinfo->qtype, qinfo->qclass);
1775 }
1776
1777 static enum localzone_type
lz_type(uint8_t * taglist,size_t taglen,uint8_t * taglist2,size_t taglen2,uint8_t * tagactions,size_t tagactionssize,enum localzone_type lzt,struct comm_reply * repinfo,struct rbtree_type * override_tree,int * tag,char ** tagname,int num_tags)1778 lz_type(uint8_t *taglist, size_t taglen, uint8_t *taglist2, size_t taglen2,
1779 uint8_t *tagactions, size_t tagactionssize, enum localzone_type lzt,
1780 struct comm_reply* repinfo, struct rbtree_type* override_tree,
1781 int* tag, char** tagname, int num_tags)
1782 {
1783 struct local_zone_override* lzo;
1784 if(repinfo && override_tree) {
1785 lzo = (struct local_zone_override*)addr_tree_lookup(
1786 override_tree, &repinfo->client_addr,
1787 repinfo->client_addrlen);
1788 if(lzo && lzo->type) {
1789 verbose(VERB_ALGO, "local zone override to type %s",
1790 local_zone_type2str(lzo->type));
1791 return lzo->type;
1792 }
1793 }
1794 if(!taglist || !taglist2)
1795 return lzt;
1796 return local_data_find_tag_action(taglist, taglen, taglist2, taglen2,
1797 tagactions, tagactionssize, lzt, tag, tagname, num_tags);
1798 }
1799
1800 enum localzone_type
local_data_find_tag_action(const uint8_t * taglist,size_t taglen,const uint8_t * taglist2,size_t taglen2,const uint8_t * tagactions,size_t tagactionssize,enum localzone_type lzt,int * tag,char * const * tagname,int num_tags)1801 local_data_find_tag_action(const uint8_t* taglist, size_t taglen,
1802 const uint8_t* taglist2, size_t taglen2, const uint8_t* tagactions,
1803 size_t tagactionssize, enum localzone_type lzt, int* tag,
1804 char* const* tagname, int num_tags)
1805 {
1806 size_t i, j;
1807 uint8_t tagmatch;
1808
1809 for(i=0; i<taglen && i<taglen2; i++) {
1810 tagmatch = (taglist[i] & taglist2[i]);
1811 for(j=0; j<8 && tagmatch>0; j++) {
1812 if((tagmatch & 0x1)) {
1813 *tag = (int)(i*8+j);
1814 verbose(VERB_ALGO, "matched tag [%d] %s",
1815 *tag, (*tag<num_tags?tagname[*tag]:"null"));
1816 /* does this tag have a tag action? */
1817 if(i*8+j < tagactionssize && tagactions
1818 && tagactions[i*8+j] != 0) {
1819 verbose(VERB_ALGO, "tag action [%d] %s to type %s",
1820 *tag, (*tag<num_tags?tagname[*tag]:"null"),
1821 local_zone_type2str(
1822 (enum localzone_type)
1823 tagactions[i*8+j]));
1824 return (enum localzone_type)tagactions[i*8+j];
1825 }
1826 return lzt;
1827 }
1828 tagmatch >>= 1;
1829 }
1830 }
1831 return lzt;
1832 }
1833
1834 int
local_zones_answer(struct local_zones * zones,struct module_env * env,struct query_info * qinfo,struct edns_data * edns,sldns_buffer * buf,struct regional * temp,struct comm_reply * repinfo,uint8_t * taglist,size_t taglen,uint8_t * tagactions,size_t tagactionssize,struct config_strlist ** tag_datas,size_t tag_datas_size,char ** tagname,int num_tags,struct view * view)1835 local_zones_answer(struct local_zones* zones, struct module_env* env,
1836 struct query_info* qinfo, struct edns_data* edns, sldns_buffer* buf,
1837 struct regional* temp, struct comm_reply* repinfo, uint8_t* taglist,
1838 size_t taglen, uint8_t* tagactions, size_t tagactionssize,
1839 struct config_strlist** tag_datas, size_t tag_datas_size,
1840 char** tagname, int num_tags, struct view* view)
1841 {
1842 /* see if query is covered by a zone,
1843 * if so: - try to match (exact) local data
1844 * - look at zone type for negative response. */
1845 int labs = dname_count_labels(qinfo->qname);
1846 struct local_data* ld = NULL;
1847 struct local_zone* z = NULL;
1848 enum localzone_type lzt = local_zone_transparent;
1849 int r, tag = -1;
1850
1851 if(view) {
1852 lock_rw_rdlock(&view->lock);
1853 if(view->local_zones &&
1854 (z = local_zones_lookup(view->local_zones,
1855 qinfo->qname, qinfo->qname_len, labs,
1856 qinfo->qclass, qinfo->qtype))) {
1857 lock_rw_rdlock(&z->lock);
1858 lzt = z->type;
1859 }
1860 if(lzt == local_zone_noview) {
1861 lock_rw_unlock(&z->lock);
1862 z = NULL;
1863 }
1864 if(z && (lzt == local_zone_transparent ||
1865 lzt == local_zone_typetransparent ||
1866 lzt == local_zone_inform ||
1867 lzt == local_zone_always_transparent ||
1868 lzt == local_zone_block_a) &&
1869 local_zone_does_not_cover(z, qinfo, labs)) {
1870 lock_rw_unlock(&z->lock);
1871 z = NULL;
1872 }
1873 if(view->local_zones && !z && !view->isfirst){
1874 lock_rw_unlock(&view->lock);
1875 return 0;
1876 }
1877 if(z && verbosity >= VERB_ALGO) {
1878 char zname[255+1];
1879 dname_str(z->name, zname);
1880 verbose(VERB_ALGO, "using localzone %s %s from view %s",
1881 zname, local_zone_type2str(lzt), view->name);
1882 }
1883 lock_rw_unlock(&view->lock);
1884 }
1885 if(!z) {
1886 /* try global local_zones tree */
1887 lock_rw_rdlock(&zones->lock);
1888 if(!(z = local_zones_tags_lookup(zones, qinfo->qname,
1889 qinfo->qname_len, labs, qinfo->qclass, qinfo->qtype,
1890 taglist, taglen, 0))) {
1891 lock_rw_unlock(&zones->lock);
1892 return 0;
1893 }
1894 lock_rw_rdlock(&z->lock);
1895 lzt = lz_type(taglist, taglen, z->taglist, z->taglen,
1896 tagactions, tagactionssize, z->type, repinfo,
1897 z->override_tree, &tag, tagname, num_tags);
1898 lock_rw_unlock(&zones->lock);
1899 if(z && verbosity >= VERB_ALGO) {
1900 char zname[255+1];
1901 dname_str(z->name, zname);
1902 verbose(VERB_ALGO, "using localzone %s %s", zname,
1903 local_zone_type2str(lzt));
1904 }
1905 }
1906 if((env->cfg->log_local_actions ||
1907 lzt == local_zone_inform ||
1908 lzt == local_zone_inform_deny ||
1909 lzt == local_zone_inform_redirect)
1910 && repinfo)
1911 lz_inform_print(z, qinfo, &repinfo->client_addr,
1912 repinfo->client_addrlen);
1913
1914 if(lzt != local_zone_always_refuse
1915 && lzt != local_zone_always_transparent
1916 && lzt != local_zone_block_a
1917 && lzt != local_zone_always_nxdomain
1918 && lzt != local_zone_always_nodata
1919 && lzt != local_zone_always_deny
1920 && local_data_answer(z, env, qinfo, edns, repinfo, buf, temp, labs,
1921 &ld, lzt, tag, tag_datas, tag_datas_size, tagname, num_tags)) {
1922 lock_rw_unlock(&z->lock);
1923 /* We should tell the caller that encode is deferred if we found
1924 * a local alias. */
1925 return !qinfo->local_alias;
1926 }
1927 r = local_zones_zone_answer(z, env, qinfo, edns, repinfo, buf, temp, ld, lzt);
1928 lock_rw_unlock(&z->lock);
1929 return r && !qinfo->local_alias; /* see above */
1930 }
1931
local_zone_type2str(enum localzone_type t)1932 const char* local_zone_type2str(enum localzone_type t)
1933 {
1934 switch(t) {
1935 case local_zone_unset: return "unset";
1936 case local_zone_deny: return "deny";
1937 case local_zone_refuse: return "refuse";
1938 case local_zone_redirect: return "redirect";
1939 case local_zone_transparent: return "transparent";
1940 case local_zone_typetransparent: return "typetransparent";
1941 case local_zone_static: return "static";
1942 case local_zone_nodefault: return "nodefault";
1943 case local_zone_inform: return "inform";
1944 case local_zone_inform_deny: return "inform_deny";
1945 case local_zone_inform_redirect: return "inform_redirect";
1946 case local_zone_always_transparent: return "always_transparent";
1947 case local_zone_block_a: return "block_a";
1948 case local_zone_always_refuse: return "always_refuse";
1949 case local_zone_always_nxdomain: return "always_nxdomain";
1950 case local_zone_always_nodata: return "always_nodata";
1951 case local_zone_always_deny: return "always_deny";
1952 case local_zone_always_null: return "always_null";
1953 case local_zone_noview: return "noview";
1954 case local_zone_truncate: return "truncate";
1955 case local_zone_invalid: return "invalid";
1956 }
1957 return "badtyped";
1958 }
1959
local_zone_str2type(const char * type,enum localzone_type * t)1960 int local_zone_str2type(const char* type, enum localzone_type* t)
1961 {
1962 if(strcmp(type, "deny") == 0)
1963 *t = local_zone_deny;
1964 else if(strcmp(type, "refuse") == 0)
1965 *t = local_zone_refuse;
1966 else if(strcmp(type, "static") == 0)
1967 *t = local_zone_static;
1968 else if(strcmp(type, "transparent") == 0)
1969 *t = local_zone_transparent;
1970 else if(strcmp(type, "typetransparent") == 0)
1971 *t = local_zone_typetransparent;
1972 else if(strcmp(type, "redirect") == 0)
1973 *t = local_zone_redirect;
1974 else if(strcmp(type, "inform") == 0)
1975 *t = local_zone_inform;
1976 else if(strcmp(type, "inform_deny") == 0)
1977 *t = local_zone_inform_deny;
1978 else if(strcmp(type, "inform_redirect") == 0)
1979 *t = local_zone_inform_redirect;
1980 else if(strcmp(type, "always_transparent") == 0)
1981 *t = local_zone_always_transparent;
1982 else if(strcmp(type, "block_a") == 0)
1983 *t = local_zone_block_a;
1984 else if(strcmp(type, "always_refuse") == 0)
1985 *t = local_zone_always_refuse;
1986 else if(strcmp(type, "always_nxdomain") == 0)
1987 *t = local_zone_always_nxdomain;
1988 else if(strcmp(type, "always_nodata") == 0)
1989 *t = local_zone_always_nodata;
1990 else if(strcmp(type, "always_deny") == 0)
1991 *t = local_zone_always_deny;
1992 else if(strcmp(type, "always_null") == 0)
1993 *t = local_zone_always_null;
1994 else if(strcmp(type, "noview") == 0)
1995 *t = local_zone_noview;
1996 else if(strcmp(type, "truncate") == 0)
1997 *t = local_zone_truncate;
1998 else if(strcmp(type, "nodefault") == 0)
1999 *t = local_zone_nodefault;
2000 else return 0;
2001 return 1;
2002 }
2003
2004 /** iterate over the kiddies of the given name and set their parent ptr */
2005 static void
set_kiddo_parents(struct local_zone * z,struct local_zone * match,struct local_zone * newp)2006 set_kiddo_parents(struct local_zone* z, struct local_zone* match,
2007 struct local_zone* newp)
2008 {
2009 /* both zones and z are locked already */
2010 /* in the sorted rbtree, the kiddies of z are located after z */
2011 /* z must be present in the tree */
2012 struct local_zone* p = z;
2013 p = (struct local_zone*)rbtree_next(&p->node);
2014 while(p!=(struct local_zone*)RBTREE_NULL &&
2015 p->dclass == z->dclass && dname_strict_subdomain(p->name,
2016 p->namelabs, z->name, z->namelabs)) {
2017 /* update parent ptr */
2018 /* only when matches with existing parent pointer, so that
2019 * deeper child structures are not touched, i.e.
2020 * update of x, and a.x, b.x, f.b.x, g.b.x, c.x, y
2021 * gets to update a.x, b.x and c.x */
2022 lock_rw_wrlock(&p->lock);
2023 if(p->parent == match)
2024 p->parent = newp;
2025 lock_rw_unlock(&p->lock);
2026 p = (struct local_zone*)rbtree_next(&p->node);
2027 }
2028 }
2029
local_zones_add_zone(struct local_zones * zones,uint8_t * name,size_t len,int labs,uint16_t dclass,enum localzone_type tp)2030 struct local_zone* local_zones_add_zone(struct local_zones* zones,
2031 uint8_t* name, size_t len, int labs, uint16_t dclass,
2032 enum localzone_type tp)
2033 {
2034 int exact;
2035 /* create */
2036 struct local_zone *prev;
2037 struct local_zone* z = local_zone_create(name, len, labs, tp, dclass);
2038 if(!z) {
2039 free(name);
2040 return NULL;
2041 }
2042 lock_rw_wrlock(&z->lock);
2043
2044 /* find the closest parent */
2045 prev = local_zones_find_le(zones, name, len, labs, dclass, &exact);
2046 if(!exact)
2047 z->parent = find_closest_parent(z, prev);
2048
2049 /* insert into the tree */
2050 if(exact||!rbtree_insert(&zones->ztree, &z->node)) {
2051 /* duplicate entry! */
2052 lock_rw_unlock(&z->lock);
2053 local_zone_delete(z);
2054 log_err("internal: duplicate entry in local_zones_add_zone");
2055 return NULL;
2056 }
2057
2058 /* set parent pointers right */
2059 set_kiddo_parents(z, z->parent, z);
2060
2061 lock_rw_unlock(&z->lock);
2062 return z;
2063 }
2064
local_zones_del_zone(struct local_zones * zones,struct local_zone * z)2065 void local_zones_del_zone(struct local_zones* zones, struct local_zone* z)
2066 {
2067 /* fix up parents in tree */
2068 lock_rw_wrlock(&z->lock);
2069 set_kiddo_parents(z, z, z->parent);
2070
2071 /* remove from tree */
2072 (void)rbtree_delete(&zones->ztree, z);
2073
2074 /* delete the zone */
2075 lock_rw_unlock(&z->lock);
2076 local_zone_delete(z);
2077 }
2078
2079 int
local_zones_add_RR(struct local_zones * zones,const char * rr)2080 local_zones_add_RR(struct local_zones* zones, const char* rr)
2081 {
2082 uint8_t* rr_name;
2083 uint16_t rr_class, rr_type;
2084 size_t len;
2085 int labs;
2086 struct local_zone* z;
2087 int r;
2088 if(!get_rr_nameclass(rr, &rr_name, &rr_class, &rr_type)) {
2089 return 0;
2090 }
2091 labs = dname_count_size_labels(rr_name, &len);
2092 /* could first try readlock then get writelock if zone does not exist,
2093 * but we do not add enough RRs (from multiple threads) to optimize */
2094 lock_rw_wrlock(&zones->lock);
2095 z = local_zones_lookup(zones, rr_name, len, labs, rr_class, rr_type);
2096 if(!z) {
2097 z = local_zones_add_zone(zones, rr_name, len, labs, rr_class,
2098 local_zone_transparent);
2099 if(!z) {
2100 lock_rw_unlock(&zones->lock);
2101 return 0;
2102 }
2103 } else {
2104 free(rr_name);
2105 }
2106 lock_rw_wrlock(&z->lock);
2107 lock_rw_unlock(&zones->lock);
2108 r = lz_enter_rr_into_zone(z, rr);
2109 lock_rw_unlock(&z->lock);
2110 return r;
2111 }
2112
2113 /** returns true if the node is terminal so no deeper domain names exist */
2114 static int
is_terminal(struct local_data * d)2115 is_terminal(struct local_data* d)
2116 {
2117 /* for empty nonterminals, the deeper domain names are sorted
2118 * right after them, so simply check the next name in the tree
2119 */
2120 struct local_data* n = (struct local_data*)rbtree_next(&d->node);
2121 if(n == (struct local_data*)RBTREE_NULL)
2122 return 1; /* last in tree, no deeper node */
2123 if(dname_strict_subdomain(n->name, n->namelabs, d->name, d->namelabs))
2124 return 0; /* there is a deeper node */
2125 return 1;
2126 }
2127
2128 /** delete empty terminals from tree when final data is deleted */
2129 static void
del_empty_term(struct local_zone * z,struct local_data * d,uint8_t * name,size_t len,int labs)2130 del_empty_term(struct local_zone* z, struct local_data* d,
2131 uint8_t* name, size_t len, int labs)
2132 {
2133 while(d && d->rrsets == NULL && is_terminal(d)) {
2134 /* is this empty nonterminal? delete */
2135 /* note, no memory recycling in zone region */
2136 (void)rbtree_delete(&z->data, d);
2137
2138 /* go up and to the next label */
2139 if(dname_is_root(name))
2140 return;
2141 dname_remove_label(&name, &len);
2142 labs--;
2143 d = local_zone_find_data(z, name, len, labs);
2144 }
2145 }
2146
2147 /** find and remove type from list in domain struct */
2148 static void
del_local_rrset(struct local_data * d,uint16_t dtype)2149 del_local_rrset(struct local_data* d, uint16_t dtype)
2150 {
2151 struct local_rrset* prev=NULL, *p=d->rrsets;
2152 while(p && ntohs(p->rrset->rk.type) != dtype) {
2153 prev = p;
2154 p = p->next;
2155 }
2156 if(!p)
2157 return; /* rrset type not found */
2158 /* unlink it */
2159 if(prev) prev->next = p->next;
2160 else d->rrsets = p->next;
2161 /* no memory recycling for zone deletions ... */
2162 }
2163
local_zones_del_data(struct local_zones * zones,uint8_t * name,size_t len,int labs,uint16_t dclass)2164 void local_zones_del_data(struct local_zones* zones,
2165 uint8_t* name, size_t len, int labs, uint16_t dclass)
2166 {
2167 /* find zone */
2168 struct local_zone* z;
2169 struct local_data* d;
2170
2171 /* remove DS */
2172 lock_rw_rdlock(&zones->lock);
2173 z = local_zones_lookup(zones, name, len, labs, dclass, LDNS_RR_TYPE_DS);
2174 if(z) {
2175 lock_rw_wrlock(&z->lock);
2176 d = local_zone_find_data(z, name, len, labs);
2177 if(d) {
2178 del_local_rrset(d, LDNS_RR_TYPE_DS);
2179 del_empty_term(z, d, name, len, labs);
2180 }
2181 lock_rw_unlock(&z->lock);
2182 }
2183 lock_rw_unlock(&zones->lock);
2184
2185 /* remove other types */
2186 lock_rw_rdlock(&zones->lock);
2187 z = local_zones_lookup(zones, name, len, labs, dclass, 0);
2188 if(!z) {
2189 /* no such zone, we're done */
2190 lock_rw_unlock(&zones->lock);
2191 return;
2192 }
2193 lock_rw_wrlock(&z->lock);
2194 lock_rw_unlock(&zones->lock);
2195
2196 /* find the domain */
2197 d = local_zone_find_data(z, name, len, labs);
2198 if(d) {
2199 /* no memory recycling for zone deletions ... */
2200 d->rrsets = NULL;
2201 /* did we delete the soa record ? */
2202 if(query_dname_compare(d->name, z->name) == 0) {
2203 z->soa = NULL;
2204 z->soa_negative = NULL;
2205 }
2206
2207 /* cleanup the empty nonterminals for this name */
2208 del_empty_term(z, d, name, len, labs);
2209 }
2210
2211 lock_rw_unlock(&z->lock);
2212 }
2213