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