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