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