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 /** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
125 /** the target fetch policy for the iterator */
126 char* target_fetch_policy;
128 /** automatic interface for incoming messages. Uses ipv6 remapping,
129 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
131 /** SO_RCVBUF size to set on port 53 UDP socket */
133 /** SO_SNDBUF size to set on port 53 UDP socket */
135 /** SO_REUSEPORT requested on port 53 sockets */
138 /** number of interfaces to open. If 0 default all interfaces. */
140 /** interface description strings (IP addresses) */
143 /** number of outgoing interfaces to open.
144 * If 0 default all interfaces. */
146 /** outgoing interface description strings (IP addresses) */
149 /** the root hints */
150 struct config_strlist* root_hints;
151 /** the stub definitions, linked list */
152 struct config_stub* stubs;
153 /** the forward zone definitions, linked list */
154 struct config_stub* forwards;
155 /** list of donotquery addresses, linked list */
156 struct config_strlist* donotqueryaddrs;
157 /** list of access control entries, linked list */
158 struct config_str2list* acls;
159 /** use default localhost donotqueryaddr entries */
160 int donotquery_localhost;
162 /** harden against very small edns buffer sizes */
163 int harden_short_bufsize;
164 /** harden against very large query sizes */
165 int harden_large_queries;
166 /** harden against spoofed glue (out of zone data) */
168 /** harden against receiving no DNSSEC data for trust anchor */
169 int harden_dnssec_stripped;
170 /** harden against queries that fall under known nxdomain names */
171 int harden_below_nxdomain;
172 /** harden the referral path, query for NS,A,AAAA and validate */
173 int harden_referral_path;
174 /** use 0x20 bits in query as random ID bits */
175 int use_caps_bits_for_id;
176 /** strip away these private addrs from answers, no DNS Rebinding */
177 struct config_strlist* private_address;
178 /** allow domain (and subdomains) to use private address space */
179 struct config_strlist* private_domain;
180 /** what threshold for unwanted action. */
181 size_t unwanted_threshold;
182 /** the number of seconds maximal TTL used for RRsets and messages */
184 /** the number of seconds minimum TTL used for RRsets and messages */
186 /** if prefetching of messages should be performed. */
188 /** if prefetching of DNSKEYs should be performed. */
191 /** chrootdir, if not "" or chroot will be done */
193 /** username to change to, if not "". */
197 /** working directory */
199 /** filename to log to. */
201 /** pidfile to write pid to. */
204 /** should log messages be sent to syslogd */
206 /** log timestamp in ascii UTC */
208 /** log queries with one line per query */
211 /** do not report identity (id.server, hostname.bind) */
213 /** do not report version (version.server, version.bind) */
215 /** identity, hostname is returned if "". */
217 /** version, package version returned if "". */
220 /** the module configuration string */
223 /** files with trusted DS and DNSKEYs in zonefile format, list */
224 struct config_strlist* trust_anchor_file_list;
225 /** list of trustanchor keys, linked list */
226 struct config_strlist* trust_anchor_list;
227 /** files with 5011 autotrust tracked keys */
228 struct config_strlist* auto_trust_anchor_file_list;
229 /** files with trusted DNSKEYs in named.conf format, list */
230 struct config_strlist* trusted_keys_file_list;
231 /** DLV anchor file */
232 char* dlv_anchor_file;
233 /** DLV anchor inline */
234 struct config_strlist* dlv_anchor_list;
235 /** insecure domain list */
236 struct config_strlist* domain_insecure;
238 /** if not 0, this value is the validation date for RRSIGs */
239 int32_t val_date_override;
240 /** the minimum for signature clock skew */
241 int32_t val_sig_skew_min;
242 /** the maximum for signature clock skew */
243 int32_t val_sig_skew_max;
244 /** this value sets the number of seconds before revalidating bogus */
246 /** should validator clean additional section for secure msgs */
247 int val_clean_additional;
248 /** log bogus messages by the validator */
250 /** squelch val_log_level to log - this is library goes to callback */
252 /** should validator allow bogus messages to go through */
253 int val_permissive_mode;
254 /** ignore the CD flag in incoming queries and refuse them bogus data */
256 /** nsec3 maximum iterations per key size, string */
257 char* val_nsec3_key_iterations;
258 /** autotrust add holddown time, in seconds */
259 unsigned int add_holddown;
260 /** autotrust del holddown time, in seconds */
261 unsigned int del_holddown;
262 /** autotrust keep_missing time, in seconds. 0 is forever. */
263 unsigned int keep_missing;
265 /** size of the key cache */
266 size_t key_cache_size;
267 /** slabs in the key cache. */
268 size_t key_cache_slabs;
269 /** size of the neg cache */
270 size_t neg_cache_size;
272 /** local zones config */
273 struct config_str2list* local_zones;
274 /** local zones nodefault list */
275 struct config_strlist* local_zones_nodefault;
276 /** local data RRs configged */
277 struct config_strlist* local_data;
278 /** unblock lan zones (reverse lookups for 10/8 and so on) */
279 int unblock_lan_zones;
281 /** remote control section. enable toggle. */
282 int remote_control_enable;
283 /** the interfaces the remote control should listen on */
284 struct config_strlist* control_ifs;
285 /** port number for the control port */
287 /** use certificates for remote control */
288 int remote_control_use_cert;
289 /** private key file for server */
290 char* server_key_file;
291 /** certificate file for server */
292 char* server_cert_file;
293 /** private key file for unbound-control */
294 char* control_key_file;
295 /** certificate file for unbound-control */
296 char* control_cert_file;
298 /** Python script file */
301 /** daemonize, i.e. fork into the background. */
304 /* minimal response when positive answer */
305 int minimal_responses;
307 /* RRSet roundrobin */
308 int rrset_roundrobin;
310 /* maximum UDP response size */
316 /* Synthetize all AAAA record despite the presence of an authoritative one */
319 /** true to enable dnstap support */
321 /** dnstap socket path */
322 char* dnstap_socket_path;
323 /** true to send "identity" via dnstap */
324 int dnstap_send_identity;
325 /** true to send "version" via dnstap */
326 int dnstap_send_version;
327 /** dnstap "identity", hostname is used if "". */
328 char* dnstap_identity;
329 /** dnstap "version", package version is used if "". */
330 char* dnstap_version;
332 /** true to log dnstap RESOLVER_QUERY message events */
333 int dnstap_log_resolver_query_messages;
334 /** true to log dnstap RESOLVER_RESPONSE message events */
335 int dnstap_log_resolver_response_messages;
336 /** true to log dnstap CLIENT_QUERY message events */
337 int dnstap_log_client_query_messages;
338 /** true to log dnstap CLIENT_RESPONSE message events */
339 int dnstap_log_client_response_messages;
340 /** true to log dnstap FORWARDER_QUERY message events */
341 int dnstap_log_forwarder_query_messages;
342 /** true to log dnstap FORWARDER_RESPONSE message events */
343 int dnstap_log_forwarder_response_messages;
347 * Stub config options
351 struct config_stub* next;
352 /** domain name (in text) of the stub apex domain */
354 /** list of stub nameserver hosts (domain name) */
355 struct config_strlist* hosts;
356 /** list of stub nameserver addresses (IP address) */
357 struct config_strlist* addrs;
358 /** if stub-prime is set */
360 /** if forward-first is set (failover to without if fails) */
365 * List of strings for config options
367 struct config_strlist {
368 /** next item in list */
369 struct config_strlist* next;
370 /** config option string */
375 * List of two strings for config options
377 struct config_str2list {
378 /** next item in list */
379 struct config_str2list* next;
386 /** List head for strlist processing, used for append operation. */
387 struct config_strlist_head {
388 /** first in list of text items */
389 struct config_strlist* first;
390 /** last in list of text items */
391 struct config_strlist* last;
395 * Create config file structure. Filled with default values.
396 * @return: the new structure or NULL on memory error.
398 struct config_file* config_create(void);
401 * Create config file structure for library use. Filled with default values.
402 * @return: the new structure or NULL on memory error.
404 struct config_file* config_create_forlib(void);
407 * Read the config file from the specified filename.
408 * @param config: where options are stored into, must be freshly created.
409 * @param filename: name of configfile. If NULL nothing is done.
410 * @param chroot: if not NULL, the chroot dir currently in use (for include).
411 * @return: false on error. In that case errno is set, ENOENT means
414 int config_read(struct config_file* config, const char* filename,
418 * Destroy the config file structure.
419 * @param config: to delete.
421 void config_delete(struct config_file* config);
424 * Apply config to global constants; this routine is called in single thread.
425 * @param config: to apply. Side effect: global constants change.
427 void config_apply(struct config_file* config);
430 * Find username, sets uid and gid.
431 * @param config: the config structure.
433 void config_lookup_uid(struct config_file* config);
436 * Set the given keyword to the given value.
437 * @param config: where to store config
438 * @param option: option name, including the ':' character.
439 * @param value: value, this string is copied if needed, or parsed.
440 * The caller owns the value string.
441 * @return 0 on error (malloc or syntax error).
443 int config_set_option(struct config_file* config, const char* option,
447 * Call print routine for the given option.
448 * @param cfg: config.
449 * @param opt: option name without trailing :.
450 * This is different from config_set_option.
451 * @param func: print func, called as (str, arg) for every data element.
452 * @param arg: user argument for print func.
453 * @return false if the option name is not supported (syntax error).
455 int config_get_option(struct config_file* cfg, const char* opt,
456 void (*func)(char*,void*), void* arg);
459 * Get an option and return strlist
460 * @param cfg: config file
461 * @param opt: option name.
462 * @param list: list is returned here. malloced, caller must free it.
463 * @return 0=OK, 1=syntax error, 2=malloc failed.
465 int config_get_option_list(struct config_file* cfg, const char* opt,
466 struct config_strlist** list);
469 * Get an option and collate results into string
470 * @param cfg: config file
471 * @param opt: option name.
472 * @param str: string. malloced, caller must free it.
473 * @return 0=OK, 1=syntax error, 2=malloc failed.
475 int config_get_option_collate(struct config_file* cfg, const char* opt,
479 * function to print to a file, use as func with config_get_option.
480 * @param line: text to print. \n appended.
481 * @param arg: pass a FILE*, like stdout.
483 void config_print_func(char* line, void* arg);
486 * function to collate the text strings into a strlist_head.
487 * @param line: text to append.
488 * @param arg: pass a strlist_head structure. zeroed on start.
490 void config_collate_func(char* line, void* arg);
493 * take a strlist_head list and return a malloc string. separated with newline.
494 * @param list: strlist first to collate. zeroes return "".
495 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
497 char* config_collate_cat(struct config_strlist* list);
500 * Append text at end of list.
501 * @param list: list head. zeroed at start.
502 * @param item: new item. malloced by caller. if NULL the insertion fails.
503 * @return true on success.
505 int cfg_strlist_append(struct config_strlist_head* list, char* item);
508 * Insert string into strlist.
509 * @param head: pointer to strlist head variable.
510 * @param item: new item. malloced by caller. If NULL the insertion fails.
511 * @return: true on success.
513 int cfg_strlist_insert(struct config_strlist** head, char* item);
516 * Insert string into str2list.
517 * @param head: pointer to str2list head variable.
518 * @param item: new item. malloced by caller. If NULL the insertion fails.
519 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
520 * @return: true on success.
522 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
525 * Delete items in config string list.
528 void config_delstrlist(struct config_strlist* list);
531 * Delete items in config double string list.
534 void config_deldblstrlist(struct config_str2list* list);
537 * Delete items in config stub list.
540 void config_delstubs(struct config_stub* list);
543 * Convert 14digit to time value
544 * @param str: string of 14 digits
545 * @return time value or 0 for error.
547 time_t cfg_convert_timeval(const char* str);
550 * Count number of values in the string.
551 * format ::= (sp num)+ sp
553 * sp ::= (space|tab)*
556 * @return: 0 on parse error, or empty string, else
557 * number of integer values in the string.
559 int cfg_count_numbers(const char* str);
562 * Convert a 'nice' memory or file size into a bytecount
563 * From '100k' to 102400. and so on. Understands kKmMgG.
564 * k=1024, m=1024*1024, g=1024*1024*1024.
566 * @param res: result is stored here, size in bytes.
567 * @return: true if parsed correctly, or 0 on a parse error (and an error
570 int cfg_parse_memsize(const char* str, size_t* res);
573 * Parse local-zone directive into two strings and register it in the config.
574 * @param cfg: to put it in.
575 * @param val: argument strings to local-zone, "example.com nodefault".
576 * @return: false on failure
578 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
581 * Mark "number" or "low-high" as available or not in ports array.
582 * @param str: string in input
583 * @param allow: give true if this range is permitted.
584 * @param avail: the array from cfg.
585 * @param num: size of the array (65536).
586 * @return: true if parsed correctly, or 0 on a parse error (and an error
589 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
592 * Get a condensed list of ports returned. allocated.
593 * @param cfg: config file.
594 * @param avail: the available ports array is returned here.
595 * @return: number of ports in array or 0 on error.
597 int cfg_condense_ports(struct config_file* cfg, int** avail);
600 * Scan ports available
601 * @param avail: the array from cfg.
602 * @param num: size of the array (65536).
603 * @return the number of ports available for use.
605 int cfg_scan_ports(int* avail, int num);
608 * Convert a filename to full pathname in original filesys
609 * @param fname: the path name to convert.
610 * Must not be null or empty.
611 * @param cfg: config struct for chroot and chdir (if set).
612 * @param use_chdir: if false, only chroot is applied.
613 * @return pointer to malloced buffer which is: [chroot][chdir]fname
614 * or NULL on malloc failure.
616 char* fname_after_chroot(const char* fname, struct config_file* cfg,
620 * Convert a ptr shorthand into a full reverse-notation PTR record.
621 * @param str: input string, "IP name"
622 * @return: malloced string "reversed-ip-name PTR name"
624 char* cfg_ptr_reverse(char* str);
627 * Append text to the error info for validation.
628 * @param qstate: query state.
629 * @param str: copied into query region and appended.
630 * Failures to allocate are logged.
632 void errinf(struct module_qstate* qstate, const char* str);
635 * Append text to error info: from 1.2.3.4
636 * @param qstate: query state.
637 * @param origin: sock list with origin of trouble.
638 * Every element added.
639 * If NULL: nothing is added.
640 * if 0len element: 'from cache' is added.
642 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
645 * Append text to error info: for RRset name type class
646 * @param qstate: query state.
647 * @param rr: rrset_key.
649 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
652 * Append text to error info: str dname
653 * @param qstate: query state.
654 * @param str: explanation string
655 * @param dname: the dname.
657 void errinf_dname(struct module_qstate* qstate, const char* str,
661 * Create error info in string
662 * @param qstate: query state.
663 * @return string or NULL on malloc failure (already logged).
664 * This string is malloced and has to be freed by caller.
666 char* errinf_to_str(struct module_qstate* qstate);
669 * Used during options parsing
671 struct config_parser_state {
672 /** name of file being parser */
674 /** line number in the file, starts at 1 */
676 /** number of errors encountered */
678 /** the result of parsing is stored here. */
679 struct config_file* cfg;
680 /** the current chroot dir (or NULL if none) */
684 /** global config parser object used during config parsing */
685 extern struct config_parser_state* cfg_parser;
686 /** init lex state */
687 void init_cfg_parse(void);
689 extern FILE* ub_c_in;
691 extern FILE* ub_c_out;
692 /** the yacc lex generated parse function */
693 int ub_c_parse(void);
694 /** the lexer function */
698 /** parsing helpers: print error with file and line numbers */
699 void ub_c_error(const char* msg);
700 /** parsing helpers: print error with file and line numbers */
701 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
705 * Obtain registry string (if it exists).
706 * @param key: key string
707 * @param name: name of value to fetch.
708 * @return malloced string with the result or NULL if it did not
709 * exist on an error (logged with log_err) was encountered.
711 char* w_lookup_reg_str(const char* key, const char* name);
712 #endif /* UB_ON_WINDOWS */
714 #endif /* UTIL_CONFIG_FILE_H */