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