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