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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * 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;
123 /** the target fetch policy for the iterator */
124 char* target_fetch_policy;
126 /** automatic interface for incoming messages. Uses ipv6 remapping,
127 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
129 /** SO_RCVBUF size to set on port 53 UDP socket */
131 /** SO_SNDBUF size to set on port 53 UDP socket */
134 /** number of interfaces to open. If 0 default all interfaces. */
136 /** interface description strings (IP addresses) */
139 /** number of outgoing interfaces to open.
140 * If 0 default all interfaces. */
142 /** outgoing interface description strings (IP addresses) */
145 /** the root hints */
146 struct config_strlist* root_hints;
147 /** the stub definitions, linked list */
148 struct config_stub* stubs;
149 /** the forward zone definitions, linked list */
150 struct config_stub* forwards;
151 /** list of donotquery addresses, linked list */
152 struct config_strlist* donotqueryaddrs;
153 /** list of access control entries, linked list */
154 struct config_str2list* acls;
155 /** use default localhost donotqueryaddr entries */
156 int donotquery_localhost;
158 /** harden against very small edns buffer sizes */
159 int harden_short_bufsize;
160 /** harden against very large query sizes */
161 int harden_large_queries;
162 /** harden against spoofed glue (out of zone data) */
164 /** harden against receiving no DNSSEC data for trust anchor */
165 int harden_dnssec_stripped;
166 /** harden against queries that fall under known nxdomain names */
167 int harden_below_nxdomain;
168 /** harden the referral path, query for NS,A,AAAA and validate */
169 int harden_referral_path;
170 /** use 0x20 bits in query as random ID bits */
171 int use_caps_bits_for_id;
172 /** strip away these private addrs from answers, no DNS Rebinding */
173 struct config_strlist* private_address;
174 /** allow domain (and subdomains) to use private address space */
175 struct config_strlist* private_domain;
176 /** what threshold for unwanted action. */
177 size_t unwanted_threshold;
178 /** the number of seconds maximal TTL used for RRsets and messages */
180 /** the number of seconds minimum TTL used for RRsets and messages */
182 /** if prefetching of messages should be performed. */
184 /** if prefetching of DNSKEYs should be performed. */
187 /** chrootdir, if not "" or chroot will be done */
189 /** username to change to, if not "". */
191 /** working directory */
193 /** filename to log to. */
195 /** pidfile to write pid to. */
198 /** should log messages be sent to syslogd */
200 /** log timestamp in ascii UTC */
202 /** log queries with one line per query */
205 /** do not report identity (id.server, hostname.bind) */
207 /** do not report version (version.server, version.bind) */
209 /** identity, hostname is returned if "". */
211 /** version, package version returned if "". */
214 /** the module configuration string */
217 /** files with trusted DS and DNSKEYs in zonefile format, list */
218 struct config_strlist* trust_anchor_file_list;
219 /** list of trustanchor keys, linked list */
220 struct config_strlist* trust_anchor_list;
221 /** files with 5011 autotrust tracked keys */
222 struct config_strlist* auto_trust_anchor_file_list;
223 /** files with trusted DNSKEYs in named.conf format, list */
224 struct config_strlist* trusted_keys_file_list;
225 /** DLV anchor file */
226 char* dlv_anchor_file;
227 /** DLV anchor inline */
228 struct config_strlist* dlv_anchor_list;
229 /** insecure domain list */
230 struct config_strlist* domain_insecure;
232 /** if not 0, this value is the validation date for RRSIGs */
233 int32_t val_date_override;
234 /** the minimum for signature clock skew */
235 int32_t val_sig_skew_min;
236 /** the maximum for signature clock skew */
237 int32_t val_sig_skew_max;
238 /** this value sets the number of seconds before revalidating bogus */
240 /** should validator clean additional section for secure msgs */
241 int val_clean_additional;
242 /** log bogus messages by the validator */
244 /** squelch val_log_level to log - this is library goes to callback */
246 /** should validator allow bogus messages to go through */
247 int val_permissive_mode;
248 /** ignore the CD flag in incoming queries and refuse them bogus data */
250 /** nsec3 maximum iterations per key size, string */
251 char* val_nsec3_key_iterations;
252 /** autotrust add holddown time, in seconds */
253 unsigned int add_holddown;
254 /** autotrust del holddown time, in seconds */
255 unsigned int del_holddown;
256 /** autotrust keep_missing time, in seconds. 0 is forever. */
257 unsigned int keep_missing;
259 /** size of the key cache */
260 size_t key_cache_size;
261 /** slabs in the key cache. */
262 size_t key_cache_slabs;
263 /** size of the neg cache */
264 size_t neg_cache_size;
266 /** local zones config */
267 struct config_str2list* local_zones;
268 /** local zones nodefault list */
269 struct config_strlist* local_zones_nodefault;
270 /** local data RRs configged */
271 struct config_strlist* local_data;
273 /** remote control section. enable toggle. */
274 int remote_control_enable;
275 /** the interfaces the remote control should listen on */
276 struct config_strlist* control_ifs;
277 /** port number for the control port */
279 /** private key file for server */
280 char* server_key_file;
281 /** certificate file for server */
282 char* server_cert_file;
283 /** private key file for unbound-control */
284 char* control_key_file;
285 /** certificate file for unbound-control */
286 char* control_cert_file;
288 /** Python script file */
291 /** daemonize, i.e. fork into the background. */
294 /* minimal response when positive answer */
295 int minimal_responses;
297 /* RRSet roundrobin */
298 int rrset_roundrobin;
302 * Stub config options
306 struct config_stub* next;
307 /** domain name (in text) of the stub apex domain */
309 /** list of stub nameserver hosts (domain name) */
310 struct config_strlist* hosts;
311 /** list of stub nameserver addresses (IP address) */
312 struct config_strlist* addrs;
313 /** if stub-prime is set */
315 /** if forward-first is set (failover to without if fails) */
320 * List of strings for config options
322 struct config_strlist {
323 /** next item in list */
324 struct config_strlist* next;
325 /** config option string */
330 * List of two strings for config options
332 struct config_str2list {
333 /** next item in list */
334 struct config_str2list* next;
341 /** List head for strlist processing, used for append operation. */
342 struct config_strlist_head {
343 /** first in list of text items */
344 struct config_strlist* first;
345 /** last in list of text items */
346 struct config_strlist* last;
350 * Create config file structure. Filled with default values.
351 * @return: the new structure or NULL on memory error.
353 struct config_file* config_create(void);
356 * Create config file structure for library use. Filled with default values.
357 * @return: the new structure or NULL on memory error.
359 struct config_file* config_create_forlib(void);
362 * Read the config file from the specified filename.
363 * @param config: where options are stored into, must be freshly created.
364 * @param filename: name of configfile. If NULL nothing is done.
365 * @param chroot: if not NULL, the chroot dir currently in use (for include).
366 * @return: false on error. In that case errno is set, ENOENT means
369 int config_read(struct config_file* config, const char* filename,
373 * Destroy the config file structure.
374 * @param config: to delete.
376 void config_delete(struct config_file* config);
379 * Apply config to global constants; this routine is called in single thread.
380 * @param config: to apply. Side effect: global constants change.
382 void config_apply(struct config_file* config);
385 * Set the given keyword to the given value.
386 * @param config: where to store config
387 * @param option: option name, including the ':' character.
388 * @param value: value, this string is copied if needed, or parsed.
389 * The caller owns the value string.
390 * @return 0 on error (malloc or syntax error).
392 int config_set_option(struct config_file* config, const char* option,
396 * Call print routine for the given option.
397 * @param cfg: config.
398 * @param opt: option name without trailing :.
399 * This is different from config_set_option.
400 * @param func: print func, called as (str, arg) for every data element.
401 * @param arg: user argument for print func.
402 * @return false if the option name is not supported (syntax error).
404 int config_get_option(struct config_file* cfg, const char* opt,
405 void (*func)(char*,void*), void* arg);
408 * Get an option and return strlist
409 * @param cfg: config file
410 * @param opt: option name.
411 * @param list: list is returned here. malloced, caller must free it.
412 * @return 0=OK, 1=syntax error, 2=malloc failed.
414 int config_get_option_list(struct config_file* cfg, const char* opt,
415 struct config_strlist** list);
418 * Get an option and collate results into string
419 * @param cfg: config file
420 * @param opt: option name.
421 * @param str: string. malloced, caller must free it.
422 * @return 0=OK, 1=syntax error, 2=malloc failed.
424 int config_get_option_collate(struct config_file* cfg, const char* opt,
428 * function to print to a file, use as func with config_get_option.
429 * @param line: text to print. \n appended.
430 * @param arg: pass a FILE*, like stdout.
432 void config_print_func(char* line, void* arg);
435 * function to collate the text strings into a strlist_head.
436 * @param line: text to append.
437 * @param arg: pass a strlist_head structure. zeroed on start.
439 void config_collate_func(char* line, void* arg);
442 * take a strlist_head list and return a malloc string. separated with newline.
443 * @param list: strlist first to collate. zeroes return "".
444 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
446 char* config_collate_cat(struct config_strlist* list);
449 * Append text at end of list.
450 * @param list: list head. zeroed at start.
451 * @param item: new item. malloced by caller. if NULL the insertion fails.
452 * @return true on success.
454 int cfg_strlist_append(struct config_strlist_head* list, char* item);
457 * Insert string into strlist.
458 * @param head: pointer to strlist head variable.
459 * @param item: new item. malloced by caller. If NULL the insertion fails.
460 * @return: true on success.
462 int cfg_strlist_insert(struct config_strlist** head, char* item);
465 * Insert string into str2list.
466 * @param head: pointer to str2list head variable.
467 * @param item: new item. malloced by caller. If NULL the insertion fails.
468 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
469 * @return: true on success.
471 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
474 * Delete items in config string list.
477 void config_delstrlist(struct config_strlist* list);
480 * Delete items in config double string list.
483 void config_deldblstrlist(struct config_str2list* list);
486 * Delete items in config stub list.
489 void config_delstubs(struct config_stub* list);
492 * Convert 14digit to time value
493 * @param str: string of 14 digits
494 * @return time value or 0 for error.
496 uint32_t cfg_convert_timeval(const char* str);
499 * Count number of values in the string.
500 * format ::= (sp num)+ sp
502 * sp ::= (space|tab)*
505 * @return: 0 on parse error, or empty string, else
506 * number of integer values in the string.
508 int cfg_count_numbers(const char* str);
511 * Convert a 'nice' memory or file size into a bytecount
512 * From '100k' to 102400. and so on. Understands kKmMgG.
513 * k=1024, m=1024*1024, g=1024*1024*1024.
515 * @param res: result is stored here, size in bytes.
516 * @return: true if parsed correctly, or 0 on a parse error (and an error
519 int cfg_parse_memsize(const char* str, size_t* res);
522 * Parse local-zone directive into two strings and register it in the config.
523 * @param cfg: to put it in.
524 * @param val: argument strings to local-zone, "example.com nodefault".
525 * @return: false on failure
527 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
530 * Mark "number" or "low-high" as available or not in ports array.
531 * @param str: string in input
532 * @param allow: give true if this range is permitted.
533 * @param avail: the array from cfg.
534 * @param num: size of the array (65536).
535 * @return: true if parsed correctly, or 0 on a parse error (and an error
538 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
541 * Get a condensed list of ports returned. allocated.
542 * @param cfg: config file.
543 * @param avail: the available ports array is returned here.
544 * @return: number of ports in array or 0 on error.
546 int cfg_condense_ports(struct config_file* cfg, int** avail);
549 * Scan ports available
550 * @param avail: the array from cfg.
551 * @param num: size of the array (65536).
552 * @return the number of ports available for use.
554 int cfg_scan_ports(int* avail, int num);
557 * Convert a filename to full pathname in original filesys
558 * @param fname: the path name to convert.
559 * Must not be null or empty.
560 * @param cfg: config struct for chroot and chdir (if set).
561 * @param use_chdir: if false, only chroot is applied.
562 * @return pointer to malloced buffer which is: [chroot][chdir]fname
563 * or NULL on malloc failure.
565 char* fname_after_chroot(const char* fname, struct config_file* cfg,
569 * Convert a ptr shorthand into a full reverse-notation PTR record.
570 * @param str: input string, "IP name"
571 * @return: malloced string "reversed-ip-name PTR name"
573 char* cfg_ptr_reverse(char* str);
576 * Append text to the error info for validation.
577 * @param qstate: query state.
578 * @param str: copied into query region and appended.
579 * Failures to allocate are logged.
581 void errinf(struct module_qstate* qstate, const char* str);
584 * Append text to error info: from 1.2.3.4
585 * @param qstate: query state.
586 * @param origin: sock list with origin of trouble.
587 * Every element added.
588 * If NULL: nothing is added.
589 * if 0len element: 'from cache' is added.
591 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
594 * Append text to error info: for RRset name type class
595 * @param qstate: query state.
596 * @param rr: rrset_key.
598 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
601 * Append text to error info: str dname
602 * @param qstate: query state.
603 * @param str: explanation string
604 * @param dname: the dname.
606 void errinf_dname(struct module_qstate* qstate, const char* str,
610 * Create error info in string
611 * @param qstate: query state.
612 * @return string or NULL on malloc failure (already logged).
613 * This string is malloced and has to be freed by caller.
615 char* errinf_to_str(struct module_qstate* qstate);
618 * Used during options parsing
620 struct config_parser_state {
621 /** name of file being parser */
623 /** line number in the file, starts at 1 */
625 /** number of errors encountered */
627 /** the result of parsing is stored here. */
628 struct config_file* cfg;
629 /** the current chroot dir (or NULL if none) */
633 /** global config parser object used during config parsing */
634 extern struct config_parser_state* cfg_parser;
636 extern FILE* ub_c_in;
638 extern FILE* ub_c_out;
639 /** the yacc lex generated parse function */
640 int ub_c_parse(void);
641 /** the lexer function */
645 /** parsing helpers: print error with file and line numbers */
646 void ub_c_error(const char* msg);
647 /** parsing helpers: print error with file and line numbers */
648 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
652 * Obtain registry string (if it exists).
653 * @param key: key string
654 * @param name: name of value to fetch.
655 * @return malloced string with the result or NULL if it did not
656 * exist on an error (logged with log_err) was encountered.
658 char* w_lookup_reg_str(const char* key, const char* name);
659 #endif /* UB_ON_WINDOWS */
661 #endif /* UTIL_CONFIG_FILE_H */