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
;
276 /** unblock lan zones (reverse lookups for 10/8 and so on) */
277 int unblock_lan_zones
;
279 /** remote control section. enable toggle. */
280 int remote_control_enable
;
281 /** the interfaces the remote control should listen on */
282 struct config_strlist
* control_ifs
;
283 /** port number for the control port */
285 /** private key file for server */
286 char* server_key_file
;
287 /** certificate file for server */
288 char* server_cert_file
;
289 /** private key file for unbound-control */
290 char* control_key_file
;
291 /** certificate file for unbound-control */
292 char* control_cert_file
;
294 /** Python script file */
297 /** daemonize, i.e. fork into the background. */
300 /* minimal response when positive answer */
301 int minimal_responses
;
303 /* RRSet roundrobin */
304 int rrset_roundrobin
;
306 /* maximum UDP response size */
312 /* Synthetize all AAAA record despite the presence of an authoritative one */
315 /** true to enable dnstap support */
317 /** dnstap socket path */
318 char* dnstap_socket_path
;
319 /** true to send "identity" via dnstap */
320 int dnstap_send_identity
;
321 /** true to send "version" via dnstap */
322 int dnstap_send_version
;
323 /** dnstap "identity", hostname is used if "". */
324 char* dnstap_identity
;
325 /** dnstap "version", package version is used if "". */
326 char* dnstap_version
;
328 /** true to log dnstap RESOLVER_QUERY message events */
329 int dnstap_log_resolver_query_messages
;
330 /** true to log dnstap RESOLVER_RESPONSE message events */
331 int dnstap_log_resolver_response_messages
;
332 /** true to log dnstap CLIENT_QUERY message events */
333 int dnstap_log_client_query_messages
;
334 /** true to log dnstap CLIENT_RESPONSE message events */
335 int dnstap_log_client_response_messages
;
336 /** true to log dnstap FORWARDER_QUERY message events */
337 int dnstap_log_forwarder_query_messages
;
338 /** true to log dnstap FORWARDER_RESPONSE message events */
339 int dnstap_log_forwarder_response_messages
;
343 * Stub config options
347 struct config_stub
* next
;
348 /** domain name (in text) of the stub apex domain */
350 /** list of stub nameserver hosts (domain name) */
351 struct config_strlist
* hosts
;
352 /** list of stub nameserver addresses (IP address) */
353 struct config_strlist
* addrs
;
354 /** if stub-prime is set */
356 /** if forward-first is set (failover to without if fails) */
361 * List of strings for config options
363 struct config_strlist
{
364 /** next item in list */
365 struct config_strlist
* next
;
366 /** config option string */
371 * List of two strings for config options
373 struct config_str2list
{
374 /** next item in list */
375 struct config_str2list
* next
;
382 /** List head for strlist processing, used for append operation. */
383 struct config_strlist_head
{
384 /** first in list of text items */
385 struct config_strlist
* first
;
386 /** last in list of text items */
387 struct config_strlist
* last
;
391 * Create config file structure. Filled with default values.
392 * @return: the new structure or NULL on memory error.
394 struct config_file
* config_create(void);
397 * Create config file structure for library use. Filled with default values.
398 * @return: the new structure or NULL on memory error.
400 struct config_file
* config_create_forlib(void);
403 * Read the config file from the specified filename.
404 * @param config: where options are stored into, must be freshly created.
405 * @param filename: name of configfile. If NULL nothing is done.
406 * @param chroot: if not NULL, the chroot dir currently in use (for include).
407 * @return: false on error. In that case errno is set, ENOENT means
410 int config_read(struct config_file
* config
, const char* filename
,
414 * Destroy the config file structure.
415 * @param config: to delete.
417 void config_delete(struct config_file
* config
);
420 * Apply config to global constants; this routine is called in single thread.
421 * @param config: to apply. Side effect: global constants change.
423 void config_apply(struct config_file
* config
);
426 * Set the given keyword to the given value.
427 * @param config: where to store config
428 * @param option: option name, including the ':' character.
429 * @param value: value, this string is copied if needed, or parsed.
430 * The caller owns the value string.
431 * @return 0 on error (malloc or syntax error).
433 int config_set_option(struct config_file
* config
, const char* option
,
437 * Call print routine for the given option.
438 * @param cfg: config.
439 * @param opt: option name without trailing :.
440 * This is different from config_set_option.
441 * @param func: print func, called as (str, arg) for every data element.
442 * @param arg: user argument for print func.
443 * @return false if the option name is not supported (syntax error).
445 int config_get_option(struct config_file
* cfg
, const char* opt
,
446 void (*func
)(char*,void*), void* arg
);
449 * Get an option and return strlist
450 * @param cfg: config file
451 * @param opt: option name.
452 * @param list: list is returned here. malloced, caller must free it.
453 * @return 0=OK, 1=syntax error, 2=malloc failed.
455 int config_get_option_list(struct config_file
* cfg
, const char* opt
,
456 struct config_strlist
** list
);
459 * Get an option and collate results into string
460 * @param cfg: config file
461 * @param opt: option name.
462 * @param str: string. malloced, caller must free it.
463 * @return 0=OK, 1=syntax error, 2=malloc failed.
465 int config_get_option_collate(struct config_file
* cfg
, const char* opt
,
469 * function to print to a file, use as func with config_get_option.
470 * @param line: text to print. \n appended.
471 * @param arg: pass a FILE*, like stdout.
473 void config_print_func(char* line
, void* arg
);
476 * function to collate the text strings into a strlist_head.
477 * @param line: text to append.
478 * @param arg: pass a strlist_head structure. zeroed on start.
480 void config_collate_func(char* line
, void* arg
);
483 * take a strlist_head list and return a malloc string. separated with newline.
484 * @param list: strlist first to collate. zeroes return "".
485 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
487 char* config_collate_cat(struct config_strlist
* list
);
490 * Append text at end of list.
491 * @param list: list head. zeroed at start.
492 * @param item: new item. malloced by caller. if NULL the insertion fails.
493 * @return true on success.
495 int cfg_strlist_append(struct config_strlist_head
* list
, char* item
);
498 * Insert string into strlist.
499 * @param head: pointer to strlist head variable.
500 * @param item: new item. malloced by caller. If NULL the insertion fails.
501 * @return: true on success.
503 int cfg_strlist_insert(struct config_strlist
** head
, char* item
);
506 * Insert string into str2list.
507 * @param head: pointer to str2list head variable.
508 * @param item: new item. malloced by caller. If NULL the insertion fails.
509 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
510 * @return: true on success.
512 int cfg_str2list_insert(struct config_str2list
** head
, char* item
, char* i2
);
515 * Delete items in config string list.
518 void config_delstrlist(struct config_strlist
* list
);
521 * Delete items in config double string list.
524 void config_deldblstrlist(struct config_str2list
* list
);
527 * Delete items in config stub list.
530 void config_delstubs(struct config_stub
* list
);
533 * Convert 14digit to time value
534 * @param str: string of 14 digits
535 * @return time value or 0 for error.
537 time_t cfg_convert_timeval(const char* str
);
540 * Count number of values in the string.
541 * format ::= (sp num)+ sp
543 * sp ::= (space|tab)*
546 * @return: 0 on parse error, or empty string, else
547 * number of integer values in the string.
549 int cfg_count_numbers(const char* str
);
552 * Convert a 'nice' memory or file size into a bytecount
553 * From '100k' to 102400. and so on. Understands kKmMgG.
554 * k=1024, m=1024*1024, g=1024*1024*1024.
556 * @param res: result is stored here, size in bytes.
557 * @return: true if parsed correctly, or 0 on a parse error (and an error
560 int cfg_parse_memsize(const char* str
, size_t* res
);
563 * Parse local-zone directive into two strings and register it in the config.
564 * @param cfg: to put it in.
565 * @param val: argument strings to local-zone, "example.com nodefault".
566 * @return: false on failure
568 int cfg_parse_local_zone(struct config_file
* cfg
, const char* val
);
571 * Mark "number" or "low-high" as available or not in ports array.
572 * @param str: string in input
573 * @param allow: give true if this range is permitted.
574 * @param avail: the array from cfg.
575 * @param num: size of the array (65536).
576 * @return: true if parsed correctly, or 0 on a parse error (and an error
579 int cfg_mark_ports(const char* str
, int allow
, int* avail
, int num
);
582 * Get a condensed list of ports returned. allocated.
583 * @param cfg: config file.
584 * @param avail: the available ports array is returned here.
585 * @return: number of ports in array or 0 on error.
587 int cfg_condense_ports(struct config_file
* cfg
, int** avail
);
590 * Scan ports available
591 * @param avail: the array from cfg.
592 * @param num: size of the array (65536).
593 * @return the number of ports available for use.
595 int cfg_scan_ports(int* avail
, int num
);
598 * Convert a filename to full pathname in original filesys
599 * @param fname: the path name to convert.
600 * Must not be null or empty.
601 * @param cfg: config struct for chroot and chdir (if set).
602 * @param use_chdir: if false, only chroot is applied.
603 * @return pointer to malloced buffer which is: [chroot][chdir]fname
604 * or NULL on malloc failure.
606 char* fname_after_chroot(const char* fname
, struct config_file
* cfg
,
610 * Convert a ptr shorthand into a full reverse-notation PTR record.
611 * @param str: input string, "IP name"
612 * @return: malloced string "reversed-ip-name PTR name"
614 char* cfg_ptr_reverse(char* str
);
617 * Append text to the error info for validation.
618 * @param qstate: query state.
619 * @param str: copied into query region and appended.
620 * Failures to allocate are logged.
622 void errinf(struct module_qstate
* qstate
, const char* str
);
625 * Append text to error info: from 1.2.3.4
626 * @param qstate: query state.
627 * @param origin: sock list with origin of trouble.
628 * Every element added.
629 * If NULL: nothing is added.
630 * if 0len element: 'from cache' is added.
632 void errinf_origin(struct module_qstate
* qstate
, struct sock_list
*origin
);
635 * Append text to error info: for RRset name type class
636 * @param qstate: query state.
637 * @param rr: rrset_key.
639 void errinf_rrset(struct module_qstate
* qstate
, struct ub_packed_rrset_key
*rr
);
642 * Append text to error info: str dname
643 * @param qstate: query state.
644 * @param str: explanation string
645 * @param dname: the dname.
647 void errinf_dname(struct module_qstate
* qstate
, const char* str
,
651 * Create error info in string
652 * @param qstate: query state.
653 * @return string or NULL on malloc failure (already logged).
654 * This string is malloced and has to be freed by caller.
656 char* errinf_to_str(struct module_qstate
* qstate
);
659 * Used during options parsing
661 struct config_parser_state
{
662 /** name of file being parser */
664 /** line number in the file, starts at 1 */
666 /** number of errors encountered */
668 /** the result of parsing is stored here. */
669 struct config_file
* cfg
;
670 /** the current chroot dir (or NULL if none) */
674 /** global config parser object used during config parsing */
675 extern struct config_parser_state
* cfg_parser
;
676 /** init lex state */
677 void init_cfg_parse(void);
679 extern FILE* ub_c_in
;
681 extern FILE* ub_c_out
;
682 /** the yacc lex generated parse function */
683 int ub_c_parse(void);
684 /** the lexer function */
688 /** parsing helpers: print error with file and line numbers */
689 void ub_c_error(const char* msg
);
690 /** parsing helpers: print error with file and line numbers */
691 void ub_c_error_msg(const char* fmt
, ...) ATTR_FORMAT(printf
, 1, 2);
695 * Obtain registry string (if it exists).
696 * @param key: key string
697 * @param name: name of value to fetch.
698 * @return malloced string with the result or NULL if it did not
699 * exist on an error (logged with log_err) was encountered.
701 char* w_lookup_reg_str(const char* key
, const char* name
);
702 #endif /* UB_ON_WINDOWS */
704 #endif /* UTIL_CONFIG_FILE_H */