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