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) */
82 /** private key file for dnstcp-ssl service (enabled if not NULL) */
83 char* ssl_service_key;
84 /** public key file for dnstcp-ssl service */
85 char* ssl_service_pem;
86 /** port on which to provide ssl service */
88 /** if outgoing tcp connections use SSL */
91 /** outgoing port range number of ports (per thread) */
92 int outgoing_num_ports;
93 /** number of outgoing tcp buffers per (per thread) */
94 size_t outgoing_num_tcp;
95 /** number of incoming tcp buffers per (per thread) */
96 size_t incoming_num_tcp;
97 /** allowed udp port numbers, array with 0 if not allowed */
98 int* outgoing_avail_ports;
100 /** EDNS buffer size to use */
101 size_t edns_buffer_size;
102 /** number of bytes buffer size for DNS messages */
103 size_t msg_buffer_size;
104 /** size of the message cache */
105 size_t msg_cache_size;
106 /** slabs in the message cache. */
107 size_t msg_cache_slabs;
108 /** number of queries every thread can service */
109 size_t num_queries_per_thread;
110 /** number of msec to wait before items can be jostled out */
112 /** size of the rrset cache */
113 size_t rrset_cache_size;
114 /** slabs in the rrset cache */
115 size_t rrset_cache_slabs;
116 /** host cache ttl in seconds */
118 /** number of slabs in the infra host cache */
119 size_t infra_cache_slabs;
120 /** max number of hosts in the infra cache */
121 size_t infra_cache_numhosts;
122 /** min value for infra cache rtt */
123 int infra_cache_min_rtt;
124 /** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
127 /** the target fetch policy for the iterator */
128 char* target_fetch_policy;
130 /** automatic interface for incoming messages. Uses ipv6 remapping,
131 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
133 /** SO_RCVBUF size to set on port 53 UDP socket */
135 /** SO_SNDBUF size to set on port 53 UDP socket */
137 /** SO_REUSEPORT requested on port 53 sockets */
139 /** IP_TRANSPARENT socket option requested on port 53 sockets */
142 /** number of interfaces to open. If 0 default all interfaces. */
144 /** interface description strings (IP addresses) */
147 /** number of outgoing interfaces to open.
148 * If 0 default all interfaces. */
150 /** outgoing interface description strings (IP addresses) */
153 /** the root hints */
154 struct config_strlist* root_hints;
155 /** the stub definitions, linked list */
156 struct config_stub* stubs;
157 /** the forward zone definitions, linked list */
158 struct config_stub* forwards;
159 /** list of donotquery addresses, linked list */
160 struct config_strlist* donotqueryaddrs;
161 /** list of access control entries, linked list */
162 struct config_str2list* acls;
163 /** use default localhost donotqueryaddr entries */
164 int donotquery_localhost;
166 /** harden against very small edns buffer sizes */
167 int harden_short_bufsize;
168 /** harden against very large query sizes */
169 int harden_large_queries;
170 /** harden against spoofed glue (out of zone data) */
172 /** harden against receiving no DNSSEC data for trust anchor */
173 int harden_dnssec_stripped;
174 /** harden against queries that fall under known nxdomain names */
175 int harden_below_nxdomain;
176 /** harden the referral path, query for NS,A,AAAA and validate */
177 int harden_referral_path;
178 /** harden against algorithm downgrade */
179 int harden_algo_downgrade;
180 /** use 0x20 bits in query as random ID bits */
181 int use_caps_bits_for_id;
182 /** 0x20 whitelist, domains that do not use capsforid */
183 struct config_strlist* caps_whitelist;
184 /** strip away these private addrs from answers, no DNS Rebinding */
185 struct config_strlist* private_address;
186 /** allow domain (and subdomains) to use private address space */
187 struct config_strlist* private_domain;
188 /** what threshold for unwanted action. */
189 size_t unwanted_threshold;
190 /** the number of seconds maximal TTL used for RRsets and messages */
192 /** the number of seconds minimum TTL used for RRsets and messages */
194 /** the number of seconds maximal negative TTL for SOA in auth */
195 int max_negative_ttl;
196 /** if prefetching of messages should be performed. */
198 /** if prefetching of DNSKEYs should be performed. */
201 /** chrootdir, if not "" or chroot will be done */
203 /** username to change to, if not "". */
205 /** working directory */
207 /** filename to log to. */
209 /** pidfile to write pid to. */
212 /** should log messages be sent to syslogd */
214 /** log timestamp in ascii UTC */
216 /** log queries with one line per query */
219 /** do not report identity (id.server, hostname.bind) */
221 /** do not report version (version.server, version.bind) */
223 /** identity, hostname is returned if "". */
225 /** version, package version returned if "". */
228 /** the module configuration string */
231 /** files with trusted DS and DNSKEYs in zonefile format, list */
232 struct config_strlist* trust_anchor_file_list;
233 /** list of trustanchor keys, linked list */
234 struct config_strlist* trust_anchor_list;
235 /** files with 5011 autotrust tracked keys */
236 struct config_strlist* auto_trust_anchor_file_list;
237 /** files with trusted DNSKEYs in named.conf format, list */
238 struct config_strlist* trusted_keys_file_list;
239 /** DLV anchor file */
240 char* dlv_anchor_file;
241 /** DLV anchor inline */
242 struct config_strlist* dlv_anchor_list;
243 /** insecure domain list */
244 struct config_strlist* domain_insecure;
246 /** if not 0, this value is the validation date for RRSIGs */
247 int32_t val_date_override;
248 /** the minimum for signature clock skew */
249 int32_t val_sig_skew_min;
250 /** the maximum for signature clock skew */
251 int32_t val_sig_skew_max;
252 /** this value sets the number of seconds before revalidating bogus */
254 /** should validator clean additional section for secure msgs */
255 int val_clean_additional;
256 /** log bogus messages by the validator */
258 /** squelch val_log_level to log - this is library goes to callback */
260 /** should validator allow bogus messages to go through */
261 int val_permissive_mode;
262 /** ignore the CD flag in incoming queries and refuse them bogus data */
264 /** nsec3 maximum iterations per key size, string */
265 char* val_nsec3_key_iterations;
266 /** autotrust add holddown time, in seconds */
267 unsigned int add_holddown;
268 /** autotrust del holddown time, in seconds */
269 unsigned int del_holddown;
270 /** autotrust keep_missing time, in seconds. 0 is forever. */
271 unsigned int keep_missing;
272 /** permit small holddown values, allowing 5011 rollover very fast */
273 int permit_small_holddown;
275 /** size of the key cache */
276 size_t key_cache_size;
277 /** slabs in the key cache. */
278 size_t key_cache_slabs;
279 /** size of the neg cache */
280 size_t neg_cache_size;
282 /** local zones config */
283 struct config_str2list* local_zones;
284 /** local zones nodefault list */
285 struct config_strlist* local_zones_nodefault;
286 /** local data RRs configured */
287 struct config_strlist* local_data;
288 /** unblock lan zones (reverse lookups for 10/8 and so on) */
289 int unblock_lan_zones;
291 /** remote control section. enable toggle. */
292 int remote_control_enable;
293 /** the interfaces the remote control should listen on */
294 struct config_strlist* control_ifs;
295 /** port number for the control port */
297 /** use certificates for remote control */
298 int remote_control_use_cert;
299 /** private key file for server */
300 char* server_key_file;
301 /** certificate file for server */
302 char* server_cert_file;
303 /** private key file for unbound-control */
304 char* control_key_file;
305 /** certificate file for unbound-control */
306 char* control_cert_file;
308 /** Python script file */
311 /** daemonize, i.e. fork into the background. */
314 /* minimal response when positive answer */
315 int minimal_responses;
317 /* RRSet roundrobin */
318 int rrset_roundrobin;
320 /* maximum UDP response size */
326 /* Synthetize all AAAA record despite the presence of an authoritative one */
329 /** true to enable dnstap support */
331 /** dnstap socket path */
332 char* dnstap_socket_path;
333 /** true to send "identity" via dnstap */
334 int dnstap_send_identity;
335 /** true to send "version" via dnstap */
336 int dnstap_send_version;
337 /** dnstap "identity", hostname is used if "". */
338 char* dnstap_identity;
339 /** dnstap "version", package version is used if "". */
340 char* dnstap_version;
342 /** true to log dnstap RESOLVER_QUERY message events */
343 int dnstap_log_resolver_query_messages;
344 /** true to log dnstap RESOLVER_RESPONSE message events */
345 int dnstap_log_resolver_response_messages;
346 /** true to log dnstap CLIENT_QUERY message events */
347 int dnstap_log_client_query_messages;
348 /** true to log dnstap CLIENT_RESPONSE message events */
349 int dnstap_log_client_response_messages;
350 /** true to log dnstap FORWARDER_QUERY message events */
351 int dnstap_log_forwarder_query_messages;
352 /** true to log dnstap FORWARDER_RESPONSE message events */
353 int dnstap_log_forwarder_response_messages;
355 /** ratelimit 0 is off, otherwise qps (unless overridden) */
357 /** number of slabs for ratelimit cache */
358 size_t ratelimit_slabs;
359 /** memory size in bytes for ratelimit cache */
360 size_t ratelimit_size;
361 /** ratelimits for domain (exact match) */
362 struct config_str2list* ratelimit_for_domain;
363 /** ratelimits below domain */
364 struct config_str2list* ratelimit_below_domain;
365 /** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
366 int ratelimit_factor;
367 /** minimise outgoing QNAME and hide original QTYPE if possible */
368 int qname_minimisation;
371 /** from cfg username, after daemonise setup performed */
372 extern uid_t cfg_uid;
373 /** from cfg username, after daemonise setup performed */
374 extern gid_t cfg_gid;
375 /** debug and enable small timeouts */
376 extern int autr_permit_small_holddown;
379 * Stub config options
383 struct config_stub* next;
384 /** domain name (in text) of the stub apex domain */
386 /** list of stub nameserver hosts (domain name) */
387 struct config_strlist* hosts;
388 /** list of stub nameserver addresses (IP address) */
389 struct config_strlist* addrs;
390 /** if stub-prime is set */
392 /** if forward-first is set (failover to without if fails) */
397 * List of strings for config options
399 struct config_strlist {
400 /** next item in list */
401 struct config_strlist* next;
402 /** config option string */
407 * List of two strings for config options
409 struct config_str2list {
410 /** next item in list */
411 struct config_str2list* next;
418 /** List head for strlist processing, used for append operation. */
419 struct config_strlist_head {
420 /** first in list of text items */
421 struct config_strlist* first;
422 /** last in list of text items */
423 struct config_strlist* last;
427 * Create config file structure. Filled with default values.
428 * @return: the new structure or NULL on memory error.
430 struct config_file* config_create(void);
433 * Create config file structure for library use. Filled with default values.
434 * @return: the new structure or NULL on memory error.
436 struct config_file* config_create_forlib(void);
439 * Read the config file from the specified filename.
440 * @param config: where options are stored into, must be freshly created.
441 * @param filename: name of configfile. If NULL nothing is done.
442 * @param chroot: if not NULL, the chroot dir currently in use (for include).
443 * @return: false on error. In that case errno is set, ENOENT means
446 int config_read(struct config_file* config, const char* filename,
450 * Destroy the config file structure.
451 * @param config: to delete.
453 void config_delete(struct config_file* config);
456 * Apply config to global constants; this routine is called in single thread.
457 * @param config: to apply. Side effect: global constants change.
459 void config_apply(struct config_file* config);
462 * Find username, sets cfg_uid and cfg_gid.
463 * @param config: the config structure.
465 void config_lookup_uid(struct config_file* config);
468 * Set the given keyword to the given value.
469 * @param config: where to store config
470 * @param option: option name, including the ':' character.
471 * @param value: value, this string is copied if needed, or parsed.
472 * The caller owns the value string.
473 * @return 0 on error (malloc or syntax error).
475 int config_set_option(struct config_file* config, const char* option,
479 * Call print routine for the given option.
480 * @param cfg: config.
481 * @param opt: option name without trailing :.
482 * This is different from config_set_option.
483 * @param func: print func, called as (str, arg) for every data element.
484 * @param arg: user argument for print func.
485 * @return false if the option name is not supported (syntax error).
487 int config_get_option(struct config_file* cfg, const char* opt,
488 void (*func)(char*,void*), void* arg);
491 * Get an option and return strlist
492 * @param cfg: config file
493 * @param opt: option name.
494 * @param list: list is returned here. malloced, caller must free it.
495 * @return 0=OK, 1=syntax error, 2=malloc failed.
497 int config_get_option_list(struct config_file* cfg, const char* opt,
498 struct config_strlist** list);
501 * Get an option and collate results into string
502 * @param cfg: config file
503 * @param opt: option name.
504 * @param str: string. malloced, caller must free it.
505 * @return 0=OK, 1=syntax error, 2=malloc failed.
507 int config_get_option_collate(struct config_file* cfg, const char* opt,
511 * function to print to a file, use as func with config_get_option.
512 * @param line: text to print. \n appended.
513 * @param arg: pass a FILE*, like stdout.
515 void config_print_func(char* line, void* arg);
518 * function to collate the text strings into a strlist_head.
519 * @param line: text to append.
520 * @param arg: pass a strlist_head structure. zeroed on start.
522 void config_collate_func(char* line, void* arg);
525 * take a strlist_head list and return a malloc string. separated with newline.
526 * @param list: strlist first to collate. zeroes return "".
527 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
529 char* config_collate_cat(struct config_strlist* list);
532 * Append text at end of list.
533 * @param list: list head. zeroed at start.
534 * @param item: new item. malloced by caller. if NULL the insertion fails.
535 * @return true on success.
537 int cfg_strlist_append(struct config_strlist_head* list, char* item);
540 * Insert string into strlist.
541 * @param head: pointer to strlist head variable.
542 * @param item: new item. malloced by caller. If NULL the insertion fails.
543 * @return: true on success.
545 int cfg_strlist_insert(struct config_strlist** head, char* item);
548 * Insert string into str2list.
549 * @param head: pointer to str2list head variable.
550 * @param item: new item. malloced by caller. If NULL the insertion fails.
551 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
552 * @return: true on success.
554 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
557 * Delete items in config string list.
560 void config_delstrlist(struct config_strlist* list);
563 * Delete items in config double string list.
566 void config_deldblstrlist(struct config_str2list* list);
569 * Delete items in config stub list.
572 void config_delstubs(struct config_stub* list);
575 * Convert 14digit to time value
576 * @param str: string of 14 digits
577 * @return time value or 0 for error.
579 time_t cfg_convert_timeval(const char* str);
582 * Count number of values in the string.
583 * format ::= (sp num)+ sp
585 * sp ::= (space|tab)*
588 * @return: 0 on parse error, or empty string, else
589 * number of integer values in the string.
591 int cfg_count_numbers(const char* str);
594 * Convert a 'nice' memory or file size into a bytecount
595 * From '100k' to 102400. and so on. Understands kKmMgG.
596 * k=1024, m=1024*1024, g=1024*1024*1024.
598 * @param res: result is stored here, size in bytes.
599 * @return: true if parsed correctly, or 0 on a parse error (and an error
602 int cfg_parse_memsize(const char* str, size_t* res);
605 * Parse local-zone directive into two strings and register it in the config.
606 * @param cfg: to put it in.
607 * @param val: argument strings to local-zone, "example.com nodefault".
608 * @return: false on failure
610 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
613 * Mark "number" or "low-high" as available or not in ports array.
614 * @param str: string in input
615 * @param allow: give true if this range is permitted.
616 * @param avail: the array from cfg.
617 * @param num: size of the array (65536).
618 * @return: true if parsed correctly, or 0 on a parse error (and an error
621 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
624 * Get a condensed list of ports returned. allocated.
625 * @param cfg: config file.
626 * @param avail: the available ports array is returned here.
627 * @return: number of ports in array or 0 on error.
629 int cfg_condense_ports(struct config_file* cfg, int** avail);
632 * Scan ports available
633 * @param avail: the array from cfg.
634 * @param num: size of the array (65536).
635 * @return the number of ports available for use.
637 int cfg_scan_ports(int* avail, int num);
640 * Convert a filename to full pathname in original filesys
641 * @param fname: the path name to convert.
642 * Must not be null or empty.
643 * @param cfg: config struct for chroot and chdir (if set).
644 * @param use_chdir: if false, only chroot is applied.
645 * @return pointer to malloced buffer which is: [chroot][chdir]fname
646 * or NULL on malloc failure.
648 char* fname_after_chroot(const char* fname, struct config_file* cfg,
652 * Convert a ptr shorthand into a full reverse-notation PTR record.
653 * @param str: input string, "IP name"
654 * @return: malloced string "reversed-ip-name PTR name"
656 char* cfg_ptr_reverse(char* str);
659 * Append text to the error info for validation.
660 * @param qstate: query state.
661 * @param str: copied into query region and appended.
662 * Failures to allocate are logged.
664 void errinf(struct module_qstate* qstate, const char* str);
667 * Append text to error info: from 1.2.3.4
668 * @param qstate: query state.
669 * @param origin: sock list with origin of trouble.
670 * Every element added.
671 * If NULL: nothing is added.
672 * if 0len element: 'from cache' is added.
674 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
677 * Append text to error info: for RRset name type class
678 * @param qstate: query state.
679 * @param rr: rrset_key.
681 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
684 * Append text to error info: str dname
685 * @param qstate: query state.
686 * @param str: explanation string
687 * @param dname: the dname.
689 void errinf_dname(struct module_qstate* qstate, const char* str,
693 * Create error info in string
694 * @param qstate: query state.
695 * @return string or NULL on malloc failure (already logged).
696 * This string is malloced and has to be freed by caller.
698 char* errinf_to_str(struct module_qstate* qstate);
701 * Used during options parsing
703 struct config_parser_state {
704 /** name of file being parser */
706 /** line number in the file, starts at 1 */
708 /** number of errors encountered */
710 /** the result of parsing is stored here. */
711 struct config_file* cfg;
712 /** the current chroot dir (or NULL if none) */
716 /** global config parser object used during config parsing */
717 extern struct config_parser_state* cfg_parser;
718 /** init lex state */
719 void init_cfg_parse(void);
721 extern FILE* ub_c_in;
723 extern FILE* ub_c_out;
724 /** the yacc lex generated parse function */
725 int ub_c_parse(void);
726 /** the lexer function */
730 /** parsing helpers: print error with file and line numbers */
731 void ub_c_error(const char* msg);
732 /** parsing helpers: print error with file and line numbers */
733 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
737 * Obtain registry string (if it exists).
738 * @param key: key string
739 * @param name: name of value to fetch.
740 * @return malloced string with the result or NULL if it did not
741 * exist on an error (logged with log_err) was encountered.
743 char* w_lookup_reg_str(const char* key, const char* name);
745 /** Modify directory in options for module file name */
746 void w_config_adjust_directory(struct config_file* cfg);
747 #endif /* UB_ON_WINDOWS */
749 #endif /* UTIL_CONFIG_FILE_H */