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