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