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