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