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