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