1 /* 2 * util/config_file.c - 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 #include "config.h" 43 #include <ctype.h> 44 #include <stdarg.h> 45 #ifdef HAVE_TIME_H 46 #include <time.h> 47 #endif 48 #include "util/log.h" 49 #include "util/configyyrename.h" 50 #include "util/config_file.h" 51 #include "configparser.h" 52 #include "util/net_help.h" 53 #include "util/data/msgparse.h" 54 #include "util/module.h" 55 #include "util/regional.h" 56 #include "util/fptr_wlist.h" 57 #include "util/data/dname.h" 58 #include "util/rtt.h" 59 #include "services/cache/infra.h" 60 #include "sldns/wire2str.h" 61 #include "sldns/parseutil.h" 62 #include "iterator/iterator.h" 63 #ifdef HAVE_GLOB_H 64 # include <glob.h> 65 #endif 66 #ifdef CLIENT_SUBNET 67 #include "edns-subnet/edns-subnet.h" 68 #endif 69 #ifdef HAVE_PWD_H 70 #include <pwd.h> 71 #endif 72 73 /** from cfg username, after daemonize setup performed */ 74 uid_t cfg_uid = (uid_t)-1; 75 /** from cfg username, after daemonize setup performed */ 76 gid_t cfg_gid = (gid_t)-1; 77 /** for debug allow small timeout values for fast rollovers */ 78 int autr_permit_small_holddown = 0; 79 /** size (in bytes) of stream wait buffers max */ 80 size_t stream_wait_max = 4 * 1024 * 1024; 81 size_t http2_query_buffer_max = 4 * 1024 * 1024; 82 size_t http2_response_buffer_max = 4 * 1024 * 1024; 83 84 /** global config during parsing */ 85 struct config_parser_state* cfg_parser = 0; 86 87 /** init ports possible for use */ 88 static void init_outgoing_availports(int* array, int num); 89 90 struct config_file* 91 config_create(void) 92 { 93 struct config_file* cfg; 94 cfg = (struct config_file*)calloc(1, sizeof(struct config_file)); 95 if(!cfg) 96 return NULL; 97 /* the defaults if no config is present */ 98 cfg->verbosity = 1; 99 cfg->stat_interval = 0; 100 cfg->stat_cumulative = 0; 101 cfg->stat_extended = 0; 102 cfg->num_threads = 1; 103 cfg->port = UNBOUND_DNS_PORT; 104 cfg->do_ip4 = 1; 105 cfg->do_ip6 = 1; 106 cfg->do_udp = 1; 107 cfg->do_tcp = 1; 108 cfg->tcp_upstream = 0; 109 cfg->udp_upstream_without_downstream = 0; 110 cfg->tcp_mss = 0; 111 cfg->outgoing_tcp_mss = 0; 112 cfg->tcp_idle_timeout = 30 * 1000; /* 30s in millisecs */ 113 cfg->do_tcp_keepalive = 0; 114 cfg->tcp_keepalive_timeout = 120 * 1000; /* 120s in millisecs */ 115 cfg->ssl_service_key = NULL; 116 cfg->ssl_service_pem = NULL; 117 cfg->ssl_port = UNBOUND_DNS_OVER_TLS_PORT; 118 cfg->ssl_upstream = 0; 119 cfg->tls_cert_bundle = NULL; 120 cfg->tls_win_cert = 0; 121 cfg->tls_use_sni = 1; 122 cfg->https_port = UNBOUND_DNS_OVER_HTTPS_PORT; 123 if(!(cfg->http_endpoint = strdup("/dns-query"))) goto error_exit; 124 cfg->http_max_streams = 100; 125 cfg->http_query_buffer_size = 4*1024*1024; 126 cfg->http_response_buffer_size = 4*1024*1024; 127 cfg->http_nodelay = 1; 128 cfg->use_syslog = 1; 129 cfg->log_identity = NULL; /* changed later with argv[0] */ 130 cfg->log_time_ascii = 0; 131 cfg->log_queries = 0; 132 cfg->log_replies = 0; 133 cfg->log_tag_queryreply = 0; 134 cfg->log_local_actions = 0; 135 cfg->log_servfail = 0; 136 #ifndef USE_WINSOCK 137 # ifdef USE_MINI_EVENT 138 /* select max 1024 sockets */ 139 cfg->outgoing_num_ports = 960; 140 cfg->num_queries_per_thread = 512; 141 # else 142 /* libevent can use many sockets */ 143 cfg->outgoing_num_ports = 4096; 144 cfg->num_queries_per_thread = 1024; 145 # endif 146 cfg->outgoing_num_tcp = 10; 147 cfg->incoming_num_tcp = 10; 148 #else 149 cfg->outgoing_num_ports = 48; /* windows is limited in num fds */ 150 cfg->num_queries_per_thread = 24; 151 cfg->outgoing_num_tcp = 2; /* leaves 64-52=12 for: 4if,1stop,thread4 */ 152 cfg->incoming_num_tcp = 2; 153 #endif 154 cfg->stream_wait_size = 4 * 1024 * 1024; 155 cfg->edns_buffer_size = 1232; /* from DNS flagday recommendation */ 156 cfg->msg_buffer_size = 65552; /* 64 k + a small margin */ 157 cfg->msg_cache_size = 4 * 1024 * 1024; 158 cfg->msg_cache_slabs = 4; 159 cfg->jostle_time = 200; 160 cfg->rrset_cache_size = 4 * 1024 * 1024; 161 cfg->rrset_cache_slabs = 4; 162 cfg->host_ttl = 900; 163 cfg->bogus_ttl = 60; 164 cfg->min_ttl = 0; 165 cfg->max_ttl = 3600 * 24; 166 cfg->max_negative_ttl = 3600; 167 cfg->prefetch = 0; 168 cfg->prefetch_key = 0; 169 cfg->deny_any = 0; 170 cfg->infra_cache_slabs = 4; 171 cfg->infra_cache_numhosts = 10000; 172 cfg->infra_cache_min_rtt = 50; 173 cfg->infra_keep_probing = 0; 174 cfg->delay_close = 0; 175 cfg->udp_connect = 1; 176 if(!(cfg->outgoing_avail_ports = (int*)calloc(65536, sizeof(int)))) 177 goto error_exit; 178 init_outgoing_availports(cfg->outgoing_avail_ports, 65536); 179 if(!(cfg->username = strdup(UB_USERNAME))) goto error_exit; 180 #ifdef HAVE_CHROOT 181 if(!(cfg->chrootdir = strdup(CHROOT_DIR))) goto error_exit; 182 #endif 183 if(!(cfg->directory = strdup(RUN_DIR))) goto error_exit; 184 if(!(cfg->logfile = strdup(""))) goto error_exit; 185 if(!(cfg->pidfile = strdup(PIDFILE))) goto error_exit; 186 if(!(cfg->target_fetch_policy = strdup("3 2 1 0 0"))) goto error_exit; 187 cfg->fast_server_permil = 0; 188 cfg->fast_server_num = 3; 189 cfg->donotqueryaddrs = NULL; 190 cfg->donotquery_localhost = 1; 191 cfg->root_hints = NULL; 192 cfg->use_systemd = 0; 193 cfg->do_daemonize = 1; 194 cfg->if_automatic = 0; 195 cfg->so_rcvbuf = 0; 196 cfg->so_sndbuf = 0; 197 cfg->so_reuseport = REUSEPORT_DEFAULT; 198 cfg->ip_transparent = 0; 199 cfg->ip_freebind = 0; 200 cfg->ip_dscp = 0; 201 cfg->num_ifs = 0; 202 cfg->ifs = NULL; 203 cfg->num_out_ifs = 0; 204 cfg->out_ifs = NULL; 205 cfg->stubs = NULL; 206 cfg->forwards = NULL; 207 cfg->auths = NULL; 208 #ifdef CLIENT_SUBNET 209 cfg->client_subnet = NULL; 210 cfg->client_subnet_zone = NULL; 211 cfg->client_subnet_opcode = LDNS_EDNS_CLIENT_SUBNET; 212 cfg->client_subnet_always_forward = 0; 213 cfg->max_client_subnet_ipv4 = 24; 214 cfg->max_client_subnet_ipv6 = 56; 215 cfg->min_client_subnet_ipv4 = 0; 216 cfg->min_client_subnet_ipv6 = 0; 217 cfg->max_ecs_tree_size_ipv4 = 100; 218 cfg->max_ecs_tree_size_ipv6 = 100; 219 #endif 220 cfg->views = NULL; 221 cfg->acls = NULL; 222 cfg->tcp_connection_limits = NULL; 223 cfg->harden_short_bufsize = 0; 224 cfg->harden_large_queries = 0; 225 cfg->harden_glue = 1; 226 cfg->harden_dnssec_stripped = 1; 227 cfg->harden_below_nxdomain = 1; 228 cfg->harden_referral_path = 0; 229 cfg->harden_algo_downgrade = 0; 230 cfg->use_caps_bits_for_id = 0; 231 cfg->caps_whitelist = NULL; 232 cfg->private_address = NULL; 233 cfg->private_domain = NULL; 234 cfg->unwanted_threshold = 0; 235 cfg->hide_identity = 0; 236 cfg->hide_version = 0; 237 cfg->hide_trustanchor = 0; 238 cfg->identity = NULL; 239 cfg->version = NULL; 240 cfg->auto_trust_anchor_file_list = NULL; 241 cfg->trust_anchor_file_list = NULL; 242 cfg->trust_anchor_list = NULL; 243 cfg->trusted_keys_file_list = NULL; 244 cfg->trust_anchor_signaling = 1; 245 cfg->root_key_sentinel = 1; 246 cfg->domain_insecure = NULL; 247 cfg->val_date_override = 0; 248 cfg->val_sig_skew_min = 3600; /* at least daylight savings trouble */ 249 cfg->val_sig_skew_max = 86400; /* at most timezone settings trouble */ 250 cfg->val_clean_additional = 1; 251 cfg->val_log_level = 0; 252 cfg->val_log_squelch = 0; 253 cfg->val_permissive_mode = 0; 254 cfg->aggressive_nsec = 0; 255 cfg->ignore_cd = 0; 256 cfg->serve_expired = 0; 257 cfg->serve_expired_ttl = 0; 258 cfg->serve_expired_ttl_reset = 0; 259 cfg->serve_expired_reply_ttl = 30; 260 cfg->serve_expired_client_timeout = 0; 261 cfg->add_holddown = 30*24*3600; 262 cfg->del_holddown = 30*24*3600; 263 cfg->keep_missing = 366*24*3600; /* one year plus a little leeway */ 264 cfg->permit_small_holddown = 0; 265 cfg->key_cache_size = 4 * 1024 * 1024; 266 cfg->key_cache_slabs = 4; 267 cfg->neg_cache_size = 1 * 1024 * 1024; 268 cfg->local_zones = NULL; 269 cfg->local_zones_nodefault = NULL; 270 #ifdef USE_IPSET 271 cfg->local_zones_ipset = NULL; 272 #endif 273 cfg->local_zones_disable_default = 0; 274 cfg->local_data = NULL; 275 cfg->local_zone_overrides = NULL; 276 cfg->unblock_lan_zones = 0; 277 cfg->insecure_lan_zones = 0; 278 cfg->python_script = NULL; 279 cfg->dynlib_file = NULL; 280 cfg->remote_control_enable = 0; 281 cfg->control_ifs.first = NULL; 282 cfg->control_ifs.last = NULL; 283 cfg->control_port = UNBOUND_CONTROL_PORT; 284 cfg->control_use_cert = 1; 285 cfg->minimal_responses = 1; 286 cfg->rrset_roundrobin = 1; 287 cfg->unknown_server_time_limit = 376; 288 cfg->max_udp_size = 4096; 289 if(!(cfg->server_key_file = strdup(RUN_DIR"/unbound_server.key"))) 290 goto error_exit; 291 if(!(cfg->server_cert_file = strdup(RUN_DIR"/unbound_server.pem"))) 292 goto error_exit; 293 if(!(cfg->control_key_file = strdup(RUN_DIR"/unbound_control.key"))) 294 goto error_exit; 295 if(!(cfg->control_cert_file = strdup(RUN_DIR"/unbound_control.pem"))) 296 goto error_exit; 297 298 #ifdef CLIENT_SUBNET 299 if(!(cfg->module_conf = strdup("subnetcache validator iterator"))) goto error_exit; 300 #else 301 if(!(cfg->module_conf = strdup("validator iterator"))) goto error_exit; 302 #endif 303 if(!(cfg->val_nsec3_key_iterations = 304 strdup("1024 150 2048 500 4096 2500"))) goto error_exit; 305 #if defined(DNSTAP_SOCKET_PATH) 306 if(!(cfg->dnstap_socket_path = strdup(DNSTAP_SOCKET_PATH))) 307 goto error_exit; 308 #endif 309 cfg->dnstap_bidirectional = 1; 310 cfg->dnstap_tls = 1; 311 cfg->disable_dnssec_lame_check = 0; 312 cfg->ip_ratelimit = 0; 313 cfg->ratelimit = 0; 314 cfg->ip_ratelimit_slabs = 4; 315 cfg->ratelimit_slabs = 4; 316 cfg->ip_ratelimit_size = 4*1024*1024; 317 cfg->ratelimit_size = 4*1024*1024; 318 cfg->ratelimit_for_domain = NULL; 319 cfg->ratelimit_below_domain = NULL; 320 cfg->ip_ratelimit_factor = 10; 321 cfg->ratelimit_factor = 10; 322 cfg->qname_minimisation = 1; 323 cfg->qname_minimisation_strict = 0; 324 cfg->shm_enable = 0; 325 cfg->shm_key = 11777; 326 cfg->edns_client_strings = NULL; 327 cfg->edns_client_string_opcode = 65001; 328 cfg->dnscrypt = 0; 329 cfg->dnscrypt_port = 0; 330 cfg->dnscrypt_provider = NULL; 331 cfg->dnscrypt_provider_cert = NULL; 332 cfg->dnscrypt_provider_cert_rotated = NULL; 333 cfg->dnscrypt_secret_key = NULL; 334 cfg->dnscrypt_shared_secret_cache_size = 4*1024*1024; 335 cfg->dnscrypt_shared_secret_cache_slabs = 4; 336 cfg->dnscrypt_nonce_cache_size = 4*1024*1024; 337 cfg->dnscrypt_nonce_cache_slabs = 4; 338 #ifdef USE_IPSECMOD 339 cfg->ipsecmod_enabled = 1; 340 cfg->ipsecmod_ignore_bogus = 0; 341 cfg->ipsecmod_hook = NULL; 342 cfg->ipsecmod_max_ttl = 3600; 343 cfg->ipsecmod_whitelist = NULL; 344 cfg->ipsecmod_strict = 0; 345 #endif 346 #ifdef USE_CACHEDB 347 if(!(cfg->cachedb_backend = strdup("testframe"))) goto error_exit; 348 if(!(cfg->cachedb_secret = strdup("default"))) goto error_exit; 349 #ifdef USE_REDIS 350 if(!(cfg->redis_server_host = strdup("127.0.0.1"))) goto error_exit; 351 cfg->redis_timeout = 100; 352 cfg->redis_server_port = 6379; 353 cfg->redis_expire_records = 0; 354 #endif /* USE_REDIS */ 355 #endif /* USE_CACHEDB */ 356 #ifdef USE_IPSET 357 cfg->ipset_name_v4 = NULL; 358 cfg->ipset_name_v6 = NULL; 359 #endif 360 return cfg; 361 error_exit: 362 config_delete(cfg); 363 return NULL; 364 } 365 366 struct config_file* config_create_forlib(void) 367 { 368 struct config_file* cfg = config_create(); 369 if(!cfg) return NULL; 370 /* modifications for library use, less verbose, less memory */ 371 free(cfg->chrootdir); 372 cfg->chrootdir = NULL; 373 cfg->verbosity = 0; 374 cfg->outgoing_num_ports = 16; /* in library use, this is 'reasonable' 375 and probably within the ulimit(maxfds) of the user */ 376 cfg->outgoing_num_tcp = 2; 377 cfg->msg_cache_size = 1024*1024; 378 cfg->msg_cache_slabs = 1; 379 cfg->rrset_cache_size = 1024*1024; 380 cfg->rrset_cache_slabs = 1; 381 cfg->infra_cache_slabs = 1; 382 cfg->use_syslog = 0; 383 cfg->key_cache_size = 1024*1024; 384 cfg->key_cache_slabs = 1; 385 cfg->neg_cache_size = 100 * 1024; 386 cfg->donotquery_localhost = 0; /* allow, so that you can ask a 387 forward nameserver running on localhost */ 388 cfg->val_log_level = 2; /* to fill why_bogus with */ 389 cfg->val_log_squelch = 1; 390 cfg->minimal_responses = 0; 391 return cfg; 392 } 393 394 /** check that the value passed is >= 0 */ 395 #define IS_NUMBER_OR_ZERO \ 396 if(atoi(val) == 0 && strcmp(val, "0") != 0) return 0 397 /** check that the value passed is > 0 */ 398 #define IS_NONZERO_NUMBER \ 399 if(atoi(val) == 0) return 0 400 /** check that the value passed is not 0 and a power of 2 */ 401 #define IS_POW2_NUMBER \ 402 if(atoi(val) == 0 || !is_pow2((size_t)atoi(val))) return 0 403 /** check that the value passed is yes or no */ 404 #define IS_YES_OR_NO \ 405 if(strcmp(val, "yes") != 0 && strcmp(val, "no") != 0) return 0 406 /** put integer_or_zero into variable */ 407 #define S_NUMBER_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 408 { IS_NUMBER_OR_ZERO; cfg->var = atoi(val); } 409 /** put integer_nonzero into variable */ 410 #define S_NUMBER_NONZERO(str, var) if(strcmp(opt, str) == 0) \ 411 { IS_NONZERO_NUMBER; cfg->var = atoi(val); } 412 /** put integer_or_zero into unsigned */ 413 #define S_UNSIGNED_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 414 { IS_NUMBER_OR_ZERO; cfg->var = (unsigned)atoi(val); } 415 /** put integer_or_zero into size_t */ 416 #define S_SIZET_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 417 { IS_NUMBER_OR_ZERO; cfg->var = (size_t)atoi(val); } 418 /** put integer_nonzero into size_t */ 419 #define S_SIZET_NONZERO(str, var) if(strcmp(opt, str) == 0) \ 420 { IS_NONZERO_NUMBER; cfg->var = (size_t)atoi(val); } 421 /** put yesno into variable */ 422 #define S_YNO(str, var) if(strcmp(opt, str) == 0) \ 423 { IS_YES_OR_NO; cfg->var = (strcmp(val, "yes") == 0); } 424 /** put memsize into variable */ 425 #define S_MEMSIZE(str, var) if(strcmp(opt, str)==0) \ 426 { return cfg_parse_memsize(val, &cfg->var); } 427 /** put pow2 number into variable */ 428 #define S_POW2(str, var) if(strcmp(opt, str)==0) \ 429 { IS_POW2_NUMBER; cfg->var = (size_t)atoi(val); } 430 /** put string into variable */ 431 #define S_STR(str, var) if(strcmp(opt, str)==0) \ 432 { free(cfg->var); return (cfg->var = strdup(val)) != NULL; } 433 /** put string into strlist */ 434 #define S_STRLIST(str, var) if(strcmp(opt, str)==0) \ 435 { return cfg_strlist_insert(&cfg->var, strdup(val)); } 436 /** put string into strlist if not present yet*/ 437 #define S_STRLIST_UNIQ(str, var) if(strcmp(opt, str)==0) \ 438 { if(cfg_strlist_find(cfg->var, val)) { return 0;} \ 439 return cfg_strlist_insert(&cfg->var, strdup(val)); } 440 /** append string to strlist */ 441 #define S_STRLIST_APPEND(str, var) if(strcmp(opt, str)==0) \ 442 { return cfg_strlist_append(&cfg->var, strdup(val)); } 443 444 int config_set_option(struct config_file* cfg, const char* opt, 445 const char* val) 446 { 447 char buf[64]; 448 if(!opt) return 0; 449 if(opt[strlen(opt)-1] != ':' && strlen(opt)+2<sizeof(buf)) { 450 snprintf(buf, sizeof(buf), "%s:", opt); 451 opt = buf; 452 } 453 S_NUMBER_OR_ZERO("verbosity:", verbosity) 454 else if(strcmp(opt, "statistics-interval:") == 0) { 455 if(strcmp(val, "0") == 0 || strcmp(val, "") == 0) 456 cfg->stat_interval = 0; 457 else if(atoi(val) == 0) 458 return 0; 459 else cfg->stat_interval = atoi(val); 460 } else if(strcmp(opt, "num_threads:") == 0) { 461 /* not supported, library must have 1 thread in bgworker */ 462 return 0; 463 } else if(strcmp(opt, "outgoing-port-permit:") == 0) { 464 return cfg_mark_ports(val, 1, 465 cfg->outgoing_avail_ports, 65536); 466 } else if(strcmp(opt, "outgoing-port-avoid:") == 0) { 467 return cfg_mark_ports(val, 0, 468 cfg->outgoing_avail_ports, 65536); 469 } else if(strcmp(opt, "local-zone:") == 0) { 470 return cfg_parse_local_zone(cfg, val); 471 } else if(strcmp(opt, "val-override-date:") == 0) { 472 if(strcmp(val, "") == 0 || strcmp(val, "0") == 0) { 473 cfg->val_date_override = 0; 474 } else if(strlen(val) == 14) { 475 cfg->val_date_override = cfg_convert_timeval(val); 476 return cfg->val_date_override != 0; 477 } else { 478 if(atoi(val) == 0) return 0; 479 cfg->val_date_override = (uint32_t)atoi(val); 480 } 481 } else if(strcmp(opt, "local-data-ptr:") == 0) { 482 char* ptr = cfg_ptr_reverse((char*)opt); 483 return cfg_strlist_insert(&cfg->local_data, ptr); 484 } else if(strcmp(opt, "logfile:") == 0) { 485 cfg->use_syslog = 0; 486 free(cfg->logfile); 487 return (cfg->logfile = strdup(val)) != NULL; 488 } 489 else if(strcmp(opt, "log-time-ascii:") == 0) 490 { IS_YES_OR_NO; cfg->log_time_ascii = (strcmp(val, "yes") == 0); 491 log_set_time_asc(cfg->log_time_ascii); } 492 else S_SIZET_NONZERO("max-udp-size:", max_udp_size) 493 else S_YNO("use-syslog:", use_syslog) 494 else S_STR("log-identity:", log_identity) 495 else S_YNO("extended-statistics:", stat_extended) 496 else S_YNO("statistics-cumulative:", stat_cumulative) 497 else S_YNO("shm-enable:", shm_enable) 498 else S_NUMBER_OR_ZERO("shm-key:", shm_key) 499 else S_YNO("do-ip4:", do_ip4) 500 else S_YNO("do-ip6:", do_ip6) 501 else S_YNO("do-udp:", do_udp) 502 else S_YNO("do-tcp:", do_tcp) 503 else S_YNO("prefer-ip4:", prefer_ip4) 504 else S_YNO("prefer-ip6:", prefer_ip6) 505 else S_YNO("tcp-upstream:", tcp_upstream) 506 else S_YNO("udp-upstream-without-downstream:", 507 udp_upstream_without_downstream) 508 else S_NUMBER_NONZERO("tcp-mss:", tcp_mss) 509 else S_NUMBER_NONZERO("outgoing-tcp-mss:", outgoing_tcp_mss) 510 else S_NUMBER_NONZERO("tcp-idle-timeout:", tcp_idle_timeout) 511 else S_YNO("edns-tcp-keepalive:", do_tcp_keepalive) 512 else S_NUMBER_NONZERO("edns-tcp-keepalive-timeout:", tcp_keepalive_timeout) 513 else S_YNO("ssl-upstream:", ssl_upstream) 514 else S_STR("ssl-service-key:", ssl_service_key) 515 else S_STR("ssl-service-pem:", ssl_service_pem) 516 else S_NUMBER_NONZERO("ssl-port:", ssl_port) 517 else S_STR("tls-cert-bundle:", tls_cert_bundle) 518 else S_YNO("tls-win-cert:", tls_win_cert) 519 else S_STRLIST("additional-tls-port:", tls_additional_port) 520 else S_STRLIST("tls-additional-ports:", tls_additional_port) 521 else S_STRLIST("tls-additional-port:", tls_additional_port) 522 else S_STRLIST_APPEND("tls-session-ticket-keys:", tls_session_ticket_keys) 523 else S_STR("tls-ciphers:", tls_ciphers) 524 else S_STR("tls-ciphersuites:", tls_ciphersuites) 525 else S_YNO("tls-use-sni:", tls_use_sni) 526 else S_NUMBER_NONZERO("https-port:", https_port) 527 else S_STR("http-endpoint:", http_endpoint) 528 else S_NUMBER_NONZERO("http-max-streams:", http_max_streams) 529 else S_MEMSIZE("http-query-buffer-size:", http_query_buffer_size) 530 else S_MEMSIZE("http-response-buffer-size:", http_response_buffer_size) 531 else S_YNO("http-nodelay:", http_nodelay) 532 else S_YNO("http-notls-downstream:", http_notls_downstream) 533 else S_YNO("interface-automatic:", if_automatic) 534 else S_YNO("use-systemd:", use_systemd) 535 else S_YNO("do-daemonize:", do_daemonize) 536 else S_NUMBER_NONZERO("port:", port) 537 else S_NUMBER_NONZERO("outgoing-range:", outgoing_num_ports) 538 else S_SIZET_OR_ZERO("outgoing-num-tcp:", outgoing_num_tcp) 539 else S_SIZET_OR_ZERO("incoming-num-tcp:", incoming_num_tcp) 540 else S_MEMSIZE("stream-wait-size:", stream_wait_size) 541 else S_SIZET_NONZERO("edns-buffer-size:", edns_buffer_size) 542 else S_SIZET_NONZERO("msg-buffer-size:", msg_buffer_size) 543 else S_MEMSIZE("msg-cache-size:", msg_cache_size) 544 else S_POW2("msg-cache-slabs:", msg_cache_slabs) 545 else S_SIZET_NONZERO("num-queries-per-thread:",num_queries_per_thread) 546 else S_SIZET_OR_ZERO("jostle-timeout:", jostle_time) 547 else S_MEMSIZE("so-rcvbuf:", so_rcvbuf) 548 else S_MEMSIZE("so-sndbuf:", so_sndbuf) 549 else S_YNO("so-reuseport:", so_reuseport) 550 else S_YNO("ip-transparent:", ip_transparent) 551 else S_YNO("ip-freebind:", ip_freebind) 552 else S_NUMBER_OR_ZERO("ip-dscp:", ip_dscp) 553 else S_MEMSIZE("rrset-cache-size:", rrset_cache_size) 554 else S_POW2("rrset-cache-slabs:", rrset_cache_slabs) 555 else S_YNO("prefetch:", prefetch) 556 else S_YNO("prefetch-key:", prefetch_key) 557 else S_YNO("deny-any:", deny_any) 558 else if(strcmp(opt, "cache-max-ttl:") == 0) 559 { IS_NUMBER_OR_ZERO; cfg->max_ttl = atoi(val); MAX_TTL=(time_t)cfg->max_ttl;} 560 else if(strcmp(opt, "cache-max-negative-ttl:") == 0) 561 { IS_NUMBER_OR_ZERO; cfg->max_negative_ttl = atoi(val); MAX_NEG_TTL=(time_t)cfg->max_negative_ttl;} 562 else if(strcmp(opt, "cache-min-ttl:") == 0) 563 { IS_NUMBER_OR_ZERO; cfg->min_ttl = atoi(val); MIN_TTL=(time_t)cfg->min_ttl;} 564 else if(strcmp(opt, "infra-cache-min-rtt:") == 0) { 565 IS_NUMBER_OR_ZERO; cfg->infra_cache_min_rtt = atoi(val); 566 RTT_MIN_TIMEOUT=cfg->infra_cache_min_rtt; 567 } 568 else S_YNO("infra-keep-probing:", infra_keep_probing) 569 else S_NUMBER_OR_ZERO("infra-host-ttl:", host_ttl) 570 else S_POW2("infra-cache-slabs:", infra_cache_slabs) 571 else S_SIZET_NONZERO("infra-cache-numhosts:", infra_cache_numhosts) 572 else S_NUMBER_OR_ZERO("delay-close:", delay_close) 573 else S_YNO("udp-connect:", udp_connect) 574 else S_STR("chroot:", chrootdir) 575 else S_STR("username:", username) 576 else S_STR("directory:", directory) 577 else S_STR("pidfile:", pidfile) 578 else S_YNO("hide-identity:", hide_identity) 579 else S_YNO("hide-version:", hide_version) 580 else S_YNO("hide-trustanchor:", hide_trustanchor) 581 else S_STR("identity:", identity) 582 else S_STR("version:", version) 583 else S_STRLIST("root-hints:", root_hints) 584 else S_STR("target-fetch-policy:", target_fetch_policy) 585 else S_YNO("harden-glue:", harden_glue) 586 else S_YNO("harden-short-bufsize:", harden_short_bufsize) 587 else S_YNO("harden-large-queries:", harden_large_queries) 588 else S_YNO("harden-dnssec-stripped:", harden_dnssec_stripped) 589 else S_YNO("harden-below-nxdomain:", harden_below_nxdomain) 590 else S_YNO("harden-referral-path:", harden_referral_path) 591 else S_YNO("harden-algo-downgrade:", harden_algo_downgrade) 592 else S_YNO("use-caps-for-id:", use_caps_bits_for_id) 593 else S_STRLIST("caps-whitelist:", caps_whitelist) 594 else S_SIZET_OR_ZERO("unwanted-reply-threshold:", unwanted_threshold) 595 else S_STRLIST("private-address:", private_address) 596 else S_STRLIST("private-domain:", private_domain) 597 else S_YNO("do-not-query-localhost:", donotquery_localhost) 598 else S_STRLIST("do-not-query-address:", donotqueryaddrs) 599 else S_STRLIST("auto-trust-anchor-file:", auto_trust_anchor_file_list) 600 else S_STRLIST("trust-anchor-file:", trust_anchor_file_list) 601 else S_STRLIST("trust-anchor:", trust_anchor_list) 602 else S_STRLIST("trusted-keys-file:", trusted_keys_file_list) 603 else S_YNO("trust-anchor-signaling:", trust_anchor_signaling) 604 else S_YNO("root-key-sentinel:", root_key_sentinel) 605 else S_STRLIST("domain-insecure:", domain_insecure) 606 else S_NUMBER_OR_ZERO("val-bogus-ttl:", bogus_ttl) 607 else S_YNO("val-clean-additional:", val_clean_additional) 608 else S_NUMBER_OR_ZERO("val-log-level:", val_log_level) 609 else S_YNO("val-log-squelch:", val_log_squelch) 610 else S_YNO("log-queries:", log_queries) 611 else S_YNO("log-replies:", log_replies) 612 else S_YNO("log-tag-queryreply:", log_tag_queryreply) 613 else S_YNO("log-local-actions:", log_local_actions) 614 else S_YNO("log-servfail:", log_servfail) 615 else S_YNO("val-permissive-mode:", val_permissive_mode) 616 else S_YNO("aggressive-nsec:", aggressive_nsec) 617 else S_YNO("ignore-cd-flag:", ignore_cd) 618 else if(strcmp(opt, "serve-expired:") == 0) 619 { IS_YES_OR_NO; cfg->serve_expired = (strcmp(val, "yes") == 0); 620 SERVE_EXPIRED = cfg->serve_expired; } 621 else if(strcmp(opt, "serve-expired-ttl:") == 0) 622 { IS_NUMBER_OR_ZERO; cfg->serve_expired_ttl = atoi(val); SERVE_EXPIRED_TTL=(time_t)cfg->serve_expired_ttl;} 623 else S_YNO("serve-expired-ttl-reset:", serve_expired_ttl_reset) 624 else if(strcmp(opt, "serve-expired-reply-ttl:") == 0) 625 { IS_NUMBER_OR_ZERO; cfg->serve_expired_reply_ttl = atoi(val); SERVE_EXPIRED_REPLY_TTL=(time_t)cfg->serve_expired_reply_ttl;} 626 else S_NUMBER_OR_ZERO("serve-expired-client-timeout:", serve_expired_client_timeout) 627 else S_STR("val-nsec3-keysize-iterations:", val_nsec3_key_iterations) 628 else S_UNSIGNED_OR_ZERO("add-holddown:", add_holddown) 629 else S_UNSIGNED_OR_ZERO("del-holddown:", del_holddown) 630 else S_UNSIGNED_OR_ZERO("keep-missing:", keep_missing) 631 else if(strcmp(opt, "permit-small-holddown:") == 0) 632 { IS_YES_OR_NO; cfg->permit_small_holddown = (strcmp(val, "yes") == 0); 633 autr_permit_small_holddown = cfg->permit_small_holddown; } 634 else S_MEMSIZE("key-cache-size:", key_cache_size) 635 else S_POW2("key-cache-slabs:", key_cache_slabs) 636 else S_MEMSIZE("neg-cache-size:", neg_cache_size) 637 else S_YNO("minimal-responses:", minimal_responses) 638 else S_YNO("rrset-roundrobin:", rrset_roundrobin) 639 else S_NUMBER_OR_ZERO("unknown-server-time-limit:", unknown_server_time_limit) 640 else S_STRLIST("local-data:", local_data) 641 else S_YNO("unblock-lan-zones:", unblock_lan_zones) 642 else S_YNO("insecure-lan-zones:", insecure_lan_zones) 643 else S_YNO("control-enable:", remote_control_enable) 644 else S_STRLIST_APPEND("control-interface:", control_ifs) 645 else S_NUMBER_NONZERO("control-port:", control_port) 646 else S_STR("server-key-file:", server_key_file) 647 else S_STR("server-cert-file:", server_cert_file) 648 else S_STR("control-key-file:", control_key_file) 649 else S_STR("control-cert-file:", control_cert_file) 650 else S_STR("module-config:", module_conf) 651 else S_STRLIST("python-script:", python_script) 652 else S_STRLIST("dynlib-file:", dynlib_file) 653 else S_YNO("disable-dnssec-lame-check:", disable_dnssec_lame_check) 654 #ifdef CLIENT_SUBNET 655 /* Can't set max subnet prefix here, since that value is used when 656 * generating the address tree. */ 657 /* No client-subnet-always-forward here, module registration depends on 658 * this option. */ 659 #endif 660 #ifdef USE_DNSTAP 661 else S_YNO("dnstap-enable:", dnstap) 662 else S_YNO("dnstap-bidirectional:", dnstap_bidirectional) 663 else S_STR("dnstap-socket-path:", dnstap_socket_path) 664 else S_STR("dnstap-ip:", dnstap_ip) 665 else S_YNO("dnstap-tls:", dnstap_tls) 666 else S_STR("dnstap-tls-server-name:", dnstap_tls_server_name) 667 else S_STR("dnstap-tls-cert-bundle:", dnstap_tls_cert_bundle) 668 else S_STR("dnstap-tls-client-key-file:", dnstap_tls_client_key_file) 669 else S_STR("dnstap-tls-client-cert-file:", 670 dnstap_tls_client_cert_file) 671 else S_YNO("dnstap-send-identity:", dnstap_send_identity) 672 else S_YNO("dnstap-send-version:", dnstap_send_version) 673 else S_STR("dnstap-identity:", dnstap_identity) 674 else S_STR("dnstap-version:", dnstap_version) 675 else S_YNO("dnstap-log-resolver-query-messages:", 676 dnstap_log_resolver_query_messages) 677 else S_YNO("dnstap-log-resolver-response-messages:", 678 dnstap_log_resolver_response_messages) 679 else S_YNO("dnstap-log-client-query-messages:", 680 dnstap_log_client_query_messages) 681 else S_YNO("dnstap-log-client-response-messages:", 682 dnstap_log_client_response_messages) 683 else S_YNO("dnstap-log-forwarder-query-messages:", 684 dnstap_log_forwarder_query_messages) 685 else S_YNO("dnstap-log-forwarder-response-messages:", 686 dnstap_log_forwarder_response_messages) 687 #endif 688 #ifdef USE_DNSCRYPT 689 else S_YNO("dnscrypt-enable:", dnscrypt) 690 else S_NUMBER_NONZERO("dnscrypt-port:", dnscrypt_port) 691 else S_STR("dnscrypt-provider:", dnscrypt_provider) 692 else S_STRLIST_UNIQ("dnscrypt-provider-cert:", dnscrypt_provider_cert) 693 else S_STRLIST("dnscrypt-provider-cert-rotated:", dnscrypt_provider_cert_rotated) 694 else S_STRLIST_UNIQ("dnscrypt-secret-key:", dnscrypt_secret_key) 695 else S_MEMSIZE("dnscrypt-shared-secret-cache-size:", 696 dnscrypt_shared_secret_cache_size) 697 else S_POW2("dnscrypt-shared-secret-cache-slabs:", 698 dnscrypt_shared_secret_cache_slabs) 699 else S_MEMSIZE("dnscrypt-nonce-cache-size:", 700 dnscrypt_nonce_cache_size) 701 else S_POW2("dnscrypt-nonce-cache-slabs:", 702 dnscrypt_nonce_cache_slabs) 703 #endif 704 else if(strcmp(opt, "ip-ratelimit:") == 0) { 705 IS_NUMBER_OR_ZERO; cfg->ip_ratelimit = atoi(val); 706 infra_ip_ratelimit=cfg->ip_ratelimit; 707 } 708 else if(strcmp(opt, "ratelimit:") == 0) { 709 IS_NUMBER_OR_ZERO; cfg->ratelimit = atoi(val); 710 infra_dp_ratelimit=cfg->ratelimit; 711 } 712 else S_MEMSIZE("ip-ratelimit-size:", ip_ratelimit_size) 713 else S_MEMSIZE("ratelimit-size:", ratelimit_size) 714 else S_POW2("ip-ratelimit-slabs:", ip_ratelimit_slabs) 715 else S_POW2("ratelimit-slabs:", ratelimit_slabs) 716 else S_NUMBER_OR_ZERO("ip-ratelimit-factor:", ip_ratelimit_factor) 717 else S_NUMBER_OR_ZERO("ratelimit-factor:", ratelimit_factor) 718 else S_SIZET_NONZERO("fast-server-num:", fast_server_num) 719 else S_NUMBER_OR_ZERO("fast-server-permil:", fast_server_permil) 720 else S_YNO("qname-minimisation:", qname_minimisation) 721 else S_YNO("qname-minimisation-strict:", qname_minimisation_strict) 722 #ifdef USE_IPSECMOD 723 else S_YNO("ipsecmod-enabled:", ipsecmod_enabled) 724 else S_YNO("ipsecmod-ignore-bogus:", ipsecmod_ignore_bogus) 725 else if(strcmp(opt, "ipsecmod-max-ttl:") == 0) 726 { IS_NUMBER_OR_ZERO; cfg->ipsecmod_max_ttl = atoi(val); } 727 else S_YNO("ipsecmod-strict:", ipsecmod_strict) 728 #endif 729 else if(strcmp(opt, "define-tag:") ==0) { 730 return config_add_tag(cfg, val); 731 /* val_sig_skew_min and max are copied into val_env during init, 732 * so this does not update val_env with set_option */ 733 } else if(strcmp(opt, "val-sig-skew-min:") == 0) 734 { IS_NUMBER_OR_ZERO; cfg->val_sig_skew_min = (int32_t)atoi(val); } 735 else if(strcmp(opt, "val-sig-skew-max:") == 0) 736 { IS_NUMBER_OR_ZERO; cfg->val_sig_skew_max = (int32_t)atoi(val); } 737 else if (strcmp(opt, "outgoing-interface:") == 0) { 738 char* d = strdup(val); 739 char** oi = 740 (char**)reallocarray(NULL, (size_t)cfg->num_out_ifs+1, sizeof(char*)); 741 if(!d || !oi) { free(d); free(oi); return -1; } 742 if(cfg->out_ifs && cfg->num_out_ifs) { 743 memmove(oi, cfg->out_ifs, cfg->num_out_ifs*sizeof(char*)); 744 free(cfg->out_ifs); 745 } 746 oi[cfg->num_out_ifs++] = d; 747 cfg->out_ifs = oi; 748 } else { 749 /* unknown or unsupported (from the set_option interface): 750 * interface, outgoing-interface, access-control, 751 * stub-zone, name, stub-addr, stub-host, stub-prime 752 * forward-first, stub-first, forward-ssl-upstream, 753 * stub-ssl-upstream, forward-zone, auth-zone 754 * name, forward-addr, forward-host, 755 * ratelimit-for-domain, ratelimit-below-domain, 756 * local-zone-tag, access-control-view, 757 * send-client-subnet, client-subnet-always-forward, 758 * max-client-subnet-ipv4, max-client-subnet-ipv6, 759 * min-client-subnet-ipv4, min-client-subnet-ipv6, 760 * max-ecs-tree-size-ipv4, max-ecs-tree-size-ipv6, ipsecmod_hook, 761 * ipsecmod_whitelist. */ 762 return 0; 763 } 764 return 1; 765 } 766 767 void config_print_func(char* line, void* arg) 768 { 769 FILE* f = (FILE*)arg; 770 (void)fprintf(f, "%s\n", line); 771 } 772 773 /** collate func arg */ 774 struct config_collate_arg { 775 /** list of result items */ 776 struct config_strlist_head list; 777 /** if a malloc error occurred, 0 is OK */ 778 int status; 779 }; 780 781 void config_collate_func(char* line, void* arg) 782 { 783 struct config_collate_arg* m = (struct config_collate_arg*)arg; 784 if(m->status) 785 return; 786 if(!cfg_strlist_append(&m->list, strdup(line))) 787 m->status = 1; 788 } 789 790 int config_get_option_list(struct config_file* cfg, const char* opt, 791 struct config_strlist** list) 792 { 793 struct config_collate_arg m; 794 memset(&m, 0, sizeof(m)); 795 *list = NULL; 796 if(!config_get_option(cfg, opt, config_collate_func, &m)) 797 return 1; 798 if(m.status) { 799 config_delstrlist(m.list.first); 800 return 2; 801 } 802 *list = m.list.first; 803 return 0; 804 } 805 806 int 807 config_get_option_collate(struct config_file* cfg, const char* opt, char** str) 808 { 809 struct config_strlist* list = NULL; 810 int r; 811 *str = NULL; 812 if((r = config_get_option_list(cfg, opt, &list)) != 0) 813 return r; 814 *str = config_collate_cat(list); 815 config_delstrlist(list); 816 if(!*str) return 2; 817 return 0; 818 } 819 820 char* 821 config_collate_cat(struct config_strlist* list) 822 { 823 size_t total = 0, left; 824 struct config_strlist* s; 825 char *r, *w; 826 if(!list) /* no elements */ 827 return strdup(""); 828 if(list->next == NULL) /* one element , no newline at end. */ 829 return strdup(list->str); 830 /* count total length */ 831 for(s=list; s; s=s->next) 832 total += strlen(s->str) + 1; /* len + newline */ 833 left = total+1; /* one extra for nul at end */ 834 r = malloc(left); 835 if(!r) 836 return NULL; 837 w = r; 838 for(s=list; s; s=s->next) { 839 size_t this = strlen(s->str); 840 if(this+2 > left) { /* sanity check */ 841 free(r); 842 return NULL; 843 } 844 snprintf(w, left, "%s\n", s->str); 845 this = strlen(w); 846 w += this; 847 left -= this; 848 } 849 return r; 850 } 851 852 /** compare and print decimal option */ 853 #define O_DEC(opt, str, var) if(strcmp(opt, str)==0) \ 854 {snprintf(buf, len, "%d", (int)cfg->var); \ 855 func(buf, arg);} 856 /** compare and print unsigned option */ 857 #define O_UNS(opt, str, var) if(strcmp(opt, str)==0) \ 858 {snprintf(buf, len, "%u", (unsigned)cfg->var); \ 859 func(buf, arg);} 860 /** compare and print yesno option */ 861 #define O_YNO(opt, str, var) if(strcmp(opt, str)==0) \ 862 {func(cfg->var?"yes":"no", arg);} 863 /** compare and print string option */ 864 #define O_STR(opt, str, var) if(strcmp(opt, str)==0) \ 865 {func(cfg->var?cfg->var:"", arg);} 866 /** compare and print array option */ 867 #define O_IFC(opt, str, num, arr) if(strcmp(opt, str)==0) \ 868 {int i; for(i=0; i<cfg->num; i++) func(cfg->arr[i], arg);} 869 /** compare and print memorysize option */ 870 #define O_MEM(opt, str, var) if(strcmp(opt, str)==0) { \ 871 if(cfg->var > 1024*1024*1024) { \ 872 size_t f=cfg->var/(size_t)1000000, b=cfg->var%(size_t)1000000; \ 873 snprintf(buf, len, "%u%6.6u", (unsigned)f, (unsigned)b); \ 874 } else snprintf(buf, len, "%u", (unsigned)cfg->var); \ 875 func(buf, arg);} 876 /** compare and print list option */ 877 #define O_LST(opt, name, lst) if(strcmp(opt, name)==0) { \ 878 struct config_strlist* p = cfg->lst; \ 879 for(p = cfg->lst; p; p = p->next) \ 880 func(p->str, arg); \ 881 } 882 /** compare and print list option */ 883 #define O_LS2(opt, name, lst) if(strcmp(opt, name)==0) { \ 884 struct config_str2list* p = cfg->lst; \ 885 for(p = cfg->lst; p; p = p->next) { \ 886 snprintf(buf, len, "%s %s", p->str, p->str2); \ 887 func(buf, arg); \ 888 } \ 889 } 890 /** compare and print list option */ 891 #define O_LS3(opt, name, lst) if(strcmp(opt, name)==0) { \ 892 struct config_str3list* p = cfg->lst; \ 893 for(p = cfg->lst; p; p = p->next) { \ 894 snprintf(buf, len, "%s %s %s", p->str, p->str2, p->str3); \ 895 func(buf, arg); \ 896 } \ 897 } 898 /** compare and print taglist option */ 899 #define O_LTG(opt, name, lst) if(strcmp(opt, name)==0) { \ 900 char* tmpstr = NULL; \ 901 struct config_strbytelist *p = cfg->lst; \ 902 for(p = cfg->lst; p; p = p->next) {\ 903 tmpstr = config_taglist2str(cfg, p->str2, p->str2len); \ 904 if(tmpstr) {\ 905 snprintf(buf, len, "%s %s", p->str, tmpstr); \ 906 func(buf, arg); \ 907 free(tmpstr); \ 908 } \ 909 } \ 910 } 911 912 int 913 config_get_option(struct config_file* cfg, const char* opt, 914 void (*func)(char*,void*), void* arg) 915 { 916 char buf[1024], nopt[64]; 917 size_t len = sizeof(buf); 918 if(!opt) return 0; 919 if(opt && opt[strlen(opt)-1] == ':' && strlen(opt)<sizeof(nopt)) { 920 memmove(nopt, opt, strlen(opt)); 921 nopt[strlen(opt)-1] = 0; 922 opt = nopt; 923 } 924 fptr_ok(fptr_whitelist_print_func(func)); 925 O_DEC(opt, "verbosity", verbosity) 926 else O_DEC(opt, "statistics-interval", stat_interval) 927 else O_YNO(opt, "statistics-cumulative", stat_cumulative) 928 else O_YNO(opt, "extended-statistics", stat_extended) 929 else O_YNO(opt, "shm-enable", shm_enable) 930 else O_DEC(opt, "shm-key", shm_key) 931 else O_YNO(opt, "use-syslog", use_syslog) 932 else O_STR(opt, "log-identity", log_identity) 933 else O_YNO(opt, "log-time-ascii", log_time_ascii) 934 else O_DEC(opt, "num-threads", num_threads) 935 else O_IFC(opt, "interface", num_ifs, ifs) 936 else O_IFC(opt, "outgoing-interface", num_out_ifs, out_ifs) 937 else O_YNO(opt, "interface-automatic", if_automatic) 938 else O_DEC(opt, "port", port) 939 else O_DEC(opt, "outgoing-range", outgoing_num_ports) 940 else O_DEC(opt, "outgoing-num-tcp", outgoing_num_tcp) 941 else O_DEC(opt, "incoming-num-tcp", incoming_num_tcp) 942 else O_MEM(opt, "stream-wait-size", stream_wait_size) 943 else O_DEC(opt, "edns-buffer-size", edns_buffer_size) 944 else O_DEC(opt, "msg-buffer-size", msg_buffer_size) 945 else O_MEM(opt, "msg-cache-size", msg_cache_size) 946 else O_DEC(opt, "msg-cache-slabs", msg_cache_slabs) 947 else O_DEC(opt, "num-queries-per-thread", num_queries_per_thread) 948 else O_UNS(opt, "jostle-timeout", jostle_time) 949 else O_MEM(opt, "so-rcvbuf", so_rcvbuf) 950 else O_MEM(opt, "so-sndbuf", so_sndbuf) 951 else O_YNO(opt, "so-reuseport", so_reuseport) 952 else O_YNO(opt, "ip-transparent", ip_transparent) 953 else O_YNO(opt, "ip-freebind", ip_freebind) 954 else O_DEC(opt, "ip-dscp", ip_dscp) 955 else O_MEM(opt, "rrset-cache-size", rrset_cache_size) 956 else O_DEC(opt, "rrset-cache-slabs", rrset_cache_slabs) 957 else O_YNO(opt, "prefetch-key", prefetch_key) 958 else O_YNO(opt, "prefetch", prefetch) 959 else O_YNO(opt, "deny-any", deny_any) 960 else O_DEC(opt, "cache-max-ttl", max_ttl) 961 else O_DEC(opt, "cache-max-negative-ttl", max_negative_ttl) 962 else O_DEC(opt, "cache-min-ttl", min_ttl) 963 else O_DEC(opt, "infra-host-ttl", host_ttl) 964 else O_DEC(opt, "infra-cache-slabs", infra_cache_slabs) 965 else O_DEC(opt, "infra-cache-min-rtt", infra_cache_min_rtt) 966 else O_YNO(opt, "infra-keep-probing", infra_keep_probing) 967 else O_MEM(opt, "infra-cache-numhosts", infra_cache_numhosts) 968 else O_UNS(opt, "delay-close", delay_close) 969 else O_YNO(opt, "udp-connect", udp_connect) 970 else O_YNO(opt, "do-ip4", do_ip4) 971 else O_YNO(opt, "do-ip6", do_ip6) 972 else O_YNO(opt, "do-udp", do_udp) 973 else O_YNO(opt, "do-tcp", do_tcp) 974 else O_YNO(opt, "prefer-ip4", prefer_ip4) 975 else O_YNO(opt, "prefer-ip6", prefer_ip6) 976 else O_YNO(opt, "tcp-upstream", tcp_upstream) 977 else O_YNO(opt, "udp-upstream-without-downstream", udp_upstream_without_downstream) 978 else O_DEC(opt, "tcp-mss", tcp_mss) 979 else O_DEC(opt, "outgoing-tcp-mss", outgoing_tcp_mss) 980 else O_DEC(opt, "tcp-idle-timeout", tcp_idle_timeout) 981 else O_YNO(opt, "edns-tcp-keepalive", do_tcp_keepalive) 982 else O_DEC(opt, "edns-tcp-keepalive-timeout", tcp_keepalive_timeout) 983 else O_YNO(opt, "ssl-upstream", ssl_upstream) 984 else O_STR(opt, "ssl-service-key", ssl_service_key) 985 else O_STR(opt, "ssl-service-pem", ssl_service_pem) 986 else O_DEC(opt, "ssl-port", ssl_port) 987 else O_STR(opt, "tls-cert-bundle", tls_cert_bundle) 988 else O_YNO(opt, "tls-win-cert", tls_win_cert) 989 else O_LST(opt, "tls-additional-port", tls_additional_port) 990 else O_LST(opt, "tls-session-ticket-keys", tls_session_ticket_keys.first) 991 else O_STR(opt, "tls-ciphers", tls_ciphers) 992 else O_STR(opt, "tls-ciphersuites", tls_ciphersuites) 993 else O_YNO(opt, "tls-use-sni", tls_use_sni) 994 else O_DEC(opt, "https-port", https_port) 995 else O_STR(opt, "http-endpoint", http_endpoint) 996 else O_UNS(opt, "http-max-streams", http_max_streams) 997 else O_MEM(opt, "http-query-buffer-size", http_query_buffer_size) 998 else O_MEM(opt, "http-response-buffer-size", http_response_buffer_size) 999 else O_YNO(opt, "http-nodelay", http_nodelay) 1000 else O_YNO(opt, "http-notls-downstream", http_notls_downstream) 1001 else O_YNO(opt, "use-systemd", use_systemd) 1002 else O_YNO(opt, "do-daemonize", do_daemonize) 1003 else O_STR(opt, "chroot", chrootdir) 1004 else O_STR(opt, "username", username) 1005 else O_STR(opt, "directory", directory) 1006 else O_STR(opt, "logfile", logfile) 1007 else O_YNO(opt, "log-queries", log_queries) 1008 else O_YNO(opt, "log-replies", log_replies) 1009 else O_YNO(opt, "log-tag-queryreply", log_tag_queryreply) 1010 else O_YNO(opt, "log-local-actions", log_local_actions) 1011 else O_YNO(opt, "log-servfail", log_servfail) 1012 else O_STR(opt, "pidfile", pidfile) 1013 else O_YNO(opt, "hide-identity", hide_identity) 1014 else O_YNO(opt, "hide-version", hide_version) 1015 else O_YNO(opt, "hide-trustanchor", hide_trustanchor) 1016 else O_STR(opt, "identity", identity) 1017 else O_STR(opt, "version", version) 1018 else O_STR(opt, "target-fetch-policy", target_fetch_policy) 1019 else O_YNO(opt, "harden-short-bufsize", harden_short_bufsize) 1020 else O_YNO(opt, "harden-large-queries", harden_large_queries) 1021 else O_YNO(opt, "harden-glue", harden_glue) 1022 else O_YNO(opt, "harden-dnssec-stripped", harden_dnssec_stripped) 1023 else O_YNO(opt, "harden-below-nxdomain", harden_below_nxdomain) 1024 else O_YNO(opt, "harden-referral-path", harden_referral_path) 1025 else O_YNO(opt, "harden-algo-downgrade", harden_algo_downgrade) 1026 else O_YNO(opt, "use-caps-for-id", use_caps_bits_for_id) 1027 else O_LST(opt, "caps-whitelist", caps_whitelist) 1028 else O_DEC(opt, "unwanted-reply-threshold", unwanted_threshold) 1029 else O_YNO(opt, "do-not-query-localhost", donotquery_localhost) 1030 else O_STR(opt, "module-config", module_conf) 1031 else O_DEC(opt, "val-bogus-ttl", bogus_ttl) 1032 else O_YNO(opt, "val-clean-additional", val_clean_additional) 1033 else O_DEC(opt, "val-log-level", val_log_level) 1034 else O_YNO(opt, "val-permissive-mode", val_permissive_mode) 1035 else O_YNO(opt, "aggressive-nsec", aggressive_nsec) 1036 else O_YNO(opt, "ignore-cd-flag", ignore_cd) 1037 else O_YNO(opt, "serve-expired", serve_expired) 1038 else O_DEC(opt, "serve-expired-ttl", serve_expired_ttl) 1039 else O_YNO(opt, "serve-expired-ttl-reset", serve_expired_ttl_reset) 1040 else O_DEC(opt, "serve-expired-reply-ttl", serve_expired_reply_ttl) 1041 else O_DEC(opt, "serve-expired-client-timeout", serve_expired_client_timeout) 1042 else O_STR(opt, "val-nsec3-keysize-iterations",val_nsec3_key_iterations) 1043 else O_UNS(opt, "add-holddown", add_holddown) 1044 else O_UNS(opt, "del-holddown", del_holddown) 1045 else O_UNS(opt, "keep-missing", keep_missing) 1046 else O_YNO(opt, "permit-small-holddown", permit_small_holddown) 1047 else O_MEM(opt, "key-cache-size", key_cache_size) 1048 else O_DEC(opt, "key-cache-slabs", key_cache_slabs) 1049 else O_MEM(opt, "neg-cache-size", neg_cache_size) 1050 else O_YNO(opt, "control-enable", remote_control_enable) 1051 else O_DEC(opt, "control-port", control_port) 1052 else O_STR(opt, "server-key-file", server_key_file) 1053 else O_STR(opt, "server-cert-file", server_cert_file) 1054 else O_STR(opt, "control-key-file", control_key_file) 1055 else O_STR(opt, "control-cert-file", control_cert_file) 1056 else O_LST(opt, "root-hints", root_hints) 1057 else O_LS2(opt, "access-control", acls) 1058 else O_LS2(opt, "tcp-connection-limit", tcp_connection_limits) 1059 else O_LST(opt, "do-not-query-address", donotqueryaddrs) 1060 else O_LST(opt, "private-address", private_address) 1061 else O_LST(opt, "private-domain", private_domain) 1062 else O_LST(opt, "auto-trust-anchor-file", auto_trust_anchor_file_list) 1063 else O_LST(opt, "trust-anchor-file", trust_anchor_file_list) 1064 else O_LST(opt, "trust-anchor", trust_anchor_list) 1065 else O_LST(opt, "trusted-keys-file", trusted_keys_file_list) 1066 else O_YNO(opt, "trust-anchor-signaling", trust_anchor_signaling) 1067 else O_YNO(opt, "root-key-sentinel", root_key_sentinel) 1068 else O_LST(opt, "control-interface", control_ifs.first) 1069 else O_LST(opt, "domain-insecure", domain_insecure) 1070 else O_UNS(opt, "val-override-date", val_date_override) 1071 else O_YNO(opt, "minimal-responses", minimal_responses) 1072 else O_YNO(opt, "rrset-roundrobin", rrset_roundrobin) 1073 else O_DEC(opt, "unknown-server-time-limit", unknown_server_time_limit) 1074 #ifdef CLIENT_SUBNET 1075 else O_LST(opt, "send-client-subnet", client_subnet) 1076 else O_LST(opt, "client-subnet-zone", client_subnet_zone) 1077 else O_DEC(opt, "max-client-subnet-ipv4", max_client_subnet_ipv4) 1078 else O_DEC(opt, "max-client-subnet-ipv6", max_client_subnet_ipv6) 1079 else O_DEC(opt, "min-client-subnet-ipv4", min_client_subnet_ipv4) 1080 else O_DEC(opt, "min-client-subnet-ipv6", min_client_subnet_ipv6) 1081 else O_DEC(opt, "max-ecs-tree-size-ipv4", max_ecs_tree_size_ipv4) 1082 else O_DEC(opt, "max-ecs-tree-size-ipv6", max_ecs_tree_size_ipv6) 1083 else O_YNO(opt, "client-subnet-always-forward:", 1084 client_subnet_always_forward) 1085 #endif 1086 #ifdef USE_DNSTAP 1087 else O_YNO(opt, "dnstap-enable", dnstap) 1088 else O_YNO(opt, "dnstap-bidirectional", dnstap_bidirectional) 1089 else O_STR(opt, "dnstap-socket-path", dnstap_socket_path) 1090 else O_STR(opt, "dnstap-ip", dnstap_ip) 1091 else O_YNO(opt, "dnstap-tls", dnstap_tls) 1092 else O_STR(opt, "dnstap-tls-server-name", dnstap_tls_server_name) 1093 else O_STR(opt, "dnstap-tls-cert-bundle", dnstap_tls_cert_bundle) 1094 else O_STR(opt, "dnstap-tls-client-key-file", 1095 dnstap_tls_client_key_file) 1096 else O_STR(opt, "dnstap-tls-client-cert-file", 1097 dnstap_tls_client_cert_file) 1098 else O_YNO(opt, "dnstap-send-identity", dnstap_send_identity) 1099 else O_YNO(opt, "dnstap-send-version", dnstap_send_version) 1100 else O_STR(opt, "dnstap-identity", dnstap_identity) 1101 else O_STR(opt, "dnstap-version", dnstap_version) 1102 else O_YNO(opt, "dnstap-log-resolver-query-messages", 1103 dnstap_log_resolver_query_messages) 1104 else O_YNO(opt, "dnstap-log-resolver-response-messages", 1105 dnstap_log_resolver_response_messages) 1106 else O_YNO(opt, "dnstap-log-client-query-messages", 1107 dnstap_log_client_query_messages) 1108 else O_YNO(opt, "dnstap-log-client-response-messages", 1109 dnstap_log_client_response_messages) 1110 else O_YNO(opt, "dnstap-log-forwarder-query-messages", 1111 dnstap_log_forwarder_query_messages) 1112 else O_YNO(opt, "dnstap-log-forwarder-response-messages", 1113 dnstap_log_forwarder_response_messages) 1114 #endif 1115 #ifdef USE_DNSCRYPT 1116 else O_YNO(opt, "dnscrypt-enable", dnscrypt) 1117 else O_DEC(opt, "dnscrypt-port", dnscrypt_port) 1118 else O_STR(opt, "dnscrypt-provider", dnscrypt_provider) 1119 else O_LST(opt, "dnscrypt-provider-cert", dnscrypt_provider_cert) 1120 else O_LST(opt, "dnscrypt-provider-cert-rotated", dnscrypt_provider_cert_rotated) 1121 else O_LST(opt, "dnscrypt-secret-key", dnscrypt_secret_key) 1122 else O_MEM(opt, "dnscrypt-shared-secret-cache-size", 1123 dnscrypt_shared_secret_cache_size) 1124 else O_DEC(opt, "dnscrypt-shared-secret-cache-slabs", 1125 dnscrypt_shared_secret_cache_slabs) 1126 else O_MEM(opt, "dnscrypt-nonce-cache-size", 1127 dnscrypt_nonce_cache_size) 1128 else O_DEC(opt, "dnscrypt-nonce-cache-slabs", 1129 dnscrypt_nonce_cache_slabs) 1130 #endif 1131 else O_YNO(opt, "unblock-lan-zones", unblock_lan_zones) 1132 else O_YNO(opt, "insecure-lan-zones", insecure_lan_zones) 1133 else O_DEC(opt, "max-udp-size", max_udp_size) 1134 else O_LST(opt, "python-script", python_script) 1135 else O_LST(opt, "dynlib-file", dynlib_file) 1136 else O_YNO(opt, "disable-dnssec-lame-check", disable_dnssec_lame_check) 1137 else O_DEC(opt, "ip-ratelimit", ip_ratelimit) 1138 else O_DEC(opt, "ratelimit", ratelimit) 1139 else O_MEM(opt, "ip-ratelimit-size", ip_ratelimit_size) 1140 else O_MEM(opt, "ratelimit-size", ratelimit_size) 1141 else O_DEC(opt, "ip-ratelimit-slabs", ip_ratelimit_slabs) 1142 else O_DEC(opt, "ratelimit-slabs", ratelimit_slabs) 1143 else O_LS2(opt, "ratelimit-for-domain", ratelimit_for_domain) 1144 else O_LS2(opt, "ratelimit-below-domain", ratelimit_below_domain) 1145 else O_DEC(opt, "ip-ratelimit-factor", ip_ratelimit_factor) 1146 else O_DEC(opt, "ratelimit-factor", ratelimit_factor) 1147 else O_DEC(opt, "fast-server-num", fast_server_num) 1148 else O_DEC(opt, "fast-server-permil", fast_server_permil) 1149 else O_DEC(opt, "val-sig-skew-min", val_sig_skew_min) 1150 else O_DEC(opt, "val-sig-skew-max", val_sig_skew_max) 1151 else O_YNO(opt, "qname-minimisation", qname_minimisation) 1152 else O_YNO(opt, "qname-minimisation-strict", qname_minimisation_strict) 1153 else O_IFC(opt, "define-tag", num_tags, tagname) 1154 else O_LTG(opt, "local-zone-tag", local_zone_tags) 1155 else O_LTG(opt, "access-control-tag", acl_tags) 1156 else O_LTG(opt, "response-ip-tag", respip_tags) 1157 else O_LS3(opt, "local-zone-override", local_zone_overrides) 1158 else O_LS3(opt, "access-control-tag-action", acl_tag_actions) 1159 else O_LS3(opt, "access-control-tag-data", acl_tag_datas) 1160 else O_LS2(opt, "access-control-view", acl_view) 1161 else O_LS2(opt, "edns-client-strings", edns_client_strings) 1162 #ifdef USE_IPSECMOD 1163 else O_YNO(opt, "ipsecmod-enabled", ipsecmod_enabled) 1164 else O_YNO(opt, "ipsecmod-ignore-bogus", ipsecmod_ignore_bogus) 1165 else O_STR(opt, "ipsecmod-hook", ipsecmod_hook) 1166 else O_DEC(opt, "ipsecmod-max-ttl", ipsecmod_max_ttl) 1167 else O_LST(opt, "ipsecmod-whitelist", ipsecmod_whitelist) 1168 else O_YNO(opt, "ipsecmod-strict", ipsecmod_strict) 1169 #endif 1170 #ifdef USE_CACHEDB 1171 else O_STR(opt, "backend", cachedb_backend) 1172 else O_STR(opt, "secret-seed", cachedb_secret) 1173 #ifdef USE_REDIS 1174 else O_STR(opt, "redis-server-host", redis_server_host) 1175 else O_DEC(opt, "redis-server-port", redis_server_port) 1176 else O_DEC(opt, "redis-timeout", redis_timeout) 1177 else O_YNO(opt, "redis-expire-records", redis_expire_records) 1178 #endif /* USE_REDIS */ 1179 #endif /* USE_CACHEDB */ 1180 #ifdef USE_IPSET 1181 else O_STR(opt, "name-v4", ipset_name_v4) 1182 else O_STR(opt, "name-v6", ipset_name_v6) 1183 #endif 1184 /* not here: 1185 * outgoing-permit, outgoing-avoid - have list of ports 1186 * local-zone - zones and nodefault variables 1187 * local-data - see below 1188 * local-data-ptr - converted to local-data entries 1189 * stub-zone, name, stub-addr, stub-host, stub-prime 1190 * forward-zone, name, forward-addr, forward-host 1191 */ 1192 else return 0; 1193 return 1; 1194 } 1195 1196 /** initialize the global cfg_parser object */ 1197 static void 1198 create_cfg_parser(struct config_file* cfg, char* filename, const char* chroot) 1199 { 1200 static struct config_parser_state st; 1201 cfg_parser = &st; 1202 cfg_parser->filename = filename; 1203 cfg_parser->line = 1; 1204 cfg_parser->errors = 0; 1205 cfg_parser->cfg = cfg; 1206 cfg_parser->chroot = chroot; 1207 init_cfg_parse(); 1208 } 1209 1210 int 1211 config_read(struct config_file* cfg, const char* filename, const char* chroot) 1212 { 1213 FILE *in; 1214 char *fname = (char*)filename; 1215 #ifdef HAVE_GLOB 1216 glob_t g; 1217 size_t i; 1218 int r, flags; 1219 #endif 1220 if(!fname) 1221 return 1; 1222 1223 /* check for wildcards */ 1224 #ifdef HAVE_GLOB 1225 if(!(!strchr(fname, '*') && !strchr(fname, '?') && !strchr(fname, '[') && 1226 !strchr(fname, '{') && !strchr(fname, '~'))) { 1227 verbose(VERB_QUERY, "wildcard found, processing %s", fname); 1228 flags = 0 1229 #ifdef GLOB_ERR 1230 | GLOB_ERR 1231 #endif 1232 #ifdef GLOB_NOSORT 1233 | GLOB_NOSORT 1234 #endif 1235 #ifdef GLOB_BRACE 1236 | GLOB_BRACE 1237 #endif 1238 #ifdef GLOB_TILDE 1239 | GLOB_TILDE 1240 #endif 1241 ; 1242 memset(&g, 0, sizeof(g)); 1243 r = glob(fname, flags, NULL, &g); 1244 if(r) { 1245 /* some error */ 1246 globfree(&g); 1247 if(r == GLOB_NOMATCH) { 1248 verbose(VERB_QUERY, "include: " 1249 "no matches for %s", fname); 1250 return 1; 1251 } else if(r == GLOB_NOSPACE) { 1252 log_err("include: %s: " 1253 "fnametern out of memory", fname); 1254 } else if(r == GLOB_ABORTED) { 1255 log_err("wildcard include: %s: expansion " 1256 "aborted (%s)", fname, strerror(errno)); 1257 } else { 1258 log_err("wildcard include: %s: expansion " 1259 "failed (%s)", fname, strerror(errno)); 1260 } 1261 /* ignore globs that yield no files */ 1262 return 1; 1263 } 1264 /* process files found, if any */ 1265 for(i=0; i<(size_t)g.gl_pathc; i++) { 1266 if(!config_read(cfg, g.gl_pathv[i], chroot)) { 1267 log_err("error reading wildcard " 1268 "include: %s", g.gl_pathv[i]); 1269 globfree(&g); 1270 return 0; 1271 } 1272 } 1273 globfree(&g); 1274 return 1; 1275 } 1276 #endif /* HAVE_GLOB */ 1277 1278 in = fopen(fname, "r"); 1279 if(!in) { 1280 log_err("Could not open %s: %s", fname, strerror(errno)); 1281 return 0; 1282 } 1283 create_cfg_parser(cfg, fname, chroot); 1284 ub_c_in = in; 1285 ub_c_parse(); 1286 fclose(in); 1287 1288 if(!cfg->dnscrypt) cfg->dnscrypt_port = 0; 1289 1290 if(cfg_parser->errors != 0) { 1291 fprintf(stderr, "read %s failed: %d errors in configuration file\n", 1292 fname, cfg_parser->errors); 1293 errno=EINVAL; 1294 return 0; 1295 } 1296 1297 return 1; 1298 } 1299 1300 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm) 1301 { 1302 struct config_stub* p = *(*pp); 1303 while(p) { 1304 if(strcmp(p->name, nm) == 0) 1305 return p; 1306 (*pp) = &p->next; 1307 p = p->next; 1308 } 1309 return NULL; 1310 } 1311 1312 void 1313 config_delstrlist(struct config_strlist* p) 1314 { 1315 struct config_strlist *np; 1316 while(p) { 1317 np = p->next; 1318 free(p->str); 1319 free(p); 1320 p = np; 1321 } 1322 } 1323 1324 void 1325 config_deldblstrlist(struct config_str2list* p) 1326 { 1327 struct config_str2list *np; 1328 while(p) { 1329 np = p->next; 1330 free(p->str); 1331 free(p->str2); 1332 free(p); 1333 p = np; 1334 } 1335 } 1336 1337 void 1338 config_deltrplstrlist(struct config_str3list* p) 1339 { 1340 struct config_str3list *np; 1341 while(p) { 1342 np = p->next; 1343 free(p->str); 1344 free(p->str2); 1345 free(p->str3); 1346 free(p); 1347 p = np; 1348 } 1349 } 1350 1351 void 1352 config_delauth(struct config_auth* p) 1353 { 1354 if(!p) return; 1355 free(p->name); 1356 config_delstrlist(p->masters); 1357 config_delstrlist(p->urls); 1358 config_delstrlist(p->allow_notify); 1359 free(p->zonefile); 1360 free(p->rpz_taglist); 1361 free(p->rpz_action_override); 1362 free(p->rpz_cname); 1363 free(p->rpz_log_name); 1364 free(p); 1365 } 1366 1367 void 1368 config_delauths(struct config_auth* p) 1369 { 1370 struct config_auth* np; 1371 while(p) { 1372 np = p->next; 1373 config_delauth(p); 1374 p = np; 1375 } 1376 } 1377 1378 void 1379 config_delstub(struct config_stub* p) 1380 { 1381 if(!p) return; 1382 free(p->name); 1383 config_delstrlist(p->hosts); 1384 config_delstrlist(p->addrs); 1385 free(p); 1386 } 1387 1388 void 1389 config_delstubs(struct config_stub* p) 1390 { 1391 struct config_stub* np; 1392 while(p) { 1393 np = p->next; 1394 config_delstub(p); 1395 p = np; 1396 } 1397 } 1398 1399 void 1400 config_delview(struct config_view* p) 1401 { 1402 if(!p) return; 1403 free(p->name); 1404 config_deldblstrlist(p->local_zones); 1405 config_delstrlist(p->local_zones_nodefault); 1406 #ifdef USE_IPSET 1407 config_delstrlist(p->local_zones_ipset); 1408 #endif 1409 config_delstrlist(p->local_data); 1410 free(p); 1411 } 1412 1413 void 1414 config_delviews(struct config_view* p) 1415 { 1416 struct config_view* np; 1417 while(p) { 1418 np = p->next; 1419 config_delview(p); 1420 p = np; 1421 } 1422 } 1423 1424 void 1425 config_del_strarray(char** array, int num) 1426 { 1427 int i; 1428 if(!array) 1429 return; 1430 for(i=0; i<num; i++) { 1431 free(array[i]); 1432 } 1433 free(array); 1434 } 1435 1436 void 1437 config_del_strbytelist(struct config_strbytelist* p) 1438 { 1439 struct config_strbytelist* np; 1440 while(p) { 1441 np = p->next; 1442 free(p->str); 1443 free(p->str2); 1444 free(p); 1445 p = np; 1446 } 1447 } 1448 1449 void 1450 config_delete(struct config_file* cfg) 1451 { 1452 if(!cfg) return; 1453 free(cfg->username); 1454 free(cfg->chrootdir); 1455 free(cfg->directory); 1456 free(cfg->logfile); 1457 free(cfg->pidfile); 1458 free(cfg->target_fetch_policy); 1459 free(cfg->ssl_service_key); 1460 free(cfg->ssl_service_pem); 1461 free(cfg->tls_cert_bundle); 1462 config_delstrlist(cfg->tls_additional_port); 1463 config_delstrlist(cfg->tls_session_ticket_keys.first); 1464 free(cfg->tls_ciphers); 1465 free(cfg->tls_ciphersuites); 1466 free(cfg->http_endpoint); 1467 if(cfg->log_identity) { 1468 log_ident_revert_to_default(); 1469 free(cfg->log_identity); 1470 } 1471 config_del_strarray(cfg->ifs, cfg->num_ifs); 1472 config_del_strarray(cfg->out_ifs, cfg->num_out_ifs); 1473 config_delstubs(cfg->stubs); 1474 config_delstubs(cfg->forwards); 1475 config_delauths(cfg->auths); 1476 config_delviews(cfg->views); 1477 config_delstrlist(cfg->donotqueryaddrs); 1478 config_delstrlist(cfg->root_hints); 1479 #ifdef CLIENT_SUBNET 1480 config_delstrlist(cfg->client_subnet); 1481 config_delstrlist(cfg->client_subnet_zone); 1482 #endif 1483 free(cfg->identity); 1484 free(cfg->version); 1485 free(cfg->module_conf); 1486 free(cfg->outgoing_avail_ports); 1487 config_delstrlist(cfg->caps_whitelist); 1488 config_delstrlist(cfg->private_address); 1489 config_delstrlist(cfg->private_domain); 1490 config_delstrlist(cfg->auto_trust_anchor_file_list); 1491 config_delstrlist(cfg->trust_anchor_file_list); 1492 config_delstrlist(cfg->trusted_keys_file_list); 1493 config_delstrlist(cfg->trust_anchor_list); 1494 config_delstrlist(cfg->domain_insecure); 1495 config_deldblstrlist(cfg->acls); 1496 config_deldblstrlist(cfg->tcp_connection_limits); 1497 free(cfg->val_nsec3_key_iterations); 1498 config_deldblstrlist(cfg->local_zones); 1499 config_delstrlist(cfg->local_zones_nodefault); 1500 #ifdef USE_IPSET 1501 config_delstrlist(cfg->local_zones_ipset); 1502 #endif 1503 config_delstrlist(cfg->local_data); 1504 config_deltrplstrlist(cfg->local_zone_overrides); 1505 config_del_strarray(cfg->tagname, cfg->num_tags); 1506 config_del_strbytelist(cfg->local_zone_tags); 1507 config_del_strbytelist(cfg->acl_tags); 1508 config_del_strbytelist(cfg->respip_tags); 1509 config_deltrplstrlist(cfg->acl_tag_actions); 1510 config_deltrplstrlist(cfg->acl_tag_datas); 1511 config_delstrlist(cfg->control_ifs.first); 1512 free(cfg->server_key_file); 1513 free(cfg->server_cert_file); 1514 free(cfg->control_key_file); 1515 free(cfg->control_cert_file); 1516 free(cfg->dns64_prefix); 1517 config_delstrlist(cfg->dns64_ignore_aaaa); 1518 free(cfg->dnstap_socket_path); 1519 free(cfg->dnstap_ip); 1520 free(cfg->dnstap_tls_server_name); 1521 free(cfg->dnstap_tls_cert_bundle); 1522 free(cfg->dnstap_tls_client_key_file); 1523 free(cfg->dnstap_tls_client_cert_file); 1524 free(cfg->dnstap_identity); 1525 free(cfg->dnstap_version); 1526 config_deldblstrlist(cfg->ratelimit_for_domain); 1527 config_deldblstrlist(cfg->ratelimit_below_domain); 1528 config_delstrlist(cfg->python_script); 1529 config_delstrlist(cfg->dynlib_file); 1530 config_deldblstrlist(cfg->edns_client_strings); 1531 #ifdef USE_IPSECMOD 1532 free(cfg->ipsecmod_hook); 1533 config_delstrlist(cfg->ipsecmod_whitelist); 1534 #endif 1535 #ifdef USE_CACHEDB 1536 free(cfg->cachedb_backend); 1537 free(cfg->cachedb_secret); 1538 #ifdef USE_REDIS 1539 free(cfg->redis_server_host); 1540 #endif /* USE_REDIS */ 1541 #endif /* USE_CACHEDB */ 1542 #ifdef USE_IPSET 1543 free(cfg->ipset_name_v4); 1544 free(cfg->ipset_name_v6); 1545 #endif 1546 free(cfg); 1547 } 1548 1549 static void 1550 init_outgoing_availports(int* a, int num) 1551 { 1552 /* generated with make iana_update */ 1553 const int iana_assigned[] = { 1554 #include "util/iana_ports.inc" 1555 -1 }; /* end marker to put behind trailing comma */ 1556 1557 int i; 1558 /* do not use <1024, that could be trouble with the system, privs */ 1559 for(i=1024; i<num; i++) { 1560 a[i] = i; 1561 } 1562 /* create empty spot at 49152 to keep ephemeral ports available 1563 * to other programs */ 1564 for(i=49152; i<49152+256; i++) 1565 a[i] = 0; 1566 /* pick out all the IANA assigned ports */ 1567 for(i=0; iana_assigned[i]!=-1; i++) { 1568 if(iana_assigned[i] < num) 1569 a[iana_assigned[i]] = 0; 1570 } 1571 } 1572 1573 int 1574 cfg_mark_ports(const char* str, int allow, int* avail, int num) 1575 { 1576 char* mid = strchr(str, '-'); 1577 #ifdef DISABLE_EXPLICIT_PORT_RANDOMISATION 1578 log_warn("Explicit port randomisation disabled, ignoring " 1579 "outgoing-port-permit and outgoing-port-avoid configuration " 1580 "options"); 1581 #endif 1582 if(!mid) { 1583 int port = atoi(str); 1584 if(port == 0 && strcmp(str, "0") != 0) { 1585 log_err("cannot parse port number '%s'", str); 1586 return 0; 1587 } 1588 if(port < num) 1589 avail[port] = (allow?port:0); 1590 } else { 1591 int i, low, high = atoi(mid+1); 1592 char buf[16]; 1593 if(high == 0 && strcmp(mid+1, "0") != 0) { 1594 log_err("cannot parse port number '%s'", mid+1); 1595 return 0; 1596 } 1597 if( (int)(mid-str)+1 >= (int)sizeof(buf) ) { 1598 log_err("cannot parse port number '%s'", str); 1599 return 0; 1600 } 1601 if(mid > str) 1602 memcpy(buf, str, (size_t)(mid-str)); 1603 buf[mid-str] = 0; 1604 low = atoi(buf); 1605 if(low == 0 && strcmp(buf, "0") != 0) { 1606 log_err("cannot parse port number '%s'", buf); 1607 return 0; 1608 } 1609 for(i=low; i<=high; i++) { 1610 if(i < num) 1611 avail[i] = (allow?i:0); 1612 } 1613 return 1; 1614 } 1615 return 1; 1616 } 1617 1618 int 1619 cfg_scan_ports(int* avail, int num) 1620 { 1621 int i; 1622 int count = 0; 1623 for(i=0; i<num; i++) { 1624 if(avail[i]) 1625 count++; 1626 } 1627 return count; 1628 } 1629 1630 int cfg_condense_ports(struct config_file* cfg, int** avail) 1631 { 1632 int num = cfg_scan_ports(cfg->outgoing_avail_ports, 65536); 1633 int i, at = 0; 1634 *avail = NULL; 1635 if(num == 0) 1636 return 0; 1637 *avail = (int*)reallocarray(NULL, (size_t)num, sizeof(int)); 1638 if(!*avail) 1639 return 0; 1640 for(i=0; i<65536; i++) { 1641 if(cfg->outgoing_avail_ports[i]) 1642 (*avail)[at++] = cfg->outgoing_avail_ports[i]; 1643 } 1644 log_assert(at == num); 1645 return num; 1646 } 1647 1648 /** print error with file and line number */ 1649 static void ub_c_error_va_list(const char *fmt, va_list args) 1650 { 1651 cfg_parser->errors++; 1652 fprintf(stderr, "%s:%d: error: ", cfg_parser->filename, 1653 cfg_parser->line); 1654 vfprintf(stderr, fmt, args); 1655 fprintf(stderr, "\n"); 1656 } 1657 1658 /** print error with file and line number */ 1659 void ub_c_error_msg(const char* fmt, ...) 1660 { 1661 va_list args; 1662 va_start(args, fmt); 1663 ub_c_error_va_list(fmt, args); 1664 va_end(args); 1665 } 1666 1667 void ub_c_error(const char *str) 1668 { 1669 cfg_parser->errors++; 1670 fprintf(stderr, "%s:%d: error: %s\n", cfg_parser->filename, 1671 cfg_parser->line, str); 1672 } 1673 1674 int ub_c_wrap(void) 1675 { 1676 return 1; 1677 } 1678 1679 int cfg_strlist_append(struct config_strlist_head* list, char* item) 1680 { 1681 struct config_strlist *s; 1682 if(!item || !list) { 1683 free(item); 1684 return 0; 1685 } 1686 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 1687 if(!s) { 1688 free(item); 1689 return 0; 1690 } 1691 s->str = item; 1692 s->next = NULL; 1693 if(list->last) 1694 list->last->next = s; 1695 else 1696 list->first = s; 1697 list->last = s; 1698 return 1; 1699 } 1700 1701 int 1702 cfg_region_strlist_insert(struct regional* region, 1703 struct config_strlist** head, char* item) 1704 { 1705 struct config_strlist *s; 1706 if(!item || !head) 1707 return 0; 1708 s = (struct config_strlist*)regional_alloc_zero(region, 1709 sizeof(struct config_strlist)); 1710 if(!s) 1711 return 0; 1712 s->str = item; 1713 s->next = *head; 1714 *head = s; 1715 return 1; 1716 } 1717 1718 struct config_strlist* 1719 cfg_strlist_find(struct config_strlist* head, const char *item) 1720 { 1721 struct config_strlist *s = head; 1722 if(!head){ 1723 return NULL; 1724 } 1725 while(s) { 1726 if(strcmp(s->str, item) == 0) { 1727 return s; 1728 } 1729 s = s->next; 1730 } 1731 return NULL; 1732 } 1733 1734 int 1735 cfg_strlist_insert(struct config_strlist** head, char* item) 1736 { 1737 struct config_strlist *s; 1738 if(!item || !head) { 1739 free(item); 1740 return 0; 1741 } 1742 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 1743 if(!s) { 1744 free(item); 1745 return 0; 1746 } 1747 s->str = item; 1748 s->next = *head; 1749 *head = s; 1750 return 1; 1751 } 1752 1753 int 1754 cfg_strlist_append_ex(struct config_strlist** head, char* item) 1755 { 1756 struct config_strlist *s; 1757 if(!item || !head) 1758 return 0; 1759 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 1760 if(!s) 1761 return 0; 1762 s->str = item; 1763 s->next = NULL; 1764 1765 if (*head==NULL) { 1766 *head = s; 1767 } else { 1768 struct config_strlist *last = *head; 1769 while (last->next!=NULL) { 1770 last = last->next; 1771 } 1772 last->next = s; 1773 } 1774 1775 return 1; 1776 } 1777 1778 int 1779 cfg_str2list_insert(struct config_str2list** head, char* item, char* i2) 1780 { 1781 struct config_str2list *s; 1782 if(!item || !i2 || !head) { 1783 free(item); 1784 free(i2); 1785 return 0; 1786 } 1787 s = (struct config_str2list*)calloc(1, sizeof(struct config_str2list)); 1788 if(!s) { 1789 free(item); 1790 free(i2); 1791 return 0; 1792 } 1793 s->str = item; 1794 s->str2 = i2; 1795 s->next = *head; 1796 *head = s; 1797 return 1; 1798 } 1799 1800 int 1801 cfg_str3list_insert(struct config_str3list** head, char* item, char* i2, 1802 char* i3) 1803 { 1804 struct config_str3list *s; 1805 if(!item || !i2 || !i3 || !head) 1806 return 0; 1807 s = (struct config_str3list*)calloc(1, sizeof(struct config_str3list)); 1808 if(!s) 1809 return 0; 1810 s->str = item; 1811 s->str2 = i2; 1812 s->str3 = i3; 1813 s->next = *head; 1814 *head = s; 1815 return 1; 1816 } 1817 1818 int 1819 cfg_strbytelist_insert(struct config_strbytelist** head, char* item, 1820 uint8_t* i2, size_t i2len) 1821 { 1822 struct config_strbytelist* s; 1823 if(!item || !i2 || !head) 1824 return 0; 1825 s = (struct config_strbytelist*)calloc(1, sizeof(*s)); 1826 if(!s) 1827 return 0; 1828 s->str = item; 1829 s->str2 = i2; 1830 s->str2len = i2len; 1831 s->next = *head; 1832 *head = s; 1833 return 1; 1834 } 1835 1836 time_t 1837 cfg_convert_timeval(const char* str) 1838 { 1839 time_t t; 1840 struct tm tm; 1841 memset(&tm, 0, sizeof(tm)); 1842 if(strlen(str) < 14) 1843 return 0; 1844 if(sscanf(str, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, 1845 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) 1846 return 0; 1847 tm.tm_year -= 1900; 1848 tm.tm_mon--; 1849 /* Check values */ 1850 if (tm.tm_year < 70) return 0; 1851 if (tm.tm_mon < 0 || tm.tm_mon > 11) return 0; 1852 if (tm.tm_mday < 1 || tm.tm_mday > 31) return 0; 1853 if (tm.tm_hour < 0 || tm.tm_hour > 23) return 0; 1854 if (tm.tm_min < 0 || tm.tm_min > 59) return 0; 1855 if (tm.tm_sec < 0 || tm.tm_sec > 59) return 0; 1856 /* call ldns conversion function */ 1857 t = sldns_mktime_from_utc(&tm); 1858 return t; 1859 } 1860 1861 int 1862 cfg_count_numbers(const char* s) 1863 { 1864 /* format ::= (sp num)+ sp */ 1865 /* num ::= [-](0-9)+ */ 1866 /* sp ::= (space|tab)* */ 1867 int num = 0; 1868 while(*s) { 1869 while(*s && isspace((unsigned char)*s)) 1870 s++; 1871 if(!*s) /* end of string */ 1872 break; 1873 if(*s == '-') 1874 s++; 1875 if(!*s) /* only - not allowed */ 1876 return 0; 1877 if(!isdigit((unsigned char)*s)) /* bad character */ 1878 return 0; 1879 while(*s && isdigit((unsigned char)*s)) 1880 s++; 1881 num++; 1882 } 1883 return num; 1884 } 1885 1886 /** all digit number */ 1887 static int isalldigit(const char* str, size_t l) 1888 { 1889 size_t i; 1890 for(i=0; i<l; i++) 1891 if(!isdigit((unsigned char)str[i])) 1892 return 0; 1893 return 1; 1894 } 1895 1896 int 1897 cfg_parse_memsize(const char* str, size_t* res) 1898 { 1899 size_t len; 1900 size_t mult = 1; 1901 if(!str || (len=(size_t)strlen(str)) == 0) { 1902 log_err("not a size: '%s'", str); 1903 return 0; 1904 } 1905 if(isalldigit(str, len)) { 1906 *res = (size_t)atol(str); 1907 return 1; 1908 } 1909 /* check appended num */ 1910 while(len>0 && str[len-1]==' ') 1911 len--; 1912 if(len > 1 && str[len-1] == 'b') 1913 len--; 1914 else if(len > 1 && str[len-1] == 'B') 1915 len--; 1916 1917 if(len > 1 && tolower((unsigned char)str[len-1]) == 'g') 1918 mult = 1024*1024*1024; 1919 else if(len > 1 && tolower((unsigned char)str[len-1]) == 'm') 1920 mult = 1024*1024; 1921 else if(len > 1 && tolower((unsigned char)str[len-1]) == 'k') 1922 mult = 1024; 1923 else if(len > 0 && isdigit((unsigned char)str[len-1])) 1924 mult = 1; 1925 else { 1926 log_err("unknown size specifier: '%s'", str); 1927 return 0; 1928 } 1929 while(len>1 && str[len-2]==' ') 1930 len--; 1931 1932 if(!isalldigit(str, len-1)) { 1933 log_err("unknown size specifier: '%s'", str); 1934 return 0; 1935 } 1936 *res = ((size_t)atol(str)) * mult; 1937 return 1; 1938 } 1939 1940 int 1941 find_tag_id(struct config_file* cfg, const char* tag) 1942 { 1943 int i; 1944 for(i=0; i<cfg->num_tags; i++) { 1945 if(strcmp(cfg->tagname[i], tag) == 0) 1946 return i; 1947 } 1948 return -1; 1949 } 1950 1951 int 1952 config_add_tag(struct config_file* cfg, const char* tag) 1953 { 1954 char** newarray; 1955 char* newtag; 1956 if(find_tag_id(cfg, tag) != -1) 1957 return 1; /* nothing to do */ 1958 newarray = (char**)malloc(sizeof(char*)*(cfg->num_tags+1)); 1959 if(!newarray) 1960 return 0; 1961 newtag = strdup(tag); 1962 if(!newtag) { 1963 free(newarray); 1964 return 0; 1965 } 1966 if(cfg->tagname) { 1967 memcpy(newarray, cfg->tagname, sizeof(char*)*cfg->num_tags); 1968 free(cfg->tagname); 1969 } 1970 newarray[cfg->num_tags++] = newtag; 1971 cfg->tagname = newarray; 1972 return 1; 1973 } 1974 1975 /** set a bit in a bit array */ 1976 static void 1977 cfg_set_bit(uint8_t* bitlist, size_t len, int id) 1978 { 1979 int pos = id/8; 1980 log_assert((size_t)pos < len); 1981 (void)len; 1982 bitlist[pos] |= 1<<(id%8); 1983 } 1984 1985 uint8_t* config_parse_taglist(struct config_file* cfg, char* str, 1986 size_t* listlen) 1987 { 1988 uint8_t* taglist = NULL; 1989 size_t len = 0; 1990 char* p, *s; 1991 1992 /* allocate */ 1993 if(cfg->num_tags == 0) { 1994 log_err("parse taglist, but no tags defined"); 1995 return 0; 1996 } 1997 len = (size_t)(cfg->num_tags+7)/8; 1998 taglist = calloc(1, len); 1999 if(!taglist) { 2000 log_err("out of memory"); 2001 return 0; 2002 } 2003 2004 /* parse */ 2005 s = str; 2006 while((p=strsep(&s, " \t\n")) != NULL) { 2007 if(*p) { 2008 int id = find_tag_id(cfg, p); 2009 /* set this bit in the bitlist */ 2010 if(id == -1) { 2011 log_err("unknown tag: %s", p); 2012 free(taglist); 2013 return 0; 2014 } 2015 cfg_set_bit(taglist, len, id); 2016 } 2017 } 2018 2019 *listlen = len; 2020 return taglist; 2021 } 2022 2023 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist, 2024 size_t taglen) 2025 { 2026 char buf[10240]; 2027 size_t i, j, len = 0; 2028 buf[0] = 0; 2029 for(i=0; i<taglen; i++) { 2030 if(taglist[i] == 0) 2031 continue; 2032 for(j=0; j<8; j++) { 2033 if((taglist[i] & (1<<j)) != 0) { 2034 size_t id = i*8 + j; 2035 snprintf(buf+len, sizeof(buf)-len, "%s%s", 2036 (len==0?"":" "), cfg->tagname[id]); 2037 len += strlen(buf+len); 2038 } 2039 } 2040 } 2041 return strdup(buf); 2042 } 2043 2044 int taglist_intersect(uint8_t* list1, size_t list1len, const uint8_t* list2, 2045 size_t list2len) 2046 { 2047 size_t i; 2048 if(!list1 || !list2) 2049 return 0; 2050 for(i=0; i<list1len && i<list2len; i++) { 2051 if((list1[i] & list2[i]) != 0) 2052 return 1; 2053 } 2054 return 0; 2055 } 2056 2057 void 2058 config_apply(struct config_file* config) 2059 { 2060 MAX_TTL = (time_t)config->max_ttl; 2061 MIN_TTL = (time_t)config->min_ttl; 2062 SERVE_EXPIRED = config->serve_expired; 2063 SERVE_EXPIRED_TTL = (time_t)config->serve_expired_ttl; 2064 SERVE_EXPIRED_REPLY_TTL = (time_t)config->serve_expired_reply_ttl; 2065 MAX_NEG_TTL = (time_t)config->max_negative_ttl; 2066 RTT_MIN_TIMEOUT = config->infra_cache_min_rtt; 2067 EDNS_ADVERTISED_SIZE = (uint16_t)config->edns_buffer_size; 2068 MINIMAL_RESPONSES = config->minimal_responses; 2069 RRSET_ROUNDROBIN = config->rrset_roundrobin; 2070 LOG_TAG_QUERYREPLY = config->log_tag_queryreply; 2071 UNKNOWN_SERVER_NICENESS = config->unknown_server_time_limit; 2072 log_set_time_asc(config->log_time_ascii); 2073 autr_permit_small_holddown = config->permit_small_holddown; 2074 stream_wait_max = config->stream_wait_size; 2075 http2_query_buffer_max = config->http_query_buffer_size; 2076 http2_response_buffer_max = config->http_response_buffer_size; 2077 } 2078 2079 void config_lookup_uid(struct config_file* cfg) 2080 { 2081 #ifdef HAVE_GETPWNAM 2082 /* translate username into uid and gid */ 2083 if(cfg->username && cfg->username[0]) { 2084 struct passwd *pwd; 2085 if((pwd = getpwnam(cfg->username)) != NULL) { 2086 cfg_uid = pwd->pw_uid; 2087 cfg_gid = pwd->pw_gid; 2088 } 2089 } 2090 #else 2091 (void)cfg; 2092 #endif 2093 } 2094 2095 /** 2096 * Calculate string length of full pathname in original filesys 2097 * @param fname: the path name to convert. 2098 * Must not be null or empty. 2099 * @param cfg: config struct for chroot and chdir (if set). 2100 * @param use_chdir: if false, only chroot is applied. 2101 * @return length of string. 2102 * remember to allocate one more for 0 at end in mallocs. 2103 */ 2104 static size_t 2105 strlen_after_chroot(const char* fname, struct config_file* cfg, int use_chdir) 2106 { 2107 size_t len = 0; 2108 int slashit = 0; 2109 if(cfg->chrootdir && cfg->chrootdir[0] && 2110 strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) { 2111 /* already full pathname, return it */ 2112 return strlen(fname); 2113 } 2114 /* chroot */ 2115 if(cfg->chrootdir && cfg->chrootdir[0]) { 2116 /* start with chrootdir */ 2117 len += strlen(cfg->chrootdir); 2118 slashit = 1; 2119 } 2120 /* chdir */ 2121 #ifdef UB_ON_WINDOWS 2122 if(fname[0] != 0 && fname[1] == ':') { 2123 /* full path, no chdir */ 2124 } else 2125 #endif 2126 if(fname[0] == '/' || !use_chdir) { 2127 /* full path, no chdir */ 2128 } else if(cfg->directory && cfg->directory[0]) { 2129 /* prepend chdir */ 2130 if(slashit && cfg->directory[0] != '/') 2131 len++; 2132 if(cfg->chrootdir && cfg->chrootdir[0] && 2133 strncmp(cfg->chrootdir, cfg->directory, 2134 strlen(cfg->chrootdir)) == 0) 2135 len += strlen(cfg->directory)-strlen(cfg->chrootdir); 2136 else len += strlen(cfg->directory); 2137 slashit = 1; 2138 } 2139 /* fname */ 2140 if(slashit && fname[0] != '/') 2141 len++; 2142 len += strlen(fname); 2143 return len; 2144 } 2145 2146 char* 2147 fname_after_chroot(const char* fname, struct config_file* cfg, int use_chdir) 2148 { 2149 size_t len = strlen_after_chroot(fname, cfg, use_chdir)+1; 2150 int slashit = 0; 2151 char* buf = (char*)malloc(len); 2152 if(!buf) 2153 return NULL; 2154 buf[0] = 0; 2155 /* is fname already in chroot ? */ 2156 if(cfg->chrootdir && cfg->chrootdir[0] && 2157 strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) { 2158 /* already full pathname, return it */ 2159 (void)strlcpy(buf, fname, len); 2160 buf[len-1] = 0; 2161 return buf; 2162 } 2163 /* chroot */ 2164 if(cfg->chrootdir && cfg->chrootdir[0]) { 2165 /* start with chrootdir */ 2166 (void)strlcpy(buf, cfg->chrootdir, len); 2167 slashit = 1; 2168 } 2169 #ifdef UB_ON_WINDOWS 2170 if(fname[0] != 0 && fname[1] == ':') { 2171 /* full path, no chdir */ 2172 } else 2173 #endif 2174 /* chdir */ 2175 if(fname[0] == '/' || !use_chdir) { 2176 /* full path, no chdir */ 2177 } else if(cfg->directory && cfg->directory[0]) { 2178 /* prepend chdir */ 2179 if(slashit && cfg->directory[0] != '/') 2180 (void)strlcat(buf, "/", len); 2181 /* is the directory already in the chroot? */ 2182 if(cfg->chrootdir && cfg->chrootdir[0] && 2183 strncmp(cfg->chrootdir, cfg->directory, 2184 strlen(cfg->chrootdir)) == 0) 2185 (void)strlcat(buf, cfg->directory+strlen(cfg->chrootdir), 2186 len); 2187 else (void)strlcat(buf, cfg->directory, len); 2188 slashit = 1; 2189 } 2190 /* fname */ 2191 if(slashit && fname[0] != '/') 2192 (void)strlcat(buf, "/", len); 2193 (void)strlcat(buf, fname, len); 2194 buf[len-1] = 0; 2195 return buf; 2196 } 2197 2198 /** return next space character in string */ 2199 static char* next_space_pos(const char* str) 2200 { 2201 char* sp = strchr(str, ' '); 2202 char* tab = strchr(str, '\t'); 2203 if(!tab && !sp) 2204 return NULL; 2205 if(!sp) return tab; 2206 if(!tab) return sp; 2207 return (sp<tab)?sp:tab; 2208 } 2209 2210 /** return last space character in string */ 2211 static char* last_space_pos(const char* str) 2212 { 2213 char* sp = strrchr(str, ' '); 2214 char* tab = strrchr(str, '\t'); 2215 if(!tab && !sp) 2216 return NULL; 2217 if(!sp) return tab; 2218 if(!tab) return sp; 2219 return (sp>tab)?sp:tab; 2220 } 2221 2222 int 2223 cfg_parse_local_zone(struct config_file* cfg, const char* val) 2224 { 2225 const char *type, *name_end, *name; 2226 char buf[256]; 2227 2228 /* parse it as: [zone_name] [between stuff] [zone_type] */ 2229 name = val; 2230 while(*name && isspace((unsigned char)*name)) 2231 name++; 2232 if(!*name) { 2233 log_err("syntax error: too short: %s", val); 2234 return 0; 2235 } 2236 name_end = next_space_pos(name); 2237 if(!name_end || !*name_end) { 2238 log_err("syntax error: expected zone type: %s", val); 2239 return 0; 2240 } 2241 if (name_end - name > 255) { 2242 log_err("syntax error: bad zone name: %s", val); 2243 return 0; 2244 } 2245 (void)strlcpy(buf, name, sizeof(buf)); 2246 buf[name_end-name] = '\0'; 2247 2248 type = last_space_pos(name_end); 2249 while(type && *type && isspace((unsigned char)*type)) 2250 type++; 2251 if(!type || !*type) { 2252 log_err("syntax error: expected zone type: %s", val); 2253 return 0; 2254 } 2255 2256 if(strcmp(type, "nodefault")==0) { 2257 return cfg_strlist_insert(&cfg->local_zones_nodefault, 2258 strdup(name)); 2259 #ifdef USE_IPSET 2260 } else if(strcmp(type, "ipset")==0) { 2261 return cfg_strlist_insert(&cfg->local_zones_ipset, 2262 strdup(name)); 2263 #endif 2264 } else { 2265 return cfg_str2list_insert(&cfg->local_zones, strdup(buf), 2266 strdup(type)); 2267 } 2268 } 2269 2270 char* cfg_ptr_reverse(char* str) 2271 { 2272 char* ip, *ip_end; 2273 char* name; 2274 char* result; 2275 char buf[1024]; 2276 struct sockaddr_storage addr; 2277 socklen_t addrlen; 2278 2279 /* parse it as: [IP] [between stuff] [name] */ 2280 ip = str; 2281 while(*ip && isspace((unsigned char)*ip)) 2282 ip++; 2283 if(!*ip) { 2284 log_err("syntax error: too short: %s", str); 2285 return NULL; 2286 } 2287 ip_end = next_space_pos(ip); 2288 if(!ip_end || !*ip_end) { 2289 log_err("syntax error: expected name: %s", str); 2290 return NULL; 2291 } 2292 2293 name = last_space_pos(ip_end); 2294 if(!name || !*name) { 2295 log_err("syntax error: expected name: %s", str); 2296 return NULL; 2297 } 2298 2299 sscanf(ip, "%100s", buf); 2300 buf[sizeof(buf)-1]=0; 2301 2302 if(!ipstrtoaddr(buf, UNBOUND_DNS_PORT, &addr, &addrlen)) { 2303 log_err("syntax error: cannot parse address: %s", str); 2304 return NULL; 2305 } 2306 2307 /* reverse IPv4: 2308 * ddd.ddd.ddd.ddd.in-addr-arpa. 2309 * IPv6: (h.){32}.ip6.arpa. */ 2310 2311 if(addr_is_ip6(&addr, addrlen)) { 2312 uint8_t ad[16]; 2313 const char* hex = "0123456789abcdef"; 2314 char *p = buf; 2315 int i; 2316 memmove(ad, &((struct sockaddr_in6*)&addr)->sin6_addr, 2317 sizeof(ad)); 2318 for(i=15; i>=0; i--) { 2319 uint8_t b = ad[i]; 2320 *p++ = hex[ (b&0x0f) ]; 2321 *p++ = '.'; 2322 *p++ = hex[ (b&0xf0) >> 4 ]; 2323 *p++ = '.'; 2324 } 2325 snprintf(buf+16*4, sizeof(buf)-16*4, "ip6.arpa. "); 2326 } else { 2327 uint8_t ad[4]; 2328 memmove(ad, &((struct sockaddr_in*)&addr)->sin_addr, 2329 sizeof(ad)); 2330 snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa. ", 2331 (unsigned)ad[3], (unsigned)ad[2], 2332 (unsigned)ad[1], (unsigned)ad[0]); 2333 } 2334 2335 /* printed the reverse address, now the between goop and name on end */ 2336 while(*ip_end && isspace((unsigned char)*ip_end)) 2337 ip_end++; 2338 if(name>ip_end) { 2339 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%.*s", 2340 (int)(name-ip_end), ip_end); 2341 } 2342 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), " PTR %s", name); 2343 2344 result = strdup(buf); 2345 if(!result) { 2346 log_err("out of memory parsing %s", str); 2347 return NULL; 2348 } 2349 return result; 2350 } 2351 2352 #ifdef UB_ON_WINDOWS 2353 char* 2354 w_lookup_reg_str(const char* key, const char* name) 2355 { 2356 HKEY hk = NULL; 2357 DWORD type = 0; 2358 BYTE buf[1024]; 2359 DWORD len = (DWORD)sizeof(buf); 2360 LONG ret; 2361 char* result = NULL; 2362 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hk); 2363 if(ret == ERROR_FILE_NOT_FOUND) 2364 return NULL; /* key does not exist */ 2365 else if(ret != ERROR_SUCCESS) { 2366 log_err("RegOpenKeyEx failed"); 2367 return NULL; 2368 } 2369 ret = RegQueryValueEx(hk, (LPCTSTR)name, 0, &type, buf, &len); 2370 if(RegCloseKey(hk)) 2371 log_err("RegCloseKey"); 2372 if(ret == ERROR_FILE_NOT_FOUND) 2373 return NULL; /* name does not exist */ 2374 else if(ret != ERROR_SUCCESS) { 2375 log_err("RegQueryValueEx failed"); 2376 return NULL; 2377 } 2378 if(type == REG_SZ || type == REG_MULTI_SZ || type == REG_EXPAND_SZ) { 2379 buf[sizeof(buf)-1] = 0; 2380 buf[sizeof(buf)-2] = 0; /* for multi_sz */ 2381 result = strdup((char*)buf); 2382 if(!result) log_err("out of memory"); 2383 } 2384 return result; 2385 } 2386 2387 void w_config_adjust_directory(struct config_file* cfg) 2388 { 2389 if(cfg->directory && cfg->directory[0]) { 2390 TCHAR dirbuf[2*MAX_PATH+4]; 2391 if(strcmp(cfg->directory, "%EXECUTABLE%") == 0) { 2392 /* get executable path, and if that contains 2393 * directories, snip off the filename part */ 2394 dirbuf[0] = 0; 2395 if(!GetModuleFileName(NULL, dirbuf, MAX_PATH)) 2396 log_err("could not GetModuleFileName"); 2397 if(strrchr(dirbuf, '\\')) { 2398 (strrchr(dirbuf, '\\'))[0] = 0; 2399 } else log_err("GetModuleFileName had no path"); 2400 if(dirbuf[0]) { 2401 /* adjust directory for later lookups to work*/ 2402 free(cfg->directory); 2403 cfg->directory = memdup(dirbuf, strlen(dirbuf)+1); 2404 } 2405 } 2406 } 2407 } 2408 #endif /* UB_ON_WINDOWS */ 2409 2410 void errinf(struct module_qstate* qstate, const char* str) 2411 { 2412 struct config_strlist* p; 2413 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !str) 2414 return; 2415 p = (struct config_strlist*)regional_alloc(qstate->region, sizeof(*p)); 2416 if(!p) { 2417 log_err("malloc failure in validator-error-info string"); 2418 return; 2419 } 2420 p->next = NULL; 2421 p->str = regional_strdup(qstate->region, str); 2422 if(!p->str) { 2423 log_err("malloc failure in validator-error-info string"); 2424 return; 2425 } 2426 /* add at end */ 2427 if(qstate->errinf) { 2428 struct config_strlist* q = qstate->errinf; 2429 while(q->next) 2430 q = q->next; 2431 q->next = p; 2432 } else qstate->errinf = p; 2433 } 2434 2435 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin) 2436 { 2437 struct sock_list* p; 2438 if(qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) 2439 return; 2440 for(p=origin; p; p=p->next) { 2441 char buf[256]; 2442 if(p == origin) 2443 snprintf(buf, sizeof(buf), "from "); 2444 else snprintf(buf, sizeof(buf), "and "); 2445 if(p->len == 0) 2446 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), 2447 "cache"); 2448 else 2449 addr_to_str(&p->addr, p->len, buf+strlen(buf), 2450 sizeof(buf)-strlen(buf)); 2451 errinf(qstate, buf); 2452 } 2453 } 2454 2455 char* errinf_to_str_bogus(struct module_qstate* qstate) 2456 { 2457 char buf[20480]; 2458 char* p = buf; 2459 size_t left = sizeof(buf); 2460 struct config_strlist* s; 2461 char dname[LDNS_MAX_DOMAINLEN+1]; 2462 char t[16], c[16]; 2463 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t)); 2464 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c)); 2465 dname_str(qstate->qinfo.qname, dname); 2466 snprintf(p, left, "validation failure <%s %s %s>:", dname, t, c); 2467 left -= strlen(p); p += strlen(p); 2468 if(!qstate->errinf) 2469 snprintf(p, left, " misc failure"); 2470 else for(s=qstate->errinf; s; s=s->next) { 2471 snprintf(p, left, " %s", s->str); 2472 left -= strlen(p); p += strlen(p); 2473 } 2474 p = strdup(buf); 2475 if(!p) 2476 log_err("malloc failure in errinf_to_str"); 2477 return p; 2478 } 2479 2480 char* errinf_to_str_servfail(struct module_qstate* qstate) 2481 { 2482 char buf[20480]; 2483 char* p = buf; 2484 size_t left = sizeof(buf); 2485 struct config_strlist* s; 2486 char dname[LDNS_MAX_DOMAINLEN+1]; 2487 char t[16], c[16]; 2488 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t)); 2489 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c)); 2490 dname_str(qstate->qinfo.qname, dname); 2491 snprintf(p, left, "SERVFAIL <%s %s %s>:", dname, t, c); 2492 left -= strlen(p); p += strlen(p); 2493 if(!qstate->errinf) 2494 snprintf(p, left, " misc failure"); 2495 else for(s=qstate->errinf; s; s=s->next) { 2496 snprintf(p, left, " %s", s->str); 2497 left -= strlen(p); p += strlen(p); 2498 } 2499 p = strdup(buf); 2500 if(!p) 2501 log_err("malloc failure in errinf_to_str"); 2502 return p; 2503 } 2504 2505 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr) 2506 { 2507 char buf[1024]; 2508 char dname[LDNS_MAX_DOMAINLEN+1]; 2509 char t[16], c[16]; 2510 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !rr) 2511 return; 2512 sldns_wire2str_type_buf(ntohs(rr->rk.type), t, sizeof(t)); 2513 sldns_wire2str_class_buf(ntohs(rr->rk.rrset_class), c, sizeof(c)); 2514 dname_str(rr->rk.dname, dname); 2515 snprintf(buf, sizeof(buf), "for <%s %s %s>", dname, t, c); 2516 errinf(qstate, buf); 2517 } 2518 2519 void errinf_dname(struct module_qstate* qstate, const char* str, uint8_t* dname) 2520 { 2521 char b[1024]; 2522 char buf[LDNS_MAX_DOMAINLEN+1]; 2523 if((qstate->env->cfg->val_log_level < 2 && !qstate->env->cfg->log_servfail) || !str || !dname) 2524 return; 2525 dname_str(dname, buf); 2526 snprintf(b, sizeof(b), "%s %s", str, buf); 2527 errinf(qstate, b); 2528 } 2529 2530 int options_remote_is_address(struct config_file* cfg) 2531 { 2532 if(!cfg->remote_control_enable) return 0; 2533 if(!cfg->control_ifs.first) return 1; 2534 if(!cfg->control_ifs.first->str) return 1; 2535 if(cfg->control_ifs.first->str[0] == 0) return 1; 2536 return (cfg->control_ifs.first->str[0] != '/'); 2537 } 2538 2539