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 295 /** 296 * Stub config options 297 */ 298 struct config_stub { 299 /** next in list */ 300 struct config_stub* next; 301 /** domain name (in text) of the stub apex domain */ 302 char* name; 303 /** list of stub nameserver hosts (domain name) */ 304 struct config_strlist* hosts; 305 /** list of stub nameserver addresses (IP address) */ 306 struct config_strlist* addrs; 307 /** if stub-prime is set */ 308 int isprime; 309 }; 310 311 /** 312 * List of strings for config options 313 */ 314 struct config_strlist { 315 /** next item in list */ 316 struct config_strlist* next; 317 /** config option string */ 318 char* str; 319 }; 320 321 /** 322 * List of two strings for config options 323 */ 324 struct config_str2list { 325 /** next item in list */ 326 struct config_str2list* next; 327 /** first string */ 328 char* str; 329 /** second string */ 330 char* str2; 331 }; 332 333 /** List head for strlist processing, used for append operation. */ 334 struct config_strlist_head { 335 /** first in list of text items */ 336 struct config_strlist* first; 337 /** last in list of text items */ 338 struct config_strlist* last; 339 }; 340 341 /** 342 * Create config file structure. Filled with default values. 343 * @return: the new structure or NULL on memory error. 344 */ 345 struct config_file* config_create(void); 346 347 /** 348 * Create config file structure for library use. Filled with default values. 349 * @return: the new structure or NULL on memory error. 350 */ 351 struct config_file* config_create_forlib(void); 352 353 /** 354 * Read the config file from the specified filename. 355 * @param config: where options are stored into, must be freshly created. 356 * @param filename: name of configfile. If NULL nothing is done. 357 * @param chroot: if not NULL, the chroot dir currently in use (for include). 358 * @return: false on error. In that case errno is set, ENOENT means 359 * file not found. 360 */ 361 int config_read(struct config_file* config, const char* filename, 362 const char* chroot); 363 364 /** 365 * Destroy the config file structure. 366 * @param config: to delete. 367 */ 368 void config_delete(struct config_file* config); 369 370 /** 371 * Apply config to global constants; this routine is called in single thread. 372 * @param config: to apply. Side effect: global constants change. 373 */ 374 void config_apply(struct config_file* config); 375 376 /** 377 * Set the given keyword to the given value. 378 * @param config: where to store config 379 * @param option: option name, including the ':' character. 380 * @param value: value, this string is copied if needed, or parsed. 381 * The caller owns the value string. 382 * @return 0 on error (malloc or syntax error). 383 */ 384 int config_set_option(struct config_file* config, const char* option, 385 const char* value); 386 387 /** 388 * Call print routine for the given option. 389 * @param cfg: config. 390 * @param opt: option name without trailing :. 391 * This is different from config_set_option. 392 * @param func: print func, called as (str, arg) for every data element. 393 * @param arg: user argument for print func. 394 * @return false if the option name is not supported (syntax error). 395 */ 396 int config_get_option(struct config_file* cfg, const char* opt, 397 void (*func)(char*,void*), void* arg); 398 399 /** 400 * Get an option and return strlist 401 * @param cfg: config file 402 * @param opt: option name. 403 * @param list: list is returned here. malloced, caller must free it. 404 * @return 0=OK, 1=syntax error, 2=malloc failed. 405 */ 406 int config_get_option_list(struct config_file* cfg, const char* opt, 407 struct config_strlist** list); 408 409 /** 410 * Get an option and collate results into string 411 * @param cfg: config file 412 * @param opt: option name. 413 * @param str: string. malloced, caller must free it. 414 * @return 0=OK, 1=syntax error, 2=malloc failed. 415 */ 416 int config_get_option_collate(struct config_file* cfg, const char* opt, 417 char** str); 418 419 /** 420 * function to print to a file, use as func with config_get_option. 421 * @param line: text to print. \n appended. 422 * @param arg: pass a FILE*, like stdout. 423 */ 424 void config_print_func(char* line, void* arg); 425 426 /** 427 * function to collate the text strings into a strlist_head. 428 * @param line: text to append. 429 * @param arg: pass a strlist_head structure. zeroed on start. 430 */ 431 void config_collate_func(char* line, void* arg); 432 433 /** 434 * take a strlist_head list and return a malloc string. separated with newline. 435 * @param list: strlist first to collate. zeroes return "". 436 * @return NULL on malloc failure. Or if malloc failure happened in strlist. 437 */ 438 char* config_collate_cat(struct config_strlist* list); 439 440 /** 441 * Append text at end of list. 442 * @param list: list head. zeroed at start. 443 * @param item: new item. malloced by caller. if NULL the insertion fails. 444 * @return true on success. 445 */ 446 int cfg_strlist_append(struct config_strlist_head* list, char* item); 447 448 /** 449 * Insert string into strlist. 450 * @param head: pointer to strlist head variable. 451 * @param item: new item. malloced by caller. If NULL the insertion fails. 452 * @return: true on success. 453 */ 454 int cfg_strlist_insert(struct config_strlist** head, char* item); 455 456 /** 457 * Insert string into str2list. 458 * @param head: pointer to str2list head variable. 459 * @param item: new item. malloced by caller. If NULL the insertion fails. 460 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails. 461 * @return: true on success. 462 */ 463 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2); 464 465 /** 466 * Delete items in config string list. 467 * @param list: list. 468 */ 469 void config_delstrlist(struct config_strlist* list); 470 471 /** 472 * Delete items in config double string list. 473 * @param list: list. 474 */ 475 void config_deldblstrlist(struct config_str2list* list); 476 477 /** 478 * Delete items in config stub list. 479 * @param list: list. 480 */ 481 void config_delstubs(struct config_stub* list); 482 483 /** 484 * Convert 14digit to time value 485 * @param str: string of 14 digits 486 * @return time value or 0 for error. 487 */ 488 uint32_t cfg_convert_timeval(const char* str); 489 490 /** 491 * Count number of values in the string. 492 * format ::= (sp num)+ sp 493 * num ::= [-](0-9)+ 494 * sp ::= (space|tab)* 495 * 496 * @param str: string 497 * @return: 0 on parse error, or empty string, else 498 * number of integer values in the string. 499 */ 500 int cfg_count_numbers(const char* str); 501 502 /** 503 * Convert a 'nice' memory or file size into a bytecount 504 * From '100k' to 102400. and so on. Understands kKmMgG. 505 * k=1024, m=1024*1024, g=1024*1024*1024. 506 * @param str: string 507 * @param res: result is stored here, size in bytes. 508 * @return: true if parsed correctly, or 0 on a parse error (and an error 509 * is logged). 510 */ 511 int cfg_parse_memsize(const char* str, size_t* res); 512 513 /** 514 * Parse local-zone directive into two strings and register it in the config. 515 * @param cfg: to put it in. 516 * @param val: argument strings to local-zone, "example.com nodefault". 517 * @return: false on failure 518 */ 519 int cfg_parse_local_zone(struct config_file* cfg, const char* val); 520 521 /** 522 * Mark "number" or "low-high" as available or not in ports array. 523 * @param str: string in input 524 * @param allow: give true if this range is permitted. 525 * @param avail: the array from cfg. 526 * @param num: size of the array (65536). 527 * @return: true if parsed correctly, or 0 on a parse error (and an error 528 * is logged). 529 */ 530 int cfg_mark_ports(const char* str, int allow, int* avail, int num); 531 532 /** 533 * Get a condensed list of ports returned. allocated. 534 * @param cfg: config file. 535 * @param avail: the available ports array is returned here. 536 * @return: number of ports in array or 0 on error. 537 */ 538 int cfg_condense_ports(struct config_file* cfg, int** avail); 539 540 /** 541 * Scan ports available 542 * @param avail: the array from cfg. 543 * @param num: size of the array (65536). 544 * @return the number of ports available for use. 545 */ 546 int cfg_scan_ports(int* avail, int num); 547 548 /** 549 * Convert a filename to full pathname in original filesys 550 * @param fname: the path name to convert. 551 * Must not be null or empty. 552 * @param cfg: config struct for chroot and chdir (if set). 553 * @param use_chdir: if false, only chroot is applied. 554 * @return pointer to malloced buffer which is: [chroot][chdir]fname 555 * or NULL on malloc failure. 556 */ 557 char* fname_after_chroot(const char* fname, struct config_file* cfg, 558 int use_chdir); 559 560 /** 561 * Convert a ptr shorthand into a full reverse-notation PTR record. 562 * @param str: input string, "IP name" 563 * @return: malloced string "reversed-ip-name PTR name" 564 */ 565 char* cfg_ptr_reverse(char* str); 566 567 /** 568 * Append text to the error info for validation. 569 * @param qstate: query state. 570 * @param str: copied into query region and appended. 571 * Failures to allocate are logged. 572 */ 573 void errinf(struct module_qstate* qstate, const char* str); 574 575 /** 576 * Append text to error info: from 1.2.3.4 577 * @param qstate: query state. 578 * @param origin: sock list with origin of trouble. 579 * Every element added. 580 * If NULL: nothing is added. 581 * if 0len element: 'from cache' is added. 582 */ 583 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin); 584 585 /** 586 * Append text to error info: for RRset name type class 587 * @param qstate: query state. 588 * @param rr: rrset_key. 589 */ 590 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr); 591 592 /** 593 * Append text to error info: str dname 594 * @param qstate: query state. 595 * @param str: explanation string 596 * @param dname: the dname. 597 */ 598 void errinf_dname(struct module_qstate* qstate, const char* str, 599 uint8_t* dname); 600 601 /** 602 * Create error info in string 603 * @param qstate: query state. 604 * @return string or NULL on malloc failure (already logged). 605 * This string is malloced and has to be freed by caller. 606 */ 607 char* errinf_to_str(struct module_qstate* qstate); 608 609 /** 610 * Used during options parsing 611 */ 612 struct config_parser_state { 613 /** name of file being parser */ 614 char* filename; 615 /** line number in the file, starts at 1 */ 616 int line; 617 /** number of errors encountered */ 618 int errors; 619 /** the result of parsing is stored here. */ 620 struct config_file* cfg; 621 /** the current chroot dir (or NULL if none) */ 622 const char* chroot; 623 }; 624 625 /** global config parser object used during config parsing */ 626 extern struct config_parser_state* cfg_parser; 627 /** parsing helpers: print error with file and line numbers */ 628 void ub_c_error(const char* msg); 629 /** parsing helpers: print error with file and line numbers */ 630 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2); 631 632 #endif /* UTIL_CONFIG_FILE_H */ 633