xref: /freebsd/contrib/unbound/util/config_file.h (revision cc698b490001646c7174d14af6500400be9bd4ff)
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 struct config_stub;
45 struct config_auth;
46 struct config_view;
47 struct config_strlist;
48 struct config_str2list;
49 struct config_str3list;
50 struct config_strbytelist;
51 struct module_qstate;
52 struct sock_list;
53 struct ub_packed_rrset_key;
54 struct regional;
55 
56 /** List head for strlist processing, used for append operation. */
57 struct config_strlist_head {
58 	/** first in list of text items */
59 	struct config_strlist* first;
60 	/** last in list of text items */
61 	struct config_strlist* last;
62 };
63 
64 /**
65  * The configuration options.
66  * Strings are malloced.
67  */
68 struct config_file {
69 	/** verbosity level as specified in the config file */
70 	int verbosity;
71 
72 	/** statistics interval (in seconds) */
73 	int stat_interval;
74 	/** if false, statistics values are reset after printing them */
75 	int stat_cumulative;
76 	/** if true, the statistics are kept in greater detail */
77 	int stat_extended;
78 
79 	/** number of threads to create */
80 	int num_threads;
81 
82 	/** port on which queries are answered. */
83 	int port;
84 	/** do ip4 query support. */
85 	int do_ip4;
86 	/** do ip6 query support. */
87 	int do_ip6;
88 	/** prefer ip6 upstream queries. */
89 	int prefer_ip6;
90 	/** do udp query support. */
91 	int do_udp;
92 	/** do tcp query support. */
93 	int do_tcp;
94 	/** tcp upstream queries (no UDP upstream queries) */
95 	int tcp_upstream;
96 	/** udp upstream enabled when no UDP downstream is enabled (do_udp no)*/
97 	int udp_upstream_without_downstream;
98 	/** maximum segment size of tcp socket which queries are answered */
99 	int tcp_mss;
100 	/** maximum segment size of tcp socket for outgoing queries */
101 	int outgoing_tcp_mss;
102 	/** tcp idle timeout, in msec */
103 	int tcp_idle_timeout;
104 	/** do edns tcp keepalive */
105 	int do_tcp_keepalive;
106 	/** tcp keepalive timeout, in msec */
107 	int tcp_keepalive_timeout;
108 
109 	/** private key file for dnstcp-ssl service (enabled if not NULL) */
110 	char* ssl_service_key;
111 	/** public key file for dnstcp-ssl service */
112 	char* ssl_service_pem;
113 	/** port on which to provide ssl service */
114 	int ssl_port;
115 	/** if outgoing tcp connections use SSL */
116 	int ssl_upstream;
117 	/** cert bundle for outgoing connections */
118 	char* tls_cert_bundle;
119 	/** should the system certificate store get added to the cert bundle */
120 	int tls_win_cert;
121 	/** additional tls ports */
122 	struct config_strlist* tls_additional_port;
123 	/** secret key used to encrypt and decrypt TLS session ticket */
124 	struct config_strlist_head tls_session_ticket_keys;
125 	/** TLS ciphers */
126 	char* tls_ciphers;
127 	/** TLS chiphersuites (TLSv1.3) */
128 	char* tls_ciphersuites;
129 
130 	/** outgoing port range number of ports (per thread) */
131 	int outgoing_num_ports;
132 	/** number of outgoing tcp buffers per (per thread) */
133 	size_t outgoing_num_tcp;
134 	/** number of incoming tcp buffers per (per thread) */
135 	size_t incoming_num_tcp;
136 	/** allowed udp port numbers, array with 0 if not allowed */
137 	int* outgoing_avail_ports;
138 
139 	/** EDNS buffer size to use */
140 	size_t edns_buffer_size;
141 	/** size of the stream wait buffers, max */
142 	size_t stream_wait_size;
143 	/** number of bytes buffer size for DNS messages */
144 	size_t msg_buffer_size;
145 	/** size of the message cache */
146 	size_t msg_cache_size;
147 	/** slabs in the message cache. */
148 	size_t msg_cache_slabs;
149 	/** number of queries every thread can service */
150 	size_t num_queries_per_thread;
151 	/** number of msec to wait before items can be jostled out */
152 	size_t jostle_time;
153 	/** size of the rrset cache */
154 	size_t rrset_cache_size;
155 	/** slabs in the rrset cache */
156 	size_t rrset_cache_slabs;
157 	/** host cache ttl in seconds */
158 	int host_ttl;
159 	/** number of slabs in the infra host cache */
160 	size_t infra_cache_slabs;
161 	/** max number of hosts in the infra cache */
162 	size_t infra_cache_numhosts;
163 	/** min value for infra cache rtt */
164 	int infra_cache_min_rtt;
165 	/** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
166 	int delay_close;
167 
168 	/** the target fetch policy for the iterator */
169 	char* target_fetch_policy;
170 	/** percent*10, how many times in 1000 to pick from the fastest
171 	 * destinations */
172 	int fast_server_permil;
173 	/** number of fastest server to select from */
174 	size_t fast_server_num;
175 
176 	/** automatic interface for incoming messages. Uses ipv6 remapping,
177 	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
178 	int if_automatic;
179 	/** SO_RCVBUF size to set on port 53 UDP socket */
180 	size_t so_rcvbuf;
181 	/** SO_SNDBUF size to set on port 53 UDP socket */
182 	size_t so_sndbuf;
183 	/** SO_REUSEPORT requested on port 53 sockets */
184 	int so_reuseport;
185 	/** IP_TRANSPARENT socket option requested on port 53 sockets */
186 	int ip_transparent;
187 	/** IP_FREEBIND socket option request on port 53 sockets */
188 	int ip_freebind;
189 
190 	/** number of interfaces to open. If 0 default all interfaces. */
191 	int num_ifs;
192 	/** interface description strings (IP addresses) */
193 	char **ifs;
194 
195 	/** number of outgoing interfaces to open.
196 	 * If 0 default all interfaces. */
197 	int num_out_ifs;
198 	/** outgoing interface description strings (IP addresses) */
199 	char **out_ifs;
200 
201 	/** the root hints */
202 	struct config_strlist* root_hints;
203 	/** the stub definitions, linked list */
204 	struct config_stub* stubs;
205 	/** the forward zone definitions, linked list */
206 	struct config_stub* forwards;
207 	/** the auth zone definitions, linked list */
208 	struct config_auth* auths;
209 	/** the views definitions, linked list */
210 	struct config_view* views;
211 	/** list of donotquery addresses, linked list */
212 	struct config_strlist* donotqueryaddrs;
213 #ifdef CLIENT_SUBNET
214 	/** list of servers we send edns-client-subnet option to and
215 	 * accept option from, linked list */
216 	struct config_strlist* client_subnet;
217 	/** list of zones we send edns-client-subnet option for */
218 	struct config_strlist* client_subnet_zone;
219 	/** opcode assigned by IANA for edns0-client-subnet option */
220 	uint16_t client_subnet_opcode;
221 	/** Do not check whitelist if incoming query contains an ECS record */
222 	int client_subnet_always_forward;
223 	/** Subnet length we are willing to give up privacy for */
224 	uint8_t max_client_subnet_ipv4;
225 	uint8_t max_client_subnet_ipv6;
226 	/** Minimum subnet length we are willing to answer */
227 	uint8_t min_client_subnet_ipv4;
228 	uint8_t min_client_subnet_ipv6;
229 	/** Max number of nodes in the ECS radix tree */
230 	uint32_t max_ecs_tree_size_ipv4;
231 	uint32_t max_ecs_tree_size_ipv6;
232 #endif
233 	/** list of access control entries, linked list */
234 	struct config_str2list* acls;
235 	/** use default localhost donotqueryaddr entries */
236 	int donotquery_localhost;
237 
238 	/** list of tcp connection limitss, linked list */
239 	struct config_str2list* tcp_connection_limits;
240 
241 	/** harden against very small edns buffer sizes */
242 	int harden_short_bufsize;
243 	/** harden against very large query sizes */
244 	int harden_large_queries;
245 	/** harden against spoofed glue (out of zone data) */
246 	int harden_glue;
247 	/** harden against receiving no DNSSEC data for trust anchor */
248 	int harden_dnssec_stripped;
249 	/** harden against queries that fall under known nxdomain names */
250 	int harden_below_nxdomain;
251 	/** harden the referral path, query for NS,A,AAAA and validate */
252 	int harden_referral_path;
253 	/** harden against algorithm downgrade */
254 	int harden_algo_downgrade;
255 	/** use 0x20 bits in query as random ID bits */
256 	int use_caps_bits_for_id;
257 	/** 0x20 whitelist, domains that do not use capsforid */
258 	struct config_strlist* caps_whitelist;
259 	/** strip away these private addrs from answers, no DNS Rebinding */
260 	struct config_strlist* private_address;
261 	/** allow domain (and subdomains) to use private address space */
262 	struct config_strlist* private_domain;
263 	/** what threshold for unwanted action. */
264 	size_t unwanted_threshold;
265 	/** the number of seconds maximal TTL used for RRsets and messages */
266 	int max_ttl;
267 	/** the number of seconds minimum TTL used for RRsets and messages */
268 	int min_ttl;
269 	/** the number of seconds maximal negative TTL for SOA in auth */
270 	int max_negative_ttl;
271 	/** if prefetching of messages should be performed. */
272 	int prefetch;
273 	/** if prefetching of DNSKEYs should be performed. */
274 	int prefetch_key;
275 	/** deny queries of type ANY with an empty answer */
276 	int deny_any;
277 
278 	/** chrootdir, if not "" or chroot will be done */
279 	char* chrootdir;
280 	/** username to change to, if not "". */
281 	char* username;
282 	/** working directory */
283 	char* directory;
284 	/** filename to log to. */
285 	char* logfile;
286 	/** pidfile to write pid to. */
287 	char* pidfile;
288 
289 	/** should log messages be sent to syslogd */
290 	int use_syslog;
291 	/** log timestamp in ascii UTC */
292 	int log_time_ascii;
293 	/** log queries with one line per query */
294 	int log_queries;
295 	/** log replies with one line per reply */
296 	int log_replies;
297 	/** tag log_queries and log_replies for filtering */
298 	int log_tag_queryreply;
299 	/** log every local-zone hit **/
300 	int log_local_actions;
301 	/** log servfails with a reason */
302 	int log_servfail;
303 	/** log identity to report */
304 	char* log_identity;
305 
306 	/** do not report identity (id.server, hostname.bind) */
307 	int hide_identity;
308 	/** do not report version (version.server, version.bind) */
309 	int hide_version;
310 	/** do not report trustanchor (trustanchor.unbound) */
311 	int hide_trustanchor;
312 	/** identity, hostname is returned if "". */
313 	char* identity;
314 	/** version, package version returned if "". */
315 	char* version;
316 
317 	/** the module configuration string */
318 	char* module_conf;
319 
320 	/** files with trusted DS and DNSKEYs in zonefile format, list */
321 	struct config_strlist* trust_anchor_file_list;
322 	/** list of trustanchor keys, linked list */
323 	struct config_strlist* trust_anchor_list;
324 	/** files with 5011 autotrust tracked keys */
325 	struct config_strlist* auto_trust_anchor_file_list;
326 	/** files with trusted DNSKEYs in named.conf format, list */
327 	struct config_strlist* trusted_keys_file_list;
328 	/** DLV anchor file */
329 	char* dlv_anchor_file;
330 	/** DLV anchor inline */
331 	struct config_strlist* dlv_anchor_list;
332 	/** insecure domain list */
333 	struct config_strlist* domain_insecure;
334 	/** send key tag query */
335 	int trust_anchor_signaling;
336 	/** enable root key sentinel */
337 	int root_key_sentinel;
338 
339 	/** if not 0, this value is the validation date for RRSIGs */
340 	int32_t val_date_override;
341 	/** the minimum for signature clock skew */
342 	int32_t val_sig_skew_min;
343 	/** the maximum for signature clock skew */
344 	int32_t val_sig_skew_max;
345 	/** this value sets the number of seconds before revalidating bogus */
346 	int bogus_ttl;
347 	/** should validator clean additional section for secure msgs */
348 	int val_clean_additional;
349 	/** log bogus messages by the validator */
350 	int val_log_level;
351 	/** squelch val_log_level to log - this is library goes to callback */
352 	int val_log_squelch;
353 	/** should validator allow bogus messages to go through */
354 	int val_permissive_mode;
355 	/** use cached NSEC records to synthesise (negative) answers */
356 	int aggressive_nsec;
357 	/** ignore the CD flag in incoming queries and refuse them bogus data */
358 	int ignore_cd;
359 	/** serve expired entries and prefetch them */
360 	int serve_expired;
361 	/** serve expired entries until TTL after expiration */
362 	int serve_expired_ttl;
363 	/** reset serve expired TTL after failed update attempt */
364 	int serve_expired_ttl_reset;
365 	/** nsec3 maximum iterations per key size, string */
366 	char* val_nsec3_key_iterations;
367 	/** autotrust add holddown time, in seconds */
368 	unsigned int add_holddown;
369 	/** autotrust del holddown time, in seconds */
370 	unsigned int del_holddown;
371 	/** autotrust keep_missing time, in seconds. 0 is forever. */
372 	unsigned int keep_missing;
373 	/** permit small holddown values, allowing 5011 rollover very fast */
374 	int permit_small_holddown;
375 
376 	/** size of the key cache */
377 	size_t key_cache_size;
378 	/** slabs in the key cache. */
379 	size_t key_cache_slabs;
380 	/** size of the neg cache */
381 	size_t neg_cache_size;
382 
383 	/** local zones config */
384 	struct config_str2list* local_zones;
385 	/** local zones nodefault list */
386 	struct config_strlist* local_zones_nodefault;
387 	/** do not add any default local zone */
388 	int local_zones_disable_default;
389 	/** local data RRs configured */
390 	struct config_strlist* local_data;
391 	/** local zone override types per netblock */
392 	struct config_str3list* local_zone_overrides;
393 	/** unblock lan zones (reverse lookups for AS112 zones) */
394 	int unblock_lan_zones;
395 	/** insecure lan zones (don't validate AS112 zones) */
396 	int insecure_lan_zones;
397 	/** list of zonename, tagbitlist */
398 	struct config_strbytelist* local_zone_tags;
399 	/** list of aclname, tagbitlist */
400 	struct config_strbytelist* acl_tags;
401 	/** list of aclname, tagname, localzonetype */
402 	struct config_str3list* acl_tag_actions;
403 	/** list of aclname, tagname, redirectdata */
404 	struct config_str3list* acl_tag_datas;
405 	/** list of aclname, view*/
406 	struct config_str2list* acl_view;
407 	/** list of IP-netblock, tagbitlist */
408 	struct config_strbytelist* respip_tags;
409 	/** list of response-driven access control entries, linked list */
410 	struct config_str2list* respip_actions;
411 	/** RRs configured for response-driven access controls */
412 	struct config_str2list* respip_data;
413 	/** tag list, array with tagname[i] is malloced string */
414 	char** tagname;
415 	/** number of items in the taglist */
416 	int num_tags;
417 
418 	/** remote control section. enable toggle. */
419 	int remote_control_enable;
420 	/** the interfaces the remote control should listen on */
421 	struct config_strlist_head control_ifs;
422 	/** if the use-cert option is set */
423 	int control_use_cert;
424 	/** port number for the control port */
425 	int control_port;
426 	/** private key file for server */
427 	char* server_key_file;
428 	/** certificate file for server */
429 	char* server_cert_file;
430 	/** private key file for unbound-control */
431 	char* control_key_file;
432 	/** certificate file for unbound-control */
433 	char* control_cert_file;
434 
435 	/** Python script file */
436 	char* python_script;
437 
438 	/** Use systemd socket activation. */
439 	int use_systemd;
440 
441 	/** daemonize, i.e. fork into the background. */
442 	int do_daemonize;
443 
444 	/* minimal response when positive answer */
445 	int minimal_responses;
446 
447 	/* RRSet roundrobin */
448 	int rrset_roundrobin;
449 
450 	/* wait time for unknown server in msec */
451 	int unknown_server_time_limit;
452 
453 	/* maximum UDP response size */
454 	size_t max_udp_size;
455 
456 	/* DNS64 prefix */
457 	char* dns64_prefix;
458 
459 	/* Synthetize all AAAA record despite the presence of an authoritative one */
460 	int dns64_synthall;
461 	/** ignore AAAAs for these domain names and use A record anyway */
462 	struct config_strlist* dns64_ignore_aaaa;
463 
464 	/** true to enable dnstap support */
465 	int dnstap;
466 	/** dnstap socket path */
467 	char* dnstap_socket_path;
468 	/** true to send "identity" via dnstap */
469 	int dnstap_send_identity;
470 	/** true to send "version" via dnstap */
471 	int dnstap_send_version;
472 	/** dnstap "identity", hostname is used if "". */
473 	char* dnstap_identity;
474 	/** dnstap "version", package version is used if "". */
475 	char* dnstap_version;
476 
477 	/** true to log dnstap RESOLVER_QUERY message events */
478 	int dnstap_log_resolver_query_messages;
479 	/** true to log dnstap RESOLVER_RESPONSE message events */
480 	int dnstap_log_resolver_response_messages;
481 	/** true to log dnstap CLIENT_QUERY message events */
482 	int dnstap_log_client_query_messages;
483 	/** true to log dnstap CLIENT_RESPONSE message events */
484 	int dnstap_log_client_response_messages;
485 	/** true to log dnstap FORWARDER_QUERY message events */
486 	int dnstap_log_forwarder_query_messages;
487 	/** true to log dnstap FORWARDER_RESPONSE message events */
488 	int dnstap_log_forwarder_response_messages;
489 
490 	/** true to disable DNSSEC lameness check in iterator */
491 	int disable_dnssec_lame_check;
492 
493 	/** ratelimit for ip addresses. 0 is off, otherwise qps (unless overridden) */
494 	int ip_ratelimit;
495 	/** number of slabs for ip_ratelimit cache */
496 	size_t ip_ratelimit_slabs;
497 	/** memory size in bytes for ip_ratelimit cache */
498 	size_t ip_ratelimit_size;
499 	/** ip_ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
500 	int ip_ratelimit_factor;
501 
502 	/** ratelimit for domains. 0 is off, otherwise qps (unless overridden) */
503 	int ratelimit;
504 	/** number of slabs for ratelimit cache */
505 	size_t ratelimit_slabs;
506 	/** memory size in bytes for ratelimit cache */
507 	size_t ratelimit_size;
508 	/** ratelimits for domain (exact match) */
509 	struct config_str2list* ratelimit_for_domain;
510 	/** ratelimits below domain */
511 	struct config_str2list* ratelimit_below_domain;
512 	/** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
513 	int ratelimit_factor;
514 	/** minimise outgoing QNAME and hide original QTYPE if possible */
515 	int qname_minimisation;
516 	/** minimise QNAME in strict mode, minimise according to RFC.
517 	 *  Do not apply fallback */
518 	int qname_minimisation_strict;
519 	/** SHM data - true if shm is enabled */
520 	int shm_enable;
521 	/** SHM data - key for the shm */
522 	int shm_key;
523 
524 	/** DNSCrypt */
525 	/** true to enable dnscrypt */
526 	int dnscrypt;
527 	/** port on which to provide dnscrypt service */
528 	int dnscrypt_port;
529 	/** provider name 2.dnscrypt-cert.example.com */
530 	char* dnscrypt_provider;
531 	/** dnscrypt secret keys 1.key */
532 	struct config_strlist* dnscrypt_secret_key;
533 	/** dnscrypt provider certs 1.cert */
534 	struct config_strlist* dnscrypt_provider_cert;
535 	/** dnscrypt provider certs 1.cert which have been rotated and should not be
536 	* advertised through DNS's providername TXT record but are required to be
537 	* able to handle existing traffic using the old cert. */
538 	struct config_strlist* dnscrypt_provider_cert_rotated;
539 	/** memory size in bytes for dnscrypt shared secrets cache */
540 	size_t dnscrypt_shared_secret_cache_size;
541 	/** number of slabs for dnscrypt shared secrets cache */
542 	size_t dnscrypt_shared_secret_cache_slabs;
543 	/** memory size in bytes for dnscrypt nonces cache */
544 	size_t dnscrypt_nonce_cache_size;
545 	/** number of slabs for dnscrypt nonces cache */
546 	size_t dnscrypt_nonce_cache_slabs;
547 	/** IPsec module */
548 #ifdef USE_IPSECMOD
549 	/** false to bypass the IPsec module */
550 	int ipsecmod_enabled;
551 	/** whitelisted domains for ipsecmod */
552 	struct config_strlist* ipsecmod_whitelist;
553 	/** path to external hook */
554 	char* ipsecmod_hook;
555 	/** true to proceed even with a bogus IPSECKEY */
556 	int ipsecmod_ignore_bogus;
557 	/** max TTL for the A/AAAA records that call the hook */
558 	int ipsecmod_max_ttl;
559 	/** false to proceed even when ipsecmod_hook fails */
560 	int ipsecmod_strict;
561 #endif
562 
563 	/* cachedb module */
564 #ifdef USE_CACHEDB
565 	/** backend DB name */
566 	char* cachedb_backend;
567 	/** secret seed for hash key calculation */
568 	char* cachedb_secret;
569 #ifdef USE_REDIS
570 	/** redis server's IP address or host name */
571 	char* redis_server_host;
572 	/** redis server's TCP port */
573 	int redis_server_port;
574 	/** timeout (in ms) for communication with the redis server */
575 	int redis_timeout;
576 #endif
577 #endif
578 };
579 
580 /** from cfg username, after daemonize setup performed */
581 extern uid_t cfg_uid;
582 /** from cfg username, after daemonize setup performed */
583 extern gid_t cfg_gid;
584 /** debug and enable small timeouts */
585 extern int autr_permit_small_holddown;
586 /** size (in bytes) of stream wait buffers max */
587 extern size_t stream_wait_max;
588 
589 /**
590  * Stub config options
591  */
592 struct config_stub {
593 	/** next in list */
594 	struct config_stub* next;
595 	/** domain name (in text) of the stub apex domain */
596 	char* name;
597 	/** list of stub nameserver hosts (domain name) */
598 	struct config_strlist* hosts;
599 	/** list of stub nameserver addresses (IP address) */
600 	struct config_strlist* addrs;
601 	/** if stub-prime is set */
602 	int isprime;
603 	/** if forward-first is set (failover to without if fails) */
604 	int isfirst;
605 	/** use SSL for queries to this stub */
606 	int ssl_upstream;
607 	/*** no cache */
608 	int no_cache;
609 };
610 
611 /**
612  * Auth config options
613  */
614 struct config_auth {
615 	/** next in list */
616 	struct config_auth* next;
617 	/** domain name (in text) of the auth apex domain */
618 	char* name;
619 	/** list of masters */
620 	struct config_strlist* masters;
621 	/** list of urls */
622 	struct config_strlist* urls;
623 	/** list of allow-notify */
624 	struct config_strlist* allow_notify;
625 	/** zonefile (or NULL) */
626 	char* zonefile;
627 	/** provide downstream answers */
628 	int for_downstream;
629 	/** provide upstream answers */
630 	int for_upstream;
631 	/** fallback to recursion to authorities if zone expired and other
632 	 * reasons perhaps (like, query bogus) */
633 	int fallback_enabled;
634 };
635 
636 /**
637  * View config options
638  */
639 struct config_view {
640 	/** next in list */
641 	struct config_view* next;
642 	/** view name */
643 	char* name;
644 	/** local zones */
645 	struct config_str2list* local_zones;
646 	/** local data RRs */
647 	struct config_strlist* local_data;
648 	/** local zones nodefault list */
649 	struct config_strlist* local_zones_nodefault;
650 	/** Fallback to global local_zones when there is no match in the view
651 	 * view specific tree. 1 for yes, 0 for no */
652 	int isfirst;
653 	/** predefined actions for particular IP address responses */
654 	struct config_str2list* respip_actions;
655 	/** data complementing the 'redirect' response IP actions */
656 	struct config_str2list* respip_data;
657 };
658 
659 /**
660  * List of strings for config options
661  */
662 struct config_strlist {
663 	/** next item in list */
664 	struct config_strlist* next;
665 	/** config option string */
666 	char* str;
667 };
668 
669 /**
670  * List of two strings for config options
671  */
672 struct config_str2list {
673 	/** next item in list */
674 	struct config_str2list* next;
675 	/** first string */
676 	char* str;
677 	/** second string */
678 	char* str2;
679 };
680 
681 /**
682  * List of three strings for config options
683  */
684 struct config_str3list {
685 	/** next item in list */
686 	struct config_str3list* next;
687 	/** first string */
688 	char* str;
689 	/** second string */
690 	char* str2;
691 	/** third string */
692 	char* str3;
693 };
694 
695 
696 /**
697  * List of string, bytestring for config options
698  */
699 struct config_strbytelist {
700 	/** next item in list */
701 	struct config_strbytelist* next;
702 	/** first string */
703 	char* str;
704 	/** second bytestring */
705 	uint8_t* str2;
706 	size_t str2len;
707 };
708 
709 /**
710  * Create config file structure. Filled with default values.
711  * @return: the new structure or NULL on memory error.
712  */
713 struct config_file* config_create(void);
714 
715 /**
716  * Create config file structure for library use. Filled with default values.
717  * @return: the new structure or NULL on memory error.
718  */
719 struct config_file* config_create_forlib(void);
720 
721 /**
722  * Read the config file from the specified filename.
723  * @param config: where options are stored into, must be freshly created.
724  * @param filename: name of configfile. If NULL nothing is done.
725  * @param chroot: if not NULL, the chroot dir currently in use (for include).
726  * @return: false on error. In that case errno is set, ENOENT means
727  * 	file not found.
728  */
729 int config_read(struct config_file* config, const char* filename,
730 	const char* chroot);
731 
732 /**
733  * Destroy the config file structure.
734  * @param config: to delete.
735  */
736 void config_delete(struct config_file* config);
737 
738 /**
739  * Apply config to global constants; this routine is called in single thread.
740  * @param config: to apply. Side effect: global constants change.
741  */
742 void config_apply(struct config_file* config);
743 
744 /**
745  * Find username, sets cfg_uid and cfg_gid.
746  * @param config: the config structure.
747  */
748 void config_lookup_uid(struct config_file* config);
749 
750 /**
751  * Set the given keyword to the given value.
752  * @param config: where to store config
753  * @param option: option name, including the ':' character.
754  * @param value: value, this string is copied if needed, or parsed.
755  * 	The caller owns the value string.
756  * @return 0 on error (malloc or syntax error).
757  */
758 int config_set_option(struct config_file* config, const char* option,
759 	const char* value);
760 
761 /**
762  * Call print routine for the given option.
763  * @param cfg: config.
764  * @param opt: option name without trailing :.
765  *	This is different from config_set_option.
766  * @param func: print func, called as (str, arg) for every data element.
767  * @param arg: user argument for print func.
768  * @return false if the option name is not supported (syntax error).
769  */
770 int config_get_option(struct config_file* cfg, const char* opt,
771 	void (*func)(char*,void*), void* arg);
772 
773 /**
774  * Get an option and return strlist
775  * @param cfg: config file
776  * @param opt: option name.
777  * @param list: list is returned here. malloced, caller must free it.
778  * @return 0=OK, 1=syntax error, 2=malloc failed.
779  */
780 int config_get_option_list(struct config_file* cfg, const char* opt,
781 	struct config_strlist** list);
782 
783 /**
784  * Get an option and collate results into string
785  * @param cfg: config file
786  * @param opt: option name.
787  * @param str: string. malloced, caller must free it.
788  * @return 0=OK, 1=syntax error, 2=malloc failed.
789  */
790 int config_get_option_collate(struct config_file* cfg, const char* opt,
791 	char** str);
792 
793 /**
794  * function to print to a file, use as func with config_get_option.
795  * @param line: text to print. \n appended.
796  * @param arg: pass a FILE*, like stdout.
797  */
798 void config_print_func(char* line, void* arg);
799 
800 /**
801  * function to collate the text strings into a strlist_head.
802  * @param line: text to append.
803  * @param arg: pass a strlist_head structure. zeroed on start.
804  */
805 void config_collate_func(char* line, void* arg);
806 
807 /**
808  * take a strlist_head list and return a malloc string. separated with newline.
809  * @param list: strlist first to collate. zeroes return "".
810  * @return NULL on malloc failure. Or if malloc failure happened in strlist.
811  */
812 char* config_collate_cat(struct config_strlist* list);
813 
814 /**
815  * Append text at end of list.
816  * @param list: list head. zeroed at start.
817  * @param item: new item. malloced by caller. if NULL the insertion fails.
818  * @return true on success.
819  * on fail the item is free()ed.
820  */
821 int cfg_strlist_append(struct config_strlist_head* list, char* item);
822 
823 /**
824  * Find string in strlist.
825  * @param head: pointer to strlist head variable.
826  * @param item: the item to search for.
827  * @return: the element in the list when found, NULL otherwise.
828  */
829 struct config_strlist* cfg_strlist_find(struct config_strlist* head,
830 	const char* item);
831 
832 /**
833  * Insert string into strlist.
834  * @param head: pointer to strlist head variable.
835  * @param item: new item. malloced by caller. If NULL the insertion fails.
836  * @return: true on success.
837  * on fail, the item is free()d.
838  */
839 int cfg_strlist_insert(struct config_strlist** head, char* item);
840 
841 /** insert with region for allocation. */
842 int cfg_region_strlist_insert(struct regional* region,
843 	struct config_strlist** head, char* item);
844 
845 /**
846  * Insert string into str2list.
847  * @param head: pointer to str2list head variable.
848  * @param item: new item. malloced by caller. If NULL the insertion fails.
849  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
850  * @return: true on success.
851  * on fail, the item and i2 are free()d.
852  */
853 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
854 
855 /**
856  * Insert string into str3list.
857  * @param head: pointer to str3list head variable.
858  * @param item: new item. malloced by caller. If NULL the insertion fails.
859  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
860  * @param i3: 3rd string, malloced by caller. If NULL the insertion fails.
861  * @return: true on success.
862  */
863 int cfg_str3list_insert(struct config_str3list** head, char* item, char* i2,
864 	char* i3);
865 
866 /**
867  * Insert string into strbytelist.
868  * @param head: pointer to strbytelist head variable.
869  * @param item: new item. malloced by caller. If NULL the insertion fails.
870  * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
871  * @param i2len: length of the i2 bytestring.
872  * @return: true on success.
873  */
874 int cfg_strbytelist_insert(struct config_strbytelist** head, char* item,
875 	uint8_t* i2, size_t i2len);
876 
877 /**
878  * Find stub in config list, also returns prevptr (for deletion).
879  * @param pp: call routine with pointer to a pointer to the start of the list,
880  * 	if the stub is found, on exit, the value contains a pointer to the
881  * 	next pointer that points to the found element (or to the list start
882  * 	pointer if it is the first element).
883  * @param nm: name of stub to find.
884  * @return: pointer to config_stub if found, or NULL if not found.
885  */
886 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm);
887 
888 /**
889  * Delete items in config string list.
890  * @param list: list.
891  */
892 void config_delstrlist(struct config_strlist* list);
893 
894 /**
895  * Delete items in config double string list.
896  * @param list: list.
897  */
898 void config_deldblstrlist(struct config_str2list* list);
899 
900 /**
901  * Delete items in config triple string list.
902  * @param list: list.
903  */
904 void config_deltrplstrlist(struct config_str3list* list);
905 
906 /** delete stringbytelist */
907 void config_del_strbytelist(struct config_strbytelist* list);
908 
909 /**
910  * Delete a stub item
911  * @param p: stub item
912  */
913 void config_delstub(struct config_stub* p);
914 
915 /**
916  * Delete items in config stub list.
917  * @param list: list.
918  */
919 void config_delstubs(struct config_stub* list);
920 
921 /**
922  * Delete an auth item
923  * @param p: auth item
924  */
925 void config_delauth(struct config_auth* p);
926 
927 /**
928  * Delete items in config auth list.
929  * @param list: list.
930  */
931 void config_delauths(struct config_auth* list);
932 
933 /**
934  * Delete a view item
935  * @param p: view item
936  */
937 void config_delview(struct config_view* p);
938 
939 /**
940  * Delete items in config view list.
941  * @param list: list.
942  */
943 void config_delviews(struct config_view* list);
944 
945 /** check if config for remote control turns on IP-address interface
946  * with certificates or a named pipe without certificates. */
947 int options_remote_is_address(struct config_file* cfg);
948 
949 /**
950  * Convert 14digit to time value
951  * @param str: string of 14 digits
952  * @return time value or 0 for error.
953  */
954 time_t cfg_convert_timeval(const char* str);
955 
956 /**
957  * Count number of values in the string.
958  * format ::= (sp num)+ sp
959  * num ::= [-](0-9)+
960  * sp ::= (space|tab)*
961  *
962  * @param str: string
963  * @return: 0 on parse error, or empty string, else
964  *	number of integer values in the string.
965  */
966 int cfg_count_numbers(const char* str);
967 
968 /**
969  * Convert a 'nice' memory or file size into a bytecount
970  * From '100k' to 102400. and so on. Understands kKmMgG.
971  * k=1024, m=1024*1024, g=1024*1024*1024.
972  * @param str: string
973  * @param res: result is stored here, size in bytes.
974  * @return: true if parsed correctly, or 0 on a parse error (and an error
975  * is logged).
976  */
977 int cfg_parse_memsize(const char* str, size_t* res);
978 
979 /**
980  * Add a tag name to the config.  It is added at the end with a new ID value.
981  * @param cfg: the config structure.
982  * @param tag: string (which is copied) with the name.
983  * @return: false on alloc failure.
984  */
985 int config_add_tag(struct config_file* cfg, const char* tag);
986 
987 /**
988  * Find tag ID in the tag list.
989  * @param cfg: the config structure.
990  * @param tag: string with tag name to search for.
991  * @return: 0..(num_tags-1) with tag ID, or -1 if tagname is not found.
992  */
993 int find_tag_id(struct config_file* cfg, const char* tag);
994 
995 /**
996  * parse taglist from string into bytestring with bitlist.
997  * @param cfg: the config structure (with tagnames)
998  * @param str: the string to parse.  Parse puts 0 bytes in string.
999  * @param listlen: returns length of in bytes.
1000  * @return malloced bytes with a bitlist of the tags.  or NULL on parse error
1001  * or malloc failure.
1002  */
1003 uint8_t* config_parse_taglist(struct config_file* cfg, char* str,
1004 	size_t* listlen);
1005 
1006 /**
1007  * convert tag bitlist to a malloced string with tag names.  For debug output.
1008  * @param cfg: the config structure (with tagnames)
1009  * @param taglist: the tag bitlist.
1010  * @param len: length of the tag bitlist.
1011  * @return malloced string or NULL.
1012  */
1013 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist,
1014 	size_t len);
1015 
1016 /**
1017  * see if two taglists intersect (have tags in common).
1018  * @param list1: first tag bitlist.
1019  * @param list1len: length in bytes of first list.
1020  * @param list2: second tag bitlist.
1021  * @param list2len: length in bytes of second list.
1022  * @return true if there are tags in common, 0 if not.
1023  */
1024 int taglist_intersect(uint8_t* list1, size_t list1len, uint8_t* list2,
1025 	size_t list2len);
1026 
1027 /**
1028  * Parse local-zone directive into two strings and register it in the config.
1029  * @param cfg: to put it in.
1030  * @param val: argument strings to local-zone, "example.com nodefault".
1031  * @return: false on failure
1032  */
1033 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
1034 
1035 /**
1036  * Mark "number" or "low-high" as available or not in ports array.
1037  * @param str: string in input
1038  * @param allow: give true if this range is permitted.
1039  * @param avail: the array from cfg.
1040  * @param num: size of the array (65536).
1041  * @return: true if parsed correctly, or 0 on a parse error (and an error
1042  * is logged).
1043  */
1044 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
1045 
1046 /**
1047  * Get a condensed list of ports returned. allocated.
1048  * @param cfg: config file.
1049  * @param avail: the available ports array is returned here.
1050  * @return: number of ports in array or 0 on error.
1051  */
1052 int cfg_condense_ports(struct config_file* cfg, int** avail);
1053 
1054 /**
1055  * Scan ports available
1056  * @param avail: the array from cfg.
1057  * @param num: size of the array (65536).
1058  * @return the number of ports available for use.
1059  */
1060 int cfg_scan_ports(int* avail, int num);
1061 
1062 /**
1063  * Convert a filename to full pathname in original filesys
1064  * @param fname: the path name to convert.
1065  *      Must not be null or empty.
1066  * @param cfg: config struct for chroot and chdir (if set).
1067  * @param use_chdir: if false, only chroot is applied.
1068  * @return pointer to malloced buffer which is: [chroot][chdir]fname
1069  *      or NULL on malloc failure.
1070  */
1071 char* fname_after_chroot(const char* fname, struct config_file* cfg,
1072 	int use_chdir);
1073 
1074 /**
1075  * Convert a ptr shorthand into a full reverse-notation PTR record.
1076  * @param str: input string, "IP name"
1077  * @return: malloced string "reversed-ip-name PTR name"
1078  */
1079 char* cfg_ptr_reverse(char* str);
1080 
1081 /**
1082  * Append text to the error info for validation.
1083  * @param qstate: query state.
1084  * @param str: copied into query region and appended.
1085  * Failures to allocate are logged.
1086  */
1087 void errinf(struct module_qstate* qstate, const char* str);
1088 
1089 /**
1090  * Append text to error info:  from 1.2.3.4
1091  * @param qstate: query state.
1092  * @param origin: sock list with origin of trouble.
1093  *	Every element added.
1094  *	If NULL: nothing is added.
1095  *	if 0len element: 'from cache' is added.
1096  */
1097 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
1098 
1099 /**
1100  * Append text to error info:  for RRset name type class
1101  * @param qstate: query state.
1102  * @param rr: rrset_key.
1103  */
1104 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
1105 
1106 /**
1107  * Append text to error info:  str dname
1108  * @param qstate: query state.
1109  * @param str: explanation string
1110  * @param dname: the dname.
1111  */
1112 void errinf_dname(struct module_qstate* qstate, const char* str,
1113 	uint8_t* dname);
1114 
1115 /**
1116  * Create error info in string.  For validation failures.
1117  * @param qstate: query state.
1118  * @return string or NULL on malloc failure (already logged).
1119  *    This string is malloced and has to be freed by caller.
1120  */
1121 char* errinf_to_str_bogus(struct module_qstate* qstate);
1122 
1123 /**
1124  * Create error info in string.  For other servfails.
1125  * @param qstate: query state.
1126  * @return string or NULL on malloc failure (already logged).
1127  *    This string is malloced and has to be freed by caller.
1128  */
1129 char* errinf_to_str_servfail(struct module_qstate* qstate);
1130 
1131 /**
1132  * Used during options parsing
1133  */
1134 struct config_parser_state {
1135 	/** name of file being parser */
1136 	char* filename;
1137 	/** line number in the file, starts at 1 */
1138 	int line;
1139 	/** number of errors encountered */
1140 	int errors;
1141 	/** the result of parsing is stored here. */
1142 	struct config_file* cfg;
1143 	/** the current chroot dir (or NULL if none) */
1144 	const char* chroot;
1145 };
1146 
1147 /** global config parser object used during config parsing */
1148 extern struct config_parser_state* cfg_parser;
1149 /** init lex state */
1150 void init_cfg_parse(void);
1151 /** lex in file */
1152 extern FILE* ub_c_in;
1153 /** lex out file */
1154 extern FILE* ub_c_out;
1155 /** the yacc lex generated parse function */
1156 int ub_c_parse(void);
1157 /** the lexer function */
1158 int ub_c_lex(void);
1159 /** wrap function */
1160 int ub_c_wrap(void);
1161 /** parsing helpers: print error with file and line numbers */
1162 void ub_c_error(const char* msg);
1163 /** parsing helpers: print error with file and line numbers */
1164 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
1165 
1166 #ifdef UB_ON_WINDOWS
1167 /**
1168  * Obtain registry string (if it exists).
1169  * @param key: key string
1170  * @param name: name of value to fetch.
1171  * @return malloced string with the result or NULL if it did not
1172  * 	exist on an error (logged with log_err) was encountered.
1173  */
1174 char* w_lookup_reg_str(const char* key, const char* name);
1175 
1176 /** Modify directory in options for module file name */
1177 void w_config_adjust_directory(struct config_file* cfg);
1178 #endif /* UB_ON_WINDOWS */
1179 
1180 /** debug option for unit tests. */
1181 extern int fake_dsa, fake_sha1;
1182 
1183 #endif /* UTIL_CONFIG_FILE_H */
1184