xref: /freebsd/contrib/unbound/util/config_file.h (revision 1fb9c5ffe25fcba0857c4fdf6ed19ca5bf6bc858)
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 	/** limit on validator validation attempts. */
802 	int val_validation_attempts;
803 	/** limit on validator hash attempts. */
804 	int val_hash_attempts;
805 	/** Should the iterator scrub promiscuous NS rrsets, from positive
806 	 * answers. */
807 	int iter_scrub_promiscuous;
808 };
809 
810 /** from cfg username, after daemonize setup performed */
811 extern uid_t cfg_uid;
812 /** from cfg username, after daemonize setup performed */
813 extern gid_t cfg_gid;
814 /** debug and enable small timeouts */
815 extern int autr_permit_small_holddown;
816 /** size (in bytes) of stream wait buffers max */
817 extern size_t stream_wait_max;
818 /** size (in bytes) of all total HTTP2 query buffers max */
819 extern size_t http2_query_buffer_max;
820 /** size (in bytes) of all total HTTP2 response buffers max */
821 extern size_t http2_response_buffer_max;
822 
823 /**
824  * Stub config options
825  */
826 struct config_stub {
827 	/** next in list */
828 	struct config_stub* next;
829 	/** domain name (in text) of the stub apex domain */
830 	char* name;
831 	/** list of stub nameserver hosts (domain name) */
832 	struct config_strlist* hosts;
833 	/** list of stub nameserver addresses (IP address) */
834 	struct config_strlist* addrs;
835 	/** if stub-prime is set */
836 	int isprime;
837 	/** if forward-first is set (failover to without if fails) */
838 	int isfirst;
839 	/** use tcp for queries to this stub */
840 	int tcp_upstream;
841 	/** use SSL for queries to this stub */
842 	int ssl_upstream;
843 	/*** no cache */
844 	int no_cache;
845 };
846 
847 /**
848  * Auth config options
849  */
850 struct config_auth {
851 	/** next in list */
852 	struct config_auth* next;
853 	/** domain name (in text) of the auth apex domain */
854 	char* name;
855 	/** list of masters */
856 	struct config_strlist* masters;
857 	/** list of urls */
858 	struct config_strlist* urls;
859 	/** list of allow-notify */
860 	struct config_strlist* allow_notify;
861 	/** zonefile (or NULL) */
862 	char* zonefile;
863 	/** provide downstream answers */
864 	int for_downstream;
865 	/** provide upstream answers */
866 	int for_upstream;
867 	/** fallback to recursion to authorities if zone expired and other
868 	 * reasons perhaps (like, query bogus) */
869 	int fallback_enabled;
870 	/** this zone is used to create local-zone policies */
871 	int isrpz;
872 	/** rpz tags (or NULL) */
873 	uint8_t* rpz_taglist;
874 	/** length of the taglist (in bytes) */
875 	size_t rpz_taglistlen;
876 	/** Override RPZ action for this zone, regardless of zone content */
877 	char* rpz_action_override;
878 	/** Log when this RPZ policy is applied */
879 	int rpz_log;
880 	/** Display this name in the log when RPZ policy is applied */
881 	char* rpz_log_name;
882 	/** Always reply with this CNAME target if the cname override action is
883 	 * used */
884 	char* rpz_cname;
885 	/** signal nxdomain block with unset RA */
886 	int rpz_signal_nxdomain_ra;
887 	/** Check ZONEMD records for this zone */
888 	int zonemd_check;
889 	/** Reject absence of ZONEMD records, zone must have one */
890 	int zonemd_reject_absence;
891 	/** The maximum auth zone transfer size, in bytes. */
892 	size_t max_transfer_size;
893 	/** The maximum auth zone transfer time taken, in msec. */
894 	int max_transfer_time;
895 };
896 
897 /**
898  * View config options
899  */
900 struct config_view {
901 	/** next in list */
902 	struct config_view* next;
903 	/** view name */
904 	char* name;
905 	/** local zones */
906 	struct config_str2list* local_zones;
907 	/** local data RRs */
908 	struct config_strlist* local_data;
909 	/** local zones nodefault list */
910 	struct config_strlist* local_zones_nodefault;
911 #ifdef USE_IPSET
912 	/** local zones ipset list */
913 	struct config_strlist* local_zones_ipset;
914 #endif
915 	/** Fallback to global local_zones when there is no match in the view
916 	 * view specific tree. 1 for yes, 0 for no */
917 	int isfirst;
918 	/** predefined actions for particular IP address responses */
919 	struct config_str2list* respip_actions;
920 	/** data complementing the 'redirect' response IP actions */
921 	struct config_str2list* respip_data;
922 };
923 
924 /**
925  * List of strings for config options
926  */
927 struct config_strlist {
928 	/** next item in list */
929 	struct config_strlist* next;
930 	/** config option string */
931 	char* str;
932 };
933 
934 /**
935  * List of two strings for config options
936  */
937 struct config_str2list {
938 	/** next item in list */
939 	struct config_str2list* next;
940 	/** first string */
941 	char* str;
942 	/** second string */
943 	char* str2;
944 };
945 
946 /**
947  * List of three strings for config options
948  */
949 struct config_str3list {
950 	/** next item in list */
951 	struct config_str3list* next;
952 	/** first string */
953 	char* str;
954 	/** second string */
955 	char* str2;
956 	/** third string */
957 	char* str3;
958 };
959 
960 
961 /**
962  * List of string, bytestring for config options
963  */
964 struct config_strbytelist {
965 	/** next item in list */
966 	struct config_strbytelist* next;
967 	/** first string */
968 	char* str;
969 	/** second bytestring */
970 	uint8_t* str2;
971 	size_t str2len;
972 };
973 
974 /**
975  * Create config file structure. Filled with default values.
976  * @return: the new structure or NULL on memory error.
977  */
978 struct config_file* config_create(void);
979 
980 /**
981  * Create config file structure for library use. Filled with default values.
982  * @return: the new structure or NULL on memory error.
983  */
984 struct config_file* config_create_forlib(void);
985 
986 /**
987  * If _slabs values are not explicitly configured, 0 value, put them in a
988  * pow2 value close to the number of threads used.
989  * Starts at the current default 4.
990  * If num_threads is in between two pow2 values, 1/3 of the way stays with
991  * the lower pow2 value.
992  * Exported for unit testing.
993  * @param config: where the _slabs values reside.
994  */
995 void config_auto_slab_values(struct config_file* config);
996 
997 /**
998  * Read the config file from the specified filename.
999  * @param config: where options are stored into, must be freshly created.
1000  * @param filename: name of configfile. If NULL nothing is done.
1001  * @param chroot: if not NULL, the chroot dir currently in use (for include).
1002  * @return: false on error. In that case errno is set, ENOENT means
1003  * 	file not found.
1004  */
1005 int config_read(struct config_file* config, const char* filename,
1006 	const char* chroot);
1007 
1008 /**
1009  * Destroy the config file structure.
1010  * @param config: to delete.
1011  */
1012 void config_delete(struct config_file* config);
1013 
1014 /**
1015  * Apply config to global constants; this routine is called in single thread.
1016  * @param config: to apply. Side effect: global constants change.
1017  */
1018 void config_apply(struct config_file* config);
1019 
1020 /** Apply the relevant changes that rely upon RTT_MAX_TIMEOUT;
1021  *  exported for unit test */
1022 int config_apply_max_rtt(int max_rtt);
1023 
1024 /**
1025  * Find username, sets cfg_uid and cfg_gid.
1026  * @param config: the config structure.
1027  */
1028 void config_lookup_uid(struct config_file* config);
1029 
1030 /**
1031  * Set the given keyword to the given value.
1032  * @param config: where to store config
1033  * @param option: option name, including the ':' character.
1034  * @param value: value, this string is copied if needed, or parsed.
1035  * 	The caller owns the value string.
1036  * @return 0 on error (malloc or syntax error).
1037  */
1038 int config_set_option(struct config_file* config, const char* option,
1039 	const char* value);
1040 
1041 /**
1042  * Call print routine for the given option.
1043  * @param cfg: config.
1044  * @param opt: option name without trailing :.
1045  *	This is different from config_set_option.
1046  * @param func: print func, called as (str, arg) for every data element.
1047  * @param arg: user argument for print func.
1048  * @return false if the option name is not supported (syntax error).
1049  */
1050 int config_get_option(struct config_file* cfg, const char* opt,
1051 	void (*func)(char*,void*), void* arg);
1052 
1053 /**
1054  * Get an option and return strlist
1055  * @param cfg: config file
1056  * @param opt: option name.
1057  * @param list: list is returned here. malloced, caller must free it.
1058  * @return 0=OK, 1=syntax error, 2=malloc failed.
1059  */
1060 int config_get_option_list(struct config_file* cfg, const char* opt,
1061 	struct config_strlist** list);
1062 
1063 /**
1064  * Get an option and collate results into string
1065  * @param cfg: config file
1066  * @param opt: option name.
1067  * @param str: string. malloced, caller must free it.
1068  * @return 0=OK, 1=syntax error, 2=malloc failed.
1069  */
1070 int config_get_option_collate(struct config_file* cfg, const char* opt,
1071 	char** str);
1072 
1073 /**
1074  * function to print to a file, use as func with config_get_option.
1075  * @param line: text to print. \n appended.
1076  * @param arg: pass a FILE*, like stdout.
1077  */
1078 void config_print_func(char* line, void* arg);
1079 
1080 /**
1081  * function to collate the text strings into a strlist_head.
1082  * @param line: text to append.
1083  * @param arg: pass a strlist_head structure. zeroed on start.
1084  */
1085 void config_collate_func(char* line, void* arg);
1086 
1087 /**
1088  * take a strlist_head list and return a malloc string. separated with newline.
1089  * @param list: strlist first to collate. zeroes return "".
1090  * @return NULL on malloc failure. Or if malloc failure happened in strlist.
1091  */
1092 char* config_collate_cat(struct config_strlist* list);
1093 
1094 /**
1095  * Append text at end of list.
1096  * @param list: list head. zeroed at start.
1097  * @param item: new item. malloced by caller. if NULL the insertion fails.
1098  * @return true on success.
1099  * on fail the item is free()ed.
1100  */
1101 int cfg_strlist_append(struct config_strlist_head* list, char* item);
1102 
1103 /**
1104  * Searches the end of a string list and appends the given text.
1105  * @param head: pointer to strlist head variable.
1106  * @param item: new item. malloced by caller. if NULL the insertion fails.
1107  * @return true on success.
1108  */
1109 int cfg_strlist_append_ex(struct config_strlist** head, char* item);
1110 
1111 /**
1112  * Find string in strlist.
1113  * @param head: pointer to strlist head variable.
1114  * @param item: the item to search for.
1115  * @return: the element in the list when found, NULL otherwise.
1116  */
1117 struct config_strlist* cfg_strlist_find(struct config_strlist* head,
1118 	const char* item);
1119 
1120 /**
1121  * Insert string into strlist.
1122  * @param head: pointer to strlist head variable.
1123  * @param item: new item. malloced by caller. If NULL the insertion fails.
1124  * @return: true on success.
1125  * on fail, the item is free()d.
1126  */
1127 int cfg_strlist_insert(struct config_strlist** head, char* item);
1128 
1129 /** insert with region for allocation. */
1130 int cfg_region_strlist_insert(struct regional* region,
1131 	struct config_strlist** head, char* item);
1132 
1133 /**
1134  * Insert string into str2list.
1135  * @param head: pointer to str2list head variable.
1136  * @param item: new item. malloced by caller. If NULL the insertion fails.
1137  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
1138  * @return: true on success.
1139  * on fail, the item and i2 are free()d.
1140  */
1141 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
1142 
1143 /**
1144  * Insert string into str3list.
1145  * @param head: pointer to str3list head variable.
1146  * @param item: new item. malloced by caller. If NULL the insertion fails.
1147  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
1148  * @param i3: 3rd string, malloced by caller. If NULL the insertion fails.
1149  * @return: true on success.
1150  */
1151 int cfg_str3list_insert(struct config_str3list** head, char* item, char* i2,
1152 	char* i3);
1153 
1154 /**
1155  * Insert string into strbytelist.
1156  * @param head: pointer to strbytelist head variable.
1157  * @param item: new item. malloced by caller. If NULL the insertion fails.
1158  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
1159  * @param i2len: length of the i2 bytestring.
1160  * @return: true on success.
1161  */
1162 int cfg_strbytelist_insert(struct config_strbytelist** head, char* item,
1163 	uint8_t* i2, size_t i2len);
1164 
1165 /**
1166  * Find stub in config list, also returns prevptr (for deletion).
1167  * @param pp: call routine with pointer to a pointer to the start of the list,
1168  * 	if the stub is found, on exit, the value contains a pointer to the
1169  * 	next pointer that points to the found element (or to the list start
1170  * 	pointer if it is the first element).
1171  * @param nm: name of stub to find.
1172  * @return: pointer to config_stub if found, or NULL if not found.
1173  */
1174 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm);
1175 
1176 /**
1177  * Delete items in config string list.
1178  * @param list: list.
1179  */
1180 void config_delstrlist(struct config_strlist* list);
1181 
1182 /**
1183  * Delete items in config double string list.
1184  * @param list: list.
1185  */
1186 void config_deldblstrlist(struct config_str2list* list);
1187 
1188 /**
1189  * Delete items in config triple string list.
1190  * @param list: list.
1191  */
1192 void config_deltrplstrlist(struct config_str3list* list);
1193 
1194 /** delete string array */
1195 void config_del_strarray(char** array, int num);
1196 
1197 /** delete stringbytelist */
1198 void config_del_strbytelist(struct config_strbytelist* list);
1199 
1200 /**
1201  * Delete a stub item
1202  * @param p: stub item
1203  */
1204 void config_delstub(struct config_stub* p);
1205 
1206 /**
1207  * Delete items in config stub list.
1208  * @param list: list.
1209  */
1210 void config_delstubs(struct config_stub* list);
1211 
1212 /**
1213  * Delete an auth item
1214  * @param p: auth item
1215  */
1216 void config_delauth(struct config_auth* p);
1217 
1218 /**
1219  * Delete items in config auth list.
1220  * @param list: list.
1221  */
1222 void config_delauths(struct config_auth* list);
1223 
1224 /**
1225  * Delete a view item
1226  * @param p: view item
1227  */
1228 void config_delview(struct config_view* p);
1229 
1230 /**
1231  * Delete items in config view list.
1232  * @param list: list.
1233  */
1234 void config_delviews(struct config_view* list);
1235 
1236 /** check if config for remote control turns on IP-address interface
1237  * with certificates or a named pipe without certificates. */
1238 int options_remote_is_address(struct config_file* cfg);
1239 
1240 /**
1241  * Convert 14digit to time value
1242  * @param str: string of 14 digits
1243  * @return time value or 0 for error.
1244  */
1245 time_t cfg_convert_timeval(const char* str);
1246 
1247 /**
1248  * Count number of values in the string.
1249  * format ::= (sp num)+ sp
1250  * num ::= [-](0-9)+
1251  * sp ::= (space|tab)*
1252  *
1253  * @param str: string
1254  * @return: 0 on parse error, or empty string, else
1255  *	number of integer values in the string.
1256  */
1257 int cfg_count_numbers(const char* str);
1258 
1259 /**
1260  * Convert a 'nice' memory or file size into a bytecount
1261  * From '100k' to 102400. and so on. Understands kKmMgG.
1262  * k=1024, m=1024*1024, g=1024*1024*1024.
1263  * @param str: string
1264  * @param res: result is stored here, size in bytes.
1265  * @return: true if parsed correctly, or 0 on a parse error (and an error
1266  * is logged).
1267  */
1268 int cfg_parse_memsize(const char* str, size_t* res);
1269 
1270 /**
1271  * Parse nsid from string into binary nsid. nsid is either a hexadecimal
1272  * string or an ascii string prepended with ascii_ in which case the
1273  * characters after ascii_ are simply copied.
1274  * @param str: the string to parse.
1275  * @param nsid_len: returns length of nsid in bytes.
1276  * @return malloced bytes or NULL on parse error or malloc failure.
1277  */
1278 uint8_t* cfg_parse_nsid(const char* str, uint16_t* nsid_len);
1279 
1280 /**
1281  * Add a tag name to the config.  It is added at the end with a new ID value.
1282  * @param cfg: the config structure.
1283  * @param tag: string (which is copied) with the name.
1284  * @return: false on alloc failure.
1285  */
1286 int config_add_tag(struct config_file* cfg, const char* tag);
1287 
1288 /**
1289  * Find tag ID in the tag list.
1290  * @param cfg: the config structure.
1291  * @param tag: string with tag name to search for.
1292  * @return: 0..(num_tags-1) with tag ID, or -1 if tagname is not found.
1293  */
1294 int find_tag_id(struct config_file* cfg, const char* tag);
1295 
1296 /**
1297  * parse taglist from string into bytestring with bitlist.
1298  * @param cfg: the config structure (with tagnames)
1299  * @param str: the string to parse.  Parse puts 0 bytes in string.
1300  * @param listlen: returns length of in bytes.
1301  * @return malloced bytes with a bitlist of the tags.  or NULL on parse error
1302  * or malloc failure.
1303  */
1304 uint8_t* config_parse_taglist(struct config_file* cfg, char* str,
1305 	size_t* listlen);
1306 
1307 /**
1308  * convert tag bitlist to a malloced string with tag names.  For debug output.
1309  * @param cfg: the config structure (with tagnames)
1310  * @param taglist: the tag bitlist.
1311  * @param len: length of the tag bitlist.
1312  * @return malloced string or NULL.
1313  */
1314 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist,
1315 	size_t len);
1316 
1317 /**
1318  * see if two taglists intersect (have tags in common).
1319  * @param list1: first tag bitlist.
1320  * @param list1len: length in bytes of first list.
1321  * @param list2: second tag bitlist.
1322  * @param list2len: length in bytes of second list.
1323  * @return true if there are tags in common, 0 if not.
1324  */
1325 int taglist_intersect(uint8_t* list1, size_t list1len, const uint8_t* list2,
1326 	size_t list2len);
1327 
1328 /**
1329  * Parse local-zone directive into two strings and register it in the config.
1330  * @param cfg: to put it in.
1331  * @param val: argument strings to local-zone, "example.com nodefault".
1332  * @return: false on failure
1333  */
1334 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
1335 
1336 /**
1337  * Mark "number" or "low-high" as available or not in ports array.
1338  * @param str: string in input
1339  * @param allow: give true if this range is permitted.
1340  * @param avail: the array from cfg.
1341  * @param num: size of the array (65536).
1342  * @return: true if parsed correctly, or 0 on a parse error (and an error
1343  * is logged).
1344  */
1345 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
1346 
1347 /**
1348  * Get a condensed list of ports returned. allocated.
1349  * @param cfg: config file.
1350  * @param avail: the available ports array is returned here.
1351  * @return: number of ports in array or 0 on error.
1352  */
1353 int cfg_condense_ports(struct config_file* cfg, int** avail);
1354 
1355 /**
1356  * Apply system specific port range policy.
1357  * @param cfg: config file.
1358  * @param num: size of the array (65536).
1359  */
1360 void cfg_apply_local_port_policy(struct config_file* cfg, int num);
1361 
1362 /**
1363  * Scan ports available
1364  * @param avail: the array from cfg.
1365  * @param num: size of the array (65536).
1366  * @return the number of ports available for use.
1367  */
1368 int cfg_scan_ports(int* avail, int num);
1369 
1370 /**
1371  * Convert a filename to full pathname in original filesys
1372  * @param fname: the path name to convert.
1373  *      Must not be null or empty.
1374  * @param cfg: config struct for chroot and chdir (if set).
1375  * @param use_chdir: if false, only chroot is applied.
1376  * @return pointer to malloced buffer which is: [chroot][chdir]fname
1377  *      or NULL on malloc failure.
1378  */
1379 char* fname_after_chroot(const char* fname, struct config_file* cfg,
1380 	int use_chdir);
1381 
1382 /**
1383  * Convert a ptr shorthand into a full reverse-notation PTR record.
1384  * @param str: input string, "IP name"
1385  * @return: malloced string "reversed-ip-name PTR name"
1386  */
1387 char* cfg_ptr_reverse(char* str);
1388 
1389 /**
1390  * Used during options parsing
1391  */
1392 struct config_parser_state {
1393 	/** name of file being parser */
1394 	char* filename;
1395 	/** line number in the file, starts at 1 */
1396 	int line;
1397 	/** number of errors encountered */
1398 	int errors;
1399 	/** the result of parsing is stored here. */
1400 	struct config_file* cfg;
1401 	/** the current chroot dir (or NULL if none) */
1402 	const char* chroot;
1403 	/** if we are started in a toplevel, or not, after a force_toplevel */
1404 	int started_toplevel;
1405 };
1406 
1407 /** global config parser object used during config parsing */
1408 extern struct config_parser_state* cfg_parser;
1409 /** init lex state */
1410 void init_cfg_parse(void);
1411 /** lex in file */
1412 extern FILE* ub_c_in;
1413 /** lex out file */
1414 extern FILE* ub_c_out;
1415 /** the yacc lex generated parse function */
1416 int ub_c_parse(void);
1417 /** the lexer function */
1418 int ub_c_lex(void);
1419 /** wrap function */
1420 int ub_c_wrap(void);
1421 /** parsing helpers: print error with file and line numbers */
1422 void ub_c_error(const char* msg);
1423 /** parsing helpers: print error with file and line numbers */
1424 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
1425 
1426 #ifdef UB_ON_WINDOWS
1427 /**
1428  * Obtain registry string (if it exists).
1429  * @param key: key string
1430  * @param name: name of value to fetch.
1431  * @return malloced string with the result or NULL if it did not
1432  * 	exist on an error (logged with log_err) was encountered.
1433  */
1434 char* w_lookup_reg_str(const char* key, const char* name);
1435 
1436 /** Modify directory in options for module file name */
1437 void w_config_adjust_directory(struct config_file* cfg);
1438 #endif /* UB_ON_WINDOWS */
1439 
1440 /** debug option for unit tests. */
1441 extern int fake_dsa, fake_sha1;
1442 
1443 /** Return true if interface will listen to specific port(s).
1444  * @param ifname: the interface as configured in the configuration file.
1445  * @param default_port: the default port to use as the interface port if ifname
1446  *	does not include a port via the '@' notation.
1447  * @param port: port to check for, if 0 it will not be checked.
1448  * @param additional_ports: additional configured ports, if any (nonNULL) to
1449  *	be checked against.
1450  * @return true if one of (port, additional_ports) matches the interface port.
1451  */
1452 int if_listens_on(const char* ifname, int default_port, int port,
1453 	struct config_strlist* additional_ports);
1454 
1455 /** see if interface will listen on https;
1456  *  its port number == the https port number */
1457 int if_is_https(const char* ifname, int default_port, int https_port);
1458 
1459 /** see if interface will listen on ssl;
1460  *  its port number == the ssl port number or any of the additional ports */
1461 int if_is_ssl(const char* ifname, int default_port, int ssl_port,
1462 	struct config_strlist* tls_additional_port);
1463 
1464 /** see if interface will listen on PROXYv2;
1465  *  its port number == any of the proxy ports number */
1466 int if_is_pp2(const char* ifname, int default_port,
1467 	struct config_strlist* proxy_protocol_port);
1468 
1469 /** see if interface will listen on DNSCRYPT;
1470  *  its port number == the dnscrypt port number */
1471 int if_is_dnscrypt(const char* ifname, int default_port, int dnscrypt_port);
1472 
1473 /** see if interface will listen on quic;
1474  *  its port number == the quic port number */
1475 int if_is_quic(const char* ifname, int default_port, int quic_port);
1476 
1477 /**
1478  * Return true if the config contains settings that enable https.
1479  * @param cfg: config information.
1480  * @return true if https ports are used for server.
1481  */
1482 int cfg_has_https(struct config_file* cfg);
1483 
1484 /**
1485  * Return true if the config contains settings that enable quic.
1486  * @param cfg: config information.
1487  * @return true if quic ports are used for server.
1488  */
1489 int cfg_has_quic(struct config_file* cfg);
1490 
1491 #ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
1492 #define LINUX_IP_LOCAL_PORT_RANGE_PATH "/proc/sys/net/ipv4/ip_local_port_range"
1493 #endif
1494 
1495 /** get memory for string */
1496 size_t getmem_str(char* str);
1497 
1498 /**
1499  * See if the if_automatic_ports list contains the value.
1500  * @param ports: String with port numbers.
1501  * @param p: number looked for.
1502  * @return true if found, false if not found or parse failure.
1503  */
1504 int cfg_ports_list_contains(char* ports, int p);
1505 
1506 /**
1507  * Check if the configured string contains supported TLS protocols.
1508  * @param tls_protocols: String with TLS protocols.
1509  * @return true if all options are valid, else false.
1510  */
1511 int cfg_tls_protocols_is_valid(const char* tls_protocols);
1512 
1513 /**
1514  * Based on the configured TLS protocols fill which ones are allowed.
1515  * @param tls_protocols: String with TLS protocols.
1516  * @param allow12: will be true if TLSv1.2 is configured.
1517  * @param allow13: will be true if TLSv1.3 is configured.
1518  */
1519 void cfg_tls_protocols_allowed(const char* tls_protocols, int* allow12, int* allow13);
1520 
1521 /** get the file mtime stat (or error, with errno and nonexist) */
1522 int file_get_mtime(const char* file, time_t* mtime, long* ns, int* nonexist);
1523 
1524 #endif /* UTIL_CONFIG_FILE_H */
1525