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