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 */
140 /** number of interfaces to open. If 0 default all interfaces. */
142 /** interface description strings (IP addresses) */
145 /** number of outgoing interfaces to open.
146 * If 0 default all interfaces. */
148 /** outgoing interface description strings (IP addresses) */
151 /** the root hints */
152 struct config_strlist* root_hints;
153 /** the stub definitions, linked list */
154 struct config_stub* stubs;
155 /** the forward zone definitions, linked list */
156 struct config_stub* forwards;
157 /** list of donotquery addresses, linked list */
158 struct config_strlist* donotqueryaddrs;
159 /** list of access control entries, linked list */
160 struct config_str2list* acls;
161 /** use default localhost donotqueryaddr entries */
162 int donotquery_localhost;
164 /** harden against very small edns buffer sizes */
165 int harden_short_bufsize;
166 /** harden against very large query sizes */
167 int harden_large_queries;
168 /** harden against spoofed glue (out of zone data) */
170 /** harden against receiving no DNSSEC data for trust anchor */
171 int harden_dnssec_stripped;
172 /** harden against queries that fall under known nxdomain names */
173 int harden_below_nxdomain;
174 /** harden the referral path, query for NS,A,AAAA and validate */
175 int harden_referral_path;
176 /** use 0x20 bits in query as random ID bits */
177 int use_caps_bits_for_id;
178 /** strip away these private addrs from answers, no DNS Rebinding */
179 struct config_strlist* private_address;
180 /** allow domain (and subdomains) to use private address space */
181 struct config_strlist* private_domain;
182 /** what threshold for unwanted action. */
183 size_t unwanted_threshold;
184 /** the number of seconds maximal TTL used for RRsets and messages */
186 /** the number of seconds minimum TTL used for RRsets and messages */
188 /** if prefetching of messages should be performed. */
190 /** if prefetching of DNSKEYs should be performed. */
193 /** chrootdir, if not "" or chroot will be done */
195 /** 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;
346 /** from cfg username, after daemonise setup performed */
347 extern uid_t cfg_uid;
348 /** from cfg username, after daemonise setup performed */
349 extern gid_t cfg_gid;
352 * Stub config options
356 struct config_stub* next;
357 /** domain name (in text) of the stub apex domain */
359 /** list of stub nameserver hosts (domain name) */
360 struct config_strlist* hosts;
361 /** list of stub nameserver addresses (IP address) */
362 struct config_strlist* addrs;
363 /** if stub-prime is set */
365 /** if forward-first is set (failover to without if fails) */
370 * List of strings for config options
372 struct config_strlist {
373 /** next item in list */
374 struct config_strlist* next;
375 /** config option string */
380 * List of two strings for config options
382 struct config_str2list {
383 /** next item in list */
384 struct config_str2list* next;
391 /** List head for strlist processing, used for append operation. */
392 struct config_strlist_head {
393 /** first in list of text items */
394 struct config_strlist* first;
395 /** last in list of text items */
396 struct config_strlist* last;
400 * Create config file structure. Filled with default values.
401 * @return: the new structure or NULL on memory error.
403 struct config_file* config_create(void);
406 * Create config file structure for library use. Filled with default values.
407 * @return: the new structure or NULL on memory error.
409 struct config_file* config_create_forlib(void);
412 * Read the config file from the specified filename.
413 * @param config: where options are stored into, must be freshly created.
414 * @param filename: name of configfile. If NULL nothing is done.
415 * @param chroot: if not NULL, the chroot dir currently in use (for include).
416 * @return: false on error. In that case errno is set, ENOENT means
419 int config_read(struct config_file* config, const char* filename,
423 * Destroy the config file structure.
424 * @param config: to delete.
426 void config_delete(struct config_file* config);
429 * Apply config to global constants; this routine is called in single thread.
430 * @param config: to apply. Side effect: global constants change.
432 void config_apply(struct config_file* config);
435 * Find username, sets cfg_uid and cfg_gid.
436 * @param config: the config structure.
438 void config_lookup_uid(struct config_file* config);
441 * Set the given keyword to the given value.
442 * @param config: where to store config
443 * @param option: option name, including the ':' character.
444 * @param value: value, this string is copied if needed, or parsed.
445 * The caller owns the value string.
446 * @return 0 on error (malloc or syntax error).
448 int config_set_option(struct config_file* config, const char* option,
452 * Call print routine for the given option.
453 * @param cfg: config.
454 * @param opt: option name without trailing :.
455 * This is different from config_set_option.
456 * @param func: print func, called as (str, arg) for every data element.
457 * @param arg: user argument for print func.
458 * @return false if the option name is not supported (syntax error).
460 int config_get_option(struct config_file* cfg, const char* opt,
461 void (*func)(char*,void*), void* arg);
464 * Get an option and return strlist
465 * @param cfg: config file
466 * @param opt: option name.
467 * @param list: list is returned here. malloced, caller must free it.
468 * @return 0=OK, 1=syntax error, 2=malloc failed.
470 int config_get_option_list(struct config_file* cfg, const char* opt,
471 struct config_strlist** list);
474 * Get an option and collate results into string
475 * @param cfg: config file
476 * @param opt: option name.
477 * @param str: string. malloced, caller must free it.
478 * @return 0=OK, 1=syntax error, 2=malloc failed.
480 int config_get_option_collate(struct config_file* cfg, const char* opt,
484 * function to print to a file, use as func with config_get_option.
485 * @param line: text to print. \n appended.
486 * @param arg: pass a FILE*, like stdout.
488 void config_print_func(char* line, void* arg);
491 * function to collate the text strings into a strlist_head.
492 * @param line: text to append.
493 * @param arg: pass a strlist_head structure. zeroed on start.
495 void config_collate_func(char* line, void* arg);
498 * take a strlist_head list and return a malloc string. separated with newline.
499 * @param list: strlist first to collate. zeroes return "".
500 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
502 char* config_collate_cat(struct config_strlist* list);
505 * Append text at end of list.
506 * @param list: list head. zeroed at start.
507 * @param item: new item. malloced by caller. if NULL the insertion fails.
508 * @return true on success.
510 int cfg_strlist_append(struct config_strlist_head* list, char* item);
513 * Insert string into strlist.
514 * @param head: pointer to strlist head variable.
515 * @param item: new item. malloced by caller. If NULL the insertion fails.
516 * @return: true on success.
518 int cfg_strlist_insert(struct config_strlist** head, char* item);
521 * Insert string into str2list.
522 * @param head: pointer to str2list head variable.
523 * @param item: new item. malloced by caller. If NULL the insertion fails.
524 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
525 * @return: true on success.
527 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
530 * Delete items in config string list.
533 void config_delstrlist(struct config_strlist* list);
536 * Delete items in config double string list.
539 void config_deldblstrlist(struct config_str2list* list);
542 * Delete items in config stub list.
545 void config_delstubs(struct config_stub* list);
548 * Convert 14digit to time value
549 * @param str: string of 14 digits
550 * @return time value or 0 for error.
552 time_t cfg_convert_timeval(const char* str);
555 * Count number of values in the string.
556 * format ::= (sp num)+ sp
558 * sp ::= (space|tab)*
561 * @return: 0 on parse error, or empty string, else
562 * number of integer values in the string.
564 int cfg_count_numbers(const char* str);
567 * Convert a 'nice' memory or file size into a bytecount
568 * From '100k' to 102400. and so on. Understands kKmMgG.
569 * k=1024, m=1024*1024, g=1024*1024*1024.
571 * @param res: result is stored here, size in bytes.
572 * @return: true if parsed correctly, or 0 on a parse error (and an error
575 int cfg_parse_memsize(const char* str, size_t* res);
578 * Parse local-zone directive into two strings and register it in the config.
579 * @param cfg: to put it in.
580 * @param val: argument strings to local-zone, "example.com nodefault".
581 * @return: false on failure
583 int cfg_parse_local_zone(struct config_file* cfg, const char* val);
586 * Mark "number" or "low-high" as available or not in ports array.
587 * @param str: string in input
588 * @param allow: give true if this range is permitted.
589 * @param avail: the array from cfg.
590 * @param num: size of the array (65536).
591 * @return: true if parsed correctly, or 0 on a parse error (and an error
594 int cfg_mark_ports(const char* str, int allow, int* avail, int num);
597 * Get a condensed list of ports returned. allocated.
598 * @param cfg: config file.
599 * @param avail: the available ports array is returned here.
600 * @return: number of ports in array or 0 on error.
602 int cfg_condense_ports(struct config_file* cfg, int** avail);
605 * Scan ports available
606 * @param avail: the array from cfg.
607 * @param num: size of the array (65536).
608 * @return the number of ports available for use.
610 int cfg_scan_ports(int* avail, int num);
613 * Convert a filename to full pathname in original filesys
614 * @param fname: the path name to convert.
615 * Must not be null or empty.
616 * @param cfg: config struct for chroot and chdir (if set).
617 * @param use_chdir: if false, only chroot is applied.
618 * @return pointer to malloced buffer which is: [chroot][chdir]fname
619 * or NULL on malloc failure.
621 char* fname_after_chroot(const char* fname, struct config_file* cfg,
625 * Convert a ptr shorthand into a full reverse-notation PTR record.
626 * @param str: input string, "IP name"
627 * @return: malloced string "reversed-ip-name PTR name"
629 char* cfg_ptr_reverse(char* str);
632 * Append text to the error info for validation.
633 * @param qstate: query state.
634 * @param str: copied into query region and appended.
635 * Failures to allocate are logged.
637 void errinf(struct module_qstate* qstate, const char* str);
640 * Append text to error info: from 1.2.3.4
641 * @param qstate: query state.
642 * @param origin: sock list with origin of trouble.
643 * Every element added.
644 * If NULL: nothing is added.
645 * if 0len element: 'from cache' is added.
647 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
650 * Append text to error info: for RRset name type class
651 * @param qstate: query state.
652 * @param rr: rrset_key.
654 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
657 * Append text to error info: str dname
658 * @param qstate: query state.
659 * @param str: explanation string
660 * @param dname: the dname.
662 void errinf_dname(struct module_qstate* qstate, const char* str,
666 * Create error info in string
667 * @param qstate: query state.
668 * @return string or NULL on malloc failure (already logged).
669 * This string is malloced and has to be freed by caller.
671 char* errinf_to_str(struct module_qstate* qstate);
674 * Used during options parsing
676 struct config_parser_state {
677 /** name of file being parser */
679 /** line number in the file, starts at 1 */
681 /** number of errors encountered */
683 /** the result of parsing is stored here. */
684 struct config_file* cfg;
685 /** the current chroot dir (or NULL if none) */
689 /** global config parser object used during config parsing */
690 extern struct config_parser_state* cfg_parser;
691 /** init lex state */
692 void init_cfg_parse(void);
694 extern FILE* ub_c_in;
696 extern FILE* ub_c_out;
697 /** the yacc lex generated parse function */
698 int ub_c_parse(void);
699 /** the lexer function */
703 /** parsing helpers: print error with file and line numbers */
704 void ub_c_error(const char* msg);
705 /** parsing helpers: print error with file and line numbers */
706 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
710 * Obtain registry string (if it exists).
711 * @param key: key string
712 * @param name: name of value to fetch.
713 * @return malloced string with the result or NULL if it did not
714 * exist on an error (logged with log_err) was encountered.
716 char* w_lookup_reg_str(const char* key, const char* name);
717 #endif /* UB_ON_WINDOWS */
719 #endif /* UTIL_CONFIG_FILE_H */