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