2 * util/config_file.h - reads and stores the config file for unbound.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains functions for the config file.
42 #ifndef UTIL_CONFIG_FILE_H
43 #define UTIL_CONFIG_FILE_H
45 struct config_strlist;
46 struct config_str2list;
49 struct ub_packed_rrset_key;
52 * The configuration options.
53 * Strings are malloced.
56 /** verbosity level as specified in the config file */
59 /** statistics interval (in seconds) */
61 /** if false, statistics values are reset after printing them */
63 /** if true, the statistics are kept in greater detail */
66 /** number of threads to create */
69 /** port on which queries are answered. */
71 /** do ip4 query support. */
73 /** do ip6 query support. */
75 /** do udp query support. */
77 /** do tcp query support. */
79 /** tcp upstream queries (no UDP upstream queries) */
81 /** maximum segment size of tcp socket which queries are answered */
83 /** maximum segment size of tcp socket for outgoing queries */
86 /** private key file for dnstcp-ssl service (enabled if not NULL) */
87 char* ssl_service_key;
88 /** public key file for dnstcp-ssl service */
89 char* ssl_service_pem;
90 /** port on which to provide ssl service */
92 /** if outgoing tcp connections use SSL */
95 /** outgoing port range number of ports (per thread) */
96 int outgoing_num_ports;
97 /** number of outgoing tcp buffers per (per thread) */
98 size_t outgoing_num_tcp;
99 /** number of incoming tcp buffers per (per thread) */
100 size_t incoming_num_tcp;
101 /** allowed udp port numbers, array with 0 if not allowed */
102 int* outgoing_avail_ports;
104 /** EDNS buffer size to use */
105 size_t edns_buffer_size;
106 /** number of bytes buffer size for DNS messages */
107 size_t msg_buffer_size;
108 /** size of the message cache */
109 size_t msg_cache_size;
110 /** slabs in the message cache. */
111 size_t msg_cache_slabs;
112 /** number of queries every thread can service */
113 size_t num_queries_per_thread;
114 /** number of msec to wait before items can be jostled out */
116 /** size of the rrset cache */
117 size_t rrset_cache_size;
118 /** slabs in the rrset cache */
119 size_t rrset_cache_slabs;
120 /** host cache ttl in seconds */
122 /** number of slabs in the infra host cache */
123 size_t infra_cache_slabs;
124 /** max number of hosts in the infra cache */
125 size_t infra_cache_numhosts;
126 /** min value for infra cache rtt */
127 int infra_cache_min_rtt;
128 /** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
131 /** the target fetch policy for the iterator */
132 char* target_fetch_policy;
134 /** automatic interface for incoming messages. Uses ipv6 remapping,
135 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
137 /** SO_RCVBUF size to set on port 53 UDP socket */
139 /** SO_SNDBUF size to set on port 53 UDP socket */
141 /** SO_REUSEPORT requested on port 53 sockets */
143 /** IP_TRANSPARENT socket option requested on port 53 sockets */
146 /** number of interfaces to open. If 0 default all interfaces. */
148 /** interface description strings (IP addresses) */
151 /** number of outgoing interfaces to open.
152 * If 0 default all interfaces. */
154 /** outgoing interface description strings (IP addresses) */
157 /** the root hints */
158 struct config_strlist* root_hints;
159 /** the stub definitions, linked list */
160 struct config_stub* stubs;
161 /** the forward zone definitions, linked list */
162 struct config_stub* forwards;
163 /** list of donotquery addresses, linked list */
164 struct config_strlist* donotqueryaddrs;
165 /** list of access control entries, linked list */
166 struct config_str2list* acls;
167 /** use default localhost donotqueryaddr entries */
168 int donotquery_localhost;
170 /** harden against very small edns buffer sizes */
171 int harden_short_bufsize;
172 /** harden against very large query sizes */
173 int harden_large_queries;
174 /** harden against spoofed glue (out of zone data) */
176 /** harden against receiving no DNSSEC data for trust anchor */
177 int harden_dnssec_stripped;
178 /** harden against queries that fall under known nxdomain names */
179 int harden_below_nxdomain;
180 /** harden the referral path, query for NS,A,AAAA and validate */
181 int harden_referral_path;
182 /** harden against algorithm downgrade */
183 int harden_algo_downgrade;
184 /** use 0x20 bits in query as random ID bits */
185 int use_caps_bits_for_id;
186 /** 0x20 whitelist, domains that do not use capsforid */
187 struct config_strlist* caps_whitelist;
188 /** strip away these private addrs from answers, no DNS Rebinding */
189 struct config_strlist* private_address;
190 /** allow domain (and subdomains) to use private address space */
191 struct config_strlist* private_domain;
192 /** what threshold for unwanted action. */
193 size_t unwanted_threshold;
194 /** the number of seconds maximal TTL used for RRsets and messages */
196 /** the number of seconds minimum TTL used for RRsets and messages */
198 /** the number of seconds maximal negative TTL for SOA in auth */
199 int max_negative_ttl;
200 /** if prefetching of messages should be performed. */
202 /** if prefetching of DNSKEYs should be performed. */
205 /** chrootdir, if not "" or chroot will be done */
207 /** username to change to, if not "". */
209 /** working directory */
211 /** filename to log to. */
213 /** pidfile to write pid to. */
216 /** should log messages be sent to syslogd */
218 /** log timestamp in ascii UTC */
220 /** log queries with one line per query */
223 /** do not report identity (id.server, hostname.bind) */
225 /** do not report version (version.server, version.bind) */
227 /** identity, hostname is returned if "". */
229 /** version, package version returned if "". */
232 /** the module configuration string */
235 /** files with trusted DS and DNSKEYs in zonefile format, list */
236 struct config_strlist* trust_anchor_file_list;
237 /** list of trustanchor keys, linked list */
238 struct config_strlist* trust_anchor_list;
239 /** files with 5011 autotrust tracked keys */
240 struct config_strlist* auto_trust_anchor_file_list;
241 /** files with trusted DNSKEYs in named.conf format, list */
242 struct config_strlist* trusted_keys_file_list;
243 /** DLV anchor file */
244 char* dlv_anchor_file;
245 /** DLV anchor inline */
246 struct config_strlist* dlv_anchor_list;
247 /** insecure domain list */
248 struct config_strlist* domain_insecure;
250 /** if not 0, this value is the validation date for RRSIGs */
251 int32_t val_date_override;
252 /** the minimum for signature clock skew */
253 int32_t val_sig_skew_min;
254 /** the maximum for signature clock skew */
255 int32_t val_sig_skew_max;
256 /** this value sets the number of seconds before revalidating bogus */
258 /** should validator clean additional section for secure msgs */
259 int val_clean_additional;
260 /** log bogus messages by the validator */
262 /** squelch val_log_level to log - this is library goes to callback */
264 /** should validator allow bogus messages to go through */
265 int val_permissive_mode;
266 /** ignore the CD flag in incoming queries and refuse them bogus data */
268 /** nsec3 maximum iterations per key size, string */
269 char* val_nsec3_key_iterations;
270 /** autotrust add holddown time, in seconds */
271 unsigned int add_holddown;
272 /** autotrust del holddown time, in seconds */
273 unsigned int del_holddown;
274 /** autotrust keep_missing time, in seconds. 0 is forever. */
275 unsigned int keep_missing;
276 /** permit small holddown values, allowing 5011 rollover very fast */
277 int permit_small_holddown;
279 /** size of the key cache */
280 size_t key_cache_size;
281 /** slabs in the key cache. */
282 size_t key_cache_slabs;
283 /** size of the neg cache */
284 size_t neg_cache_size;
286 /** local zones config */
287 struct config_str2list* local_zones;
288 /** local zones nodefault list */
289 struct config_strlist* local_zones_nodefault;
290 /** local data RRs configured */
291 struct config_strlist* local_data;
292 /** unblock lan zones (reverse lookups for AS112 zones) */
293 int unblock_lan_zones;
294 /** insecure lan zones (don't validate AS112 zones) */
295 int insecure_lan_zones;
297 /** remote control section. enable toggle. */
298 int remote_control_enable;
299 /** the interfaces the remote control should listen on */
300 struct config_strlist* control_ifs;
301 /** port number for the control port */
303 /** use certificates for remote control */
304 int remote_control_use_cert;
305 /** private key file for server */
306 char* server_key_file;
307 /** certificate file for server */
308 char* server_cert_file;
309 /** private key file for unbound-control */
310 char* control_key_file;
311 /** certificate file for unbound-control */
312 char* control_cert_file;
314 /** Python script file */
317 /** daemonize, i.e. fork into the background. */
320 /* minimal response when positive answer */
321 int minimal_responses;
323 /* RRSet roundrobin */
324 int rrset_roundrobin;
326 /* maximum UDP response size */
332 /* Synthetize all AAAA record despite the presence of an authoritative one */
335 /** true to enable dnstap support */
337 /** dnstap socket path */
338 char* dnstap_socket_path;
339 /** true to send "identity" via dnstap */
340 int dnstap_send_identity;
341 /** true to send "version" via dnstap */
342 int dnstap_send_version;
343 /** dnstap "identity", hostname is used if "". */
344 char* dnstap_identity;
345 /** dnstap "version", package version is used if "". */
346 char* dnstap_version;
348 /** true to log dnstap RESOLVER_QUERY message events */
349 int dnstap_log_resolver_query_messages;
350 /** true to log dnstap RESOLVER_RESPONSE message events */
351 int dnstap_log_resolver_response_messages;
352 /** true to log dnstap CLIENT_QUERY message events */
353 int dnstap_log_client_query_messages;
354 /** true to log dnstap CLIENT_RESPONSE message events */
355 int dnstap_log_client_response_messages;
356 /** true to log dnstap FORWARDER_QUERY message events */
357 int dnstap_log_forwarder_query_messages;
358 /** true to log dnstap FORWARDER_RESPONSE message events */
359 int dnstap_log_forwarder_response_messages;
361 /** ratelimit 0 is off, otherwise qps (unless overridden) */
363 /** number of slabs for ratelimit cache */
364 size_t ratelimit_slabs;
365 /** memory size in bytes for ratelimit cache */
366 size_t ratelimit_size;
367 /** ratelimits for domain (exact match) */
368 struct config_str2list* ratelimit_for_domain;
369 /** ratelimits below domain */
370 struct config_str2list* ratelimit_below_domain;
371 /** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
372 int ratelimit_factor;
373 /** minimise outgoing QNAME and hide original QTYPE if possible */
374 int qname_minimisation;
377 /** from cfg username, after daemonise setup performed */
378 extern uid_t cfg_uid;
379 /** from cfg username, after daemonise setup performed */
380 extern gid_t cfg_gid;
381 /** debug and enable small timeouts */
382 extern int autr_permit_small_holddown;
385 * Stub config options
389 struct config_stub* next;
390 /** domain name (in text) of the stub apex domain */
392 /** list of stub nameserver hosts (domain name) */
393 struct config_strlist* hosts;
394 /** list of stub nameserver addresses (IP address) */
395 struct config_strlist* addrs;
396 /** if stub-prime is set */
398 /** if forward-first is set (failover to without if fails) */
403 * List of strings for config options
405 struct config_strlist {
406 /** next item in list */
407 struct config_strlist* next;
408 /** config option string */
413 * List of two strings for config options
415 struct config_str2list {
416 /** next item in list */
417 struct config_str2list* next;
424 /** List head for strlist processing, used for append operation. */
425 struct config_strlist_head {
426 /** first in list of text items */
427 struct config_strlist* first;
428 /** last in list of text items */
429 struct config_strlist* last;
433 * Create config file structure. Filled with default values.
434 * @return: the new structure or NULL on memory error.
436 struct config_file* config_create(void);
439 * Create config file structure for library use. Filled with default values.
440 * @return: the new structure or NULL on memory error.
442 struct config_file* config_create_forlib(void);
445 * Read the config file from the specified filename.
446 * @param config: where options are stored into, must be freshly created.
447 * @param filename: name of configfile. If NULL nothing is done.
448 * @param chroot: if not NULL, the chroot dir currently in use (for include).
449 * @return: false on error. In that case errno is set, ENOENT means
452 int config_read(struct config_file* config, const char* filename,
456 * Destroy the config file structure.
457 * @param config: to delete.
459 void config_delete(struct config_file* config);
462 * Apply config to global constants; this routine is called in single thread.
463 * @param config: to apply. Side effect: global constants change.
465 void config_apply(struct config_file* config);
468 * Find username, sets cfg_uid and cfg_gid.
469 * @param config: the config structure.
471 void config_lookup_uid(struct config_file* config);
474 * Set the given keyword to the given value.
475 * @param config: where to store config
476 * @param option: option name, including the ':' character.
477 * @param value: value, this string is copied if needed, or parsed.
478 * The caller owns the value string.
479 * @return 0 on error (malloc or syntax error).
481 int config_set_option(struct config_file* config, const char* option,
485 * Call print routine for the given option.
486 * @param cfg: config.
487 * @param opt: option name without trailing :.
488 * This is different from config_set_option.
489 * @param func: print func, called as (str, arg) for every data element.
490 * @param arg: user argument for print func.
491 * @return false if the option name is not supported (syntax error).
493 int config_get_option(struct config_file* cfg, const char* opt,
494 void (*func)(char*,void*), void* arg);
497 * Get an option and return strlist
498 * @param cfg: config file
499 * @param opt: option name.
500 * @param list: list is returned here. malloced, caller must free it.
501 * @return 0=OK, 1=syntax error, 2=malloc failed.
503 int config_get_option_list(struct config_file* cfg, const char* opt,
504 struct config_strlist** list);
507 * Get an option and collate results into string
508 * @param cfg: config file
509 * @param opt: option name.
510 * @param str: string. malloced, caller must free it.
511 * @return 0=OK, 1=syntax error, 2=malloc failed.
513 int config_get_option_collate(struct config_file* cfg, const char* opt,
517 * function to print to a file, use as func with config_get_option.
518 * @param line: text to print. \n appended.
519 * @param arg: pass a FILE*, like stdout.
521 void config_print_func(char* line, void* arg);
524 * function to collate the text strings into a strlist_head.
525 * @param line: text to append.
526 * @param arg: pass a strlist_head structure. zeroed on start.
528 void config_collate_func(char* line, void* arg);
531 * take a strlist_head list and return a malloc string. separated with newline.
532 * @param list: strlist first to collate. zeroes return "".
533 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
535 char* config_collate_cat(struct config_strlist* list);
538 * Append text at end of list.
539 * @param list: list head. zeroed at start.
540 * @param item: new item. malloced by caller. if NULL the insertion fails.
541 * @return true on success.
543 int cfg_strlist_append(struct config_strlist_head* list, char* item);
546 * Insert string into strlist.
547 * @param head: pointer to strlist head variable.
548 * @param item: new item. malloced by caller. If NULL the insertion fails.
549 * @return: true on success.
551 int cfg_strlist_insert(struct config_strlist** head, char* item);
554 * Insert string into str2list.
555 * @param head: pointer to str2list head variable.
556 * @param item: new item. malloced by caller. If NULL the insertion fails.
557 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
558 * @return: true on success.
560 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
563 * Find stub in config list, also returns prevptr (for deletion).
564 * @param pp: call routine with pointer to a pointer to the start of the list,
565 * if the stub is found, on exit, the value contains a pointer to the
566 * next pointer that points to the found element (or to the list start
567 * pointer if it is the first element).
568 * @param nm: name of stub to find.
569 * @return: pointer to config_stub if found, or NULL if not found.
571 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm);
574 * Delete items in config string list.
577 void config_delstrlist(struct config_strlist* list);
580 * Delete items in config double string list.
583 void config_deldblstrlist(struct config_str2list* list);
587 * @param p: stub item
589 void config_delstub(struct config_stub* p);
592 * Delete items in config stub list.
595 void config_delstubs(struct config_stub* list);
598 * Convert 14digit to time value
599 * @param str: string of 14 digits
600 * @return time value or 0 for error.
602 time_t cfg_convert_timeval(const char* str);
605 * Count number of values in the string.
606 * format ::= (sp num)+ sp
608 * sp ::= (space|tab)*
611 * @return: 0 on parse error, or empty string, else
612 * number of integer values in the string.
614 int cfg_count_numbers(const char* str);
617 * Convert a 'nice' memory or file size into a bytecount
618 * From '100k' to 102400. and so on. Understands kKmMgG.
619 * k=1024, m=1024*1024, g=1024*1024*1024.
621 * @param res: result is stored here, size in bytes.
622 * @return: true if parsed correctly, or 0 on a parse error (and an error
625 int cfg_parse_memsize(const char* str, size_t* res);
628 * Parse local-zone directive into two strings and register it in the config.
629 * @param cfg: to put it in.
630 * @param val: argument strings to local-zone, "example.com nodefault".
631 * @return: false on failure
633 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
636 * Mark "number" or "low-high" as available or not in ports array.
637 * @param str: string in input
638 * @param allow: give true if this range is permitted.
639 * @param avail: the array from cfg.
640 * @param num: size of the array (65536).
641 * @return: true if parsed correctly, or 0 on a parse error (and an error
644 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
647 * Get a condensed list of ports returned. allocated.
648 * @param cfg: config file.
649 * @param avail: the available ports array is returned here.
650 * @return: number of ports in array or 0 on error.
652 int cfg_condense_ports(struct config_file* cfg, int** avail);
655 * Scan ports available
656 * @param avail: the array from cfg.
657 * @param num: size of the array (65536).
658 * @return the number of ports available for use.
660 int cfg_scan_ports(int* avail, int num);
663 * Convert a filename to full pathname in original filesys
664 * @param fname: the path name to convert.
665 * Must not be null or empty.
666 * @param cfg: config struct for chroot and chdir (if set).
667 * @param use_chdir: if false, only chroot is applied.
668 * @return pointer to malloced buffer which is: [chroot][chdir]fname
669 * or NULL on malloc failure.
671 char* fname_after_chroot(const char* fname, struct config_file* cfg,
675 * Convert a ptr shorthand into a full reverse-notation PTR record.
676 * @param str: input string, "IP name"
677 * @return: malloced string "reversed-ip-name PTR name"
679 char* cfg_ptr_reverse(char* str);
682 * Append text to the error info for validation.
683 * @param qstate: query state.
684 * @param str: copied into query region and appended.
685 * Failures to allocate are logged.
687 void errinf(struct module_qstate* qstate, const char* str);
690 * Append text to error info: from 1.2.3.4
691 * @param qstate: query state.
692 * @param origin: sock list with origin of trouble.
693 * Every element added.
694 * If NULL: nothing is added.
695 * if 0len element: 'from cache' is added.
697 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
700 * Append text to error info: for RRset name type class
701 * @param qstate: query state.
702 * @param rr: rrset_key.
704 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
707 * Append text to error info: str dname
708 * @param qstate: query state.
709 * @param str: explanation string
710 * @param dname: the dname.
712 void errinf_dname(struct module_qstate* qstate, const char* str,
716 * Create error info in string
717 * @param qstate: query state.
718 * @return string or NULL on malloc failure (already logged).
719 * This string is malloced and has to be freed by caller.
721 char* errinf_to_str(struct module_qstate* qstate);
724 * Used during options parsing
726 struct config_parser_state {
727 /** name of file being parser */
729 /** line number in the file, starts at 1 */
731 /** number of errors encountered */
733 /** the result of parsing is stored here. */
734 struct config_file* cfg;
735 /** the current chroot dir (or NULL if none) */
739 /** global config parser object used during config parsing */
740 extern struct config_parser_state* cfg_parser;
741 /** init lex state */
742 void init_cfg_parse(void);
744 extern FILE* ub_c_in;
746 extern FILE* ub_c_out;
747 /** the yacc lex generated parse function */
748 int ub_c_parse(void);
749 /** the lexer function */
753 /** parsing helpers: print error with file and line numbers */
754 void ub_c_error(const char* msg);
755 /** parsing helpers: print error with file and line numbers */
756 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
760 * Obtain registry string (if it exists).
761 * @param key: key string
762 * @param name: name of value to fetch.
763 * @return malloced string with the result or NULL if it did not
764 * exist on an error (logged with log_err) was encountered.
766 char* w_lookup_reg_str(const char* key, const char* name);
768 /** Modify directory in options for module file name */
769 void w_config_adjust_directory(struct config_file* cfg);
770 #endif /* UB_ON_WINDOWS */
772 #endif /* UTIL_CONFIG_FILE_H */