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