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