xref: /freebsd/contrib/unbound/services/authzone.h (revision 7a789145f88a6aceacc59029a0cafe7de7aeefea)
1 /*
2  * services/authzone.h - authoritative zone that is locally hosted.
3  *
4  * Copyright (c) 2017, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains 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.
42  */
43 
44 #ifndef SERVICES_AUTHZONE_H
45 #define SERVICES_AUTHZONE_H
46 #include "util/rbtree.h"
47 #include "util/locks.h"
48 #include "services/mesh.h"
49 #include "services/rpz.h"
50 struct ub_packed_rrset_key;
51 struct regional;
52 struct config_file;
53 struct config_auth;
54 struct query_info;
55 struct dns_msg;
56 struct edns_data;
57 struct module_env;
58 struct worker;
59 struct comm_point;
60 struct comm_timer;
61 struct comm_reply;
62 struct auth_rrset;
63 struct auth_nextprobe;
64 struct auth_probe;
65 struct auth_transfer;
66 struct auth_master;
67 struct auth_chunk;
68 
69 /**
70  * Authoritative zones, shared.
71  */
72 struct auth_zones {
73 	/** lock on the authzone trees. It is locked after views, respip,
74 	 * local_zones and before fwds and stubs. */
75 	lock_rw_type lock;
76 	/** rbtree of struct auth_zone */
77 	rbtree_type ztree;
78 	/** rbtree of struct auth_xfer */
79 	rbtree_type xtree;
80 	/** do we have downstream enabled */
81 	int have_downstream;
82 	/** first auth zone containing rpz item in linked list */
83 	struct auth_zone* rpz_first;
84 	/** rw lock for rpz linked list, needed when iterating or editing linked
85 	 * list. */
86 	lock_rw_type rpz_lock;
87 };
88 
89 /**
90  * Auth zone.  Authoritative data, that is fetched from instead of sending
91  * packets to the internet.
92  */
93 struct auth_zone {
94 	/** rbtree node, key is name and class */
95 	rbnode_type node;
96 
97 	/** zone name, in uncompressed wireformat */
98 	uint8_t* name;
99 	/** length of zone name */
100 	size_t namelen;
101 	/** number of labels in zone name */
102 	int namelabs;
103 	/** the class of this zone, in host byteorder.
104 	 * uses 'dclass' to not conflict with c++ keyword class. */
105 	uint16_t dclass;
106 
107 	/** lock on the data in the structure
108 	 * For the node, parent, name, namelen, namelabs, dclass, you
109 	 * need to also hold the zones_tree lock to change them (or to
110 	 * delete this zone) */
111 	lock_rw_type lock;
112 
113 	/** auth data for this zone
114 	 * rbtree of struct auth_data */
115 	rbtree_type data;
116 
117 	/** zonefile name (or NULL for no zonefile) */
118 	char* zonefile;
119 	/** fallback to the internet on failure or ttl-expiry of auth zone */
120 	int fallback_enabled;
121 	/** the time when zone was transferred from upstream */
122 	time_t soa_zone_acquired;
123 	/** the zone has expired (enabled by the xfer worker), fallback
124 	 * happens if that option is enabled. */
125 	int zone_expired;
126 	/** zone is a slave zone (it has masters) */
127 	int zone_is_slave;
128 	/** for downstream: this zone answers queries towards the downstream
129 	 * clients */
130 	int for_downstream;
131 	/** for upstream: this zone answers queries that unbound intends to
132 	 * send upstream. */
133 	int for_upstream;
134 	/** check ZONEMD records */
135 	int zonemd_check;
136 	/** reject absence of ZONEMD records */
137 	int zonemd_reject_absence;
138 	/** RPZ zones */
139 	struct rpz* rpz;
140 	/** store the env (worker thread specific) for the zonemd callbacks
141 	 * from the mesh with the results of the lookup, if nonNULL, some
142 	 * worker has already picked up the zonemd verification task and
143 	 * this worker does not have to do it as well. */
144 	struct module_env* zonemd_callback_env;
145 	/** for the zonemd callback, the type of data looked up */
146 	uint16_t zonemd_callback_qtype;
147 	/** for the zonemd callback, the unique info */
148 	void* zonemd_callback_unique_info;
149 	/** zone has been deleted */
150 	int zone_deleted;
151 	/** deletelist pointer, unused normally except during delete */
152 	struct auth_zone* delete_next;
153 	/* not protected by auth_zone lock, must be last items in struct */
154 	/** next auth zone containing RPZ data, or NULL */
155 	struct auth_zone* rpz_az_next;
156 	/** previous auth zone containing RPZ data, or NULL */
157 	struct auth_zone* rpz_az_prev;
158 	/** The maximum auth zone transfer size, in bytes. */
159 	size_t max_transfer_size;
160 	/** The maximum auth zone transfer time taken, in msec. */
161 	int max_transfer_time;
162 };
163 
164 /**
165  * Auth data. One domain name, and the RRs to go with it.
166  */
167 struct auth_data {
168 	/** rbtree node, key is name only */
169 	rbnode_type node;
170 	/** domain name */
171 	uint8_t* name;
172 	/** length of name */
173 	size_t namelen;
174 	/** number of labels in name */
175 	int namelabs;
176 	/** the data rrsets, with different types, linked list.
177 	 * if the list if NULL the node would be an empty non-terminal,
178 	 * but in this data structure such nodes that represent an empty
179 	 * non-terminal are not needed; they just don't exist.
180 	 */
181 	struct auth_rrset* rrsets;
182 };
183 
184 /**
185  * A auth data RRset
186  */
187 struct auth_rrset {
188 	/** next in list */
189 	struct auth_rrset* next;
190 	/** RR type in host byteorder */
191 	uint16_t type;
192 	/** RRset data item */
193 	struct packed_rrset_data* data;
194 };
195 
196 /**
197  * Authoritative zone transfer structure.
198  * Create and destroy needs the auth_zones* biglock.
199  * The structure consists of different tasks.  Each can be unowned (-1) or
200  * owner by a worker (worker-num).  A worker can pick up a task and then do
201  * it.  This means the events (timeouts, sockets) are for that worker.
202  *
203  * (move this to tasks).
204  * They don't have locks themselves, the worker (that owns it) uses it,
205  * also as part of callbacks, hence it has separate zonename pointers for
206  * lookup in the main zonetree.  If the zone has no transfers, this
207  * structure is not created.
208  */
209 struct auth_xfer {
210 	/** rbtree node, key is name and class */
211 	rbnode_type node;
212 
213 	/** lock on this structure, and on the workernum elements of the
214 	 * tasks.  First hold the tree-lock in auth_zones, find the auth_xfer,
215 	 * lock this lock.  Then a worker can reassign itself to fill up
216 	 * one of the tasks.
217 	 * Once it has the task assigned to it, the worker can access the
218 	 * other elements of the task structure without a lock, because that
219 	 * is necessary for the eventloop and callbacks from that.
220 	 * The auth_zone->lock is locked before this lock.
221 	 */
222 	lock_basic_type lock;
223 
224 	/** zone name, in uncompressed wireformat */
225 	uint8_t* name;
226 	/** length of zone name */
227 	size_t namelen;
228 	/** number of labels in zone name */
229 	int namelabs;
230 	/** the class of this zone, in host byteorder.
231 	 * uses 'dclass' to not conflict with c++ keyword class. */
232 	uint16_t dclass;
233 
234 	/** task to wait for next-probe-timeout,
235 	 * once timeouted, see if a SOA probe is needed, or already
236 	 * in progress */
237 	struct auth_nextprobe* task_nextprobe;
238 
239 	/** task for SOA probe.  Check if the zone can be updated */
240 	struct auth_probe* task_probe;
241 
242 	/** Task for transfer.  Transferring and updating the zone.  This
243 	 * includes trying (potentially) several upstream masters.  Downloading
244 	 * and storing the zone */
245 	struct auth_transfer* task_transfer;
246 
247 	/** a notify was received, but a zone transfer or probe was already
248 	 * acted on.
249 	 * However, the zone transfer could signal a newer serial number.
250 	 * The serial number of that notify is saved below.  The transfer and
251 	 * probe tasks should check this once done to see if they need to
252 	 * restart the transfer task for the newer notify serial.
253 	 * Hold the lock to access this member (and the serial).
254 	 */
255 	int notify_received;
256 	/** true if the notify_received has a serial number */
257 	int notify_has_serial;
258 	/** serial number of the notify */
259 	uint32_t notify_serial;
260 	/** the list of masters for checking notifies.  This list is
261 	 * empty on start, and a copy of the list from the probe_task when
262 	 * it is done looking them up. */
263 	struct auth_master* allow_notify_list;
264 
265 	/* protected by the lock on the structure, information about
266 	 * the loaded authority zone. */
267 	/** is the zone currently considered expired? after expiry also older
268          * serial numbers are allowed (not just newer) */
269 	int zone_expired;
270 	/** do we have a zone (if 0, no zone data at all) */
271 	int have_zone;
272 	/** the time when zone was transferred from upstream */
273 	time_t soa_zone_acquired;
274 
275 	/** current serial (from SOA), if we have no zone, 0 */
276 	uint32_t serial;
277 	/** retry time (from SOA), time to wait with next_probe
278 	 * if no master responds */
279 	time_t retry;
280 	/** refresh time (from SOA), time to wait with next_probe
281 	 * if everything is fine */
282 	time_t refresh;
283 	/** expiry time (from SOA), time until zone data is not considered
284 	 * valid any more, if no master responds within this time, either
285 	 * with the current zone or a new zone. */
286 	time_t expiry;
287 
288 	/** zone lease start time (start+expiry is expiration time).
289 	 * this is renewed every SOA probe and transfer.  On zone load
290 	 * from zonefile it is also set (with probe set soon to check) */
291 	time_t lease_time;
292 
293 	/** The maximum auth zone transfer size, in bytes. */
294 	size_t max_transfer_size;
295 	/** The maximum auth zone transfer time taken, in msec. */
296 	int max_transfer_time;
297 	/** the zone is an rpz zone */
298 	int is_rpz;
299 	/** the number of IXFRs since the last full transfer. */
300 	int num_ixfrs;
301 };
302 
303 /**
304  * The next probe task.
305  * This task consists of waiting for the probetimeout.  It is a task because
306  * it needs an event in the eventtable.  Once the timeout has passed, that
307  * worker can (potentially) become the auth_probe worker, or if another worker
308  * is already doing that, do nothing.  Tasks becomes unowned.
309  * The probe worker, if it detects nothing has to be done picks up this task,
310  * if unowned.
311  */
312 struct auth_nextprobe {
313 	/* Worker pointer. NULL means unowned. */
314 	struct worker* worker;
315 	/* module env for this task */
316 	struct module_env* env;
317 
318 	/** increasing backoff for failures */
319 	time_t backoff;
320 	/** Timeout for next probe (for SOA) */
321 	time_t next_probe;
322 	/** timeout callback for next_probe or expiry(if that is sooner).
323 	 * it is on the worker's event_base */
324 	struct comm_timer* timer;
325 };
326 
327 /**
328  * The probe task.
329  * Send a SOA UDP query to see if the zone needs to be updated (or similar,
330  * potential, HTTP probe query) and check serial number.
331  * If yes, start the auth_transfer task.  If no, make sure auth_nextprobe
332  * timeout wait task is running.
333  * Needs to be a task, because the UDP query needs an event entry.
334  * This task could also be started by eg. a NOTIFY being received, even though
335  * another worker is performing the nextprobe task (and that worker keeps
336  * waiting uninterrupted).
337  */
338 struct auth_probe {
339 	/* Worker pointer. NULL means unowned. */
340 	struct worker* worker;
341 	/* module env for this task */
342 	struct module_env* env;
343 
344 	/** list of upstream masters for this zone, from config */
345 	struct auth_master* masters;
346 
347 	/** for the hostname lookups, which master is current */
348 	struct auth_master* lookup_target;
349 	/** for the lookup, the callback unique info */
350 	void* lookup_unique_info;
351 	/** are we looking up A or AAAA, first A, then AAAA (if ip6 enabled) */
352 	int lookup_aaaa;
353 	/** we only want to do lookups for making config work (for notify),
354 	 * don't proceed with UDP SOA probe queries */
355 	int only_lookup;
356 	/** we have seen a new lease this scan, because one of the masters
357 	 * replied with the current SOA serial version */
358 	int have_new_lease;
359 
360 	/** once notified, or the timeout has been reached. a scan starts. */
361 	/** the scan specific target (notify source), or NULL if none */
362 	struct auth_master* scan_specific;
363 	/** scan tries all the upstream masters. the scan current target.
364 	 * or NULL if not working on sequential scan */
365 	struct auth_master* scan_target;
366 	/** if not NULL, the specific addr for the current master */
367 	struct auth_addr* scan_addr;
368 
369 	/** dns id of packet in flight */
370 	uint16_t id;
371 	/** the SOA probe udp event.
372 	 * on the workers event base. */
373 	struct comm_point* cp;
374 	/** is the cp for ip6 or ip4 */
375 	int cp_is_ip6;
376 	/** timeout for packets.
377 	 * on the workers event base. */
378 	struct comm_timer* timer;
379 	/** timeout in msec */
380 	int timeout;
381 };
382 
383 /**
384  * The transfer task.
385  * Once done, make sure the nextprobe waiting task is running, whether done
386  * with failure or success.  If failure, use shorter timeout for wait time.
387  */
388 struct auth_transfer {
389 	/* Worker pointer. NULL means unowned. */
390 	struct worker* worker;
391 	/* module env for this task */
392 	struct module_env* env;
393 
394 	/** xfer data that has been transferred, the data is applied
395 	 * once the transfer has completed correctly */
396 	struct auth_chunk* chunks_first;
397 	/** last element in chunks list (to append new data at the end) */
398 	struct auth_chunk* chunks_last;
399 	/** running total of bytes held in chunks_first..chunks_last */
400 	size_t chunks_total;
401 	/** start time of the transfer */
402 	struct timeval start_time;
403 
404 	/** list of upstream masters for this zone, from config */
405 	struct auth_master* masters;
406 
407 	/** for the hostname lookups, which master is current */
408 	struct auth_master* lookup_target;
409 	/** for the lookup, the callback unique info */
410 	void* lookup_unique_info;
411 	/** are we looking up A or AAAA, first A, then AAAA (if ip6 enabled) */
412 	int lookup_aaaa;
413 
414 	/** once notified, or the timeout has been reached. a scan starts. */
415 	/** the scan specific target (notify source), or NULL if none */
416 	struct auth_master* scan_specific;
417 	/** scan tries all the upstream masters. the scan current target.
418 	 * or NULL if not working on sequential scan */
419 	struct auth_master* scan_target;
420 	/** what address we are scanning for the master, or NULL if the
421 	 * master is in IP format itself */
422 	struct auth_addr* scan_addr;
423 	/** the zone transfer in progress (or NULL if in scan).  It is
424 	 * from this master */
425 	struct auth_master* master;
426 
427 	/** failed ixfr transfer, retry with axfr (to the current master),
428 	 * the IXFR was 'REFUSED', 'SERVFAIL', 'NOTIMPL' or the contents of
429 	 * the IXFR did not apply cleanly (out of sync, delete of nonexistent
430 	 * data or add of duplicate data).  Flag is cleared once the retry
431 	 * with axfr is done. */
432 	int ixfr_fail;
433 	/** we saw an ixfr-indicating timeout, count of them */
434 	int ixfr_possible_timeout_count;
435 	/** we are doing IXFR right now */
436 	int on_ixfr;
437 	/** did we detect the current AXFR/IXFR serial number yet, 0 not yet,
438 	 * 1 we saw the first, 2 we saw the second, 3 must be last SOA in xfr*/
439 	int got_xfr_serial;
440 	/** number of RRs scanned for AXFR/IXFR detection */
441 	size_t rr_scan_num;
442 	/** we are doing an IXFR but we detected an AXFR contents */
443 	int on_ixfr_is_axfr;
444 	/** the serial number for the current AXFR/IXFR incoming reply,
445 	 * for IXFR, the outermost SOA records serial */
446 	uint32_t incoming_xfr_serial;
447 
448 	/** dns id of AXFR query */
449 	uint16_t id;
450 	/** the transfer (TCP) to the master.
451 	 * on the workers event base. */
452 	struct comm_point* cp;
453 	/** timeout for the transfer.
454 	 * on the workers event base. */
455 	struct comm_timer* timer;
456 };
457 
458 /** list of addresses */
459 struct auth_addr {
460 	/** next in list */
461 	struct auth_addr* next;
462 	/** IP address */
463 	struct sockaddr_storage addr;
464 	/** addr length */
465 	socklen_t addrlen;
466 };
467 
468 /** auth zone master upstream, and the config settings for it */
469 struct auth_master {
470 	/** next master in list */
471 	struct auth_master* next;
472 	/** master IP address (and port), or hostname, string */
473 	char* host;
474 	/** for http, filename */
475 	char* file;
476 	/** use HTTP for this master */
477 	int http;
478 	/** use IXFR for this master */
479 	int ixfr;
480 	/** this is an allow notify member, the master can send notifies
481 	 * to us, but we don't send SOA probes, or zone transfer from it */
482 	int allow_notify;
483 	/** use ssl for channel */
484 	int ssl;
485 	/** the port number (for urls) */
486 	int port;
487 	/** if the host is a hostname, the list of resolved addrs, if any*/
488 	struct auth_addr* list;
489 };
490 
491 /** auth zone master zone transfer data chunk */
492 struct auth_chunk {
493 	/** next chunk in list */
494 	struct auth_chunk* next;
495 	/** the data from this chunk, this is what was received.
496 	 * for an IXFR that means results from comm_net tcp actions,
497 	 * packets. also for an AXFR. For HTTP a zonefile chunk. */
498 	uint8_t* data;
499 	/** length of allocated data */
500 	size_t len;
501 };
502 
503 /**
504  * Create auth zones structure
505  */
506 struct auth_zones* auth_zones_create(void);
507 
508 /**
509  * Apply configuration to auth zones.  Reads zonefiles.
510  * @param az: auth zones structure
511  * @param cfg: config to apply.
512  * @param setup: if true, also sets up values in the auth zones structure
513  * @param is_rpz: set to 1 if at least one RPZ zone is configured.
514  * @param env: environment for offline verification.
515  * @param mods: modules in environment.
516  * @return false on failure.
517  */
518 int auth_zones_apply_cfg(struct auth_zones* az, struct config_file* cfg,
519 	int setup, int* is_rpz, struct module_env* env,
520 	struct module_stack* mods);
521 
522 /** initial pick up of worker timeouts, ties events to worker event loop
523  * @param az: auth zones structure
524  * @param env: worker env, of first worker that receives the events (if any)
525  * 	in its eventloop.
526  */
527 void auth_xfer_pickup_initial(struct auth_zones* az, struct module_env* env);
528 
529 /**
530  * Cleanup auth zones.  This removes all events from event bases.
531  * Stops the xfr tasks.  But leaves zone data.
532  * @param az: auth zones structure.
533  */
534 void auth_zones_cleanup(struct auth_zones* az);
535 
536 /**
537  * Delete auth zones structure
538  */
539 void auth_zones_delete(struct auth_zones* az);
540 
541 /**
542  * Write auth zone data to file, in zonefile format.
543  */
544 int auth_zone_write_file(struct auth_zone* z, const char* fname);
545 
546 /**
547  * Use auth zones to lookup the answer to a query.
548  * The query is from the iterator.  And the auth zones attempts to provide
549  * the answer instead of going to the internet.
550  *
551  * @param az: auth zones structure.
552  * @param qinfo: query info to lookup.
553  * @param region: region to use to allocate the reply in.
554  * @param msg: reply is stored here (if one).
555  * @param fallback: if true, fallback to making a query to the internet.
556  * @param dp_nm: name of delegation point to look for.  This zone is used
557  *	to answer the query.
558  *	If the dp_nm is not found, fallback is set to true and false returned.
559  * @param dp_nmlen: length of dp_nm.
560  * @return 0: failure (an error of some sort, like servfail).
561  *         if 0 and fallback is true, fallback to the internet.
562  *         if 0 and fallback is false, like getting servfail.
563  *         If true, an answer is available.
564  */
565 int auth_zones_lookup(struct auth_zones* az, struct query_info* qinfo,
566 	struct regional* region, struct dns_msg** msg, int* fallback,
567 	uint8_t* dp_nm, size_t dp_nmlen);
568 
569 /**
570  * Answer query from auth zone.  Create authoritative answer.
571  * @param az: auth zones structure.
572  * @param env: the module environment.
573  * @param qinfo: query info (parsed).
574  * @param edns: edns info (parsed).
575  * @param buf: buffer with query ID and flags, also for reply.
576  * @param repinfo: reply information for a communication point.
577  * @param temp: temporary storage region.
578  * @return false if not answered
579  */
580 int auth_zones_downstream_answer(struct auth_zones* az, struct module_env* env,
581 	struct query_info* qinfo, struct edns_data* edns,
582 	struct comm_reply* repinfo, struct sldns_buffer* buf,
583 	struct regional* temp);
584 
585 /**
586  * Find the auth zone that is above the given qname.
587  * Return NULL when there is no auth_zone above the give name, otherwise
588  * returns the closest auth_zone above the qname that pertains to it.
589  * @param az: auth zones structure.
590  * @param name: query to look up for.
591  * @param name_len: length of name.
592  * @param dclass: class of zone to find.
593  * @return NULL or auth_zone that pertains to the query.
594  */
595 struct auth_zone* auth_zones_find_zone(struct auth_zones* az,
596 	uint8_t* name, size_t name_len, uint16_t dclass);
597 
598 /** find an auth zone by name (exact match by name or NULL returned) */
599 struct auth_zone* auth_zone_find(struct auth_zones* az, uint8_t* nm,
600 	size_t nmlen, uint16_t dclass);
601 
602 /** find an xfer zone by name (exact match by name or NULL returned) */
603 struct auth_xfer* auth_xfer_find(struct auth_zones* az, uint8_t* nm,
604 	size_t nmlen, uint16_t dclass);
605 
606 /** create an auth zone. returns wrlocked zone. caller must have wrlock
607  * on az. returns NULL on malloc failure */
608 struct auth_zone* auth_zone_create(struct auth_zones* az, uint8_t* nm,
609 	size_t nmlen, uint16_t dclass);
610 
611 /** set auth zone zonefile string. caller must have lock on zone */
612 int auth_zone_set_zonefile(struct auth_zone* z, char* zonefile);
613 
614 /** set auth zone fallback. caller must have lock on zone.
615  * fallbackstr is "yes" or "no". false on parse failure. */
616 int auth_zone_set_fallback(struct auth_zone* z, char* fallbackstr);
617 
618 /** see if the auth zone for the name can fallback
619  * @param az: auth zones
620  * @param nm: name of delegation point.
621  * @param nmlen: length of nm.
622  * @param dclass: class of zone to look for.
623  * @return true if fallback_enabled is true. false if not.
624  * if the zone does not exist, fallback is true (more lenient)
625  * also true if zone does not do upstream requests.
626  */
627 int auth_zones_can_fallback(struct auth_zones* az, uint8_t* nm, size_t nmlen,
628 	uint16_t dclass);
629 
630 /** process notify for auth zones.
631  * first checks the access list.  Then processes the notify. This starts
632  * the probe sequence or it notes the serial number (if any)
633  * @param az: auth zones structure.
634  * @param env: module env of the worker that is handling the notify. it will
635  * 	pick up the task probe (or transfer), unless already in progress by
636  * 	another worker.
637  * @param nm: name of the zone.  Uncompressed. from query.
638  * @param nmlen: length of name.
639  * @param dclass: class of zone.
640  * @param addr: source address of notify
641  * @param addrlen: length of addr.
642  * @param has_serial: if true, the notify has a serial attached.
643  * @param serial: the serial number, if has_serial is true.
644  * @param refused: is set to true on failure to note refused access.
645  * @return fail on failures (refused is false) and when access is
646  * 	denied (refused is true).  True when processed.
647  */
648 int auth_zones_notify(struct auth_zones* az, struct module_env* env,
649 	uint8_t* nm, size_t nmlen, uint16_t dclass,
650 	struct sockaddr_storage* addr, socklen_t addrlen, int has_serial,
651 	uint32_t serial, int* refused);
652 
653 /** process notify packet and read serial number from SOA.
654  * returns 0 if no soa record in the notify */
655 int auth_zone_parse_notify_serial(struct sldns_buffer* pkt, uint32_t *serial);
656 
657 /** for the zone and if not already going, starts the probe sequence.
658  * false if zone cannot be found.  This is like a notify arrived and was
659  * accepted for that zone. */
660 int auth_zones_startprobesequence(struct auth_zones* az,
661 	struct module_env* env, uint8_t* nm, size_t nmlen, uint16_t dclass);
662 
663 /** read auth zone from zonefile. caller must lock zone. false on failure */
664 int auth_zone_read_zonefile(struct auth_zone* z, struct config_file* cfg);
665 
666 /** find the apex SOA RRset, if it exists. NULL if no SOA RRset. */
667 struct auth_rrset* auth_zone_get_soa_rrset(struct auth_zone* z);
668 
669 /** find serial number of zone or false if none (no SOA record) */
670 int auth_zone_get_serial(struct auth_zone* z, uint32_t* serial);
671 
672 /** Find auth_zone SOA and populate the values in xfr(soa values). */
673 int xfr_find_soa(struct auth_zone* z, struct auth_xfer* xfr);
674 
675 /** compare auth_zones for sorted rbtree */
676 int auth_zone_cmp(const void* z1, const void* z2);
677 
678 /** compare auth_data for sorted rbtree */
679 int auth_data_cmp(const void* z1, const void* z2);
680 
681 /** compare auth_xfer for sorted rbtree */
682 int auth_xfer_cmp(const void* z1, const void* z2);
683 
684 /** Create auth_xfer structure.
685  * Caller must have wrlock on az. Returns locked xfer zone.
686  * @param az: zones structure.
687  * @param z: zone with name and class
688  * @return xfer zone or NULL
689  */
690 struct auth_xfer* auth_xfer_create(struct auth_zones* az, struct auth_zone* z);
691 
692 /**
693  * Set masters in auth xfer structure from config.
694  * @param list: pointer to start of list.  The malloced list is returned here.
695  * @param c: the config items to copy over.
696  * @param with_http: if true, http urls are also included, before the masters.
697  * @return false on failure.
698  */
699 int xfer_set_masters(struct auth_master** list, struct config_auth* c,
700 	int with_http);
701 
702 /** xfer nextprobe timeout callback, this is part of task_nextprobe */
703 void auth_xfer_timer(void* arg);
704 
705 /** callback for commpoint udp replies to task_probe */
706 int auth_xfer_probe_udp_callback(struct comm_point* c, void* arg, int err,
707         struct comm_reply* repinfo);
708 /** callback for task_transfer tcp connections */
709 int auth_xfer_transfer_tcp_callback(struct comm_point* c, void* arg, int err,
710         struct comm_reply* repinfo);
711 /** callback for task_transfer http connections */
712 int auth_xfer_transfer_http_callback(struct comm_point* c, void* arg, int err,
713         struct comm_reply* repinfo);
714 /** xfer probe timeout callback, part of task_probe */
715 void auth_xfer_probe_timer_callback(void* arg);
716 /** xfer transfer timeout callback, part of task_transfer */
717 void auth_xfer_transfer_timer_callback(void* arg);
718 /** mesh callback for task_probe on lookup of host names */
719 void auth_xfer_probe_lookup_callback(void* arg, int rcode,
720 	struct sldns_buffer* buf, enum sec_status sec, char* why_bogus,
721 	int was_ratelimited);
722 /** mesh callback for task_transfer on lookup of host names */
723 void auth_xfer_transfer_lookup_callback(void* arg, int rcode,
724 	struct sldns_buffer* buf, enum sec_status sec, char* why_bogus,
725 	int was_ratelimited);
726 
727 /*
728  * Compares two 32-bit serial numbers as defined in RFC1982.  Returns
729  * <0 if a < b, 0 if a == b, and >0 if a > b.  The result is undefined
730  * if a != b but neither is greater or smaller (see RFC1982 section
731  * 3.2.).
732  */
733 int compare_serial(uint32_t a, uint32_t b);
734 
735 /**
736  * Generate ZONEMD digest for the auth zone.
737  * @param z: the auth zone to digest.
738  * 	omits zonemd at apex and its RRSIG from the digest.
739  * @param scheme: the collation scheme to use.  Numbers as defined for ZONEMD.
740  * @param hashalgo: the hash algo, from the registry defined for ZONEMD type.
741  * @param hash: the result buffer.
742  * @param buflen: size of the result buffer, must be large enough. or the
743  * 	routine fails.
744  * @param resultlen: size of the hash in the result buffer of the result.
745  * @param region: temp region for allocs during canonicalisation.
746  * @param buf: temp buffer during canonicalisation.
747  * @param reason: failure reason, returns a string, NULL on success.
748  * @return false on failure.
749  */
750 int auth_zone_generate_zonemd_hash(struct auth_zone* z, int scheme,
751 	int hashalgo, uint8_t* hash, size_t buflen, size_t* resultlen,
752 	struct regional* region, struct sldns_buffer* buf, char** reason);
753 
754 /** ZONEMD scheme definitions */
755 #define ZONEMD_SCHEME_SIMPLE 1
756 
757 /** ZONEMD hash algorithm definition for SHA384 */
758 #define ZONEMD_ALGO_SHA384 1
759 /** ZONEMD hash algorithm definition for SHA512 */
760 #define ZONEMD_ALGO_SHA512 2
761 
762 /** returns true if a zonemd hash algo is supported */
763 int zonemd_hashalgo_supported(int hashalgo);
764 /** returns true if a zonemd scheme is supported */
765 int zonemd_scheme_supported(int scheme);
766 
767 /**
768  * Check ZONEMD digest for the auth zone.
769  * @param z: auth zone to digest.
770  * @param scheme: zonemd scheme.
771  * @param hashalgo: zonemd hash algorithm.
772  * @param hash: the hash to check.
773  * @param hashlen: length of hash buffer.
774  * @param region: temp region for allocs during canonicalisation.
775  * @param buf: temp buffer during canonicalisation.
776  * @param reason: string returned with failure reason.
777  * 	If the hash cannot be checked, but it is allowed, for unknown
778  * 	algorithms, the routine returns success, and the reason is nonNULL,
779  * 	with the allowance reason.
780  * @return false on failure.
781  */
782 int auth_zone_generate_zonemd_check(struct auth_zone* z, int scheme,
783 	int hashalgo, uint8_t* hash, size_t hashlen, struct regional* region,
784 	struct sldns_buffer* buf, char** reason);
785 
786 /**
787  * Perform ZONEMD checks and verification for the auth zone.
788  * This includes DNSSEC verification if applicable.
789  * @param z: auth zone to check.  Caller holds lock. wrlock.
790  * @param env: with temp region, buffer and config.
791  * @param mods: module stack for validator env.
792  * @param result: if not NULL, result string strdupped in here.
793  * @param offline: if true, there is no spawned lookup when online is needed.
794  * 	Those zones are skipped for ZONEMD checking.
795  * @param only_online: if true, only for ZONEMD that need online lookup
796  * 	of DNSKEY chain of trust are processed.
797  */
798 void auth_zone_verify_zonemd(struct auth_zone* z, struct module_env* env,
799 	struct module_stack* mods, char** result, int offline,
800 	int only_online);
801 
802 /** mesh callback for zonemd on lookup of dnskey */
803 void auth_zonemd_dnskey_lookup_callback(void* arg, int rcode,
804 	struct sldns_buffer* buf, enum sec_status sec, char* why_bogus,
805 	int was_ratelimited);
806 
807 /**
808  * Check the ZONEMD records that need online DNSSEC chain lookups,
809  * for them spawn the lookup process to get it checked out.
810  * Attaches the lookup process to the worker event base and mesh state.
811  * @param az: auth zones, every zones is checked.
812  * @param env: env of the worker where the task is attached.
813  */
814 void auth_zones_pickup_zonemd_verify(struct auth_zones* az,
815 	struct module_env* env);
816 
817 /** Get memory usage for auth zones. The routine locks and unlocks
818  * for reading. */
819 size_t auth_zones_get_mem(struct auth_zones* zones);
820 
821 /**
822  * Initial pick up of the auth zone nextprobe timeout and that turns
823  * into further zone transfer work, if any. Also sets the lease time.
824  * @param x: xfer structure, locked by caller.
825  * @param env: environment of the worker that picks up the task.
826  */
827 void auth_xfer_pickup_initial_zone(struct auth_xfer* x,
828 	struct module_env* env);
829 
830 /**
831  * Initial pick up of the auth zone, it sets the acquired time.
832  * @param z: the zone, write locked by caller.
833  * @param env: environment of the worker, with current time.
834  */
835 void auth_zone_pickup_initial_zone(struct auth_zone* z,
836 	struct module_env* env);
837 
838 /**
839  * Delete auth xfer structure
840  * @param xfr: delete this xfer and its tasks.
841  */
842 void auth_xfer_delete(struct auth_xfer* xfr);
843 
844 /**
845  * Disown tasks from the xfr that belong to this worker.
846  * Only tasks for the worker in question, the comm point and timer
847  * delete functions need to run in the thread of that worker to be
848  * able to delete the callback from the event base.
849  * @param xfr: xfr structure
850  * @param worker: the worker for which to stop tasks.
851  */
852 void xfr_disown_tasks(struct auth_xfer* xfr, struct worker* worker);
853 
854 /** count number of open and closed parenthesis in a chunkline */
855 int chunkline_count_parens(struct sldns_buffer* buf, size_t start);
856 
857 /** Clear data in auth zone */
858 void auth_zone_clear_data(struct auth_zone* z);
859 
860 #endif /* SERVICES_AUTHZONE_H */
861