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 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. 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 123 /** the target fetch policy for the iterator */ 124 char* target_fetch_policy; 125 126 /** automatic interface for incoming messages. Uses ipv6 remapping, 127 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */ 128 int if_automatic; 129 /** SO_RCVBUF size to set on port 53 UDP socket */ 130 size_t so_rcvbuf; 131 /** SO_SNDBUF size to set on port 53 UDP socket */ 132 size_t so_sndbuf; 133 134 /** number of interfaces to open. If 0 default all interfaces. */ 135 int num_ifs; 136 /** interface description strings (IP addresses) */ 137 char **ifs; 138 139 /** number of outgoing interfaces to open. 140 * If 0 default all interfaces. */ 141 int num_out_ifs; 142 /** outgoing interface description strings (IP addresses) */ 143 char **out_ifs; 144 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; 157 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) */ 163 int harden_glue; 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 */ 179 int max_ttl; 180 /** the number of seconds minimum TTL used for RRsets and messages */ 181 int min_ttl; 182 /** if prefetching of messages should be performed. */ 183 int prefetch; 184 /** if prefetching of DNSKEYs should be performed. */ 185 int prefetch_key; 186 187 /** chrootdir, if not "" or chroot will be done */ 188 char* chrootdir; 189 /** username to change to, if not "". */ 190 char* username; 191 /** working directory */ 192 char* directory; 193 /** filename to log to. */ 194 char* logfile; 195 /** pidfile to write pid to. */ 196 char* pidfile; 197 198 /** should log messages be sent to syslogd */ 199 int use_syslog; 200 /** log timestamp in ascii UTC */ 201 int log_time_ascii; 202 /** log queries with one line per query */ 203 int log_queries; 204 205 /** do not report identity (id.server, hostname.bind) */ 206 int hide_identity; 207 /** do not report version (version.server, version.bind) */ 208 int hide_version; 209 /** identity, hostname is returned if "". */ 210 char* identity; 211 /** version, package version returned if "". */ 212 char* version; 213 214 /** the module configuration string */ 215 char* module_conf; 216 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; 231 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 */ 239 int bogus_ttl; 240 /** should validator clean additional section for secure msgs */ 241 int val_clean_additional; 242 /** log bogus messages by the validator */ 243 int val_log_level; 244 /** squelch val_log_level to log - this is library goes to callback */ 245 int val_log_squelch; 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 */ 249 int ignore_cd; 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; 258 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; 265 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; 272 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 */ 278 int 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; 287 288 /** Python script file */ 289 char* python_script; 290 291 /** daemonize, i.e. fork into the background. */ 292 int do_daemonize; 293 294 /* minimal response when positive answer */ 295 int minimal_responses; 296 297 /* RRSet roundrobin */ 298 int rrset_roundrobin; 299 }; 300 301 /** 302 * Stub config options 303 */ 304 struct config_stub { 305 /** next in list */ 306 struct config_stub* next; 307 /** domain name (in text) of the stub apex domain */ 308 char* name; 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 */ 314 int isprime; 315 /** if forward-first is set (failover to without if fails) */ 316 int isfirst; 317 }; 318 319 /** 320 * List of strings for config options 321 */ 322 struct config_strlist { 323 /** next item in list */ 324 struct config_strlist* next; 325 /** config option string */ 326 char* str; 327 }; 328 329 /** 330 * List of two strings for config options 331 */ 332 struct config_str2list { 333 /** next item in list */ 334 struct config_str2list* next; 335 /** first string */ 336 char* str; 337 /** second string */ 338 char* str2; 339 }; 340 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; 347 }; 348 349 /** 350 * Create config file structure. Filled with default values. 351 * @return: the new structure or NULL on memory error. 352 */ 353 struct config_file* config_create(void); 354 355 /** 356 * Create config file structure for library use. Filled with default values. 357 * @return: the new structure or NULL on memory error. 358 */ 359 struct config_file* config_create_forlib(void); 360 361 /** 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 367 * file not found. 368 */ 369 int config_read(struct config_file* config, const char* filename, 370 const char* chroot); 371 372 /** 373 * Destroy the config file structure. 374 * @param config: to delete. 375 */ 376 void config_delete(struct config_file* config); 377 378 /** 379 * Apply config to global constants; this routine is called in single thread. 380 * @param config: to apply. Side effect: global constants change. 381 */ 382 void config_apply(struct config_file* config); 383 384 /** 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). 391 */ 392 int config_set_option(struct config_file* config, const char* option, 393 const char* value); 394 395 /** 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). 403 */ 404 int config_get_option(struct config_file* cfg, const char* opt, 405 void (*func)(char*,void*), void* arg); 406 407 /** 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. 413 */ 414 int config_get_option_list(struct config_file* cfg, const char* opt, 415 struct config_strlist** list); 416 417 /** 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. 423 */ 424 int config_get_option_collate(struct config_file* cfg, const char* opt, 425 char** str); 426 427 /** 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. 431 */ 432 void config_print_func(char* line, void* arg); 433 434 /** 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. 438 */ 439 void config_collate_func(char* line, void* arg); 440 441 /** 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. 445 */ 446 char* config_collate_cat(struct config_strlist* list); 447 448 /** 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. 453 */ 454 int cfg_strlist_append(struct config_strlist_head* list, char* item); 455 456 /** 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. 461 */ 462 int cfg_strlist_insert(struct config_strlist** head, char* item); 463 464 /** 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. 470 */ 471 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2); 472 473 /** 474 * Delete items in config string list. 475 * @param list: list. 476 */ 477 void config_delstrlist(struct config_strlist* list); 478 479 /** 480 * Delete items in config double string list. 481 * @param list: list. 482 */ 483 void config_deldblstrlist(struct config_str2list* list); 484 485 /** 486 * Delete items in config stub list. 487 * @param list: list. 488 */ 489 void config_delstubs(struct config_stub* list); 490 491 /** 492 * Convert 14digit to time value 493 * @param str: string of 14 digits 494 * @return time value or 0 for error. 495 */ 496 uint32_t cfg_convert_timeval(const char* str); 497 498 /** 499 * Count number of values in the string. 500 * format ::= (sp num)+ sp 501 * num ::= [-](0-9)+ 502 * sp ::= (space|tab)* 503 * 504 * @param str: string 505 * @return: 0 on parse error, or empty string, else 506 * number of integer values in the string. 507 */ 508 int cfg_count_numbers(const char* str); 509 510 /** 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. 514 * @param str: string 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 517 * is logged). 518 */ 519 int cfg_parse_memsize(const char* str, size_t* res); 520 521 /** 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 526 */ 527 int cfg_parse_local_zone(struct config_file* cfg, const char* val); 528 529 /** 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 536 * is logged). 537 */ 538 int cfg_mark_ports(const char* str, int allow, int* avail, int num); 539 540 /** 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. 545 */ 546 int cfg_condense_ports(struct config_file* cfg, int** avail); 547 548 /** 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. 553 */ 554 int cfg_scan_ports(int* avail, int num); 555 556 /** 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. 564 */ 565 char* fname_after_chroot(const char* fname, struct config_file* cfg, 566 int use_chdir); 567 568 /** 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" 572 */ 573 char* cfg_ptr_reverse(char* str); 574 575 /** 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. 580 */ 581 void errinf(struct module_qstate* qstate, const char* str); 582 583 /** 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. 590 */ 591 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin); 592 593 /** 594 * Append text to error info: for RRset name type class 595 * @param qstate: query state. 596 * @param rr: rrset_key. 597 */ 598 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr); 599 600 /** 601 * Append text to error info: str dname 602 * @param qstate: query state. 603 * @param str: explanation string 604 * @param dname: the dname. 605 */ 606 void errinf_dname(struct module_qstate* qstate, const char* str, 607 uint8_t* dname); 608 609 /** 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. 614 */ 615 char* errinf_to_str(struct module_qstate* qstate); 616 617 /** 618 * Used during options parsing 619 */ 620 struct config_parser_state { 621 /** name of file being parser */ 622 char* filename; 623 /** line number in the file, starts at 1 */ 624 int line; 625 /** number of errors encountered */ 626 int errors; 627 /** the result of parsing is stored here. */ 628 struct config_file* cfg; 629 /** the current chroot dir (or NULL if none) */ 630 const char* chroot; 631 }; 632 633 /** global config parser object used during config parsing */ 634 extern struct config_parser_state* cfg_parser; 635 /** lex in file */ 636 extern FILE* ub_c_in; 637 /** lex out file */ 638 extern FILE* ub_c_out; 639 /** the yacc lex generated parse function */ 640 int ub_c_parse(void); 641 /** the lexer function */ 642 int ub_c_lex(void); 643 /** wrap function */ 644 int ub_c_wrap(void); 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); 649 650 #ifdef UB_ON_WINDOWS 651 /** 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. 657 */ 658 char* w_lookup_reg_str(const char* key, const char* name); 659 #endif /* UB_ON_WINDOWS */ 660 661 #endif /* UTIL_CONFIG_FILE_H */ 662