1/* 2 * util/config_file.h - reads and stores the config file for unbound. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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. 34 */ 35 36/** 37 * \file 38 * 39 * This file contains functions for the config file. 40 */ 41 42#ifndef UTIL_CONFIG_FILE_H 43#define UTIL_CONFIG_FILE_H 44struct config_stub; 45struct config_strlist; 46struct config_str2list; 47struct module_qstate; 48struct sock_list; 49struct ub_packed_rrset_key; 50 51/** 52 * The configuration options. 53 * Strings are malloced. 54 */ 55struct config_file { 56 /** verbosity level as specified in the config file */ 57 int verbosity; 58 59 /** statistics interval (in seconds) */ 60 int stat_interval; 61 /** if false, statistics values are reset after printing them */ 62 int stat_cumulative; 63 /** if true, the statistics are kept in greater detail */ 64 int stat_extended; 65 66 /** number of threads to create */ 67 int num_threads; 68 69 /** port on which queries are answered. */ 70 int port; 71 /** do ip4 query support. */ 72 int do_ip4; 73 /** do ip6 query support. */ 74 int do_ip6; 75 /** do udp query support. */ 76 int do_udp; 77 /** do tcp query support. */ 78 int do_tcp; 79 /** tcp upstream queries (no UDP upstream queries) */ 80 int tcp_upstream; 81 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 */ 87 int ssl_port; 88 /** if outgoing tcp connections use SSL */ 89 int ssl_upstream; 90 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; 99 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 */ 111 size_t jostle_time; 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 */ 117 int host_ttl; 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 */ 123 int delay_close; 124 125 /** the target fetch policy for the iterator */ 126 char* target_fetch_policy; 127 128 /** automatic interface for incoming messages. Uses ipv6 remapping, 129 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */ 130 int if_automatic; 131 /** SO_RCVBUF size to set on port 53 UDP socket */ 132 size_t so_rcvbuf; 133 /** SO_SNDBUF size to set on port 53 UDP socket */ 134 size_t so_sndbuf; 135 /** SO_REUSEPORT requested on port 53 sockets */ 136 int so_reuseport; 137 138 /** number of interfaces to open. If 0 default all interfaces. */ 139 int num_ifs; 140 /** interface description strings (IP addresses) */ 141 char **ifs; 142 143 /** number of outgoing interfaces to open. 144 * If 0 default all interfaces. */ 145 int num_out_ifs; 146 /** outgoing interface description strings (IP addresses) */ 147 char **out_ifs; 148 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; 161 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) */ 167 int harden_glue; 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 */ 183 int max_ttl; 184 /** the number of seconds minimum TTL used for RRsets and messages */ 185 int min_ttl; 186 /** if prefetching of messages should be performed. */ 187 int prefetch; 188 /** if prefetching of DNSKEYs should be performed. */ 189 int prefetch_key; 190 191 /** chrootdir, if not "" or chroot will be done */ 192 char* chrootdir; 193 /** username to change to, if not "". */ 194 char* username; 195 /** working directory */ 196 char* directory; 197 /** filename to log to. */ 198 char* logfile; 199 /** pidfile to write pid to. */ 200 char* pidfile; 201 202 /** should log messages be sent to syslogd */ 203 int use_syslog; 204 /** log timestamp in ascii UTC */ 205 int log_time_ascii; 206 /** log queries with one line per query */ 207 int log_queries; 208 209 /** do not report identity (id.server, hostname.bind) */ 210 int hide_identity; 211 /** do not report version (version.server, version.bind) */ 212 int hide_version; 213 /** identity, hostname is returned if "". */ 214 char* identity; 215 /** version, package version returned if "". */ 216 char* version; 217 218 /** the module configuration string */ 219 char* module_conf; 220 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; 235 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 */ 243 int bogus_ttl; 244 /** should validator clean additional section for secure msgs */ 245 int val_clean_additional; 246 /** log bogus messages by the validator */ 247 int val_log_level; 248 /** squelch val_log_level to log - this is library goes to callback */ 249 int val_log_squelch; 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 */ 253 int ignore_cd; 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; 262 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; 269 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; 278 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 */ 284 int 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; 293 294 /** Python script file */ 295 char* python_script; 296 297 /** daemonize, i.e. fork into the background. */ 298 int do_daemonize; 299 300 /* minimal response when positive answer */ 301 int minimal_responses; 302 303 /* RRSet roundrobin */ 304 int rrset_roundrobin; 305 306 /* maximum UDP response size */ 307 size_t max_udp_size;
|
340}; 341 342/** 343 * Stub config options 344 */ 345struct config_stub { 346 /** next in list */ 347 struct config_stub* next; 348 /** domain name (in text) of the stub apex domain */ 349 char* name; 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 */ 355 int isprime; 356 /** if forward-first is set (failover to without if fails) */ 357 int isfirst; 358}; 359 360/** 361 * List of strings for config options 362 */ 363struct config_strlist { 364 /** next item in list */ 365 struct config_strlist* next; 366 /** config option string */ 367 char* str; 368}; 369 370/** 371 * List of two strings for config options 372 */ 373struct config_str2list { 374 /** next item in list */ 375 struct config_str2list* next; 376 /** first string */ 377 char* str; 378 /** second string */ 379 char* str2; 380}; 381 382/** List head for strlist processing, used for append operation. */ 383struct 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; 388}; 389 390/** 391 * Create config file structure. Filled with default values. 392 * @return: the new structure or NULL on memory error. 393 */ 394struct config_file* config_create(void); 395 396/** 397 * Create config file structure for library use. Filled with default values. 398 * @return: the new structure or NULL on memory error. 399 */ 400struct config_file* config_create_forlib(void); 401 402/** 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 408 * file not found. 409 */ 410int config_read(struct config_file* config, const char* filename, 411 const char* chroot); 412 413/** 414 * Destroy the config file structure. 415 * @param config: to delete. 416 */ 417void config_delete(struct config_file* config); 418 419/** 420 * Apply config to global constants; this routine is called in single thread. 421 * @param config: to apply. Side effect: global constants change. 422 */ 423void config_apply(struct config_file* config); 424 425/** 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). 432 */ 433int config_set_option(struct config_file* config, const char* option, 434 const char* value); 435 436/** 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). 444 */ 445int config_get_option(struct config_file* cfg, const char* opt, 446 void (*func)(char*,void*), void* arg); 447 448/** 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. 454 */ 455int config_get_option_list(struct config_file* cfg, const char* opt, 456 struct config_strlist** list); 457 458/** 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. 464 */ 465int config_get_option_collate(struct config_file* cfg, const char* opt, 466 char** str); 467 468/** 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. 472 */ 473void config_print_func(char* line, void* arg); 474 475/** 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. 479 */ 480void config_collate_func(char* line, void* arg); 481 482/** 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. 486 */ 487char* config_collate_cat(struct config_strlist* list); 488 489/** 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. 494 */ 495int cfg_strlist_append(struct config_strlist_head* list, char* item); 496 497/** 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. 502 */ 503int cfg_strlist_insert(struct config_strlist** head, char* item); 504 505/** 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. 511 */ 512int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2); 513 514/** 515 * Delete items in config string list. 516 * @param list: list. 517 */ 518void config_delstrlist(struct config_strlist* list); 519 520/** 521 * Delete items in config double string list. 522 * @param list: list. 523 */ 524void config_deldblstrlist(struct config_str2list* list); 525 526/** 527 * Delete items in config stub list. 528 * @param list: list. 529 */ 530void config_delstubs(struct config_stub* list); 531 532/** 533 * Convert 14digit to time value 534 * @param str: string of 14 digits 535 * @return time value or 0 for error. 536 */ 537time_t cfg_convert_timeval(const char* str); 538 539/** 540 * Count number of values in the string. 541 * format ::= (sp num)+ sp 542 * num ::= [-](0-9)+ 543 * sp ::= (space|tab)* 544 * 545 * @param str: string 546 * @return: 0 on parse error, or empty string, else 547 * number of integer values in the string. 548 */ 549int cfg_count_numbers(const char* str); 550 551/** 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. 555 * @param str: string 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 558 * is logged). 559 */ 560int cfg_parse_memsize(const char* str, size_t* res); 561 562/** 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 567 */ 568int cfg_parse_local_zone(struct config_file* cfg, const char* val); 569 570/** 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 577 * is logged). 578 */ 579int cfg_mark_ports(const char* str, int allow, int* avail, int num); 580 581/** 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. 586 */ 587int cfg_condense_ports(struct config_file* cfg, int** avail); 588 589/** 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. 594 */ 595int cfg_scan_ports(int* avail, int num); 596 597/** 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. 605 */ 606char* fname_after_chroot(const char* fname, struct config_file* cfg, 607 int use_chdir); 608 609/** 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" 613 */ 614char* cfg_ptr_reverse(char* str); 615 616/** 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. 621 */ 622void errinf(struct module_qstate* qstate, const char* str); 623 624/** 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. 631 */ 632void errinf_origin(struct module_qstate* qstate, struct sock_list *origin); 633 634/** 635 * Append text to error info: for RRset name type class 636 * @param qstate: query state. 637 * @param rr: rrset_key. 638 */ 639void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr); 640 641/** 642 * Append text to error info: str dname 643 * @param qstate: query state. 644 * @param str: explanation string 645 * @param dname: the dname. 646 */ 647void errinf_dname(struct module_qstate* qstate, const char* str, 648 uint8_t* dname); 649 650/** 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. 655 */ 656char* errinf_to_str(struct module_qstate* qstate); 657 658/** 659 * Used during options parsing 660 */ 661struct config_parser_state { 662 /** name of file being parser */ 663 char* filename; 664 /** line number in the file, starts at 1 */ 665 int line; 666 /** number of errors encountered */ 667 int errors; 668 /** the result of parsing is stored here. */ 669 struct config_file* cfg; 670 /** the current chroot dir (or NULL if none) */ 671 const char* chroot; 672}; 673 674/** global config parser object used during config parsing */ 675extern struct config_parser_state* cfg_parser; 676/** init lex state */ 677void init_cfg_parse(void); 678/** lex in file */ 679extern FILE* ub_c_in; 680/** lex out file */ 681extern FILE* ub_c_out; 682/** the yacc lex generated parse function */ 683int ub_c_parse(void); 684/** the lexer function */ 685int ub_c_lex(void); 686/** wrap function */ 687int ub_c_wrap(void); 688/** parsing helpers: print error with file and line numbers */ 689void ub_c_error(const char* msg); 690/** parsing helpers: print error with file and line numbers */ 691void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2); 692 693#ifdef UB_ON_WINDOWS 694/** 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. 700 */ 701char* w_lookup_reg_str(const char* key, const char* name); 702#endif /* UB_ON_WINDOWS */ 703 704#endif /* UTIL_CONFIG_FILE_H */
|