xref: /freebsd/contrib/unbound/util/config_file.h (revision 7a789145f88a6aceacc59029a0cafe7de7aeefea)
1 /*
2  * util/config_file.h - reads and stores the config file for unbound.
3  *
4  * Copyright (c) 2007, 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 functions for the config file.
40  */
41 
42 #ifndef UTIL_CONFIG_FILE_H
43 #define UTIL_CONFIG_FILE_H
44 #include "sldns/rrdef.h"
45 struct config_stub;
46 struct config_auth;
47 struct config_view;
48 struct config_strlist;
49 struct config_str2list;
50 struct config_str3list;
51 struct config_strbytelist;
52 struct module_qstate;
53 struct sock_list;
54 struct ub_packed_rrset_key;
55 struct regional;
56 
57 /** Default value for PROBE_MAXRTO */
58 #define PROBE_MAXRTO_DEFAULT 12000
59 
60 /** List head for strlist processing, used for append operation. */
61 struct config_strlist_head {
62 	/** first in list of text items */
63 	struct config_strlist* first;
64 	/** last in list of text items */
65 	struct config_strlist* last;
66 };
67 
68 /**
69  * The configuration options.
70  * Strings are malloced.
71  */
72 struct config_file {
73 	/** verbosity level as specified in the config file */
74 	int verbosity;
75 
76 	/** statistics interval (in seconds) */
77 	int stat_interval;
78 	/** if false, statistics values are reset after printing them */
79 	int stat_cumulative;
80 	/** if true, the statistics are kept in greater detail */
81 	int stat_extended;
82 	/** if true, inhibits a lot of =0 lines from the extended stats output */
83 	int stat_inhibit_zero;
84 
85 	/** number of threads to create */
86 	int num_threads;
87 
88 	/** port on which queries are answered. */
89 	int port;
90 	/** do ip4 query support. */
91 	int do_ip4;
92 	/** do ip6 query support. */
93 	int do_ip6;
94 	/** do nat64 on queries */
95 	int do_nat64;
96 	/** prefer ip4 upstream queries. */
97 	int prefer_ip4;
98 	/** prefer ip6 upstream queries. */
99 	int prefer_ip6;
100 	/** do udp query support. */
101 	int do_udp;
102 	/** do tcp query support. */
103 	int do_tcp;
104 	/** max number of queries on a reuse connection. */
105 	size_t max_reuse_tcp_queries;
106 	/** timeout for REUSE entries in milliseconds. */
107 	int tcp_reuse_timeout;
108 	/** timeout in milliseconds for TCP queries to auth servers. */
109 	int tcp_auth_query_timeout;
110 	/** tcp upstream queries (no UDP upstream queries) */
111 	int tcp_upstream;
112 	/** udp upstream enabled when no UDP downstream is enabled (do_udp no)*/
113 	int udp_upstream_without_downstream;
114 	/** maximum segment size of tcp socket which queries are answered */
115 	int tcp_mss;
116 	/** maximum segment size of tcp socket for outgoing queries */
117 	int outgoing_tcp_mss;
118 	/** tcp idle timeout, in msec */
119 	int tcp_idle_timeout;
120 	/** do edns tcp keepalive */
121 	int do_tcp_keepalive;
122 	/** tcp keepalive timeout, in msec */
123 	int tcp_keepalive_timeout;
124 	/** timeout of packets sitting in the socket queue */
125 	int sock_queue_timeout;
126 	/** proxy protocol ports */
127 	struct config_strlist* proxy_protocol_port;
128 
129 	/** private key file for dnstcp-ssl service (enabled if not NULL) */
130 	char* ssl_service_key;
131 	/** public key file for dnstcp-ssl service */
132 	char* ssl_service_pem;
133 	/** port on which to provide ssl service */
134 	int ssl_port;
135 	/** if outgoing tcp connections use SSL */
136 	int ssl_upstream;
137 	/** cert bundle for outgoing connections */
138 	char* tls_cert_bundle;
139 	/** should the system certificate store get added to the cert bundle */
140 	int tls_win_cert;
141 	/** additional tls ports */
142 	struct config_strlist* tls_additional_port;
143 	/** secret key used to encrypt and decrypt TLS session ticket */
144 	struct config_strlist_head tls_session_ticket_keys;
145 	/** TLS ciphers */
146 	char* tls_ciphers;
147 	/** TLS chiphersuites (TLSv1.3) */
148 	char* tls_ciphersuites;
149 	/** if SNI is to be used */
150 	int tls_use_sni;
151 	/** TLS protocols */
152 	char* tls_protocols;
153 
154 	/** port on which to provide DNS over HTTPS service */
155 	int https_port;
156 	/** endpoint for HTTP service */
157 	char* http_endpoint;
158 	/** MAX_CONCURRENT_STREAMS HTTP/2 setting */
159 	uint32_t http_max_streams;
160 	/** maximum size of all HTTP2 query buffers combined. */
161 	size_t http_query_buffer_size;
162 	/** maximum size of all HTTP2 response buffers combined. */
163 	size_t http_response_buffer_size;
164 	/** set TCP_NODELAY option for http sockets */
165 	int http_nodelay;
166 	/** Disable TLS for http sockets downstream */
167 	int http_notls_downstream;
168 
169 	/** port on which to provide DNS over QUIC service */
170 	int quic_port;
171 	/** size of the quic data, max bytes */
172 	size_t quic_size;
173 
174 	/** outgoing port range number of ports (per thread) */
175 	int outgoing_num_ports;
176 	/** number of outgoing tcp buffers per (per thread) */
177 	size_t outgoing_num_tcp;
178 	/** number of incoming tcp buffers per (per thread) */
179 	size_t incoming_num_tcp;
180 	/** allowed udp port numbers, array with 0 if not allowed */
181 	int* outgoing_avail_ports;
182 
183 	/** EDNS buffer size to use */
184 	size_t edns_buffer_size;
185 	/** size of the stream wait buffers, max */
186 	size_t stream_wait_size;
187 	/** number of bytes buffer size for DNS messages */
188 	size_t msg_buffer_size;
189 	/** size of the message cache */
190 	size_t msg_cache_size;
191 	/** slabs in the message cache. */
192 	size_t msg_cache_slabs;
193 	/** number of queries every thread can service */
194 	size_t num_queries_per_thread;
195 	/** number of msec to wait before items can be jostled out */
196 	size_t jostle_time;
197 	/** size of the rrset cache */
198 	size_t rrset_cache_size;
199 	/** slabs in the rrset cache */
200 	size_t rrset_cache_slabs;
201 	/** host cache ttl in seconds */
202 	int host_ttl;
203 	/** number of slabs in the infra host cache */
204 	size_t infra_cache_slabs;
205 	/** max number of hosts in the infra cache */
206 	size_t infra_cache_numhosts;
207 	/** min value for infra cache rtt (min retransmit timeout) */
208 	int infra_cache_min_rtt;
209 	/** max value for infra cache rtt (max retransmit timeout) */
210 	int infra_cache_max_rtt;
211 	/** keep probing hosts that are down */
212 	int infra_keep_probing;
213 	/** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
214 	int delay_close;
215 	/** udp_connect enable uses UDP connect to mitigate ICMP side channel */
216 	int udp_connect;
217 
218 	/** the target fetch policy for the iterator */
219 	char* target_fetch_policy;
220 	/** percent*10, how many times in 1000 to pick from the fastest
221 	 * destinations */
222 	int fast_server_permil;
223 	/** number of fastest server to select from */
224 	size_t fast_server_num;
225 
226 	/** automatic interface for incoming messages. Uses ipv6 remapping,
227 	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
228 	int if_automatic;
229 	/** extra ports to open if if_automatic enabled, or NULL for default */
230 	char* if_automatic_ports;
231 	/** SO_RCVBUF size to set on port 53 UDP socket */
232 	size_t so_rcvbuf;
233 	/** SO_SNDBUF size to set on port 53 UDP socket */
234 	size_t so_sndbuf;
235 	/** SO_REUSEPORT requested on port 53 sockets */
236 	int so_reuseport;
237 	/** IP_TRANSPARENT socket option requested on port 53 sockets */
238 	int ip_transparent;
239 	/** IP_FREEBIND socket option request on port 53 sockets */
240 	int ip_freebind;
241 	/** IP_TOS socket option requested on port 53 sockets */
242 	int ip_dscp;
243 
244 	/** number of interfaces to open. If 0 default all interfaces. */
245 	int num_ifs;
246 	/** interface description strings (IP addresses) */
247 	char **ifs;
248 
249 	/** number of outgoing interfaces to open.
250 	 * If 0 default all interfaces. */
251 	int num_out_ifs;
252 	/** outgoing interface description strings (IP addresses) */
253 	char **out_ifs;
254 
255 	/** the root hints */
256 	struct config_strlist* root_hints;
257 	/** the stub definitions, linked list */
258 	struct config_stub* stubs;
259 	/** the forward zone definitions, linked list */
260 	struct config_stub* forwards;
261 	/** the auth zone definitions, linked list */
262 	struct config_auth* auths;
263 	/** the views definitions, linked list */
264 	struct config_view* views;
265 	/** list of donotquery addresses, linked list */
266 	struct config_strlist* donotqueryaddrs;
267 #ifdef CLIENT_SUBNET
268 	/** list of servers we send edns-client-subnet option to and
269 	 * accept option from, linked list */
270 	struct config_strlist* client_subnet;
271 	/** list of zones we send edns-client-subnet option for */
272 	struct config_strlist* client_subnet_zone;
273 	/** opcode assigned by IANA for edns0-client-subnet option */
274 	uint16_t client_subnet_opcode;
275 	/** Do not check whitelist if incoming query contains an ECS record */
276 	int client_subnet_always_forward;
277 	/** Subnet length we are willing to give up privacy for */
278 	uint8_t max_client_subnet_ipv4;
279 	uint8_t max_client_subnet_ipv6;
280 	/** Minimum subnet length we are willing to answer */
281 	uint8_t min_client_subnet_ipv4;
282 	uint8_t min_client_subnet_ipv6;
283 	/** Max number of nodes in the ECS radix tree */
284 	uint32_t max_ecs_tree_size_ipv4;
285 	uint32_t max_ecs_tree_size_ipv6;
286 #endif
287 	/** list of access control entries, linked list */
288 	struct config_str2list* acls;
289 	/** use default localhost donotqueryaddr entries */
290 	int donotquery_localhost;
291 
292 	/** list of tcp connection limitss, linked list */
293 	struct config_str2list* tcp_connection_limits;
294 
295 	/** harden against very small edns buffer sizes */
296 	int harden_short_bufsize;
297 	/** harden against very large query sizes */
298 	int harden_large_queries;
299 	/** harden against spoofed glue (out of zone data) */
300 	int harden_glue;
301 	/** harden against unverified glue */
302 	int harden_unverified_glue;
303 	/** harden against receiving no DNSSEC data for trust anchor */
304 	int harden_dnssec_stripped;
305 	/** harden against queries that fall under known nxdomain names */
306 	int harden_below_nxdomain;
307 	/** harden the referral path, query for NS,A,AAAA and validate */
308 	int harden_referral_path;
309 	/** harden against algorithm downgrade */
310 	int harden_algo_downgrade;
311 	/** harden against unknown records in the authority section and in
312 	 * the additional section */
313 	int harden_unknown_additional;
314 	/** use 0x20 bits in query as random ID bits */
315 	int use_caps_bits_for_id;
316 	/** 0x20 whitelist, domains that do not use capsforid */
317 	struct config_strlist* caps_whitelist;
318 	/** strip away these private addrs from answers, no DNS Rebinding */
319 	struct config_strlist* private_address;
320 	/** allow domain (and subdomains) to use private address space */
321 	struct config_strlist* private_domain;
322 	/** what threshold for unwanted action. */
323 	size_t unwanted_threshold;
324 	/** the number of seconds maximal TTL used for RRsets and messages */
325 	int max_ttl;
326 	/** the number of seconds minimum TTL used for RRsets and messages */
327 	int min_ttl;
328 	/** the number of seconds maximal negative TTL for SOA in auth */
329 	int max_negative_ttl;
330 	/** the number of seconds minimal negative TTL for SOA in auth */
331 	int min_negative_ttl;
332 	/** if prefetching of messages should be performed. */
333 	int prefetch;
334 	/** if prefetching of DNSKEYs should be performed. */
335 	int prefetch_key;
336 	/** deny queries of type ANY with an empty answer */
337 	int deny_any;
338 
339 	/** chrootdir, if not "" or chroot will be done */
340 	char* chrootdir;
341 	/** username to change to, if not "". */
342 	char* username;
343 	/** working directory */
344 	char* directory;
345 	/** filename to log to. */
346 	char* logfile;
347 	/** pidfile to write pid to. */
348 	char* pidfile;
349 
350 	/** should log messages be sent to syslogd */
351 	int use_syslog;
352 	/** log timestamp in ascii UTC */
353 	int log_time_ascii;
354 	/** log timestamp in ISO8601 format */
355 	int log_time_iso;
356 	/** log queries with one line per query */
357 	int log_queries;
358 	/** log replies with one line per reply */
359 	int log_replies;
360 	/** tag log_queries and log_replies for filtering */
361 	int log_tag_queryreply;
362 	/** log every local-zone hit **/
363 	int log_local_actions;
364 	/** log servfails with a reason */
365 	int log_servfail;
366 	/** log identity to report */
367 	char* log_identity;
368 	/** log dest addr for log_replies */
369 	int log_destaddr;
370 	/** log linux thread ID */
371 	int log_thread_id;
372 
373 	/** do not report identity (id.server, hostname.bind) */
374 	int hide_identity;
375 	/** do not report version (version.server, version.bind) */
376 	int hide_version;
377 	/** do not report trustanchor (trustanchor.unbound) */
378 	int hide_trustanchor;
379 	/** do not report the User-Agent HTTP header */
380 	int hide_http_user_agent;
381 	/** identity, hostname is returned if "". */
382 	char* identity;
383 	/** version, package version returned if "". */
384 	char* version;
385 	/** User-Agent for HTTP header */
386 	char* http_user_agent;
387 	/** nsid */
388 	char *nsid_cfg_str;
389 	uint8_t *nsid;
390 	uint16_t nsid_len;
391 
392 	/** the module configuration string */
393 	char* module_conf;
394 
395 	/** files with trusted DS and DNSKEYs in zonefile format, list */
396 	struct config_strlist* trust_anchor_file_list;
397 	/** list of trustanchor keys, linked list */
398 	struct config_strlist* trust_anchor_list;
399 	/** files with 5011 autotrust tracked keys */
400 	struct config_strlist* auto_trust_anchor_file_list;
401 	/** files with trusted DNSKEYs in named.conf format, list */
402 	struct config_strlist* trusted_keys_file_list;
403 	/** insecure domain list */
404 	struct config_strlist* domain_insecure;
405 	/** send key tag query */
406 	int trust_anchor_signaling;
407 	/** enable root key sentinel */
408 	int root_key_sentinel;
409 
410 	/** if not 0, this value is the validation date for RRSIGs */
411 	int32_t val_date_override;
412 	/** the minimum for signature clock skew */
413 	int32_t val_sig_skew_min;
414 	/** the maximum for signature clock skew */
415 	int32_t val_sig_skew_max;
416 	/** max number of query restarts, number of IPs to probe */
417 	int32_t val_max_restart;
418 	/** this value sets the number of seconds before revalidating bogus */
419 	int bogus_ttl;
420 	/** should validator clean additional section for secure msgs */
421 	int val_clean_additional;
422 	/** log bogus messages by the validator */
423 	int val_log_level;
424 	/** squelch val_log_level to log - this is library goes to callback */
425 	int val_log_squelch;
426 	/** should validator allow bogus messages to go through */
427 	int val_permissive_mode;
428 	/** use cached NSEC records to synthesise (negative) answers */
429 	int aggressive_nsec;
430 	/** ignore the CD flag in incoming queries and refuse them bogus data */
431 	int ignore_cd;
432 	/** disable EDNS DO flag in outgoing requests */
433 	int disable_edns_do;
434 	/** serve expired entries and prefetch them */
435 	int serve_expired;
436 	/** serve expired entries until TTL after expiration */
437 	int serve_expired_ttl;
438 	/** reset serve expired TTL after failed update attempt */
439 	int serve_expired_ttl_reset;
440 	/** TTL for the serve expired replies */
441 	int serve_expired_reply_ttl;
442 	/** serve expired entries only after trying to update the entries and this
443 	 *  timeout (in milliseconds) is reached */
444 	int serve_expired_client_timeout;
445 	/** serve original TTLs rather than decrementing ones */
446 	int serve_original_ttl;
447 	/** nsec3 maximum iterations per key size, string */
448 	char* val_nsec3_key_iterations;
449 	/** if zonemd failures are permitted, only logged */
450 	int zonemd_permissive_mode;
451 	/** autotrust add holddown time, in seconds */
452 	unsigned int add_holddown;
453 	/** autotrust del holddown time, in seconds */
454 	unsigned int del_holddown;
455 	/** autotrust keep_missing time, in seconds. 0 is forever. */
456 	unsigned int keep_missing;
457 	/** permit small holddown values, allowing 5011 rollover very fast */
458 	int permit_small_holddown;
459 
460 	/** size of the key cache */
461 	size_t key_cache_size;
462 	/** slabs in the key cache. */
463 	size_t key_cache_slabs;
464 	/** size of the neg cache */
465 	size_t neg_cache_size;
466 
467 	/** local zones config */
468 	struct config_str2list* local_zones;
469 	/** local zones nodefault list */
470 	struct config_strlist* local_zones_nodefault;
471 #ifdef USE_IPSET
472 	/** local zones ipset list */
473 	struct config_strlist* local_zones_ipset;
474 #endif
475 	/** do not add any default local zone */
476 	int local_zones_disable_default;
477 	/** local data RRs configured */
478 	struct config_strlist* local_data;
479 	/** local zone override types per netblock */
480 	struct config_str3list* local_zone_overrides;
481 	/** unblock lan zones (reverse lookups for AS112 zones) */
482 	int unblock_lan_zones;
483 	/** insecure lan zones (don't validate AS112 zones) */
484 	int insecure_lan_zones;
485 	/** list of zonename, tagbitlist */
486 	struct config_strbytelist* local_zone_tags;
487 	/** list of aclname, tagbitlist */
488 	struct config_strbytelist* acl_tags;
489 	/** list of aclname, tagname, localzonetype */
490 	struct config_str3list* acl_tag_actions;
491 	/** list of aclname, tagname, redirectdata */
492 	struct config_str3list* acl_tag_datas;
493 	/** list of aclname, view*/
494 	struct config_str2list* acl_view;
495 	/** list of interface action entries, linked list */
496 	struct config_str2list* interface_actions;
497 	/** list of interface, tagbitlist */
498 	struct config_strbytelist* interface_tags;
499 	/** list of interface, tagname, localzonetype */
500 	struct config_str3list* interface_tag_actions;
501 	/** list of interface, tagname, redirectdata */
502 	struct config_str3list* interface_tag_datas;
503 	/** list of interface, view*/
504 	struct config_str2list* interface_view;
505 	/** list of IP-netblock, tagbitlist */
506 	struct config_strbytelist* respip_tags;
507 	/** list of response-driven access control entries, linked list */
508 	struct config_str2list* respip_actions;
509 	/** RRs configured for response-driven access controls */
510 	struct config_str2list* respip_data;
511 	/** tag list, array with tagname[i] is malloced string */
512 	char** tagname;
513 	/** number of items in the taglist */
514 	int num_tags;
515 
516 	/** remote control section. enable toggle. */
517 	int remote_control_enable;
518 	/** the interfaces the remote control should listen on */
519 	struct config_strlist_head control_ifs;
520 	/** if the use-cert option is set */
521 	int control_use_cert;
522 	/** port number for the control port */
523 	int control_port;
524 	/** private key file for server */
525 	char* server_key_file;
526 	/** certificate file for server */
527 	char* server_cert_file;
528 	/** private key file for unbound-control */
529 	char* control_key_file;
530 	/** certificate file for unbound-control */
531 	char* control_cert_file;
532 
533 	/** Python script file */
534 	struct config_strlist* python_script;
535 
536 	/** Dynamic library file */
537 	struct config_strlist* dynlib_file;
538 
539 	/** Use systemd socket activation. */
540 	int use_systemd;
541 
542 	/** daemonize, i.e. fork into the background. */
543 	int do_daemonize;
544 
545 	/* minimal response when positive answer */
546 	int minimal_responses;
547 
548 	/* RRSet roundrobin */
549 	int rrset_roundrobin;
550 
551 	/* wait time for unknown server in msec */
552 	int unknown_server_time_limit;
553 
554 	/** Wait time to drop recursion replies */
555 	int discard_timeout;
556 
557 	/** Wait limit for number of replies per IP address */
558 	int wait_limit;
559 
560 	/** Wait limit for number of replies per IP address with cookie */
561 	int wait_limit_cookie;
562 
563 	/** wait limit per netblock */
564 	struct config_str2list* wait_limit_netblock;
565 
566 	/** wait limit with cookie per netblock */
567 	struct config_str2list* wait_limit_cookie_netblock;
568 
569 	/* maximum UDP response size */
570 	size_t max_udp_size;
571 
572 	/* DNS64 prefix */
573 	char* dns64_prefix;
574 
575 	/* Synthetize all AAAA record despite the presence of an authoritative one */
576 	int dns64_synthall;
577 	/** ignore AAAAs for these domain names and use A record anyway */
578 	struct config_strlist* dns64_ignore_aaaa;
579 
580 	/* NAT64 prefix; if unset defaults to dns64_prefix */
581 	char* nat64_prefix;
582 
583 	/** true to enable dnstap support */
584 	int dnstap;
585 	/** using bidirectional frame streams if true */
586 	int dnstap_bidirectional;
587 	/** dnstap socket path */
588 	char* dnstap_socket_path;
589 	/** dnstap IP */
590 	char* dnstap_ip;
591 	/** dnstap TLS enable */
592 	int dnstap_tls;
593 	/** dnstap tls server authentication name */
594 	char* dnstap_tls_server_name;
595 	/** dnstap server cert bundle */
596 	char* dnstap_tls_cert_bundle;
597 	/** dnstap client key for client authentication */
598 	char* dnstap_tls_client_key_file;
599 	/** dnstap client cert for client authentication */
600 	char* dnstap_tls_client_cert_file;
601 	/** true to send "identity" via dnstap */
602 	int dnstap_send_identity;
603 	/** true to send "version" via dnstap */
604 	int dnstap_send_version;
605 	/** dnstap "identity", hostname is used if "". */
606 	char* dnstap_identity;
607 	/** dnstap "version", package version is used if "". */
608 	char* dnstap_version;
609 	/** dnstap sample rate */
610 	int dnstap_sample_rate;
611 
612 	/** true to log dnstap RESOLVER_QUERY message events */
613 	int dnstap_log_resolver_query_messages;
614 	/** true to log dnstap RESOLVER_RESPONSE message events */
615 	int dnstap_log_resolver_response_messages;
616 	/** true to log dnstap CLIENT_QUERY message events */
617 	int dnstap_log_client_query_messages;
618 	/** true to log dnstap CLIENT_RESPONSE message events */
619 	int dnstap_log_client_response_messages;
620 	/** true to log dnstap FORWARDER_QUERY message events */
621 	int dnstap_log_forwarder_query_messages;
622 	/** true to log dnstap FORWARDER_RESPONSE message events */
623 	int dnstap_log_forwarder_response_messages;
624 
625 	/** true to disable DNSSEC lameness check in iterator */
626 	int disable_dnssec_lame_check;
627 
628 	/** ratelimit for ip addresses. 0 is off, otherwise qps (unless overridden) */
629 	int ip_ratelimit;
630 	/** ratelimit for ip addresses with a valid DNS Cookie. 0 is off,
631 	 *  otherwise qps (unless overridden) */
632 	int ip_ratelimit_cookie;
633 	/** number of slabs for ip_ratelimit cache */
634 	size_t ip_ratelimit_slabs;
635 	/** memory size in bytes for ip_ratelimit cache */
636 	size_t ip_ratelimit_size;
637 	/** ip_ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
638 	int ip_ratelimit_factor;
639 	/** ratelimit backoff, when on, if the limit is reached it is
640 	 *  considered an attack and it backs off until 'demand' decreases over
641 	 *  the RATE_WINDOW. */
642 	int ip_ratelimit_backoff;
643 
644 	/** ratelimit for domains. 0 is off, otherwise qps (unless overridden) */
645 	int ratelimit;
646 	/** number of slabs for ratelimit cache */
647 	size_t ratelimit_slabs;
648 	/** memory size in bytes for ratelimit cache */
649 	size_t ratelimit_size;
650 	/** ratelimits for domain (exact match) */
651 	struct config_str2list* ratelimit_for_domain;
652 	/** ratelimits below domain */
653 	struct config_str2list* ratelimit_below_domain;
654 	/** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
655 	int ratelimit_factor;
656 	/** ratelimit backoff, when on, if the limit is reached it is
657 	 *  considered an attack and it backs off until 'demand' decreases over
658 	 *  the RATE_WINDOW. */
659 	int ratelimit_backoff;
660 
661 	/** number of retries on outgoing queries */
662 	int outbound_msg_retry;
663 	/** max sent queries per qstate; resets on query restarts (e.g.,
664 	 *  CNAMES) and referrals */
665 	int max_sent_count;
666 	/** max number of query restarts; determines max length of CNAME chain */
667 	int max_query_restarts;
668 	/** minimise outgoing QNAME and hide original QTYPE if possible */
669 	int qname_minimisation;
670 	/** minimise QNAME in strict mode, minimise according to RFC.
671 	 *  Do not apply fallback */
672 	int qname_minimisation_strict;
673 	/** SHM data - true if shm is enabled */
674 	int shm_enable;
675 	/** SHM data - key for the shm */
676 	int shm_key;
677 
678 	/** list of EDNS client string entries, linked list */
679 	struct config_str2list* edns_client_strings;
680 	/** EDNS opcode to use for EDNS client strings */
681 	uint16_t edns_client_string_opcode;
682 
683 	/** DNSCrypt */
684 	/** true to enable dnscrypt */
685 	int dnscrypt;
686 	/** port on which to provide dnscrypt service */
687 	int dnscrypt_port;
688 	/** provider name 2.dnscrypt-cert.example.com */
689 	char* dnscrypt_provider;
690 	/** dnscrypt secret keys 1.key */
691 	struct config_strlist* dnscrypt_secret_key;
692 	/** dnscrypt provider certs 1.cert */
693 	struct config_strlist* dnscrypt_provider_cert;
694 	/** dnscrypt provider certs 1.cert which have been rotated and should not be
695 	* advertised through DNS's providername TXT record but are required to be
696 	* able to handle existing traffic using the old cert. */
697 	struct config_strlist* dnscrypt_provider_cert_rotated;
698 	/** memory size in bytes for dnscrypt shared secrets cache */
699 	size_t dnscrypt_shared_secret_cache_size;
700 	/** number of slabs for dnscrypt shared secrets cache */
701 	size_t dnscrypt_shared_secret_cache_slabs;
702 	/** memory size in bytes for dnscrypt nonces cache */
703 	size_t dnscrypt_nonce_cache_size;
704 	/** number of slabs for dnscrypt nonces cache */
705 	size_t dnscrypt_nonce_cache_slabs;
706 
707 	/** EDNS padding according to RFC7830 and RFC8467 */
708 	/** true to enable padding of responses (default: on) */
709 	int pad_responses;
710 	/** block size with which to pad encrypted responses (default: 468) */
711 	size_t pad_responses_block_size;
712 	/** true to enable padding of queries (default: on) */
713 	int pad_queries;
714 	/** block size with which to pad encrypted queries (default: 128) */
715 	size_t pad_queries_block_size;
716 
717 	/** IPsec module */
718 #ifdef USE_IPSECMOD
719 	/** false to bypass the IPsec module */
720 	int ipsecmod_enabled;
721 	/** whitelisted domains for ipsecmod */
722 	struct config_strlist* ipsecmod_whitelist;
723 	/** path to external hook */
724 	char* ipsecmod_hook;
725 	/** true to proceed even with a bogus IPSECKEY */
726 	int ipsecmod_ignore_bogus;
727 	/** max TTL for the A/AAAA records that call the hook */
728 	int ipsecmod_max_ttl;
729 	/** false to proceed even when ipsecmod_hook fails */
730 	int ipsecmod_strict;
731 #endif
732 
733 	/* cachedb module */
734 #ifdef USE_CACHEDB
735 	/** backend DB name */
736 	char* cachedb_backend;
737 	/** secret seed for hash key calculation */
738 	char* cachedb_secret;
739 	/** cachedb that does not store, but only reads from database, if on */
740 	int cachedb_no_store;
741 	/** cachedb check before serving serve-expired response */
742 	int cachedb_check_when_serve_expired;
743 #ifdef USE_REDIS
744 	/** redis server's IP address or host name */
745 	char* redis_server_host;
746 	char* redis_replica_server_host;
747 	/** redis server's TCP port */
748 	int redis_server_port;
749 	int redis_replica_server_port;
750 	/** redis server's unix path. Or "", NULL if unused */
751 	char* redis_server_path;
752 	char* redis_replica_server_path;
753 	/** redis server's AUTH password. Or "", NULL if unused */
754 	char* redis_server_password;
755 	char* redis_replica_server_password;
756 	/** timeout (in ms) for communication with the redis server */
757 	int redis_timeout;
758 	int redis_replica_timeout;
759 	/** timeout (in ms) for redis commands */
760 	int redis_command_timeout;
761 	int redis_replica_command_timeout;
762 	/** timeout (in ms) for redis connection set up */
763 	int redis_connect_timeout;
764 	int redis_replica_connect_timeout;
765 	/** set timeout on redis records based on DNS response ttl */
766 	int redis_expire_records;
767 	/** set the redis logical database upon connection */
768 	int redis_logical_db;
769 	int redis_replica_logical_db;
770 #endif
771 #endif
772 	/** Downstream DNS Cookies */
773 	/** do answer with server cookie when request contained cookie option */
774 	int do_answer_cookie;
775 	/** cookie secret */
776 	uint8_t cookie_secret[40];
777 	/** cookie secret length */
778 	size_t  cookie_secret_len;
779 	/** path to cookie secret store */
780 	char* cookie_secret_file;
781 
782 	/* ipset module */
783 #ifdef USE_IPSET
784 	char* ipset_name_v4;
785 	char* ipset_name_v6;
786 #endif
787 	/** respond with Extended DNS Errors (RFC8914) */
788 	int ede;
789 	/** serve EDE code 3 - Stale Answer (RFC8914) for expired entries */
790 	int ede_serve_expired;
791 	/** send DNS Error Reports to upstream reporting agent (RFC9567) */
792 	int dns_error_reporting;
793 	/** limit on NS RRs in RRset for the iterator scrubber. */
794 	size_t iter_scrub_ns;
795 	/** limit on CNAME, DNAME RRs in answer for the iterator scrubber. */
796 	int iter_scrub_cname;
797 	/** limit on RRSIGs for an RRset for the iterator scrubber. */
798 	int iter_scrub_rrsig;
799 	/** limit on upstream queries for an incoming query and subqueries. */
800 	int max_global_quota;
801 	/** Should the iterator scrub promiscuous NS rrsets, from positive
802 	 * answers. */
803 	int iter_scrub_promiscuous;
804 };
805 
806 /** from cfg username, after daemonize setup performed */
807 extern uid_t cfg_uid;
808 /** from cfg username, after daemonize setup performed */
809 extern gid_t cfg_gid;
810 /** debug and enable small timeouts */
811 extern int autr_permit_small_holddown;
812 /** size (in bytes) of stream wait buffers max */
813 extern size_t stream_wait_max;
814 /** size (in bytes) of all total HTTP2 query buffers max */
815 extern size_t http2_query_buffer_max;
816 /** size (in bytes) of all total HTTP2 response buffers max */
817 extern size_t http2_response_buffer_max;
818 
819 /**
820  * Stub config options
821  */
822 struct config_stub {
823 	/** next in list */
824 	struct config_stub* next;
825 	/** domain name (in text) of the stub apex domain */
826 	char* name;
827 	/** list of stub nameserver hosts (domain name) */
828 	struct config_strlist* hosts;
829 	/** list of stub nameserver addresses (IP address) */
830 	struct config_strlist* addrs;
831 	/** if stub-prime is set */
832 	int isprime;
833 	/** if forward-first is set (failover to without if fails) */
834 	int isfirst;
835 	/** use tcp for queries to this stub */
836 	int tcp_upstream;
837 	/** use SSL for queries to this stub */
838 	int ssl_upstream;
839 	/*** no cache */
840 	int no_cache;
841 };
842 
843 /**
844  * Auth config options
845  */
846 struct config_auth {
847 	/** next in list */
848 	struct config_auth* next;
849 	/** domain name (in text) of the auth apex domain */
850 	char* name;
851 	/** list of masters */
852 	struct config_strlist* masters;
853 	/** list of urls */
854 	struct config_strlist* urls;
855 	/** list of allow-notify */
856 	struct config_strlist* allow_notify;
857 	/** zonefile (or NULL) */
858 	char* zonefile;
859 	/** provide downstream answers */
860 	int for_downstream;
861 	/** provide upstream answers */
862 	int for_upstream;
863 	/** fallback to recursion to authorities if zone expired and other
864 	 * reasons perhaps (like, query bogus) */
865 	int fallback_enabled;
866 	/** this zone is used to create local-zone policies */
867 	int isrpz;
868 	/** rpz tags (or NULL) */
869 	uint8_t* rpz_taglist;
870 	/** length of the taglist (in bytes) */
871 	size_t rpz_taglistlen;
872 	/** Override RPZ action for this zone, regardless of zone content */
873 	char* rpz_action_override;
874 	/** Log when this RPZ policy is applied */
875 	int rpz_log;
876 	/** Display this name in the log when RPZ policy is applied */
877 	char* rpz_log_name;
878 	/** Always reply with this CNAME target if the cname override action is
879 	 * used */
880 	char* rpz_cname;
881 	/** signal nxdomain block with unset RA */
882 	int rpz_signal_nxdomain_ra;
883 	/** Check ZONEMD records for this zone */
884 	int zonemd_check;
885 	/** Reject absence of ZONEMD records, zone must have one */
886 	int zonemd_reject_absence;
887 	/** The maximum auth zone transfer size, in bytes. */
888 	size_t max_transfer_size;
889 	/** The maximum auth zone transfer time taken, in msec. */
890 	int max_transfer_time;
891 };
892 
893 /**
894  * View config options
895  */
896 struct config_view {
897 	/** next in list */
898 	struct config_view* next;
899 	/** view name */
900 	char* name;
901 	/** local zones */
902 	struct config_str2list* local_zones;
903 	/** local data RRs */
904 	struct config_strlist* local_data;
905 	/** local zones nodefault list */
906 	struct config_strlist* local_zones_nodefault;
907 #ifdef USE_IPSET
908 	/** local zones ipset list */
909 	struct config_strlist* local_zones_ipset;
910 #endif
911 	/** Fallback to global local_zones when there is no match in the view
912 	 * view specific tree. 1 for yes, 0 for no */
913 	int isfirst;
914 	/** predefined actions for particular IP address responses */
915 	struct config_str2list* respip_actions;
916 	/** data complementing the 'redirect' response IP actions */
917 	struct config_str2list* respip_data;
918 };
919 
920 /**
921  * List of strings for config options
922  */
923 struct config_strlist {
924 	/** next item in list */
925 	struct config_strlist* next;
926 	/** config option string */
927 	char* str;
928 };
929 
930 /**
931  * List of two strings for config options
932  */
933 struct config_str2list {
934 	/** next item in list */
935 	struct config_str2list* next;
936 	/** first string */
937 	char* str;
938 	/** second string */
939 	char* str2;
940 };
941 
942 /**
943  * List of three strings for config options
944  */
945 struct config_str3list {
946 	/** next item in list */
947 	struct config_str3list* next;
948 	/** first string */
949 	char* str;
950 	/** second string */
951 	char* str2;
952 	/** third string */
953 	char* str3;
954 };
955 
956 
957 /**
958  * List of string, bytestring for config options
959  */
960 struct config_strbytelist {
961 	/** next item in list */
962 	struct config_strbytelist* next;
963 	/** first string */
964 	char* str;
965 	/** second bytestring */
966 	uint8_t* str2;
967 	size_t str2len;
968 };
969 
970 /**
971  * Create config file structure. Filled with default values.
972  * @return: the new structure or NULL on memory error.
973  */
974 struct config_file* config_create(void);
975 
976 /**
977  * Create config file structure for library use. Filled with default values.
978  * @return: the new structure or NULL on memory error.
979  */
980 struct config_file* config_create_forlib(void);
981 
982 /**
983  * If _slabs values are not explicitly configured, 0 value, put them in a
984  * pow2 value close to the number of threads used.
985  * Starts at the current default 4.
986  * If num_threads is in between two pow2 values, 1/3 of the way stays with
987  * the lower pow2 value.
988  * Exported for unit testing.
989  * @param config: where the _slabs values reside.
990  */
991 void config_auto_slab_values(struct config_file* config);
992 
993 /**
994  * Read the config file from the specified filename.
995  * @param config: where options are stored into, must be freshly created.
996  * @param filename: name of configfile. If NULL nothing is done.
997  * @param chroot: if not NULL, the chroot dir currently in use (for include).
998  * @return: false on error. In that case errno is set, ENOENT means
999  * 	file not found.
1000  */
1001 int config_read(struct config_file* config, const char* filename,
1002 	const char* chroot);
1003 
1004 /**
1005  * Destroy the config file structure.
1006  * @param config: to delete.
1007  */
1008 void config_delete(struct config_file* config);
1009 
1010 /**
1011  * Apply config to global constants; this routine is called in single thread.
1012  * @param config: to apply. Side effect: global constants change.
1013  */
1014 void config_apply(struct config_file* config);
1015 
1016 /** Apply the relevant changes that rely upon RTT_MAX_TIMEOUT;
1017  *  exported for unit test */
1018 int config_apply_max_rtt(int max_rtt);
1019 
1020 /**
1021  * Find username, sets cfg_uid and cfg_gid.
1022  * @param config: the config structure.
1023  */
1024 void config_lookup_uid(struct config_file* config);
1025 
1026 /**
1027  * Set the given keyword to the given value.
1028  * @param config: where to store config
1029  * @param option: option name, including the ':' character.
1030  * @param value: value, this string is copied if needed, or parsed.
1031  * 	The caller owns the value string.
1032  * @return 0 on error (malloc or syntax error).
1033  */
1034 int config_set_option(struct config_file* config, const char* option,
1035 	const char* value);
1036 
1037 /**
1038  * Call print routine for the given option.
1039  * @param cfg: config.
1040  * @param opt: option name without trailing :.
1041  *	This is different from config_set_option.
1042  * @param func: print func, called as (str, arg) for every data element.
1043  * @param arg: user argument for print func.
1044  * @return false if the option name is not supported (syntax error).
1045  */
1046 int config_get_option(struct config_file* cfg, const char* opt,
1047 	void (*func)(char*,void*), void* arg);
1048 
1049 /**
1050  * Get an option and return strlist
1051  * @param cfg: config file
1052  * @param opt: option name.
1053  * @param list: list is returned here. malloced, caller must free it.
1054  * @return 0=OK, 1=syntax error, 2=malloc failed.
1055  */
1056 int config_get_option_list(struct config_file* cfg, const char* opt,
1057 	struct config_strlist** list);
1058 
1059 /**
1060  * Get an option and collate results into string
1061  * @param cfg: config file
1062  * @param opt: option name.
1063  * @param str: string. malloced, caller must free it.
1064  * @return 0=OK, 1=syntax error, 2=malloc failed.
1065  */
1066 int config_get_option_collate(struct config_file* cfg, const char* opt,
1067 	char** str);
1068 
1069 /**
1070  * function to print to a file, use as func with config_get_option.
1071  * @param line: text to print. \n appended.
1072  * @param arg: pass a FILE*, like stdout.
1073  */
1074 void config_print_func(char* line, void* arg);
1075 
1076 /**
1077  * function to collate the text strings into a strlist_head.
1078  * @param line: text to append.
1079  * @param arg: pass a strlist_head structure. zeroed on start.
1080  */
1081 void config_collate_func(char* line, void* arg);
1082 
1083 /**
1084  * take a strlist_head list and return a malloc string. separated with newline.
1085  * @param list: strlist first to collate. zeroes return "".
1086  * @return NULL on malloc failure. Or if malloc failure happened in strlist.
1087  */
1088 char* config_collate_cat(struct config_strlist* list);
1089 
1090 /**
1091  * Append text at end of list.
1092  * @param list: list head. zeroed at start.
1093  * @param item: new item. malloced by caller. if NULL the insertion fails.
1094  * @return true on success.
1095  * on fail the item is free()ed.
1096  */
1097 int cfg_strlist_append(struct config_strlist_head* list, char* item);
1098 
1099 /**
1100  * Searches the end of a string list and appends the given text.
1101  * @param head: pointer to strlist head variable.
1102  * @param item: new item. malloced by caller. if NULL the insertion fails.
1103  * @return true on success.
1104  */
1105 int cfg_strlist_append_ex(struct config_strlist** head, char* item);
1106 
1107 /**
1108  * Find string in strlist.
1109  * @param head: pointer to strlist head variable.
1110  * @param item: the item to search for.
1111  * @return: the element in the list when found, NULL otherwise.
1112  */
1113 struct config_strlist* cfg_strlist_find(struct config_strlist* head,
1114 	const char* item);
1115 
1116 /**
1117  * Insert string into strlist.
1118  * @param head: pointer to strlist head variable.
1119  * @param item: new item. malloced by caller. If NULL the insertion fails.
1120  * @return: true on success.
1121  * on fail, the item is free()d.
1122  */
1123 int cfg_strlist_insert(struct config_strlist** head, char* item);
1124 
1125 /** insert with region for allocation. */
1126 int cfg_region_strlist_insert(struct regional* region,
1127 	struct config_strlist** head, char* item);
1128 
1129 /**
1130  * Insert string into str2list.
1131  * @param head: pointer to str2list head variable.
1132  * @param item: new item. malloced by caller. If NULL the insertion fails.
1133  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
1134  * @return: true on success.
1135  * on fail, the item and i2 are free()d.
1136  */
1137 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
1138 
1139 /**
1140  * Insert string into str3list.
1141  * @param head: pointer to str3list head variable.
1142  * @param item: new item. malloced by caller. If NULL the insertion fails.
1143  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
1144  * @param i3: 3rd string, malloced by caller. If NULL the insertion fails.
1145  * @return: true on success.
1146  */
1147 int cfg_str3list_insert(struct config_str3list** head, char* item, char* i2,
1148 	char* i3);
1149 
1150 /**
1151  * Insert string into strbytelist.
1152  * @param head: pointer to strbytelist head variable.
1153  * @param item: new item. malloced by caller. If NULL the insertion fails.
1154  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
1155  * @param i2len: length of the i2 bytestring.
1156  * @return: true on success.
1157  */
1158 int cfg_strbytelist_insert(struct config_strbytelist** head, char* item,
1159 	uint8_t* i2, size_t i2len);
1160 
1161 /**
1162  * Find stub in config list, also returns prevptr (for deletion).
1163  * @param pp: call routine with pointer to a pointer to the start of the list,
1164  * 	if the stub is found, on exit, the value contains a pointer to the
1165  * 	next pointer that points to the found element (or to the list start
1166  * 	pointer if it is the first element).
1167  * @param nm: name of stub to find.
1168  * @return: pointer to config_stub if found, or NULL if not found.
1169  */
1170 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm);
1171 
1172 /**
1173  * Delete items in config string list.
1174  * @param list: list.
1175  */
1176 void config_delstrlist(struct config_strlist* list);
1177 
1178 /**
1179  * Delete items in config double string list.
1180  * @param list: list.
1181  */
1182 void config_deldblstrlist(struct config_str2list* list);
1183 
1184 /**
1185  * Delete items in config triple string list.
1186  * @param list: list.
1187  */
1188 void config_deltrplstrlist(struct config_str3list* list);
1189 
1190 /** delete string array */
1191 void config_del_strarray(char** array, int num);
1192 
1193 /** delete stringbytelist */
1194 void config_del_strbytelist(struct config_strbytelist* list);
1195 
1196 /**
1197  * Delete a stub item
1198  * @param p: stub item
1199  */
1200 void config_delstub(struct config_stub* p);
1201 
1202 /**
1203  * Delete items in config stub list.
1204  * @param list: list.
1205  */
1206 void config_delstubs(struct config_stub* list);
1207 
1208 /**
1209  * Delete an auth item
1210  * @param p: auth item
1211  */
1212 void config_delauth(struct config_auth* p);
1213 
1214 /**
1215  * Delete items in config auth list.
1216  * @param list: list.
1217  */
1218 void config_delauths(struct config_auth* list);
1219 
1220 /**
1221  * Delete a view item
1222  * @param p: view item
1223  */
1224 void config_delview(struct config_view* p);
1225 
1226 /**
1227  * Delete items in config view list.
1228  * @param list: list.
1229  */
1230 void config_delviews(struct config_view* list);
1231 
1232 /** check if config for remote control turns on IP-address interface
1233  * with certificates or a named pipe without certificates. */
1234 int options_remote_is_address(struct config_file* cfg);
1235 
1236 /**
1237  * Convert 14digit to time value
1238  * @param str: string of 14 digits
1239  * @return time value or 0 for error.
1240  */
1241 time_t cfg_convert_timeval(const char* str);
1242 
1243 /**
1244  * Count number of values in the string.
1245  * format ::= (sp num)+ sp
1246  * num ::= [-](0-9)+
1247  * sp ::= (space|tab)*
1248  *
1249  * @param str: string
1250  * @return: 0 on parse error, or empty string, else
1251  *	number of integer values in the string.
1252  */
1253 int cfg_count_numbers(const char* str);
1254 
1255 /**
1256  * Convert a 'nice' memory or file size into a bytecount
1257  * From '100k' to 102400. and so on. Understands kKmMgG.
1258  * k=1024, m=1024*1024, g=1024*1024*1024.
1259  * @param str: string
1260  * @param res: result is stored here, size in bytes.
1261  * @return: true if parsed correctly, or 0 on a parse error (and an error
1262  * is logged).
1263  */
1264 int cfg_parse_memsize(const char* str, size_t* res);
1265 
1266 /**
1267  * Parse nsid from string into binary nsid. nsid is either a hexadecimal
1268  * string or an ascii string prepended with ascii_ in which case the
1269  * characters after ascii_ are simply copied.
1270  * @param str: the string to parse.
1271  * @param nsid_len: returns length of nsid in bytes.
1272  * @return malloced bytes or NULL on parse error or malloc failure.
1273  */
1274 uint8_t* cfg_parse_nsid(const char* str, uint16_t* nsid_len);
1275 
1276 /**
1277  * Add a tag name to the config.  It is added at the end with a new ID value.
1278  * @param cfg: the config structure.
1279  * @param tag: string (which is copied) with the name.
1280  * @return: false on alloc failure.
1281  */
1282 int config_add_tag(struct config_file* cfg, const char* tag);
1283 
1284 /**
1285  * Find tag ID in the tag list.
1286  * @param cfg: the config structure.
1287  * @param tag: string with tag name to search for.
1288  * @return: 0..(num_tags-1) with tag ID, or -1 if tagname is not found.
1289  */
1290 int find_tag_id(struct config_file* cfg, const char* tag);
1291 
1292 /**
1293  * parse taglist from string into bytestring with bitlist.
1294  * @param cfg: the config structure (with tagnames)
1295  * @param str: the string to parse.  Parse puts 0 bytes in string.
1296  * @param listlen: returns length of in bytes.
1297  * @return malloced bytes with a bitlist of the tags.  or NULL on parse error
1298  * or malloc failure.
1299  */
1300 uint8_t* config_parse_taglist(struct config_file* cfg, char* str,
1301 	size_t* listlen);
1302 
1303 /**
1304  * convert tag bitlist to a malloced string with tag names.  For debug output.
1305  * @param cfg: the config structure (with tagnames)
1306  * @param taglist: the tag bitlist.
1307  * @param len: length of the tag bitlist.
1308  * @return malloced string or NULL.
1309  */
1310 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist,
1311 	size_t len);
1312 
1313 /**
1314  * see if two taglists intersect (have tags in common).
1315  * @param list1: first tag bitlist.
1316  * @param list1len: length in bytes of first list.
1317  * @param list2: second tag bitlist.
1318  * @param list2len: length in bytes of second list.
1319  * @return true if there are tags in common, 0 if not.
1320  */
1321 int taglist_intersect(uint8_t* list1, size_t list1len, const uint8_t* list2,
1322 	size_t list2len);
1323 
1324 /**
1325  * Parse local-zone directive into two strings and register it in the config.
1326  * @param cfg: to put it in.
1327  * @param val: argument strings to local-zone, "example.com nodefault".
1328  * @return: false on failure
1329  */
1330 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
1331 
1332 /**
1333  * Mark "number" or "low-high" as available or not in ports array.
1334  * @param str: string in input
1335  * @param allow: give true if this range is permitted.
1336  * @param avail: the array from cfg.
1337  * @param num: size of the array (65536).
1338  * @return: true if parsed correctly, or 0 on a parse error (and an error
1339  * is logged).
1340  */
1341 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
1342 
1343 /**
1344  * Get a condensed list of ports returned. allocated.
1345  * @param cfg: config file.
1346  * @param avail: the available ports array is returned here.
1347  * @return: number of ports in array or 0 on error.
1348  */
1349 int cfg_condense_ports(struct config_file* cfg, int** avail);
1350 
1351 /**
1352  * Apply system specific port range policy.
1353  * @param cfg: config file.
1354  * @param num: size of the array (65536).
1355  */
1356 void cfg_apply_local_port_policy(struct config_file* cfg, int num);
1357 
1358 /**
1359  * Scan ports available
1360  * @param avail: the array from cfg.
1361  * @param num: size of the array (65536).
1362  * @return the number of ports available for use.
1363  */
1364 int cfg_scan_ports(int* avail, int num);
1365 
1366 /**
1367  * Convert a filename to full pathname in original filesys
1368  * @param fname: the path name to convert.
1369  *      Must not be null or empty.
1370  * @param cfg: config struct for chroot and chdir (if set).
1371  * @param use_chdir: if false, only chroot is applied.
1372  * @return pointer to malloced buffer which is: [chroot][chdir]fname
1373  *      or NULL on malloc failure.
1374  */
1375 char* fname_after_chroot(const char* fname, struct config_file* cfg,
1376 	int use_chdir);
1377 
1378 /**
1379  * Convert a ptr shorthand into a full reverse-notation PTR record.
1380  * @param str: input string, "IP name"
1381  * @return: malloced string "reversed-ip-name PTR name"
1382  */
1383 char* cfg_ptr_reverse(char* str);
1384 
1385 /**
1386  * Used during options parsing
1387  */
1388 struct config_parser_state {
1389 	/** name of file being parser */
1390 	char* filename;
1391 	/** line number in the file, starts at 1 */
1392 	int line;
1393 	/** number of errors encountered */
1394 	int errors;
1395 	/** the result of parsing is stored here. */
1396 	struct config_file* cfg;
1397 	/** the current chroot dir (or NULL if none) */
1398 	const char* chroot;
1399 	/** if we are started in a toplevel, or not, after a force_toplevel */
1400 	int started_toplevel;
1401 };
1402 
1403 /** global config parser object used during config parsing */
1404 extern struct config_parser_state* cfg_parser;
1405 /** init lex state */
1406 void init_cfg_parse(void);
1407 /** lex in file */
1408 extern FILE* ub_c_in;
1409 /** lex out file */
1410 extern FILE* ub_c_out;
1411 /** the yacc lex generated parse function */
1412 int ub_c_parse(void);
1413 /** the lexer function */
1414 int ub_c_lex(void);
1415 /** wrap function */
1416 int ub_c_wrap(void);
1417 /** parsing helpers: print error with file and line numbers */
1418 void ub_c_error(const char* msg);
1419 /** parsing helpers: print error with file and line numbers */
1420 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
1421 
1422 #ifdef UB_ON_WINDOWS
1423 /**
1424  * Obtain registry string (if it exists).
1425  * @param key: key string
1426  * @param name: name of value to fetch.
1427  * @return malloced string with the result or NULL if it did not
1428  * 	exist on an error (logged with log_err) was encountered.
1429  */
1430 char* w_lookup_reg_str(const char* key, const char* name);
1431 
1432 /** Modify directory in options for module file name */
1433 void w_config_adjust_directory(struct config_file* cfg);
1434 #endif /* UB_ON_WINDOWS */
1435 
1436 /** debug option for unit tests. */
1437 extern int fake_dsa, fake_sha1;
1438 
1439 /** Return true if interface will listen to specific port(s).
1440  * @param ifname: the interface as configured in the configuration file.
1441  * @param default_port: the default port to use as the interface port if ifname
1442  *	does not include a port via the '@' notation.
1443  * @param port: port to check for, if 0 it will not be checked.
1444  * @param additional_ports: additional configured ports, if any (nonNULL) to
1445  *	be checked against.
1446  * @return true if one of (port, additional_ports) matches the interface port.
1447  */
1448 int if_listens_on(const char* ifname, int default_port, int port,
1449 	struct config_strlist* additional_ports);
1450 
1451 /** see if interface will listen on https;
1452  *  its port number == the https port number */
1453 int if_is_https(const char* ifname, int default_port, int https_port);
1454 
1455 /** see if interface will listen on ssl;
1456  *  its port number == the ssl port number or any of the additional ports */
1457 int if_is_ssl(const char* ifname, int default_port, int ssl_port,
1458 	struct config_strlist* tls_additional_port);
1459 
1460 /** see if interface will listen on PROXYv2;
1461  *  its port number == any of the proxy ports number */
1462 int if_is_pp2(const char* ifname, int default_port,
1463 	struct config_strlist* proxy_protocol_port);
1464 
1465 /** see if interface will listen on DNSCRYPT;
1466  *  its port number == the dnscrypt port number */
1467 int if_is_dnscrypt(const char* ifname, int default_port, int dnscrypt_port);
1468 
1469 /** see if interface will listen on quic;
1470  *  its port number == the quic port number */
1471 int if_is_quic(const char* ifname, int default_port, int quic_port);
1472 
1473 /**
1474  * Return true if the config contains settings that enable https.
1475  * @param cfg: config information.
1476  * @return true if https ports are used for server.
1477  */
1478 int cfg_has_https(struct config_file* cfg);
1479 
1480 /**
1481  * Return true if the config contains settings that enable quic.
1482  * @param cfg: config information.
1483  * @return true if quic ports are used for server.
1484  */
1485 int cfg_has_quic(struct config_file* cfg);
1486 
1487 #ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
1488 #define LINUX_IP_LOCAL_PORT_RANGE_PATH "/proc/sys/net/ipv4/ip_local_port_range"
1489 #endif
1490 
1491 /** get memory for string */
1492 size_t getmem_str(char* str);
1493 
1494 /**
1495  * See if the if_automatic_ports list contains the value.
1496  * @param ports: String with port numbers.
1497  * @param p: number looked for.
1498  * @return true if found, false if not found or parse failure.
1499  */
1500 int cfg_ports_list_contains(char* ports, int p);
1501 
1502 /**
1503  * Check if the configured string contains supported TLS protocols.
1504  * @param tls_protocols: String with TLS protocols.
1505  * @return true if all options are valid, else false.
1506  */
1507 int cfg_tls_protocols_is_valid(const char* tls_protocols);
1508 
1509 /**
1510  * Based on the configured TLS protocols fill which ones are allowed.
1511  * @param tls_protocols: String with TLS protocols.
1512  * @param allow12: will be true if TLSv1.2 is configured.
1513  * @param allow13: will be true if TLSv1.3 is configured.
1514  */
1515 void cfg_tls_protocols_allowed(const char* tls_protocols, int* allow12, int* allow13);
1516 
1517 /** get the file mtime stat (or error, with errno and nonexist) */
1518 int file_get_mtime(const char* file, time_t* mtime, long* ns, int* nonexist);
1519 
1520 #endif /* UTIL_CONFIG_FILE_H */
1521