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 44 struct config_stub; 45 struct config_strlist; 46 struct config_str2list; 47 struct module_qstate; 48 struct sock_list; 49 struct ub_packed_rrset_key; 50 51 /** 52 * The configuration options. 53 * Strings are malloced. 54 */ 55 struct 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 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 */ 282 int 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; 291 292 /** Python script file */ 293 char* python_script; 294 295 /** daemonize, i.e. fork into the background. */ 296 int do_daemonize; 297 298 /* minimal response when positive answer */ 299 int minimal_responses; 300 301 /* RRSet roundrobin */ 302 int rrset_roundrobin; 303 304 /* maximum UDP response size */ 305 size_t max_udp_size; 306 }; 307 308 /** 309 * Stub config options 310 */ 311 struct config_stub { 312 /** next in list */ 313 struct config_stub* next; 314 /** domain name (in text) of the stub apex domain */ 315 char* name; 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 */ 321 int isprime; 322 /** if forward-first is set (failover to without if fails) */ 323 int isfirst; 324 }; 325 326 /** 327 * List of strings for config options 328 */ 329 struct config_strlist { 330 /** next item in list */ 331 struct config_strlist* next; 332 /** config option string */ 333 char* str; 334 }; 335 336 /** 337 * List of two strings for config options 338 */ 339 struct config_str2list { 340 /** next item in list */ 341 struct config_str2list* next; 342 /** first string */ 343 char* str; 344 /** second string */ 345 char* str2; 346 }; 347 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; 354 }; 355 356 /** 357 * Create config file structure. Filled with default values. 358 * @return: the new structure or NULL on memory error. 359 */ 360 struct config_file* config_create(void); 361 362 /** 363 * Create config file structure for library use. Filled with default values. 364 * @return: the new structure or NULL on memory error. 365 */ 366 struct config_file* config_create_forlib(void); 367 368 /** 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 374 * file not found. 375 */ 376 int config_read(struct config_file* config, const char* filename, 377 const char* chroot); 378 379 /** 380 * Destroy the config file structure. 381 * @param config: to delete. 382 */ 383 void config_delete(struct config_file* config); 384 385 /** 386 * Apply config to global constants; this routine is called in single thread. 387 * @param config: to apply. Side effect: global constants change. 388 */ 389 void config_apply(struct config_file* config); 390 391 /** 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). 398 */ 399 int config_set_option(struct config_file* config, const char* option, 400 const char* value); 401 402 /** 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). 410 */ 411 int config_get_option(struct config_file* cfg, const char* opt, 412 void (*func)(char*,void*), void* arg); 413 414 /** 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. 420 */ 421 int config_get_option_list(struct config_file* cfg, const char* opt, 422 struct config_strlist** list); 423 424 /** 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. 430 */ 431 int config_get_option_collate(struct config_file* cfg, const char* opt, 432 char** str); 433 434 /** 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. 438 */ 439 void config_print_func(char* line, void* arg); 440 441 /** 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. 445 */ 446 void config_collate_func(char* line, void* arg); 447 448 /** 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. 452 */ 453 char* config_collate_cat(struct config_strlist* list); 454 455 /** 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. 460 */ 461 int cfg_strlist_append(struct config_strlist_head* list, char* item); 462 463 /** 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. 468 */ 469 int cfg_strlist_insert(struct config_strlist** head, char* item); 470 471 /** 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. 477 */ 478 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2); 479 480 /** 481 * Delete items in config string list. 482 * @param list: list. 483 */ 484 void config_delstrlist(struct config_strlist* list); 485 486 /** 487 * Delete items in config double string list. 488 * @param list: list. 489 */ 490 void config_deldblstrlist(struct config_str2list* list); 491 492 /** 493 * Delete items in config stub list. 494 * @param list: list. 495 */ 496 void config_delstubs(struct config_stub* list); 497 498 /** 499 * Convert 14digit to time value 500 * @param str: string of 14 digits 501 * @return time value or 0 for error. 502 */ 503 time_t cfg_convert_timeval(const char* str); 504 505 /** 506 * Count number of values in the string. 507 * format ::= (sp num)+ sp 508 * num ::= [-](0-9)+ 509 * sp ::= (space|tab)* 510 * 511 * @param str: string 512 * @return: 0 on parse error, or empty string, else 513 * number of integer values in the string. 514 */ 515 int cfg_count_numbers(const char* str); 516 517 /** 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. 521 * @param str: string 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 524 * is logged). 525 */ 526 int cfg_parse_memsize(const char* str, size_t* res); 527 528 /** 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 533 */ 534 int cfg_parse_local_zone(struct config_file* cfg, const char* val); 535 536 /** 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 543 * is logged). 544 */ 545 int cfg_mark_ports(const char* str, int allow, int* avail, int num); 546 547 /** 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. 552 */ 553 int cfg_condense_ports(struct config_file* cfg, int** avail); 554 555 /** 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. 560 */ 561 int cfg_scan_ports(int* avail, int num); 562 563 /** 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. 571 */ 572 char* fname_after_chroot(const char* fname, struct config_file* cfg, 573 int use_chdir); 574 575 /** 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" 579 */ 580 char* cfg_ptr_reverse(char* str); 581 582 /** 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. 587 */ 588 void errinf(struct module_qstate* qstate, const char* str); 589 590 /** 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. 597 */ 598 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin); 599 600 /** 601 * Append text to error info: for RRset name type class 602 * @param qstate: query state. 603 * @param rr: rrset_key. 604 */ 605 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr); 606 607 /** 608 * Append text to error info: str dname 609 * @param qstate: query state. 610 * @param str: explanation string 611 * @param dname: the dname. 612 */ 613 void errinf_dname(struct module_qstate* qstate, const char* str, 614 uint8_t* dname); 615 616 /** 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. 621 */ 622 char* errinf_to_str(struct module_qstate* qstate); 623 624 /** 625 * Used during options parsing 626 */ 627 struct config_parser_state { 628 /** name of file being parser */ 629 char* filename; 630 /** line number in the file, starts at 1 */ 631 int line; 632 /** number of errors encountered */ 633 int errors; 634 /** the result of parsing is stored here. */ 635 struct config_file* cfg; 636 /** the current chroot dir (or NULL if none) */ 637 const char* chroot; 638 }; 639 640 /** global config parser object used during config parsing */ 641 extern struct config_parser_state* cfg_parser; 642 /** parsing helpers: print error with file and line numbers */ 643 void ub_c_error(const char* msg); 644 /** parsing helpers: print error with file and line numbers */ 645 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2); 646 647 #ifdef UB_ON_WINDOWS 648 /** 649 * Obtain registry string (if it exists). 650 * @param key: key string 651 * @param name: name of value to fetch. 652 * @return malloced string with the result or NULL if it did not 653 * exist on an error (logged with log_err) was encountered. 654 */ 655 char* w_lookup_reg_str(const char* key, const char* name); 656 #endif /* UB_ON_WINDOWS */ 657 658 #endif /* UTIL_CONFIG_FILE_H */ 659