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