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