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