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 HAVE_PWD_H 66 #include <pwd.h> 67 #endif 68 69 /** from cfg username, after daemonise setup performed */ 70 uid_t cfg_uid = (uid_t)-1; 71 /** from cfg username, after daemonise setup performed */ 72 gid_t cfg_gid = (gid_t)-1; 73 /** for debug allow small timeout values for fast rollovers */ 74 int autr_permit_small_holddown = 0; 75 76 /** global config during parsing */ 77 struct config_parser_state* cfg_parser = 0; 78 79 /** init ports possible for use */ 80 static void init_outgoing_availports(int* array, int num); 81 82 struct config_file* 83 config_create(void) 84 { 85 struct config_file* cfg; 86 cfg = (struct config_file*)calloc(1, sizeof(struct config_file)); 87 if(!cfg) 88 return NULL; 89 /* the defaults if no config is present */ 90 cfg->verbosity = 1; 91 cfg->stat_interval = 0; 92 cfg->stat_cumulative = 0; 93 cfg->stat_extended = 0; 94 cfg->num_threads = 1; 95 cfg->port = UNBOUND_DNS_PORT; 96 cfg->do_ip4 = 1; 97 cfg->do_ip6 = 1; 98 cfg->do_udp = 1; 99 cfg->do_tcp = 1; 100 cfg->tcp_upstream = 0; 101 cfg->ssl_service_key = NULL; 102 cfg->ssl_service_pem = NULL; 103 cfg->ssl_port = 443; 104 cfg->ssl_upstream = 0; 105 cfg->use_syslog = 1; 106 cfg->log_time_ascii = 0; 107 cfg->log_queries = 0; 108 #ifndef USE_WINSOCK 109 # ifdef USE_MINI_EVENT 110 /* select max 1024 sockets */ 111 cfg->outgoing_num_ports = 960; 112 cfg->num_queries_per_thread = 512; 113 # else 114 /* libevent can use many sockets */ 115 cfg->outgoing_num_ports = 4096; 116 cfg->num_queries_per_thread = 1024; 117 # endif 118 cfg->outgoing_num_tcp = 10; 119 cfg->incoming_num_tcp = 10; 120 #else 121 cfg->outgoing_num_ports = 48; /* windows is limited in num fds */ 122 cfg->num_queries_per_thread = 24; 123 cfg->outgoing_num_tcp = 2; /* leaves 64-52=12 for: 4if,1stop,thread4 */ 124 cfg->incoming_num_tcp = 2; 125 #endif 126 cfg->edns_buffer_size = 4096; /* 4k from rfc recommendation */ 127 cfg->msg_buffer_size = 65552; /* 64 k + a small margin */ 128 cfg->msg_cache_size = 4 * 1024 * 1024; 129 cfg->msg_cache_slabs = 4; 130 cfg->jostle_time = 200; 131 cfg->rrset_cache_size = 4 * 1024 * 1024; 132 cfg->rrset_cache_slabs = 4; 133 cfg->host_ttl = 900; 134 cfg->bogus_ttl = 60; 135 cfg->min_ttl = 0; 136 cfg->max_ttl = 3600 * 24; 137 cfg->max_negative_ttl = 3600; 138 cfg->prefetch = 0; 139 cfg->prefetch_key = 0; 140 cfg->infra_cache_slabs = 4; 141 cfg->infra_cache_numhosts = 10000; 142 cfg->infra_cache_min_rtt = 50; 143 cfg->delay_close = 0; 144 if(!(cfg->outgoing_avail_ports = (int*)calloc(65536, sizeof(int)))) 145 goto error_exit; 146 init_outgoing_availports(cfg->outgoing_avail_ports, 65536); 147 if(!(cfg->username = strdup(UB_USERNAME))) goto error_exit; 148 #ifdef HAVE_CHROOT 149 if(!(cfg->chrootdir = strdup(CHROOT_DIR))) goto error_exit; 150 #endif 151 if(!(cfg->directory = strdup(RUN_DIR))) goto error_exit; 152 if(!(cfg->logfile = strdup(""))) goto error_exit; 153 if(!(cfg->pidfile = strdup(PIDFILE))) goto error_exit; 154 if(!(cfg->target_fetch_policy = strdup("3 2 1 0 0"))) goto error_exit; 155 cfg->donotqueryaddrs = NULL; 156 cfg->donotquery_localhost = 1; 157 cfg->root_hints = NULL; 158 cfg->do_daemonize = 1; 159 cfg->if_automatic = 0; 160 cfg->so_rcvbuf = 0; 161 cfg->so_sndbuf = 0; 162 cfg->so_reuseport = 0; 163 cfg->ip_transparent = 0; 164 cfg->num_ifs = 0; 165 cfg->ifs = NULL; 166 cfg->num_out_ifs = 0; 167 cfg->out_ifs = NULL; 168 cfg->stubs = NULL; 169 cfg->forwards = NULL; 170 cfg->acls = NULL; 171 cfg->harden_short_bufsize = 0; 172 cfg->harden_large_queries = 0; 173 cfg->harden_glue = 1; 174 cfg->harden_dnssec_stripped = 1; 175 cfg->harden_below_nxdomain = 0; 176 cfg->harden_referral_path = 0; 177 cfg->harden_algo_downgrade = 0; 178 cfg->use_caps_bits_for_id = 0; 179 cfg->caps_whitelist = NULL; 180 cfg->private_address = NULL; 181 cfg->private_domain = NULL; 182 cfg->unwanted_threshold = 0; 183 cfg->hide_identity = 0; 184 cfg->hide_version = 0; 185 cfg->identity = NULL; 186 cfg->version = NULL; 187 cfg->auto_trust_anchor_file_list = NULL; 188 cfg->trust_anchor_file_list = NULL; 189 cfg->trust_anchor_list = NULL; 190 cfg->trusted_keys_file_list = NULL; 191 cfg->dlv_anchor_file = NULL; 192 cfg->dlv_anchor_list = NULL; 193 cfg->domain_insecure = NULL; 194 cfg->val_date_override = 0; 195 cfg->val_sig_skew_min = 3600; /* at least daylight savings trouble */ 196 cfg->val_sig_skew_max = 86400; /* at most timezone settings trouble */ 197 cfg->val_clean_additional = 1; 198 cfg->val_log_level = 0; 199 cfg->val_log_squelch = 0; 200 cfg->val_permissive_mode = 0; 201 cfg->ignore_cd = 0; 202 cfg->add_holddown = 30*24*3600; 203 cfg->del_holddown = 30*24*3600; 204 cfg->keep_missing = 366*24*3600; /* one year plus a little leeway */ 205 cfg->permit_small_holddown = 0; 206 cfg->key_cache_size = 4 * 1024 * 1024; 207 cfg->key_cache_slabs = 4; 208 cfg->neg_cache_size = 1 * 1024 * 1024; 209 cfg->local_zones = NULL; 210 cfg->local_zones_nodefault = NULL; 211 cfg->local_data = NULL; 212 cfg->unblock_lan_zones = 0; 213 cfg->python_script = NULL; 214 cfg->remote_control_enable = 0; 215 cfg->control_ifs = NULL; 216 cfg->control_port = UNBOUND_CONTROL_PORT; 217 cfg->remote_control_use_cert = 1; 218 cfg->minimal_responses = 0; 219 cfg->rrset_roundrobin = 0; 220 cfg->max_udp_size = 4096; 221 if(!(cfg->server_key_file = strdup(RUN_DIR"/unbound_server.key"))) 222 goto error_exit; 223 if(!(cfg->server_cert_file = strdup(RUN_DIR"/unbound_server.pem"))) 224 goto error_exit; 225 if(!(cfg->control_key_file = strdup(RUN_DIR"/unbound_control.key"))) 226 goto error_exit; 227 if(!(cfg->control_cert_file = strdup(RUN_DIR"/unbound_control.pem"))) 228 goto error_exit; 229 230 if(!(cfg->module_conf = strdup("validator iterator"))) goto error_exit; 231 if(!(cfg->val_nsec3_key_iterations = 232 strdup("1024 150 2048 500 4096 2500"))) goto error_exit; 233 #if defined(DNSTAP_SOCKET_PATH) 234 if(!(cfg->dnstap_socket_path = strdup(DNSTAP_SOCKET_PATH))) 235 goto error_exit; 236 #endif 237 cfg->ratelimit = 0; 238 cfg->ratelimit_slabs = 4; 239 cfg->ratelimit_size = 4*1024*1024; 240 cfg->ratelimit_for_domain = NULL; 241 cfg->ratelimit_below_domain = NULL; 242 cfg->ratelimit_factor = 10; 243 return cfg; 244 error_exit: 245 config_delete(cfg); 246 return NULL; 247 } 248 249 struct config_file* config_create_forlib(void) 250 { 251 struct config_file* cfg = config_create(); 252 if(!cfg) return NULL; 253 /* modifications for library use, less verbose, less memory */ 254 free(cfg->chrootdir); 255 cfg->chrootdir = NULL; 256 cfg->verbosity = 0; 257 cfg->outgoing_num_ports = 16; /* in library use, this is 'reasonable' 258 and probably within the ulimit(maxfds) of the user */ 259 cfg->outgoing_num_tcp = 2; 260 cfg->msg_cache_size = 1024*1024; 261 cfg->msg_cache_slabs = 1; 262 cfg->rrset_cache_size = 1024*1024; 263 cfg->rrset_cache_slabs = 1; 264 cfg->infra_cache_slabs = 1; 265 cfg->use_syslog = 0; 266 cfg->key_cache_size = 1024*1024; 267 cfg->key_cache_slabs = 1; 268 cfg->neg_cache_size = 100 * 1024; 269 cfg->donotquery_localhost = 0; /* allow, so that you can ask a 270 forward nameserver running on localhost */ 271 cfg->val_log_level = 2; /* to fill why_bogus with */ 272 cfg->val_log_squelch = 1; 273 return cfg; 274 } 275 276 /** check that the value passed is >= 0 */ 277 #define IS_NUMBER_OR_ZERO \ 278 if(atoi(val) == 0 && strcmp(val, "0") != 0) return 0 279 /** check that the value passed is > 0 */ 280 #define IS_NONZERO_NUMBER \ 281 if(atoi(val) == 0) return 0 282 /** check that the value passed is not 0 and a power of 2 */ 283 #define IS_POW2_NUMBER \ 284 if(atoi(val) == 0 || !is_pow2((size_t)atoi(val))) return 0 285 /** check that the value passed is yes or no */ 286 #define IS_YES_OR_NO \ 287 if(strcmp(val, "yes") != 0 && strcmp(val, "no") != 0) return 0 288 /** put integer_or_zero into variable */ 289 #define S_NUMBER_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 290 { IS_NUMBER_OR_ZERO; cfg->var = atoi(val); } 291 /** put integer_nonzero into variable */ 292 #define S_NUMBER_NONZERO(str, var) if(strcmp(opt, str) == 0) \ 293 { IS_NONZERO_NUMBER; cfg->var = atoi(val); } 294 /** put integer_or_zero into unsigned */ 295 #define S_UNSIGNED_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 296 { IS_NUMBER_OR_ZERO; cfg->var = (unsigned)atoi(val); } 297 /** put integer_or_zero into size_t */ 298 #define S_SIZET_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 299 { IS_NUMBER_OR_ZERO; cfg->var = (size_t)atoi(val); } 300 /** put integer_nonzero into size_t */ 301 #define S_SIZET_NONZERO(str, var) if(strcmp(opt, str) == 0) \ 302 { IS_NONZERO_NUMBER; cfg->var = (size_t)atoi(val); } 303 /** put yesno into variable */ 304 #define S_YNO(str, var) if(strcmp(opt, str) == 0) \ 305 { IS_YES_OR_NO; cfg->var = (strcmp(val, "yes") == 0); } 306 /** put memsize into variable */ 307 #define S_MEMSIZE(str, var) if(strcmp(opt, str)==0) \ 308 { return cfg_parse_memsize(val, &cfg->var); } 309 /** put pow2 number into variable */ 310 #define S_POW2(str, var) if(strcmp(opt, str)==0) \ 311 { IS_POW2_NUMBER; cfg->var = (size_t)atoi(val); } 312 /** put string into variable */ 313 #define S_STR(str, var) if(strcmp(opt, str)==0) \ 314 { free(cfg->var); return (cfg->var = strdup(val)) != NULL; } 315 /** put string into strlist */ 316 #define S_STRLIST(str, var) if(strcmp(opt, str)==0) \ 317 { return cfg_strlist_insert(&cfg->var, strdup(val)); } 318 319 int config_set_option(struct config_file* cfg, const char* opt, 320 const char* val) 321 { 322 S_NUMBER_OR_ZERO("verbosity:", verbosity) 323 else if(strcmp(opt, "statistics-interval:") == 0) { 324 if(strcmp(val, "0") == 0 || strcmp(val, "") == 0) 325 cfg->stat_interval = 0; 326 else if(atoi(val) == 0) 327 return 0; 328 else cfg->stat_interval = atoi(val); 329 } else if(strcmp(opt, "num_threads:") == 0) { 330 /* not supported, library must have 1 thread in bgworker */ 331 return 0; 332 } else if(strcmp(opt, "outgoing-port-permit:") == 0) { 333 return cfg_mark_ports(val, 1, 334 cfg->outgoing_avail_ports, 65536); 335 } else if(strcmp(opt, "outgoing-port-avoid:") == 0) { 336 return cfg_mark_ports(val, 0, 337 cfg->outgoing_avail_ports, 65536); 338 } else if(strcmp(opt, "local-zone:") == 0) { 339 return cfg_parse_local_zone(cfg, val); 340 } else if(strcmp(opt, "val-override-date:") == 0) { 341 if(strcmp(val, "") == 0 || strcmp(val, "0") == 0) { 342 cfg->val_date_override = 0; 343 } else if(strlen(val) == 14) { 344 cfg->val_date_override = cfg_convert_timeval(val); 345 return cfg->val_date_override != 0; 346 } else { 347 if(atoi(val) == 0) return 0; 348 cfg->val_date_override = (uint32_t)atoi(val); 349 } 350 } else if(strcmp(opt, "local-data-ptr:") == 0) { 351 char* ptr = cfg_ptr_reverse((char*)opt); 352 return cfg_strlist_insert(&cfg->local_data, ptr); 353 } else if(strcmp(opt, "logfile:") == 0) { 354 cfg->use_syslog = 0; 355 free(cfg->logfile); 356 return (cfg->logfile = strdup(val)) != NULL; 357 } 358 else if(strcmp(opt, "log-time-ascii:") == 0) 359 { IS_YES_OR_NO; cfg->log_time_ascii = (strcmp(val, "yes") == 0); 360 log_set_time_asc(cfg->log_time_ascii); } 361 else S_SIZET_NONZERO("max-udp-size:", max_udp_size) 362 else S_YNO("use-syslog:", use_syslog) 363 else S_YNO("extended-statistics:", stat_extended) 364 else S_YNO("statistics-cumulative:", stat_cumulative) 365 else S_YNO("do-ip4:", do_ip4) 366 else S_YNO("do-ip6:", do_ip6) 367 else S_YNO("do-udp:", do_udp) 368 else S_YNO("do-tcp:", do_tcp) 369 else S_YNO("tcp-upstream:", tcp_upstream) 370 else S_YNO("ssl-upstream:", ssl_upstream) 371 else S_STR("ssl-service-key:", ssl_service_key) 372 else S_STR("ssl-service-pem:", ssl_service_pem) 373 else S_NUMBER_NONZERO("ssl-port:", ssl_port) 374 else S_YNO("interface-automatic:", if_automatic) 375 else S_YNO("do-daemonize:", do_daemonize) 376 else S_NUMBER_NONZERO("port:", port) 377 else S_NUMBER_NONZERO("outgoing-range:", outgoing_num_ports) 378 else S_SIZET_OR_ZERO("outgoing-num-tcp:", outgoing_num_tcp) 379 else S_SIZET_OR_ZERO("incoming-num-tcp:", incoming_num_tcp) 380 else S_SIZET_NONZERO("edns-buffer-size:", edns_buffer_size) 381 else S_SIZET_NONZERO("msg-buffer-size:", msg_buffer_size) 382 else S_MEMSIZE("msg-cache-size:", msg_cache_size) 383 else S_POW2("msg-cache-slabs:", msg_cache_slabs) 384 else S_SIZET_NONZERO("num-queries-per-thread:",num_queries_per_thread) 385 else S_SIZET_OR_ZERO("jostle-timeout:", jostle_time) 386 else S_MEMSIZE("so-rcvbuf:", so_rcvbuf) 387 else S_MEMSIZE("so-sndbuf:", so_sndbuf) 388 else S_YNO("so-reuseport:", so_reuseport) 389 else S_YNO("ip-transparent:", ip_transparent) 390 else S_MEMSIZE("rrset-cache-size:", rrset_cache_size) 391 else S_POW2("rrset-cache-slabs:", rrset_cache_slabs) 392 else S_YNO("prefetch:", prefetch) 393 else S_YNO("prefetch-key:", prefetch_key) 394 else if(strcmp(opt, "cache-max-ttl:") == 0) 395 { IS_NUMBER_OR_ZERO; cfg->max_ttl = atoi(val); MAX_TTL=(time_t)cfg->max_ttl;} 396 else if(strcmp(opt, "cache-max-negative-ttl:") == 0) 397 { IS_NUMBER_OR_ZERO; cfg->max_negative_ttl = atoi(val); MAX_NEG_TTL=(time_t)cfg->max_negative_ttl;} 398 else if(strcmp(opt, "cache-min-ttl:") == 0) 399 { IS_NUMBER_OR_ZERO; cfg->min_ttl = atoi(val); MIN_TTL=(time_t)cfg->min_ttl;} 400 else if(strcmp(opt, "infra-cache-min-rtt:") == 0) { 401 IS_NUMBER_OR_ZERO; cfg->infra_cache_min_rtt = atoi(val); 402 RTT_MIN_TIMEOUT=cfg->infra_cache_min_rtt; 403 } 404 else S_NUMBER_OR_ZERO("infra-host-ttl:", host_ttl) 405 else S_POW2("infra-cache-slabs:", infra_cache_slabs) 406 else S_SIZET_NONZERO("infra-cache-numhosts:", infra_cache_numhosts) 407 else S_NUMBER_OR_ZERO("delay-close:", delay_close) 408 else S_STR("chroot:", chrootdir) 409 else S_STR("username:", username) 410 else S_STR("directory:", directory) 411 else S_STR("pidfile:", pidfile) 412 else S_YNO("hide-identity:", hide_identity) 413 else S_YNO("hide-version:", hide_version) 414 else S_STR("identity:", identity) 415 else S_STR("version:", version) 416 else S_STRLIST("root-hints:", root_hints) 417 else S_STR("target-fetch-policy:", target_fetch_policy) 418 else S_YNO("harden-glue:", harden_glue) 419 else S_YNO("harden-short-bufsize:", harden_short_bufsize) 420 else S_YNO("harden-large-queries:", harden_large_queries) 421 else S_YNO("harden-dnssec-stripped:", harden_dnssec_stripped) 422 else S_YNO("harden-below-nxdomain:", harden_below_nxdomain) 423 else S_YNO("harden-referral-path:", harden_referral_path) 424 else S_YNO("harden-algo-downgrade:", harden_algo_downgrade) 425 else S_YNO("use-caps-for-id", use_caps_bits_for_id) 426 else S_STRLIST("caps-whitelist:", caps_whitelist) 427 else S_SIZET_OR_ZERO("unwanted-reply-threshold:", unwanted_threshold) 428 else S_STRLIST("private-address:", private_address) 429 else S_STRLIST("private-domain:", private_domain) 430 else S_YNO("do-not-query-localhost:", donotquery_localhost) 431 else S_STRLIST("do-not-query-address:", donotqueryaddrs) 432 else S_STRLIST("auto-trust-anchor-file:", auto_trust_anchor_file_list) 433 else S_STRLIST("trust-anchor-file:", trust_anchor_file_list) 434 else S_STRLIST("trust-anchor:", trust_anchor_list) 435 else S_STRLIST("trusted-keys-file:", trusted_keys_file_list) 436 else S_STR("dlv-anchor-file:", dlv_anchor_file) 437 else S_STRLIST("dlv-anchor:", dlv_anchor_list) 438 else S_STRLIST("domain-insecure:", domain_insecure) 439 else S_NUMBER_OR_ZERO("val-bogus-ttl:", bogus_ttl) 440 else S_YNO("val-clean-additional:", val_clean_additional) 441 else S_NUMBER_OR_ZERO("val-log-level:", val_log_level) 442 else S_YNO("val-log-squelch:", val_log_squelch) 443 else S_YNO("log-queries:", log_queries) 444 else S_YNO("val-permissive-mode:", val_permissive_mode) 445 else S_YNO("ignore-cd-flag:", ignore_cd) 446 else S_STR("val-nsec3-keysize-iterations:", val_nsec3_key_iterations) 447 else S_UNSIGNED_OR_ZERO("add-holddown:", add_holddown) 448 else S_UNSIGNED_OR_ZERO("del-holddown:", del_holddown) 449 else S_UNSIGNED_OR_ZERO("keep-missing:", keep_missing) 450 else if(strcmp(opt, "permit-small-holddown:") == 0) 451 { IS_YES_OR_NO; cfg->permit_small_holddown = (strcmp(val, "yes") == 0); 452 autr_permit_small_holddown = cfg->permit_small_holddown; } 453 else S_MEMSIZE("key-cache-size:", key_cache_size) 454 else S_POW2("key-cache-slabs:", key_cache_slabs) 455 else S_MEMSIZE("neg-cache-size:", neg_cache_size) 456 else S_YNO("minimal-responses:", minimal_responses) 457 else S_YNO("rrset-roundrobin:", rrset_roundrobin) 458 else S_STRLIST("local-data:", local_data) 459 else S_YNO("unblock-lan-zones:", unblock_lan_zones) 460 else S_YNO("control-enable:", remote_control_enable) 461 else S_STRLIST("control-interface:", control_ifs) 462 else S_NUMBER_NONZERO("control-port:", control_port) 463 else S_STR("server-key-file:", server_key_file) 464 else S_STR("server-cert-file:", server_cert_file) 465 else S_STR("control-key-file:", control_key_file) 466 else S_STR("control-cert-file:", control_cert_file) 467 else S_STR("module-config:", module_conf) 468 else S_STR("python-script:", python_script) 469 else if(strcmp(opt, "ratelimit:") == 0) { 470 IS_NUMBER_OR_ZERO; cfg->ratelimit = atoi(val); 471 infra_dp_ratelimit=cfg->ratelimit; 472 } 473 else S_MEMSIZE("ratelimit-size:", ratelimit_size) 474 else S_POW2("ratelimit-slabs:", ratelimit_slabs) 475 else S_NUMBER_OR_ZERO("ratelimit-factor:", ratelimit_factor) 476 /* val_sig_skew_min and max are copied into val_env during init, 477 * so this does not update val_env with set_option */ 478 else if(strcmp(opt, "val-sig-skew-min:") == 0) 479 { IS_NUMBER_OR_ZERO; cfg->val_sig_skew_min = (int32_t)atoi(val); } 480 else if(strcmp(opt, "val-sig-skew-max:") == 0) 481 { IS_NUMBER_OR_ZERO; cfg->val_sig_skew_max = (int32_t)atoi(val); } 482 else if (strcmp(opt, "outgoing-interface:") == 0) { 483 char* d = strdup(val); 484 char** oi = 485 (char**)reallocarray(NULL, (size_t)cfg->num_out_ifs+1, sizeof(char*)); 486 if(!d || !oi) { free(d); free(oi); return -1; } 487 if(cfg->out_ifs && cfg->num_out_ifs) { 488 memmove(oi, cfg->out_ifs, cfg->num_out_ifs*sizeof(char*)); 489 free(cfg->out_ifs); 490 } 491 oi[cfg->num_out_ifs++] = d; 492 cfg->out_ifs = oi; 493 } else { 494 /* unknown or unsupported (from the set_option interface): 495 * interface, outgoing-interface, access-control, 496 * stub-zone, name, stub-addr, stub-host, stub-prime 497 * forward-first, stub-first, 498 * forward-zone, name, forward-addr, forward-host, 499 * ratelimit-for-domain, ratelimit-below-domain */ 500 return 0; 501 } 502 return 1; 503 } 504 505 void config_print_func(char* line, void* arg) 506 { 507 FILE* f = (FILE*)arg; 508 (void)fprintf(f, "%s\n", line); 509 } 510 511 /** collate func arg */ 512 struct config_collate_arg { 513 /** list of result items */ 514 struct config_strlist_head list; 515 /** if a malloc error occurred, 0 is OK */ 516 int status; 517 }; 518 519 void config_collate_func(char* line, void* arg) 520 { 521 struct config_collate_arg* m = (struct config_collate_arg*)arg; 522 if(m->status) 523 return; 524 if(!cfg_strlist_append(&m->list, strdup(line))) 525 m->status = 1; 526 } 527 528 int config_get_option_list(struct config_file* cfg, const char* opt, 529 struct config_strlist** list) 530 { 531 struct config_collate_arg m; 532 memset(&m, 0, sizeof(m)); 533 *list = NULL; 534 if(!config_get_option(cfg, opt, config_collate_func, &m)) 535 return 1; 536 if(m.status) { 537 config_delstrlist(m.list.first); 538 return 2; 539 } 540 *list = m.list.first; 541 return 0; 542 } 543 544 int 545 config_get_option_collate(struct config_file* cfg, const char* opt, char** str) 546 { 547 struct config_strlist* list = NULL; 548 int r; 549 *str = NULL; 550 if((r = config_get_option_list(cfg, opt, &list)) != 0) 551 return r; 552 *str = config_collate_cat(list); 553 config_delstrlist(list); 554 if(!*str) return 2; 555 return 0; 556 } 557 558 char* 559 config_collate_cat(struct config_strlist* list) 560 { 561 size_t total = 0, left; 562 struct config_strlist* s; 563 char *r, *w; 564 if(!list) /* no elements */ 565 return strdup(""); 566 if(list->next == NULL) /* one element , no newline at end. */ 567 return strdup(list->str); 568 /* count total length */ 569 for(s=list; s; s=s->next) 570 total += strlen(s->str) + 1; /* len + newline */ 571 left = total+1; /* one extra for nul at end */ 572 r = malloc(left); 573 if(!r) 574 return NULL; 575 w = r; 576 for(s=list; s; s=s->next) { 577 size_t this = strlen(s->str); 578 if(this+2 > left) { /* sanity check */ 579 free(r); 580 return NULL; 581 } 582 snprintf(w, left, "%s\n", s->str); 583 this = strlen(w); 584 w += this; 585 left -= this; 586 } 587 return r; 588 } 589 590 /** compare and print decimal option */ 591 #define O_DEC(opt, str, var) if(strcmp(opt, str)==0) \ 592 {snprintf(buf, len, "%d", (int)cfg->var); \ 593 func(buf, arg);} 594 /** compare and print unsigned option */ 595 #define O_UNS(opt, str, var) if(strcmp(opt, str)==0) \ 596 {snprintf(buf, len, "%u", (unsigned)cfg->var); \ 597 func(buf, arg);} 598 /** compare and print yesno option */ 599 #define O_YNO(opt, str, var) if(strcmp(opt, str)==0) \ 600 {func(cfg->var?"yes":"no", arg);} 601 /** compare and print string option */ 602 #define O_STR(opt, str, var) if(strcmp(opt, str)==0) \ 603 {func(cfg->var?cfg->var:"", arg);} 604 /** compare and print array option */ 605 #define O_IFC(opt, str, num, arr) if(strcmp(opt, str)==0) \ 606 {int i; for(i=0; i<cfg->num; i++) func(cfg->arr[i], arg);} 607 /** compare and print memorysize option */ 608 #define O_MEM(opt, str, var) if(strcmp(opt, str)==0) { \ 609 if(cfg->var > 1024*1024*1024) { \ 610 size_t f=cfg->var/(size_t)1000000, b=cfg->var%(size_t)1000000; \ 611 snprintf(buf, len, "%u%6.6u", (unsigned)f, (unsigned)b); \ 612 } else snprintf(buf, len, "%u", (unsigned)cfg->var); \ 613 func(buf, arg);} 614 /** compare and print list option */ 615 #define O_LST(opt, name, lst) if(strcmp(opt, name)==0) { \ 616 struct config_strlist* p = cfg->lst; \ 617 for(p = cfg->lst; p; p = p->next) \ 618 func(p->str, arg); \ 619 } 620 /** compare and print list option */ 621 #define O_LS2(opt, name, lst) if(strcmp(opt, name)==0) { \ 622 struct config_str2list* p = cfg->lst; \ 623 for(p = cfg->lst; p; p = p->next) \ 624 snprintf(buf, len, "%s %s\n", p->str, p->str2); \ 625 func(buf, arg); \ 626 } 627 628 int 629 config_get_option(struct config_file* cfg, const char* opt, 630 void (*func)(char*,void*), void* arg) 631 { 632 char buf[1024]; 633 size_t len = sizeof(buf); 634 fptr_ok(fptr_whitelist_print_func(func)); 635 O_DEC(opt, "verbosity", verbosity) 636 else O_DEC(opt, "statistics-interval", stat_interval) 637 else O_YNO(opt, "statistics-cumulative", stat_cumulative) 638 else O_YNO(opt, "extended-statistics", stat_extended) 639 else O_YNO(opt, "use-syslog", use_syslog) 640 else O_YNO(opt, "log-time-ascii", log_time_ascii) 641 else O_DEC(opt, "num-threads", num_threads) 642 else O_IFC(opt, "interface", num_ifs, ifs) 643 else O_IFC(opt, "outgoing-interface", num_out_ifs, out_ifs) 644 else O_YNO(opt, "interface-automatic", if_automatic) 645 else O_DEC(opt, "port", port) 646 else O_DEC(opt, "outgoing-range", outgoing_num_ports) 647 else O_DEC(opt, "outgoing-num-tcp", outgoing_num_tcp) 648 else O_DEC(opt, "incoming-num-tcp", incoming_num_tcp) 649 else O_DEC(opt, "edns-buffer-size", edns_buffer_size) 650 else O_DEC(opt, "msg-buffer-size", msg_buffer_size) 651 else O_MEM(opt, "msg-cache-size", msg_cache_size) 652 else O_DEC(opt, "msg-cache-slabs", msg_cache_slabs) 653 else O_DEC(opt, "num-queries-per-thread", num_queries_per_thread) 654 else O_UNS(opt, "jostle-timeout", jostle_time) 655 else O_MEM(opt, "so-rcvbuf", so_rcvbuf) 656 else O_MEM(opt, "so-sndbuf", so_sndbuf) 657 else O_YNO(opt, "so-reuseport", so_reuseport) 658 else O_YNO(opt, "ip-transparent", ip_transparent) 659 else O_MEM(opt, "rrset-cache-size", rrset_cache_size) 660 else O_DEC(opt, "rrset-cache-slabs", rrset_cache_slabs) 661 else O_YNO(opt, "prefetch-key", prefetch_key) 662 else O_YNO(opt, "prefetch", prefetch) 663 else O_DEC(opt, "cache-max-ttl", max_ttl) 664 else O_DEC(opt, "cache-max-negative-ttl", max_negative_ttl) 665 else O_DEC(opt, "cache-min-ttl", min_ttl) 666 else O_DEC(opt, "infra-host-ttl", host_ttl) 667 else O_DEC(opt, "infra-cache-slabs", infra_cache_slabs) 668 else O_DEC(opt, "infra-cache-min-rtt", infra_cache_min_rtt) 669 else O_MEM(opt, "infra-cache-numhosts", infra_cache_numhosts) 670 else O_UNS(opt, "delay-close", delay_close) 671 else O_YNO(opt, "do-ip4", do_ip4) 672 else O_YNO(opt, "do-ip6", do_ip6) 673 else O_YNO(opt, "do-udp", do_udp) 674 else O_YNO(opt, "do-tcp", do_tcp) 675 else O_YNO(opt, "tcp-upstream", tcp_upstream) 676 else O_YNO(opt, "ssl-upstream", ssl_upstream) 677 else O_STR(opt, "ssl-service-key", ssl_service_key) 678 else O_STR(opt, "ssl-service-pem", ssl_service_pem) 679 else O_DEC(opt, "ssl-port", ssl_port) 680 else O_YNO(opt, "do-daemonize", do_daemonize) 681 else O_STR(opt, "chroot", chrootdir) 682 else O_STR(opt, "username", username) 683 else O_STR(opt, "directory", directory) 684 else O_STR(opt, "logfile", logfile) 685 else O_YNO(opt, "log-queries", log_queries) 686 else O_STR(opt, "pidfile", pidfile) 687 else O_YNO(opt, "hide-identity", hide_identity) 688 else O_YNO(opt, "hide-version", hide_version) 689 else O_STR(opt, "identity", identity) 690 else O_STR(opt, "version", version) 691 else O_STR(opt, "target-fetch-policy", target_fetch_policy) 692 else O_YNO(opt, "harden-short-bufsize", harden_short_bufsize) 693 else O_YNO(opt, "harden-large-queries", harden_large_queries) 694 else O_YNO(opt, "harden-glue", harden_glue) 695 else O_YNO(opt, "harden-dnssec-stripped", harden_dnssec_stripped) 696 else O_YNO(opt, "harden-below-nxdomain", harden_below_nxdomain) 697 else O_YNO(opt, "harden-referral-path", harden_referral_path) 698 else O_YNO(opt, "harden-algo-downgrade", harden_algo_downgrade) 699 else O_YNO(opt, "use-caps-for-id", use_caps_bits_for_id) 700 else O_LST(opt, "caps-whitelist", caps_whitelist) 701 else O_DEC(opt, "unwanted-reply-threshold", unwanted_threshold) 702 else O_YNO(opt, "do-not-query-localhost", donotquery_localhost) 703 else O_STR(opt, "module-config", module_conf) 704 else O_STR(opt, "dlv-anchor-file", dlv_anchor_file) 705 else O_DEC(opt, "val-bogus-ttl", bogus_ttl) 706 else O_YNO(opt, "val-clean-additional", val_clean_additional) 707 else O_DEC(opt, "val-log-level", val_log_level) 708 else O_YNO(opt, "val-permissive-mode", val_permissive_mode) 709 else O_YNO(opt, "ignore-cd-flag", ignore_cd) 710 else O_STR(opt, "val-nsec3-keysize-iterations",val_nsec3_key_iterations) 711 else O_UNS(opt, "add-holddown", add_holddown) 712 else O_UNS(opt, "del-holddown", del_holddown) 713 else O_UNS(opt, "keep-missing", keep_missing) 714 else O_YNO(opt, "permit-small-holddown", permit_small_holddown) 715 else O_MEM(opt, "key-cache-size", key_cache_size) 716 else O_DEC(opt, "key-cache-slabs", key_cache_slabs) 717 else O_MEM(opt, "neg-cache-size", neg_cache_size) 718 else O_YNO(opt, "control-enable", remote_control_enable) 719 else O_DEC(opt, "control-port", control_port) 720 else O_STR(opt, "server-key-file", server_key_file) 721 else O_STR(opt, "server-cert-file", server_cert_file) 722 else O_STR(opt, "control-key-file", control_key_file) 723 else O_STR(opt, "control-cert-file", control_cert_file) 724 else O_LST(opt, "root-hints", root_hints) 725 else O_LS2(opt, "access-control", acls) 726 else O_LST(opt, "do-not-query-address", donotqueryaddrs) 727 else O_LST(opt, "private-address", private_address) 728 else O_LST(opt, "private-domain", private_domain) 729 else O_LST(opt, "auto-trust-anchor-file", auto_trust_anchor_file_list) 730 else O_LST(opt, "trust-anchor-file", trust_anchor_file_list) 731 else O_LST(opt, "trust-anchor", trust_anchor_list) 732 else O_LST(opt, "trusted-keys-file", trusted_keys_file_list) 733 else O_LST(opt, "dlv-anchor", dlv_anchor_list) 734 else O_LST(opt, "control-interface", control_ifs) 735 else O_LST(opt, "domain-insecure", domain_insecure) 736 else O_UNS(opt, "val-override-date", val_date_override) 737 else O_YNO(opt, "minimal-responses", minimal_responses) 738 else O_YNO(opt, "rrset-roundrobin", rrset_roundrobin) 739 else O_YNO(opt, "unblock-lan-zones", unblock_lan_zones) 740 else O_DEC(opt, "max-udp-size", max_udp_size) 741 else O_STR(opt, "python-script", python_script) 742 else O_DEC(opt, "ratelimit", ratelimit) 743 else O_MEM(opt, "ratelimit-size", ratelimit_size) 744 else O_DEC(opt, "ratelimit-slabs", ratelimit_slabs) 745 else O_LS2(opt, "ratelimit-for-domain", ratelimit_for_domain) 746 else O_LS2(opt, "ratelimit-below-domain", ratelimit_below_domain) 747 else O_DEC(opt, "ratelimit-factor", ratelimit_factor) 748 else O_DEC(opt, "val-sig-skew-min", val_sig_skew_min) 749 else O_DEC(opt, "val-sig-skew-max", val_sig_skew_max) 750 /* not here: 751 * outgoing-permit, outgoing-avoid - have list of ports 752 * local-zone - zones and nodefault variables 753 * local-data - see below 754 * local-data-ptr - converted to local-data entries 755 * stub-zone, name, stub-addr, stub-host, stub-prime 756 * forward-zone, name, forward-addr, forward-host 757 */ 758 else return 0; 759 return 1; 760 } 761 762 /** initialize the global cfg_parser object */ 763 static void 764 create_cfg_parser(struct config_file* cfg, char* filename, const char* chroot) 765 { 766 static struct config_parser_state st; 767 cfg_parser = &st; 768 cfg_parser->filename = filename; 769 cfg_parser->line = 1; 770 cfg_parser->errors = 0; 771 cfg_parser->cfg = cfg; 772 cfg_parser->chroot = chroot; 773 init_cfg_parse(); 774 } 775 776 int 777 config_read(struct config_file* cfg, const char* filename, const char* chroot) 778 { 779 FILE *in; 780 char *fname = (char*)filename; 781 #ifdef HAVE_GLOB 782 glob_t g; 783 size_t i; 784 int r, flags; 785 #endif 786 if(!fname) 787 return 1; 788 789 /* check for wildcards */ 790 #ifdef HAVE_GLOB 791 if(!(!strchr(fname, '*') && !strchr(fname, '?') && !strchr(fname, '[') && 792 !strchr(fname, '{') && !strchr(fname, '~'))) { 793 verbose(VERB_QUERY, "wildcard found, processing %s", fname); 794 flags = 0 795 #ifdef GLOB_ERR 796 | GLOB_ERR 797 #endif 798 #ifdef GLOB_NOSORT 799 | GLOB_NOSORT 800 #endif 801 #ifdef GLOB_BRACE 802 | GLOB_BRACE 803 #endif 804 #ifdef GLOB_TILDE 805 | GLOB_TILDE 806 #endif 807 ; 808 memset(&g, 0, sizeof(g)); 809 r = glob(fname, flags, NULL, &g); 810 if(r) { 811 /* some error */ 812 globfree(&g); 813 if(r == GLOB_NOMATCH) { 814 verbose(VERB_QUERY, "include: " 815 "no matches for %s", fname); 816 return 1; 817 } else if(r == GLOB_NOSPACE) { 818 log_err("include: %s: " 819 "fnametern out of memory", fname); 820 } else if(r == GLOB_ABORTED) { 821 log_err("wildcard include: %s: expansion " 822 "aborted (%s)", fname, strerror(errno)); 823 } else { 824 log_err("wildcard include: %s: expansion " 825 "failed (%s)", fname, strerror(errno)); 826 } 827 /* ignore globs that yield no files */ 828 return 1; 829 } 830 /* process files found, if any */ 831 for(i=0; i<(size_t)g.gl_pathc; i++) { 832 if(!config_read(cfg, g.gl_pathv[i], chroot)) { 833 log_err("error reading wildcard " 834 "include: %s", g.gl_pathv[i]); 835 globfree(&g); 836 return 0; 837 } 838 } 839 globfree(&g); 840 return 1; 841 } 842 #endif /* HAVE_GLOB */ 843 844 in = fopen(fname, "r"); 845 if(!in) { 846 log_err("Could not open %s: %s", fname, strerror(errno)); 847 return 0; 848 } 849 create_cfg_parser(cfg, fname, chroot); 850 ub_c_in = in; 851 ub_c_parse(); 852 fclose(in); 853 854 if(cfg_parser->errors != 0) { 855 fprintf(stderr, "read %s failed: %d errors in configuration file\n", 856 fname, cfg_parser->errors); 857 errno=EINVAL; 858 return 0; 859 } 860 861 return 1; 862 } 863 864 void 865 config_delstrlist(struct config_strlist* p) 866 { 867 struct config_strlist *np; 868 while(p) { 869 np = p->next; 870 free(p->str); 871 free(p); 872 p = np; 873 } 874 } 875 876 void 877 config_deldblstrlist(struct config_str2list* p) 878 { 879 struct config_str2list *np; 880 while(p) { 881 np = p->next; 882 free(p->str); 883 free(p->str2); 884 free(p); 885 p = np; 886 } 887 } 888 889 void 890 config_delstubs(struct config_stub* p) 891 { 892 struct config_stub* np; 893 while(p) { 894 np = p->next; 895 free(p->name); 896 config_delstrlist(p->hosts); 897 config_delstrlist(p->addrs); 898 free(p); 899 p = np; 900 } 901 } 902 903 void 904 config_delete(struct config_file* cfg) 905 { 906 if(!cfg) return; 907 free(cfg->username); 908 free(cfg->chrootdir); 909 free(cfg->directory); 910 free(cfg->logfile); 911 free(cfg->pidfile); 912 free(cfg->target_fetch_policy); 913 free(cfg->ssl_service_key); 914 free(cfg->ssl_service_pem); 915 if(cfg->ifs) { 916 int i; 917 for(i=0; i<cfg->num_ifs; i++) 918 free(cfg->ifs[i]); 919 free(cfg->ifs); 920 } 921 if(cfg->out_ifs) { 922 int i; 923 for(i=0; i<cfg->num_out_ifs; i++) 924 free(cfg->out_ifs[i]); 925 free(cfg->out_ifs); 926 } 927 config_delstubs(cfg->stubs); 928 config_delstubs(cfg->forwards); 929 config_delstrlist(cfg->donotqueryaddrs); 930 config_delstrlist(cfg->root_hints); 931 free(cfg->identity); 932 free(cfg->version); 933 free(cfg->module_conf); 934 free(cfg->outgoing_avail_ports); 935 config_delstrlist(cfg->caps_whitelist); 936 config_delstrlist(cfg->private_address); 937 config_delstrlist(cfg->private_domain); 938 config_delstrlist(cfg->auto_trust_anchor_file_list); 939 config_delstrlist(cfg->trust_anchor_file_list); 940 config_delstrlist(cfg->trusted_keys_file_list); 941 config_delstrlist(cfg->trust_anchor_list); 942 config_delstrlist(cfg->domain_insecure); 943 free(cfg->dlv_anchor_file); 944 config_delstrlist(cfg->dlv_anchor_list); 945 config_deldblstrlist(cfg->acls); 946 free(cfg->val_nsec3_key_iterations); 947 config_deldblstrlist(cfg->local_zones); 948 config_delstrlist(cfg->local_zones_nodefault); 949 config_delstrlist(cfg->local_data); 950 config_delstrlist(cfg->control_ifs); 951 free(cfg->server_key_file); 952 free(cfg->server_cert_file); 953 free(cfg->control_key_file); 954 free(cfg->control_cert_file); 955 free(cfg->dns64_prefix); 956 free(cfg->dnstap_socket_path); 957 free(cfg->dnstap_identity); 958 free(cfg->dnstap_version); 959 config_deldblstrlist(cfg->ratelimit_for_domain); 960 config_deldblstrlist(cfg->ratelimit_below_domain); 961 free(cfg); 962 } 963 964 static void 965 init_outgoing_availports(int* a, int num) 966 { 967 /* generated with make iana_update */ 968 const int iana_assigned[] = { 969 #include "util/iana_ports.inc" 970 -1 }; /* end marker to put behind trailing comma */ 971 972 int i; 973 /* do not use <1024, that could be trouble with the system, privs */ 974 for(i=1024; i<num; i++) { 975 a[i] = i; 976 } 977 /* create empty spot at 49152 to keep ephemeral ports available 978 * to other programs */ 979 for(i=49152; i<49152+256; i++) 980 a[i] = 0; 981 /* pick out all the IANA assigned ports */ 982 for(i=0; iana_assigned[i]!=-1; i++) { 983 if(iana_assigned[i] < num) 984 a[iana_assigned[i]] = 0; 985 } 986 } 987 988 int 989 cfg_mark_ports(const char* str, int allow, int* avail, int num) 990 { 991 char* mid = strchr(str, '-'); 992 if(!mid) { 993 int port = atoi(str); 994 if(port == 0 && strcmp(str, "0") != 0) { 995 log_err("cannot parse port number '%s'", str); 996 return 0; 997 } 998 if(port < num) 999 avail[port] = (allow?port:0); 1000 } else { 1001 int i, low, high = atoi(mid+1); 1002 char buf[16]; 1003 if(high == 0 && strcmp(mid+1, "0") != 0) { 1004 log_err("cannot parse port number '%s'", mid+1); 1005 return 0; 1006 } 1007 if( (int)(mid-str)+1 >= (int)sizeof(buf) ) { 1008 log_err("cannot parse port number '%s'", str); 1009 return 0; 1010 } 1011 if(mid > str) 1012 memcpy(buf, str, (size_t)(mid-str)); 1013 buf[mid-str] = 0; 1014 low = atoi(buf); 1015 if(low == 0 && strcmp(buf, "0") != 0) { 1016 log_err("cannot parse port number '%s'", buf); 1017 return 0; 1018 } 1019 for(i=low; i<=high; i++) { 1020 if(i < num) 1021 avail[i] = (allow?i:0); 1022 } 1023 return 1; 1024 } 1025 return 1; 1026 } 1027 1028 int 1029 cfg_scan_ports(int* avail, int num) 1030 { 1031 int i; 1032 int count = 0; 1033 for(i=0; i<num; i++) { 1034 if(avail[i]) 1035 count++; 1036 } 1037 return count; 1038 } 1039 1040 int cfg_condense_ports(struct config_file* cfg, int** avail) 1041 { 1042 int num = cfg_scan_ports(cfg->outgoing_avail_ports, 65536); 1043 int i, at = 0; 1044 *avail = NULL; 1045 if(num == 0) 1046 return 0; 1047 *avail = (int*)reallocarray(NULL, (size_t)num, sizeof(int)); 1048 if(!*avail) 1049 return 0; 1050 for(i=0; i<65536; i++) { 1051 if(cfg->outgoing_avail_ports[i]) 1052 (*avail)[at++] = cfg->outgoing_avail_ports[i]; 1053 } 1054 log_assert(at == num); 1055 return num; 1056 } 1057 1058 /** print error with file and line number */ 1059 static void ub_c_error_va_list(const char *fmt, va_list args) 1060 { 1061 cfg_parser->errors++; 1062 fprintf(stderr, "%s:%d: error: ", cfg_parser->filename, 1063 cfg_parser->line); 1064 vfprintf(stderr, fmt, args); 1065 fprintf(stderr, "\n"); 1066 } 1067 1068 /** print error with file and line number */ 1069 void ub_c_error_msg(const char* fmt, ...) 1070 { 1071 va_list args; 1072 va_start(args, fmt); 1073 ub_c_error_va_list(fmt, args); 1074 va_end(args); 1075 } 1076 1077 void ub_c_error(const char *str) 1078 { 1079 cfg_parser->errors++; 1080 fprintf(stderr, "%s:%d: error: %s\n", cfg_parser->filename, 1081 cfg_parser->line, str); 1082 } 1083 1084 int ub_c_wrap(void) 1085 { 1086 return 1; 1087 } 1088 1089 int cfg_strlist_append(struct config_strlist_head* list, char* item) 1090 { 1091 struct config_strlist *s; 1092 if(!item || !list) 1093 return 0; 1094 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 1095 if(!s) 1096 return 0; 1097 s->str = item; 1098 s->next = NULL; 1099 if(list->last) 1100 list->last->next = s; 1101 else 1102 list->first = s; 1103 list->last = s; 1104 return 1; 1105 } 1106 1107 int 1108 cfg_strlist_insert(struct config_strlist** head, char* item) 1109 { 1110 struct config_strlist *s; 1111 if(!item || !head) 1112 return 0; 1113 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 1114 if(!s) 1115 return 0; 1116 s->str = item; 1117 s->next = *head; 1118 *head = s; 1119 return 1; 1120 } 1121 1122 int 1123 cfg_str2list_insert(struct config_str2list** head, char* item, char* i2) 1124 { 1125 struct config_str2list *s; 1126 if(!item || !i2 || !head) 1127 return 0; 1128 s = (struct config_str2list*)calloc(1, sizeof(struct config_str2list)); 1129 if(!s) 1130 return 0; 1131 s->str = item; 1132 s->str2 = i2; 1133 s->next = *head; 1134 *head = s; 1135 return 1; 1136 } 1137 1138 time_t 1139 cfg_convert_timeval(const char* str) 1140 { 1141 time_t t; 1142 struct tm tm; 1143 memset(&tm, 0, sizeof(tm)); 1144 if(strlen(str) < 14) 1145 return 0; 1146 if(sscanf(str, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, 1147 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) 1148 return 0; 1149 tm.tm_year -= 1900; 1150 tm.tm_mon--; 1151 /* Check values */ 1152 if (tm.tm_year < 70) return 0; 1153 if (tm.tm_mon < 0 || tm.tm_mon > 11) return 0; 1154 if (tm.tm_mday < 1 || tm.tm_mday > 31) return 0; 1155 if (tm.tm_hour < 0 || tm.tm_hour > 23) return 0; 1156 if (tm.tm_min < 0 || tm.tm_min > 59) return 0; 1157 if (tm.tm_sec < 0 || tm.tm_sec > 59) return 0; 1158 /* call ldns conversion function */ 1159 t = sldns_mktime_from_utc(&tm); 1160 return t; 1161 } 1162 1163 int 1164 cfg_count_numbers(const char* s) 1165 { 1166 /* format ::= (sp num)+ sp */ 1167 /* num ::= [-](0-9)+ */ 1168 /* sp ::= (space|tab)* */ 1169 int num = 0; 1170 while(*s) { 1171 while(*s && isspace((unsigned char)*s)) 1172 s++; 1173 if(!*s) /* end of string */ 1174 break; 1175 if(*s == '-') 1176 s++; 1177 if(!*s) /* only - not allowed */ 1178 return 0; 1179 if(!isdigit((unsigned char)*s)) /* bad character */ 1180 return 0; 1181 while(*s && isdigit((unsigned char)*s)) 1182 s++; 1183 num++; 1184 } 1185 return num; 1186 } 1187 1188 /** all digit number */ 1189 static int isalldigit(const char* str, size_t l) 1190 { 1191 size_t i; 1192 for(i=0; i<l; i++) 1193 if(!isdigit((unsigned char)str[i])) 1194 return 0; 1195 return 1; 1196 } 1197 1198 int 1199 cfg_parse_memsize(const char* str, size_t* res) 1200 { 1201 size_t len; 1202 size_t mult = 1; 1203 if(!str || (len=(size_t)strlen(str)) == 0) { 1204 log_err("not a size: '%s'", str); 1205 return 0; 1206 } 1207 if(isalldigit(str, len)) { 1208 *res = (size_t)atol(str); 1209 return 1; 1210 } 1211 /* check appended num */ 1212 while(len>0 && str[len-1]==' ') 1213 len--; 1214 if(len > 1 && str[len-1] == 'b') 1215 len--; 1216 else if(len > 1 && str[len-1] == 'B') 1217 len--; 1218 1219 if(len > 1 && tolower((unsigned char)str[len-1]) == 'g') 1220 mult = 1024*1024*1024; 1221 else if(len > 1 && tolower((unsigned char)str[len-1]) == 'm') 1222 mult = 1024*1024; 1223 else if(len > 1 && tolower((unsigned char)str[len-1]) == 'k') 1224 mult = 1024; 1225 else if(len > 0 && isdigit((unsigned char)str[len-1])) 1226 mult = 1; 1227 else { 1228 log_err("unknown size specifier: '%s'", str); 1229 return 0; 1230 } 1231 while(len>1 && str[len-2]==' ') 1232 len--; 1233 1234 if(!isalldigit(str, len-1)) { 1235 log_err("unknown size specifier: '%s'", str); 1236 return 0; 1237 } 1238 *res = ((size_t)atol(str)) * mult; 1239 return 1; 1240 } 1241 1242 void 1243 config_apply(struct config_file* config) 1244 { 1245 MAX_TTL = (time_t)config->max_ttl; 1246 MIN_TTL = (time_t)config->min_ttl; 1247 MAX_NEG_TTL = (time_t)config->max_negative_ttl; 1248 RTT_MIN_TIMEOUT = config->infra_cache_min_rtt; 1249 EDNS_ADVERTISED_SIZE = (uint16_t)config->edns_buffer_size; 1250 MINIMAL_RESPONSES = config->minimal_responses; 1251 RRSET_ROUNDROBIN = config->rrset_roundrobin; 1252 log_set_time_asc(config->log_time_ascii); 1253 autr_permit_small_holddown = config->permit_small_holddown; 1254 } 1255 1256 void config_lookup_uid(struct config_file* cfg) 1257 { 1258 #ifdef HAVE_GETPWNAM 1259 /* translate username into uid and gid */ 1260 if(cfg->username && cfg->username[0]) { 1261 struct passwd *pwd; 1262 if((pwd = getpwnam(cfg->username)) != NULL) { 1263 cfg_uid = pwd->pw_uid; 1264 cfg_gid = pwd->pw_gid; 1265 } 1266 } 1267 #else 1268 (void)cfg; 1269 #endif 1270 } 1271 1272 /** 1273 * Calculate string length of full pathname in original filesys 1274 * @param fname: the path name to convert. 1275 * Must not be null or empty. 1276 * @param cfg: config struct for chroot and chdir (if set). 1277 * @param use_chdir: if false, only chroot is applied. 1278 * @return length of string. 1279 * remember to allocate one more for 0 at end in mallocs. 1280 */ 1281 static size_t 1282 strlen_after_chroot(const char* fname, struct config_file* cfg, int use_chdir) 1283 { 1284 size_t len = 0; 1285 int slashit = 0; 1286 if(cfg->chrootdir && cfg->chrootdir[0] && 1287 strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) { 1288 /* already full pathname, return it */ 1289 return strlen(fname); 1290 } 1291 /* chroot */ 1292 if(cfg->chrootdir && cfg->chrootdir[0]) { 1293 /* start with chrootdir */ 1294 len += strlen(cfg->chrootdir); 1295 slashit = 1; 1296 } 1297 /* chdir */ 1298 #ifdef UB_ON_WINDOWS 1299 if(fname[0] != 0 && fname[1] == ':') { 1300 /* full path, no chdir */ 1301 } else 1302 #endif 1303 if(fname[0] == '/' || !use_chdir) { 1304 /* full path, no chdir */ 1305 } else if(cfg->directory && cfg->directory[0]) { 1306 /* prepend chdir */ 1307 if(slashit && cfg->directory[0] != '/') 1308 len++; 1309 if(cfg->chrootdir && cfg->chrootdir[0] && 1310 strncmp(cfg->chrootdir, cfg->directory, 1311 strlen(cfg->chrootdir)) == 0) 1312 len += strlen(cfg->directory)-strlen(cfg->chrootdir); 1313 else len += strlen(cfg->directory); 1314 slashit = 1; 1315 } 1316 /* fname */ 1317 if(slashit && fname[0] != '/') 1318 len++; 1319 len += strlen(fname); 1320 return len; 1321 } 1322 1323 char* 1324 fname_after_chroot(const char* fname, struct config_file* cfg, int use_chdir) 1325 { 1326 size_t len = strlen_after_chroot(fname, cfg, use_chdir)+1; 1327 int slashit = 0; 1328 char* buf = (char*)malloc(len); 1329 if(!buf) 1330 return NULL; 1331 buf[0] = 0; 1332 /* is fname already in chroot ? */ 1333 if(cfg->chrootdir && cfg->chrootdir[0] && 1334 strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) { 1335 /* already full pathname, return it */ 1336 (void)strlcpy(buf, fname, len); 1337 buf[len-1] = 0; 1338 return buf; 1339 } 1340 /* chroot */ 1341 if(cfg->chrootdir && cfg->chrootdir[0]) { 1342 /* start with chrootdir */ 1343 (void)strlcpy(buf, cfg->chrootdir, len); 1344 slashit = 1; 1345 } 1346 #ifdef UB_ON_WINDOWS 1347 if(fname[0] != 0 && fname[1] == ':') { 1348 /* full path, no chdir */ 1349 } else 1350 #endif 1351 /* chdir */ 1352 if(fname[0] == '/' || !use_chdir) { 1353 /* full path, no chdir */ 1354 } else if(cfg->directory && cfg->directory[0]) { 1355 /* prepend chdir */ 1356 if(slashit && cfg->directory[0] != '/') 1357 (void)strlcat(buf, "/", len); 1358 /* is the directory already in the chroot? */ 1359 if(cfg->chrootdir && cfg->chrootdir[0] && 1360 strncmp(cfg->chrootdir, cfg->directory, 1361 strlen(cfg->chrootdir)) == 0) 1362 (void)strlcat(buf, cfg->directory+strlen(cfg->chrootdir), 1363 len); 1364 else (void)strlcat(buf, cfg->directory, len); 1365 slashit = 1; 1366 } 1367 /* fname */ 1368 if(slashit && fname[0] != '/') 1369 (void)strlcat(buf, "/", len); 1370 (void)strlcat(buf, fname, len); 1371 buf[len-1] = 0; 1372 return buf; 1373 } 1374 1375 /** return next space character in string */ 1376 static char* next_space_pos(const char* str) 1377 { 1378 char* sp = strchr(str, ' '); 1379 char* tab = strchr(str, '\t'); 1380 if(!tab && !sp) 1381 return NULL; 1382 if(!sp) return tab; 1383 if(!tab) return sp; 1384 return (sp<tab)?sp:tab; 1385 } 1386 1387 /** return last space character in string */ 1388 static char* last_space_pos(const char* str) 1389 { 1390 char* sp = strrchr(str, ' '); 1391 char* tab = strrchr(str, '\t'); 1392 if(!tab && !sp) 1393 return NULL; 1394 if(!sp) return tab; 1395 if(!tab) return sp; 1396 return (sp>tab)?sp:tab; 1397 } 1398 1399 int 1400 cfg_parse_local_zone(struct config_file* cfg, const char* val) 1401 { 1402 const char *type, *name_end, *name; 1403 char buf[256]; 1404 1405 /* parse it as: [zone_name] [between stuff] [zone_type] */ 1406 name = val; 1407 while(*name && isspace((unsigned char)*name)) 1408 name++; 1409 if(!*name) { 1410 log_err("syntax error: too short: %s", val); 1411 return 0; 1412 } 1413 name_end = next_space_pos(name); 1414 if(!name_end || !*name_end) { 1415 log_err("syntax error: expected zone type: %s", val); 1416 return 0; 1417 } 1418 if (name_end - name > 255) { 1419 log_err("syntax error: bad zone name: %s", val); 1420 return 0; 1421 } 1422 (void)strlcpy(buf, name, sizeof(buf)); 1423 buf[name_end-name] = '\0'; 1424 1425 type = last_space_pos(name_end); 1426 while(type && *type && isspace((unsigned char)*type)) 1427 type++; 1428 if(!type || !*type) { 1429 log_err("syntax error: expected zone type: %s", val); 1430 return 0; 1431 } 1432 1433 if(strcmp(type, "nodefault")==0) { 1434 return cfg_strlist_insert(&cfg->local_zones_nodefault, 1435 strdup(name)); 1436 } else { 1437 return cfg_str2list_insert(&cfg->local_zones, strdup(buf), 1438 strdup(type)); 1439 } 1440 } 1441 1442 char* cfg_ptr_reverse(char* str) 1443 { 1444 char* ip, *ip_end; 1445 char* name; 1446 char* result; 1447 char buf[1024]; 1448 struct sockaddr_storage addr; 1449 socklen_t addrlen; 1450 1451 /* parse it as: [IP] [between stuff] [name] */ 1452 ip = str; 1453 while(*ip && isspace((unsigned char)*ip)) 1454 ip++; 1455 if(!*ip) { 1456 log_err("syntax error: too short: %s", str); 1457 return NULL; 1458 } 1459 ip_end = next_space_pos(ip); 1460 if(!ip_end || !*ip_end) { 1461 log_err("syntax error: expected name: %s", str); 1462 return NULL; 1463 } 1464 1465 name = last_space_pos(ip_end); 1466 if(!name || !*name) { 1467 log_err("syntax error: expected name: %s", str); 1468 return NULL; 1469 } 1470 1471 sscanf(ip, "%100s", buf); 1472 buf[sizeof(buf)-1]=0; 1473 1474 if(!ipstrtoaddr(buf, UNBOUND_DNS_PORT, &addr, &addrlen)) { 1475 log_err("syntax error: cannot parse address: %s", str); 1476 return NULL; 1477 } 1478 1479 /* reverse IPv4: 1480 * ddd.ddd.ddd.ddd.in-addr-arpa. 1481 * IPv6: (h.){32}.ip6.arpa. */ 1482 1483 if(addr_is_ip6(&addr, addrlen)) { 1484 uint8_t ad[16]; 1485 const char* hex = "0123456789abcdef"; 1486 char *p = buf; 1487 int i; 1488 memmove(ad, &((struct sockaddr_in6*)&addr)->sin6_addr, 1489 sizeof(ad)); 1490 for(i=15; i>=0; i--) { 1491 uint8_t b = ad[i]; 1492 *p++ = hex[ (b&0x0f) ]; 1493 *p++ = '.'; 1494 *p++ = hex[ (b&0xf0) >> 4 ]; 1495 *p++ = '.'; 1496 } 1497 snprintf(buf+16*4, sizeof(buf)-16*4, "ip6.arpa. "); 1498 } else { 1499 uint8_t ad[4]; 1500 memmove(ad, &((struct sockaddr_in*)&addr)->sin_addr, 1501 sizeof(ad)); 1502 snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa. ", 1503 (unsigned)ad[3], (unsigned)ad[2], 1504 (unsigned)ad[1], (unsigned)ad[0]); 1505 } 1506 1507 /* printed the reverse address, now the between goop and name on end */ 1508 while(*ip_end && isspace((unsigned char)*ip_end)) 1509 ip_end++; 1510 if(name>ip_end) { 1511 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%.*s", 1512 (int)(name-ip_end), ip_end); 1513 } 1514 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), " PTR %s", name); 1515 1516 result = strdup(buf); 1517 if(!result) { 1518 log_err("out of memory parsing %s", str); 1519 return NULL; 1520 } 1521 return result; 1522 } 1523 1524 #ifdef UB_ON_WINDOWS 1525 char* 1526 w_lookup_reg_str(const char* key, const char* name) 1527 { 1528 HKEY hk = NULL; 1529 DWORD type = 0; 1530 BYTE buf[1024]; 1531 DWORD len = (DWORD)sizeof(buf); 1532 LONG ret; 1533 char* result = NULL; 1534 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hk); 1535 if(ret == ERROR_FILE_NOT_FOUND) 1536 return NULL; /* key does not exist */ 1537 else if(ret != ERROR_SUCCESS) { 1538 log_err("RegOpenKeyEx failed"); 1539 return NULL; 1540 } 1541 ret = RegQueryValueEx(hk, (LPCTSTR)name, 0, &type, buf, &len); 1542 if(RegCloseKey(hk)) 1543 log_err("RegCloseKey"); 1544 if(ret == ERROR_FILE_NOT_FOUND) 1545 return NULL; /* name does not exist */ 1546 else if(ret != ERROR_SUCCESS) { 1547 log_err("RegQueryValueEx failed"); 1548 return NULL; 1549 } 1550 if(type == REG_SZ || type == REG_MULTI_SZ || type == REG_EXPAND_SZ) { 1551 buf[sizeof(buf)-1] = 0; 1552 buf[sizeof(buf)-2] = 0; /* for multi_sz */ 1553 result = strdup((char*)buf); 1554 if(!result) log_err("out of memory"); 1555 } 1556 return result; 1557 } 1558 #endif /* UB_ON_WINDOWS */ 1559 1560 void errinf(struct module_qstate* qstate, const char* str) 1561 { 1562 struct config_strlist* p; 1563 if(qstate->env->cfg->val_log_level < 2 || !str) 1564 return; 1565 p = (struct config_strlist*)regional_alloc(qstate->region, sizeof(*p)); 1566 if(!p) { 1567 log_err("malloc failure in validator-error-info string"); 1568 return; 1569 } 1570 p->next = NULL; 1571 p->str = regional_strdup(qstate->region, str); 1572 if(!p->str) { 1573 log_err("malloc failure in validator-error-info string"); 1574 return; 1575 } 1576 /* add at end */ 1577 if(qstate->errinf) { 1578 struct config_strlist* q = qstate->errinf; 1579 while(q->next) 1580 q = q->next; 1581 q->next = p; 1582 } else qstate->errinf = p; 1583 } 1584 1585 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin) 1586 { 1587 struct sock_list* p; 1588 if(qstate->env->cfg->val_log_level < 2) 1589 return; 1590 for(p=origin; p; p=p->next) { 1591 char buf[256]; 1592 if(p == origin) 1593 snprintf(buf, sizeof(buf), "from "); 1594 else snprintf(buf, sizeof(buf), "and "); 1595 if(p->len == 0) 1596 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), 1597 "cache"); 1598 else 1599 addr_to_str(&p->addr, p->len, buf+strlen(buf), 1600 sizeof(buf)-strlen(buf)); 1601 errinf(qstate, buf); 1602 } 1603 } 1604 1605 char* errinf_to_str(struct module_qstate* qstate) 1606 { 1607 char buf[20480]; 1608 char* p = buf; 1609 size_t left = sizeof(buf); 1610 struct config_strlist* s; 1611 char dname[LDNS_MAX_DOMAINLEN+1]; 1612 char t[16], c[16]; 1613 sldns_wire2str_type_buf(qstate->qinfo.qtype, t, sizeof(t)); 1614 sldns_wire2str_class_buf(qstate->qinfo.qclass, c, sizeof(c)); 1615 dname_str(qstate->qinfo.qname, dname); 1616 snprintf(p, left, "validation failure <%s %s %s>:", dname, t, c); 1617 left -= strlen(p); p += strlen(p); 1618 if(!qstate->errinf) 1619 snprintf(p, left, " misc failure"); 1620 else for(s=qstate->errinf; s; s=s->next) { 1621 snprintf(p, left, " %s", s->str); 1622 left -= strlen(p); p += strlen(p); 1623 } 1624 p = strdup(buf); 1625 if(!p) 1626 log_err("malloc failure in errinf_to_str"); 1627 return p; 1628 } 1629 1630 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr) 1631 { 1632 char buf[1024]; 1633 char dname[LDNS_MAX_DOMAINLEN+1]; 1634 char t[16], c[16]; 1635 if(qstate->env->cfg->val_log_level < 2 || !rr) 1636 return; 1637 sldns_wire2str_type_buf(ntohs(rr->rk.type), t, sizeof(t)); 1638 sldns_wire2str_class_buf(ntohs(rr->rk.rrset_class), c, sizeof(c)); 1639 dname_str(rr->rk.dname, dname); 1640 snprintf(buf, sizeof(buf), "for <%s %s %s>", dname, t, c); 1641 errinf(qstate, buf); 1642 } 1643 1644 void errinf_dname(struct module_qstate* qstate, const char* str, uint8_t* dname) 1645 { 1646 char b[1024]; 1647 char buf[LDNS_MAX_DOMAINLEN+1]; 1648 if(qstate->env->cfg->val_log_level < 2 || !str || !dname) 1649 return; 1650 dname_str(dname, buf); 1651 snprintf(b, sizeof(b), "%s %s", str, buf); 1652 errinf(qstate, b); 1653 } 1654