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 "". */
195 /** working directory */
197 /** filename to log to. */
199 /** pidfile to write pid to. */
202 /** should log messages be sent to syslogd */
204 /** log timestamp in ascii UTC */
206 /** log queries with one line per query */
209 /** do not report identity (id.server, hostname.bind) */
211 /** do not report version (version.server, version.bind) */
213 /** identity, hostname is returned if "". */
215 /** version, package version returned if "". */
218 /** the module configuration string */
221 /** files with trusted DS and DNSKEYs in zonefile format, list */
222 struct config_strlist* trust_anchor_file_list;
223 /** list of trustanchor keys, linked list */
224 struct config_strlist* trust_anchor_list;
225 /** files with 5011 autotrust tracked keys */
226 struct config_strlist* auto_trust_anchor_file_list;
227 /** files with trusted DNSKEYs in named.conf format, list */
228 struct config_strlist* trusted_keys_file_list;
229 /** DLV anchor file */
230 char* dlv_anchor_file;
231 /** DLV anchor inline */
232 struct config_strlist* dlv_anchor_list;
233 /** insecure domain list */
234 struct config_strlist* domain_insecure;
236 /** if not 0, this value is the validation date for RRSIGs */
237 int32_t val_date_override;
238 /** the minimum for signature clock skew */
239 int32_t val_sig_skew_min;
240 /** the maximum for signature clock skew */
241 int32_t val_sig_skew_max;
242 /** this value sets the number of seconds before revalidating bogus */
244 /** should validator clean additional section for secure msgs */
245 int val_clean_additional;
246 /** log bogus messages by the validator */
248 /** squelch val_log_level to log - this is library goes to callback */
250 /** should validator allow bogus messages to go through */
251 int val_permissive_mode;
252 /** ignore the CD flag in incoming queries and refuse them bogus data */
254 /** nsec3 maximum iterations per key size, string */
255 char* val_nsec3_key_iterations;
256 /** autotrust add holddown time, in seconds */
257 unsigned int add_holddown;
258 /** autotrust del holddown time, in seconds */
259 unsigned int del_holddown;
260 /** autotrust keep_missing time, in seconds. 0 is forever. */
261 unsigned int keep_missing;
263 /** size of the key cache */
264 size_t key_cache_size;
265 /** slabs in the key cache. */
266 size_t key_cache_slabs;
267 /** size of the neg cache */
268 size_t neg_cache_size;
270 /** local zones config */
271 struct config_str2list* local_zones;
272 /** local zones nodefault list */
273 struct config_strlist* local_zones_nodefault;
274 /** local data RRs configged */
275 struct config_strlist* local_data;
277 /** remote control section. enable toggle. */
278 int remote_control_enable;
279 /** the interfaces the remote control should listen on */
280 struct config_strlist* control_ifs;
281 /** port number for the control port */
283 /** private key file for server */
284 char* server_key_file;
285 /** certificate file for server */
286 char* server_cert_file;
287 /** private key file for unbound-control */
288 char* control_key_file;
289 /** certificate file for unbound-control */
290 char* control_cert_file;
292 /** Python script file */
295 /** daemonize, i.e. fork into the background. */
298 /* minimal response when positive answer */
299 int minimal_responses;
301 /* RRSet roundrobin */
302 int rrset_roundrobin;
304 /* maximum UDP response size */
309 * Stub config options
313 struct config_stub* next;
314 /** domain name (in text) of the stub apex domain */
316 /** list of stub nameserver hosts (domain name) */
317 struct config_strlist* hosts;
318 /** list of stub nameserver addresses (IP address) */
319 struct config_strlist* addrs;
320 /** if stub-prime is set */
322 /** if forward-first is set (failover to without if fails) */
327 * List of strings for config options
329 struct config_strlist {
330 /** next item in list */
331 struct config_strlist* next;
332 /** config option string */
337 * List of two strings for config options
339 struct config_str2list {
340 /** next item in list */
341 struct config_str2list* next;
348 /** List head for strlist processing, used for append operation. */
349 struct config_strlist_head {
350 /** first in list of text items */
351 struct config_strlist* first;
352 /** last in list of text items */
353 struct config_strlist* last;
357 * Create config file structure. Filled with default values.
358 * @return: the new structure or NULL on memory error.
360 struct config_file* config_create(void);
363 * Create config file structure for library use. Filled with default values.
364 * @return: the new structure or NULL on memory error.
366 struct config_file* config_create_forlib(void);
369 * Read the config file from the specified filename.
370 * @param config: where options are stored into, must be freshly created.
371 * @param filename: name of configfile. If NULL nothing is done.
372 * @param chroot: if not NULL, the chroot dir currently in use (for include).
373 * @return: false on error. In that case errno is set, ENOENT means
376 int config_read(struct config_file* config, const char* filename,
380 * Destroy the config file structure.
381 * @param config: to delete.
383 void config_delete(struct config_file* config);
386 * Apply config to global constants; this routine is called in single thread.
387 * @param config: to apply. Side effect: global constants change.
389 void config_apply(struct config_file* config);
392 * Set the given keyword to the given value.
393 * @param config: where to store config
394 * @param option: option name, including the ':' character.
395 * @param value: value, this string is copied if needed, or parsed.
396 * The caller owns the value string.
397 * @return 0 on error (malloc or syntax error).
399 int config_set_option(struct config_file* config, const char* option,
403 * Call print routine for the given option.
404 * @param cfg: config.
405 * @param opt: option name without trailing :.
406 * This is different from config_set_option.
407 * @param func: print func, called as (str, arg) for every data element.
408 * @param arg: user argument for print func.
409 * @return false if the option name is not supported (syntax error).
411 int config_get_option(struct config_file* cfg, const char* opt,
412 void (*func)(char*,void*), void* arg);
415 * Get an option and return strlist
416 * @param cfg: config file
417 * @param opt: option name.
418 * @param list: list is returned here. malloced, caller must free it.
419 * @return 0=OK, 1=syntax error, 2=malloc failed.
421 int config_get_option_list(struct config_file* cfg, const char* opt,
422 struct config_strlist** list);
425 * Get an option and collate results into string
426 * @param cfg: config file
427 * @param opt: option name.
428 * @param str: string. malloced, caller must free it.
429 * @return 0=OK, 1=syntax error, 2=malloc failed.
431 int config_get_option_collate(struct config_file* cfg, const char* opt,
435 * function to print to a file, use as func with config_get_option.
436 * @param line: text to print. \n appended.
437 * @param arg: pass a FILE*, like stdout.
439 void config_print_func(char* line, void* arg);
442 * function to collate the text strings into a strlist_head.
443 * @param line: text to append.
444 * @param arg: pass a strlist_head structure. zeroed on start.
446 void config_collate_func(char* line, void* arg);
449 * take a strlist_head list and return a malloc string. separated with newline.
450 * @param list: strlist first to collate. zeroes return "".
451 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
453 char* config_collate_cat(struct config_strlist* list);
456 * Append text at end of list.
457 * @param list: list head. zeroed at start.
458 * @param item: new item. malloced by caller. if NULL the insertion fails.
459 * @return true on success.
461 int cfg_strlist_append(struct config_strlist_head* list, char* item);
464 * Insert string into strlist.
465 * @param head: pointer to strlist head variable.
466 * @param item: new item. malloced by caller. If NULL the insertion fails.
467 * @return: true on success.
469 int cfg_strlist_insert(struct config_strlist** head, char* item);
472 * Insert string into str2list.
473 * @param head: pointer to str2list head variable.
474 * @param item: new item. malloced by caller. If NULL the insertion fails.
475 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
476 * @return: true on success.
478 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
481 * Delete items in config string list.
484 void config_delstrlist(struct config_strlist* list);
487 * Delete items in config double string list.
490 void config_deldblstrlist(struct config_str2list* list);
493 * Delete items in config stub list.
496 void config_delstubs(struct config_stub* list);
499 * Convert 14digit to time value
500 * @param str: string of 14 digits
501 * @return time value or 0 for error.
503 time_t cfg_convert_timeval(const char* str);
506 * Count number of values in the string.
507 * format ::= (sp num)+ sp
509 * sp ::= (space|tab)*
512 * @return: 0 on parse error, or empty string, else
513 * number of integer values in the string.
515 int cfg_count_numbers(const char* str);
518 * Convert a 'nice' memory or file size into a bytecount
519 * From '100k' to 102400. and so on. Understands kKmMgG.
520 * k=1024, m=1024*1024, g=1024*1024*1024.
522 * @param res: result is stored here, size in bytes.
523 * @return: true if parsed correctly, or 0 on a parse error (and an error
526 int cfg_parse_memsize(const char* str, size_t* res);
529 * Parse local-zone directive into two strings and register it in the config.
530 * @param cfg: to put it in.
531 * @param val: argument strings to local-zone, "example.com nodefault".
532 * @return: false on failure
534 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
537 * Mark "number" or "low-high" as available or not in ports array.
538 * @param str: string in input
539 * @param allow: give true if this range is permitted.
540 * @param avail: the array from cfg.
541 * @param num: size of the array (65536).
542 * @return: true if parsed correctly, or 0 on a parse error (and an error
545 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
548 * Get a condensed list of ports returned. allocated.
549 * @param cfg: config file.
550 * @param avail: the available ports array is returned here.
551 * @return: number of ports in array or 0 on error.
553 int cfg_condense_ports(struct config_file* cfg, int** avail);
556 * Scan ports available
557 * @param avail: the array from cfg.
558 * @param num: size of the array (65536).
559 * @return the number of ports available for use.
561 int cfg_scan_ports(int* avail, int num);
564 * Convert a filename to full pathname in original filesys
565 * @param fname: the path name to convert.
566 * Must not be null or empty.
567 * @param cfg: config struct for chroot and chdir (if set).
568 * @param use_chdir: if false, only chroot is applied.
569 * @return pointer to malloced buffer which is: [chroot][chdir]fname
570 * or NULL on malloc failure.
572 char* fname_after_chroot(const char* fname, struct config_file* cfg,
576 * Convert a ptr shorthand into a full reverse-notation PTR record.
577 * @param str: input string, "IP name"
578 * @return: malloced string "reversed-ip-name PTR name"
580 char* cfg_ptr_reverse(char* str);
583 * Append text to the error info for validation.
584 * @param qstate: query state.
585 * @param str: copied into query region and appended.
586 * Failures to allocate are logged.
588 void errinf(struct module_qstate* qstate, const char* str);
591 * Append text to error info: from 1.2.3.4
592 * @param qstate: query state.
593 * @param origin: sock list with origin of trouble.
594 * Every element added.
595 * If NULL: nothing is added.
596 * if 0len element: 'from cache' is added.
598 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
601 * Append text to error info: for RRset name type class
602 * @param qstate: query state.
603 * @param rr: rrset_key.
605 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
608 * Append text to error info: str dname
609 * @param qstate: query state.
610 * @param str: explanation string
611 * @param dname: the dname.
613 void errinf_dname(struct module_qstate* qstate, const char* str,
617 * Create error info in string
618 * @param qstate: query state.
619 * @return string or NULL on malloc failure (already logged).
620 * This string is malloced and has to be freed by caller.
622 char* errinf_to_str(struct module_qstate* qstate);
625 * Used during options parsing
627 struct config_parser_state {
628 /** name of file being parser */
630 /** line number in the file, starts at 1 */
632 /** number of errors encountered */
634 /** the result of parsing is stored here. */
635 struct config_file* cfg;
636 /** the current chroot dir (or NULL if none) */
640 /** global config parser object used during config parsing */
641 extern struct config_parser_state* cfg_parser;
642 /** init lex state */
643 void init_cfg_parse(void);
645 extern FILE* ub_c_in;
647 extern FILE* ub_c_out;
648 /** the yacc lex generated parse function */
649 int ub_c_parse(void);
650 /** the lexer function */
654 /** parsing helpers: print error with file and line numbers */
655 void ub_c_error(const char* msg);
656 /** parsing helpers: print error with file and line numbers */
657 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
661 * Obtain registry string (if it exists).
662 * @param key: key string
663 * @param name: name of value to fetch.
664 * @return malloced string with the result or NULL if it did not
665 * exist on an error (logged with log_err) was encountered.
667 char* w_lookup_reg_str(const char* key, const char* name);
668 #endif /* UB_ON_WINDOWS */
670 #endif /* UTIL_CONFIG_FILE_H */