2 * services/authzone.c - authoritative zone that is locally hosted.
4 * Copyright (c) 2017, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains the functions for an authority zone. This zone
40 * is queried by the iterator, just like a stub or forward zone, but then
41 * the data is locally held.
45 #include "services/authzone.h"
46 #include "util/data/dname.h"
47 #include "util/data/msgparse.h"
48 #include "util/data/msgreply.h"
49 #include "util/data/msgencode.h"
50 #include "util/data/packed_rrset.h"
51 #include "util/regional.h"
52 #include "util/net_help.h"
53 #include "util/netevent.h"
54 #include "util/config_file.h"
56 #include "util/module.h"
57 #include "util/random.h"
58 #include "services/cache/dns.h"
59 #include "services/outside_network.h"
60 #include "services/listen_dnsport.h"
61 #include "services/mesh.h"
62 #include "sldns/rrdef.h"
63 #include "sldns/pkthdr.h"
64 #include "sldns/sbuffer.h"
65 #include "sldns/str2wire.h"
66 #include "sldns/wire2str.h"
67 #include "sldns/parseutil.h"
68 #include "sldns/keyraw.h"
69 #include "validator/val_nsec3.h"
70 #include "validator/val_nsec.h"
71 #include "validator/val_secalgo.h"
72 #include "validator/val_sigcrypt.h"
73 #include "validator/val_anchor.h"
74 #include "validator/val_utils.h"
77 /** bytes to use for NSEC3 hash buffer. 20 for sha1 */
78 #define N3HASHBUFLEN 32
79 /** max number of CNAMEs we are willing to follow (in one answer) */
80 #define MAX_CNAME_CHAIN 8
81 /** timeout for probe packets for SOA */
82 #define AUTH_PROBE_TIMEOUT 100 /* msec */
83 /** when to stop with SOA probes (when exponential timeouts exceed this) */
84 #define AUTH_PROBE_TIMEOUT_STOP 1000 /* msec */
85 /* auth transfer timeout for TCP connections, in msec */
86 #define AUTH_TRANSFER_TIMEOUT 10000 /* msec */
87 /* auth transfer max backoff for failed transfers and probes */
88 #define AUTH_TRANSFER_MAX_BACKOFF 86400 /* sec */
89 /* auth http port number */
90 #define AUTH_HTTP_PORT 80
91 /* auth https port number */
92 #define AUTH_HTTPS_PORT 443
93 /* max depth for nested $INCLUDEs */
94 #define MAX_INCLUDE_DEPTH 10
95 /** number of timeouts before we fallback from IXFR to AXFR,
96 * because some versions of servers (eg. dnsmasq) drop IXFR packets. */
97 #define NUM_TIMEOUTS_FALLBACK_IXFR 3
99 /** pick up nextprobe task to start waiting to perform transfer actions */
100 static void xfr_set_timeout(struct auth_xfer* xfr, struct module_env* env,
101 int failure, int lookup_only);
102 /** move to sending the probe packets, next if fails. task_probe */
103 static void xfr_probe_send_or_end(struct auth_xfer* xfr,
104 struct module_env* env);
105 /** pick up probe task with specified(or NULL) destination first,
106 * or transfer task if nothing to probe, or false if already in progress */
107 static int xfr_start_probe(struct auth_xfer* xfr, struct module_env* env,
108 struct auth_master* spec);
109 /** delete xfer structure (not its tree entry) */
110 static void auth_xfer_delete(struct auth_xfer* xfr);
112 /** create new dns_msg */
113 static struct dns_msg*
114 msg_create(struct regional* region, struct query_info* qinfo)
116 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
117 sizeof(struct dns_msg));
120 msg->qinfo.qname = regional_alloc_init(region, qinfo->qname,
122 if(!msg->qinfo.qname)
124 msg->qinfo.qname_len = qinfo->qname_len;
125 msg->qinfo.qtype = qinfo->qtype;
126 msg->qinfo.qclass = qinfo->qclass;
127 msg->qinfo.local_alias = NULL;
128 /* non-packed reply_info, because it needs to grow the array */
129 msg->rep = (struct reply_info*)regional_alloc_zero(region,
130 sizeof(struct reply_info)-sizeof(struct rrset_ref));
133 msg->rep->flags = (uint16_t)(BIT_QR | BIT_AA);
134 msg->rep->authoritative = 1;
135 msg->rep->reason_bogus = LDNS_EDE_NONE;
136 msg->rep->qdcount = 1;
137 /* rrsets is NULL, no rrsets yet */
141 /** grow rrset array by one in msg */
143 msg_grow_array(struct regional* region, struct dns_msg* msg)
145 if(msg->rep->rrsets == NULL) {
146 msg->rep->rrsets = regional_alloc_zero(region,
147 sizeof(struct ub_packed_rrset_key*)*(msg->rep->rrset_count+1));
148 if(!msg->rep->rrsets)
151 struct ub_packed_rrset_key** rrsets_old = msg->rep->rrsets;
152 msg->rep->rrsets = regional_alloc_zero(region,
153 sizeof(struct ub_packed_rrset_key*)*(msg->rep->rrset_count+1));
154 if(!msg->rep->rrsets)
156 memmove(msg->rep->rrsets, rrsets_old,
157 sizeof(struct ub_packed_rrset_key*)*msg->rep->rrset_count);
162 /** get ttl of rrset */
164 get_rrset_ttl(struct ub_packed_rrset_key* k)
166 struct packed_rrset_data* d = (struct packed_rrset_data*)
171 /** Copy rrset into region from domain-datanode and packet rrset */
172 static struct ub_packed_rrset_key*
173 auth_packed_rrset_copy_region(struct auth_zone* z, struct auth_data* node,
174 struct auth_rrset* rrset, struct regional* region, time_t adjust)
176 struct ub_packed_rrset_key key;
177 memset(&key, 0, sizeof(key));
178 key.entry.key = &key;
179 key.entry.data = rrset->data;
180 key.rk.dname = node->name;
181 key.rk.dname_len = node->namelen;
182 key.rk.type = htons(rrset->type);
183 key.rk.rrset_class = htons(z->dclass);
184 key.entry.hash = rrset_key_hash(&key.rk);
185 return packed_rrset_copy_region(&key, region, adjust);
188 /** fix up msg->rep TTL and prefetch ttl */
190 msg_ttl(struct dns_msg* msg)
192 if(msg->rep->rrset_count == 0) return;
193 if(msg->rep->rrset_count == 1) {
194 msg->rep->ttl = get_rrset_ttl(msg->rep->rrsets[0]);
195 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
196 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
197 } else if(get_rrset_ttl(msg->rep->rrsets[msg->rep->rrset_count-1]) <
199 msg->rep->ttl = get_rrset_ttl(msg->rep->rrsets[
200 msg->rep->rrset_count-1]);
201 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
202 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
206 /** see if rrset is a duplicate in the answer message */
208 msg_rrset_duplicate(struct dns_msg* msg, uint8_t* nm, size_t nmlen,
209 uint16_t type, uint16_t dclass)
212 for(i=0; i<msg->rep->rrset_count; i++) {
213 struct ub_packed_rrset_key* k = msg->rep->rrsets[i];
214 if(ntohs(k->rk.type) == type && k->rk.dname_len == nmlen &&
215 ntohs(k->rk.rrset_class) == dclass &&
216 query_dname_compare(k->rk.dname, nm) == 0)
222 /** add rrset to answer section (no auth, add rrsets yet) */
224 msg_add_rrset_an(struct auth_zone* z, struct regional* region,
225 struct dns_msg* msg, struct auth_data* node, struct auth_rrset* rrset)
227 log_assert(msg->rep->ns_numrrsets == 0);
228 log_assert(msg->rep->ar_numrrsets == 0);
231 if(msg_rrset_duplicate(msg, node->name, node->namelen, rrset->type,
235 if(!msg_grow_array(region, msg))
238 if(!(msg->rep->rrsets[msg->rep->rrset_count] =
239 auth_packed_rrset_copy_region(z, node, rrset, region, 0)))
241 msg->rep->rrset_count++;
242 msg->rep->an_numrrsets++;
247 /** add rrset to authority section (no additional section rrsets yet) */
249 msg_add_rrset_ns(struct auth_zone* z, struct regional* region,
250 struct dns_msg* msg, struct auth_data* node, struct auth_rrset* rrset)
252 log_assert(msg->rep->ar_numrrsets == 0);
255 if(msg_rrset_duplicate(msg, node->name, node->namelen, rrset->type,
259 if(!msg_grow_array(region, msg))
262 if(!(msg->rep->rrsets[msg->rep->rrset_count] =
263 auth_packed_rrset_copy_region(z, node, rrset, region, 0)))
265 msg->rep->rrset_count++;
266 msg->rep->ns_numrrsets++;
271 /** add rrset to additional section */
273 msg_add_rrset_ar(struct auth_zone* z, struct regional* region,
274 struct dns_msg* msg, struct auth_data* node, struct auth_rrset* rrset)
278 if(msg_rrset_duplicate(msg, node->name, node->namelen, rrset->type,
282 if(!msg_grow_array(region, msg))
285 if(!(msg->rep->rrsets[msg->rep->rrset_count] =
286 auth_packed_rrset_copy_region(z, node, rrset, region, 0)))
288 msg->rep->rrset_count++;
289 msg->rep->ar_numrrsets++;
294 struct auth_zones* auth_zones_create(void)
296 struct auth_zones* az = (struct auth_zones*)calloc(1, sizeof(*az));
298 log_err("out of memory");
301 rbtree_init(&az->ztree, &auth_zone_cmp);
302 rbtree_init(&az->xtree, &auth_xfer_cmp);
303 lock_rw_init(&az->lock);
304 lock_protect(&az->lock, &az->ztree, sizeof(az->ztree));
305 lock_protect(&az->lock, &az->xtree, sizeof(az->xtree));
306 /* also lock protects the rbnode's in struct auth_zone, auth_xfer */
307 lock_rw_init(&az->rpz_lock);
308 lock_protect(&az->rpz_lock, &az->rpz_first, sizeof(az->rpz_first));
312 int auth_zone_cmp(const void* z1, const void* z2)
314 /* first sort on class, so that hierarchy can be maintained within
316 struct auth_zone* a = (struct auth_zone*)z1;
317 struct auth_zone* b = (struct auth_zone*)z2;
319 if(a->dclass != b->dclass) {
320 if(a->dclass < b->dclass)
324 /* sorted such that higher zones sort before lower zones (their
326 return dname_lab_cmp(a->name, a->namelabs, b->name, b->namelabs, &m);
329 int auth_data_cmp(const void* z1, const void* z2)
331 struct auth_data* a = (struct auth_data*)z1;
332 struct auth_data* b = (struct auth_data*)z2;
334 /* canonical sort, because DNSSEC needs that */
335 return dname_canon_lab_cmp(a->name, a->namelabs, b->name,
339 int auth_xfer_cmp(const void* z1, const void* z2)
341 /* first sort on class, so that hierarchy can be maintained within
343 struct auth_xfer* a = (struct auth_xfer*)z1;
344 struct auth_xfer* b = (struct auth_xfer*)z2;
346 if(a->dclass != b->dclass) {
347 if(a->dclass < b->dclass)
351 /* sorted such that higher zones sort before lower zones (their
353 return dname_lab_cmp(a->name, a->namelabs, b->name, b->namelabs, &m);
356 /** delete auth rrset node */
358 auth_rrset_delete(struct auth_rrset* rrset)
365 /** delete auth data domain node */
367 auth_data_delete(struct auth_data* n)
369 struct auth_rrset* p, *np;
374 auth_rrset_delete(p);
381 /** helper traverse to delete zones */
383 auth_data_del(rbnode_type* n, void* ATTR_UNUSED(arg))
385 struct auth_data* z = (struct auth_data*)n->key;
389 /** delete an auth zone structure (tree remove must be done elsewhere) */
391 auth_zone_delete(struct auth_zone* z, struct auth_zones* az)
394 lock_rw_destroy(&z->lock);
395 traverse_postorder(&z->data, auth_data_del, NULL);
398 /* keep RPZ linked list intact */
399 lock_rw_wrlock(&az->rpz_lock);
401 z->rpz_az_prev->rpz_az_next = z->rpz_az_next;
403 az->rpz_first = z->rpz_az_next;
405 z->rpz_az_next->rpz_az_prev = z->rpz_az_prev;
406 lock_rw_unlock(&az->rpz_lock);
416 auth_zone_create(struct auth_zones* az, uint8_t* nm, size_t nmlen,
419 struct auth_zone* z = (struct auth_zone*)calloc(1, sizeof(*z));
426 z->namelabs = dname_count_labels(nm);
427 z->name = memdup(nm, nmlen);
432 rbtree_init(&z->data, &auth_data_cmp);
433 lock_rw_init(&z->lock);
434 lock_protect(&z->lock, &z->name, sizeof(*z)-sizeof(rbnode_type)-
435 sizeof(&z->rpz_az_next)-sizeof(&z->rpz_az_prev));
436 lock_rw_wrlock(&z->lock);
437 /* z lock protects all, except rbtree itself and the rpz linked list
438 * pointers, which are protected using az->lock */
439 if(!rbtree_insert(&az->ztree, &z->node)) {
440 lock_rw_unlock(&z->lock);
441 auth_zone_delete(z, NULL);
442 log_warn("duplicate auth zone");
449 auth_zone_find(struct auth_zones* az, uint8_t* nm, size_t nmlen,
452 struct auth_zone key;
457 key.namelabs = dname_count_labels(nm);
458 return (struct auth_zone*)rbtree_search(&az->ztree, &key);
462 auth_xfer_find(struct auth_zones* az, uint8_t* nm, size_t nmlen,
465 struct auth_xfer key;
470 key.namelabs = dname_count_labels(nm);
471 return (struct auth_xfer*)rbtree_search(&az->xtree, &key);
474 /** find an auth zone or sorted less-or-equal, return true if exact */
476 auth_zone_find_less_equal(struct auth_zones* az, uint8_t* nm, size_t nmlen,
477 uint16_t dclass, struct auth_zone** z)
479 struct auth_zone key;
484 key.namelabs = dname_count_labels(nm);
485 return rbtree_find_less_equal(&az->ztree, &key, (rbnode_type**)z);
489 /** find the auth zone that is above the given name */
491 auth_zones_find_zone(struct auth_zones* az, uint8_t* name, size_t name_len,
495 size_t nmlen = name_len;
497 if(auth_zone_find_less_equal(az, nm, nmlen, dclass, &z)) {
501 /* less-or-nothing */
502 if(!z) return NULL; /* nothing smaller, nothing above it */
503 /* we found smaller name; smaller may be above the name,
504 * but not below it. */
505 nm = dname_get_shared_topdomain(z->name, name);
506 dname_count_size_labels(nm, &nmlen);
512 z = auth_zone_find(az, nm, nmlen, dclass);
514 if(dname_is_root(nm)) break;
515 dname_remove_label(&nm, &nmlen);
520 /** find or create zone with name str. caller must have lock on az.
521 * returns a wrlocked zone */
522 static struct auth_zone*
523 auth_zones_find_or_add_zone(struct auth_zones* az, char* name)
525 uint8_t nm[LDNS_MAX_DOMAINLEN+1];
526 size_t nmlen = sizeof(nm);
529 if(sldns_str2wire_dname_buf(name, nm, &nmlen) != 0) {
530 log_err("cannot parse auth zone name: %s", name);
533 z = auth_zone_find(az, nm, nmlen, LDNS_RR_CLASS_IN);
535 /* not found, create the zone */
536 z = auth_zone_create(az, nm, nmlen, LDNS_RR_CLASS_IN);
538 lock_rw_wrlock(&z->lock);
543 /** find or create xfer zone with name str. caller must have lock on az.
544 * returns a locked xfer */
545 static struct auth_xfer*
546 auth_zones_find_or_add_xfer(struct auth_zones* az, struct auth_zone* z)
549 x = auth_xfer_find(az, z->name, z->namelen, z->dclass);
551 /* not found, create the zone */
552 x = auth_xfer_create(az, z);
554 lock_basic_lock(&x->lock);
560 auth_zone_set_zonefile(struct auth_zone* z, char* zonefile)
562 if(z->zonefile) free(z->zonefile);
563 if(zonefile == NULL) {
566 z->zonefile = strdup(zonefile);
568 log_err("malloc failure");
575 /** set auth zone fallback. caller must have lock on zone */
577 auth_zone_set_fallback(struct auth_zone* z, char* fallbackstr)
579 if(strcmp(fallbackstr, "yes") != 0 && strcmp(fallbackstr, "no") != 0){
580 log_err("auth zone fallback, expected yes or no, got %s",
584 z->fallback_enabled = (strcmp(fallbackstr, "yes")==0);
588 /** create domain with the given name */
589 static struct auth_data*
590 az_domain_create(struct auth_zone* z, uint8_t* nm, size_t nmlen)
592 struct auth_data* n = (struct auth_data*)malloc(sizeof(*n));
594 memset(n, 0, sizeof(*n));
596 n->name = memdup(nm, nmlen);
602 n->namelabs = dname_count_labels(nm);
603 if(!rbtree_insert(&z->data, &n->node)) {
604 log_warn("duplicate auth domain name");
612 /** find domain with exactly the given name */
613 static struct auth_data*
614 az_find_name(struct auth_zone* z, uint8_t* nm, size_t nmlen)
616 struct auth_zone key;
620 key.namelabs = dname_count_labels(nm);
621 return (struct auth_data*)rbtree_search(&z->data, &key);
624 /** Find domain name (or closest match) */
626 az_find_domain(struct auth_zone* z, struct query_info* qinfo, int* node_exact,
627 struct auth_data** node)
629 struct auth_zone key;
631 key.name = qinfo->qname;
632 key.namelen = qinfo->qname_len;
633 key.namelabs = dname_count_labels(key.name);
634 *node_exact = rbtree_find_less_equal(&z->data, &key,
635 (rbnode_type**)node);
638 /** find or create domain with name in zone */
639 static struct auth_data*
640 az_domain_find_or_create(struct auth_zone* z, uint8_t* dname,
643 struct auth_data* n = az_find_name(z, dname, dname_len);
645 n = az_domain_create(z, dname, dname_len);
650 /** find rrset of given type in the domain */
651 static struct auth_rrset*
652 az_domain_rrset(struct auth_data* n, uint16_t t)
654 struct auth_rrset* rrset;
665 /** remove rrset of this type from domain */
667 domain_remove_rrset(struct auth_data* node, uint16_t rr_type)
669 struct auth_rrset* rrset, *prev;
672 rrset = node->rrsets;
674 if(rrset->type == rr_type) {
675 /* found it, now delete it */
676 if(prev) prev->next = rrset->next;
677 else node->rrsets = rrset->next;
678 auth_rrset_delete(rrset);
686 /** find an rrsig index in the rrset. returns true if found */
688 az_rrset_find_rrsig(struct packed_rrset_data* d, uint8_t* rdata, size_t len,
692 for(i=d->count; i<d->count + d->rrsig_count; i++) {
693 if(d->rr_len[i] != len)
695 if(memcmp(d->rr_data[i], rdata, len) == 0) {
703 /** see if rdata is duplicate */
705 rdata_duplicate(struct packed_rrset_data* d, uint8_t* rdata, size_t len)
708 for(i=0; i<d->count + d->rrsig_count; i++) {
709 if(d->rr_len[i] != len)
711 if(memcmp(d->rr_data[i], rdata, len) == 0)
717 /** get rrsig type covered from rdata.
718 * @param rdata: rdata in wireformat, starting with 16bit rdlength.
719 * @param rdatalen: length of rdata buffer.
720 * @return type covered (or 0).
723 rrsig_rdata_get_type_covered(uint8_t* rdata, size_t rdatalen)
727 return sldns_read_uint16(rdata+2);
730 /** remove RR from existing RRset. Also sig, if it is a signature.
731 * reallocates the packed rrset for a new one, false on alloc failure */
733 rrset_remove_rr(struct auth_rrset* rrset, size_t index)
735 struct packed_rrset_data* d, *old = rrset->data;
737 if(index >= old->count + old->rrsig_count)
738 return 0; /* index out of bounds */
739 d = (struct packed_rrset_data*)calloc(1, packed_rrset_sizeof(old) - (
740 sizeof(size_t) + sizeof(uint8_t*) + sizeof(time_t) +
741 old->rr_len[index]));
743 log_err("malloc failure");
747 d->count = old->count;
748 d->rrsig_count = old->rrsig_count;
749 if(index < d->count) d->count--;
750 else d->rrsig_count--;
751 d->trust = old->trust;
752 d->security = old->security;
754 /* set rr_len, needed for ptr_fixup */
755 d->rr_len = (size_t*)((uint8_t*)d +
756 sizeof(struct packed_rrset_data));
758 memmove(d->rr_len, old->rr_len, (index)*sizeof(size_t));
759 if(index+1 < old->count+old->rrsig_count)
760 memmove(&d->rr_len[index], &old->rr_len[index+1],
761 (old->count+old->rrsig_count - (index+1))*sizeof(size_t));
762 packed_rrset_ptr_fixup(d);
766 memmove(d->rr_ttl, old->rr_ttl, (index)*sizeof(time_t));
767 if(index+1 < old->count+old->rrsig_count)
768 memmove(&d->rr_ttl[index], &old->rr_ttl[index+1],
769 (old->count+old->rrsig_count - (index+1))*sizeof(time_t));
771 /* move over rr_data */
772 for(i=0; i<d->count+d->rrsig_count; i++) {
774 if(i < index) oldi = i;
776 memmove(d->rr_data[i], old->rr_data[oldi], d->rr_len[i]);
779 /* recalc ttl (lowest of remaining RR ttls) */
780 if(d->count + d->rrsig_count > 0)
781 d->ttl = d->rr_ttl[0];
782 for(i=0; i<d->count+d->rrsig_count; i++) {
783 if(d->rr_ttl[i] < d->ttl)
784 d->ttl = d->rr_ttl[i];
792 /** add RR to existing RRset. If insert_sig is true, add to rrsigs.
793 * This reallocates the packed rrset for a new one */
795 rrset_add_rr(struct auth_rrset* rrset, uint32_t rr_ttl, uint8_t* rdata,
796 size_t rdatalen, int insert_sig)
798 struct packed_rrset_data* d, *old = rrset->data;
799 size_t total, old_total;
801 d = (struct packed_rrset_data*)calloc(1, packed_rrset_sizeof(old)
802 + sizeof(size_t) + sizeof(uint8_t*) + sizeof(time_t)
805 log_err("out of memory");
808 /* copy base values */
809 memcpy(d, old, sizeof(struct packed_rrset_data));
815 old_total = old->count + old->rrsig_count;
816 total = d->count + d->rrsig_count;
817 /* set rr_len, needed for ptr_fixup */
818 d->rr_len = (size_t*)((uint8_t*)d +
819 sizeof(struct packed_rrset_data));
821 memmove(d->rr_len, old->rr_len, old->count*sizeof(size_t));
822 if(old->rrsig_count != 0)
823 memmove(d->rr_len+d->count, old->rr_len+old->count,
824 old->rrsig_count*sizeof(size_t));
826 d->rr_len[d->count-1] = rdatalen;
827 else d->rr_len[total-1] = rdatalen;
828 packed_rrset_ptr_fixup(d);
829 if((time_t)rr_ttl < d->ttl)
832 /* copy old values into new array */
833 if(old->count != 0) {
834 memmove(d->rr_ttl, old->rr_ttl, old->count*sizeof(time_t));
835 /* all the old rr pieces are allocated sequential, so we
836 * can copy them in one go */
837 memmove(d->rr_data[0], old->rr_data[0],
838 (old->rr_data[old->count-1] - old->rr_data[0]) +
839 old->rr_len[old->count-1]);
841 if(old->rrsig_count != 0) {
842 memmove(d->rr_ttl+d->count, old->rr_ttl+old->count,
843 old->rrsig_count*sizeof(time_t));
844 memmove(d->rr_data[d->count], old->rr_data[old->count],
845 (old->rr_data[old_total-1] - old->rr_data[old->count]) +
846 old->rr_len[old_total-1]);
849 /* insert new value */
851 d->rr_ttl[d->count-1] = rr_ttl;
852 memmove(d->rr_data[d->count-1], rdata, rdatalen);
854 d->rr_ttl[total-1] = rr_ttl;
855 memmove(d->rr_data[total-1], rdata, rdatalen);
863 /** Create new rrset for node with packed rrset with one RR element */
864 static struct auth_rrset*
865 rrset_create(struct auth_data* node, uint16_t rr_type, uint32_t rr_ttl,
866 uint8_t* rdata, size_t rdatalen)
868 struct auth_rrset* rrset = (struct auth_rrset*)calloc(1,
870 struct auth_rrset* p, *prev;
871 struct packed_rrset_data* d;
873 log_err("out of memory");
876 rrset->type = rr_type;
878 /* the rrset data structure, with one RR */
879 d = (struct packed_rrset_data*)calloc(1,
880 sizeof(struct packed_rrset_data) + sizeof(size_t) +
881 sizeof(uint8_t*) + sizeof(time_t) + rdatalen);
884 log_err("out of memory");
889 d->trust = rrset_trust_prim_noglue;
890 d->rr_len = (size_t*)((uint8_t*)d + sizeof(struct packed_rrset_data));
891 d->rr_data = (uint8_t**)&(d->rr_len[1]);
892 d->rr_ttl = (time_t*)&(d->rr_data[1]);
893 d->rr_data[0] = (uint8_t*)&(d->rr_ttl[1]);
896 d->rr_len[0] = rdatalen;
897 d->rr_ttl[0] = rr_ttl;
898 memmove(d->rr_data[0], rdata, rdatalen);
901 /* insert rrset into linked list for domain */
902 /* find sorted place to link the rrset into the list */
905 while(p && p->type<=rr_type) {
909 /* so, prev is smaller, and p is larger than rr_type */
911 if(prev) prev->next = rrset;
912 else node->rrsets = rrset;
916 /** count number (and size) of rrsigs that cover a type */
918 rrsig_num_that_cover(struct auth_rrset* rrsig, uint16_t rr_type, size_t* sigsz)
920 struct packed_rrset_data* d = rrsig->data;
923 log_assert(d && rrsig->type == LDNS_RR_TYPE_RRSIG);
924 for(i=0; i<d->count+d->rrsig_count; i++) {
925 if(rrsig_rdata_get_type_covered(d->rr_data[i],
926 d->rr_len[i]) == rr_type) {
928 (*sigsz) += d->rr_len[i];
934 /** See if rrsig set has covered sigs for rrset and move them over */
936 rrset_moveover_rrsigs(struct auth_data* node, uint16_t rr_type,
937 struct auth_rrset* rrset, struct auth_rrset* rrsig)
939 size_t sigs, sigsz, i, j, total;
940 struct packed_rrset_data* sigold = rrsig->data;
941 struct packed_rrset_data* old = rrset->data;
942 struct packed_rrset_data* d, *sigd;
944 log_assert(rrset->type == rr_type);
945 log_assert(rrsig->type == LDNS_RR_TYPE_RRSIG);
946 sigs = rrsig_num_that_cover(rrsig, rr_type, &sigsz);
948 /* 0 rrsigs to move over, done */
952 /* allocate rrset sigsz larger for extra sigs elements, and
953 * allocate rrsig sigsz smaller for less sigs elements. */
954 d = (struct packed_rrset_data*)calloc(1, packed_rrset_sizeof(old)
955 + sigs*(sizeof(size_t) + sizeof(uint8_t*) + sizeof(time_t))
958 log_err("out of memory");
961 /* copy base values */
962 total = old->count + old->rrsig_count;
963 memcpy(d, old, sizeof(struct packed_rrset_data));
964 d->rrsig_count += sigs;
966 d->rr_len = (size_t*)((uint8_t*)d +
967 sizeof(struct packed_rrset_data));
969 memmove(d->rr_len, old->rr_len, total*sizeof(size_t));
970 j = d->count+d->rrsig_count-sigs;
971 for(i=0; i<sigold->count+sigold->rrsig_count; i++) {
972 if(rrsig_rdata_get_type_covered(sigold->rr_data[i],
973 sigold->rr_len[i]) == rr_type) {
974 d->rr_len[j] = sigold->rr_len[i];
978 packed_rrset_ptr_fixup(d);
980 /* copy old values into new array */
982 memmove(d->rr_ttl, old->rr_ttl, total*sizeof(time_t));
983 /* all the old rr pieces are allocated sequential, so we
984 * can copy them in one go */
985 memmove(d->rr_data[0], old->rr_data[0],
986 (old->rr_data[total-1] - old->rr_data[0]) +
987 old->rr_len[total-1]);
990 /* move over the rrsigs to the larger rrset*/
991 j = d->count+d->rrsig_count-sigs;
992 for(i=0; i<sigold->count+sigold->rrsig_count; i++) {
993 if(rrsig_rdata_get_type_covered(sigold->rr_data[i],
994 sigold->rr_len[i]) == rr_type) {
995 /* move this one over to location j */
996 d->rr_ttl[j] = sigold->rr_ttl[i];
997 memmove(d->rr_data[j], sigold->rr_data[i],
999 if(d->rr_ttl[j] < d->ttl)
1000 d->ttl = d->rr_ttl[j];
1005 /* put it in and deallocate the old rrset */
1009 /* now make rrsig set smaller */
1010 if(sigold->count+sigold->rrsig_count == sigs) {
1011 /* remove all sigs from rrsig, remove it entirely */
1012 domain_remove_rrset(node, LDNS_RR_TYPE_RRSIG);
1015 log_assert(packed_rrset_sizeof(sigold) > sigs*(sizeof(size_t) +
1016 sizeof(uint8_t*) + sizeof(time_t)) + sigsz);
1017 sigd = (struct packed_rrset_data*)calloc(1, packed_rrset_sizeof(sigold)
1018 - sigs*(sizeof(size_t) + sizeof(uint8_t*) + sizeof(time_t))
1021 /* no need to free up d, it has already been placed in the
1022 * node->rrset structure */
1023 log_err("out of memory");
1026 /* copy base values */
1027 memcpy(sigd, sigold, sizeof(struct packed_rrset_data));
1028 /* in sigd the RRSIGs are stored in the base of the RR, in count */
1029 sigd->count -= sigs;
1031 sigd->rr_len = (size_t*)((uint8_t*)sigd +
1032 sizeof(struct packed_rrset_data));
1034 for(i=0; i<sigold->count+sigold->rrsig_count; i++) {
1035 if(rrsig_rdata_get_type_covered(sigold->rr_data[i],
1036 sigold->rr_len[i]) != rr_type) {
1037 sigd->rr_len[j] = sigold->rr_len[i];
1041 packed_rrset_ptr_fixup(sigd);
1043 /* copy old values into new rrsig array */
1045 for(i=0; i<sigold->count+sigold->rrsig_count; i++) {
1046 if(rrsig_rdata_get_type_covered(sigold->rr_data[i],
1047 sigold->rr_len[i]) != rr_type) {
1048 /* move this one over to location j */
1049 sigd->rr_ttl[j] = sigold->rr_ttl[i];
1050 memmove(sigd->rr_data[j], sigold->rr_data[i],
1052 if(j==0) sigd->ttl = sigd->rr_ttl[j];
1054 if(sigd->rr_ttl[j] < sigd->ttl)
1055 sigd->ttl = sigd->rr_ttl[j];
1061 /* put it in and deallocate the old rrset */
1068 /** copy the rrsigs from the rrset to the rrsig rrset, because the rrset
1069 * is going to be deleted. reallocates the RRSIG rrset data. */
1071 rrsigs_copy_from_rrset_to_rrsigset(struct auth_rrset* rrset,
1072 struct auth_rrset* rrsigset)
1075 if(rrset->data->rrsig_count == 0)
1078 /* move them over one by one, because there might be duplicates,
1079 * duplicates are ignored */
1080 for(i=rrset->data->count;
1081 i<rrset->data->count+rrset->data->rrsig_count; i++) {
1082 uint8_t* rdata = rrset->data->rr_data[i];
1083 size_t rdatalen = rrset->data->rr_len[i];
1084 time_t rr_ttl = rrset->data->rr_ttl[i];
1086 if(rdata_duplicate(rrsigset->data, rdata, rdatalen)) {
1089 if(!rrset_add_rr(rrsigset, rr_ttl, rdata, rdatalen, 0))
1095 /** Add rr to node, ignores duplicate RRs,
1096 * rdata points to buffer with rdatalen octets, starts with 2bytelength. */
1098 az_domain_add_rr(struct auth_data* node, uint16_t rr_type, uint32_t rr_ttl,
1099 uint8_t* rdata, size_t rdatalen, int* duplicate)
1101 struct auth_rrset* rrset;
1102 /* packed rrsets have their rrsigs along with them, sort them out */
1103 if(rr_type == LDNS_RR_TYPE_RRSIG) {
1104 uint16_t ctype = rrsig_rdata_get_type_covered(rdata, rdatalen);
1105 if((rrset=az_domain_rrset(node, ctype))!= NULL) {
1106 /* a node of the correct type exists, add the RRSIG
1107 * to the rrset of the covered data type */
1108 if(rdata_duplicate(rrset->data, rdata, rdatalen)) {
1109 if(duplicate) *duplicate = 1;
1112 if(!rrset_add_rr(rrset, rr_ttl, rdata, rdatalen, 1))
1114 } else if((rrset=az_domain_rrset(node, rr_type))!= NULL) {
1115 /* add RRSIG to rrset of type RRSIG */
1116 if(rdata_duplicate(rrset->data, rdata, rdatalen)) {
1117 if(duplicate) *duplicate = 1;
1120 if(!rrset_add_rr(rrset, rr_ttl, rdata, rdatalen, 0))
1123 /* create rrset of type RRSIG */
1124 if(!rrset_create(node, rr_type, rr_ttl, rdata,
1129 /* normal RR type */
1130 if((rrset=az_domain_rrset(node, rr_type))!= NULL) {
1131 /* add data to existing node with data type */
1132 if(rdata_duplicate(rrset->data, rdata, rdatalen)) {
1133 if(duplicate) *duplicate = 1;
1136 if(!rrset_add_rr(rrset, rr_ttl, rdata, rdatalen, 0))
1139 struct auth_rrset* rrsig;
1140 /* create new node with data type */
1141 if(!(rrset=rrset_create(node, rr_type, rr_ttl, rdata,
1145 /* see if node of type RRSIG has signatures that
1146 * cover the data type, and move them over */
1147 /* and then make the RRSIG type smaller */
1148 if((rrsig=az_domain_rrset(node, LDNS_RR_TYPE_RRSIG))
1150 if(!rrset_moveover_rrsigs(node, rr_type,
1159 /** insert RR into zone, ignore duplicates */
1161 az_insert_rr(struct auth_zone* z, uint8_t* rr, size_t rr_len,
1162 size_t dname_len, int* duplicate)
1164 struct auth_data* node;
1165 uint8_t* dname = rr;
1166 uint16_t rr_type = sldns_wirerr_get_type(rr, rr_len, dname_len);
1167 uint16_t rr_class = sldns_wirerr_get_class(rr, rr_len, dname_len);
1168 uint32_t rr_ttl = sldns_wirerr_get_ttl(rr, rr_len, dname_len);
1169 size_t rdatalen = ((size_t)sldns_wirerr_get_rdatalen(rr, rr_len,
1171 /* rdata points to rdata prefixed with uint16 rdatalength */
1172 uint8_t* rdata = sldns_wirerr_get_rdatawl(rr, rr_len, dname_len);
1174 if(rr_class != z->dclass) {
1175 log_err("wrong class for RR");
1178 if(!(node=az_domain_find_or_create(z, dname, dname_len))) {
1179 log_err("cannot create domain");
1182 if(!az_domain_add_rr(node, rr_type, rr_ttl, rdata, rdatalen,
1184 log_err("cannot add RR to domain");
1188 if(!(rpz_insert_rr(z->rpz, z->name, z->namelen, dname,
1189 dname_len, rr_type, rr_class, rr_ttl, rdata, rdatalen,
1196 /** Remove rr from node, ignores nonexisting RRs,
1197 * rdata points to buffer with rdatalen octets, starts with 2bytelength. */
1199 az_domain_remove_rr(struct auth_data* node, uint16_t rr_type,
1200 uint8_t* rdata, size_t rdatalen, int* nonexist)
1202 struct auth_rrset* rrset;
1205 /* find the plain RR of the given type */
1206 if((rrset=az_domain_rrset(node, rr_type))!= NULL) {
1207 if(packed_rrset_find_rr(rrset->data, rdata, rdatalen, &index)) {
1208 if(rrset->data->count == 1 &&
1209 rrset->data->rrsig_count == 0) {
1210 /* last RR, delete the rrset */
1211 domain_remove_rrset(node, rr_type);
1212 } else if(rrset->data->count == 1 &&
1213 rrset->data->rrsig_count != 0) {
1214 /* move RRSIGs to the RRSIG rrset, or
1215 * this one becomes that RRset */
1216 struct auth_rrset* rrsigset = az_domain_rrset(
1217 node, LDNS_RR_TYPE_RRSIG);
1219 /* move left over rrsigs to the
1220 * existing rrset of type RRSIG */
1221 rrsigs_copy_from_rrset_to_rrsigset(
1223 /* and then delete the rrset */
1224 domain_remove_rrset(node, rr_type);
1226 /* no rrset of type RRSIG, this
1227 * set is now of that type,
1228 * just remove the rr */
1229 if(!rrset_remove_rr(rrset, index))
1231 rrset->type = LDNS_RR_TYPE_RRSIG;
1232 rrset->data->count = rrset->data->rrsig_count;
1233 rrset->data->rrsig_count = 0;
1236 /* remove the RR from the rrset */
1237 if(!rrset_remove_rr(rrset, index))
1242 /* rr not found in rrset */
1245 /* is it a type RRSIG, look under the covered type */
1246 if(rr_type == LDNS_RR_TYPE_RRSIG) {
1247 uint16_t ctype = rrsig_rdata_get_type_covered(rdata, rdatalen);
1248 if((rrset=az_domain_rrset(node, ctype))!= NULL) {
1249 if(az_rrset_find_rrsig(rrset->data, rdata, rdatalen,
1251 /* rrsig should have d->count > 0, be
1252 * over some rr of that type */
1253 /* remove the rrsig from the rrsigs list of the
1255 if(!rrset_remove_rr(rrset, index))
1260 /* also RRSIG not found */
1263 /* nothing found to delete */
1264 if(nonexist) *nonexist = 1;
1268 /** remove RR from zone, ignore if it does not exist, false on alloc failure*/
1270 az_remove_rr(struct auth_zone* z, uint8_t* rr, size_t rr_len,
1271 size_t dname_len, int* nonexist)
1273 struct auth_data* node;
1274 uint8_t* dname = rr;
1275 uint16_t rr_type = sldns_wirerr_get_type(rr, rr_len, dname_len);
1276 uint16_t rr_class = sldns_wirerr_get_class(rr, rr_len, dname_len);
1277 size_t rdatalen = ((size_t)sldns_wirerr_get_rdatalen(rr, rr_len,
1279 /* rdata points to rdata prefixed with uint16 rdatalength */
1280 uint8_t* rdata = sldns_wirerr_get_rdatawl(rr, rr_len, dname_len);
1282 if(rr_class != z->dclass) {
1283 log_err("wrong class for RR");
1284 /* really also a nonexisting entry, because no records
1285 * of that class in the zone, but return an error because
1286 * getting records of the wrong class is a failure of the
1290 node = az_find_name(z, dname, dname_len);
1292 /* node with that name does not exist */
1293 /* nonexisting entry, because no such name */
1297 if(!az_domain_remove_rr(node, rr_type, rdata, rdatalen, nonexist)) {
1298 /* alloc failure or so */
1301 /* remove the node, if necessary */
1302 /* an rrsets==NULL entry is not kept around for empty nonterminals,
1303 * and also parent nodes are not kept around, so we just delete it */
1304 if(node->rrsets == NULL) {
1305 (void)rbtree_delete(&z->data, node);
1306 auth_data_delete(node);
1309 rpz_remove_rr(z->rpz, z->name, z->namelen, dname, dname_len,
1310 rr_type, rr_class, rdata, rdatalen);
1315 /** decompress an RR into the buffer where it'll be an uncompressed RR
1316 * with uncompressed dname and uncompressed rdata (dnames) */
1318 decompress_rr_into_buffer(struct sldns_buffer* buf, uint8_t* pkt,
1319 size_t pktlen, uint8_t* dname, uint16_t rr_type, uint16_t rr_class,
1320 uint32_t rr_ttl, uint8_t* rr_data, uint16_t rr_rdlen)
1322 sldns_buffer pktbuf;
1323 size_t dname_len = 0;
1327 const sldns_rr_descriptor* desc;
1328 sldns_buffer_init_frm_data(&pktbuf, pkt, pktlen);
1329 sldns_buffer_clear(buf);
1331 /* decompress dname */
1332 sldns_buffer_set_position(&pktbuf,
1333 (size_t)(dname - sldns_buffer_current(&pktbuf)));
1334 dname_len = pkt_dname_len(&pktbuf);
1335 if(dname_len == 0) return 0; /* parse fail on dname */
1336 if(!sldns_buffer_available(buf, dname_len)) return 0;
1337 dname_pkt_copy(&pktbuf, sldns_buffer_current(buf), dname);
1338 sldns_buffer_skip(buf, (ssize_t)dname_len);
1340 /* type, class, ttl and rdatalength fields */
1341 if(!sldns_buffer_available(buf, 10)) return 0;
1342 sldns_buffer_write_u16(buf, rr_type);
1343 sldns_buffer_write_u16(buf, rr_class);
1344 sldns_buffer_write_u32(buf, rr_ttl);
1345 rdlenpos = sldns_buffer_position(buf);
1346 sldns_buffer_write_u16(buf, 0); /* rd length position */
1348 /* decompress rdata */
1349 desc = sldns_rr_descript(rr_type);
1352 if(rdlen > 0 && desc && desc->_dname_count > 0) {
1353 int count = (int)desc->_dname_count;
1355 size_t len; /* how much rdata to plain copy */
1356 size_t uncompressed_len, compressed_len;
1358 /* decompress dnames. */
1359 while(rdlen > 0 && count) {
1360 switch(desc->_wireformat[rdf]) {
1361 case LDNS_RDF_TYPE_DNAME:
1362 sldns_buffer_set_position(&pktbuf,
1364 sldns_buffer_begin(&pktbuf)));
1365 oldpos = sldns_buffer_position(&pktbuf);
1366 /* moves pktbuf to right after the
1367 * compressed dname, and returns uncompressed
1369 uncompressed_len = pkt_dname_len(&pktbuf);
1370 if(!uncompressed_len)
1371 return 0; /* parse error in dname */
1372 if(!sldns_buffer_available(buf,
1374 /* dname too long for buffer */
1376 dname_pkt_copy(&pktbuf,
1377 sldns_buffer_current(buf), rd);
1378 sldns_buffer_skip(buf, (ssize_t)uncompressed_len);
1379 compressed_len = sldns_buffer_position(
1381 rd += compressed_len;
1382 rdlen -= compressed_len;
1386 case LDNS_RDF_TYPE_STR:
1390 len = get_rdf_size(desc->_wireformat[rdf]);
1394 if(!sldns_buffer_available(buf, len))
1395 return 0; /* too long for buffer */
1396 sldns_buffer_write(buf, rd, len);
1403 /* copy remaining data */
1405 if(!sldns_buffer_available(buf, rdlen)) return 0;
1406 sldns_buffer_write(buf, rd, rdlen);
1408 /* fixup rdlength */
1409 sldns_buffer_write_u16_at(buf, rdlenpos,
1410 sldns_buffer_position(buf)-rdlenpos-2);
1411 sldns_buffer_flip(buf);
1415 /** insert RR into zone, from packet, decompress RR,
1416 * if duplicate is nonNULL set the flag but otherwise ignore duplicates */
1418 az_insert_rr_decompress(struct auth_zone* z, uint8_t* pkt, size_t pktlen,
1419 struct sldns_buffer* scratch_buffer, uint8_t* dname, uint16_t rr_type,
1420 uint16_t rr_class, uint32_t rr_ttl, uint8_t* rr_data,
1421 uint16_t rr_rdlen, int* duplicate)
1426 if(!decompress_rr_into_buffer(scratch_buffer, pkt, pktlen, dname,
1427 rr_type, rr_class, rr_ttl, rr_data, rr_rdlen)) {
1428 log_err("could not decompress RR");
1431 rr = sldns_buffer_begin(scratch_buffer);
1432 rr_len = sldns_buffer_limit(scratch_buffer);
1433 dname_len = dname_valid(rr, rr_len);
1434 return az_insert_rr(z, rr, rr_len, dname_len, duplicate);
1437 /** remove RR from zone, from packet, decompress RR,
1438 * if nonexist is nonNULL set the flag but otherwise ignore nonexisting entries*/
1440 az_remove_rr_decompress(struct auth_zone* z, uint8_t* pkt, size_t pktlen,
1441 struct sldns_buffer* scratch_buffer, uint8_t* dname, uint16_t rr_type,
1442 uint16_t rr_class, uint32_t rr_ttl, uint8_t* rr_data,
1443 uint16_t rr_rdlen, int* nonexist)
1448 if(!decompress_rr_into_buffer(scratch_buffer, pkt, pktlen, dname,
1449 rr_type, rr_class, rr_ttl, rr_data, rr_rdlen)) {
1450 log_err("could not decompress RR");
1453 rr = sldns_buffer_begin(scratch_buffer);
1454 rr_len = sldns_buffer_limit(scratch_buffer);
1455 dname_len = dname_valid(rr, rr_len);
1456 return az_remove_rr(z, rr, rr_len, dname_len, nonexist);
1461 * @param z: zone to read in.
1462 * @param in: file to read from (just opened).
1463 * @param rr: buffer to use for RRs, 64k.
1464 * passed so that recursive includes can use the same buffer and do
1465 * not grow the stack too much.
1466 * @param rrbuflen: sizeof rr buffer.
1467 * @param state: parse state with $ORIGIN, $TTL and 'prev-dname' and so on,
1468 * that is kept between includes.
1469 * The lineno is set at 1 and then increased by the function.
1470 * @param fname: file name.
1471 * @param depth: recursion depth for includes
1472 * @param cfg: config for chroot.
1473 * returns false on failure, has printed an error message
1476 az_parse_file(struct auth_zone* z, FILE* in, uint8_t* rr, size_t rrbuflen,
1477 struct sldns_file_parse_state* state, char* fname, int depth,
1478 struct config_file* cfg)
1480 size_t rr_len, dname_len;
1487 status = sldns_fp2wire_rr_buf(in, rr, &rr_len, &dname_len,
1489 if(status == LDNS_WIREPARSE_ERR_INCLUDE && rr_len == 0) {
1490 /* we have $INCLUDE or $something */
1491 if(strncmp((char*)rr, "$INCLUDE ", 9) == 0 ||
1492 strncmp((char*)rr, "$INCLUDE\t", 9) == 0) {
1494 int lineno_orig = state->lineno;
1495 char* incfile = (char*)rr + 8;
1496 if(depth > MAX_INCLUDE_DEPTH) {
1497 log_err("%s:%d max include depth"
1498 "exceeded", fname, state->lineno);
1502 while(*incfile == ' ' || *incfile == '\t')
1504 /* adjust for chroot on include file */
1505 if(cfg->chrootdir && cfg->chrootdir[0] &&
1506 strncmp(incfile, cfg->chrootdir,
1507 strlen(cfg->chrootdir)) == 0)
1508 incfile += strlen(cfg->chrootdir);
1509 incfile = strdup(incfile);
1511 log_err("malloc failure");
1514 verbose(VERB_ALGO, "opening $INCLUDE %s",
1516 inc = fopen(incfile, "r");
1518 log_err("%s:%d cannot open include "
1519 "file %s: %s", fname,
1520 lineno_orig, incfile,
1525 /* recurse read that file now */
1526 if(!az_parse_file(z, inc, rr, rrbuflen,
1527 state, incfile, depth+1, cfg)) {
1528 log_err("%s:%d cannot parse include "
1530 lineno_orig, incfile);
1536 verbose(VERB_ALGO, "done with $INCLUDE %s",
1539 state->lineno = lineno_orig;
1544 log_err("parse error %s %d:%d: %s", fname,
1545 state->lineno, LDNS_WIREPARSE_OFFSET(status),
1546 sldns_get_errorstr_parse(status));
1550 /* EMPTY line, TTL or ORIGIN */
1553 /* insert wirerr in rrbuf */
1554 if(!az_insert_rr(z, rr, rr_len, dname_len, NULL)) {
1556 sldns_wire2str_type_buf(sldns_wirerr_get_type(rr,
1557 rr_len, dname_len), buf, sizeof(buf));
1558 log_err("%s:%d cannot insert RR of type %s",
1559 fname, state->lineno, buf);
1567 auth_zone_read_zonefile(struct auth_zone* z, struct config_file* cfg)
1569 uint8_t rr[LDNS_RR_BUF_SIZE];
1570 struct sldns_file_parse_state state;
1573 if(!z || !z->zonefile || z->zonefile[0]==0)
1574 return 1; /* no file, or "", nothing to read */
1576 zfilename = z->zonefile;
1577 if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(zfilename,
1578 cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
1579 zfilename += strlen(cfg->chrootdir);
1580 if(verbosity >= VERB_ALGO) {
1582 dname_str(z->name, nm);
1583 verbose(VERB_ALGO, "read zonefile %s for %s", zfilename, nm);
1585 in = fopen(zfilename, "r");
1587 char* n = sldns_wire2str_dname(z->name, z->namelen);
1588 if(z->zone_is_slave && errno == ENOENT) {
1589 /* we fetch the zone contents later, no file yet */
1590 verbose(VERB_ALGO, "no zonefile %s for %s",
1591 zfilename, n?n:"error");
1595 log_err("cannot open zonefile %s for %s: %s",
1596 zfilename, n?n:"error", strerror(errno));
1601 /* clear the data tree */
1602 traverse_postorder(&z->data, auth_data_del, NULL);
1603 rbtree_init(&z->data, &auth_data_cmp);
1604 /* clear the RPZ policies */
1608 memset(&state, 0, sizeof(state));
1609 /* default TTL to 3600 */
1610 state.default_ttl = 3600;
1611 /* set $ORIGIN to the zone name */
1612 if(z->namelen <= sizeof(state.origin)) {
1613 memcpy(state.origin, z->name, z->namelen);
1614 state.origin_len = z->namelen;
1616 /* parse the (toplevel) file */
1617 if(!az_parse_file(z, in, rr, sizeof(rr), &state, zfilename, 0, cfg)) {
1618 char* n = sldns_wire2str_dname(z->name, z->namelen);
1619 log_err("error parsing zonefile %s for %s",
1620 zfilename, n?n:"error");
1628 rpz_finish_config(z->rpz);
1632 /** write buffer to file and check return codes */
1634 write_out(FILE* out, const char* str, size_t len)
1639 r = fwrite(str, 1, len, out);
1641 log_err("write failed: %s", strerror(errno));
1643 } else if(r < len) {
1644 log_err("write failed: too short (disk full?)");
1650 /** convert auth rr to string */
1652 auth_rr_to_string(uint8_t* nm, size_t nmlen, uint16_t tp, uint16_t cl,
1653 struct packed_rrset_data* data, size_t i, char* s, size_t buflen)
1656 size_t slen = buflen, datlen;
1658 if(i >= data->count) tp = LDNS_RR_TYPE_RRSIG;
1661 w += sldns_wire2str_dname_scan(&dat, &datlen, &s, &slen, NULL, 0, NULL);
1662 w += sldns_str_print(&s, &slen, "\t");
1663 w += sldns_str_print(&s, &slen, "%lu\t", (unsigned long)data->rr_ttl[i]);
1664 w += sldns_wire2str_class_print(&s, &slen, cl);
1665 w += sldns_str_print(&s, &slen, "\t");
1666 w += sldns_wire2str_type_print(&s, &slen, tp);
1667 w += sldns_str_print(&s, &slen, "\t");
1668 datlen = data->rr_len[i]-2;
1669 dat = data->rr_data[i]+2;
1670 w += sldns_wire2str_rdata_scan(&dat, &datlen, &s, &slen, tp, NULL, 0, NULL);
1672 if(tp == LDNS_RR_TYPE_DNSKEY) {
1673 w += sldns_str_print(&s, &slen, " ;{id = %u}",
1674 sldns_calc_keytag_raw(data->rr_data[i]+2,
1675 data->rr_len[i]-2));
1677 w += sldns_str_print(&s, &slen, "\n");
1679 if(w >= (int)buflen) {
1680 log_nametypeclass(NO_VERBOSE, "RR too long to print", nm, tp, cl);
1686 /** write rrset to file */
1688 auth_zone_write_rrset(struct auth_zone* z, struct auth_data* node,
1689 struct auth_rrset* r, FILE* out)
1691 size_t i, count = r->data->count + r->data->rrsig_count;
1692 char buf[LDNS_RR_BUF_SIZE];
1693 for(i=0; i<count; i++) {
1694 if(!auth_rr_to_string(node->name, node->namelen, r->type,
1695 z->dclass, r->data, i, buf, sizeof(buf))) {
1696 verbose(VERB_ALGO, "failed to rr2str rr %d", (int)i);
1699 if(!write_out(out, buf, strlen(buf)))
1705 /** write domain to file */
1707 auth_zone_write_domain(struct auth_zone* z, struct auth_data* n, FILE* out)
1709 struct auth_rrset* r;
1710 /* if this is zone apex, write SOA first */
1711 if(z->namelen == n->namelen) {
1712 struct auth_rrset* soa = az_domain_rrset(n, LDNS_RR_TYPE_SOA);
1714 if(!auth_zone_write_rrset(z, n, soa, out))
1718 /* write all the RRsets for this domain */
1719 for(r = n->rrsets; r; r = r->next) {
1720 if(z->namelen == n->namelen &&
1721 r->type == LDNS_RR_TYPE_SOA)
1722 continue; /* skip SOA here */
1723 if(!auth_zone_write_rrset(z, n, r, out))
1729 int auth_zone_write_file(struct auth_zone* z, const char* fname)
1732 struct auth_data* n;
1733 out = fopen(fname, "w");
1735 log_err("could not open %s: %s", fname, strerror(errno));
1738 RBTREE_FOR(n, struct auth_data*, &z->data) {
1739 if(!auth_zone_write_domain(z, n, out)) {
1740 log_err("could not write domain to %s", fname);
1749 /** offline verify for zonemd, while reading a zone file to immediately
1750 * spot bad hashes in zonefile as they are read.
1751 * Creates temp buffers, but uses anchors and validation environment
1752 * from the module_env. */
1754 zonemd_offline_verify(struct auth_zone* z, struct module_env* env_for_val,
1755 struct module_stack* mods)
1757 struct module_env env;
1759 if(!z->zonemd_check)
1762 env.scratch_buffer = sldns_buffer_new(env.cfg->msg_buffer_size);
1763 if(!env.scratch_buffer) {
1764 log_err("out of memory");
1767 env.scratch = regional_create();
1773 log_err("out of memory");
1776 auth_zone_verify_zonemd(z, &env, mods, NULL, 1, 0);
1779 /* clean up and exit */
1780 sldns_buffer_free(env.scratch_buffer);
1781 regional_destroy(env.scratch);
1784 /** read all auth zones from file (if they have) */
1786 auth_zones_read_zones(struct auth_zones* az, struct config_file* cfg,
1787 struct module_env* env, struct module_stack* mods)
1789 struct auth_zone* z;
1790 lock_rw_wrlock(&az->lock);
1791 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
1792 lock_rw_wrlock(&z->lock);
1793 if(!auth_zone_read_zonefile(z, cfg)) {
1794 lock_rw_unlock(&z->lock);
1795 lock_rw_unlock(&az->lock);
1798 if(z->zonefile && z->zonefile[0]!=0 && env)
1799 zonemd_offline_verify(z, env, mods);
1800 lock_rw_unlock(&z->lock);
1802 lock_rw_unlock(&az->lock);
1806 /** fetch the content of a ZONEMD RR from the rdata */
1807 static int zonemd_fetch_parameters(struct auth_rrset* zonemd_rrset, size_t i,
1808 uint32_t* serial, int* scheme, int* hashalgo, uint8_t** hash,
1813 if(i >= zonemd_rrset->data->count)
1815 rr_len = zonemd_rrset->data->rr_len[i];
1816 if(rr_len < 2+4+1+1)
1817 return 0; /* too short, for rdlen+serial+scheme+algo */
1818 rdata = zonemd_rrset->data->rr_data[i];
1819 *serial = sldns_read_uint32(rdata+2);
1821 *hashalgo = rdata[7];
1822 *hashlen = rr_len - 8;
1825 else *hash = rdata+8;
1830 * See if the ZONEMD scheme, hash occurs more than once.
1831 * @param zonemd_rrset: the zonemd rrset to check with the RRs in it.
1832 * @param index: index of the original, this is allowed to have that
1833 * scheme and hashalgo, but other RRs should not have it.
1834 * @param scheme: the scheme to check for.
1835 * @param hashalgo: the hash algorithm to check for.
1836 * @return true if it occurs more than once.
1838 static int zonemd_is_duplicate_scheme_hash(struct auth_rrset* zonemd_rrset,
1839 size_t index, int scheme, int hashalgo)
1842 for(j=0; j<zonemd_rrset->data->count; j++) {
1843 uint32_t serial2 = 0;
1844 int scheme2 = 0, hashalgo2 = 0;
1845 uint8_t* hash2 = NULL;
1846 size_t hashlen2 = 0;
1848 /* this is the original */
1851 if(!zonemd_fetch_parameters(zonemd_rrset, j, &serial2,
1852 &scheme2, &hashalgo2, &hash2, &hashlen2)) {
1853 /* malformed, skip it */
1856 if(scheme == scheme2 && hashalgo == hashalgo2) {
1857 /* duplicate scheme, hash */
1858 verbose(VERB_ALGO, "zonemd duplicate for scheme %d "
1859 "and hash %d", scheme, hashalgo);
1867 * Check ZONEMDs if present for the auth zone. Depending on config
1868 * it can warn or fail on that. Checks the hash of the ZONEMD.
1869 * @param z: auth zone to check for.
1870 * caller must hold lock on zone.
1871 * @param env: module env for temp buffers.
1872 * @param reason: returned on failure.
1873 * @return false on failure, true if hash checks out.
1875 static int auth_zone_zonemd_check_hash(struct auth_zone* z,
1876 struct module_env* env, char** reason)
1878 /* loop over ZONEMDs and see which one is valid. if not print
1879 * failure (depending on config) */
1880 struct auth_data* apex;
1881 struct auth_rrset* zonemd_rrset;
1883 struct regional* region = NULL;
1884 struct sldns_buffer* buf = NULL;
1885 uint32_t soa_serial = 0;
1886 char* unsupported_reason = NULL;
1887 int only_unsupported = 1;
1888 region = env->scratch;
1889 regional_free_all(region);
1890 buf = env->scratch_buffer;
1891 if(!auth_zone_get_serial(z, &soa_serial)) {
1892 *reason = "zone has no SOA serial";
1896 apex = az_find_name(z, z->name, z->namelen);
1898 *reason = "zone has no apex";
1901 zonemd_rrset = az_domain_rrset(apex, LDNS_RR_TYPE_ZONEMD);
1902 if(!zonemd_rrset || zonemd_rrset->data->count==0) {
1903 *reason = "zone has no ZONEMD";
1904 return 0; /* no RRset or no RRs in rrset */
1907 /* we have a ZONEMD, check if it is correct */
1908 for(i=0; i<zonemd_rrset->data->count; i++) {
1909 uint32_t serial = 0;
1910 int scheme = 0, hashalgo = 0;
1911 uint8_t* hash = NULL;
1913 if(!zonemd_fetch_parameters(zonemd_rrset, i, &serial, &scheme,
1914 &hashalgo, &hash, &hashlen)) {
1916 *reason = "ZONEMD rdata malformed";
1917 only_unsupported = 0;
1920 /* check for duplicates */
1921 if(zonemd_is_duplicate_scheme_hash(zonemd_rrset, i, scheme,
1923 /* duplicate hash of the same scheme,hash
1924 * is not allowed. */
1925 *reason = "ZONEMD RRSet contains more than one RR "
1926 "with the same scheme and hash algorithm";
1927 only_unsupported = 0;
1930 regional_free_all(region);
1931 if(serial != soa_serial) {
1932 *reason = "ZONEMD serial is wrong";
1933 only_unsupported = 0;
1937 if(auth_zone_generate_zonemd_check(z, scheme, hashalgo,
1938 hash, hashlen, region, buf, reason)) {
1941 if(!unsupported_reason)
1942 unsupported_reason = *reason;
1943 /* continue to check for valid ZONEMD */
1944 if(verbosity >= VERB_ALGO) {
1946 dname_str(z->name, zstr);
1947 verbose(VERB_ALGO, "auth-zone %s ZONEMD %d %d is unsupported: %s", zstr, (int)scheme, (int)hashalgo, *reason);
1952 if(verbosity >= VERB_ALGO) {
1954 dname_str(z->name, zstr);
1956 verbose(VERB_ALGO, "auth-zone %s ZONEMD hash is correct", zstr);
1960 only_unsupported = 0;
1963 /* have we seen no failures but only unsupported algo,
1964 * and one unsupported algorithm, or more. */
1965 if(only_unsupported && unsupported_reason) {
1966 /* only unsupported algorithms, with valid serial, not
1967 * malformed. Did not see supported algorithms, failed or
1968 * successful ones. */
1969 *reason = unsupported_reason;
1972 /* fail, we may have reason */
1974 *reason = "no ZONEMD records found";
1975 if(verbosity >= VERB_ALGO) {
1977 dname_str(z->name, zstr);
1978 verbose(VERB_ALGO, "auth-zone %s ZONEMD failed: %s", zstr, *reason);
1983 /** find the apex SOA RRset, if it exists */
1984 struct auth_rrset* auth_zone_get_soa_rrset(struct auth_zone* z)
1986 struct auth_data* apex;
1987 struct auth_rrset* soa;
1988 apex = az_find_name(z, z->name, z->namelen);
1989 if(!apex) return NULL;
1990 soa = az_domain_rrset(apex, LDNS_RR_TYPE_SOA);
1994 /** find serial number of zone or false if none */
1996 auth_zone_get_serial(struct auth_zone* z, uint32_t* serial)
1998 struct auth_data* apex;
1999 struct auth_rrset* soa;
2000 struct packed_rrset_data* d;
2001 apex = az_find_name(z, z->name, z->namelen);
2003 soa = az_domain_rrset(apex, LDNS_RR_TYPE_SOA);
2004 if(!soa || soa->data->count==0)
2005 return 0; /* no RRset or no RRs in rrset */
2006 if(soa->data->rr_len[0] < 2+4*5) return 0; /* SOA too short */
2008 *serial = sldns_read_uint32(d->rr_data[0]+(d->rr_len[0]-20));
2012 /** Find auth_zone SOA and populate the values in xfr(soa values). */
2014 xfr_find_soa(struct auth_zone* z, struct auth_xfer* xfr)
2016 struct auth_data* apex;
2017 struct auth_rrset* soa;
2018 struct packed_rrset_data* d;
2019 apex = az_find_name(z, z->name, z->namelen);
2021 soa = az_domain_rrset(apex, LDNS_RR_TYPE_SOA);
2022 if(!soa || soa->data->count==0)
2023 return 0; /* no RRset or no RRs in rrset */
2024 if(soa->data->rr_len[0] < 2+4*5) return 0; /* SOA too short */
2025 /* SOA record ends with serial, refresh, retry, expiry, minimum,
2026 * as 4 byte fields */
2029 xfr->serial = sldns_read_uint32(d->rr_data[0]+(d->rr_len[0]-20));
2030 xfr->refresh = sldns_read_uint32(d->rr_data[0]+(d->rr_len[0]-16));
2031 xfr->retry = sldns_read_uint32(d->rr_data[0]+(d->rr_len[0]-12));
2032 xfr->expiry = sldns_read_uint32(d->rr_data[0]+(d->rr_len[0]-8));
2033 /* soa minimum at d->rr_len[0]-4 */
2038 * Setup auth_xfer zone
2039 * This populates the have_zone, soa values, and so on times.
2040 * Doesn't do network traffic yet, can set option flags.
2041 * @param z: locked by caller, and modified for setup
2042 * @param x: locked by caller, and modified.
2043 * @return false on failure.
2046 auth_xfer_setup(struct auth_zone* z, struct auth_xfer* x)
2048 /* for a zone without zone transfers, x==NULL, so skip them,
2049 * i.e. the zone config is fixed with no masters or urls */
2050 if(!z || !x) return 1;
2051 if(!xfr_find_soa(z, x)) {
2054 /* nothing for probe, nextprobe and transfer tasks */
2060 * @param az: auth zones structure
2061 * @return false on failure.
2064 auth_zones_setup_zones(struct auth_zones* az)
2066 struct auth_zone* z;
2067 struct auth_xfer* x;
2068 lock_rw_wrlock(&az->lock);
2069 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
2070 lock_rw_wrlock(&z->lock);
2071 x = auth_xfer_find(az, z->name, z->namelen, z->dclass);
2073 lock_basic_lock(&x->lock);
2075 if(!auth_xfer_setup(z, x)) {
2077 lock_basic_unlock(&x->lock);
2079 lock_rw_unlock(&z->lock);
2080 lock_rw_unlock(&az->lock);
2084 lock_basic_unlock(&x->lock);
2086 lock_rw_unlock(&z->lock);
2088 lock_rw_unlock(&az->lock);
2092 /** set config items and create zones */
2094 auth_zones_cfg(struct auth_zones* az, struct config_auth* c)
2096 struct auth_zone* z;
2097 struct auth_xfer* x = NULL;
2101 /* if the rpz lock is needed, grab it before the other
2102 * locks to avoid a lock dependency cycle */
2103 lock_rw_wrlock(&az->rpz_lock);
2105 lock_rw_wrlock(&az->lock);
2106 if(!(z=auth_zones_find_or_add_zone(az, c->name))) {
2107 lock_rw_unlock(&az->lock);
2109 lock_rw_unlock(&az->rpz_lock);
2113 if(c->masters || c->urls) {
2114 if(!(x=auth_zones_find_or_add_xfer(az, z))) {
2115 lock_rw_unlock(&az->lock);
2116 lock_rw_unlock(&z->lock);
2118 lock_rw_unlock(&az->rpz_lock);
2123 if(c->for_downstream)
2124 az->have_downstream = 1;
2125 lock_rw_unlock(&az->lock);
2128 z->zone_deleted = 0;
2129 if(!auth_zone_set_zonefile(z, c->zonefile)) {
2131 lock_basic_unlock(&x->lock);
2133 lock_rw_unlock(&z->lock);
2135 lock_rw_unlock(&az->rpz_lock);
2139 z->for_downstream = c->for_downstream;
2140 z->for_upstream = c->for_upstream;
2141 z->fallback_enabled = c->fallback_enabled;
2142 z->zonemd_check = c->zonemd_check;
2143 z->zonemd_reject_absence = c->zonemd_reject_absence;
2144 if(c->isrpz && !z->rpz){
2145 if(!(z->rpz = rpz_create(c))){
2146 fatal_exit("Could not setup RPZ zones");
2149 lock_protect(&z->lock, &z->rpz->local_zones, sizeof(*z->rpz));
2150 /* the az->rpz_lock is locked above */
2151 z->rpz_az_next = az->rpz_first;
2153 az->rpz_first->rpz_az_prev = z;
2157 lock_rw_unlock(&az->rpz_lock);
2162 z->zone_is_slave = 1;
2163 /* set options on xfer zone */
2164 if(!xfer_set_masters(&x->task_probe->masters, c, 0)) {
2165 lock_basic_unlock(&x->lock);
2166 lock_rw_unlock(&z->lock);
2169 if(!xfer_set_masters(&x->task_transfer->masters, c, 1)) {
2170 lock_basic_unlock(&x->lock);
2171 lock_rw_unlock(&z->lock);
2174 lock_basic_unlock(&x->lock);
2177 lock_rw_unlock(&z->lock);
2181 /** set all auth zones deleted, then in auth_zones_cfg, it marks them
2182 * as nondeleted (if they are still in the config), and then later
2183 * we can find deleted zones */
2185 az_setall_deleted(struct auth_zones* az)
2187 struct auth_zone* z;
2188 lock_rw_wrlock(&az->lock);
2189 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
2190 lock_rw_wrlock(&z->lock);
2191 z->zone_deleted = 1;
2192 lock_rw_unlock(&z->lock);
2194 lock_rw_unlock(&az->lock);
2197 /** find zones that are marked deleted and delete them.
2198 * This is called from apply_cfg, and there are no threads and no
2199 * workers, so the xfr can just be deleted. */
2201 az_delete_deleted_zones(struct auth_zones* az)
2203 struct auth_zone* z;
2204 struct auth_zone* delete_list = NULL, *next;
2205 struct auth_xfer* xfr;
2206 lock_rw_wrlock(&az->lock);
2207 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
2208 lock_rw_wrlock(&z->lock);
2209 if(z->zone_deleted) {
2210 /* we cannot alter the rbtree right now, but
2211 * we can put it on a linked list and then
2213 z->delete_next = delete_list;
2216 lock_rw_unlock(&z->lock);
2218 /* now we are out of the tree loop and we can loop and delete
2222 next = z->delete_next;
2223 xfr = auth_xfer_find(az, z->name, z->namelen, z->dclass);
2225 (void)rbtree_delete(&az->xtree, &xfr->node);
2226 auth_xfer_delete(xfr);
2228 (void)rbtree_delete(&az->ztree, &z->node);
2229 auth_zone_delete(z, az);
2232 lock_rw_unlock(&az->lock);
2235 int auth_zones_apply_cfg(struct auth_zones* az, struct config_file* cfg,
2236 int setup, int* is_rpz, struct module_env* env,
2237 struct module_stack* mods)
2239 struct config_auth* p;
2240 az_setall_deleted(az);
2241 for(p = cfg->auths; p; p = p->next) {
2242 if(!p->name || p->name[0] == 0) {
2243 log_warn("auth-zone without a name, skipped");
2246 *is_rpz = (*is_rpz || p->isrpz);
2247 if(!auth_zones_cfg(az, p)) {
2248 log_err("cannot config auth zone %s", p->name);
2252 az_delete_deleted_zones(az);
2253 if(!auth_zones_read_zones(az, cfg, env, mods))
2256 if(!auth_zones_setup_zones(az))
2263 * @param at: transfer structure with chunks list. The chunks and their
2267 auth_chunks_delete(struct auth_transfer* at)
2269 if(at->chunks_first) {
2270 struct auth_chunk* c, *cn;
2271 c = at->chunks_first;
2279 at->chunks_first = NULL;
2280 at->chunks_last = NULL;
2283 /** free master addr list */
2285 auth_free_master_addrs(struct auth_addr* list)
2287 struct auth_addr *n;
2295 /** free the masters list */
2297 auth_free_masters(struct auth_master* list)
2299 struct auth_master* n;
2302 auth_free_master_addrs(list->list);
2310 /** delete auth xfer structure
2311 * @param xfr: delete this xfer and its tasks.
2314 auth_xfer_delete(struct auth_xfer* xfr)
2317 lock_basic_destroy(&xfr->lock);
2319 if(xfr->task_nextprobe) {
2320 comm_timer_delete(xfr->task_nextprobe->timer);
2321 free(xfr->task_nextprobe);
2323 if(xfr->task_probe) {
2324 auth_free_masters(xfr->task_probe->masters);
2325 comm_point_delete(xfr->task_probe->cp);
2326 comm_timer_delete(xfr->task_probe->timer);
2327 free(xfr->task_probe);
2329 if(xfr->task_transfer) {
2330 auth_free_masters(xfr->task_transfer->masters);
2331 comm_point_delete(xfr->task_transfer->cp);
2332 comm_timer_delete(xfr->task_transfer->timer);
2333 if(xfr->task_transfer->chunks_first) {
2334 auth_chunks_delete(xfr->task_transfer);
2336 free(xfr->task_transfer);
2338 auth_free_masters(xfr->allow_notify_list);
2342 /** helper traverse to delete zones */
2344 auth_zone_del(rbnode_type* n, void* ATTR_UNUSED(arg))
2346 struct auth_zone* z = (struct auth_zone*)n->key;
2347 auth_zone_delete(z, NULL);
2350 /** helper traverse to delete xfer zones */
2352 auth_xfer_del(rbnode_type* n, void* ATTR_UNUSED(arg))
2354 struct auth_xfer* z = (struct auth_xfer*)n->key;
2355 auth_xfer_delete(z);
2358 void auth_zones_delete(struct auth_zones* az)
2361 lock_rw_destroy(&az->lock);
2362 lock_rw_destroy(&az->rpz_lock);
2363 traverse_postorder(&az->ztree, auth_zone_del, NULL);
2364 traverse_postorder(&az->xtree, auth_xfer_del, NULL);
2368 /** true if domain has only nsec3 */
2370 domain_has_only_nsec3(struct auth_data* n)
2372 struct auth_rrset* rrset = n->rrsets;
2375 if(rrset->type == LDNS_RR_TYPE_NSEC3) {
2377 } else if(rrset->type != LDNS_RR_TYPE_RRSIG) {
2380 rrset = rrset->next;
2385 /** see if the domain has a wildcard child '*.domain' */
2386 static struct auth_data*
2387 az_find_wildcard_domain(struct auth_zone* z, uint8_t* nm, size_t nmlen)
2389 uint8_t wc[LDNS_MAX_DOMAINLEN];
2390 if(nmlen+2 > sizeof(wc))
2391 return NULL; /* result would be too long */
2392 wc[0] = 1; /* length of wildcard label */
2393 wc[1] = (uint8_t)'*'; /* wildcard label */
2394 memmove(wc+2, nm, nmlen);
2395 return az_find_name(z, wc, nmlen+2);
2398 /** find wildcard between qname and cename */
2399 static struct auth_data*
2400 az_find_wildcard(struct auth_zone* z, struct query_info* qinfo,
2401 struct auth_data* ce)
2403 uint8_t* nm = qinfo->qname;
2404 size_t nmlen = qinfo->qname_len;
2405 struct auth_data* node;
2406 if(!dname_subdomain_c(nm, z->name))
2407 return NULL; /* out of zone */
2408 while((node=az_find_wildcard_domain(z, nm, nmlen))==NULL) {
2409 /* see if we can go up to find the wildcard */
2410 if(nmlen == z->namelen)
2411 return NULL; /* top of zone reached */
2412 if(ce && nmlen == ce->namelen)
2413 return NULL; /* ce reached */
2414 if(dname_is_root(nm))
2415 return NULL; /* cannot go up */
2416 dname_remove_label(&nm, &nmlen);
2421 /** domain is not exact, find first candidate ce (name that matches
2422 * a part of qname) in tree */
2423 static struct auth_data*
2424 az_find_candidate_ce(struct auth_zone* z, struct query_info* qinfo,
2425 struct auth_data* n)
2430 nm = dname_get_shared_topdomain(qinfo->qname, n->name);
2434 dname_count_size_labels(nm, &nmlen);
2435 n = az_find_name(z, nm, nmlen);
2436 /* delete labels and go up on name */
2438 if(dname_is_root(nm))
2439 return NULL; /* cannot go up */
2440 dname_remove_label(&nm, &nmlen);
2441 n = az_find_name(z, nm, nmlen);
2446 /** go up the auth tree to next existing name. */
2447 static struct auth_data*
2448 az_domain_go_up(struct auth_zone* z, struct auth_data* n)
2450 uint8_t* nm = n->name;
2451 size_t nmlen = n->namelen;
2452 while(!dname_is_root(nm)) {
2453 dname_remove_label(&nm, &nmlen);
2454 if((n=az_find_name(z, nm, nmlen)) != NULL)
2460 /** Find the closest encloser, an name that exists and is above the
2462 * return true if the node (param node) is existing, nonobscured and
2463 * can be used to generate answers from. It is then also node_exact.
2464 * returns false if the node is not good enough (or it wasn't node_exact)
2465 * in this case the ce can be filled.
2466 * if ce is NULL, no ce exists, and likely the zone is completely empty,
2467 * not even with a zone apex.
2468 * if ce is nonNULL it is the closest enclosing upper name (that exists
2469 * itself for answer purposes). That name may have DNAME, NS or wildcard
2470 * rrset is the closest DNAME or NS rrset that was found.
2473 az_find_ce(struct auth_zone* z, struct query_info* qinfo,
2474 struct auth_data* node, int node_exact, struct auth_data** ce,
2475 struct auth_rrset** rrset)
2477 struct auth_data* n = node;
2481 /* if not exact, lookup closest exact match */
2482 n = az_find_candidate_ce(z, qinfo, n);
2484 /* if exact, the node itself is the first candidate ce */
2488 /* no direct answer from nsec3-only domains */
2489 if(n && domain_has_only_nsec3(n)) {
2494 /* with exact matches, walk up the labels until we find the
2495 * delegation, or DNAME or zone end */
2497 /* see if the current candidate has issues */
2498 /* not zone apex and has type NS */
2499 if(n->namelen != z->namelen &&
2500 (*rrset=az_domain_rrset(n, LDNS_RR_TYPE_NS)) &&
2501 /* delegate here, but DS at exact the dp has notype */
2502 (qinfo->qtype != LDNS_RR_TYPE_DS ||
2503 n->namelen != qinfo->qname_len)) {
2505 /* this is ce and the lowernode is nonexisting */
2509 /* not equal to qname and has type DNAME */
2510 if(n->namelen != qinfo->qname_len &&
2511 (*rrset=az_domain_rrset(n, LDNS_RR_TYPE_DNAME))) {
2512 /* this is ce and the lowernode is nonexisting */
2517 if(*ce == NULL && !domain_has_only_nsec3(n)) {
2518 /* if not found yet, this exact name must be
2519 * our lowest match (but not nsec3onlydomain) */
2523 /* walk up the tree by removing labels from name and lookup */
2524 n = az_domain_go_up(z, n);
2526 /* found no problems, if it was an exact node, it is fine to use */
2530 /** add additional A/AAAA from domain names in rrset rdata (+offset)
2531 * offset is number of bytes in rdata where the dname is located. */
2533 az_add_additionals_from(struct auth_zone* z, struct regional* region,
2534 struct dns_msg* msg, struct auth_rrset* rrset, size_t offset)
2536 struct packed_rrset_data* d = rrset->data;
2539 for(i=0; i<d->count; i++) {
2541 struct auth_data* domain;
2542 struct auth_rrset* ref;
2543 if(d->rr_len[i] < 2+offset)
2544 continue; /* too short */
2545 if(!(dlen = dname_valid(d->rr_data[i]+2+offset,
2546 d->rr_len[i]-2-offset)))
2547 continue; /* malformed */
2548 domain = az_find_name(z, d->rr_data[i]+2+offset, dlen);
2551 if((ref=az_domain_rrset(domain, LDNS_RR_TYPE_A)) != NULL) {
2552 if(!msg_add_rrset_ar(z, region, msg, domain, ref))
2555 if((ref=az_domain_rrset(domain, LDNS_RR_TYPE_AAAA)) != NULL) {
2556 if(!msg_add_rrset_ar(z, region, msg, domain, ref))
2563 /** add negative SOA record (with negative TTL) */
2565 az_add_negative_soa(struct auth_zone* z, struct regional* region,
2566 struct dns_msg* msg)
2570 struct packed_rrset_data* d;
2571 struct auth_rrset* soa;
2572 struct auth_data* apex = az_find_name(z, z->name, z->namelen);
2574 soa = az_domain_rrset(apex, LDNS_RR_TYPE_SOA);
2576 /* must be first to put in message; we want to fix the TTL with
2577 * one RRset here, otherwise we'd need to loop over the RRs to get
2578 * the resulting lower TTL */
2579 log_assert(msg->rep->rrset_count == 0);
2580 if(!msg_add_rrset_ns(z, region, msg, apex, soa)) return 0;
2582 d = (struct packed_rrset_data*)msg->rep->rrsets[msg->rep->rrset_count-1]->entry.data;
2583 /* last 4 bytes are minimum ttl in network format */
2584 if(d->count == 0) return 0;
2585 if(d->rr_len[0] < 2+4) return 0;
2586 minimum = (time_t)sldns_read_uint32(d->rr_data[0]+(d->rr_len[0]-4));
2587 minimum = d->ttl<minimum?d->ttl:minimum;
2589 for(i=0; i < d->count + d->rrsig_count; i++)
2590 d->rr_ttl[i] = minimum;
2591 msg->rep->ttl = get_rrset_ttl(msg->rep->rrsets[0]);
2592 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
2593 msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
2597 /** See if the query goes to empty nonterminal (that has no auth_data,
2598 * but there are nodes underneath. We already checked that there are
2599 * not NS, or DNAME above, so that we only need to check if some node
2600 * exists below (with nonempty rr list), return true if emptynonterminal */
2602 az_empty_nonterminal(struct auth_zone* z, struct query_info* qinfo,
2603 struct auth_data* node)
2605 struct auth_data* next;
2607 /* no smaller was found, use first (smallest) node as the
2609 next = (struct auth_data*)rbtree_first(&z->data);
2611 next = (struct auth_data*)rbtree_next(&node->node);
2613 while(next && (rbnode_type*)next != RBTREE_NULL && next->rrsets == NULL) {
2614 /* the next name has empty rrsets, is an empty nonterminal
2615 * itself, see if there exists something below it */
2616 next = (struct auth_data*)rbtree_next(&node->node);
2618 if((rbnode_type*)next == RBTREE_NULL || !next) {
2619 /* there is no next node, so something below it cannot
2623 /* a next node exists, if there was something below the query,
2624 * this node has to be it. See if it is below the query name */
2625 if(dname_strict_subdomain_c(next->name, qinfo->qname))
2630 /** create synth cname target name in buffer, or fail if too long */
2632 synth_cname_buf(uint8_t* qname, size_t qname_len, size_t dname_len,
2633 uint8_t* dtarg, size_t dtarglen, uint8_t* buf, size_t buflen)
2635 size_t newlen = qname_len + dtarglen - dname_len;
2636 if(newlen > buflen) {
2637 /* YXDOMAIN error */
2640 /* new name is concatenation of qname front (without DNAME owner)
2641 * and DNAME target name */
2642 memcpy(buf, qname, qname_len-dname_len);
2643 memmove(buf+(qname_len-dname_len), dtarg, dtarglen);
2647 /** create synthetic CNAME rrset for in a DNAME answer in region,
2648 * false on alloc failure, cname==NULL when name too long. */
2650 create_synth_cname(uint8_t* qname, size_t qname_len, struct regional* region,
2651 struct auth_data* node, struct auth_rrset* dname, uint16_t dclass,
2652 struct ub_packed_rrset_key** cname)
2654 uint8_t buf[LDNS_MAX_DOMAINLEN];
2656 size_t dtarglen, newlen;
2657 struct packed_rrset_data* d;
2659 /* get DNAME target name */
2660 if(dname->data->count < 1) return 0;
2661 if(dname->data->rr_len[0] < 3) return 0; /* at least rdatalen +1 */
2662 dtarg = dname->data->rr_data[0]+2;
2663 dtarglen = dname->data->rr_len[0]-2;
2664 if(sldns_read_uint16(dname->data->rr_data[0]) != dtarglen)
2665 return 0; /* rdatalen in DNAME rdata is malformed */
2666 if(dname_valid(dtarg, dtarglen) != dtarglen)
2667 return 0; /* DNAME RR has malformed rdata */
2669 return 0; /* too short */
2670 if(qname_len <= node->namelen)
2671 return 0; /* qname too short for dname removal */
2673 /* synthesize a CNAME */
2674 newlen = synth_cname_buf(qname, qname_len, node->namelen,
2675 dtarg, dtarglen, buf, sizeof(buf));
2677 /* YXDOMAIN error */
2681 *cname = (struct ub_packed_rrset_key*)regional_alloc(region,
2682 sizeof(struct ub_packed_rrset_key));
2684 return 0; /* out of memory */
2685 memset(&(*cname)->entry, 0, sizeof((*cname)->entry));
2686 (*cname)->entry.key = (*cname);
2687 (*cname)->rk.type = htons(LDNS_RR_TYPE_CNAME);
2688 (*cname)->rk.rrset_class = htons(dclass);
2689 (*cname)->rk.flags = 0;
2690 (*cname)->rk.dname = regional_alloc_init(region, qname, qname_len);
2691 if(!(*cname)->rk.dname)
2692 return 0; /* out of memory */
2693 (*cname)->rk.dname_len = qname_len;
2694 (*cname)->entry.hash = rrset_key_hash(&(*cname)->rk);
2695 d = (struct packed_rrset_data*)regional_alloc_zero(region,
2696 sizeof(struct packed_rrset_data) + sizeof(size_t) +
2697 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)
2700 return 0; /* out of memory */
2701 (*cname)->entry.data = d;
2702 d->ttl = 0; /* 0 for synthesized CNAME TTL */
2705 d->trust = rrset_trust_ans_noAA;
2706 d->rr_len = (size_t*)((uint8_t*)d +
2707 sizeof(struct packed_rrset_data));
2708 d->rr_len[0] = newlen + sizeof(uint16_t);
2709 packed_rrset_ptr_fixup(d);
2710 d->rr_ttl[0] = d->ttl;
2711 sldns_write_uint16(d->rr_data[0], newlen);
2712 memmove(d->rr_data[0] + sizeof(uint16_t), buf, newlen);
2716 /** add a synthesized CNAME to the answer section */
2718 add_synth_cname(struct auth_zone* z, uint8_t* qname, size_t qname_len,
2719 struct regional* region, struct dns_msg* msg, struct auth_data* dname,
2720 struct auth_rrset* rrset)
2722 struct ub_packed_rrset_key* cname;
2723 /* synthesize a CNAME */
2724 if(!create_synth_cname(qname, qname_len, region, dname, rrset,
2725 z->dclass, &cname)) {
2730 /* cname cannot be create because of YXDOMAIN */
2731 msg->rep->flags |= LDNS_RCODE_YXDOMAIN;
2734 /* add cname to message */
2735 if(!msg_grow_array(region, msg))
2737 msg->rep->rrsets[msg->rep->rrset_count] = cname;
2738 msg->rep->rrset_count++;
2739 msg->rep->an_numrrsets++;
2744 /** Change a dname to a different one, for wildcard namechange */
2746 az_change_dnames(struct dns_msg* msg, uint8_t* oldname, uint8_t* newname,
2747 size_t newlen, int an_only)
2750 size_t start = 0, end = msg->rep->rrset_count;
2751 if(!an_only) start = msg->rep->an_numrrsets;
2752 if(an_only) end = msg->rep->an_numrrsets;
2753 for(i=start; i<end; i++) {
2754 /* allocated in region so we can change the ptrs */
2755 if(query_dname_compare(msg->rep->rrsets[i]->rk.dname, oldname)
2757 msg->rep->rrsets[i]->rk.dname = newname;
2758 msg->rep->rrsets[i]->rk.dname_len = newlen;
2759 msg->rep->rrsets[i]->entry.hash = rrset_key_hash(&msg->rep->rrsets[i]->rk);
2764 /** find NSEC record covering the query */
2765 static struct auth_rrset*
2766 az_find_nsec_cover(struct auth_zone* z, struct auth_data** node)
2768 uint8_t* nm = (*node)->name;
2769 size_t nmlen = (*node)->namelen;
2770 struct auth_rrset* rrset;
2771 /* find the NSEC for the smallest-or-equal node */
2772 /* if node == NULL, we did not find a smaller name. But the zone
2773 * name is the smallest name and should have an NSEC. So there is
2774 * no NSEC to return (for a properly signed zone) */
2775 /* for empty nonterminals, the auth-data node should not exist,
2776 * and thus we don't need to go rbtree_previous here to find
2777 * a domain with an NSEC record */
2778 /* but there could be glue, and if this is node, then it has no NSEC.
2779 * Go up to find nonglue (previous) NSEC-holding nodes */
2780 while((rrset=az_domain_rrset(*node, LDNS_RR_TYPE_NSEC)) == NULL) {
2781 if(dname_is_root(nm)) return NULL;
2782 if(nmlen == z->namelen) return NULL;
2783 dname_remove_label(&nm, &nmlen);
2784 /* adjust *node for the nsec rrset to find in */
2785 *node = az_find_name(z, nm, nmlen);
2790 /** Find NSEC and add for wildcard denial */
2792 az_nsec_wildcard_denial(struct auth_zone* z, struct regional* region,
2793 struct dns_msg* msg, uint8_t* cenm, size_t cenmlen)
2795 struct query_info qinfo;
2797 struct auth_data* node;
2798 struct auth_rrset* nsec;
2799 uint8_t wc[LDNS_MAX_DOMAINLEN];
2800 if(cenmlen+2 > sizeof(wc))
2801 return 0; /* result would be too long */
2802 wc[0] = 1; /* length of wildcard label */
2803 wc[1] = (uint8_t)'*'; /* wildcard label */
2804 memmove(wc+2, cenm, cenmlen);
2806 /* we have '*.ce' in wc wildcard name buffer */
2807 /* get nsec cover for that */
2809 qinfo.qname_len = cenmlen+2;
2812 az_find_domain(z, &qinfo, &node_exact, &node);
2813 if((nsec=az_find_nsec_cover(z, &node)) != NULL) {
2814 if(!msg_add_rrset_ns(z, region, msg, node, nsec)) return 0;
2819 /** Find the NSEC3PARAM rrset (if any) and if true you have the parameters */
2821 az_nsec3_param(struct auth_zone* z, int* algo, size_t* iter, uint8_t** salt,
2824 struct auth_data* apex;
2825 struct auth_rrset* param;
2827 apex = az_find_name(z, z->name, z->namelen);
2829 param = az_domain_rrset(apex, LDNS_RR_TYPE_NSEC3PARAM);
2830 if(!param || param->data->count==0)
2831 return 0; /* no RRset or no RRs in rrset */
2832 /* find out which NSEC3PARAM RR has supported parameters */
2833 /* skip unknown flags (dynamic signer is recalculating nsec3 chain) */
2834 for(i=0; i<param->data->count; i++) {
2835 uint8_t* rdata = param->data->rr_data[i]+2;
2836 size_t rdatalen = param->data->rr_len[i];
2838 continue; /* too short */
2839 if(!nsec3_hash_algo_size_supported((int)(rdata[0])))
2840 continue; /* unsupported algo */
2841 if(rdatalen < (size_t)(2+5+(size_t)rdata[4]))
2842 continue; /* salt missing */
2843 if((rdata[1]&NSEC3_UNKNOWN_FLAGS)!=0)
2844 continue; /* unknown flags */
2845 *algo = (int)(rdata[0]);
2846 *iter = sldns_read_uint16(rdata+2);
2847 *saltlen = rdata[4];
2850 else *salt = rdata+5;
2853 /* no supported params */
2857 /** Hash a name with nsec3param into buffer, it has zone name appended.
2858 * return length of hash */
2860 az_nsec3_hash(uint8_t* buf, size_t buflen, uint8_t* nm, size_t nmlen,
2861 int algo, size_t iter, uint8_t* salt, size_t saltlen)
2863 size_t hlen = nsec3_hash_algo_size_supported(algo);
2864 /* buffer has domain name, nsec3hash, and 256 is for max saltlen
2865 * (salt has 0-255 length) */
2866 unsigned char p[LDNS_MAX_DOMAINLEN+1+N3HASHBUFLEN+256];
2868 if(nmlen+saltlen > sizeof(p) || hlen+saltlen > sizeof(p))
2871 return 0; /* somehow too large for destination buffer */
2872 /* hashfunc(name, salt) */
2873 memmove(p, nm, nmlen);
2874 query_dname_tolower(p);
2875 if(salt && saltlen > 0)
2876 memmove(p+nmlen, salt, saltlen);
2877 (void)secalgo_nsec3_hash(algo, p, nmlen+saltlen, (unsigned char*)buf);
2878 for(i=0; i<iter; i++) {
2879 /* hashfunc(hash, salt) */
2880 memmove(p, buf, hlen);
2881 if(salt && saltlen > 0)
2882 memmove(p+hlen, salt, saltlen);
2883 (void)secalgo_nsec3_hash(algo, p, hlen+saltlen,
2884 (unsigned char*)buf);
2889 /** Hash name and return b32encoded hashname for lookup, zone name appended */
2891 az_nsec3_hashname(struct auth_zone* z, uint8_t* hashname, size_t* hashnmlen,
2892 uint8_t* nm, size_t nmlen, int algo, size_t iter, uint8_t* salt,
2895 uint8_t hash[N3HASHBUFLEN];
2898 hlen = az_nsec3_hash(hash, sizeof(hash), nm, nmlen, algo, iter,
2902 if(*hashnmlen < hlen*2+1+z->namelen) /* approx b32 as hexb16 */
2904 ret = sldns_b32_ntop_extended_hex(hash, hlen, (char*)(hashname+1),
2908 hashname[0] = (uint8_t)ret;
2910 if((*hashnmlen) - ret < z->namelen)
2912 memmove(hashname+ret, z->name, z->namelen);
2913 *hashnmlen = z->namelen+(size_t)ret;
2917 /** Find the datanode that covers the nsec3hash-name */
2918 static struct auth_data*
2919 az_nsec3_findnode(struct auth_zone* z, uint8_t* hashnm, size_t hashnmlen)
2921 struct query_info qinfo;
2922 struct auth_data* node;
2926 qinfo.qname = hashnm;
2927 qinfo.qname_len = hashnmlen;
2928 /* because canonical ordering and b32 nsec3 ordering are the same.
2929 * this is a good lookup to find the nsec3 name. */
2930 az_find_domain(z, &qinfo, &node_exact, &node);
2931 /* but we may have to skip non-nsec3 nodes */
2932 /* this may be a lot, the way to speed that up is to have a
2933 * separate nsec3 tree with nsec3 nodes */
2934 while(node && (rbnode_type*)node != RBTREE_NULL &&
2935 !az_domain_rrset(node, LDNS_RR_TYPE_NSEC3)) {
2936 node = (struct auth_data*)rbtree_previous(&node->node);
2938 if((rbnode_type*)node == RBTREE_NULL)
2943 /** Find cover for hashed(nm, nmlen) (or NULL) */
2944 static struct auth_data*
2945 az_nsec3_find_cover(struct auth_zone* z, uint8_t* nm, size_t nmlen,
2946 int algo, size_t iter, uint8_t* salt, size_t saltlen)
2948 struct auth_data* node;
2949 uint8_t hname[LDNS_MAX_DOMAINLEN];
2950 size_t hlen = sizeof(hname);
2951 if(!az_nsec3_hashname(z, hname, &hlen, nm, nmlen, algo, iter,
2954 node = az_nsec3_findnode(z, hname, hlen);
2957 /* we did not find any, perhaps because the NSEC3 hash is before
2958 * the first hash, we have to find the 'last hash' in the zone */
2959 node = (struct auth_data*)rbtree_last(&z->data);
2960 while(node && (rbnode_type*)node != RBTREE_NULL &&
2961 !az_domain_rrset(node, LDNS_RR_TYPE_NSEC3)) {
2962 node = (struct auth_data*)rbtree_previous(&node->node);
2964 if((rbnode_type*)node == RBTREE_NULL)
2969 /** Find exact match for hashed(nm, nmlen) NSEC3 record or NULL */
2970 static struct auth_data*
2971 az_nsec3_find_exact(struct auth_zone* z, uint8_t* nm, size_t nmlen,
2972 int algo, size_t iter, uint8_t* salt, size_t saltlen)
2974 struct auth_data* node;
2975 uint8_t hname[LDNS_MAX_DOMAINLEN];
2976 size_t hlen = sizeof(hname);
2977 if(!az_nsec3_hashname(z, hname, &hlen, nm, nmlen, algo, iter,
2980 node = az_find_name(z, hname, hlen);
2981 if(az_domain_rrset(node, LDNS_RR_TYPE_NSEC3))
2986 /** Return nextcloser name (as a ref into the qname). This is one label
2987 * more than the cenm (cename must be a suffix of qname) */
2989 az_nsec3_get_nextcloser(uint8_t* cenm, uint8_t* qname, size_t qname_len,
2990 uint8_t** nx, size_t* nxlen)
2992 int celabs = dname_count_labels(cenm);
2993 int qlabs = dname_count_labels(qname);
2994 int strip = qlabs - celabs -1;
2995 log_assert(dname_strict_subdomain(qname, qlabs, cenm, celabs));
2999 dname_remove_labels(nx, nxlen, strip);
3002 /** Find the closest encloser that has exact NSEC3.
3003 * updated cenm to the new name. If it went up no-exact-ce is true. */
3004 static struct auth_data*
3005 az_nsec3_find_ce(struct auth_zone* z, uint8_t** cenm, size_t* cenmlen,
3006 int* no_exact_ce, int algo, size_t iter, uint8_t* salt, size_t saltlen)
3008 struct auth_data* node;
3009 while((node = az_nsec3_find_exact(z, *cenm, *cenmlen,
3010 algo, iter, salt, saltlen)) == NULL) {
3011 if(*cenmlen == z->namelen) {
3012 /* next step up would take us out of the zone. fail */
3016 dname_remove_label(cenm, cenmlen);
3021 /* Insert NSEC3 record in authority section, if NULL does nothing */
3023 az_nsec3_insert(struct auth_zone* z, struct regional* region,
3024 struct dns_msg* msg, struct auth_data* node)
3026 struct auth_rrset* nsec3;
3027 if(!node) return 1; /* no node, skip this */
3028 nsec3 = az_domain_rrset(node, LDNS_RR_TYPE_NSEC3);
3029 if(!nsec3) return 1; /* if no nsec3 RR, skip it */
3030 if(!msg_add_rrset_ns(z, region, msg, node, nsec3)) return 0;
3034 /** add NSEC3 records to the zone for the nsec3 proof.
3035 * Specify with the flags with parts of the proof are required.
3036 * the ce is the exact matching name (for notype) but also delegation points.
3037 * qname is the one where the nextcloser name can be derived from.
3038 * If NSEC3 is not properly there (in the zone) nothing is added.
3039 * always enabled: include nsec3 proving about the Closest Encloser.
3040 * that is an exact match that should exist for it.
3041 * If that does not exist, a higher exact match + nxproof is enabled
3042 * (for some sort of opt-out empty nonterminal cases).
3043 * nodataproof: search for exact match and include that instead.
3044 * ceproof: include ce proof NSEC3 (omitted for wildcard replies).
3045 * nxproof: include denial of the qname.
3046 * wcproof: include denial of wildcard (wildcard.ce).
3049 az_add_nsec3_proof(struct auth_zone* z, struct regional* region,
3050 struct dns_msg* msg, uint8_t* cenm, size_t cenmlen, uint8_t* qname,
3051 size_t qname_len, int nodataproof, int ceproof, int nxproof,
3055 size_t iter, saltlen;
3057 int no_exact_ce = 0;
3058 struct auth_data* node;
3060 /* find parameters of nsec3 proof */
3061 if(!az_nsec3_param(z, &algo, &iter, &salt, &saltlen))
3062 return 1; /* no nsec3 */
3064 /* see if the node has a hash of itself for the nodata
3065 * proof nsec3, this has to be an exact match nsec3. */
3066 struct auth_data* match;
3067 match = az_nsec3_find_exact(z, qname, qname_len, algo,
3068 iter, salt, saltlen);
3070 if(!az_nsec3_insert(z, region, msg, match))
3072 /* only nodata NSEC3 needed, no CE or others. */
3076 /* find ce that has an NSEC3 */
3078 node = az_nsec3_find_ce(z, &cenm, &cenmlen, &no_exact_ce,
3079 algo, iter, salt, saltlen);
3080 if(no_exact_ce) nxproof = 1;
3081 if(!az_nsec3_insert(z, region, msg, node))
3088 /* create nextcloser domain name */
3089 az_nsec3_get_nextcloser(cenm, qname, qname_len, &nx, &nxlen);
3090 /* find nsec3 that matches or covers it */
3091 node = az_nsec3_find_cover(z, nx, nxlen, algo, iter, salt,
3093 if(!az_nsec3_insert(z, region, msg, node))
3097 /* create wildcard name *.ce */
3098 uint8_t wc[LDNS_MAX_DOMAINLEN];
3100 if(cenmlen+2 > sizeof(wc))
3101 return 0; /* result would be too long */
3102 wc[0] = 1; /* length of wildcard label */
3103 wc[1] = (uint8_t)'*'; /* wildcard label */
3104 memmove(wc+2, cenm, cenmlen);
3106 /* find nsec3 that matches or covers it */
3107 node = az_nsec3_find_cover(z, wc, wclen, algo, iter, salt,
3109 if(!az_nsec3_insert(z, region, msg, node))
3115 /** generate answer for positive answer */
3117 az_generate_positive_answer(struct auth_zone* z, struct regional* region,
3118 struct dns_msg* msg, struct auth_data* node, struct auth_rrset* rrset)
3120 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3121 /* see if we want additional rrs */
3122 if(rrset->type == LDNS_RR_TYPE_MX) {
3123 if(!az_add_additionals_from(z, region, msg, rrset, 2))
3125 } else if(rrset->type == LDNS_RR_TYPE_SRV) {
3126 if(!az_add_additionals_from(z, region, msg, rrset, 6))
3128 } else if(rrset->type == LDNS_RR_TYPE_NS) {
3129 if(!az_add_additionals_from(z, region, msg, rrset, 0))
3135 /** generate answer for type ANY answer */
3137 az_generate_any_answer(struct auth_zone* z, struct regional* region,
3138 struct dns_msg* msg, struct auth_data* node)
3140 struct auth_rrset* rrset;
3142 /* add a couple (at least one) RRs */
3143 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_SOA)) != NULL) {
3144 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3147 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_MX)) != NULL) {
3148 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3151 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_A)) != NULL) {
3152 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3155 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_AAAA)) != NULL) {
3156 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3159 if(added == 0 && node && node->rrsets) {
3160 if(!msg_add_rrset_an(z, region, msg, node,
3161 node->rrsets)) return 0;
3166 /** follow cname chain and add more data to the answer section */
3168 follow_cname_chain(struct auth_zone* z, uint16_t qtype,
3169 struct regional* region, struct dns_msg* msg,
3170 struct packed_rrset_data* d)
3173 /* see if we can add the target of the CNAME into the answer */
3174 while(maxchain++ < MAX_CNAME_CHAIN) {
3175 struct auth_data* node;
3176 struct auth_rrset* rrset;
3178 /* d has cname rdata */
3179 if(d->count == 0) break; /* no CNAME */
3180 if(d->rr_len[0] < 2+1) break; /* too small */
3181 if((clen=dname_valid(d->rr_data[0]+2, d->rr_len[0]-2))==0)
3182 break; /* malformed */
3183 if(!dname_subdomain_c(d->rr_data[0]+2, z->name))
3184 break; /* target out of zone */
3185 if((node = az_find_name(z, d->rr_data[0]+2, clen))==NULL)
3186 break; /* no such target name */
3187 if((rrset=az_domain_rrset(node, qtype))!=NULL) {
3188 /* done we found the target */
3189 if(!msg_add_rrset_an(z, region, msg, node, rrset))
3193 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_CNAME))==NULL)
3194 break; /* no further CNAME chain, notype */
3195 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3201 /** generate answer for cname answer */
3203 az_generate_cname_answer(struct auth_zone* z, struct query_info* qinfo,
3204 struct regional* region, struct dns_msg* msg,
3205 struct auth_data* node, struct auth_rrset* rrset)
3207 if(!msg_add_rrset_an(z, region, msg, node, rrset)) return 0;
3208 if(!rrset) return 1;
3209 if(!follow_cname_chain(z, qinfo->qtype, region, msg, rrset->data))
3214 /** generate answer for notype answer */
3216 az_generate_notype_answer(struct auth_zone* z, struct regional* region,
3217 struct dns_msg* msg, struct auth_data* node)
3219 struct auth_rrset* rrset;
3220 if(!az_add_negative_soa(z, region, msg)) return 0;
3221 /* DNSSEC denial NSEC */
3222 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_NSEC))!=NULL) {
3223 if(!msg_add_rrset_ns(z, region, msg, node, rrset)) return 0;
3225 /* DNSSEC denial NSEC3 */
3226 if(!az_add_nsec3_proof(z, region, msg, node->name,
3227 node->namelen, msg->qinfo.qname,
3228 msg->qinfo.qname_len, 1, 1, 0, 0))
3234 /** generate answer for referral answer */
3236 az_generate_referral_answer(struct auth_zone* z, struct regional* region,
3237 struct dns_msg* msg, struct auth_data* ce, struct auth_rrset* rrset)
3239 struct auth_rrset* ds, *nsec;
3240 /* turn off AA flag, referral is nonAA because it leaves the zone */
3242 msg->rep->flags &= ~BIT_AA;
3243 if(!msg_add_rrset_ns(z, region, msg, ce, rrset)) return 0;
3244 /* add DS or deny it */
3245 if((ds=az_domain_rrset(ce, LDNS_RR_TYPE_DS))!=NULL) {
3246 if(!msg_add_rrset_ns(z, region, msg, ce, ds)) return 0;
3249 if((nsec=az_domain_rrset(ce, LDNS_RR_TYPE_NSEC))!=NULL) {
3250 if(!msg_add_rrset_ns(z, region, msg, ce, nsec))
3253 if(!az_add_nsec3_proof(z, region, msg, ce->name,
3254 ce->namelen, msg->qinfo.qname,
3255 msg->qinfo.qname_len, 1, 1, 0, 0))
3259 /* add additional rrs for type NS */
3260 if(!az_add_additionals_from(z, region, msg, rrset, 0)) return 0;
3264 /** generate answer for DNAME answer */
3266 az_generate_dname_answer(struct auth_zone* z, struct query_info* qinfo,
3267 struct regional* region, struct dns_msg* msg, struct auth_data* ce,
3268 struct auth_rrset* rrset)
3271 /* add the DNAME and then a CNAME */
3272 if(!msg_add_rrset_an(z, region, msg, ce, rrset)) return 0;
3273 if(!add_synth_cname(z, qinfo->qname, qinfo->qname_len, region,
3274 msg, ce, rrset)) return 0;
3275 if(FLAGS_GET_RCODE(msg->rep->flags) == LDNS_RCODE_YXDOMAIN)
3277 if(msg->rep->rrset_count == 0 ||
3278 !msg->rep->rrsets[msg->rep->rrset_count-1])
3280 if(!follow_cname_chain(z, qinfo->qtype, region, msg,
3281 (struct packed_rrset_data*)msg->rep->rrsets[
3282 msg->rep->rrset_count-1]->entry.data))
3287 /** generate answer for wildcard answer */
3289 az_generate_wildcard_answer(struct auth_zone* z, struct query_info* qinfo,
3290 struct regional* region, struct dns_msg* msg, struct auth_data* ce,
3291 struct auth_data* wildcard, struct auth_data* node)
3293 struct auth_rrset* rrset, *nsec;
3295 if((rrset=az_domain_rrset(wildcard, qinfo->qtype)) != NULL) {
3296 /* wildcard has type, add it */
3297 if(!msg_add_rrset_an(z, region, msg, wildcard, rrset))
3299 az_change_dnames(msg, wildcard->name, msg->qinfo.qname,
3300 msg->qinfo.qname_len, 1);
3301 } else if((rrset=az_domain_rrset(wildcard, LDNS_RR_TYPE_CNAME))!=NULL) {
3302 /* wildcard has cname instead, do that */
3303 if(!msg_add_rrset_an(z, region, msg, wildcard, rrset))
3305 az_change_dnames(msg, wildcard->name, msg->qinfo.qname,
3306 msg->qinfo.qname_len, 1);
3307 if(!follow_cname_chain(z, qinfo->qtype, region, msg,
3310 } else if(qinfo->qtype == LDNS_RR_TYPE_ANY && wildcard->rrsets) {
3311 /* add ANY rrsets from wildcard node */
3312 if(!az_generate_any_answer(z, region, msg, wildcard))
3314 az_change_dnames(msg, wildcard->name, msg->qinfo.qname,
3315 msg->qinfo.qname_len, 1);
3317 /* wildcard has nodata, notype answer */
3318 /* call other notype routine for dnssec notype denials */
3319 if(!az_generate_notype_answer(z, region, msg, wildcard))
3321 /* because the notype, there is no positive data with an
3322 * RRSIG that indicates the wildcard position. Thus the
3323 * wildcard qname denial needs to have a CE nsec3. */
3327 /* ce and node for dnssec denial of wildcard original name */
3328 if((nsec=az_find_nsec_cover(z, &node)) != NULL) {
3329 if(!msg_add_rrset_ns(z, region, msg, node, nsec)) return 0;
3331 uint8_t* wildup = wildcard->name;
3332 size_t wilduplen= wildcard->namelen;
3333 dname_remove_label(&wildup, &wilduplen);
3334 if(!az_add_nsec3_proof(z, region, msg, wildup,
3335 wilduplen, msg->qinfo.qname,
3336 msg->qinfo.qname_len, 0, insert_ce, 1, 0))
3340 /* fixup name of wildcard from *.zone to qname, use already allocated
3341 * pointer to msg qname */
3342 az_change_dnames(msg, wildcard->name, msg->qinfo.qname,
3343 msg->qinfo.qname_len, 0);
3347 /** generate answer for nxdomain answer */
3349 az_generate_nxdomain_answer(struct auth_zone* z, struct regional* region,
3350 struct dns_msg* msg, struct auth_data* ce, struct auth_data* node)
3352 struct auth_rrset* nsec;
3353 msg->rep->flags |= LDNS_RCODE_NXDOMAIN;
3354 if(!az_add_negative_soa(z, region, msg)) return 0;
3355 if((nsec=az_find_nsec_cover(z, &node)) != NULL) {
3356 if(!msg_add_rrset_ns(z, region, msg, node, nsec)) return 0;
3357 if(ce && !az_nsec_wildcard_denial(z, region, msg, ce->name,
3358 ce->namelen)) return 0;
3360 if(!az_add_nsec3_proof(z, region, msg, ce->name,
3361 ce->namelen, msg->qinfo.qname,
3362 msg->qinfo.qname_len, 0, 1, 1, 1))
3368 /** Create answers when an exact match exists for the domain name */
3370 az_generate_answer_with_node(struct auth_zone* z, struct query_info* qinfo,
3371 struct regional* region, struct dns_msg* msg, struct auth_data* node)
3373 struct auth_rrset* rrset;
3374 /* positive answer, rrset we are looking for exists */
3375 if((rrset=az_domain_rrset(node, qinfo->qtype)) != NULL) {
3376 return az_generate_positive_answer(z, region, msg, node, rrset);
3379 if((rrset=az_domain_rrset(node, LDNS_RR_TYPE_CNAME)) != NULL) {
3380 return az_generate_cname_answer(z, qinfo, region, msg,
3384 if(qinfo->qtype == LDNS_RR_TYPE_ANY) {
3385 return az_generate_any_answer(z, region, msg, node);
3387 /* NOERROR/NODATA (no such type at domain name) */
3388 return az_generate_notype_answer(z, region, msg, node);
3391 /** Generate answer without an existing-node that we can use.
3392 * So it'll be a referral, DNAME or nxdomain */
3394 az_generate_answer_nonexistnode(struct auth_zone* z, struct query_info* qinfo,
3395 struct regional* region, struct dns_msg* msg, struct auth_data* ce,
3396 struct auth_rrset* rrset, struct auth_data* node)
3398 struct auth_data* wildcard;
3400 /* we do not have an exact matching name (that exists) */
3401 /* see if we have a NS or DNAME in the ce */
3402 if(ce && rrset && rrset->type == LDNS_RR_TYPE_NS) {
3403 return az_generate_referral_answer(z, region, msg, ce, rrset);
3405 if(ce && rrset && rrset->type == LDNS_RR_TYPE_DNAME) {
3406 return az_generate_dname_answer(z, qinfo, region, msg, ce,
3409 /* if there is an empty nonterminal, wildcard and nxdomain don't
3410 * happen, it is a notype answer */
3411 if(az_empty_nonterminal(z, qinfo, node)) {
3412 return az_generate_notype_answer(z, region, msg, node);
3414 /* see if we have a wildcard under the ce */
3415 if((wildcard=az_find_wildcard(z, qinfo, ce)) != NULL) {
3416 return az_generate_wildcard_answer(z, qinfo, region, msg,
3417 ce, wildcard, node);
3419 /* generate nxdomain answer */
3420 return az_generate_nxdomain_answer(z, region, msg, ce, node);
3423 /** Lookup answer in a zone. */
3425 auth_zone_generate_answer(struct auth_zone* z, struct query_info* qinfo,
3426 struct regional* region, struct dns_msg** msg, int* fallback)
3428 struct auth_data* node, *ce;
3429 struct auth_rrset* rrset;
3430 int node_exact, node_exists;
3431 /* does the zone want fallback in case of failure? */
3432 *fallback = z->fallback_enabled;
3433 if(!(*msg=msg_create(region, qinfo))) return 0;
3435 /* lookup if there is a matching domain name for the query */
3436 az_find_domain(z, qinfo, &node_exact, &node);
3438 /* see if node exists for generating answers from (i.e. not glue and
3439 * obscured by NS or DNAME or NSEC3-only), and also return the
3440 * closest-encloser from that, closest node that should be used
3441 * to generate answers from that is above the query */
3442 node_exists = az_find_ce(z, qinfo, node, node_exact, &ce, &rrset);
3444 if(verbosity >= VERB_ALGO) {
3445 char zname[256], qname[256], nname[256], cename[256],
3446 tpstr[32], rrstr[32];
3447 sldns_wire2str_dname_buf(qinfo->qname, qinfo->qname_len, qname,
3449 sldns_wire2str_type_buf(qinfo->qtype, tpstr, sizeof(tpstr));
3450 sldns_wire2str_dname_buf(z->name, z->namelen, zname,
3453 sldns_wire2str_dname_buf(node->name, node->namelen,
3454 nname, sizeof(nname));
3455 else snprintf(nname, sizeof(nname), "NULL");
3457 sldns_wire2str_dname_buf(ce->name, ce->namelen,
3458 cename, sizeof(cename));
3459 else snprintf(cename, sizeof(cename), "NULL");
3460 if(rrset) sldns_wire2str_type_buf(rrset->type, rrstr,
3462 else snprintf(rrstr, sizeof(rrstr), "NULL");
3463 log_info("auth_zone %s query %s %s, domain %s %s %s, "
3464 "ce %s, rrset %s", zname, qname, tpstr, nname,
3465 (node_exact?"exact":"notexact"),
3466 (node_exists?"exist":"notexist"), cename, rrstr);
3470 /* the node is fine, generate answer from node */
3471 return az_generate_answer_with_node(z, qinfo, region, *msg,
3474 return az_generate_answer_nonexistnode(z, qinfo, region, *msg,
3478 int auth_zones_lookup(struct auth_zones* az, struct query_info* qinfo,
3479 struct regional* region, struct dns_msg** msg, int* fallback,
3480 uint8_t* dp_nm, size_t dp_nmlen)
3483 struct auth_zone* z;
3484 /* find the zone that should contain the answer. */
3485 lock_rw_rdlock(&az->lock);
3486 z = auth_zone_find(az, dp_nm, dp_nmlen, qinfo->qclass);
3488 lock_rw_unlock(&az->lock);
3489 /* no auth zone, fallback to internet */
3493 lock_rw_rdlock(&z->lock);
3494 lock_rw_unlock(&az->lock);
3496 /* if not for upstream queries, fallback */
3497 if(!z->for_upstream) {
3498 lock_rw_unlock(&z->lock);
3502 if(z->zone_expired) {
3503 *fallback = z->fallback_enabled;
3504 lock_rw_unlock(&z->lock);
3507 /* see what answer that zone would generate */
3508 r = auth_zone_generate_answer(z, qinfo, region, msg, fallback);
3509 lock_rw_unlock(&z->lock);
3513 /** encode auth answer */
3515 auth_answer_encode(struct query_info* qinfo, struct module_env* env,
3516 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf,
3517 struct regional* temp, struct dns_msg* msg)
3520 udpsize = edns->udp_size;
3521 edns->edns_version = EDNS_ADVERTISED_VERSION;
3522 edns->udp_size = EDNS_ADVERTISED_SIZE;
3523 edns->ext_rcode = 0;
3524 edns->bits &= EDNS_DO;
3526 if(!inplace_cb_reply_local_call(env, qinfo, NULL, msg->rep,
3527 (int)FLAGS_GET_RCODE(msg->rep->flags), edns, repinfo, temp, env->now_tv)
3528 || !reply_info_answer_encode(qinfo, msg->rep,
3529 *(uint16_t*)sldns_buffer_begin(buf),
3530 sldns_buffer_read_u16_at(buf, 2),
3531 buf, 0, 0, temp, udpsize, edns,
3532 (int)(edns->bits&EDNS_DO), 0)) {
3533 error_encode(buf, (LDNS_RCODE_SERVFAIL|BIT_AA), qinfo,
3534 *(uint16_t*)sldns_buffer_begin(buf),
3535 sldns_buffer_read_u16_at(buf, 2), edns);
3539 /** encode auth error answer */
3541 auth_error_encode(struct query_info* qinfo, struct module_env* env,
3542 struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf,
3543 struct regional* temp, int rcode)
3545 edns->edns_version = EDNS_ADVERTISED_VERSION;
3546 edns->udp_size = EDNS_ADVERTISED_SIZE;
3547 edns->ext_rcode = 0;
3548 edns->bits &= EDNS_DO;
3550 if(!inplace_cb_reply_local_call(env, qinfo, NULL, NULL,
3551 rcode, edns, repinfo, temp, env->now_tv))
3552 edns->opt_list_inplace_cb_out = NULL;
3553 error_encode(buf, rcode|BIT_AA, qinfo,
3554 *(uint16_t*)sldns_buffer_begin(buf),
3555 sldns_buffer_read_u16_at(buf, 2), edns);
3558 int auth_zones_answer(struct auth_zones* az, struct module_env* env,
3559 struct query_info* qinfo, struct edns_data* edns,
3560 struct comm_reply* repinfo, struct sldns_buffer* buf, struct regional* temp)
3562 struct dns_msg* msg = NULL;
3563 struct auth_zone* z;
3567 lock_rw_rdlock(&az->lock);
3568 if(!az->have_downstream) {
3569 /* no downstream auth zones */
3570 lock_rw_unlock(&az->lock);
3573 if(qinfo->qtype == LDNS_RR_TYPE_DS) {
3574 uint8_t* delname = qinfo->qname;
3575 size_t delnamelen = qinfo->qname_len;
3576 dname_remove_label(&delname, &delnamelen);
3577 z = auth_zones_find_zone(az, delname, delnamelen,
3580 z = auth_zones_find_zone(az, qinfo->qname, qinfo->qname_len,
3584 /* no zone above it */
3585 lock_rw_unlock(&az->lock);
3588 lock_rw_rdlock(&z->lock);
3589 lock_rw_unlock(&az->lock);
3590 if(!z->for_downstream) {
3591 lock_rw_unlock(&z->lock);
3594 if(z->zone_expired) {
3595 if(z->fallback_enabled) {
3596 lock_rw_unlock(&z->lock);
3599 lock_rw_unlock(&z->lock);
3600 lock_rw_wrlock(&az->lock);
3601 az->num_query_down++;
3602 lock_rw_unlock(&az->lock);
3603 auth_error_encode(qinfo, env, edns, repinfo, buf, temp,
3604 LDNS_RCODE_SERVFAIL);
3608 /* answer it from zone z */
3609 r = auth_zone_generate_answer(z, qinfo, temp, &msg, &fallback);
3610 lock_rw_unlock(&z->lock);
3611 if(!r && fallback) {
3612 /* fallback to regular answering (recursive) */
3615 lock_rw_wrlock(&az->lock);
3616 az->num_query_down++;
3617 lock_rw_unlock(&az->lock);
3621 auth_error_encode(qinfo, env, edns, repinfo, buf, temp,
3622 LDNS_RCODE_SERVFAIL);
3623 else auth_answer_encode(qinfo, env, edns, repinfo, buf, temp, msg);
3628 int auth_zones_can_fallback(struct auth_zones* az, uint8_t* nm, size_t nmlen,
3632 struct auth_zone* z;
3633 lock_rw_rdlock(&az->lock);
3634 z = auth_zone_find(az, nm, nmlen, dclass);
3636 lock_rw_unlock(&az->lock);
3637 /* no such auth zone, fallback */
3640 lock_rw_rdlock(&z->lock);
3641 lock_rw_unlock(&az->lock);
3642 r = z->fallback_enabled || (!z->for_upstream);
3643 lock_rw_unlock(&z->lock);
3648 auth_zone_parse_notify_serial(sldns_buffer* pkt, uint32_t *serial)
3650 struct query_info q;
3652 memset(&q, 0, sizeof(q));
3653 sldns_buffer_set_position(pkt, 0);
3654 if(!query_info_parse(&q, pkt)) return 0;
3655 if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) == 0) return 0;
3656 /* skip name of RR in answer section */
3657 if(sldns_buffer_remaining(pkt) < 1) return 0;
3658 if(pkt_dname_len(pkt) == 0) return 0;
3660 if(sldns_buffer_remaining(pkt) < 10 /* type,class,ttl,rdatalen*/)
3662 if(sldns_buffer_read_u16(pkt) != LDNS_RR_TYPE_SOA) return 0;
3663 sldns_buffer_skip(pkt, 2); /* class */
3664 sldns_buffer_skip(pkt, 4); /* ttl */
3665 rdlen = sldns_buffer_read_u16(pkt); /* rdatalen */
3666 if(sldns_buffer_remaining(pkt) < rdlen) return 0;
3667 if(rdlen < 22) return 0; /* bad soa length */
3668 sldns_buffer_skip(pkt, (ssize_t)(rdlen-20));
3669 *serial = sldns_buffer_read_u32(pkt);
3670 /* return true when has serial in answer section */
3674 /** see if addr appears in the list */
3676 addr_in_list(struct auth_addr* list, struct sockaddr_storage* addr,
3679 struct auth_addr* p;
3680 for(p=list; p; p=p->next) {
3681 if(sockaddr_cmp_addr(addr, addrlen, &p->addr, p->addrlen)==0)
3687 /** check if an address matches a master specification (or one of its
3688 * addresses in the addr list) */
3690 addr_matches_master(struct auth_master* master, struct sockaddr_storage* addr,
3691 socklen_t addrlen, struct auth_master** fromhost)
3693 struct sockaddr_storage a;
3696 if(addr_in_list(master->list, addr, addrlen)) {
3700 /* compare address (but not port number, that is the destination
3701 * port of the master, the port number of the received notify is
3702 * allowed to by any port on that master) */
3703 if(extstrtoaddr(master->host, &a, &alen, UNBOUND_DNS_PORT) &&
3704 sockaddr_cmp_addr(addr, addrlen, &a, alen)==0) {
3708 /* prefixes, addr/len, like 10.0.0.0/8 */
3709 /* not http and has a / and there is one / */
3710 if(master->allow_notify && !master->http &&
3711 strchr(master->host, '/') != NULL &&
3712 strchr(master->host, '/') == strrchr(master->host, '/') &&
3713 netblockstrtoaddr(master->host, UNBOUND_DNS_PORT, &a, &alen,
3714 &net) && alen == addrlen) {
3715 if(addr_in_common(addr, (addr_is_ip6(addr, addrlen)?128:32),
3716 &a, net, alen) >= net) {
3717 *fromhost = NULL; /* prefix does not have destination
3718 to send the probe or transfer with */
3719 return 1; /* matches the netblock */
3725 /** check access list for notifies */
3727 az_xfr_allowed_notify(struct auth_xfer* xfr, struct sockaddr_storage* addr,
3728 socklen_t addrlen, struct auth_master** fromhost)
3730 struct auth_master* p;
3731 for(p=xfr->allow_notify_list; p; p=p->next) {
3732 if(addr_matches_master(p, addr, addrlen, fromhost)) {
3739 /** see if the serial means the zone has to be updated, i.e. the serial
3740 * is newer than the zone serial, or we have no zone */
3742 xfr_serial_means_update(struct auth_xfer* xfr, uint32_t serial)
3745 return 1; /* no zone, anything is better */
3746 if(xfr->zone_expired)
3747 return 1; /* expired, the sent serial is better than expired
3749 if(compare_serial(xfr->serial, serial) < 0)
3750 return 1; /* our serial is smaller than the sent serial,
3751 the data is newer, fetch it */
3755 /** note notify serial, updates the notify information in the xfr struct */
3757 xfr_note_notify_serial(struct auth_xfer* xfr, int has_serial, uint32_t serial)
3759 if(xfr->notify_received && xfr->notify_has_serial && has_serial) {
3760 /* see if this serial is newer */
3761 if(compare_serial(xfr->notify_serial, serial) < 0)
3762 xfr->notify_serial = serial;
3763 } else if(xfr->notify_received && xfr->notify_has_serial &&
3765 /* remove serial, we have notify without serial */
3766 xfr->notify_has_serial = 0;
3767 xfr->notify_serial = 0;
3768 } else if(xfr->notify_received && !xfr->notify_has_serial) {
3769 /* we already have notify without serial, keep it
3770 * that way; no serial check when current operation
3773 xfr->notify_received = 1;
3774 xfr->notify_has_serial = has_serial;
3775 xfr->notify_serial = serial;
3779 /** process a notify serial, start new probe or note serial. xfr is locked */
3781 xfr_process_notify(struct auth_xfer* xfr, struct module_env* env,
3782 int has_serial, uint32_t serial, struct auth_master* fromhost)
3784 /* if the serial of notify is older than we have, don't fetch
3785 * a zone, we already have it */
3786 if(has_serial && !xfr_serial_means_update(xfr, serial)) {
3787 lock_basic_unlock(&xfr->lock);
3790 /* start new probe with this addr src, or note serial */
3791 if(!xfr_start_probe(xfr, env, fromhost)) {
3792 /* not started because already in progress, note the serial */
3793 xfr_note_notify_serial(xfr, has_serial, serial);
3794 lock_basic_unlock(&xfr->lock);
3796 /* successful end of start_probe unlocked xfr->lock */
3799 int auth_zones_notify(struct auth_zones* az, struct module_env* env,
3800 uint8_t* nm, size_t nmlen, uint16_t dclass,
3801 struct sockaddr_storage* addr, socklen_t addrlen, int has_serial,
3802 uint32_t serial, int* refused)
3804 struct auth_xfer* xfr;
3805 struct auth_master* fromhost = NULL;
3806 /* see which zone this is */
3807 lock_rw_rdlock(&az->lock);
3808 xfr = auth_xfer_find(az, nm, nmlen, dclass);
3810 lock_rw_unlock(&az->lock);
3811 /* no such zone, refuse the notify */
3815 lock_basic_lock(&xfr->lock);
3816 lock_rw_unlock(&az->lock);
3818 /* check access list for notifies */
3819 if(!az_xfr_allowed_notify(xfr, addr, addrlen, &fromhost)) {
3820 lock_basic_unlock(&xfr->lock);
3821 /* notify not allowed, refuse the notify */
3826 /* process the notify */
3827 xfr_process_notify(xfr, env, has_serial, serial, fromhost);
3831 int auth_zones_startprobesequence(struct auth_zones* az,
3832 struct module_env* env, uint8_t* nm, size_t nmlen, uint16_t dclass)
3834 struct auth_xfer* xfr;
3835 lock_rw_rdlock(&az->lock);
3836 xfr = auth_xfer_find(az, nm, nmlen, dclass);
3838 lock_rw_unlock(&az->lock);
3841 lock_basic_lock(&xfr->lock);
3842 lock_rw_unlock(&az->lock);
3844 xfr_process_notify(xfr, env, 0, 0, NULL);
3848 /** set a zone expired */
3850 auth_xfer_set_expired(struct auth_xfer* xfr, struct module_env* env,
3853 struct auth_zone* z;
3856 lock_basic_lock(&xfr->lock);
3857 xfr->zone_expired = expired;
3858 lock_basic_unlock(&xfr->lock);
3860 /* find auth_zone */
3861 lock_rw_rdlock(&env->auth_zones->lock);
3862 z = auth_zone_find(env->auth_zones, xfr->name, xfr->namelen,
3865 lock_rw_unlock(&env->auth_zones->lock);
3868 lock_rw_wrlock(&z->lock);
3869 lock_rw_unlock(&env->auth_zones->lock);
3871 /* expire auth_zone */
3872 z->zone_expired = expired;
3873 lock_rw_unlock(&z->lock);
3876 /** find master (from notify or probe) in list of masters */
3877 static struct auth_master*
3878 find_master_by_host(struct auth_master* list, char* host)
3880 struct auth_master* p;
3881 for(p=list; p; p=p->next) {
3882 if(strcmp(p->host, host) == 0)
3888 /** delete the looked up auth_addrs for all the masters in the list */
3890 xfr_masterlist_free_addrs(struct auth_master* list)
3892 struct auth_master* m;
3893 for(m=list; m; m=m->next) {
3895 auth_free_master_addrs(m->list);
3901 /** copy a list of auth_addrs */
3902 static struct auth_addr*
3903 auth_addr_list_copy(struct auth_addr* source)
3905 struct auth_addr* list = NULL, *last = NULL;
3906 struct auth_addr* p;
3907 for(p=source; p; p=p->next) {
3908 struct auth_addr* a = (struct auth_addr*)memdup(p, sizeof(*p));
3910 log_err("malloc failure");
3911 auth_free_master_addrs(list);
3915 if(last) last->next = a;
3922 /** copy a master to a new structure, NULL on alloc failure */
3923 static struct auth_master*
3924 auth_master_copy(struct auth_master* o)
3926 struct auth_master* m;
3928 m = (struct auth_master*)memdup(o, sizeof(*o));
3930 log_err("malloc failure");
3935 m->host = strdup(m->host);
3938 log_err("malloc failure");
3943 m->file = strdup(m->file);
3947 log_err("malloc failure");
3952 m->list = auth_addr_list_copy(m->list);
3963 /** copy the master addresses from the task_probe lookups to the allow_notify
3964 * list of masters */
3966 probe_copy_masters_for_allow_notify(struct auth_xfer* xfr)
3968 struct auth_master* list = NULL, *last = NULL;
3969 struct auth_master* p;
3970 /* build up new list with copies */
3971 for(p = xfr->task_transfer->masters; p; p=p->next) {
3972 struct auth_master* m = auth_master_copy(p);
3974 auth_free_masters(list);
3975 /* failed because of malloc failure, use old list */
3979 if(last) last->next = m;
3983 /* success, replace list */
3984 auth_free_masters(xfr->allow_notify_list);
3985 xfr->allow_notify_list = list;
3988 /** start the lookups for task_transfer */
3990 xfr_transfer_start_lookups(struct auth_xfer* xfr)
3992 /* delete all the looked up addresses in the list */
3993 xfr->task_transfer->scan_addr = NULL;
3994 xfr_masterlist_free_addrs(xfr->task_transfer->masters);
3996 /* start lookup at the first master */
3997 xfr->task_transfer->lookup_target = xfr->task_transfer->masters;
3998 xfr->task_transfer->lookup_aaaa = 0;
4001 /** move to the next lookup of hostname for task_transfer */
4003 xfr_transfer_move_to_next_lookup(struct auth_xfer* xfr, struct module_env* env)
4005 if(!xfr->task_transfer->lookup_target)
4006 return; /* already at end of list */
4007 if(!xfr->task_transfer->lookup_aaaa && env->cfg->do_ip6) {
4008 /* move to lookup AAAA */
4009 xfr->task_transfer->lookup_aaaa = 1;
4012 xfr->task_transfer->lookup_target =
4013 xfr->task_transfer->lookup_target->next;
4014 xfr->task_transfer->lookup_aaaa = 0;
4015 if(!env->cfg->do_ip4 && xfr->task_transfer->lookup_target!=NULL)
4016 xfr->task_transfer->lookup_aaaa = 1;
4019 /** start the lookups for task_probe */
4021 xfr_probe_start_lookups(struct auth_xfer* xfr)
4023 /* delete all the looked up addresses in the list */
4024 xfr->task_probe->scan_addr = NULL;
4025 xfr_masterlist_free_addrs(xfr->task_probe->masters);
4027 /* start lookup at the first master */
4028 xfr->task_probe->lookup_target = xfr->task_probe->masters;
4029 xfr->task_probe->lookup_aaaa = 0;
4032 /** move to the next lookup of hostname for task_probe */
4034 xfr_probe_move_to_next_lookup(struct auth_xfer* xfr, struct module_env* env)
4036 if(!xfr->task_probe->lookup_target)
4037 return; /* already at end of list */
4038 if(!xfr->task_probe->lookup_aaaa && env->cfg->do_ip6) {
4039 /* move to lookup AAAA */
4040 xfr->task_probe->lookup_aaaa = 1;
4043 xfr->task_probe->lookup_target = xfr->task_probe->lookup_target->next;
4044 xfr->task_probe->lookup_aaaa = 0;
4045 if(!env->cfg->do_ip4 && xfr->task_probe->lookup_target!=NULL)
4046 xfr->task_probe->lookup_aaaa = 1;
4049 /** start the iteration of the task_transfer list of masters */
4051 xfr_transfer_start_list(struct auth_xfer* xfr, struct auth_master* spec)
4054 xfr->task_transfer->scan_specific = find_master_by_host(
4055 xfr->task_transfer->masters, spec->host);
4056 if(xfr->task_transfer->scan_specific) {
4057 xfr->task_transfer->scan_target = NULL;
4058 xfr->task_transfer->scan_addr = NULL;
4059 if(xfr->task_transfer->scan_specific->list)
4060 xfr->task_transfer->scan_addr =
4061 xfr->task_transfer->scan_specific->list;
4065 /* no specific (notified) host to scan */
4066 xfr->task_transfer->scan_specific = NULL;
4067 xfr->task_transfer->scan_addr = NULL;
4068 /* pick up first scan target */
4069 xfr->task_transfer->scan_target = xfr->task_transfer->masters;
4070 if(xfr->task_transfer->scan_target && xfr->task_transfer->
4072 xfr->task_transfer->scan_addr =
4073 xfr->task_transfer->scan_target->list;
4076 /** start the iteration of the task_probe list of masters */
4078 xfr_probe_start_list(struct auth_xfer* xfr, struct auth_master* spec)
4081 xfr->task_probe->scan_specific = find_master_by_host(
4082 xfr->task_probe->masters, spec->host);
4083 if(xfr->task_probe->scan_specific) {
4084 xfr->task_probe->scan_target = NULL;
4085 xfr->task_probe->scan_addr = NULL;
4086 if(xfr->task_probe->scan_specific->list)
4087 xfr->task_probe->scan_addr =
4088 xfr->task_probe->scan_specific->list;
4092 /* no specific (notified) host to scan */
4093 xfr->task_probe->scan_specific = NULL;
4094 xfr->task_probe->scan_addr = NULL;
4095 /* pick up first scan target */
4096 xfr->task_probe->scan_target = xfr->task_probe->masters;
4097 if(xfr->task_probe->scan_target && xfr->task_probe->scan_target->list)
4098 xfr->task_probe->scan_addr =
4099 xfr->task_probe->scan_target->list;
4102 /** pick up the master that is being scanned right now, task_transfer */
4103 static struct auth_master*
4104 xfr_transfer_current_master(struct auth_xfer* xfr)
4106 if(xfr->task_transfer->scan_specific)
4107 return xfr->task_transfer->scan_specific;
4108 return xfr->task_transfer->scan_target;
4111 /** pick up the master that is being scanned right now, task_probe */
4112 static struct auth_master*
4113 xfr_probe_current_master(struct auth_xfer* xfr)
4115 if(xfr->task_probe->scan_specific)
4116 return xfr->task_probe->scan_specific;
4117 return xfr->task_probe->scan_target;
4120 /** true if at end of list, task_transfer */
4122 xfr_transfer_end_of_list(struct auth_xfer* xfr)
4124 return !xfr->task_transfer->scan_specific &&
4125 !xfr->task_transfer->scan_target;
4128 /** true if at end of list, task_probe */
4130 xfr_probe_end_of_list(struct auth_xfer* xfr)
4132 return !xfr->task_probe->scan_specific && !xfr->task_probe->scan_target;
4135 /** move to next master in list, task_transfer */
4137 xfr_transfer_nextmaster(struct auth_xfer* xfr)
4139 if(!xfr->task_transfer->scan_specific &&
4140 !xfr->task_transfer->scan_target)
4142 if(xfr->task_transfer->scan_addr) {
4143 xfr->task_transfer->scan_addr =
4144 xfr->task_transfer->scan_addr->next;
4145 if(xfr->task_transfer->scan_addr)
4148 if(xfr->task_transfer->scan_specific) {
4149 xfr->task_transfer->scan_specific = NULL;
4150 xfr->task_transfer->scan_target = xfr->task_transfer->masters;
4151 if(xfr->task_transfer->scan_target && xfr->task_transfer->
4153 xfr->task_transfer->scan_addr =
4154 xfr->task_transfer->scan_target->list;
4157 if(!xfr->task_transfer->scan_target)
4159 xfr->task_transfer->scan_target = xfr->task_transfer->scan_target->next;
4160 if(xfr->task_transfer->scan_target && xfr->task_transfer->
4162 xfr->task_transfer->scan_addr =
4163 xfr->task_transfer->scan_target->list;
4167 /** move to next master in list, task_probe */
4169 xfr_probe_nextmaster(struct auth_xfer* xfr)
4171 if(!xfr->task_probe->scan_specific && !xfr->task_probe->scan_target)
4173 if(xfr->task_probe->scan_addr) {
4174 xfr->task_probe->scan_addr = xfr->task_probe->scan_addr->next;
4175 if(xfr->task_probe->scan_addr)
4178 if(xfr->task_probe->scan_specific) {
4179 xfr->task_probe->scan_specific = NULL;
4180 xfr->task_probe->scan_target = xfr->task_probe->masters;
4181 if(xfr->task_probe->scan_target && xfr->task_probe->
4183 xfr->task_probe->scan_addr =
4184 xfr->task_probe->scan_target->list;
4187 if(!xfr->task_probe->scan_target)
4189 xfr->task_probe->scan_target = xfr->task_probe->scan_target->next;
4190 if(xfr->task_probe->scan_target && xfr->task_probe->
4192 xfr->task_probe->scan_addr =
4193 xfr->task_probe->scan_target->list;
4197 /** create SOA probe packet for xfr */
4199 xfr_create_soa_probe_packet(struct auth_xfer* xfr, sldns_buffer* buf,
4202 struct query_info qinfo;
4204 memset(&qinfo, 0, sizeof(qinfo));
4205 qinfo.qname = xfr->name;
4206 qinfo.qname_len = xfr->namelen;
4207 qinfo.qtype = LDNS_RR_TYPE_SOA;
4208 qinfo.qclass = xfr->dclass;
4209 qinfo_query_encode(buf, &qinfo);
4210 sldns_buffer_write_u16_at(buf, 0, id);
4213 /** create IXFR/AXFR packet for xfr */
4215 xfr_create_ixfr_packet(struct auth_xfer* xfr, sldns_buffer* buf, uint16_t id,
4216 struct auth_master* master)
4218 struct query_info qinfo;
4221 have_zone = xfr->have_zone;
4222 serial = xfr->serial;
4224 memset(&qinfo, 0, sizeof(qinfo));
4225 qinfo.qname = xfr->name;
4226 qinfo.qname_len = xfr->namelen;
4227 xfr->task_transfer->got_xfr_serial = 0;
4228 xfr->task_transfer->rr_scan_num = 0;
4229 xfr->task_transfer->incoming_xfr_serial = 0;
4230 xfr->task_transfer->on_ixfr_is_axfr = 0;
4231 xfr->task_transfer->on_ixfr = 1;
4232 qinfo.qtype = LDNS_RR_TYPE_IXFR;
4233 if(!have_zone || xfr->task_transfer->ixfr_fail || !master->ixfr) {
4234 qinfo.qtype = LDNS_RR_TYPE_AXFR;
4235 xfr->task_transfer->ixfr_fail = 0;
4236 xfr->task_transfer->on_ixfr = 0;
4239 qinfo.qclass = xfr->dclass;
4240 qinfo_query_encode(buf, &qinfo);
4241 sldns_buffer_write_u16_at(buf, 0, id);
4243 /* append serial for IXFR */
4244 if(qinfo.qtype == LDNS_RR_TYPE_IXFR) {
4245 size_t end = sldns_buffer_limit(buf);
4246 sldns_buffer_clear(buf);
4247 sldns_buffer_set_position(buf, end);
4248 /* auth section count 1 */
4249 sldns_buffer_write_u16_at(buf, LDNS_NSCOUNT_OFF, 1);
4251 sldns_buffer_write_u8(buf, 0xC0); /* compressed ptr to qname */
4252 sldns_buffer_write_u8(buf, 0x0C);
4253 sldns_buffer_write_u16(buf, LDNS_RR_TYPE_SOA);
4254 sldns_buffer_write_u16(buf, qinfo.qclass);
4255 sldns_buffer_write_u32(buf, 0); /* ttl */
4256 sldns_buffer_write_u16(buf, 22); /* rdata length */
4257 sldns_buffer_write_u8(buf, 0); /* . */
4258 sldns_buffer_write_u8(buf, 0); /* . */
4259 sldns_buffer_write_u32(buf, serial); /* serial */
4260 sldns_buffer_write_u32(buf, 0); /* refresh */
4261 sldns_buffer_write_u32(buf, 0); /* retry */
4262 sldns_buffer_write_u32(buf, 0); /* expire */
4263 sldns_buffer_write_u32(buf, 0); /* minimum */
4264 sldns_buffer_flip(buf);
4268 /** check if returned packet is OK */
4270 check_packet_ok(sldns_buffer* pkt, uint16_t qtype, struct auth_xfer* xfr,
4273 /* parse to see if packet worked, valid reply */
4275 /* check serial number of SOA */
4276 if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE)
4280 if(LDNS_ID_WIRE(sldns_buffer_begin(pkt)) != xfr->task_probe->id)
4283 /* check flag bits and rcode */
4284 if(!LDNS_QR_WIRE(sldns_buffer_begin(pkt)))
4286 if(LDNS_OPCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_PACKET_QUERY)
4288 if(LDNS_RCODE_WIRE(sldns_buffer_begin(pkt)) != LDNS_RCODE_NOERROR)
4292 if(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) != 1)
4294 sldns_buffer_skip(pkt, LDNS_HEADER_SIZE);
4295 if(sldns_buffer_remaining(pkt) < xfr->namelen)
4297 if(query_dname_compare(sldns_buffer_current(pkt), xfr->name) != 0)
4299 sldns_buffer_skip(pkt, (ssize_t)xfr->namelen);
4301 /* check qtype, qclass */
4302 if(sldns_buffer_remaining(pkt) < 4)
4304 if(sldns_buffer_read_u16(pkt) != qtype)
4306 if(sldns_buffer_read_u16(pkt) != xfr->dclass)
4311 /* read serial number, from answer section SOA */
4312 if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) == 0)
4314 /* read from first record SOA record */
4315 if(sldns_buffer_remaining(pkt) < 1)
4317 if(dname_pkt_compare(pkt, sldns_buffer_current(pkt),
4320 if(!pkt_dname_len(pkt))
4322 /* type, class, ttl, rdatalen */
4323 if(sldns_buffer_remaining(pkt) < 4+4+2)
4325 if(sldns_buffer_read_u16(pkt) != qtype)
4327 if(sldns_buffer_read_u16(pkt) != xfr->dclass)
4329 sldns_buffer_skip(pkt, 4); /* ttl */
4330 rdlen = sldns_buffer_read_u16(pkt);
4331 if(sldns_buffer_remaining(pkt) < rdlen)
4333 if(sldns_buffer_remaining(pkt) < 1)
4335 if(!pkt_dname_len(pkt)) /* soa name */
4337 if(sldns_buffer_remaining(pkt) < 1)
4339 if(!pkt_dname_len(pkt)) /* soa name */
4341 if(sldns_buffer_remaining(pkt) < 20)
4343 *serial = sldns_buffer_read_u32(pkt);
4348 /** read one line from chunks into buffer at current position */
4350 chunkline_get_line(struct auth_chunk** chunk, size_t* chunk_pos,
4355 /* more text in this chunk? */
4356 if(*chunk_pos < (*chunk)->len) {
4358 while(*chunk_pos < (*chunk)->len) {
4359 char c = (char)((*chunk)->data[*chunk_pos]);
4361 if(sldns_buffer_remaining(buf) < 2) {
4362 /* buffer too short */
4363 verbose(VERB_ALGO, "http chunkline, "
4367 sldns_buffer_write_u8(buf, (uint8_t)c);
4374 /* move to next chunk */
4375 *chunk = (*chunk)->next;
4379 if(readsome) return 1;
4383 /** count number of open and closed parenthesis in a chunkline */
4385 chunkline_count_parens(sldns_buffer* buf, size_t start)
4387 size_t end = sldns_buffer_position(buf);
4390 int squote = 0, dquote = 0;
4391 for(i=start; i<end; i++) {
4392 char c = (char)sldns_buffer_read_u8_at(buf, i);
4393 if(squote && c != '\'') continue;
4394 if(dquote && c != '"') continue;
4396 dquote = !dquote; /* skip quoted part */
4398 squote = !squote; /* skip quoted part */
4404 /* rest is a comment */
4411 /** remove trailing ;... comment from a line in the chunkline buffer */
4413 chunkline_remove_trailcomment(sldns_buffer* buf, size_t start)
4415 size_t end = sldns_buffer_position(buf);
4417 int squote = 0, dquote = 0;
4418 for(i=start; i<end; i++) {
4419 char c = (char)sldns_buffer_read_u8_at(buf, i);
4420 if(squote && c != '\'') continue;
4421 if(dquote && c != '"') continue;
4423 dquote = !dquote; /* skip quoted part */
4425 squote = !squote; /* skip quoted part */
4427 /* rest is a comment */
4428 sldns_buffer_set_position(buf, i);
4432 /* nothing to remove */
4435 /** see if a chunkline is a comment line (or empty line) */
4437 chunkline_is_comment_line_or_empty(sldns_buffer* buf)
4439 size_t i, end = sldns_buffer_limit(buf);
4440 for(i=0; i<end; i++) {
4441 char c = (char)sldns_buffer_read_u8_at(buf, i);
4443 return 1; /* comment */
4444 else if(c != ' ' && c != '\t' && c != '\r' && c != '\n')
4445 return 0; /* not a comment */
4447 return 1; /* empty */
4450 /** find a line with ( ) collated */
4452 chunkline_get_line_collated(struct auth_chunk** chunk, size_t* chunk_pos,
4457 sldns_buffer_clear(buf);
4458 pos = sldns_buffer_position(buf);
4459 if(!chunkline_get_line(chunk, chunk_pos, buf)) {
4460 if(sldns_buffer_position(buf) < sldns_buffer_limit(buf))
4461 sldns_buffer_write_u8_at(buf, sldns_buffer_position(buf), 0);
4462 else sldns_buffer_write_u8_at(buf, sldns_buffer_position(buf)-1, 0);
4463 sldns_buffer_flip(buf);
4466 parens += chunkline_count_parens(buf, pos);
4468 chunkline_remove_trailcomment(buf, pos);
4469 pos = sldns_buffer_position(buf);
4470 if(!chunkline_get_line(chunk, chunk_pos, buf)) {
4471 if(sldns_buffer_position(buf) < sldns_buffer_limit(buf))
4472 sldns_buffer_write_u8_at(buf, sldns_buffer_position(buf), 0);
4473 else sldns_buffer_write_u8_at(buf, sldns_buffer_position(buf)-1, 0);
4474 sldns_buffer_flip(buf);
4477 parens += chunkline_count_parens(buf, pos);
4480 if(sldns_buffer_remaining(buf) < 1) {
4481 verbose(VERB_ALGO, "http chunkline: "
4485 sldns_buffer_write_u8_at(buf, sldns_buffer_position(buf), 0);
4486 sldns_buffer_flip(buf);
4490 /** process $ORIGIN for http, 0 nothing, 1 done, 2 error */
4492 http_parse_origin(sldns_buffer* buf, struct sldns_file_parse_state* pstate)
4494 char* line = (char*)sldns_buffer_begin(buf);
4495 if(strncmp(line, "$ORIGIN", 7) == 0 &&
4496 isspace((unsigned char)line[7])) {
4498 pstate->origin_len = sizeof(pstate->origin);
4499 s = sldns_str2wire_dname_buf(sldns_strip_ws(line+8),
4500 pstate->origin, &pstate->origin_len);
4502 pstate->origin_len = 0;
4510 /** process $TTL for http, 0 nothing, 1 done, 2 error */
4512 http_parse_ttl(sldns_buffer* buf, struct sldns_file_parse_state* pstate)
4514 char* line = (char*)sldns_buffer_begin(buf);
4515 if(strncmp(line, "$TTL", 4) == 0 &&
4516 isspace((unsigned char)line[4])) {
4517 const char* end = NULL;
4519 pstate->default_ttl = sldns_str2period(
4520 sldns_strip_ws(line+5), &end, &overflow);
4529 /** find noncomment RR line in chunks, collates lines if ( ) format */
4531 chunkline_non_comment_RR(struct auth_chunk** chunk, size_t* chunk_pos,
4532 sldns_buffer* buf, struct sldns_file_parse_state* pstate)
4535 while(chunkline_get_line_collated(chunk, chunk_pos, buf)) {
4536 if(chunkline_is_comment_line_or_empty(buf)) {
4537 /* a comment, go to next line */
4540 if((ret=http_parse_origin(buf, pstate))!=0) {
4543 continue; /* $ORIGIN has been handled */
4545 if((ret=http_parse_ttl(buf, pstate))!=0) {
4548 continue; /* $TTL has been handled */
4552 /* no noncomments, fail */
4556 /** check syntax of chunklist zonefile, parse first RR, return false on
4557 * failure and return a string in the scratch buffer (first RR string)
4560 http_zonefile_syntax_check(struct auth_xfer* xfr, sldns_buffer* buf)
4562 uint8_t rr[LDNS_RR_BUF_SIZE];
4563 size_t rr_len, dname_len = 0;
4564 struct sldns_file_parse_state pstate;
4565 struct auth_chunk* chunk;
4568 memset(&pstate, 0, sizeof(pstate));
4569 pstate.default_ttl = 3600;
4570 if(xfr->namelen < sizeof(pstate.origin)) {
4571 pstate.origin_len = xfr->namelen;
4572 memmove(pstate.origin, xfr->name, xfr->namelen);
4574 chunk = xfr->task_transfer->chunks_first;
4576 if(!chunkline_non_comment_RR(&chunk, &chunk_pos, buf, &pstate)) {
4579 rr_len = sizeof(rr);
4580 e=sldns_str2wire_rr_buf((char*)sldns_buffer_begin(buf), rr, &rr_len,
4581 &dname_len, pstate.default_ttl,
4582 pstate.origin_len?pstate.origin:NULL, pstate.origin_len,
4583 pstate.prev_rr_len?pstate.prev_rr:NULL, pstate.prev_rr_len);
4585 log_err("parse failure on first RR[%d]: %s",
4586 LDNS_WIREPARSE_OFFSET(e),
4587 sldns_get_errorstr_parse(LDNS_WIREPARSE_ERROR(e)));
4590 /* check that class is correct */
4591 if(sldns_wirerr_get_class(rr, rr_len, dname_len) != xfr->dclass) {
4592 log_err("parse failure: first record in downloaded zonefile "
4593 "from wrong RR class");
4599 /** sum sizes of chunklist */
4601 chunklist_sum(struct auth_chunk* list)
4603 struct auth_chunk* p;
4605 for(p=list; p; p=p->next) {
4611 /** remove newlines from collated line */
4613 chunkline_newline_removal(sldns_buffer* buf)
4615 size_t i, end=sldns_buffer_limit(buf);
4616 for(i=0; i<end; i++) {
4617 char c = (char)sldns_buffer_read_u8_at(buf, i);
4618 if(c == '\n' && i==end-1) {
4619 sldns_buffer_write_u8_at(buf, i, 0);
4620 sldns_buffer_set_limit(buf, end-1);
4624 sldns_buffer_write_u8_at(buf, i, (uint8_t)' ');
4628 /** for http download, parse and add RR to zone */
4630 http_parse_add_rr(struct auth_xfer* xfr, struct auth_zone* z,
4631 sldns_buffer* buf, struct sldns_file_parse_state* pstate)
4633 uint8_t rr[LDNS_RR_BUF_SIZE];
4634 size_t rr_len, dname_len = 0;
4636 char* line = (char*)sldns_buffer_begin(buf);
4637 rr_len = sizeof(rr);
4638 e = sldns_str2wire_rr_buf(line, rr, &rr_len, &dname_len,
4639 pstate->default_ttl,
4640 pstate->origin_len?pstate->origin:NULL, pstate->origin_len,
4641 pstate->prev_rr_len?pstate->prev_rr:NULL, pstate->prev_rr_len);
4643 log_err("%s/%s parse failure RR[%d]: %s in '%s'",
4644 xfr->task_transfer->master->host,
4645 xfr->task_transfer->master->file,
4646 LDNS_WIREPARSE_OFFSET(e),
4647 sldns_get_errorstr_parse(LDNS_WIREPARSE_ERROR(e)),
4652 return 1; /* empty line or so */
4655 if(dname_len < sizeof(pstate->prev_rr)) {
4656 memmove(pstate->prev_rr, rr, dname_len);
4657 pstate->prev_rr_len = dname_len;
4660 return az_insert_rr(z, rr, rr_len, dname_len, NULL);
4663 /** RR list iterator, returns RRs from answer section one by one from the
4664 * dns packets in the chunklist */
4666 chunk_rrlist_start(struct auth_xfer* xfr, struct auth_chunk** rr_chunk,
4667 int* rr_num, size_t* rr_pos)
4669 *rr_chunk = xfr->task_transfer->chunks_first;
4674 /** RR list iterator, see if we are at the end of the list */
4676 chunk_rrlist_end(struct auth_chunk* rr_chunk, int rr_num)
4679 if(rr_chunk->len < LDNS_HEADER_SIZE)
4681 if(rr_num < (int)LDNS_ANCOUNT(rr_chunk->data))
4683 /* no more RRs in this chunk */
4684 /* continue with next chunk, see if it has RRs */
4685 rr_chunk = rr_chunk->next;
4691 /** RR list iterator, move to next RR */
4693 chunk_rrlist_gonext(struct auth_chunk** rr_chunk, int* rr_num,
4694 size_t* rr_pos, size_t rr_nextpos)
4696 /* already at end of chunks? */
4699 /* move within this chunk */
4700 if((*rr_chunk)->len >= LDNS_HEADER_SIZE &&
4701 (*rr_num)+1 < (int)LDNS_ANCOUNT((*rr_chunk)->data)) {
4703 *rr_pos = rr_nextpos;
4706 /* no more RRs in this chunk */
4707 /* continue with next chunk, see if it has RRs */
4709 *rr_chunk = (*rr_chunk)->next;
4713 if((*rr_chunk)->len >= LDNS_HEADER_SIZE &&
4714 LDNS_ANCOUNT((*rr_chunk)->data) > 0) {
4717 *rr_chunk = (*rr_chunk)->next;
4721 /** RR iterator, get current RR information, false on parse error */
4723 chunk_rrlist_get_current(struct auth_chunk* rr_chunk, int rr_num,
4724 size_t rr_pos, uint8_t** rr_dname, uint16_t* rr_type,
4725 uint16_t* rr_class, uint32_t* rr_ttl, uint16_t* rr_rdlen,
4726 uint8_t** rr_rdata, size_t* rr_nextpos)
4729 /* integrity checks on position */
4730 if(!rr_chunk) return 0;
4731 if(rr_chunk->len < LDNS_HEADER_SIZE) return 0;
4732 if(rr_num >= (int)LDNS_ANCOUNT(rr_chunk->data)) return 0;
4733 if(rr_pos >= rr_chunk->len) return 0;
4735 /* fetch rr information */
4736 sldns_buffer_init_frm_data(&pkt, rr_chunk->data, rr_chunk->len);
4739 /* skip question section */
4740 sldns_buffer_set_position(&pkt, LDNS_HEADER_SIZE);
4741 for(i=0; i<LDNS_QDCOUNT(rr_chunk->data); i++) {
4742 if(pkt_dname_len(&pkt) == 0) return 0;
4743 if(sldns_buffer_remaining(&pkt) < 4) return 0;
4744 sldns_buffer_skip(&pkt, 4); /* type and class */
4747 sldns_buffer_set_position(&pkt, rr_pos);
4749 *rr_dname = sldns_buffer_current(&pkt);
4750 if(pkt_dname_len(&pkt) == 0) return 0;
4751 if(sldns_buffer_remaining(&pkt) < 10) return 0;
4752 *rr_type = sldns_buffer_read_u16(&pkt);
4753 *rr_class = sldns_buffer_read_u16(&pkt);
4754 *rr_ttl = sldns_buffer_read_u32(&pkt);
4755 *rr_rdlen = sldns_buffer_read_u16(&pkt);
4756 if(sldns_buffer_remaining(&pkt) < (*rr_rdlen)) return 0;
4757 *rr_rdata = sldns_buffer_current(&pkt);
4758 sldns_buffer_skip(&pkt, (ssize_t)(*rr_rdlen));
4759 *rr_nextpos = sldns_buffer_position(&pkt);
4763 /** print log message where we are in parsing the zone transfer */
4765 log_rrlist_position(const char* label, struct auth_chunk* rr_chunk,
4766 uint8_t* rr_dname, uint16_t rr_type, size_t rr_counter)
4773 sldns_buffer_init_frm_data(&pkt, rr_chunk->data, rr_chunk->len);
4774 sldns_buffer_set_position(&pkt, (size_t)(rr_dname -
4775 sldns_buffer_begin(&pkt)));
4776 if((dlen=pkt_dname_len(&pkt)) == 0) return;
4777 if(dlen >= sizeof(buf)) return;
4778 dname_pkt_copy(&pkt, buf, rr_dname);
4779 dname_str(buf, str);
4780 (void)sldns_wire2str_type_buf(rr_type, typestr, sizeof(typestr));
4781 verbose(VERB_ALGO, "%s at[%d] %s %s", label, (int)rr_counter,
4785 /** check that start serial is OK for ixfr. we are at rr_counter == 0,
4786 * and we are going to check rr_counter == 1 (has to be type SOA) serial */
4788 ixfr_start_serial(struct auth_chunk* rr_chunk, int rr_num, size_t rr_pos,
4789 uint8_t* rr_dname, uint16_t rr_type, uint16_t rr_class,
4790 uint32_t rr_ttl, uint16_t rr_rdlen, uint8_t* rr_rdata,
4791 size_t rr_nextpos, uint32_t transfer_serial, uint32_t xfr_serial)
4793 uint32_t startserial;
4794 /* move forward on RR */
4795 chunk_rrlist_gonext(&rr_chunk, &rr_num, &rr_pos, rr_nextpos);
4796 if(chunk_rrlist_end(rr_chunk, rr_num)) {
4798 verbose(VERB_OPS, "IXFR has no second SOA record");
4801 if(!chunk_rrlist_get_current(rr_chunk, rr_num, rr_pos,
4802 &rr_dname, &rr_type, &rr_class, &rr_ttl, &rr_rdlen,
4803 &rr_rdata, &rr_nextpos)) {
4804 verbose(VERB_OPS, "IXFR cannot parse second SOA record");
4805 /* failed to parse RR */
4808 if(rr_type != LDNS_RR_TYPE_SOA) {
4809 verbose(VERB_OPS, "IXFR second record is not type SOA");
4813 verbose(VERB_OPS, "IXFR, second SOA has short rdlength");
4814 return 0; /* bad SOA rdlen */
4816 startserial = sldns_read_uint32(rr_rdata+rr_rdlen-20);
4817 if(startserial == transfer_serial) {
4818 /* empty AXFR, not an IXFR */
4819 verbose(VERB_OPS, "IXFR second serial same as first");
4822 if(startserial != xfr_serial) {
4823 /* wrong start serial, it does not match the serial in
4825 verbose(VERB_OPS, "IXFR is from serial %u to %u but %u "
4826 "in memory, rejecting the zone transfer",
4827 (unsigned)startserial, (unsigned)transfer_serial,
4828 (unsigned)xfr_serial);
4831 /* everything OK in second SOA serial */
4835 /** apply IXFR to zone in memory. z is locked. false on failure(mallocfail) */
4837 apply_ixfr(struct auth_xfer* xfr, struct auth_zone* z,
4838 struct sldns_buffer* scratch_buffer)
4840 struct auth_chunk* rr_chunk;
4843 uint8_t* rr_dname, *rr_rdata;
4844 uint16_t rr_type, rr_class, rr_rdlen;
4847 int have_transfer_serial = 0;
4848 uint32_t transfer_serial = 0;
4849 size_t rr_counter = 0;
4853 /* start RR iterator over chunklist of packets */
4854 chunk_rrlist_start(xfr, &rr_chunk, &rr_num, &rr_pos);
4855 while(!chunk_rrlist_end(rr_chunk, rr_num)) {
4856 if(!chunk_rrlist_get_current(rr_chunk, rr_num, rr_pos,
4857 &rr_dname, &rr_type, &rr_class, &rr_ttl, &rr_rdlen,
4858 &rr_rdata, &rr_nextpos)) {
4859 /* failed to parse RR */
4862 if(verbosity>=7) log_rrlist_position("apply ixfr",
4863 rr_chunk, rr_dname, rr_type, rr_counter);
4864 /* twiddle add/del mode and check for start and end */
4865 if(rr_counter == 0 && rr_type != LDNS_RR_TYPE_SOA)
4867 if(rr_counter == 1 && rr_type != LDNS_RR_TYPE_SOA) {
4868 /* this is an AXFR returned from the IXFR master */
4869 /* but that should already have been detected, by
4870 * on_ixfr_is_axfr */
4873 if(rr_type == LDNS_RR_TYPE_SOA) {
4875 if(rr_rdlen < 22) return 0; /* bad SOA rdlen */
4876 serial = sldns_read_uint32(rr_rdata+rr_rdlen-20);
4877 if(have_transfer_serial == 0) {
4878 have_transfer_serial = 1;
4879 transfer_serial = serial;
4880 delmode = 1; /* gets negated below */
4881 /* check second RR before going any further */
4882 if(!ixfr_start_serial(rr_chunk, rr_num, rr_pos,
4883 rr_dname, rr_type, rr_class, rr_ttl,
4884 rr_rdlen, rr_rdata, rr_nextpos,
4885 transfer_serial, xfr->serial)) {
4888 } else if(transfer_serial == serial) {
4889 have_transfer_serial++;
4890 if(rr_counter == 1) {
4891 /* empty AXFR, with SOA; SOA; */
4892 /* should have been detected by
4893 * on_ixfr_is_axfr */
4896 if(have_transfer_serial == 3) {
4897 /* see serial three times for end */
4900 * SOA 1 second RR, followed by del
4901 * SOA 2 followed by add
4902 * SOA 2 followed by del
4903 * SOA 3 followed by add
4905 /* ended by SOA record */
4906 xfr->serial = transfer_serial;
4910 /* twiddle add/del mode */
4911 /* switch from delete part to add part and back again
4912 * just before the soa, it gets deleted and added too
4913 * this means we switch to delete mode for the final
4914 * SOA(so skip that one) */
4917 /* process this RR */
4918 /* if the RR is deleted twice or added twice, then we
4919 * softfail, and continue with the rest of the IXFR, so
4920 * that we serve something fairly nice during the refetch */
4921 if(verbosity>=7) log_rrlist_position((delmode?"del":"add"),
4922 rr_chunk, rr_dname, rr_type, rr_counter);
4924 /* delete this RR */
4926 if(!az_remove_rr_decompress(z, rr_chunk->data,
4927 rr_chunk->len, scratch_buffer, rr_dname,
4928 rr_type, rr_class, rr_ttl, rr_rdata, rr_rdlen,
4930 /* failed, malloc error or so */
4934 /* it was removal of a nonexisting RR */
4935 if(verbosity>=4) log_rrlist_position(
4936 "IXFR error nonexistent RR",
4937 rr_chunk, rr_dname, rr_type, rr_counter);
4940 } else if(rr_counter != 0) {
4941 /* skip first SOA RR for addition, it is added in
4942 * the addition part near the end of the ixfr, when
4943 * that serial is seen the second time. */
4946 if(!az_insert_rr_decompress(z, rr_chunk->data,
4947 rr_chunk->len, scratch_buffer, rr_dname,
4948 rr_type, rr_class, rr_ttl, rr_rdata, rr_rdlen,
4950 /* failed, malloc error or so */
4954 /* it was a duplicate */
4955 if(verbosity>=4) log_rrlist_position(
4956 "IXFR error duplicate RR",
4957 rr_chunk, rr_dname, rr_type, rr_counter);
4963 chunk_rrlist_gonext(&rr_chunk, &rr_num, &rr_pos, rr_nextpos);
4966 verbose(VERB_ALGO, "IXFR did not apply cleanly, fetching full zone");
4972 /** apply AXFR to zone in memory. z is locked. false on failure(mallocfail) */
4974 apply_axfr(struct auth_xfer* xfr, struct auth_zone* z,
4975 struct sldns_buffer* scratch_buffer)
4977 struct auth_chunk* rr_chunk;
4980 uint8_t* rr_dname, *rr_rdata;
4981 uint16_t rr_type, rr_class, rr_rdlen;
4983 uint32_t serial = 0;
4985 size_t rr_counter = 0;
4986 int have_end_soa = 0;
4988 /* clear the data tree */
4989 traverse_postorder(&z->data, auth_data_del, NULL);
4990 rbtree_init(&z->data, &auth_data_cmp);
4991 /* clear the RPZ policies */
4998 /* insert all RRs in to the zone */
4999 /* insert the SOA only once, skip the last one */
5000 /* start RR iterator over chunklist of packets */
5001 chunk_rrlist_start(xfr, &rr_chunk, &rr_num, &rr_pos);
5002 while(!chunk_rrlist_end(rr_chunk, rr_num)) {
5003 if(!chunk_rrlist_get_current(rr_chunk, rr_num, rr_pos,
5004 &rr_dname, &rr_type, &rr_class, &rr_ttl, &rr_rdlen,
5005 &rr_rdata, &rr_nextpos)) {
5006 /* failed to parse RR */
5009 if(verbosity>=7) log_rrlist_position("apply_axfr",
5010 rr_chunk, rr_dname, rr_type, rr_counter);
5011 if(rr_type == LDNS_RR_TYPE_SOA) {
5012 if(rr_counter != 0) {
5013 /* end of the axfr */
5017 if(rr_rdlen < 22) return 0; /* bad SOA rdlen */
5018 serial = sldns_read_uint32(rr_rdata+rr_rdlen-20);
5022 if(!az_insert_rr_decompress(z, rr_chunk->data, rr_chunk->len,
5023 scratch_buffer, rr_dname, rr_type, rr_class, rr_ttl,
5024 rr_rdata, rr_rdlen, NULL)) {
5025 /* failed, malloc error or so */
5030 chunk_rrlist_gonext(&rr_chunk, &rr_num, &rr_pos, rr_nextpos);
5033 log_err("no end SOA record for AXFR");
5037 xfr->serial = serial;
5042 /** apply HTTP to zone in memory. z is locked. false on failure(mallocfail) */
5044 apply_http(struct auth_xfer* xfr, struct auth_zone* z,
5045 struct sldns_buffer* scratch_buffer)
5047 /* parse data in chunks */
5048 /* parse RR's and read into memory. ignore $INCLUDE from the
5050 struct sldns_file_parse_state pstate;
5051 struct auth_chunk* chunk;
5054 memset(&pstate, 0, sizeof(pstate));
5055 pstate.default_ttl = 3600;
5056 if(xfr->namelen < sizeof(pstate.origin)) {
5057 pstate.origin_len = xfr->namelen;
5058 memmove(pstate.origin, xfr->name, xfr->namelen);
5061 if(verbosity >= VERB_ALGO)
5062 verbose(VERB_ALGO, "http download %s of size %d",
5063 xfr->task_transfer->master->file,
5064 (int)chunklist_sum(xfr->task_transfer->chunks_first));
5065 if(xfr->task_transfer->chunks_first && verbosity >= VERB_ALGO) {
5067 if(xfr->task_transfer->chunks_first->len+1 > sizeof(preview)) {
5068 memmove(preview, xfr->task_transfer->chunks_first->data,
5070 preview[sizeof(preview)-1]=0;
5072 memmove(preview, xfr->task_transfer->chunks_first->data,
5073 xfr->task_transfer->chunks_first->len);
5074 preview[xfr->task_transfer->chunks_first->len]=0;
5076 log_info("auth zone http downloaded content preview: %s",
5080 /* perhaps a little syntax check before we try to apply the data? */
5081 if(!http_zonefile_syntax_check(xfr, scratch_buffer)) {
5082 log_err("http download %s/%s does not contain a zonefile, "
5083 "but got '%s'", xfr->task_transfer->master->host,
5084 xfr->task_transfer->master->file,
5085 sldns_buffer_begin(scratch_buffer));
5089 /* clear the data tree */
5090 traverse_postorder(&z->data, auth_data_del, NULL);
5091 rbtree_init(&z->data, &auth_data_cmp);
5092 /* clear the RPZ policies */
5099 chunk = xfr->task_transfer->chunks_first;
5102 while(chunkline_get_line_collated(&chunk, &chunk_pos, scratch_buffer)) {
5103 /* process this line */
5105 chunkline_newline_removal(scratch_buffer);
5106 if(chunkline_is_comment_line_or_empty(scratch_buffer)) {
5109 /* parse line and add RR */
5110 if((ret=http_parse_origin(scratch_buffer, &pstate))!=0) {
5112 verbose(VERB_ALGO, "error parsing ORIGIN on line [%s:%d] %s",
5113 xfr->task_transfer->master->file,
5115 sldns_buffer_begin(scratch_buffer));
5118 continue; /* $ORIGIN has been handled */
5120 if((ret=http_parse_ttl(scratch_buffer, &pstate))!=0) {
5122 verbose(VERB_ALGO, "error parsing TTL on line [%s:%d] %s",
5123 xfr->task_transfer->master->file,
5125 sldns_buffer_begin(scratch_buffer));
5128 continue; /* $TTL has been handled */
5130 if(!http_parse_add_rr(xfr, z, scratch_buffer, &pstate)) {
5131 verbose(VERB_ALGO, "error parsing line [%s:%d] %s",
5132 xfr->task_transfer->master->file,
5134 sldns_buffer_begin(scratch_buffer));
5141 /** write http chunks to zonefile to create downloaded file */
5143 auth_zone_write_chunks(struct auth_xfer* xfr, const char* fname)
5146 struct auth_chunk* p;
5147 out = fopen(fname, "w");
5149 log_err("could not open %s: %s", fname, strerror(errno));
5152 for(p = xfr->task_transfer->chunks_first; p ; p = p->next) {
5153 if(!write_out(out, (char*)p->data, p->len)) {
5154 log_err("could not write http download to %s", fname);
5163 /** write to zonefile after zone has been updated */
5165 xfr_write_after_update(struct auth_xfer* xfr, struct module_env* env)
5167 struct config_file* cfg = env->cfg;
5168 struct auth_zone* z;
5171 lock_basic_unlock(&xfr->lock);
5173 /* get lock again, so it is a readlock and concurrently queries
5174 * can be answered */
5175 lock_rw_rdlock(&env->auth_zones->lock);
5176 z = auth_zone_find(env->auth_zones, xfr->name, xfr->namelen,
5179 lock_rw_unlock(&env->auth_zones->lock);
5180 /* the zone is gone, ignore xfr results */
5181 lock_basic_lock(&xfr->lock);
5184 lock_rw_rdlock(&z->lock);
5185 lock_basic_lock(&xfr->lock);
5186 lock_rw_unlock(&env->auth_zones->lock);
5188 if(z->zonefile == NULL || z->zonefile[0] == 0) {
5189 lock_rw_unlock(&z->lock);
5190 /* no write needed, no zonefile set */
5193 zfilename = z->zonefile;
5194 if(cfg->chrootdir && cfg->chrootdir[0] && strncmp(zfilename,
5195 cfg->chrootdir, strlen(cfg->chrootdir)) == 0)
5196 zfilename += strlen(cfg->chrootdir);
5197 if(verbosity >= VERB_ALGO) {
5199 dname_str(z->name, nm);
5200 verbose(VERB_ALGO, "write zonefile %s for %s", zfilename, nm);
5203 /* write to tempfile first */
5204 if((size_t)strlen(zfilename) + 16 > sizeof(tmpfile)) {
5205 verbose(VERB_ALGO, "tmpfilename too long, cannot update "
5206 " zonefile %s", zfilename);
5207 lock_rw_unlock(&z->lock);
5210 snprintf(tmpfile, sizeof(tmpfile), "%s.tmp%u", zfilename,
5211 (unsigned)getpid());
5212 if(xfr->task_transfer->master->http) {
5213 /* use the stored chunk list to write them */
5214 if(!auth_zone_write_chunks(xfr, tmpfile)) {
5216 lock_rw_unlock(&z->lock);
5219 } else if(!auth_zone_write_file(z, tmpfile)) {
5221 lock_rw_unlock(&z->lock);
5224 #ifdef UB_ON_WINDOWS
5225 (void)unlink(zfilename); /* windows does not replace file with rename() */
5227 if(rename(tmpfile, zfilename) < 0) {
5228 log_err("could not rename(%s, %s): %s", tmpfile, zfilename,
5231 lock_rw_unlock(&z->lock);
5234 lock_rw_unlock(&z->lock);
5237 /** reacquire locks and structures. Starts with no locks, ends
5238 * with xfr and z locks, if fail, no z lock */
5239 static int xfr_process_reacquire_locks(struct auth_xfer* xfr,
5240 struct module_env* env, struct auth_zone** z)
5242 /* release xfr lock, then, while holding az->lock grab both
5243 * z->lock and xfr->lock */
5244 lock_rw_rdlock(&env->auth_zones->lock);
5245 *z = auth_zone_find(env->auth_zones, xfr->name, xfr->namelen,
5248 lock_rw_unlock(&env->auth_zones->lock);
5249 lock_basic_lock(&xfr->lock);
5253 lock_rw_wrlock(&(*z)->lock);
5254 lock_basic_lock(&xfr->lock);
5255 lock_rw_unlock(&env->auth_zones->lock);
5259 /** process chunk list and update zone in memory,
5260 * return false if it did not work */
5262 xfr_process_chunk_list(struct auth_xfer* xfr, struct module_env* env,
5265 struct auth_zone* z;
5267 /* obtain locks and structures */
5268 lock_basic_unlock(&xfr->lock);
5269 if(!xfr_process_reacquire_locks(xfr, env, &z)) {
5270 /* the zone is gone, ignore xfr results */
5273 /* holding xfr and z locks */
5276 if(xfr->task_transfer->master->http) {
5277 if(!apply_http(xfr, z, env->scratch_buffer)) {
5278 lock_rw_unlock(&z->lock);
5279 verbose(VERB_ALGO, "http from %s: could not store data",
5280 xfr->task_transfer->master->host);
5283 } else if(xfr->task_transfer->on_ixfr &&
5284 !xfr->task_transfer->on_ixfr_is_axfr) {
5285 if(!apply_ixfr(xfr, z, env->scratch_buffer)) {
5286 lock_rw_unlock(&z->lock);
5287 verbose(VERB_ALGO, "xfr from %s: could not store IXFR"
5288 " data", xfr->task_transfer->master->host);
5293 if(!apply_axfr(xfr, z, env->scratch_buffer)) {
5294 lock_rw_unlock(&z->lock);
5295 verbose(VERB_ALGO, "xfr from %s: could not store AXFR"
5296 " data", xfr->task_transfer->master->host);
5300 xfr->zone_expired = 0;
5301 z->zone_expired = 0;
5302 if(!xfr_find_soa(z, xfr)) {
5303 lock_rw_unlock(&z->lock);
5304 verbose(VERB_ALGO, "xfr from %s: no SOA in zone after update"
5305 " (or malformed RR)", xfr->task_transfer->master->host);
5309 /* release xfr lock while verifying zonemd because it may have
5310 * to spawn lookups in the state machines */
5311 lock_basic_unlock(&xfr->lock);
5312 /* holding z lock */
5313 auth_zone_verify_zonemd(z, env, &env->mesh->mods, NULL, 0, 0);
5314 if(z->zone_expired) {
5316 /* ZONEMD must have failed */
5317 /* reacquire locks, so we hold xfr lock on exit of routine,
5318 * and both xfr and z again after releasing xfr for potential
5319 * state machine mesh callbacks */
5320 lock_rw_unlock(&z->lock);
5321 if(!xfr_process_reacquire_locks(xfr, env, &z))
5323 dname_str(xfr->name, zname);
5324 verbose(VERB_ALGO, "xfr from %s: ZONEMD failed for %s, transfer is failed", xfr->task_transfer->master->host, zname);
5325 xfr->zone_expired = 1;
5326 lock_rw_unlock(&z->lock);
5329 /* reacquire locks, so we hold xfr lock on exit of routine,
5330 * and both xfr and z again after releasing xfr for potential
5331 * state machine mesh callbacks */
5332 lock_rw_unlock(&z->lock);
5333 if(!xfr_process_reacquire_locks(xfr, env, &z))
5335 /* holding xfr and z locks */
5338 xfr->lease_time = *env->now;
5341 rpz_finish_config(z->rpz);
5344 lock_rw_unlock(&z->lock);
5346 if(verbosity >= VERB_QUERY && xfr->have_zone) {
5348 dname_str(xfr->name, zname);
5349 verbose(VERB_QUERY, "auth zone %s updated to serial %u", zname,
5350 (unsigned)xfr->serial);
5352 /* see if we need to write to a zonefile */
5353 xfr_write_after_update(xfr, env);
5357 /** disown task_transfer. caller must hold xfr.lock */
5359 xfr_transfer_disown(struct auth_xfer* xfr)
5361 /* remove timer (from this worker's event base) */
5362 comm_timer_delete(xfr->task_transfer->timer);
5363 xfr->task_transfer->timer = NULL;
5364 /* remove the commpoint */
5365 comm_point_delete(xfr->task_transfer->cp);
5366 xfr->task_transfer->cp = NULL;
5367 /* we don't own this item anymore */
5368 xfr->task_transfer->worker = NULL;
5369 xfr->task_transfer->env = NULL;
5372 /** lookup a host name for its addresses, if needed */
5374 xfr_transfer_lookup_host(struct auth_xfer* xfr, struct module_env* env)
5376 struct sockaddr_storage addr;
5377 socklen_t addrlen = 0;
5378 struct auth_master* master = xfr->task_transfer->lookup_target;
5379 struct query_info qinfo;
5380 uint16_t qflags = BIT_RD;
5381 uint8_t dname[LDNS_MAX_DOMAINLEN+1];
5382 struct edns_data edns;
5383 sldns_buffer* buf = env->scratch_buffer;
5384 if(!master) return 0;
5385 if(extstrtoaddr(master->host, &addr, &addrlen, UNBOUND_DNS_PORT)) {
5386 /* not needed, host is in IP addr format */
5389 if(master->allow_notify)
5390 return 0; /* allow-notifies are not transferred from, no
5393 /* use mesh_new_callback to probe for non-addr hosts,
5394 * and then wait for them to be looked up (in cache, or query) */
5395 qinfo.qname_len = sizeof(dname);
5396 if(sldns_str2wire_dname_buf(master->host, dname, &qinfo.qname_len)
5398 log_err("cannot parse host name of master %s", master->host);
5401 qinfo.qname = dname;
5402 qinfo.qclass = xfr->dclass;
5403 qinfo.qtype = LDNS_RR_TYPE_A;
5404 if(xfr->task_transfer->lookup_aaaa)
5405 qinfo.qtype = LDNS_RR_TYPE_AAAA;
5406 qinfo.local_alias = NULL;
5407 if(verbosity >= VERB_ALGO) {
5409 char buf2[LDNS_MAX_DOMAINLEN+1];
5410 dname_str(xfr->name, buf2);
5411 snprintf(buf1, sizeof(buf1), "auth zone %s: master lookup"
5412 " for task_transfer", buf2);
5413 log_query_info(VERB_ALGO, buf1, &qinfo);
5415 edns.edns_present = 1;
5417 edns.edns_version = 0;
5418 edns.bits = EDNS_DO;
5419 edns.opt_list_in = NULL;
5420 edns.opt_list_out = NULL;
5421 edns.opt_list_inplace_cb_out = NULL;
5422 edns.padding_block_size = 0;
5423 edns.cookie_present = 0;
5424 edns.cookie_valid = 0;
5425 if(sldns_buffer_capacity(buf) < 65535)
5426 edns.udp_size = (uint16_t)sldns_buffer_capacity(buf);
5427 else edns.udp_size = 65535;
5429 /* unlock xfr during mesh_new_callback() because the callback can be
5430 * called straight away */
5431 lock_basic_unlock(&xfr->lock);
5432 if(!mesh_new_callback(env->mesh, &qinfo, qflags, &edns, buf, 0,
5433 &auth_xfer_transfer_lookup_callback, xfr, 0)) {
5434 lock_basic_lock(&xfr->lock);
5435 log_err("out of memory lookup up master %s", master->host);
5438 lock_basic_lock(&xfr->lock);
5442 /** initiate TCP to the target and fetch zone.
5443 * returns true if that was successfully started, and timeout setup. */
5445 xfr_transfer_init_fetch(struct auth_xfer* xfr, struct module_env* env)
5447 struct sockaddr_storage addr;
5448 socklen_t addrlen = 0;
5449 struct auth_master* master = xfr->task_transfer->master;
5450 char *auth_name = NULL;
5453 if(!master) return 0;
5454 if(master->allow_notify) return 0; /* only for notify */
5456 /* get master addr */
5457 if(xfr->task_transfer->scan_addr) {
5458 addrlen = xfr->task_transfer->scan_addr->addrlen;
5459 memmove(&addr, &xfr->task_transfer->scan_addr->addr, addrlen);
5461 if(!authextstrtoaddr(master->host, &addr, &addrlen, &auth_name)) {
5462 /* the ones that are not in addr format are supposed
5463 * to be looked up. The lookup has failed however,
5466 dname_str(xfr->name, zname);
5467 log_err("%s: failed lookup, cannot transfer from master %s",
5468 zname, master->host);
5473 /* remove previous TCP connection (if any) */
5474 if(xfr->task_transfer->cp) {
5475 comm_point_delete(xfr->task_transfer->cp);
5476 xfr->task_transfer->cp = NULL;
5478 if(!xfr->task_transfer->timer) {
5479 xfr->task_transfer->timer = comm_timer_create(env->worker_base,
5480 auth_xfer_transfer_timer_callback, xfr);
5481 if(!xfr->task_transfer->timer) {
5482 log_err("malloc failure");
5486 timeout = AUTH_TRANSFER_TIMEOUT;
5488 t.tv_sec = timeout/1000;
5489 t.tv_usec = (timeout%1000)*1000;
5493 /* perform http fetch */
5494 /* store http port number into sockaddr,
5495 * unless someone used unbound's host@port notation */
5496 xfr->task_transfer->on_ixfr = 0;
5497 if(strchr(master->host, '@') == NULL)
5498 sockaddr_store_port(&addr, addrlen, master->port);
5499 xfr->task_transfer->cp = outnet_comm_point_for_http(
5500 env->outnet, auth_xfer_transfer_http_callback, xfr,
5501 &addr, addrlen, -1, master->ssl, master->host,
5502 master->file, env->cfg);
5503 if(!xfr->task_transfer->cp) {
5504 char zname[255+1], as[256];
5505 dname_str(xfr->name, zname);
5506 addr_to_str(&addr, addrlen, as, sizeof(as));
5507 verbose(VERB_ALGO, "cannot create http cp "
5508 "connection for %s to %s", zname, as);
5511 comm_timer_set(xfr->task_transfer->timer, &t);
5512 if(verbosity >= VERB_ALGO) {
5513 char zname[255+1], as[256];
5514 dname_str(xfr->name, zname);
5515 addr_to_str(&addr, addrlen, as, sizeof(as));
5516 verbose(VERB_ALGO, "auth zone %s transfer next HTTP fetch from %s started", zname, as);
5518 /* Create or refresh the list of allow_notify addrs */
5519 probe_copy_masters_for_allow_notify(xfr);
5523 /* perform AXFR/IXFR */
5524 /* set the packet to be written */
5526 xfr->task_transfer->id = GET_RANDOM_ID(env->rnd);
5527 xfr_create_ixfr_packet(xfr, env->scratch_buffer,
5528 xfr->task_transfer->id, master);
5531 xfr->task_transfer->cp = outnet_comm_point_for_tcp(env->outnet,
5532 auth_xfer_transfer_tcp_callback, xfr, &addr, addrlen,
5533 env->scratch_buffer, -1,
5534 auth_name != NULL, auth_name);
5535 if(!xfr->task_transfer->cp) {
5536 char zname[255+1], as[256];
5537 dname_str(xfr->name, zname);
5538 addr_to_str(&addr, addrlen, as, sizeof(as));
5539 verbose(VERB_ALGO, "cannot create tcp cp connection for "
5540 "xfr %s to %s", zname, as);
5543 comm_timer_set(xfr->task_transfer->timer, &t);
5544 if(verbosity >= VERB_ALGO) {
5545 char zname[255+1], as[256];
5546 dname_str(xfr->name, zname);
5547 addr_to_str(&addr, addrlen, as, sizeof(as));
5548 verbose(VERB_ALGO, "auth zone %s transfer next %s fetch from %s started", zname,
5549 (xfr->task_transfer->on_ixfr?"IXFR":"AXFR"), as);
5554 /** perform next lookup, next transfer TCP, or end and resume wait time task */
5556 xfr_transfer_nexttarget_or_end(struct auth_xfer* xfr, struct module_env* env)
5558 log_assert(xfr->task_transfer->worker == env->worker);
5560 /* are we performing lookups? */
5561 while(xfr->task_transfer->lookup_target) {
5562 if(xfr_transfer_lookup_host(xfr, env)) {
5563 /* wait for lookup to finish,
5564 * note that the hostname may be in unbound's cache
5565 * and we may then get an instant cache response,
5566 * and that calls the callback just like a full
5567 * lookup and lookup failures also call callback */
5568 if(verbosity >= VERB_ALGO) {
5570 dname_str(xfr->name, zname);
5571 verbose(VERB_ALGO, "auth zone %s transfer next target lookup", zname);
5573 lock_basic_unlock(&xfr->lock);
5576 xfr_transfer_move_to_next_lookup(xfr, env);
5579 /* initiate TCP and fetch the zone from the master */
5580 /* and set timeout on it */
5581 while(!xfr_transfer_end_of_list(xfr)) {
5582 xfr->task_transfer->master = xfr_transfer_current_master(xfr);
5583 if(xfr_transfer_init_fetch(xfr, env)) {
5584 /* successfully started, wait for callback */
5585 lock_basic_unlock(&xfr->lock);
5588 /* failed to fetch, next master */
5589 xfr_transfer_nextmaster(xfr);
5591 if(verbosity >= VERB_ALGO) {
5593 dname_str(xfr->name, zname);
5594 verbose(VERB_ALGO, "auth zone %s transfer failed, wait", zname);
5597 /* we failed to fetch the zone, move to wait task
5598 * use the shorter retry timeout */
5599 xfr_transfer_disown(xfr);
5601 /* pick up the nextprobe task and wait */
5602 if(xfr->task_nextprobe->worker == NULL)
5603 xfr_set_timeout(xfr, env, 1, 0);
5604 lock_basic_unlock(&xfr->lock);
5607 /** add addrs from A or AAAA rrset to the master */
5609 xfr_master_add_addrs(struct auth_master* m, struct ub_packed_rrset_key* rrset,
5613 struct packed_rrset_data* data;
5614 if(!m || !rrset) return;
5615 if(rrtype != LDNS_RR_TYPE_A && rrtype != LDNS_RR_TYPE_AAAA)
5617 data = (struct packed_rrset_data*)rrset->entry.data;
5618 for(i=0; i<data->count; i++) {
5619 struct auth_addr* a;
5620 size_t len = data->rr_len[i] - 2;
5621 uint8_t* rdata = data->rr_data[i]+2;
5622 if(rrtype == LDNS_RR_TYPE_A && len != INET_SIZE)
5623 continue; /* wrong length for A */
5624 if(rrtype == LDNS_RR_TYPE_AAAA && len != INET6_SIZE)
5625 continue; /* wrong length for AAAA */
5627 /* add and alloc it */
5628 a = (struct auth_addr*)calloc(1, sizeof(*a));
5630 log_err("out of memory");
5633 if(rrtype == LDNS_RR_TYPE_A) {
5634 struct sockaddr_in* sa;
5635 a->addrlen = (socklen_t)sizeof(*sa);
5636 sa = (struct sockaddr_in*)&a->addr;
5637 sa->sin_family = AF_INET;
5638 sa->sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
5639 memmove(&sa->sin_addr, rdata, INET_SIZE);
5641 struct sockaddr_in6* sa;
5642 a->addrlen = (socklen_t)sizeof(*sa);
5643 sa = (struct sockaddr_in6*)&a->addr;
5644 sa->sin6_family = AF_INET6;
5645 sa->sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
5646 memmove(&sa->sin6_addr, rdata, INET6_SIZE);
5648 if(verbosity >= VERB_ALGO) {
5650 addr_to_str(&a->addr, a->addrlen, s, sizeof(s));
5651 verbose(VERB_ALGO, "auth host %s lookup %s",
5654 /* append to list */
5660 /** callback for task_transfer lookup of host name, of A or AAAA */
5661 void auth_xfer_transfer_lookup_callback(void* arg, int rcode, sldns_buffer* buf,
5662 enum sec_status ATTR_UNUSED(sec), char* ATTR_UNUSED(why_bogus),
5663 int ATTR_UNUSED(was_ratelimited))
5665 struct auth_xfer* xfr = (struct auth_xfer*)arg;
5666 struct module_env* env;
5667 log_assert(xfr->task_transfer);
5668 lock_basic_lock(&xfr->lock);
5669 env = xfr->task_transfer->env;
5670 if(!env || env->outnet->want_to_quit) {
5671 lock_basic_unlock(&xfr->lock);
5672 return; /* stop on quit */
5675 /* process result */
5676 if(rcode == LDNS_RCODE_NOERROR) {
5677 uint16_t wanted_qtype = LDNS_RR_TYPE_A;
5678 struct regional* temp = env->scratch;
5679 struct query_info rq;
5680 struct reply_info* rep;
5681 if(xfr->task_transfer->lookup_aaaa)
5682 wanted_qtype = LDNS_RR_TYPE_AAAA;
5683 memset(&rq, 0, sizeof(rq));
5684 rep = parse_reply_in_temp_region(buf, temp, &rq);
5685 if(rep && rq.qtype == wanted_qtype &&
5686 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR) {
5687 /* parsed successfully */
5688 struct ub_packed_rrset_key* answer =
5689 reply_find_answer_rrset(&rq, rep);
5691 xfr_master_add_addrs(xfr->task_transfer->
5692 lookup_target, answer, wanted_qtype);
5694 if(verbosity >= VERB_ALGO) {
5696 dname_str(xfr->name, zname);
5697 verbose(VERB_ALGO, "auth zone %s host %s type %s transfer lookup has nodata", zname, xfr->task_transfer->lookup_target->host, (xfr->task_transfer->lookup_aaaa?"AAAA":"A"));
5701 if(verbosity >= VERB_ALGO) {
5703 dname_str(xfr->name, zname);
5704 verbose(VERB_ALGO, "auth zone %s host %s type %s transfer lookup has no answer", zname, xfr->task_transfer->lookup_target->host, (xfr->task_transfer->lookup_aaaa?"AAAA":"A"));
5707 regional_free_all(temp);
5709 if(verbosity >= VERB_ALGO) {
5711 dname_str(xfr->name, zname);
5712 verbose(VERB_ALGO, "auth zone %s host %s type %s transfer lookup failed", zname, xfr->task_transfer->lookup_target->host, (xfr->task_transfer->lookup_aaaa?"AAAA":"A"));
5715 if(xfr->task_transfer->lookup_target->list &&
5716 xfr->task_transfer->lookup_target == xfr_transfer_current_master(xfr))
5717 xfr->task_transfer->scan_addr = xfr->task_transfer->lookup_target->list;
5719 /* move to lookup AAAA after A lookup, move to next hostname lookup,
5720 * or move to fetch the zone, or, if nothing to do, end task_transfer */
5721 xfr_transfer_move_to_next_lookup(xfr, env);
5722 xfr_transfer_nexttarget_or_end(xfr, env);
5725 /** check if xfer (AXFR or IXFR) packet is OK.
5726 * return false if we lost connection (SERVFAIL, or unreadable).
5727 * return false if we need to move from IXFR to AXFR, with gonextonfail
5728 * set to false, so the same master is tried again, but with AXFR.
5729 * return true if fine to link into data.
5730 * return true with transferdone=true when the transfer has ended.
5733 check_xfer_packet(sldns_buffer* pkt, struct auth_xfer* xfr,
5734 int* gonextonfail, int* transferdone)
5736 uint8_t* wire = sldns_buffer_begin(pkt);
5738 if(sldns_buffer_limit(pkt) < LDNS_HEADER_SIZE) {
5739 verbose(VERB_ALGO, "xfr to %s failed, packet too small",
5740 xfr->task_transfer->master->host);
5743 if(!LDNS_QR_WIRE(wire)) {
5744 verbose(VERB_ALGO, "xfr to %s failed, packet has no QR flag",
5745 xfr->task_transfer->master->host);
5748 if(LDNS_TC_WIRE(wire)) {
5749 verbose(VERB_ALGO, "xfr to %s failed, packet has TC flag",
5750 xfr->task_transfer->master->host);
5754 if(LDNS_ID_WIRE(wire) != xfr->task_transfer->id) {
5755 verbose(VERB_ALGO, "xfr to %s failed, packet wrong ID",
5756 xfr->task_transfer->master->host);
5759 if(LDNS_RCODE_WIRE(wire) != LDNS_RCODE_NOERROR) {
5761 sldns_wire2str_rcode_buf((int)LDNS_RCODE_WIRE(wire), rcode,
5763 /* if we are doing IXFR, check for fallback */
5764 if(xfr->task_transfer->on_ixfr) {
5765 if(LDNS_RCODE_WIRE(wire) == LDNS_RCODE_NOTIMPL ||
5766 LDNS_RCODE_WIRE(wire) == LDNS_RCODE_SERVFAIL ||
5767 LDNS_RCODE_WIRE(wire) == LDNS_RCODE_REFUSED ||
5768 LDNS_RCODE_WIRE(wire) == LDNS_RCODE_FORMERR) {
5769 verbose(VERB_ALGO, "xfr to %s, fallback "
5770 "from IXFR to AXFR (with rcode %s)",
5771 xfr->task_transfer->master->host,
5773 xfr->task_transfer->ixfr_fail = 1;
5778 verbose(VERB_ALGO, "xfr to %s failed, packet with rcode %s",
5779 xfr->task_transfer->master->host, rcode);
5782 if(LDNS_OPCODE_WIRE(wire) != LDNS_PACKET_QUERY) {
5783 verbose(VERB_ALGO, "xfr to %s failed, packet with bad opcode",
5784 xfr->task_transfer->master->host);
5787 if(LDNS_QDCOUNT(wire) > 1) {
5788 verbose(VERB_ALGO, "xfr to %s failed, packet has qdcount %d",
5789 xfr->task_transfer->master->host,
5790 (int)LDNS_QDCOUNT(wire));
5795 sldns_buffer_set_position(pkt, LDNS_HEADER_SIZE);
5796 for(i=0; i<(int)LDNS_QDCOUNT(wire); i++) {
5797 size_t pos = sldns_buffer_position(pkt);
5798 uint16_t qtype, qclass;
5799 if(pkt_dname_len(pkt) == 0) {
5800 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5802 xfr->task_transfer->master->host);
5805 if(dname_pkt_compare(pkt, sldns_buffer_at(pkt, pos),
5807 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5809 xfr->task_transfer->master->host);
5812 if(sldns_buffer_remaining(pkt) < 4) {
5813 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5814 "truncated query RR",
5815 xfr->task_transfer->master->host);
5818 qtype = sldns_buffer_read_u16(pkt);
5819 qclass = sldns_buffer_read_u16(pkt);
5820 if(qclass != xfr->dclass) {
5821 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5823 xfr->task_transfer->master->host);
5826 if(xfr->task_transfer->on_ixfr) {
5827 if(qtype != LDNS_RR_TYPE_IXFR) {
5828 verbose(VERB_ALGO, "xfr to %s failed, packet "
5829 "with wrong qtype, expected IXFR",
5830 xfr->task_transfer->master->host);
5834 if(qtype != LDNS_RR_TYPE_AXFR) {
5835 verbose(VERB_ALGO, "xfr to %s failed, packet "
5836 "with wrong qtype, expected AXFR",
5837 xfr->task_transfer->master->host);
5843 /* check parse of RRs in packet, store first SOA serial
5844 * to be able to detect last SOA (with that serial) to see if done */
5845 /* also check for IXFR 'zone up to date' reply */
5846 for(i=0; i<(int)LDNS_ANCOUNT(wire); i++) {
5847 size_t pos = sldns_buffer_position(pkt);
5849 if(pkt_dname_len(pkt) == 0) {
5850 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5851 "malformed dname in answer section",
5852 xfr->task_transfer->master->host);
5855 if(sldns_buffer_remaining(pkt) < 10) {
5856 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5858 xfr->task_transfer->master->host);
5861 tp = sldns_buffer_read_u16(pkt);
5862 (void)sldns_buffer_read_u16(pkt); /* class */
5863 (void)sldns_buffer_read_u32(pkt); /* ttl */
5864 rdlen = sldns_buffer_read_u16(pkt);
5865 if(sldns_buffer_remaining(pkt) < rdlen) {
5866 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5867 "truncated RR rdata",
5868 xfr->task_transfer->master->host);
5872 /* RR parses (haven't checked rdata itself), now look at
5873 * SOA records to see serial number */
5874 if(xfr->task_transfer->rr_scan_num == 0 &&
5875 tp != LDNS_RR_TYPE_SOA) {
5876 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5877 "malformed zone transfer, no start SOA",
5878 xfr->task_transfer->master->host);
5881 if(xfr->task_transfer->rr_scan_num == 1 &&
5882 tp != LDNS_RR_TYPE_SOA) {
5883 /* second RR is not a SOA record, this is not an IXFR
5884 * the master is replying with an AXFR */
5885 xfr->task_transfer->on_ixfr_is_axfr = 1;
5887 if(tp == LDNS_RR_TYPE_SOA) {
5890 verbose(VERB_ALGO, "xfr to %s failed, packet "
5891 "with SOA with malformed rdata",
5892 xfr->task_transfer->master->host);
5895 if(dname_pkt_compare(pkt, sldns_buffer_at(pkt, pos),
5897 verbose(VERB_ALGO, "xfr to %s failed, packet "
5898 "with SOA with wrong dname",
5899 xfr->task_transfer->master->host);
5903 /* read serial number of SOA */
5904 serial = sldns_buffer_read_u32_at(pkt,
5905 sldns_buffer_position(pkt)+rdlen-20);
5907 /* check for IXFR 'zone has SOA x' reply */
5908 if(xfr->task_transfer->on_ixfr &&
5909 xfr->task_transfer->rr_scan_num == 0 &&
5910 LDNS_ANCOUNT(wire)==1) {
5911 verbose(VERB_ALGO, "xfr to %s ended, "
5912 "IXFR reply that zone has serial %u,"
5913 " fallback from IXFR to AXFR",
5914 xfr->task_transfer->master->host,
5916 xfr->task_transfer->ixfr_fail = 1;
5921 /* if first SOA, store serial number */
5922 if(xfr->task_transfer->got_xfr_serial == 0) {
5923 xfr->task_transfer->got_xfr_serial = 1;
5924 xfr->task_transfer->incoming_xfr_serial =
5926 verbose(VERB_ALGO, "xfr %s: contains "
5928 xfr->task_transfer->master->host,
5930 /* see if end of AXFR */
5931 } else if(!xfr->task_transfer->on_ixfr ||
5932 xfr->task_transfer->on_ixfr_is_axfr) {
5933 /* second SOA with serial is the end
5936 verbose(VERB_ALGO, "xfr %s: last AXFR packet",
5937 xfr->task_transfer->master->host);
5938 /* for IXFR, count SOA records with that serial */
5939 } else if(xfr->task_transfer->incoming_xfr_serial ==
5940 serial && xfr->task_transfer->got_xfr_serial
5942 xfr->task_transfer->got_xfr_serial++;
5943 /* if not first soa, if serial==firstserial, the
5944 * third time we are at the end, for IXFR */
5945 } else if(xfr->task_transfer->incoming_xfr_serial ==
5946 serial && xfr->task_transfer->got_xfr_serial
5948 verbose(VERB_ALGO, "xfr %s: last IXFR packet",
5949 xfr->task_transfer->master->host);
5951 /* continue parse check, if that succeeds,
5952 * transfer is done */
5955 xfr->task_transfer->rr_scan_num++;
5957 /* skip over RR rdata to go to the next RR */
5958 sldns_buffer_skip(pkt, (ssize_t)rdlen);
5961 /* check authority section */
5962 /* we skip over the RRs checking packet format */
5963 for(i=0; i<(int)LDNS_NSCOUNT(wire); i++) {
5965 if(pkt_dname_len(pkt) == 0) {
5966 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5967 "malformed dname in authority section",
5968 xfr->task_transfer->master->host);
5971 if(sldns_buffer_remaining(pkt) < 10) {
5972 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5974 xfr->task_transfer->master->host);
5977 (void)sldns_buffer_read_u16(pkt); /* type */
5978 (void)sldns_buffer_read_u16(pkt); /* class */
5979 (void)sldns_buffer_read_u32(pkt); /* ttl */
5980 rdlen = sldns_buffer_read_u16(pkt);
5981 if(sldns_buffer_remaining(pkt) < rdlen) {
5982 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5983 "truncated RR rdata",
5984 xfr->task_transfer->master->host);
5987 /* skip over RR rdata to go to the next RR */
5988 sldns_buffer_skip(pkt, (ssize_t)rdlen);
5991 /* check additional section */
5992 for(i=0; i<(int)LDNS_ARCOUNT(wire); i++) {
5994 if(pkt_dname_len(pkt) == 0) {
5995 verbose(VERB_ALGO, "xfr to %s failed, packet with "
5996 "malformed dname in additional section",
5997 xfr->task_transfer->master->host);
6000 if(sldns_buffer_remaining(pkt) < 10) {
6001 verbose(VERB_ALGO, "xfr to %s failed, packet with "
6003 xfr->task_transfer->master->host);
6006 (void)sldns_buffer_read_u16(pkt); /* type */
6007 (void)sldns_buffer_read_u16(pkt); /* class */
6008 (void)sldns_buffer_read_u32(pkt); /* ttl */
6009 rdlen = sldns_buffer_read_u16(pkt);
6010 if(sldns_buffer_remaining(pkt) < rdlen) {
6011 verbose(VERB_ALGO, "xfr to %s failed, packet with "
6012 "truncated RR rdata",
6013 xfr->task_transfer->master->host);
6016 /* skip over RR rdata to go to the next RR */
6017 sldns_buffer_skip(pkt, (ssize_t)rdlen);
6023 /** Link the data from this packet into the worklist of transferred data */
6025 xfer_link_data(sldns_buffer* pkt, struct auth_xfer* xfr)
6028 struct auth_chunk* e;
6029 e = (struct auth_chunk*)calloc(1, sizeof(*e));
6032 e->len = sldns_buffer_limit(pkt);
6033 e->data = memdup(sldns_buffer_begin(pkt), e->len);
6039 /* alloc succeeded, link into list */
6040 if(!xfr->task_transfer->chunks_first)
6041 xfr->task_transfer->chunks_first = e;
6042 if(xfr->task_transfer->chunks_last)
6043 xfr->task_transfer->chunks_last->next = e;
6044 xfr->task_transfer->chunks_last = e;
6048 /** task transfer. the list of data is complete. process it and if failed
6049 * move to next master, if succeeded, end the task transfer */
6051 process_list_end_transfer(struct auth_xfer* xfr, struct module_env* env)
6054 if(xfr_process_chunk_list(xfr, env, &ixfr_fail)) {
6056 auth_chunks_delete(xfr->task_transfer);
6058 /* we fetched the zone, move to wait task */
6059 xfr_transfer_disown(xfr);
6061 if(xfr->notify_received && (!xfr->notify_has_serial ||
6062 (xfr->notify_has_serial &&
6063 xfr_serial_means_update(xfr, xfr->notify_serial)))) {
6064 uint32_t sr = xfr->notify_serial;
6065 int has_sr = xfr->notify_has_serial;
6066 /* we received a notify while probe/transfer was
6067 * in progress. start a new probe and transfer */
6068 xfr->notify_received = 0;
6069 xfr->notify_has_serial = 0;
6070 xfr->notify_serial = 0;
6071 if(!xfr_start_probe(xfr, env, NULL)) {
6072 /* if we couldn't start it, already in
6073 * progress; restore notify serial,
6074 * while xfr still locked */
6075 xfr->notify_received = 1;
6076 xfr->notify_has_serial = has_sr;
6077 xfr->notify_serial = sr;
6078 lock_basic_unlock(&xfr->lock);
6082 /* pick up the nextprobe task and wait (normail wait time) */
6083 if(xfr->task_nextprobe->worker == NULL)
6084 xfr_set_timeout(xfr, env, 0, 0);
6086 lock_basic_unlock(&xfr->lock);
6089 /* processing failed */
6090 /* when done, delete data from list */
6091 auth_chunks_delete(xfr->task_transfer);
6093 xfr->task_transfer->ixfr_fail = 1;
6095 xfr_transfer_nextmaster(xfr);
6097 xfr_transfer_nexttarget_or_end(xfr, env);
6100 /** callback for the task_transfer timer */
6102 auth_xfer_transfer_timer_callback(void* arg)
6104 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6105 struct module_env* env;
6106 int gonextonfail = 1;
6107 log_assert(xfr->task_transfer);
6108 lock_basic_lock(&xfr->lock);
6109 env = xfr->task_transfer->env;
6110 if(!env || env->outnet->want_to_quit) {
6111 lock_basic_unlock(&xfr->lock);
6112 return; /* stop on quit */
6115 verbose(VERB_ALGO, "xfr stopped, connection timeout to %s",
6116 xfr->task_transfer->master->host);
6118 /* see if IXFR caused the failure, if so, try AXFR */
6119 if(xfr->task_transfer->on_ixfr) {
6120 xfr->task_transfer->ixfr_possible_timeout_count++;
6121 if(xfr->task_transfer->ixfr_possible_timeout_count >=
6122 NUM_TIMEOUTS_FALLBACK_IXFR) {
6123 verbose(VERB_ALGO, "xfr to %s, fallback "
6124 "from IXFR to AXFR (because of timeouts)",
6125 xfr->task_transfer->master->host);
6126 xfr->task_transfer->ixfr_fail = 1;
6131 /* delete transferred data from list */
6132 auth_chunks_delete(xfr->task_transfer);
6133 comm_point_delete(xfr->task_transfer->cp);
6134 xfr->task_transfer->cp = NULL;
6136 xfr_transfer_nextmaster(xfr);
6137 xfr_transfer_nexttarget_or_end(xfr, env);
6140 /** callback for task_transfer tcp connections */
6142 auth_xfer_transfer_tcp_callback(struct comm_point* c, void* arg, int err,
6143 struct comm_reply* ATTR_UNUSED(repinfo))
6145 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6146 struct module_env* env;
6147 int gonextonfail = 1;
6148 int transferdone = 0;
6149 log_assert(xfr->task_transfer);
6150 lock_basic_lock(&xfr->lock);
6151 env = xfr->task_transfer->env;
6152 if(!env || env->outnet->want_to_quit) {
6153 lock_basic_unlock(&xfr->lock);
6154 return 0; /* stop on quit */
6156 /* stop the timer */
6157 comm_timer_disable(xfr->task_transfer->timer);
6159 if(err != NETEVENT_NOERROR) {
6160 /* connection failed, closed, or timeout */
6161 /* stop this transfer, cleanup
6162 * and continue task_transfer*/
6163 verbose(VERB_ALGO, "xfr stopped, connection lost to %s",
6164 xfr->task_transfer->master->host);
6166 /* see if IXFR caused the failure, if so, try AXFR */
6167 if(xfr->task_transfer->on_ixfr) {
6168 xfr->task_transfer->ixfr_possible_timeout_count++;
6169 if(xfr->task_transfer->ixfr_possible_timeout_count >=
6170 NUM_TIMEOUTS_FALLBACK_IXFR) {
6171 verbose(VERB_ALGO, "xfr to %s, fallback "
6172 "from IXFR to AXFR (because of timeouts)",
6173 xfr->task_transfer->master->host);
6174 xfr->task_transfer->ixfr_fail = 1;
6180 /* delete transferred data from list */
6181 auth_chunks_delete(xfr->task_transfer);
6182 comm_point_delete(xfr->task_transfer->cp);
6183 xfr->task_transfer->cp = NULL;
6185 xfr_transfer_nextmaster(xfr);
6186 xfr_transfer_nexttarget_or_end(xfr, env);
6189 /* note that IXFR worked without timeout */
6190 if(xfr->task_transfer->on_ixfr)
6191 xfr->task_transfer->ixfr_possible_timeout_count = 0;
6193 /* handle returned packet */
6194 /* if it fails, cleanup and end this transfer */
6195 /* if it needs to fallback from IXFR to AXFR, do that */
6196 if(!check_xfer_packet(c->buffer, xfr, &gonextonfail, &transferdone)) {
6199 /* if it is good, link it into the list of data */
6200 /* if the link into list of data fails (malloc fail) cleanup and end */
6201 if(!xfer_link_data(c->buffer, xfr)) {
6202 verbose(VERB_ALGO, "xfr stopped to %s, malloc failed",
6203 xfr->task_transfer->master->host);
6206 /* if the transfer is done now, disconnect and process the list */
6208 comm_point_delete(xfr->task_transfer->cp);
6209 xfr->task_transfer->cp = NULL;
6210 process_list_end_transfer(xfr, env);
6214 /* if we want to read more messages, setup the commpoint to read
6215 * a DNS packet, and the timeout */
6216 lock_basic_unlock(&xfr->lock);
6217 c->tcp_is_reading = 1;
6218 sldns_buffer_clear(c->buffer);
6219 comm_point_start_listening(c, -1, AUTH_TRANSFER_TIMEOUT);
6223 /** callback for task_transfer http connections */
6225 auth_xfer_transfer_http_callback(struct comm_point* c, void* arg, int err,
6226 struct comm_reply* repinfo)
6228 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6229 struct module_env* env;
6230 log_assert(xfr->task_transfer);
6231 lock_basic_lock(&xfr->lock);
6232 env = xfr->task_transfer->env;
6233 if(!env || env->outnet->want_to_quit) {
6234 lock_basic_unlock(&xfr->lock);
6235 return 0; /* stop on quit */
6237 verbose(VERB_ALGO, "auth zone transfer http callback");
6238 /* stop the timer */
6239 comm_timer_disable(xfr->task_transfer->timer);
6241 if(err != NETEVENT_NOERROR && err != NETEVENT_DONE) {
6242 /* connection failed, closed, or timeout */
6243 /* stop this transfer, cleanup
6244 * and continue task_transfer*/
6245 verbose(VERB_ALGO, "http stopped, connection lost to %s",
6246 xfr->task_transfer->master->host);
6248 /* delete transferred data from list */
6249 auth_chunks_delete(xfr->task_transfer);
6250 if(repinfo) repinfo->c = NULL; /* signal cp deleted to
6251 the routine calling this callback */
6252 comm_point_delete(xfr->task_transfer->cp);
6253 xfr->task_transfer->cp = NULL;
6254 xfr_transfer_nextmaster(xfr);
6255 xfr_transfer_nexttarget_or_end(xfr, env);
6259 /* if it is good, link it into the list of data */
6260 /* if the link into list of data fails (malloc fail) cleanup and end */
6261 if(sldns_buffer_limit(c->buffer) > 0) {
6262 verbose(VERB_ALGO, "auth zone http queued up %d bytes",
6263 (int)sldns_buffer_limit(c->buffer));
6264 if(!xfer_link_data(c->buffer, xfr)) {
6265 verbose(VERB_ALGO, "http stopped to %s, malloc failed",
6266 xfr->task_transfer->master->host);
6270 /* if the transfer is done now, disconnect and process the list */
6271 if(err == NETEVENT_DONE) {
6272 if(repinfo) repinfo->c = NULL; /* signal cp deleted to
6273 the routine calling this callback */
6274 comm_point_delete(xfr->task_transfer->cp);
6275 xfr->task_transfer->cp = NULL;
6276 process_list_end_transfer(xfr, env);
6280 /* if we want to read more messages, setup the commpoint to read
6281 * a DNS packet, and the timeout */
6282 lock_basic_unlock(&xfr->lock);
6283 c->tcp_is_reading = 1;
6284 sldns_buffer_clear(c->buffer);
6285 comm_point_start_listening(c, -1, AUTH_TRANSFER_TIMEOUT);
6290 /** start transfer task by this worker , xfr is locked. */
6292 xfr_start_transfer(struct auth_xfer* xfr, struct module_env* env,
6293 struct auth_master* master)
6295 log_assert(xfr->task_transfer != NULL);
6296 log_assert(xfr->task_transfer->worker == NULL);
6297 log_assert(xfr->task_transfer->chunks_first == NULL);
6298 log_assert(xfr->task_transfer->chunks_last == NULL);
6299 xfr->task_transfer->worker = env->worker;
6300 xfr->task_transfer->env = env;
6302 /* init transfer process */
6303 /* find that master in the transfer's list of masters? */
6304 xfr_transfer_start_list(xfr, master);
6305 /* start lookup for hostnames in transfer master list */
6306 xfr_transfer_start_lookups(xfr);
6308 /* initiate TCP, and set timeout on it */
6309 xfr_transfer_nexttarget_or_end(xfr, env);
6312 /** disown task_probe. caller must hold xfr.lock */
6314 xfr_probe_disown(struct auth_xfer* xfr)
6316 /* remove timer (from this worker's event base) */
6317 comm_timer_delete(xfr->task_probe->timer);
6318 xfr->task_probe->timer = NULL;
6319 /* remove the commpoint */
6320 comm_point_delete(xfr->task_probe->cp);
6321 xfr->task_probe->cp = NULL;
6322 /* we don't own this item anymore */
6323 xfr->task_probe->worker = NULL;
6324 xfr->task_probe->env = NULL;
6327 /** send the UDP probe to the master, this is part of task_probe */
6329 xfr_probe_send_probe(struct auth_xfer* xfr, struct module_env* env,
6332 struct sockaddr_storage addr;
6333 socklen_t addrlen = 0;
6336 struct auth_master* master = xfr_probe_current_master(xfr);
6337 char *auth_name = NULL;
6338 if(!master) return 0;
6339 if(master->allow_notify) return 0; /* only for notify */
6340 if(master->http) return 0; /* only masters get SOA UDP probe,
6341 not urls, if those are in this list */
6343 /* get master addr */
6344 if(xfr->task_probe->scan_addr) {
6345 addrlen = xfr->task_probe->scan_addr->addrlen;
6346 memmove(&addr, &xfr->task_probe->scan_addr->addr, addrlen);
6348 if(!authextstrtoaddr(master->host, &addr, &addrlen, &auth_name)) {
6349 /* the ones that are not in addr format are supposed
6350 * to be looked up. The lookup has failed however,
6353 dname_str(xfr->name, zname);
6354 log_err("%s: failed lookup, cannot probe to master %s",
6355 zname, master->host);
6358 if (auth_name != NULL) {
6359 if (addr.ss_family == AF_INET
6360 && (int)ntohs(((struct sockaddr_in *)&addr)->sin_port)
6361 == env->cfg->ssl_port)
6362 ((struct sockaddr_in *)&addr)->sin_port
6363 = htons((uint16_t)env->cfg->port);
6364 else if (addr.ss_family == AF_INET6
6365 && (int)ntohs(((struct sockaddr_in6 *)&addr)->sin6_port)
6366 == env->cfg->ssl_port)
6367 ((struct sockaddr_in6 *)&addr)->sin6_port
6368 = htons((uint16_t)env->cfg->port);
6373 /* create new ID for new probes, but not on timeout retries,
6374 * this means we'll accept replies to previous retries to same ip */
6375 if(timeout == AUTH_PROBE_TIMEOUT)
6376 xfr->task_probe->id = GET_RANDOM_ID(env->rnd);
6377 xfr_create_soa_probe_packet(xfr, env->scratch_buffer,
6378 xfr->task_probe->id);
6379 /* we need to remove the cp if we have a different ip4/ip6 type now */
6380 if(xfr->task_probe->cp &&
6381 ((xfr->task_probe->cp_is_ip6 && !addr_is_ip6(&addr, addrlen)) ||
6382 (!xfr->task_probe->cp_is_ip6 && addr_is_ip6(&addr, addrlen)))
6384 comm_point_delete(xfr->task_probe->cp);
6385 xfr->task_probe->cp = NULL;
6387 if(!xfr->task_probe->cp) {
6388 if(addr_is_ip6(&addr, addrlen))
6389 xfr->task_probe->cp_is_ip6 = 1;
6390 else xfr->task_probe->cp_is_ip6 = 0;
6391 xfr->task_probe->cp = outnet_comm_point_for_udp(env->outnet,
6392 auth_xfer_probe_udp_callback, xfr, &addr, addrlen);
6393 if(!xfr->task_probe->cp) {
6394 char zname[255+1], as[256];
6395 dname_str(xfr->name, zname);
6396 addr_to_str(&addr, addrlen, as, sizeof(as));
6397 verbose(VERB_ALGO, "cannot create udp cp for "
6398 "probe %s to %s", zname, as);
6402 if(!xfr->task_probe->timer) {
6403 xfr->task_probe->timer = comm_timer_create(env->worker_base,
6404 auth_xfer_probe_timer_callback, xfr);
6405 if(!xfr->task_probe->timer) {
6406 log_err("malloc failure");
6411 /* send udp packet */
6412 if(!comm_point_send_udp_msg(xfr->task_probe->cp, env->scratch_buffer,
6413 (struct sockaddr*)&addr, addrlen, 0)) {
6414 char zname[255+1], as[256];
6415 dname_str(xfr->name, zname);
6416 addr_to_str(&addr, addrlen, as, sizeof(as));
6417 verbose(VERB_ALGO, "failed to send soa probe for %s to %s",
6421 if(verbosity >= VERB_ALGO) {
6422 char zname[255+1], as[256];
6423 dname_str(xfr->name, zname);
6424 addr_to_str(&addr, addrlen, as, sizeof(as));
6425 verbose(VERB_ALGO, "auth zone %s soa probe sent to %s", zname,
6428 xfr->task_probe->timeout = timeout;
6430 t.tv_sec = timeout/1000;
6431 t.tv_usec = (timeout%1000)*1000;
6433 comm_timer_set(xfr->task_probe->timer, &t);
6438 /** callback for task_probe timer */
6440 auth_xfer_probe_timer_callback(void* arg)
6442 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6443 struct module_env* env;
6444 log_assert(xfr->task_probe);
6445 lock_basic_lock(&xfr->lock);
6446 env = xfr->task_probe->env;
6447 if(!env || env->outnet->want_to_quit) {
6448 lock_basic_unlock(&xfr->lock);
6449 return; /* stop on quit */
6452 if(verbosity >= VERB_ALGO) {
6454 dname_str(xfr->name, zname);
6455 verbose(VERB_ALGO, "auth zone %s soa probe timeout", zname);
6457 if(xfr->task_probe->timeout <= AUTH_PROBE_TIMEOUT_STOP) {
6458 /* try again with bigger timeout */
6459 if(xfr_probe_send_probe(xfr, env, xfr->task_probe->timeout*2)) {
6460 lock_basic_unlock(&xfr->lock);
6464 /* delete commpoint so a new one is created, with a fresh port nr */
6465 comm_point_delete(xfr->task_probe->cp);
6466 xfr->task_probe->cp = NULL;
6468 /* too many timeouts (or fail to send), move to next or end */
6469 xfr_probe_nextmaster(xfr);
6470 xfr_probe_send_or_end(xfr, env);
6473 /** callback for task_probe udp packets */
6475 auth_xfer_probe_udp_callback(struct comm_point* c, void* arg, int err,
6476 struct comm_reply* repinfo)
6478 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6479 struct module_env* env;
6480 log_assert(xfr->task_probe);
6481 lock_basic_lock(&xfr->lock);
6482 env = xfr->task_probe->env;
6483 if(!env || env->outnet->want_to_quit) {
6484 lock_basic_unlock(&xfr->lock);
6485 return 0; /* stop on quit */
6488 /* the comm_point_udp_callback is in a for loop for NUM_UDP_PER_SELECT
6489 * and we set rep.c=NULL to stop if from looking inside the commpoint*/
6491 /* stop the timer */
6492 comm_timer_disable(xfr->task_probe->timer);
6494 /* see if we got a packet and what that means */
6495 if(err == NETEVENT_NOERROR) {
6496 uint32_t serial = 0;
6497 if(check_packet_ok(c->buffer, LDNS_RR_TYPE_SOA, xfr,
6499 /* successful lookup */
6500 if(verbosity >= VERB_ALGO) {
6502 dname_str(xfr->name, buf);
6503 verbose(VERB_ALGO, "auth zone %s: soa probe "
6504 "serial is %u", buf, (unsigned)serial);
6506 /* see if this serial indicates that the zone has
6508 if(xfr_serial_means_update(xfr, serial)) {
6509 /* if updated, start the transfer task, if needed */
6510 verbose(VERB_ALGO, "auth_zone updated, start transfer");
6511 if(xfr->task_transfer->worker == NULL) {
6512 struct auth_master* master =
6513 xfr_probe_current_master(xfr);
6514 /* if we have download URLs use them
6515 * in preference to this master we
6516 * just probed the SOA from */
6517 if(xfr->task_transfer->masters &&
6518 xfr->task_transfer->masters->http)
6520 xfr_probe_disown(xfr);
6521 xfr_start_transfer(xfr, env, master);
6525 /* other tasks are running, we don't do this anymore */
6526 xfr_probe_disown(xfr);
6527 lock_basic_unlock(&xfr->lock);
6528 /* return, we don't sent a reply to this udp packet,
6529 * and we setup the tasks to do next */
6532 verbose(VERB_ALGO, "auth_zone master reports unchanged soa serial");
6533 /* we if cannot find updates amongst the
6534 * masters, this means we then have a new lease
6536 xfr->task_probe->have_new_lease = 1;
6539 if(verbosity >= VERB_ALGO) {
6541 dname_str(xfr->name, buf);
6542 verbose(VERB_ALGO, "auth zone %s: bad reply to soa probe", buf);
6546 if(verbosity >= VERB_ALGO) {
6548 dname_str(xfr->name, buf);
6549 verbose(VERB_ALGO, "auth zone %s: soa probe failed", buf);
6553 /* failed lookup or not an update */
6554 /* delete commpoint so a new one is created, with a fresh port nr */
6555 comm_point_delete(xfr->task_probe->cp);
6556 xfr->task_probe->cp = NULL;
6558 /* if the result was not a successful probe, we need
6559 * to send the next one */
6560 xfr_probe_nextmaster(xfr);
6561 xfr_probe_send_or_end(xfr, env);
6565 /** lookup a host name for its addresses, if needed */
6567 xfr_probe_lookup_host(struct auth_xfer* xfr, struct module_env* env)
6569 struct sockaddr_storage addr;
6570 socklen_t addrlen = 0;
6571 struct auth_master* master = xfr->task_probe->lookup_target;
6572 struct query_info qinfo;
6573 uint16_t qflags = BIT_RD;
6574 uint8_t dname[LDNS_MAX_DOMAINLEN+1];
6575 struct edns_data edns;
6576 sldns_buffer* buf = env->scratch_buffer;
6577 if(!master) return 0;
6578 if(extstrtoaddr(master->host, &addr, &addrlen, UNBOUND_DNS_PORT)) {
6579 /* not needed, host is in IP addr format */
6582 if(master->allow_notify && !master->http &&
6583 strchr(master->host, '/') != NULL &&
6584 strchr(master->host, '/') == strrchr(master->host, '/')) {
6585 return 0; /* is IP/prefix format, not something to look up */
6588 /* use mesh_new_callback to probe for non-addr hosts,
6589 * and then wait for them to be looked up (in cache, or query) */
6590 qinfo.qname_len = sizeof(dname);
6591 if(sldns_str2wire_dname_buf(master->host, dname, &qinfo.qname_len)
6593 log_err("cannot parse host name of master %s", master->host);
6596 qinfo.qname = dname;
6597 qinfo.qclass = xfr->dclass;
6598 qinfo.qtype = LDNS_RR_TYPE_A;
6599 if(xfr->task_probe->lookup_aaaa)
6600 qinfo.qtype = LDNS_RR_TYPE_AAAA;
6601 qinfo.local_alias = NULL;
6602 if(verbosity >= VERB_ALGO) {
6604 char buf2[LDNS_MAX_DOMAINLEN+1];
6605 dname_str(xfr->name, buf2);
6606 snprintf(buf1, sizeof(buf1), "auth zone %s: master lookup"
6607 " for task_probe", buf2);
6608 log_query_info(VERB_ALGO, buf1, &qinfo);
6610 edns.edns_present = 1;
6612 edns.edns_version = 0;
6613 edns.bits = EDNS_DO;
6614 edns.opt_list_in = NULL;
6615 edns.opt_list_out = NULL;
6616 edns.opt_list_inplace_cb_out = NULL;
6617 edns.padding_block_size = 0;
6618 edns.cookie_present = 0;
6619 edns.cookie_valid = 0;
6620 if(sldns_buffer_capacity(buf) < 65535)
6621 edns.udp_size = (uint16_t)sldns_buffer_capacity(buf);
6622 else edns.udp_size = 65535;
6624 /* unlock xfr during mesh_new_callback() because the callback can be
6625 * called straight away */
6626 lock_basic_unlock(&xfr->lock);
6627 if(!mesh_new_callback(env->mesh, &qinfo, qflags, &edns, buf, 0,
6628 &auth_xfer_probe_lookup_callback, xfr, 0)) {
6629 lock_basic_lock(&xfr->lock);
6630 log_err("out of memory lookup up master %s", master->host);
6633 lock_basic_lock(&xfr->lock);
6637 /** move to sending the probe packets, next if fails. task_probe */
6639 xfr_probe_send_or_end(struct auth_xfer* xfr, struct module_env* env)
6641 /* are we doing hostname lookups? */
6642 while(xfr->task_probe->lookup_target) {
6643 if(xfr_probe_lookup_host(xfr, env)) {
6644 /* wait for lookup to finish,
6645 * note that the hostname may be in unbound's cache
6646 * and we may then get an instant cache response,
6647 * and that calls the callback just like a full
6648 * lookup and lookup failures also call callback */
6649 if(verbosity >= VERB_ALGO) {
6651 dname_str(xfr->name, zname);
6652 verbose(VERB_ALGO, "auth zone %s probe next target lookup", zname);
6654 lock_basic_unlock(&xfr->lock);
6657 xfr_probe_move_to_next_lookup(xfr, env);
6659 /* probe of list has ended. Create or refresh the list of of
6660 * allow_notify addrs */
6661 probe_copy_masters_for_allow_notify(xfr);
6662 if(verbosity >= VERB_ALGO) {
6664 dname_str(xfr->name, zname);
6665 verbose(VERB_ALGO, "auth zone %s probe: notify addrs updated", zname);
6667 if(xfr->task_probe->only_lookup) {
6668 /* only wanted lookups for copy, stop probe and start wait */
6669 xfr->task_probe->only_lookup = 0;
6670 if(verbosity >= VERB_ALGO) {
6672 dname_str(xfr->name, zname);
6673 verbose(VERB_ALGO, "auth zone %s probe: finished only_lookup", zname);
6675 xfr_probe_disown(xfr);
6676 if(xfr->task_nextprobe->worker == NULL)
6677 xfr_set_timeout(xfr, env, 0, 0);
6678 lock_basic_unlock(&xfr->lock);
6682 /* send probe packets */
6683 while(!xfr_probe_end_of_list(xfr)) {
6684 if(xfr_probe_send_probe(xfr, env, AUTH_PROBE_TIMEOUT)) {
6685 /* successfully sent probe, wait for callback */
6686 lock_basic_unlock(&xfr->lock);
6689 /* failed to send probe, next master */
6690 xfr_probe_nextmaster(xfr);
6693 /* done with probe sequence, wait */
6694 if(xfr->task_probe->have_new_lease) {
6695 /* if zone not updated, start the wait timer again */
6696 if(verbosity >= VERB_ALGO) {
6698 dname_str(xfr->name, zname);
6699 verbose(VERB_ALGO, "auth_zone %s unchanged, new lease, wait", zname);
6701 xfr_probe_disown(xfr);
6703 xfr->lease_time = *env->now;
6704 if(xfr->task_nextprobe->worker == NULL)
6705 xfr_set_timeout(xfr, env, 0, 0);
6707 if(verbosity >= VERB_ALGO) {
6709 dname_str(xfr->name, zname);
6710 verbose(VERB_ALGO, "auth zone %s soa probe failed, wait to retry", zname);
6712 /* we failed to send this as well, move to the wait task,
6713 * use the shorter retry timeout */
6714 xfr_probe_disown(xfr);
6715 /* pick up the nextprobe task and wait */
6716 if(xfr->task_nextprobe->worker == NULL)
6717 xfr_set_timeout(xfr, env, 1, 0);
6720 lock_basic_unlock(&xfr->lock);
6723 /** callback for task_probe lookup of host name, of A or AAAA */
6724 void auth_xfer_probe_lookup_callback(void* arg, int rcode, sldns_buffer* buf,
6725 enum sec_status ATTR_UNUSED(sec), char* ATTR_UNUSED(why_bogus),
6726 int ATTR_UNUSED(was_ratelimited))
6728 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6729 struct module_env* env;
6730 log_assert(xfr->task_probe);
6731 lock_basic_lock(&xfr->lock);
6732 env = xfr->task_probe->env;
6733 if(!env || env->outnet->want_to_quit) {
6734 lock_basic_unlock(&xfr->lock);
6735 return; /* stop on quit */
6738 /* process result */
6739 if(rcode == LDNS_RCODE_NOERROR) {
6740 uint16_t wanted_qtype = LDNS_RR_TYPE_A;
6741 struct regional* temp = env->scratch;
6742 struct query_info rq;
6743 struct reply_info* rep;
6744 if(xfr->task_probe->lookup_aaaa)
6745 wanted_qtype = LDNS_RR_TYPE_AAAA;
6746 memset(&rq, 0, sizeof(rq));
6747 rep = parse_reply_in_temp_region(buf, temp, &rq);
6748 if(rep && rq.qtype == wanted_qtype &&
6749 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR) {
6750 /* parsed successfully */
6751 struct ub_packed_rrset_key* answer =
6752 reply_find_answer_rrset(&rq, rep);
6754 xfr_master_add_addrs(xfr->task_probe->
6755 lookup_target, answer, wanted_qtype);
6757 if(verbosity >= VERB_ALGO) {
6759 dname_str(xfr->name, zname);
6760 verbose(VERB_ALGO, "auth zone %s host %s type %s probe lookup has nodata", zname, xfr->task_probe->lookup_target->host, (xfr->task_probe->lookup_aaaa?"AAAA":"A"));
6764 if(verbosity >= VERB_ALGO) {
6766 dname_str(xfr->name, zname);
6767 verbose(VERB_ALGO, "auth zone %s host %s type %s probe lookup has no address", zname, xfr->task_probe->lookup_target->host, (xfr->task_probe->lookup_aaaa?"AAAA":"A"));
6770 regional_free_all(temp);
6772 if(verbosity >= VERB_ALGO) {
6774 dname_str(xfr->name, zname);
6775 verbose(VERB_ALGO, "auth zone %s host %s type %s probe lookup failed", zname, xfr->task_probe->lookup_target->host, (xfr->task_probe->lookup_aaaa?"AAAA":"A"));
6778 if(xfr->task_probe->lookup_target->list &&
6779 xfr->task_probe->lookup_target == xfr_probe_current_master(xfr))
6780 xfr->task_probe->scan_addr = xfr->task_probe->lookup_target->list;
6782 /* move to lookup AAAA after A lookup, move to next hostname lookup,
6783 * or move to send the probes, or, if nothing to do, end task_probe */
6784 xfr_probe_move_to_next_lookup(xfr, env);
6785 xfr_probe_send_or_end(xfr, env);
6788 /** disown task_nextprobe. caller must hold xfr.lock */
6790 xfr_nextprobe_disown(struct auth_xfer* xfr)
6792 /* delete the timer, because the next worker to pick this up may
6793 * not have the same event base */
6794 comm_timer_delete(xfr->task_nextprobe->timer);
6795 xfr->task_nextprobe->timer = NULL;
6796 xfr->task_nextprobe->next_probe = 0;
6797 /* we don't own this item anymore */
6798 xfr->task_nextprobe->worker = NULL;
6799 xfr->task_nextprobe->env = NULL;
6802 /** xfer nextprobe timeout callback, this is part of task_nextprobe */
6804 auth_xfer_timer(void* arg)
6806 struct auth_xfer* xfr = (struct auth_xfer*)arg;
6807 struct module_env* env;
6808 log_assert(xfr->task_nextprobe);
6809 lock_basic_lock(&xfr->lock);
6810 env = xfr->task_nextprobe->env;
6811 if(!env || env->outnet->want_to_quit) {
6812 lock_basic_unlock(&xfr->lock);
6813 return; /* stop on quit */
6816 /* see if zone has expired, and if so, also set auth_zone expired */
6817 if(xfr->have_zone && !xfr->zone_expired &&
6818 *env->now >= xfr->lease_time + xfr->expiry) {
6819 lock_basic_unlock(&xfr->lock);
6820 auth_xfer_set_expired(xfr, env, 1);
6821 lock_basic_lock(&xfr->lock);
6824 xfr_nextprobe_disown(xfr);
6826 if(!xfr_start_probe(xfr, env, NULL)) {
6827 /* not started because already in progress */
6828 lock_basic_unlock(&xfr->lock);
6832 /** return true if there are probe (SOA UDP query) targets in the master list*/
6834 have_probe_targets(struct auth_master* list)
6836 struct auth_master* p;
6837 for(p=list; p; p = p->next) {
6838 if(!p->allow_notify && p->host)
6844 /** start task_probe if possible, if no masters for probe start task_transfer
6845 * returns true if task has been started, and false if the task is already
6848 xfr_start_probe(struct auth_xfer* xfr, struct module_env* env,
6849 struct auth_master* spec)
6851 /* see if we need to start a probe (or maybe it is already in
6852 * progress (due to notify)) */
6853 if(xfr->task_probe->worker == NULL) {
6854 if(!have_probe_targets(xfr->task_probe->masters) &&
6855 !(xfr->task_probe->only_lookup &&
6856 xfr->task_probe->masters != NULL)) {
6857 /* useless to pick up task_probe, no masters to
6858 * probe. Instead attempt to pick up task transfer */
6859 if(xfr->task_transfer->worker == NULL) {
6860 xfr_start_transfer(xfr, env, spec);
6863 /* task transfer already in progress */
6867 /* pick up the probe task ourselves */
6868 xfr->task_probe->worker = env->worker;
6869 xfr->task_probe->env = env;
6870 xfr->task_probe->cp = NULL;
6872 /* start the task */
6873 /* have not seen a new lease yet, this scan */
6874 xfr->task_probe->have_new_lease = 0;
6875 /* if this was a timeout, no specific first master to scan */
6876 /* otherwise, spec is nonNULL the notified master, scan
6877 * first and also transfer first from it */
6878 xfr_probe_start_list(xfr, spec);
6879 /* setup to start the lookup of hostnames of masters afresh */
6880 xfr_probe_start_lookups(xfr);
6881 /* send the probe packet or next send, or end task */
6882 xfr_probe_send_or_end(xfr, env);
6888 /** for task_nextprobe.
6889 * determine next timeout for auth_xfer. Also (re)sets timer.
6890 * @param xfr: task structure
6891 * @param env: module environment, with worker and time.
6892 * @param failure: set true if timer should be set for failure retry.
6893 * @param lookup_only: only perform lookups when timer done, 0 sec timeout
6896 xfr_set_timeout(struct auth_xfer* xfr, struct module_env* env,
6897 int failure, int lookup_only)
6900 log_assert(xfr->task_nextprobe != NULL);
6901 log_assert(xfr->task_nextprobe->worker == NULL ||
6902 xfr->task_nextprobe->worker == env->worker);
6903 /* normally, nextprobe = startoflease + refresh,
6904 * but if expiry is sooner, use that one.
6905 * after a failure, use the retry timer instead. */
6906 xfr->task_nextprobe->next_probe = *env->now;
6907 if(xfr->lease_time && !failure)
6908 xfr->task_nextprobe->next_probe = xfr->lease_time;
6911 xfr->task_nextprobe->backoff = 0;
6913 if(xfr->task_nextprobe->backoff == 0)
6914 xfr->task_nextprobe->backoff = 3;
6915 else xfr->task_nextprobe->backoff *= 2;
6916 if(xfr->task_nextprobe->backoff > AUTH_TRANSFER_MAX_BACKOFF)
6917 xfr->task_nextprobe->backoff =
6918 AUTH_TRANSFER_MAX_BACKOFF;
6921 if(xfr->have_zone) {
6922 time_t wait = xfr->refresh;
6923 if(failure) wait = xfr->retry;
6924 if(xfr->expiry < wait)
6925 xfr->task_nextprobe->next_probe += xfr->expiry;
6926 else xfr->task_nextprobe->next_probe += wait;
6928 xfr->task_nextprobe->next_probe +=
6929 xfr->task_nextprobe->backoff;
6930 /* put the timer exactly on expiry, if possible */
6931 if(xfr->lease_time && xfr->lease_time+xfr->expiry <
6932 xfr->task_nextprobe->next_probe &&
6933 xfr->lease_time+xfr->expiry > *env->now)
6934 xfr->task_nextprobe->next_probe =
6935 xfr->lease_time+xfr->expiry;
6937 xfr->task_nextprobe->next_probe +=
6938 xfr->task_nextprobe->backoff;
6941 if(!xfr->task_nextprobe->timer) {
6942 xfr->task_nextprobe->timer = comm_timer_create(
6943 env->worker_base, auth_xfer_timer, xfr);
6944 if(!xfr->task_nextprobe->timer) {
6945 /* failed to malloc memory. likely zone transfer
6946 * also fails for that. skip the timeout */
6948 dname_str(xfr->name, zname);
6949 log_err("cannot allocate timer, no refresh for %s",
6954 xfr->task_nextprobe->worker = env->worker;
6955 xfr->task_nextprobe->env = env;
6956 if(*(xfr->task_nextprobe->env->now) <= xfr->task_nextprobe->next_probe)
6957 tv.tv_sec = xfr->task_nextprobe->next_probe -
6958 *(xfr->task_nextprobe->env->now);
6960 if(tv.tv_sec != 0 && lookup_only && xfr->task_probe->masters) {
6961 /* don't lookup_only, if lookup timeout is 0 anyway,
6962 * or if we don't have masters to lookup */
6964 if(xfr->task_probe->worker == NULL)
6965 xfr->task_probe->only_lookup = 1;
6967 if(verbosity >= VERB_ALGO) {
6969 dname_str(xfr->name, zname);
6970 verbose(VERB_ALGO, "auth zone %s timeout in %d seconds",
6971 zname, (int)tv.tv_sec);
6974 comm_timer_set(xfr->task_nextprobe->timer, &tv);
6977 /** initial pick up of worker timeouts, ties events to worker event loop */
6979 auth_xfer_pickup_initial(struct auth_zones* az, struct module_env* env)
6981 struct auth_xfer* x;
6982 lock_rw_wrlock(&az->lock);
6983 RBTREE_FOR(x, struct auth_xfer*, &az->xtree) {
6984 lock_basic_lock(&x->lock);
6985 /* set lease_time, because we now have timestamp in env,
6986 * (not earlier during startup and apply_cfg), and this
6987 * notes the start time when the data was acquired */
6989 x->lease_time = *env->now;
6990 if(x->task_nextprobe && x->task_nextprobe->worker == NULL) {
6991 xfr_set_timeout(x, env, 0, 1);
6993 lock_basic_unlock(&x->lock);
6995 lock_rw_unlock(&az->lock);
6998 void auth_zones_cleanup(struct auth_zones* az)
7000 struct auth_xfer* x;
7001 lock_rw_wrlock(&az->lock);
7002 RBTREE_FOR(x, struct auth_xfer*, &az->xtree) {
7003 lock_basic_lock(&x->lock);
7004 if(x->task_nextprobe && x->task_nextprobe->worker != NULL) {
7005 xfr_nextprobe_disown(x);
7007 if(x->task_probe && x->task_probe->worker != NULL) {
7008 xfr_probe_disown(x);
7010 if(x->task_transfer && x->task_transfer->worker != NULL) {
7011 auth_chunks_delete(x->task_transfer);
7012 xfr_transfer_disown(x);
7014 lock_basic_unlock(&x->lock);
7016 lock_rw_unlock(&az->lock);
7020 * malloc the xfer and tasks
7021 * @param z: auth_zone with name of zone.
7023 static struct auth_xfer*
7024 auth_xfer_new(struct auth_zone* z)
7026 struct auth_xfer* xfr;
7027 xfr = (struct auth_xfer*)calloc(1, sizeof(*xfr));
7028 if(!xfr) return NULL;
7029 xfr->name = memdup(z->name, z->namelen);
7034 xfr->node.key = xfr;
7035 xfr->namelen = z->namelen;
7036 xfr->namelabs = z->namelabs;
7037 xfr->dclass = z->dclass;
7039 xfr->task_nextprobe = (struct auth_nextprobe*)calloc(1,
7040 sizeof(struct auth_nextprobe));
7041 if(!xfr->task_nextprobe) {
7046 xfr->task_probe = (struct auth_probe*)calloc(1,
7047 sizeof(struct auth_probe));
7048 if(!xfr->task_probe) {
7049 free(xfr->task_nextprobe);
7054 xfr->task_transfer = (struct auth_transfer*)calloc(1,
7055 sizeof(struct auth_transfer));
7056 if(!xfr->task_transfer) {
7057 free(xfr->task_probe);
7058 free(xfr->task_nextprobe);
7064 lock_basic_init(&xfr->lock);
7065 lock_protect(&xfr->lock, &xfr->name, sizeof(xfr->name));
7066 lock_protect(&xfr->lock, &xfr->namelen, sizeof(xfr->namelen));
7067 lock_protect(&xfr->lock, xfr->name, xfr->namelen);
7068 lock_protect(&xfr->lock, &xfr->namelabs, sizeof(xfr->namelabs));
7069 lock_protect(&xfr->lock, &xfr->dclass, sizeof(xfr->dclass));
7070 lock_protect(&xfr->lock, &xfr->notify_received, sizeof(xfr->notify_received));
7071 lock_protect(&xfr->lock, &xfr->notify_serial, sizeof(xfr->notify_serial));
7072 lock_protect(&xfr->lock, &xfr->zone_expired, sizeof(xfr->zone_expired));
7073 lock_protect(&xfr->lock, &xfr->have_zone, sizeof(xfr->have_zone));
7074 lock_protect(&xfr->lock, &xfr->serial, sizeof(xfr->serial));
7075 lock_protect(&xfr->lock, &xfr->retry, sizeof(xfr->retry));
7076 lock_protect(&xfr->lock, &xfr->refresh, sizeof(xfr->refresh));
7077 lock_protect(&xfr->lock, &xfr->expiry, sizeof(xfr->expiry));
7078 lock_protect(&xfr->lock, &xfr->lease_time, sizeof(xfr->lease_time));
7079 lock_protect(&xfr->lock, &xfr->task_nextprobe->worker,
7080 sizeof(xfr->task_nextprobe->worker));
7081 lock_protect(&xfr->lock, &xfr->task_probe->worker,
7082 sizeof(xfr->task_probe->worker));
7083 lock_protect(&xfr->lock, &xfr->task_transfer->worker,
7084 sizeof(xfr->task_transfer->worker));
7085 lock_basic_lock(&xfr->lock);
7089 /** Create auth_xfer structure.
7090 * This populates the have_zone, soa values, and so on times.
7091 * and sets the timeout, if a zone transfer is needed a short timeout is set.
7092 * For that the auth_zone itself must exist (and read in zonefile)
7093 * returns false on alloc failure. */
7095 auth_xfer_create(struct auth_zones* az, struct auth_zone* z)
7097 struct auth_xfer* xfr;
7100 xfr = auth_xfer_new(z);
7102 log_err("malloc failure");
7105 /* insert in tree */
7106 (void)rbtree_insert(&az->xtree, &xfr->node);
7110 /** create new auth_master structure */
7111 static struct auth_master*
7112 auth_master_new(struct auth_master*** list)
7114 struct auth_master *m;
7115 m = (struct auth_master*)calloc(1, sizeof(*m));
7117 log_err("malloc failure");
7120 /* set first pointer to m, or next pointer of previous element to m */
7122 /* store m's next pointer as future point to store at */
7123 (*list) = &(m->next);
7127 /** dup_prefix : create string from initial part of other string, malloced */
7129 dup_prefix(char* str, size_t num)
7132 size_t len = strlen(str);
7133 if(len < num) num = len; /* not more than strlen */
7134 result = (char*)malloc(num+1);
7136 log_err("malloc failure");
7139 memmove(result, str, num);
7144 /** dup string and print error on error */
7148 char* result = strdup(str);
7150 log_err("malloc failure");
7156 /** find first of two characters */
7158 str_find_first_of_chars(char* s, char a, char b)
7160 char* ra = strchr(s, a);
7161 char* rb = strchr(s, b);
7164 if(ra < rb) return ra;
7168 /** parse URL into host and file parts, false on malloc or parse error */
7170 parse_url(char* url, char** host, char** file, int* port, int* ssl)
7173 /* parse http://www.example.com/file.htm
7174 * or http://127.0.0.1 (index.html)
7175 * or https://[::1@1234]/a/b/c/d */
7177 *port = AUTH_HTTPS_PORT;
7179 /* parse http:// or https:// */
7180 if(strncmp(p, "http://", 7) == 0) {
7183 *port = AUTH_HTTP_PORT;
7184 } else if(strncmp(p, "https://", 8) == 0) {
7186 } else if(strstr(p, "://") && strchr(p, '/') > strstr(p, "://") &&
7187 strchr(p, ':') >= strstr(p, "://")) {
7188 char* uri = dup_prefix(p, (size_t)(strstr(p, "://")-p));
7189 log_err("protocol %s:// not supported (for url %s)",
7195 /* parse hostname part */
7197 char* end = strchr(p, ']');
7198 p++; /* skip over [ */
7200 *host = dup_prefix(p, (size_t)(end-p));
7201 if(!*host) return 0;
7202 p = end+1; /* skip over ] */
7205 if(!*host) return 0;
7209 char* end = str_find_first_of_chars(p, ':', '/');
7211 *host = dup_prefix(p, (size_t)(end-p));
7212 if(!*host) return 0;
7215 if(!*host) return 0;
7217 p = end; /* at next : or / or NULL */
7220 /* parse port number */
7221 if(p && p[0] == ':') {
7223 *port = strtol(p+1, &end, 10);
7227 /* parse filename part */
7228 while(p && *p == '/')
7231 *file = strdup("/");
7232 else *file = strdup(p);
7234 log_err("malloc failure");
7241 xfer_set_masters(struct auth_master** list, struct config_auth* c,
7244 struct auth_master* m;
7245 struct config_strlist* p;
7246 /* list points to the first, or next pointer for the new element */
7248 list = &( (*list)->next );
7251 for(p = c->urls; p; p = p->next) {
7252 m = auth_master_new(&list);
7255 if(!parse_url(p->str, &m->host, &m->file, &m->port, &m->ssl))
7258 for(p = c->masters; p; p = p->next) {
7259 m = auth_master_new(&list);
7261 m->ixfr = 1; /* this flag is not configurable */
7262 m->host = strdup(p->str);
7264 log_err("malloc failure");
7268 for(p = c->allow_notify; p; p = p->next) {
7269 m = auth_master_new(&list);
7271 m->allow_notify = 1;
7272 m->host = strdup(p->str);
7274 log_err("malloc failure");
7281 #define SERIAL_BITS 32
7283 compare_serial(uint32_t a, uint32_t b)
7285 const uint32_t cutoff = ((uint32_t) 1 << (SERIAL_BITS - 1));
7289 } else if ((a < b && b - a < cutoff) || (a > b && a - b > cutoff)) {
7296 int zonemd_hashalgo_supported(int hashalgo)
7298 if(hashalgo == ZONEMD_ALGO_SHA384) return 1;
7299 if(hashalgo == ZONEMD_ALGO_SHA512) return 1;
7303 int zonemd_scheme_supported(int scheme)
7305 if(scheme == ZONEMD_SCHEME_SIMPLE) return 1;
7309 /** initialize hash for hashing with zonemd hash algo */
7310 static struct secalgo_hash* zonemd_digest_init(int hashalgo, char** reason)
7312 struct secalgo_hash *h;
7313 if(hashalgo == ZONEMD_ALGO_SHA384) {
7315 h = secalgo_hash_create_sha384();
7317 *reason = "digest sha384 could not be created";
7319 } else if(hashalgo == ZONEMD_ALGO_SHA512) {
7321 h = secalgo_hash_create_sha512();
7323 *reason = "digest sha512 could not be created";
7326 /* unknown hash algo */
7327 *reason = "unsupported algorithm";
7331 /** update the hash for zonemd */
7332 static int zonemd_digest_update(int hashalgo, struct secalgo_hash* h,
7333 uint8_t* data, size_t len, char** reason)
7335 if(hashalgo == ZONEMD_ALGO_SHA384) {
7336 if(!secalgo_hash_update(h, data, len)) {
7337 *reason = "digest sha384 failed";
7341 } else if(hashalgo == ZONEMD_ALGO_SHA512) {
7342 if(!secalgo_hash_update(h, data, len)) {
7343 *reason = "digest sha512 failed";
7348 /* unknown hash algo */
7349 *reason = "unsupported algorithm";
7353 /** finish the hash for zonemd */
7354 static int zonemd_digest_finish(int hashalgo, struct secalgo_hash* h,
7355 uint8_t* result, size_t hashlen, size_t* resultlen, char** reason)
7357 if(hashalgo == ZONEMD_ALGO_SHA384) {
7358 if(hashlen < 384/8) {
7359 *reason = "digest buffer too small for sha384";
7362 if(!secalgo_hash_final(h, result, hashlen, resultlen)) {
7363 *reason = "digest sha384 finish failed";
7367 } else if(hashalgo == ZONEMD_ALGO_SHA512) {
7368 if(hashlen < 512/8) {
7369 *reason = "digest buffer too small for sha512";
7372 if(!secalgo_hash_final(h, result, hashlen, resultlen)) {
7373 *reason = "digest sha512 finish failed";
7379 *reason = "unsupported algorithm";
7383 /** add rrsets from node to the list */
7384 static size_t authdata_rrsets_to_list(struct auth_rrset** array,
7385 size_t arraysize, struct auth_rrset* first)
7387 struct auth_rrset* rrset = first;
7390 if(num >= arraysize)
7394 rrset = rrset->next;
7399 /** compare rr list entries */
7400 static int rrlist_compare(const void* arg1, const void* arg2)
7402 struct auth_rrset* r1 = *(struct auth_rrset**)arg1;
7403 struct auth_rrset* r2 = *(struct auth_rrset**)arg2;
7405 if(r1 == NULL) t1 = LDNS_RR_TYPE_RRSIG;
7407 if(r2 == NULL) t2 = LDNS_RR_TYPE_RRSIG;
7416 /** add type RRSIG to rr list if not one there already,
7417 * this is to perform RRSIG collate processing at that point. */
7418 static void addrrsigtype_if_needed(struct auth_rrset** array,
7419 size_t arraysize, size_t* rrnum, struct auth_data* node)
7421 if(az_domain_rrset(node, LDNS_RR_TYPE_RRSIG))
7422 return; /* already one there */
7423 if((*rrnum) >= arraysize)
7424 return; /* array too small? */
7425 array[*rrnum] = NULL; /* nothing there, but need entry in list */
7429 /** collate the RRs in an RRset using the simple scheme */
7430 static int zonemd_simple_rrset(struct auth_zone* z, int hashalgo,
7431 struct secalgo_hash* h, struct auth_data* node,
7432 struct auth_rrset* rrset, struct regional* region,
7433 struct sldns_buffer* buf, char** reason)
7436 struct ub_packed_rrset_key key;
7437 memset(&key, 0, sizeof(key));
7438 key.entry.key = &key;
7439 key.entry.data = rrset->data;
7440 key.rk.dname = node->name;
7441 key.rk.dname_len = node->namelen;
7442 key.rk.type = htons(rrset->type);
7443 key.rk.rrset_class = htons(z->dclass);
7444 if(!rrset_canonicalize_to_buffer(region, buf, &key)) {
7445 *reason = "out of memory";
7448 regional_free_all(region);
7451 if(!zonemd_digest_update(hashalgo, h, sldns_buffer_begin(buf),
7452 sldns_buffer_limit(buf), reason)) {
7458 /** count number of RRSIGs in a domain name rrset list */
7459 static size_t zonemd_simple_count_rrsig(struct auth_rrset* rrset,
7460 struct auth_rrset** rrlist, size_t rrnum,
7461 struct auth_zone* z, struct auth_data* node)
7463 size_t i, count = 0;
7466 for(j = 0; j<rrset->data->count; j++) {
7467 if(rrsig_rdata_get_type_covered(rrset->data->
7468 rr_data[j], rrset->data->rr_len[j]) ==
7469 LDNS_RR_TYPE_ZONEMD &&
7470 query_dname_compare(z->name, node->name)==0) {
7471 /* omit RRSIGs over type ZONEMD at apex */
7477 for(i=0; i<rrnum; i++) {
7478 if(rrlist[i] && rrlist[i]->type == LDNS_RR_TYPE_ZONEMD &&
7479 query_dname_compare(z->name, node->name)==0) {
7480 /* omit RRSIGs over type ZONEMD at apex */
7483 count += (rrlist[i]?rrlist[i]->data->rrsig_count:0);
7488 /** allocate sparse rrset data for the number of entries in tepm region */
7489 static int zonemd_simple_rrsig_allocs(struct regional* region,
7490 struct packed_rrset_data* data, size_t count)
7492 data->rr_len = regional_alloc(region, sizeof(*data->rr_len) * count);
7496 data->rr_ttl = regional_alloc(region, sizeof(*data->rr_ttl) * count);
7500 data->rr_data = regional_alloc(region, sizeof(*data->rr_data) * count);
7501 if(!data->rr_data) {
7507 /** add the RRSIGs from the rrs in the domain into the data */
7508 static void add_rrlist_rrsigs_into_data(struct packed_rrset_data* data,
7509 size_t* done, struct auth_rrset** rrlist, size_t rrnum,
7510 struct auth_zone* z, struct auth_data* node)
7513 for(i=0; i<rrnum; i++) {
7517 if(rrlist[i]->type == LDNS_RR_TYPE_ZONEMD &&
7518 query_dname_compare(z->name, node->name)==0) {
7519 /* omit RRSIGs over type ZONEMD at apex */
7522 for(j = 0; j<rrlist[i]->data->rrsig_count; j++) {
7523 data->rr_len[*done] = rrlist[i]->data->rr_len[rrlist[i]->data->count + j];
7524 data->rr_ttl[*done] = rrlist[i]->data->rr_ttl[rrlist[i]->data->count + j];
7525 /* reference the rdata in the rrset, no need to
7526 * copy it, it is no longer needed at the end of
7528 data->rr_data[*done] = rrlist[i]->data->rr_data[rrlist[i]->data->count + j];
7534 static void add_rrset_into_data(struct packed_rrset_data* data,
7535 size_t* done, struct auth_rrset* rrset,
7536 struct auth_zone* z, struct auth_data* node)
7540 for(j = 0; j<rrset->data->count; j++) {
7541 if(rrsig_rdata_get_type_covered(rrset->data->
7542 rr_data[j], rrset->data->rr_len[j]) ==
7543 LDNS_RR_TYPE_ZONEMD &&
7544 query_dname_compare(z->name, node->name)==0) {
7545 /* omit RRSIGs over type ZONEMD at apex */
7548 data->rr_len[*done] = rrset->data->rr_len[j];
7549 data->rr_ttl[*done] = rrset->data->rr_ttl[j];
7550 /* reference the rdata in the rrset, no need to
7551 * copy it, it is no longer need at the end of
7553 data->rr_data[*done] = rrset->data->rr_data[j];
7559 /** collate the RRSIGs using the simple scheme */
7560 static int zonemd_simple_rrsig(struct auth_zone* z, int hashalgo,
7561 struct secalgo_hash* h, struct auth_data* node,
7562 struct auth_rrset* rrset, struct auth_rrset** rrlist, size_t rrnum,
7563 struct regional* region, struct sldns_buffer* buf, char** reason)
7565 /* the rrset pointer can be NULL, this means it is type RRSIG and
7566 * there is no ordinary type RRSIG there. The RRSIGs are stored
7567 * with the RRsets in their data.
7569 * The RRset pointer can be nonNULL. This happens if there is
7570 * no RR that is covered by the RRSIG for the domain. Then this
7571 * RRSIG RR is stored in an rrset of type RRSIG. The other RRSIGs
7572 * are stored in the rrset entries for the RRs in the rr list for
7573 * the domain node. We need to collate the rrset's data, if any, and
7574 * the rrlist's rrsigs */
7575 /* if this is the apex, omit RRSIGs that cover type ZONEMD */
7576 /* build rrsig rrset */
7578 struct ub_packed_rrset_key key;
7579 struct packed_rrset_data data;
7580 memset(&key, 0, sizeof(key));
7581 memset(&data, 0, sizeof(data));
7582 key.entry.key = &key;
7583 key.entry.data = &data;
7584 key.rk.dname = node->name;
7585 key.rk.dname_len = node->namelen;
7586 key.rk.type = htons(LDNS_RR_TYPE_RRSIG);
7587 key.rk.rrset_class = htons(z->dclass);
7588 data.count = zonemd_simple_count_rrsig(rrset, rrlist, rrnum, z, node);
7589 if(!zonemd_simple_rrsig_allocs(region, &data, data.count)) {
7590 *reason = "out of memory";
7591 regional_free_all(region);
7594 /* all the RRSIGs stored in the other rrsets for this domain node */
7595 add_rrlist_rrsigs_into_data(&data, &done, rrlist, rrnum, z, node);
7596 /* plus the RRSIGs stored in an rrset of type RRSIG for this node */
7597 add_rrset_into_data(&data, &done, rrset, z, node);
7600 if(!rrset_canonicalize_to_buffer(region, buf, &key)) {
7601 *reason = "out of memory";
7602 regional_free_all(region);
7605 regional_free_all(region);
7608 if(!zonemd_digest_update(hashalgo, h, sldns_buffer_begin(buf),
7609 sldns_buffer_limit(buf), reason)) {
7615 /** collate a domain's rrsets using the simple scheme */
7616 static int zonemd_simple_domain(struct auth_zone* z, int hashalgo,
7617 struct secalgo_hash* h, struct auth_data* node,
7618 struct regional* region, struct sldns_buffer* buf, char** reason)
7620 const size_t rrlistsize = 65536;
7621 struct auth_rrset* rrlist[rrlistsize];
7622 size_t i, rrnum = 0;
7623 /* see if the domain is out of scope, the zone origin,
7624 * that would be omitted */
7625 if(!dname_subdomain_c(node->name, z->name))
7626 return 1; /* continue */
7627 /* loop over the rrsets in ascending order. */
7628 rrnum = authdata_rrsets_to_list(rrlist, rrlistsize, node->rrsets);
7629 addrrsigtype_if_needed(rrlist, rrlistsize, &rrnum, node);
7630 qsort(rrlist, rrnum, sizeof(*rrlist), rrlist_compare);
7631 for(i=0; i<rrnum; i++) {
7632 if(rrlist[i] && rrlist[i]->type == LDNS_RR_TYPE_ZONEMD &&
7633 query_dname_compare(z->name, node->name) == 0) {
7634 /* omit type ZONEMD at apex */
7637 if(rrlist[i] == NULL || rrlist[i]->type ==
7638 LDNS_RR_TYPE_RRSIG) {
7639 if(!zonemd_simple_rrsig(z, hashalgo, h, node,
7640 rrlist[i], rrlist, rrnum, region, buf, reason))
7642 } else if(!zonemd_simple_rrset(z, hashalgo, h, node,
7643 rrlist[i], region, buf, reason)) {
7650 /** collate the zone using the simple scheme */
7651 static int zonemd_simple_collate(struct auth_zone* z, int hashalgo,
7652 struct secalgo_hash* h, struct regional* region,
7653 struct sldns_buffer* buf, char** reason)
7655 /* our tree is sorted in canonical order, so we can just loop over
7657 struct auth_data* n;
7658 RBTREE_FOR(n, struct auth_data*, &z->data) {
7659 if(!zonemd_simple_domain(z, hashalgo, h, n, region, buf,
7666 int auth_zone_generate_zonemd_hash(struct auth_zone* z, int scheme,
7667 int hashalgo, uint8_t* hash, size_t hashlen, size_t* resultlen,
7668 struct regional* region, struct sldns_buffer* buf, char** reason)
7670 struct secalgo_hash* h = zonemd_digest_init(hashalgo, reason);
7673 *reason = "digest init fail";
7676 if(scheme == ZONEMD_SCHEME_SIMPLE) {
7677 if(!zonemd_simple_collate(z, hashalgo, h, region, buf, reason)) {
7678 if(!*reason) *reason = "scheme simple collate fail";
7679 secalgo_hash_delete(h);
7683 if(!zonemd_digest_finish(hashalgo, h, hash, hashlen, resultlen,
7685 secalgo_hash_delete(h);
7686 *reason = "digest finish fail";
7689 secalgo_hash_delete(h);
7693 int auth_zone_generate_zonemd_check(struct auth_zone* z, int scheme,
7694 int hashalgo, uint8_t* hash, size_t hashlen, struct regional* region,
7695 struct sldns_buffer* buf, char** reason)
7700 if(!zonemd_hashalgo_supported(hashalgo)) {
7702 *reason = "unsupported algorithm";
7705 if(!zonemd_scheme_supported(scheme)) {
7707 *reason = "unsupported scheme";
7711 /* the ZONEMD draft requires digests to fail if too small */
7712 *reason = "digest length too small, less than 12";
7715 /* generate digest */
7716 if(!auth_zone_generate_zonemd_hash(z, scheme, hashalgo, gen,
7717 sizeof(gen), &genlen, region, buf, reason)) {
7718 /* reason filled in by zonemd hash routine */
7721 /* check digest length */
7722 if(hashlen != genlen) {
7723 *reason = "incorrect digest length";
7724 if(verbosity >= VERB_ALGO) {
7725 verbose(VERB_ALGO, "zonemd scheme=%d hashalgo=%d",
7727 log_hex("ZONEMD should be ", gen, genlen);
7728 log_hex("ZONEMD to check is", hash, hashlen);
7733 if(memcmp(hash, gen, genlen) != 0) {
7734 *reason = "incorrect digest";
7735 if(verbosity >= VERB_ALGO) {
7736 verbose(VERB_ALGO, "zonemd scheme=%d hashalgo=%d",
7738 log_hex("ZONEMD should be ", gen, genlen);
7739 log_hex("ZONEMD to check is", hash, hashlen);
7746 /** log auth zone message with zone name in front. */
7747 static void auth_zone_log(uint8_t* name, enum verbosity_value level,
7748 const char* format, ...) ATTR_FORMAT(printf, 3, 4);
7749 static void auth_zone_log(uint8_t* name, enum verbosity_value level,
7750 const char* format, ...)
7753 va_start(args, format);
7754 if(verbosity >= level) {
7756 char msg[MAXSYSLOGMSGLEN];
7757 dname_str(name, str);
7758 vsnprintf(msg, sizeof(msg), format, args);
7759 verbose(level, "auth zone %s %s", str, msg);
7764 /** ZONEMD, dnssec verify the rrset with the dnskey */
7765 static int zonemd_dnssec_verify_rrset(struct auth_zone* z,
7766 struct module_env* env, struct module_stack* mods,
7767 struct ub_packed_rrset_key* dnskey, struct auth_data* node,
7768 struct auth_rrset* rrset, char** why_bogus, uint8_t* sigalg)
7770 struct ub_packed_rrset_key pk;
7771 enum sec_status sec;
7774 m = modstack_find(mods, "validator");
7776 auth_zone_log(z->name, VERB_ALGO, "zonemd dnssec verify: have "
7777 "DNSKEY chain of trust, but no validator module");
7780 ve = (struct val_env*)env->modinfo[m];
7782 memset(&pk, 0, sizeof(pk));
7784 pk.entry.data = rrset->data;
7785 pk.rk.dname = node->name;
7786 pk.rk.dname_len = node->namelen;
7787 pk.rk.type = htons(rrset->type);
7788 pk.rk.rrset_class = htons(z->dclass);
7789 if(verbosity >= VERB_ALGO) {
7792 sldns_wire2str_type_buf(rrset->type, typestr, sizeof(typestr));
7793 auth_zone_log(z->name, VERB_ALGO,
7794 "zonemd: verify %s RRset with DNSKEY", typestr);
7796 sec = dnskeyset_verify_rrset(env, ve, &pk, dnskey, sigalg, why_bogus, NULL,
7797 LDNS_SECTION_ANSWER, NULL);
7798 if(sec == sec_status_secure) {
7802 auth_zone_log(z->name, VERB_ALGO, "DNSSEC verify was bogus: %s", *why_bogus);
7806 /** check for nsec3, the RR with params equal, if bitmap has the type */
7807 static int nsec3_of_param_has_type(struct auth_rrset* nsec3, int algo,
7808 size_t iter, uint8_t* salt, size_t saltlen, uint16_t rrtype)
7810 int i, count = (int)nsec3->data->count;
7811 struct ub_packed_rrset_key pk;
7812 memset(&pk, 0, sizeof(pk));
7813 pk.entry.data = nsec3->data;
7814 for(i=0; i<count; i++) {
7816 size_t rriter, rrsaltlen;
7818 if(!nsec3_get_params(&pk, i, &rralgo, &rriter, &rrsalt,
7820 continue; /* no parameters, malformed */
7821 if(rralgo != algo || rriter != iter || rrsaltlen != saltlen)
7822 continue; /* different parameters */
7824 if(rrsalt == NULL || salt == NULL)
7826 if(memcmp(rrsalt, salt, saltlen) != 0)
7827 continue; /* different salt parameters */
7829 if(nsec3_has_type(&pk, i, rrtype))
7835 /** Verify the absence of ZONEMD with DNSSEC by checking NSEC, NSEC3 type flag.
7836 * return false on failure, reason contains description of failure. */
7837 static int zonemd_check_dnssec_absence(struct auth_zone* z,
7838 struct module_env* env, struct module_stack* mods,
7839 struct ub_packed_rrset_key* dnskey, struct auth_data* apex,
7840 char** reason, char** why_bogus, uint8_t* sigalg)
7842 struct auth_rrset* nsec = NULL;
7844 *reason = "zone has no apex domain but ZONEMD missing";
7847 nsec = az_domain_rrset(apex, LDNS_RR_TYPE_NSEC);
7849 struct ub_packed_rrset_key pk;
7850 /* dnssec verify the NSEC */
7851 if(!zonemd_dnssec_verify_rrset(z, env, mods, dnskey, apex,
7852 nsec, why_bogus, sigalg)) {
7853 *reason = "DNSSEC verify failed for NSEC RRset";
7856 /* check type bitmap */
7857 memset(&pk, 0, sizeof(pk));
7858 pk.entry.data = nsec->data;
7859 if(nsec_has_type(&pk, LDNS_RR_TYPE_ZONEMD)) {
7860 *reason = "DNSSEC NSEC bitmap says type ZONEMD exists";
7863 auth_zone_log(z->name, VERB_ALGO, "zonemd DNSSEC NSEC verification of absence of ZONEMD secure");
7865 /* NSEC3 perhaps ? */
7867 size_t iter, saltlen;
7869 struct auth_rrset* nsec3param = az_domain_rrset(apex,
7870 LDNS_RR_TYPE_NSEC3PARAM);
7871 struct auth_data* match;
7872 struct auth_rrset* nsec3;
7874 *reason = "zone has no NSEC information but ZONEMD missing";
7877 if(!az_nsec3_param(z, &algo, &iter, &salt, &saltlen)) {
7878 *reason = "zone has no NSEC information but ZONEMD missing";
7881 /* find the NSEC3 record */
7882 match = az_nsec3_find_exact(z, z->name, z->namelen, algo,
7883 iter, salt, saltlen);
7885 *reason = "zone has no NSEC3 domain for the apex but ZONEMD missing";
7888 nsec3 = az_domain_rrset(match, LDNS_RR_TYPE_NSEC3);
7890 *reason = "zone has no NSEC3 RRset for the apex but ZONEMD missing";
7893 /* dnssec verify the NSEC3 */
7894 if(!zonemd_dnssec_verify_rrset(z, env, mods, dnskey, match,
7895 nsec3, why_bogus, sigalg)) {
7896 *reason = "DNSSEC verify failed for NSEC3 RRset";
7899 /* check type bitmap */
7900 if(nsec3_of_param_has_type(nsec3, algo, iter, salt, saltlen,
7901 LDNS_RR_TYPE_ZONEMD)) {
7902 *reason = "DNSSEC NSEC3 bitmap says type ZONEMD exists";
7905 auth_zone_log(z->name, VERB_ALGO, "zonemd DNSSEC NSEC3 verification of absence of ZONEMD secure");
7911 /** Verify the SOA and ZONEMD DNSSEC signatures.
7912 * return false on failure, reason contains description of failure. */
7913 static int zonemd_check_dnssec_soazonemd(struct auth_zone* z,
7914 struct module_env* env, struct module_stack* mods,
7915 struct ub_packed_rrset_key* dnskey, struct auth_data* apex,
7916 struct auth_rrset* zonemd_rrset, char** reason, char** why_bogus,
7919 struct auth_rrset* soa;
7921 *reason = "zone has no apex domain";
7924 soa = az_domain_rrset(apex, LDNS_RR_TYPE_SOA);
7926 *reason = "zone has no SOA RRset";
7929 if(!zonemd_dnssec_verify_rrset(z, env, mods, dnskey, apex, soa,
7930 why_bogus, sigalg)) {
7931 *reason = "DNSSEC verify failed for SOA RRset";
7934 if(!zonemd_dnssec_verify_rrset(z, env, mods, dnskey, apex,
7935 zonemd_rrset, why_bogus, sigalg)) {
7936 *reason = "DNSSEC verify failed for ZONEMD RRset";
7939 auth_zone_log(z->name, VERB_ALGO, "zonemd DNSSEC verification of SOA and ZONEMD RRsets secure");
7944 * Fail the ZONEMD verification.
7945 * @param z: auth zone that fails.
7946 * @param env: environment with config, to ignore failure or not.
7947 * @param reason: failure string description.
7948 * @param why_bogus: failure string for DNSSEC verification failure.
7949 * @param result: strdup result in here if not NULL.
7951 static void auth_zone_zonemd_fail(struct auth_zone* z, struct module_env* env,
7952 char* reason, char* why_bogus, char** result)
7955 /* if fail: log reason, and depending on config also take action
7956 * and drop the zone, eg. it is gone from memory, set zone_expired */
7957 dname_str(z->name, zstr);
7958 if(!reason) reason = "verification failed";
7962 snprintf(res, sizeof(res), "%s: %s", reason,
7964 *result = strdup(res);
7966 *result = strdup(reason);
7968 if(!*result) log_err("out of memory");
7970 log_warn("auth zone %s: ZONEMD verification failed: %s", zstr, reason);
7973 if(env->cfg->zonemd_permissive_mode) {
7974 verbose(VERB_ALGO, "zonemd-permissive-mode enabled, "
7975 "not blocking zone %s", zstr);
7979 /* expired means the zone gives servfail and is not used by
7980 * lookup if fallback_enabled*/
7981 z->zone_expired = 1;
7985 * Verify the zonemd with DNSSEC and hash check, with given key.
7986 * @param z: auth zone.
7987 * @param env: environment with config and temp buffers.
7988 * @param mods: module stack with validator env for verification.
7989 * @param dnskey: dnskey that we can use, or NULL. If nonnull, the key
7990 * has been verified and is the start of the chain of trust.
7991 * @param is_insecure: if true, the dnskey is not used, the zone is insecure.
7992 * And dnssec is not used. It is DNSSEC secure insecure or not under
7994 * @param sigalg: if nonNULL provide algorithm downgrade protection.
7995 * Otherwise one algorithm is enough. Must have space of ALGO_NEEDS_MAX+1.
7996 * @param result: if not NULL result reason copied here.
7999 auth_zone_verify_zonemd_with_key(struct auth_zone* z, struct module_env* env,
8000 struct module_stack* mods, struct ub_packed_rrset_key* dnskey,
8001 int is_insecure, char** result, uint8_t* sigalg)
8003 char* reason = NULL, *why_bogus = NULL;
8004 struct auth_data* apex = NULL;
8005 struct auth_rrset* zonemd_rrset = NULL;
8006 int zonemd_absent = 0, zonemd_absence_dnssecok = 0;
8008 /* see if ZONEMD is present or absent. */
8009 apex = az_find_name(z, z->name, z->namelen);
8013 zonemd_rrset = az_domain_rrset(apex, LDNS_RR_TYPE_ZONEMD);
8014 if(!zonemd_rrset || zonemd_rrset->data->count==0) {
8016 zonemd_rrset = NULL;
8020 /* if no DNSSEC, done. */
8021 /* if no ZONEMD, and DNSSEC, use DNSKEY to verify NSEC or NSEC3 for
8022 * zone apex. Check ZONEMD bit is turned off or else fail */
8023 /* if ZONEMD, and DNSSEC, check DNSSEC signature on SOA and ZONEMD,
8025 if(!dnskey && !is_insecure) {
8026 auth_zone_zonemd_fail(z, env, "DNSKEY missing", NULL, result);
8028 } else if(!zonemd_rrset && dnskey && !is_insecure) {
8029 /* fetch, DNSSEC verify, and check NSEC/NSEC3 */
8030 if(!zonemd_check_dnssec_absence(z, env, mods, dnskey, apex,
8031 &reason, &why_bogus, sigalg)) {
8032 auth_zone_zonemd_fail(z, env, reason, why_bogus, result);
8035 zonemd_absence_dnssecok = 1;
8036 } else if(zonemd_rrset && dnskey && !is_insecure) {
8037 /* check DNSSEC verify of SOA and ZONEMD */
8038 if(!zonemd_check_dnssec_soazonemd(z, env, mods, dnskey, apex,
8039 zonemd_rrset, &reason, &why_bogus, sigalg)) {
8040 auth_zone_zonemd_fail(z, env, reason, why_bogus, result);
8045 if(zonemd_absent && z->zonemd_reject_absence) {
8046 auth_zone_zonemd_fail(z, env, "ZONEMD absent and that is not allowed by config", NULL, result);
8049 if(zonemd_absent && zonemd_absence_dnssecok) {
8050 auth_zone_log(z->name, VERB_ALGO, "DNSSEC verified nonexistence of ZONEMD");
8052 *result = strdup("DNSSEC verified nonexistence of ZONEMD");
8053 if(!*result) log_err("out of memory");
8058 auth_zone_log(z->name, VERB_ALGO, "no ZONEMD present");
8060 *result = strdup("no ZONEMD present");
8061 if(!*result) log_err("out of memory");
8066 /* check ZONEMD checksum and report or else fail. */
8067 if(!auth_zone_zonemd_check_hash(z, env, &reason)) {
8068 auth_zone_zonemd_fail(z, env, reason, NULL, result);
8072 /* success! log the success */
8074 auth_zone_log(z->name, VERB_ALGO, "ZONEMD %s", reason);
8075 else auth_zone_log(z->name, VERB_ALGO, "ZONEMD verification successful");
8078 *result = strdup(reason);
8079 else *result = strdup("ZONEMD verification successful");
8080 if(!*result) log_err("out of memory");
8085 * verify the zone DNSKEY rrset from the trust anchor
8086 * This is possible because the anchor is for the zone itself, and can
8087 * thus apply straight to the zone DNSKEY set.
8088 * @param z: the auth zone.
8089 * @param env: environment with time and temp buffers.
8090 * @param mods: module stack for validator environment for dnssec validation.
8091 * @param anchor: trust anchor to use
8092 * @param is_insecure: returned, true if the zone is securely insecure.
8093 * @param why_bogus: if the routine fails, returns the failure reason.
8094 * @param keystorage: where to store the ub_packed_rrset_key that is created
8095 * on success. A pointer to it is returned on success.
8096 * @return the dnskey RRset, reference to zone data and keystorage, or
8099 static struct ub_packed_rrset_key*
8100 zonemd_get_dnskey_from_anchor(struct auth_zone* z, struct module_env* env,
8101 struct module_stack* mods, struct trust_anchor* anchor,
8102 int* is_insecure, char** why_bogus,
8103 struct ub_packed_rrset_key* keystorage)
8105 struct auth_data* apex;
8106 struct auth_rrset* dnskey_rrset;
8107 enum sec_status sec;
8111 apex = az_find_name(z, z->name, z->namelen);
8113 *why_bogus = "have trust anchor, but zone has no apex domain for DNSKEY";
8116 dnskey_rrset = az_domain_rrset(apex, LDNS_RR_TYPE_DNSKEY);
8117 if(!dnskey_rrset || dnskey_rrset->data->count==0) {
8118 *why_bogus = "have trust anchor, but zone has no DNSKEY";
8122 m = modstack_find(mods, "validator");
8124 *why_bogus = "have trust anchor, but no validator module";
8127 ve = (struct val_env*)env->modinfo[m];
8129 memset(keystorage, 0, sizeof(*keystorage));
8130 keystorage->entry.key = keystorage;
8131 keystorage->entry.data = dnskey_rrset->data;
8132 keystorage->rk.dname = apex->name;
8133 keystorage->rk.dname_len = apex->namelen;
8134 keystorage->rk.type = htons(LDNS_RR_TYPE_DNSKEY);
8135 keystorage->rk.rrset_class = htons(z->dclass);
8136 auth_zone_log(z->name, VERB_QUERY,
8137 "zonemd: verify DNSKEY RRset with trust anchor");
8138 sec = val_verify_DNSKEY_with_TA(env, ve, keystorage, anchor->ds_rrset,
8139 anchor->dnskey_rrset, NULL, why_bogus, NULL, NULL);
8140 regional_free_all(env->scratch);
8141 if(sec == sec_status_secure) {
8145 } else if(sec == sec_status_insecure) {
8151 auth_zone_log(z->name, VERB_ALGO,
8152 "zonemd: verify DNSKEY RRset with trust anchor failed: %s", *why_bogus);
8157 /** verify the DNSKEY from the zone with looked up DS record */
8158 static struct ub_packed_rrset_key*
8159 auth_zone_verify_zonemd_key_with_ds(struct auth_zone* z,
8160 struct module_env* env, struct module_stack* mods,
8161 struct ub_packed_rrset_key* ds, int* is_insecure, char** why_bogus,
8162 struct ub_packed_rrset_key* keystorage, uint8_t* sigalg)
8164 struct auth_data* apex;
8165 struct auth_rrset* dnskey_rrset;
8166 enum sec_status sec;
8170 /* fetch DNSKEY from zone data */
8171 apex = az_find_name(z, z->name, z->namelen);
8173 *why_bogus = "in verifywithDS, zone has no apex";
8176 dnskey_rrset = az_domain_rrset(apex, LDNS_RR_TYPE_DNSKEY);
8177 if(!dnskey_rrset || dnskey_rrset->data->count==0) {
8178 *why_bogus = "in verifywithDS, zone has no DNSKEY";
8182 m = modstack_find(mods, "validator");
8184 *why_bogus = "in verifywithDS, have no validator module";
8187 ve = (struct val_env*)env->modinfo[m];
8189 memset(keystorage, 0, sizeof(*keystorage));
8190 keystorage->entry.key = keystorage;
8191 keystorage->entry.data = dnskey_rrset->data;
8192 keystorage->rk.dname = apex->name;
8193 keystorage->rk.dname_len = apex->namelen;
8194 keystorage->rk.type = htons(LDNS_RR_TYPE_DNSKEY);
8195 keystorage->rk.rrset_class = htons(z->dclass);
8196 auth_zone_log(z->name, VERB_QUERY, "zonemd: verify zone DNSKEY with DS");
8197 sec = val_verify_DNSKEY_with_DS(env, ve, keystorage, ds, sigalg,
8198 why_bogus, NULL, NULL);
8199 regional_free_all(env->scratch);
8200 if(sec == sec_status_secure) {
8203 } else if(sec == sec_status_insecure) {
8209 if(*why_bogus == NULL)
8210 *why_bogus = "verify failed";
8211 auth_zone_log(z->name, VERB_ALGO,
8212 "zonemd: verify DNSKEY RRset with DS failed: %s",
8218 /** callback for ZONEMD lookup of DNSKEY */
8219 void auth_zonemd_dnskey_lookup_callback(void* arg, int rcode, sldns_buffer* buf,
8220 enum sec_status sec, char* why_bogus, int ATTR_UNUSED(was_ratelimited))
8222 struct auth_zone* z = (struct auth_zone*)arg;
8223 struct module_env* env;
8224 char* reason = NULL, *ds_bogus = NULL, *typestr="DNSKEY";
8225 struct ub_packed_rrset_key* dnskey = NULL, *ds = NULL;
8226 int is_insecure = 0, downprot;
8227 struct ub_packed_rrset_key keystorage;
8228 uint8_t sigalg[ALGO_NEEDS_MAX+1];
8230 lock_rw_wrlock(&z->lock);
8231 env = z->zonemd_callback_env;
8232 /* release the env variable so another worker can pick up the
8233 * ZONEMD verification task if it wants to */
8234 z->zonemd_callback_env = NULL;
8235 if(!env || env->outnet->want_to_quit || z->zone_deleted) {
8236 lock_rw_unlock(&z->lock);
8237 return; /* stop on quit */
8239 if(z->zonemd_callback_qtype == LDNS_RR_TYPE_DS)
8241 downprot = env->cfg->harden_algo_downgrade;
8243 /* process result */
8244 if(sec == sec_status_bogus) {
8247 if(z->zonemd_callback_qtype == LDNS_RR_TYPE_DNSKEY)
8248 reason = "lookup of DNSKEY was bogus";
8249 else reason = "lookup of DS was bogus";
8251 auth_zone_log(z->name, VERB_ALGO,
8252 "zonemd lookup of %s was bogus: %s", typestr, reason);
8253 } else if(rcode == LDNS_RCODE_NOERROR) {
8254 uint16_t wanted_qtype = z->zonemd_callback_qtype;
8255 struct regional* temp = env->scratch;
8256 struct query_info rq;
8257 struct reply_info* rep;
8258 memset(&rq, 0, sizeof(rq));
8259 rep = parse_reply_in_temp_region(buf, temp, &rq);
8260 if(rep && rq.qtype == wanted_qtype &&
8261 query_dname_compare(z->name, rq.qname) == 0 &&
8262 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR) {
8263 /* parsed successfully */
8264 struct ub_packed_rrset_key* answer =
8265 reply_find_answer_rrset(&rq, rep);
8266 if(answer && sec == sec_status_secure) {
8267 if(z->zonemd_callback_qtype == LDNS_RR_TYPE_DNSKEY)
8270 auth_zone_log(z->name, VERB_ALGO,
8271 "zonemd lookup of %s was secure", typestr);
8272 } else if(sec == sec_status_secure && !answer) {
8274 auth_zone_log(z->name, VERB_ALGO,
8275 "zonemd lookup of %s has no content, but is secure, treat as insecure", typestr);
8276 } else if(sec == sec_status_insecure) {
8278 auth_zone_log(z->name, VERB_ALGO,
8279 "zonemd lookup of %s was insecure", typestr);
8280 } else if(sec == sec_status_indeterminate) {
8282 auth_zone_log(z->name, VERB_ALGO,
8283 "zonemd lookup of %s was indeterminate, treat as insecure", typestr);
8285 auth_zone_log(z->name, VERB_ALGO,
8286 "zonemd lookup of %s has nodata", typestr);
8287 if(z->zonemd_callback_qtype == LDNS_RR_TYPE_DNSKEY)
8288 reason = "lookup of DNSKEY has nodata";
8289 else reason = "lookup of DS has nodata";
8291 } else if(rep && rq.qtype == wanted_qtype &&
8292 query_dname_compare(z->name, rq.qname) == 0 &&
8293 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN &&
8294 sec == sec_status_secure) {
8295 /* secure nxdomain, so the zone is like some RPZ zone
8296 * that does not exist in the wider internet, with
8297 * a secure nxdomain answer outside of it. So we
8298 * treat the zonemd zone without a dnssec chain of
8299 * trust, as insecure. */
8301 auth_zone_log(z->name, VERB_ALGO,
8302 "zonemd lookup of %s was secure NXDOMAIN, treat as insecure", typestr);
8303 } else if(rep && rq.qtype == wanted_qtype &&
8304 query_dname_compare(z->name, rq.qname) == 0 &&
8305 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN &&
8306 sec == sec_status_insecure) {
8308 auth_zone_log(z->name, VERB_ALGO,
8309 "zonemd lookup of %s was insecure NXDOMAIN, treat as insecure", typestr);
8310 } else if(rep && rq.qtype == wanted_qtype &&
8311 query_dname_compare(z->name, rq.qname) == 0 &&
8312 FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN &&
8313 sec == sec_status_indeterminate) {
8315 auth_zone_log(z->name, VERB_ALGO,
8316 "zonemd lookup of %s was indeterminate NXDOMAIN, treat as insecure", typestr);
8318 auth_zone_log(z->name, VERB_ALGO,
8319 "zonemd lookup of %s has no answer", typestr);
8320 if(z->zonemd_callback_qtype == LDNS_RR_TYPE_DNSKEY)
8321 reason = "lookup of DNSKEY has no answer";
8322 else reason = "lookup of DS has no answer";
8325 auth_zone_log(z->name, VERB_ALGO,
8326 "zonemd lookup of %s failed", typestr);
8327 if(z->zonemd_callback_qtype == LDNS_RR_TYPE_DNSKEY)
8328 reason = "lookup of DNSKEY failed";
8329 else reason = "lookup of DS failed";
8332 if(!reason && !is_insecure && !dnskey && ds) {
8333 dnskey = auth_zone_verify_zonemd_key_with_ds(z, env,
8334 &env->mesh->mods, ds, &is_insecure, &ds_bogus,
8335 &keystorage, downprot?sigalg:NULL);
8336 if(!dnskey && !is_insecure && !reason)
8337 reason = "DNSKEY verify with DS failed";
8341 auth_zone_zonemd_fail(z, env, reason, ds_bogus, NULL);
8342 lock_rw_unlock(&z->lock);
8346 auth_zone_verify_zonemd_with_key(z, env, &env->mesh->mods, dnskey,
8347 is_insecure, NULL, downprot?sigalg:NULL);
8348 regional_free_all(env->scratch);
8349 lock_rw_unlock(&z->lock);
8352 /** lookup DNSKEY for ZONEMD verification */
8354 zonemd_lookup_dnskey(struct auth_zone* z, struct module_env* env)
8356 struct query_info qinfo;
8357 uint16_t qflags = BIT_RD;
8358 struct edns_data edns;
8359 sldns_buffer* buf = env->scratch_buffer;
8362 if(!z->fallback_enabled) {
8363 /* we cannot actually get the DNSKEY, because it is in the
8364 * zone we have ourselves, and it is not served yet
8365 * (possibly), so fetch type DS */
8368 if(z->zonemd_callback_env) {
8369 /* another worker is already working on the callback
8370 * for the DNSKEY lookup for ZONEMD verification.
8371 * We do not also have to do ZONEMD verification, let that
8373 auth_zone_log(z->name, VERB_ALGO,
8374 "zonemd needs lookup of %s and that already is worked on by another worker", (fetch_ds?"DS":"DNSKEY"));
8378 /* use mesh_new_callback to lookup the DNSKEY,
8379 * and then wait for them to be looked up (in cache, or query) */
8380 qinfo.qname_len = z->namelen;
8381 qinfo.qname = z->name;
8382 qinfo.qclass = z->dclass;
8384 qinfo.qtype = LDNS_RR_TYPE_DS;
8385 else qinfo.qtype = LDNS_RR_TYPE_DNSKEY;
8386 qinfo.local_alias = NULL;
8387 if(verbosity >= VERB_ALGO) {
8389 char buf2[LDNS_MAX_DOMAINLEN+1];
8390 dname_str(z->name, buf2);
8391 snprintf(buf1, sizeof(buf1), "auth zone %s: lookup %s "
8392 "for zonemd verification", buf2,
8393 (fetch_ds?"DS":"DNSKEY"));
8394 log_query_info(VERB_ALGO, buf1, &qinfo);
8396 edns.edns_present = 1;
8398 edns.edns_version = 0;
8399 edns.bits = EDNS_DO;
8400 edns.opt_list_in = NULL;
8401 edns.opt_list_out = NULL;
8402 edns.opt_list_inplace_cb_out = NULL;
8403 if(sldns_buffer_capacity(buf) < 65535)
8404 edns.udp_size = (uint16_t)sldns_buffer_capacity(buf);
8405 else edns.udp_size = 65535;
8407 /* store the worker-specific module env for the callback.
8408 * We can then reference this when the callback executes */
8409 z->zonemd_callback_env = env;
8410 z->zonemd_callback_qtype = qinfo.qtype;
8411 /* the callback can be called straight away */
8412 lock_rw_unlock(&z->lock);
8413 if(!mesh_new_callback(env->mesh, &qinfo, qflags, &edns, buf, 0,
8414 &auth_zonemd_dnskey_lookup_callback, z, 0)) {
8415 lock_rw_wrlock(&z->lock);
8416 log_err("out of memory lookup of %s for zonemd",
8417 (fetch_ds?"DS":"DNSKEY"));
8420 lock_rw_wrlock(&z->lock);
8424 void auth_zone_verify_zonemd(struct auth_zone* z, struct module_env* env,
8425 struct module_stack* mods, char** result, int offline, int only_online)
8427 char* reason = NULL, *why_bogus = NULL;
8428 struct trust_anchor* anchor = NULL;
8429 struct ub_packed_rrset_key* dnskey = NULL;
8430 struct ub_packed_rrset_key keystorage;
8431 int is_insecure = 0;
8432 /* verify the ZONEMD if present.
8433 * If not present check if absence is allowed by DNSSEC */
8434 if(!z->zonemd_check)
8436 if(z->data.count == 0)
8437 return; /* no data */
8439 /* if zone is under a trustanchor */
8440 /* is it equal to trustanchor - get dnskey's verified */
8441 /* else, find chain of trust by fetching DNSKEYs lookup for zone */
8442 /* result if that, if insecure, means no DNSSEC for the ZONEMD,
8443 * otherwise we have the zone DNSKEY for the DNSSEC verification. */
8445 anchor = anchors_lookup(env->anchors, z->name, z->namelen,
8447 if(anchor && anchor->numDS == 0 && anchor->numDNSKEY == 0) {
8448 /* domain-insecure trust anchor for unsigned zones */
8449 lock_basic_unlock(&anchor->lock);
8454 } else if(anchor && query_dname_compare(z->name, anchor->name) == 0) {
8456 lock_basic_unlock(&anchor->lock);
8459 /* equal to trustanchor, no need for online lookups */
8460 dnskey = zonemd_get_dnskey_from_anchor(z, env, mods, anchor,
8461 &is_insecure, &why_bogus, &keystorage);
8462 lock_basic_unlock(&anchor->lock);
8463 if(!dnskey && !reason && !is_insecure) {
8464 reason = "verify DNSKEY RRset with trust anchor failed";
8467 lock_basic_unlock(&anchor->lock);
8468 /* perform online lookups */
8471 /* setup online lookups, and wait for them */
8472 if(zonemd_lookup_dnskey(z, env)) {
8473 /* wait for the lookup */
8476 reason = "could not lookup DNSKEY for chain of trust";
8478 /* the zone is not under a trust anchor */
8486 auth_zone_zonemd_fail(z, env, reason, why_bogus, result);
8490 auth_zone_verify_zonemd_with_key(z, env, mods, dnskey, is_insecure,
8492 regional_free_all(env->scratch);
8495 void auth_zones_pickup_zonemd_verify(struct auth_zones* az,
8496 struct module_env* env)
8498 struct auth_zone key;
8499 uint8_t savezname[255+1];
8500 size_t savezname_len;
8501 struct auth_zone* z;
8502 key.node.key = &key;
8503 lock_rw_rdlock(&az->lock);
8504 RBTREE_FOR(z, struct auth_zone*, &az->ztree) {
8505 lock_rw_wrlock(&z->lock);
8506 if(!z->zonemd_check) {
8507 lock_rw_unlock(&z->lock);
8510 key.dclass = z->dclass;
8511 key.namelabs = z->namelabs;
8512 if(z->namelen > sizeof(savezname)) {
8513 lock_rw_unlock(&z->lock);
8514 log_err("auth_zones_pickup_zonemd_verify: zone name too long");
8517 savezname_len = z->namelen;
8518 memmove(savezname, z->name, z->namelen);
8519 lock_rw_unlock(&az->lock);
8520 auth_zone_verify_zonemd(z, env, &env->mesh->mods, NULL, 0, 1);
8521 lock_rw_unlock(&z->lock);
8522 lock_rw_rdlock(&az->lock);
8523 /* find the zone we had before, it is not deleted,
8524 * because we have a flag for that that is processed at
8526 key.namelen = savezname_len;
8527 key.name = savezname;
8528 z = (struct auth_zone*)rbtree_search(&az->ztree, &key);
8532 lock_rw_unlock(&az->lock);