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 config_strbytelist; 48 struct module_qstate; 49 struct sock_list; 50 struct ub_packed_rrset_key; 51 52 /** 53 * The configuration options. 54 * Strings are malloced. 55 */ 56 struct config_file { 57 /** verbosity level as specified in the config file */ 58 int verbosity; 59 60 /** statistics interval (in seconds) */ 61 int stat_interval; 62 /** if false, statistics values are reset after printing them */ 63 int stat_cumulative; 64 /** if true, the statistics are kept in greater detail */ 65 int stat_extended; 66 67 /** number of threads to create */ 68 int num_threads; 69 70 /** port on which queries are answered. */ 71 int port; 72 /** do ip4 query support. */ 73 int do_ip4; 74 /** do ip6 query support. */ 75 int do_ip6; 76 /** do udp query support. */ 77 int do_udp; 78 /** do tcp query support. */ 79 int do_tcp; 80 /** tcp upstream queries (no UDP upstream queries) */ 81 int tcp_upstream; 82 /** maximum segment size of tcp socket which queries are answered */ 83 int tcp_mss; 84 /** maximum segment size of tcp socket for outgoing queries */ 85 int outgoing_tcp_mss; 86 87 /** private key file for dnstcp-ssl service (enabled if not NULL) */ 88 char* ssl_service_key; 89 /** public key file for dnstcp-ssl service */ 90 char* ssl_service_pem; 91 /** port on which to provide ssl service */ 92 int ssl_port; 93 /** if outgoing tcp connections use SSL */ 94 int ssl_upstream; 95 96 /** outgoing port range number of ports (per thread) */ 97 int outgoing_num_ports; 98 /** number of outgoing tcp buffers per (per thread) */ 99 size_t outgoing_num_tcp; 100 /** number of incoming tcp buffers per (per thread) */ 101 size_t incoming_num_tcp; 102 /** allowed udp port numbers, array with 0 if not allowed */ 103 int* outgoing_avail_ports; 104 105 /** EDNS buffer size to use */ 106 size_t edns_buffer_size; 107 /** number of bytes buffer size for DNS messages */ 108 size_t msg_buffer_size; 109 /** size of the message cache */ 110 size_t msg_cache_size; 111 /** slabs in the message cache. */ 112 size_t msg_cache_slabs; 113 /** number of queries every thread can service */ 114 size_t num_queries_per_thread; 115 /** number of msec to wait before items can be jostled out */ 116 size_t jostle_time; 117 /** size of the rrset cache */ 118 size_t rrset_cache_size; 119 /** slabs in the rrset cache */ 120 size_t rrset_cache_slabs; 121 /** host cache ttl in seconds */ 122 int host_ttl; 123 /** number of slabs in the infra host cache */ 124 size_t infra_cache_slabs; 125 /** max number of hosts in the infra cache */ 126 size_t infra_cache_numhosts; 127 /** min value for infra cache rtt */ 128 int infra_cache_min_rtt; 129 /** delay close of udp-timeouted ports, if 0 no delayclose. in msec */ 130 int delay_close; 131 132 /** the target fetch policy for the iterator */ 133 char* target_fetch_policy; 134 135 /** automatic interface for incoming messages. Uses ipv6 remapping, 136 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */ 137 int if_automatic; 138 /** SO_RCVBUF size to set on port 53 UDP socket */ 139 size_t so_rcvbuf; 140 /** SO_SNDBUF size to set on port 53 UDP socket */ 141 size_t so_sndbuf; 142 /** SO_REUSEPORT requested on port 53 sockets */ 143 int so_reuseport; 144 /** IP_TRANSPARENT socket option requested on port 53 sockets */ 145 int ip_transparent; 146 /** IP_FREEBIND socket option request on port 53 sockets */ 147 int ip_freebind; 148 149 /** number of interfaces to open. If 0 default all interfaces. */ 150 int num_ifs; 151 /** interface description strings (IP addresses) */ 152 char **ifs; 153 154 /** number of outgoing interfaces to open. 155 * If 0 default all interfaces. */ 156 int num_out_ifs; 157 /** outgoing interface description strings (IP addresses) */ 158 char **out_ifs; 159 160 /** the root hints */ 161 struct config_strlist* root_hints; 162 /** the stub definitions, linked list */ 163 struct config_stub* stubs; 164 /** the forward zone definitions, linked list */ 165 struct config_stub* forwards; 166 /** list of donotquery addresses, linked list */ 167 struct config_strlist* donotqueryaddrs; 168 /** list of access control entries, linked list */ 169 struct config_str2list* acls; 170 /** use default localhost donotqueryaddr entries */ 171 int donotquery_localhost; 172 173 /** harden against very small edns buffer sizes */ 174 int harden_short_bufsize; 175 /** harden against very large query sizes */ 176 int harden_large_queries; 177 /** harden against spoofed glue (out of zone data) */ 178 int harden_glue; 179 /** harden against receiving no DNSSEC data for trust anchor */ 180 int harden_dnssec_stripped; 181 /** harden against queries that fall under known nxdomain names */ 182 int harden_below_nxdomain; 183 /** harden the referral path, query for NS,A,AAAA and validate */ 184 int harden_referral_path; 185 /** harden against algorithm downgrade */ 186 int harden_algo_downgrade; 187 /** use 0x20 bits in query as random ID bits */ 188 int use_caps_bits_for_id; 189 /** 0x20 whitelist, domains that do not use capsforid */ 190 struct config_strlist* caps_whitelist; 191 /** strip away these private addrs from answers, no DNS Rebinding */ 192 struct config_strlist* private_address; 193 /** allow domain (and subdomains) to use private address space */ 194 struct config_strlist* private_domain; 195 /** what threshold for unwanted action. */ 196 size_t unwanted_threshold; 197 /** the number of seconds maximal TTL used for RRsets and messages */ 198 int max_ttl; 199 /** the number of seconds minimum TTL used for RRsets and messages */ 200 int min_ttl; 201 /** the number of seconds maximal negative TTL for SOA in auth */ 202 int max_negative_ttl; 203 /** if prefetching of messages should be performed. */ 204 int prefetch; 205 /** if prefetching of DNSKEYs should be performed. */ 206 int prefetch_key; 207 208 /** chrootdir, if not "" or chroot will be done */ 209 char* chrootdir; 210 /** username to change to, if not "". */ 211 char* username; 212 /** working directory */ 213 char* directory; 214 /** filename to log to. */ 215 char* logfile; 216 /** pidfile to write pid to. */ 217 char* pidfile; 218 219 /** should log messages be sent to syslogd */ 220 int use_syslog; 221 /** log timestamp in ascii UTC */ 222 int log_time_ascii; 223 /** log queries with one line per query */ 224 int log_queries; 225 226 /** do not report identity (id.server, hostname.bind) */ 227 int hide_identity; 228 /** do not report version (version.server, version.bind) */ 229 int hide_version; 230 /** identity, hostname is returned if "". */ 231 char* identity; 232 /** version, package version returned if "". */ 233 char* version; 234 235 /** the module configuration string */ 236 char* module_conf; 237 238 /** files with trusted DS and DNSKEYs in zonefile format, list */ 239 struct config_strlist* trust_anchor_file_list; 240 /** list of trustanchor keys, linked list */ 241 struct config_strlist* trust_anchor_list; 242 /** files with 5011 autotrust tracked keys */ 243 struct config_strlist* auto_trust_anchor_file_list; 244 /** files with trusted DNSKEYs in named.conf format, list */ 245 struct config_strlist* trusted_keys_file_list; 246 /** DLV anchor file */ 247 char* dlv_anchor_file; 248 /** DLV anchor inline */ 249 struct config_strlist* dlv_anchor_list; 250 /** insecure domain list */ 251 struct config_strlist* domain_insecure; 252 253 /** if not 0, this value is the validation date for RRSIGs */ 254 int32_t val_date_override; 255 /** the minimum for signature clock skew */ 256 int32_t val_sig_skew_min; 257 /** the maximum for signature clock skew */ 258 int32_t val_sig_skew_max; 259 /** this value sets the number of seconds before revalidating bogus */ 260 int bogus_ttl; 261 /** should validator clean additional section for secure msgs */ 262 int val_clean_additional; 263 /** log bogus messages by the validator */ 264 int val_log_level; 265 /** squelch val_log_level to log - this is library goes to callback */ 266 int val_log_squelch; 267 /** should validator allow bogus messages to go through */ 268 int val_permissive_mode; 269 /** ignore the CD flag in incoming queries and refuse them bogus data */ 270 int ignore_cd; 271 /** nsec3 maximum iterations per key size, string */ 272 char* val_nsec3_key_iterations; 273 /** autotrust add holddown time, in seconds */ 274 unsigned int add_holddown; 275 /** autotrust del holddown time, in seconds */ 276 unsigned int del_holddown; 277 /** autotrust keep_missing time, in seconds. 0 is forever. */ 278 unsigned int keep_missing; 279 /** permit small holddown values, allowing 5011 rollover very fast */ 280 int permit_small_holddown; 281 282 /** size of the key cache */ 283 size_t key_cache_size; 284 /** slabs in the key cache. */ 285 size_t key_cache_slabs; 286 /** size of the neg cache */ 287 size_t neg_cache_size; 288 289 /** local zones config */ 290 struct config_str2list* local_zones; 291 /** local zones nodefault list */ 292 struct config_strlist* local_zones_nodefault; 293 /** local data RRs configured */ 294 struct config_strlist* local_data; 295 /** unblock lan zones (reverse lookups for AS112 zones) */ 296 int unblock_lan_zones; 297 /** insecure lan zones (don't validate AS112 zones) */ 298 int insecure_lan_zones; 299 /** list of zonename, tagbitlist */ 300 struct config_strbytelist* local_zone_tags; 301 /** tag list, array with tagname[i] is malloced string */ 302 char** tagname; 303 /** number of items in the taglist */ 304 int num_tags; 305 306 /** remote control section. enable toggle. */ 307 int remote_control_enable; 308 /** the interfaces the remote control should listen on */ 309 struct config_strlist* control_ifs; 310 /** port number for the control port */ 311 int control_port; 312 /** use certificates for remote control */ 313 int remote_control_use_cert; 314 /** private key file for server */ 315 char* server_key_file; 316 /** certificate file for server */ 317 char* server_cert_file; 318 /** private key file for unbound-control */ 319 char* control_key_file; 320 /** certificate file for unbound-control */ 321 char* control_cert_file; 322 323 /** Python script file */ 324 char* python_script; 325 326 /** daemonize, i.e. fork into the background. */ 327 int do_daemonize; 328 329 /* minimal response when positive answer */ 330 int minimal_responses; 331 332 /* RRSet roundrobin */ 333 int rrset_roundrobin; 334 335 /* maximum UDP response size */ 336 size_t max_udp_size; 337 338 /* DNS64 prefix */ 339 char* dns64_prefix; 340 341 /* Synthetize all AAAA record despite the presence of an authoritative one */ 342 int dns64_synthall; 343 344 /** true to enable dnstap support */ 345 int dnstap; 346 /** dnstap socket path */ 347 char* dnstap_socket_path; 348 /** true to send "identity" via dnstap */ 349 int dnstap_send_identity; 350 /** true to send "version" via dnstap */ 351 int dnstap_send_version; 352 /** dnstap "identity", hostname is used if "". */ 353 char* dnstap_identity; 354 /** dnstap "version", package version is used if "". */ 355 char* dnstap_version; 356 357 /** true to log dnstap RESOLVER_QUERY message events */ 358 int dnstap_log_resolver_query_messages; 359 /** true to log dnstap RESOLVER_RESPONSE message events */ 360 int dnstap_log_resolver_response_messages; 361 /** true to log dnstap CLIENT_QUERY message events */ 362 int dnstap_log_client_query_messages; 363 /** true to log dnstap CLIENT_RESPONSE message events */ 364 int dnstap_log_client_response_messages; 365 /** true to log dnstap FORWARDER_QUERY message events */ 366 int dnstap_log_forwarder_query_messages; 367 /** true to log dnstap FORWARDER_RESPONSE message events */ 368 int dnstap_log_forwarder_response_messages; 369 370 /** true to disable DNSSEC lameness check in iterator */ 371 int disable_dnssec_lame_check; 372 373 /** ratelimit 0 is off, otherwise qps (unless overridden) */ 374 int ratelimit; 375 /** number of slabs for ratelimit cache */ 376 size_t ratelimit_slabs; 377 /** memory size in bytes for ratelimit cache */ 378 size_t ratelimit_size; 379 /** ratelimits for domain (exact match) */ 380 struct config_str2list* ratelimit_for_domain; 381 /** ratelimits below domain */ 382 struct config_str2list* ratelimit_below_domain; 383 /** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */ 384 int ratelimit_factor; 385 /** minimise outgoing QNAME and hide original QTYPE if possible */ 386 int qname_minimisation; 387 }; 388 389 /** from cfg username, after daemonise setup performed */ 390 extern uid_t cfg_uid; 391 /** from cfg username, after daemonise setup performed */ 392 extern gid_t cfg_gid; 393 /** debug and enable small timeouts */ 394 extern int autr_permit_small_holddown; 395 396 /** 397 * Stub config options 398 */ 399 struct config_stub { 400 /** next in list */ 401 struct config_stub* next; 402 /** domain name (in text) of the stub apex domain */ 403 char* name; 404 /** list of stub nameserver hosts (domain name) */ 405 struct config_strlist* hosts; 406 /** list of stub nameserver addresses (IP address) */ 407 struct config_strlist* addrs; 408 /** if stub-prime is set */ 409 int isprime; 410 /** if forward-first is set (failover to without if fails) */ 411 int isfirst; 412 }; 413 414 /** 415 * List of strings for config options 416 */ 417 struct config_strlist { 418 /** next item in list */ 419 struct config_strlist* next; 420 /** config option string */ 421 char* str; 422 }; 423 424 /** 425 * List of two strings for config options 426 */ 427 struct config_str2list { 428 /** next item in list */ 429 struct config_str2list* next; 430 /** first string */ 431 char* str; 432 /** second string */ 433 char* str2; 434 }; 435 436 /** 437 * List of string, bytestring for config options 438 */ 439 struct config_strbytelist { 440 /** next item in list */ 441 struct config_strbytelist* next; 442 /** first string */ 443 char* str; 444 /** second bytestring */ 445 uint8_t* str2; 446 size_t str2len; 447 }; 448 449 /** List head for strlist processing, used for append operation. */ 450 struct config_strlist_head { 451 /** first in list of text items */ 452 struct config_strlist* first; 453 /** last in list of text items */ 454 struct config_strlist* last; 455 }; 456 457 /** 458 * Create config file structure. Filled with default values. 459 * @return: the new structure or NULL on memory error. 460 */ 461 struct config_file* config_create(void); 462 463 /** 464 * Create config file structure for library use. Filled with default values. 465 * @return: the new structure or NULL on memory error. 466 */ 467 struct config_file* config_create_forlib(void); 468 469 /** 470 * Read the config file from the specified filename. 471 * @param config: where options are stored into, must be freshly created. 472 * @param filename: name of configfile. If NULL nothing is done. 473 * @param chroot: if not NULL, the chroot dir currently in use (for include). 474 * @return: false on error. In that case errno is set, ENOENT means 475 * file not found. 476 */ 477 int config_read(struct config_file* config, const char* filename, 478 const char* chroot); 479 480 /** 481 * Destroy the config file structure. 482 * @param config: to delete. 483 */ 484 void config_delete(struct config_file* config); 485 486 /** 487 * Apply config to global constants; this routine is called in single thread. 488 * @param config: to apply. Side effect: global constants change. 489 */ 490 void config_apply(struct config_file* config); 491 492 /** 493 * Find username, sets cfg_uid and cfg_gid. 494 * @param config: the config structure. 495 */ 496 void config_lookup_uid(struct config_file* config); 497 498 /** 499 * Set the given keyword to the given value. 500 * @param config: where to store config 501 * @param option: option name, including the ':' character. 502 * @param value: value, this string is copied if needed, or parsed. 503 * The caller owns the value string. 504 * @return 0 on error (malloc or syntax error). 505 */ 506 int config_set_option(struct config_file* config, const char* option, 507 const char* value); 508 509 /** 510 * Call print routine for the given option. 511 * @param cfg: config. 512 * @param opt: option name without trailing :. 513 * This is different from config_set_option. 514 * @param func: print func, called as (str, arg) for every data element. 515 * @param arg: user argument for print func. 516 * @return false if the option name is not supported (syntax error). 517 */ 518 int config_get_option(struct config_file* cfg, const char* opt, 519 void (*func)(char*,void*), void* arg); 520 521 /** 522 * Get an option and return strlist 523 * @param cfg: config file 524 * @param opt: option name. 525 * @param list: list is returned here. malloced, caller must free it. 526 * @return 0=OK, 1=syntax error, 2=malloc failed. 527 */ 528 int config_get_option_list(struct config_file* cfg, const char* opt, 529 struct config_strlist** list); 530 531 /** 532 * Get an option and collate results into string 533 * @param cfg: config file 534 * @param opt: option name. 535 * @param str: string. malloced, caller must free it. 536 * @return 0=OK, 1=syntax error, 2=malloc failed. 537 */ 538 int config_get_option_collate(struct config_file* cfg, const char* opt, 539 char** str); 540 541 /** 542 * function to print to a file, use as func with config_get_option. 543 * @param line: text to print. \n appended. 544 * @param arg: pass a FILE*, like stdout. 545 */ 546 void config_print_func(char* line, void* arg); 547 548 /** 549 * function to collate the text strings into a strlist_head. 550 * @param line: text to append. 551 * @param arg: pass a strlist_head structure. zeroed on start. 552 */ 553 void config_collate_func(char* line, void* arg); 554 555 /** 556 * take a strlist_head list and return a malloc string. separated with newline. 557 * @param list: strlist first to collate. zeroes return "". 558 * @return NULL on malloc failure. Or if malloc failure happened in strlist. 559 */ 560 char* config_collate_cat(struct config_strlist* list); 561 562 /** 563 * Append text at end of list. 564 * @param list: list head. zeroed at start. 565 * @param item: new item. malloced by caller. if NULL the insertion fails. 566 * @return true on success. 567 */ 568 int cfg_strlist_append(struct config_strlist_head* list, char* item); 569 570 /** 571 * Insert string into strlist. 572 * @param head: pointer to strlist head variable. 573 * @param item: new item. malloced by caller. If NULL the insertion fails. 574 * @return: true on success. 575 */ 576 int cfg_strlist_insert(struct config_strlist** head, char* item); 577 578 /** 579 * Insert string into str2list. 580 * @param head: pointer to str2list head variable. 581 * @param item: new item. malloced by caller. If NULL the insertion fails. 582 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails. 583 * @return: true on success. 584 */ 585 int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2); 586 587 /** 588 * Insert string into strbytelist. 589 * @param head: pointer to str2list head variable. 590 * @param item: new item. malloced by caller. If NULL the insertion fails. 591 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails. 592 * @param i2len: length of the i2 bytestring. 593 * @return: true on success. 594 */ 595 int cfg_strbytelist_insert(struct config_strbytelist** head, char* item, 596 uint8_t* i2, size_t i2len); 597 598 /** 599 * Find stub in config list, also returns prevptr (for deletion). 600 * @param pp: call routine with pointer to a pointer to the start of the list, 601 * if the stub is found, on exit, the value contains a pointer to the 602 * next pointer that points to the found element (or to the list start 603 * pointer if it is the first element). 604 * @param nm: name of stub to find. 605 * @return: pointer to config_stub if found, or NULL if not found. 606 */ 607 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm); 608 609 /** 610 * Delete items in config string list. 611 * @param list: list. 612 */ 613 void config_delstrlist(struct config_strlist* list); 614 615 /** 616 * Delete items in config double string list. 617 * @param list: list. 618 */ 619 void config_deldblstrlist(struct config_str2list* list); 620 621 /** 622 * Delete a stub item 623 * @param p: stub item 624 */ 625 void config_delstub(struct config_stub* p); 626 627 /** 628 * Delete items in config stub list. 629 * @param list: list. 630 */ 631 void config_delstubs(struct config_stub* list); 632 633 /** 634 * Convert 14digit to time value 635 * @param str: string of 14 digits 636 * @return time value or 0 for error. 637 */ 638 time_t cfg_convert_timeval(const char* str); 639 640 /** 641 * Count number of values in the string. 642 * format ::= (sp num)+ sp 643 * num ::= [-](0-9)+ 644 * sp ::= (space|tab)* 645 * 646 * @param str: string 647 * @return: 0 on parse error, or empty string, else 648 * number of integer values in the string. 649 */ 650 int cfg_count_numbers(const char* str); 651 652 /** 653 * Convert a 'nice' memory or file size into a bytecount 654 * From '100k' to 102400. and so on. Understands kKmMgG. 655 * k=1024, m=1024*1024, g=1024*1024*1024. 656 * @param str: string 657 * @param res: result is stored here, size in bytes. 658 * @return: true if parsed correctly, or 0 on a parse error (and an error 659 * is logged). 660 */ 661 int cfg_parse_memsize(const char* str, size_t* res); 662 663 /** 664 * Add a tag name to the config. It is added at the end with a new ID value. 665 * @param cfg: the config structure. 666 * @param tag: string (which is copied) with the name. 667 * @return: false on alloc failure. 668 */ 669 int config_add_tag(struct config_file* cfg, const char* tag); 670 671 /** 672 * Find tag ID in the tag list. 673 * @param cfg: the config structure. 674 * @param tag: string with tag name to search for. 675 * @return: 0..(num_tags-1) with tag ID, or -1 if tagname is not found. 676 */ 677 int find_tag_id(struct config_file* cfg, const char* tag); 678 679 /** 680 * parse taglist from string into bytestring with bitlist. 681 * @param cfg: the config structure (with tagnames) 682 * @param str: the string to parse. Parse puts 0 bytes in string. 683 * @param listlen: returns length of in bytes. 684 * @return malloced bytes with a bitlist of the tags. or NULL on parse error 685 * or malloc failure. 686 */ 687 uint8_t* config_parse_taglist(struct config_file* cfg, char* str, 688 size_t* listlen); 689 690 /** 691 * convert tag bitlist to a malloced string with tag names. For debug output. 692 * @param cfg: the config structure (with tagnames) 693 * @param taglist: the tag bitlist. 694 * @param len: length of the tag bitlist. 695 * @return malloced string or NULL. 696 */ 697 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist, 698 size_t len); 699 700 /** 701 * see if two taglists intersect (have tags in common). 702 * @param list1: first tag bitlist. 703 * @param list1len: length in bytes of first list. 704 * @param list2: second tag bitlist. 705 * @param list2len: length in bytes of second list. 706 * @return true if there are tags in common, 0 if not. 707 */ 708 int taglist_intersect(uint8_t* list1, size_t list1len, uint8_t* list2, 709 size_t list2len); 710 711 /** 712 * Parse local-zone directive into two strings and register it in the config. 713 * @param cfg: to put it in. 714 * @param val: argument strings to local-zone, "example.com nodefault". 715 * @return: false on failure 716 */ 717 int cfg_parse_local_zone(struct config_file* cfg, const char* val); 718 719 /** 720 * Mark "number" or "low-high" as available or not in ports array. 721 * @param str: string in input 722 * @param allow: give true if this range is permitted. 723 * @param avail: the array from cfg. 724 * @param num: size of the array (65536). 725 * @return: true if parsed correctly, or 0 on a parse error (and an error 726 * is logged). 727 */ 728 int cfg_mark_ports(const char* str, int allow, int* avail, int num); 729 730 /** 731 * Get a condensed list of ports returned. allocated. 732 * @param cfg: config file. 733 * @param avail: the available ports array is returned here. 734 * @return: number of ports in array or 0 on error. 735 */ 736 int cfg_condense_ports(struct config_file* cfg, int** avail); 737 738 /** 739 * Scan ports available 740 * @param avail: the array from cfg. 741 * @param num: size of the array (65536). 742 * @return the number of ports available for use. 743 */ 744 int cfg_scan_ports(int* avail, int num); 745 746 /** 747 * Convert a filename to full pathname in original filesys 748 * @param fname: the path name to convert. 749 * Must not be null or empty. 750 * @param cfg: config struct for chroot and chdir (if set). 751 * @param use_chdir: if false, only chroot is applied. 752 * @return pointer to malloced buffer which is: [chroot][chdir]fname 753 * or NULL on malloc failure. 754 */ 755 char* fname_after_chroot(const char* fname, struct config_file* cfg, 756 int use_chdir); 757 758 /** 759 * Convert a ptr shorthand into a full reverse-notation PTR record. 760 * @param str: input string, "IP name" 761 * @return: malloced string "reversed-ip-name PTR name" 762 */ 763 char* cfg_ptr_reverse(char* str); 764 765 /** 766 * Append text to the error info for validation. 767 * @param qstate: query state. 768 * @param str: copied into query region and appended. 769 * Failures to allocate are logged. 770 */ 771 void errinf(struct module_qstate* qstate, const char* str); 772 773 /** 774 * Append text to error info: from 1.2.3.4 775 * @param qstate: query state. 776 * @param origin: sock list with origin of trouble. 777 * Every element added. 778 * If NULL: nothing is added. 779 * if 0len element: 'from cache' is added. 780 */ 781 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin); 782 783 /** 784 * Append text to error info: for RRset name type class 785 * @param qstate: query state. 786 * @param rr: rrset_key. 787 */ 788 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr); 789 790 /** 791 * Append text to error info: str dname 792 * @param qstate: query state. 793 * @param str: explanation string 794 * @param dname: the dname. 795 */ 796 void errinf_dname(struct module_qstate* qstate, const char* str, 797 uint8_t* dname); 798 799 /** 800 * Create error info in string 801 * @param qstate: query state. 802 * @return string or NULL on malloc failure (already logged). 803 * This string is malloced and has to be freed by caller. 804 */ 805 char* errinf_to_str(struct module_qstate* qstate); 806 807 /** 808 * Used during options parsing 809 */ 810 struct config_parser_state { 811 /** name of file being parser */ 812 char* filename; 813 /** line number in the file, starts at 1 */ 814 int line; 815 /** number of errors encountered */ 816 int errors; 817 /** the result of parsing is stored here. */ 818 struct config_file* cfg; 819 /** the current chroot dir (or NULL if none) */ 820 const char* chroot; 821 }; 822 823 /** global config parser object used during config parsing */ 824 extern struct config_parser_state* cfg_parser; 825 /** init lex state */ 826 void init_cfg_parse(void); 827 /** lex in file */ 828 extern FILE* ub_c_in; 829 /** lex out file */ 830 extern FILE* ub_c_out; 831 /** the yacc lex generated parse function */ 832 int ub_c_parse(void); 833 /** the lexer function */ 834 int ub_c_lex(void); 835 /** wrap function */ 836 int ub_c_wrap(void); 837 /** parsing helpers: print error with file and line numbers */ 838 void ub_c_error(const char* msg); 839 /** parsing helpers: print error with file and line numbers */ 840 void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2); 841 842 #ifdef UB_ON_WINDOWS 843 /** 844 * Obtain registry string (if it exists). 845 * @param key: key string 846 * @param name: name of value to fetch. 847 * @return malloced string with the result or NULL if it did not 848 * exist on an error (logged with log_err) was encountered. 849 */ 850 char* w_lookup_reg_str(const char* key, const char* name); 851 852 /** Modify directory in options for module file name */ 853 void w_config_adjust_directory(struct config_file* cfg); 854 #endif /* UB_ON_WINDOWS */ 855 856 #endif /* UTIL_CONFIG_FILE_H */ 857