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