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