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 #include <errno.h> 46 #ifdef HAVE_TIME_H 47 #include <time.h> 48 #endif 49 #include "util/log.h" 50 #include "util/configyyrename.h" 51 #include "util/config_file.h" 52 #include "configparser.h" 53 #include "util/net_help.h" 54 #include "util/data/msgparse.h" 55 #include "util/module.h" 56 #include "util/regional.h" 57 #include "util/fptr_wlist.h" 58 #include "util/data/dname.h" 59 #include "util/random.h" 60 #include "util/rtt.h" 61 #include "services/cache/infra.h" 62 #include "sldns/wire2str.h" 63 #include "sldns/parseutil.h" 64 #include "iterator/iterator.h" 65 #ifdef HAVE_GLOB_H 66 # include <glob.h> 67 #endif 68 #ifdef CLIENT_SUBNET 69 #include "edns-subnet/edns-subnet.h" 70 #endif 71 #ifdef HAVE_PWD_H 72 #include <pwd.h> 73 #endif 74 75 /** from cfg username, after daemonize setup performed */ 76 uid_t cfg_uid = (uid_t)-1; 77 /** from cfg username, after daemonize setup performed */ 78 gid_t cfg_gid = (gid_t)-1; 79 /** for debug allow small timeout values for fast rollovers */ 80 int autr_permit_small_holddown = 0; 81 /** size (in bytes) of stream wait buffers max */ 82 size_t stream_wait_max = 4 * 1024 * 1024; 83 size_t http2_query_buffer_max = 4 * 1024 * 1024; 84 size_t http2_response_buffer_max = 4 * 1024 * 1024; 85 86 /** global config during parsing */ 87 struct config_parser_state* cfg_parser = 0; 88 89 /** init ports possible for use */ 90 static void init_outgoing_availports(int* array, int num); 91 92 /** init cookie with random data */ 93 static void init_cookie_secret(uint8_t* cookie_secret, size_t cookie_secret_len); 94 95 struct config_file* 96 config_create(void) 97 { 98 struct config_file* cfg; 99 cfg = (struct config_file*)calloc(1, sizeof(struct config_file)); 100 if(!cfg) 101 return NULL; 102 /* the defaults if no config is present */ 103 cfg->verbosity = 1; 104 cfg->stat_interval = 0; 105 cfg->stat_cumulative = 0; 106 cfg->stat_extended = 0; 107 cfg->stat_inhibit_zero = 1; 108 cfg->num_threads = 1; 109 cfg->port = UNBOUND_DNS_PORT; 110 cfg->do_ip4 = 1; 111 cfg->do_ip6 = 1; 112 cfg->do_udp = 1; 113 cfg->do_tcp = 1; 114 cfg->tcp_reuse_timeout = 60 * 1000; /* 60s in milisecs */ 115 cfg->max_reuse_tcp_queries = 200; 116 cfg->tcp_upstream = 0; 117 cfg->udp_upstream_without_downstream = 0; 118 cfg->tcp_mss = 0; 119 cfg->outgoing_tcp_mss = 0; 120 cfg->tcp_idle_timeout = 30 * 1000; /* 30s in millisecs */ 121 cfg->tcp_auth_query_timeout = 3 * 1000; /* 3s in millisecs */ 122 cfg->do_tcp_keepalive = 0; 123 cfg->tcp_keepalive_timeout = 120 * 1000; /* 120s in millisecs */ 124 cfg->sock_queue_timeout = 0; /* do not check timeout */ 125 cfg->ssl_service_key = NULL; 126 cfg->ssl_service_pem = NULL; 127 cfg->ssl_port = UNBOUND_DNS_OVER_TLS_PORT; 128 cfg->ssl_upstream = 0; 129 cfg->tls_cert_bundle = NULL; 130 cfg->tls_win_cert = 0; 131 cfg->tls_use_sni = 1; 132 cfg->https_port = UNBOUND_DNS_OVER_HTTPS_PORT; 133 if(!(cfg->http_endpoint = strdup("/dns-query"))) goto error_exit; 134 cfg->http_max_streams = 100; 135 cfg->http_query_buffer_size = 4*1024*1024; 136 cfg->http_response_buffer_size = 4*1024*1024; 137 cfg->http_nodelay = 1; 138 cfg->use_syslog = 1; 139 cfg->log_identity = NULL; /* changed later with argv[0] */ 140 cfg->log_time_ascii = 0; 141 cfg->log_queries = 0; 142 cfg->log_replies = 0; 143 cfg->log_tag_queryreply = 0; 144 cfg->log_local_actions = 0; 145 cfg->log_servfail = 0; 146 cfg->log_destaddr = 0; 147 #ifndef USE_WINSOCK 148 # ifdef USE_MINI_EVENT 149 /* select max 1024 sockets */ 150 cfg->outgoing_num_ports = 960; 151 cfg->num_queries_per_thread = 512; 152 # else 153 /* libevent can use many sockets */ 154 cfg->outgoing_num_ports = 4096; 155 cfg->num_queries_per_thread = 1024; 156 # endif 157 cfg->outgoing_num_tcp = 10; 158 cfg->incoming_num_tcp = 10; 159 #else 160 cfg->outgoing_num_ports = 48; /* windows is limited in num fds */ 161 cfg->num_queries_per_thread = 24; 162 cfg->outgoing_num_tcp = 2; /* leaves 64-52=12 for: 4if,1stop,thread4 */ 163 cfg->incoming_num_tcp = 2; 164 #endif 165 cfg->stream_wait_size = 4 * 1024 * 1024; 166 cfg->edns_buffer_size = 1232; /* from DNS flagday recommendation */ 167 cfg->msg_buffer_size = 65552; /* 64 k + a small margin */ 168 cfg->msg_cache_size = 4 * 1024 * 1024; 169 cfg->msg_cache_slabs = 4; 170 cfg->jostle_time = 200; 171 cfg->rrset_cache_size = 4 * 1024 * 1024; 172 cfg->rrset_cache_slabs = 4; 173 cfg->host_ttl = 900; 174 cfg->bogus_ttl = 60; 175 cfg->min_ttl = 0; 176 cfg->max_ttl = 3600 * 24; 177 cfg->max_negative_ttl = 3600; 178 cfg->min_negative_ttl = 0; 179 cfg->prefetch = 0; 180 cfg->prefetch_key = 0; 181 cfg->deny_any = 0; 182 cfg->infra_cache_slabs = 4; 183 cfg->infra_cache_numhosts = 10000; 184 cfg->infra_cache_min_rtt = 50; 185 cfg->infra_cache_max_rtt = 120000; 186 cfg->infra_keep_probing = 0; 187 cfg->delay_close = 0; 188 cfg->udp_connect = 1; 189 if(!(cfg->outgoing_avail_ports = (int*)calloc(65536, sizeof(int)))) 190 goto error_exit; 191 init_outgoing_availports(cfg->outgoing_avail_ports, 65536); 192 if(!(cfg->username = strdup(UB_USERNAME))) goto error_exit; 193 #ifdef HAVE_CHROOT 194 if(!(cfg->chrootdir = strdup(CHROOT_DIR))) goto error_exit; 195 #endif 196 if(!(cfg->directory = strdup(RUN_DIR))) goto error_exit; 197 if(!(cfg->logfile = strdup(""))) goto error_exit; 198 if(!(cfg->pidfile = strdup(PIDFILE))) goto error_exit; 199 if(!(cfg->target_fetch_policy = strdup("3 2 1 0 0"))) goto error_exit; 200 cfg->fast_server_permil = 0; 201 cfg->fast_server_num = 3; 202 cfg->donotqueryaddrs = NULL; 203 cfg->donotquery_localhost = 1; 204 cfg->root_hints = NULL; 205 cfg->use_systemd = 0; 206 cfg->do_daemonize = 1; 207 cfg->if_automatic = 0; 208 cfg->if_automatic_ports = NULL; 209 cfg->so_rcvbuf = 0; 210 cfg->so_sndbuf = 0; 211 cfg->so_reuseport = REUSEPORT_DEFAULT; 212 cfg->ip_transparent = 0; 213 cfg->ip_freebind = 0; 214 cfg->ip_dscp = 0; 215 cfg->num_ifs = 0; 216 cfg->ifs = NULL; 217 cfg->num_out_ifs = 0; 218 cfg->out_ifs = NULL; 219 cfg->stubs = NULL; 220 cfg->forwards = NULL; 221 cfg->auths = NULL; 222 #ifdef CLIENT_SUBNET 223 cfg->client_subnet = NULL; 224 cfg->client_subnet_zone = NULL; 225 cfg->client_subnet_opcode = LDNS_EDNS_CLIENT_SUBNET; 226 cfg->client_subnet_always_forward = 0; 227 cfg->max_client_subnet_ipv4 = 24; 228 cfg->max_client_subnet_ipv6 = 56; 229 cfg->min_client_subnet_ipv4 = 0; 230 cfg->min_client_subnet_ipv6 = 0; 231 cfg->max_ecs_tree_size_ipv4 = 100; 232 cfg->max_ecs_tree_size_ipv6 = 100; 233 #endif 234 cfg->views = NULL; 235 cfg->acls = NULL; 236 cfg->tcp_connection_limits = NULL; 237 cfg->harden_short_bufsize = 1; 238 cfg->harden_large_queries = 0; 239 cfg->harden_glue = 1; 240 cfg->harden_dnssec_stripped = 1; 241 cfg->harden_below_nxdomain = 1; 242 cfg->harden_referral_path = 0; 243 cfg->harden_algo_downgrade = 0; 244 cfg->harden_unknown_additional = 0; 245 cfg->use_caps_bits_for_id = 0; 246 cfg->caps_whitelist = NULL; 247 cfg->private_address = NULL; 248 cfg->private_domain = NULL; 249 cfg->unwanted_threshold = 0; 250 cfg->hide_identity = 0; 251 cfg->hide_version = 0; 252 cfg->hide_trustanchor = 0; 253 cfg->hide_http_user_agent = 0; 254 cfg->identity = NULL; 255 cfg->version = NULL; 256 cfg->http_user_agent = NULL; 257 cfg->nsid_cfg_str = NULL; 258 cfg->nsid = NULL; 259 cfg->nsid_len = 0; 260 cfg->auto_trust_anchor_file_list = NULL; 261 cfg->trust_anchor_file_list = NULL; 262 cfg->trust_anchor_list = NULL; 263 cfg->trusted_keys_file_list = NULL; 264 cfg->trust_anchor_signaling = 1; 265 cfg->root_key_sentinel = 1; 266 cfg->domain_insecure = NULL; 267 cfg->val_date_override = 0; 268 cfg->val_sig_skew_min = 3600; /* at least daylight savings trouble */ 269 cfg->val_sig_skew_max = 86400; /* at most timezone settings trouble */ 270 cfg->val_max_restart = 5; 271 cfg->val_clean_additional = 1; 272 cfg->val_log_level = 0; 273 cfg->val_log_squelch = 0; 274 cfg->val_permissive_mode = 0; 275 cfg->aggressive_nsec = 1; 276 cfg->ignore_cd = 0; 277 cfg->disable_edns_do = 0; 278 cfg->serve_expired = 0; 279 cfg->serve_expired_ttl = 0; 280 cfg->serve_expired_ttl_reset = 0; 281 cfg->serve_expired_reply_ttl = 30; 282 cfg->serve_expired_client_timeout = 0; 283 cfg->ede_serve_expired = 0; 284 cfg->serve_original_ttl = 0; 285 cfg->zonemd_permissive_mode = 0; 286 cfg->add_holddown = 30*24*3600; 287 cfg->del_holddown = 30*24*3600; 288 cfg->keep_missing = 366*24*3600; /* one year plus a little leeway */ 289 cfg->permit_small_holddown = 0; 290 cfg->key_cache_size = 4 * 1024 * 1024; 291 cfg->key_cache_slabs = 4; 292 cfg->neg_cache_size = 1 * 1024 * 1024; 293 cfg->local_zones = NULL; 294 cfg->local_zones_nodefault = NULL; 295 #ifdef USE_IPSET 296 cfg->local_zones_ipset = NULL; 297 #endif 298 cfg->local_zones_disable_default = 0; 299 cfg->local_data = NULL; 300 cfg->local_zone_overrides = NULL; 301 cfg->unblock_lan_zones = 0; 302 cfg->insecure_lan_zones = 0; 303 cfg->python_script = NULL; 304 cfg->dynlib_file = NULL; 305 cfg->remote_control_enable = 0; 306 cfg->control_ifs.first = NULL; 307 cfg->control_ifs.last = NULL; 308 cfg->control_port = UNBOUND_CONTROL_PORT; 309 cfg->control_use_cert = 1; 310 cfg->minimal_responses = 1; 311 cfg->rrset_roundrobin = 1; 312 cfg->unknown_server_time_limit = 376; 313 cfg->discard_timeout = 1900; /* msec */ 314 cfg->wait_limit = 1000; 315 cfg->wait_limit_cookie = 10000; 316 cfg->wait_limit_netblock = NULL; 317 cfg->wait_limit_cookie_netblock = NULL; 318 cfg->max_udp_size = 1232; /* value taken from edns_buffer_size */ 319 if(!(cfg->server_key_file = strdup(RUN_DIR"/unbound_server.key"))) 320 goto error_exit; 321 if(!(cfg->server_cert_file = strdup(RUN_DIR"/unbound_server.pem"))) 322 goto error_exit; 323 if(!(cfg->control_key_file = strdup(RUN_DIR"/unbound_control.key"))) 324 goto error_exit; 325 if(!(cfg->control_cert_file = strdup(RUN_DIR"/unbound_control.pem"))) 326 goto error_exit; 327 328 #ifdef CLIENT_SUBNET 329 if(!(cfg->module_conf = strdup("subnetcache validator iterator"))) goto error_exit; 330 #else 331 if(!(cfg->module_conf = strdup("validator iterator"))) goto error_exit; 332 #endif 333 if(!(cfg->val_nsec3_key_iterations = 334 strdup("1024 150 2048 150 4096 150"))) goto error_exit; 335 #if defined(DNSTAP_SOCKET_PATH) 336 if(!(cfg->dnstap_socket_path = strdup(DNSTAP_SOCKET_PATH))) 337 goto error_exit; 338 #endif 339 cfg->dnstap_bidirectional = 1; 340 cfg->dnstap_tls = 1; 341 cfg->disable_dnssec_lame_check = 0; 342 cfg->ip_ratelimit_cookie = 0; 343 cfg->ip_ratelimit = 0; 344 cfg->ratelimit = 0; 345 cfg->ip_ratelimit_slabs = 4; 346 cfg->ratelimit_slabs = 4; 347 cfg->ip_ratelimit_size = 4*1024*1024; 348 cfg->ratelimit_size = 4*1024*1024; 349 cfg->ratelimit_for_domain = NULL; 350 cfg->ratelimit_below_domain = NULL; 351 cfg->ip_ratelimit_factor = 10; 352 cfg->ratelimit_factor = 10; 353 cfg->ip_ratelimit_backoff = 0; 354 cfg->ratelimit_backoff = 0; 355 cfg->outbound_msg_retry = 5; 356 cfg->max_sent_count = 32; 357 cfg->max_query_restarts = 11; 358 cfg->qname_minimisation = 1; 359 cfg->qname_minimisation_strict = 0; 360 cfg->shm_enable = 0; 361 cfg->shm_key = 11777; 362 cfg->edns_client_strings = NULL; 363 cfg->edns_client_string_opcode = 65001; 364 cfg->dnscrypt = 0; 365 cfg->dnscrypt_port = 0; 366 cfg->dnscrypt_provider = NULL; 367 cfg->dnscrypt_provider_cert = NULL; 368 cfg->dnscrypt_provider_cert_rotated = NULL; 369 cfg->dnscrypt_secret_key = NULL; 370 cfg->dnscrypt_shared_secret_cache_size = 4*1024*1024; 371 cfg->dnscrypt_shared_secret_cache_slabs = 4; 372 cfg->dnscrypt_nonce_cache_size = 4*1024*1024; 373 cfg->dnscrypt_nonce_cache_slabs = 4; 374 cfg->pad_responses = 1; 375 cfg->pad_responses_block_size = 468; /* from RFC8467 */ 376 cfg->pad_queries = 1; 377 cfg->pad_queries_block_size = 128; /* from RFC8467 */ 378 #ifdef USE_IPSECMOD 379 cfg->ipsecmod_enabled = 1; 380 cfg->ipsecmod_ignore_bogus = 0; 381 cfg->ipsecmod_hook = NULL; 382 cfg->ipsecmod_max_ttl = 3600; 383 cfg->ipsecmod_whitelist = NULL; 384 cfg->ipsecmod_strict = 0; 385 #endif 386 cfg->do_answer_cookie = 0; 387 memset(cfg->cookie_secret, 0, sizeof(cfg->cookie_secret)); 388 cfg->cookie_secret_len = 16; 389 init_cookie_secret(cfg->cookie_secret, cfg->cookie_secret_len); 390 cfg->cookie_secret_file = NULL; 391 #ifdef USE_CACHEDB 392 if(!(cfg->cachedb_backend = strdup("testframe"))) goto error_exit; 393 if(!(cfg->cachedb_secret = strdup("default"))) goto error_exit; 394 cfg->cachedb_no_store = 0; 395 cfg->cachedb_check_when_serve_expired = 1; 396 #ifdef USE_REDIS 397 if(!(cfg->redis_server_host = strdup("127.0.0.1"))) goto error_exit; 398 cfg->redis_server_path = NULL; 399 cfg->redis_server_password = NULL; 400 cfg->redis_timeout = 100; 401 cfg->redis_server_port = 6379; 402 cfg->redis_expire_records = 0; 403 cfg->redis_logical_db = 0; 404 #endif /* USE_REDIS */ 405 #endif /* USE_CACHEDB */ 406 #ifdef USE_IPSET 407 cfg->ipset_name_v4 = NULL; 408 cfg->ipset_name_v6 = NULL; 409 #endif 410 cfg->ede = 0; 411 return cfg; 412 error_exit: 413 config_delete(cfg); 414 return NULL; 415 } 416 417 struct config_file* config_create_forlib(void) 418 { 419 struct config_file* cfg = config_create(); 420 if(!cfg) return NULL; 421 /* modifications for library use, less verbose, less memory */ 422 free(cfg->chrootdir); 423 cfg->chrootdir = NULL; 424 cfg->verbosity = 0; 425 cfg->outgoing_num_ports = 16; /* in library use, this is 'reasonable' 426 and probably within the ulimit(maxfds) of the user */ 427 cfg->outgoing_num_tcp = 2; 428 cfg->msg_cache_size = 1024*1024; 429 cfg->msg_cache_slabs = 1; 430 cfg->rrset_cache_size = 1024*1024; 431 cfg->rrset_cache_slabs = 1; 432 cfg->infra_cache_slabs = 1; 433 cfg->use_syslog = 0; 434 cfg->key_cache_size = 1024*1024; 435 cfg->key_cache_slabs = 1; 436 cfg->neg_cache_size = 100 * 1024; 437 cfg->donotquery_localhost = 0; /* allow, so that you can ask a 438 forward nameserver running on localhost */ 439 cfg->val_log_level = 2; /* to fill why_bogus with */ 440 cfg->val_log_squelch = 1; 441 cfg->minimal_responses = 0; 442 cfg->harden_short_bufsize = 1; 443 return cfg; 444 } 445 446 /** check that the value passed is >= 0 */ 447 #define IS_NUMBER_OR_ZERO \ 448 if(atoi(val) == 0 && strcmp(val, "0") != 0) return 0 449 /** check that the value passed is > 0 */ 450 #define IS_NONZERO_NUMBER \ 451 if(atoi(val) == 0) return 0 452 /** check that the value passed is not 0 and a power of 2 */ 453 #define IS_POW2_NUMBER \ 454 if(atoi(val) == 0 || !is_pow2((size_t)atoi(val))) return 0 455 /** check that the value passed is yes or no */ 456 #define IS_YES_OR_NO \ 457 if(strcmp(val, "yes") != 0 && strcmp(val, "no") != 0) return 0 458 /** put integer_or_zero into variable */ 459 #define S_NUMBER_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 460 { IS_NUMBER_OR_ZERO; cfg->var = atoi(val); } 461 /** put integer_nonzero into variable */ 462 #define S_NUMBER_NONZERO(str, var) if(strcmp(opt, str) == 0) \ 463 { IS_NONZERO_NUMBER; cfg->var = atoi(val); } 464 /** put integer_or_zero into unsigned */ 465 #define S_UNSIGNED_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 466 { IS_NUMBER_OR_ZERO; cfg->var = (unsigned)atoi(val); } 467 /** put integer_or_zero into size_t */ 468 #define S_SIZET_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \ 469 { IS_NUMBER_OR_ZERO; cfg->var = (size_t)atoi(val); } 470 /** put integer_nonzero into size_t */ 471 #define S_SIZET_NONZERO(str, var) if(strcmp(opt, str) == 0) \ 472 { IS_NONZERO_NUMBER; cfg->var = (size_t)atoi(val); } 473 /** put yesno into variable */ 474 #define S_YNO(str, var) if(strcmp(opt, str) == 0) \ 475 { IS_YES_OR_NO; cfg->var = (strcmp(val, "yes") == 0); } 476 /** put memsize into variable */ 477 #define S_MEMSIZE(str, var) if(strcmp(opt, str)==0) \ 478 { return cfg_parse_memsize(val, &cfg->var); } 479 /** put pow2 number into variable */ 480 #define S_POW2(str, var) if(strcmp(opt, str)==0) \ 481 { IS_POW2_NUMBER; cfg->var = (size_t)atoi(val); } 482 /** put string into variable */ 483 #define S_STR(str, var) if(strcmp(opt, str)==0) \ 484 { free(cfg->var); return (cfg->var = strdup(val)) != NULL; } 485 /** put string into strlist */ 486 #define S_STRLIST(str, var) if(strcmp(opt, str)==0) \ 487 { return cfg_strlist_insert(&cfg->var, strdup(val)); } 488 /** put string into strlist if not present yet*/ 489 #define S_STRLIST_UNIQ(str, var) if(strcmp(opt, str)==0) \ 490 { if(cfg_strlist_find(cfg->var, val)) { return 0;} \ 491 return cfg_strlist_insert(&cfg->var, strdup(val)); } 492 /** append string to strlist */ 493 #define S_STRLIST_APPEND(str, var) if(strcmp(opt, str)==0) \ 494 { return cfg_strlist_append(&cfg->var, strdup(val)); } 495 496 int config_set_option(struct config_file* cfg, const char* opt, 497 const char* val) 498 { 499 char buf[64]; 500 if(!opt) return 0; 501 if(opt[strlen(opt)-1] != ':' && strlen(opt)+2<sizeof(buf)) { 502 snprintf(buf, sizeof(buf), "%s:", opt); 503 opt = buf; 504 } 505 S_NUMBER_OR_ZERO("verbosity:", verbosity) 506 else if(strcmp(opt, "statistics-interval:") == 0) { 507 if(strcmp(val, "0") == 0 || strcmp(val, "") == 0) 508 cfg->stat_interval = 0; 509 else if(atoi(val) == 0) 510 return 0; 511 else cfg->stat_interval = atoi(val); 512 } else if(strcmp(opt, "num-threads:") == 0) { 513 /* not supported, library must have 1 thread in bgworker */ 514 return 0; 515 } else if(strcmp(opt, "outgoing-port-permit:") == 0) { 516 return cfg_mark_ports(val, 1, 517 cfg->outgoing_avail_ports, 65536); 518 } else if(strcmp(opt, "outgoing-port-avoid:") == 0) { 519 return cfg_mark_ports(val, 0, 520 cfg->outgoing_avail_ports, 65536); 521 } else if(strcmp(opt, "local-zone:") == 0) { 522 return cfg_parse_local_zone(cfg, val); 523 } else if(strcmp(opt, "val-override-date:") == 0) { 524 if(strcmp(val, "") == 0 || strcmp(val, "0") == 0) { 525 cfg->val_date_override = 0; 526 } else if(strlen(val) == 14) { 527 cfg->val_date_override = cfg_convert_timeval(val); 528 return cfg->val_date_override != 0; 529 } else { 530 if(atoi(val) == 0) return 0; 531 cfg->val_date_override = (uint32_t)atoi(val); 532 } 533 } else if(strcmp(opt, "local-data-ptr:") == 0) { 534 char* ptr = cfg_ptr_reverse((char*)opt); 535 return cfg_strlist_insert(&cfg->local_data, ptr); 536 } else if(strcmp(opt, "logfile:") == 0) { 537 cfg->use_syslog = 0; 538 free(cfg->logfile); 539 return (cfg->logfile = strdup(val)) != NULL; 540 } 541 else if(strcmp(opt, "log-time-ascii:") == 0) 542 { IS_YES_OR_NO; cfg->log_time_ascii = (strcmp(val, "yes") == 0); 543 log_set_time_asc(cfg->log_time_ascii); } 544 else S_SIZET_NONZERO("max-udp-size:", max_udp_size) 545 else S_YNO("use-syslog:", use_syslog) 546 else S_STR("log-identity:", log_identity) 547 else S_YNO("extended-statistics:", stat_extended) 548 else S_YNO("statistics-inhibit-zero:", stat_inhibit_zero) 549 else S_YNO("statistics-cumulative:", stat_cumulative) 550 else S_YNO("shm-enable:", shm_enable) 551 else S_NUMBER_OR_ZERO("shm-key:", shm_key) 552 else S_YNO("do-ip4:", do_ip4) 553 else S_YNO("do-ip6:", do_ip6) 554 else S_YNO("do-udp:", do_udp) 555 else S_YNO("do-tcp:", do_tcp) 556 else S_YNO("prefer-ip4:", prefer_ip4) 557 else S_YNO("prefer-ip6:", prefer_ip6) 558 else S_YNO("tcp-upstream:", tcp_upstream) 559 else S_YNO("udp-upstream-without-downstream:", 560 udp_upstream_without_downstream) 561 else S_NUMBER_NONZERO("tcp-mss:", tcp_mss) 562 else S_NUMBER_NONZERO("outgoing-tcp-mss:", outgoing_tcp_mss) 563 else S_NUMBER_NONZERO("tcp-auth-query-timeout:", tcp_auth_query_timeout) 564 else S_NUMBER_NONZERO("tcp-idle-timeout:", tcp_idle_timeout) 565 else S_NUMBER_NONZERO("max-reuse-tcp-queries:", max_reuse_tcp_queries) 566 else S_NUMBER_NONZERO("tcp-reuse-timeout:", tcp_reuse_timeout) 567 else S_YNO("edns-tcp-keepalive:", do_tcp_keepalive) 568 else S_NUMBER_NONZERO("edns-tcp-keepalive-timeout:", tcp_keepalive_timeout) 569 else S_NUMBER_OR_ZERO("sock-queue-timeout:", sock_queue_timeout) 570 else S_YNO("ssl-upstream:", ssl_upstream) 571 else S_YNO("tls-upstream:", ssl_upstream) 572 else S_STR("ssl-service-key:", ssl_service_key) 573 else S_STR("tls-service-key:", ssl_service_key) 574 else S_STR("ssl-service-pem:", ssl_service_pem) 575 else S_STR("tls-service-pem:", ssl_service_pem) 576 else S_NUMBER_NONZERO("ssl-port:", ssl_port) 577 else S_NUMBER_NONZERO("tls-port:", ssl_port) 578 else S_STR("ssl-cert-bundle:", tls_cert_bundle) 579 else S_STR("tls-cert-bundle:", tls_cert_bundle) 580 else S_YNO("tls-win-cert:", tls_win_cert) 581 else S_YNO("tls-system-cert:", tls_win_cert) 582 else S_STRLIST("additional-ssl-port:", tls_additional_port) 583 else S_STRLIST("additional-tls-port:", tls_additional_port) 584 else S_STRLIST("tls-additional-ports:", tls_additional_port) 585 else S_STRLIST("tls-additional-port:", tls_additional_port) 586 else S_STRLIST_APPEND("tls-session-ticket-keys:", tls_session_ticket_keys) 587 else S_STR("tls-ciphers:", tls_ciphers) 588 else S_STR("tls-ciphersuites:", tls_ciphersuites) 589 else S_YNO("tls-use-sni:", tls_use_sni) 590 else S_NUMBER_NONZERO("https-port:", https_port) 591 else S_STR("http-endpoint:", http_endpoint) 592 else S_NUMBER_NONZERO("http-max-streams:", http_max_streams) 593 else S_MEMSIZE("http-query-buffer-size:", http_query_buffer_size) 594 else S_MEMSIZE("http-response-buffer-size:", http_response_buffer_size) 595 else S_YNO("http-nodelay:", http_nodelay) 596 else S_YNO("http-notls-downstream:", http_notls_downstream) 597 else S_YNO("interface-automatic:", if_automatic) 598 else S_STR("interface-automatic-ports:", if_automatic_ports) 599 else S_YNO("use-systemd:", use_systemd) 600 else S_YNO("do-daemonize:", do_daemonize) 601 else S_NUMBER_NONZERO("port:", port) 602 else S_NUMBER_NONZERO("outgoing-range:", outgoing_num_ports) 603 else S_SIZET_OR_ZERO("outgoing-num-tcp:", outgoing_num_tcp) 604 else S_SIZET_OR_ZERO("incoming-num-tcp:", incoming_num_tcp) 605 else S_MEMSIZE("stream-wait-size:", stream_wait_size) 606 else S_SIZET_NONZERO("edns-buffer-size:", edns_buffer_size) 607 else S_SIZET_NONZERO("msg-buffer-size:", msg_buffer_size) 608 else S_MEMSIZE("msg-cache-size:", msg_cache_size) 609 else S_POW2("msg-cache-slabs:", msg_cache_slabs) 610 else S_SIZET_NONZERO("num-queries-per-thread:",num_queries_per_thread) 611 else S_SIZET_OR_ZERO("jostle-timeout:", jostle_time) 612 else S_MEMSIZE("so-rcvbuf:", so_rcvbuf) 613 else S_MEMSIZE("so-sndbuf:", so_sndbuf) 614 else S_YNO("so-reuseport:", so_reuseport) 615 else S_YNO("ip-transparent:", ip_transparent) 616 else S_YNO("ip-freebind:", ip_freebind) 617 else S_NUMBER_OR_ZERO("ip-dscp:", ip_dscp) 618 else S_MEMSIZE("rrset-cache-size:", rrset_cache_size) 619 else S_POW2("rrset-cache-slabs:", rrset_cache_slabs) 620 else S_YNO("prefetch:", prefetch) 621 else S_YNO("prefetch-key:", prefetch_key) 622 else S_YNO("deny-any:", deny_any) 623 else if(strcmp(opt, "cache-max-ttl:") == 0) 624 { IS_NUMBER_OR_ZERO; cfg->max_ttl = atoi(val); MAX_TTL=(time_t)cfg->max_ttl;} 625 else if(strcmp(opt, "cache-max-negative-ttl:") == 0) 626 { IS_NUMBER_OR_ZERO; cfg->max_negative_ttl = atoi(val); MAX_NEG_TTL=(time_t)cfg->max_negative_ttl;} 627 else if(strcmp(opt, "cache-min-negative-ttl:") == 0) 628 { IS_NUMBER_OR_ZERO; cfg->min_negative_ttl = atoi(val); MIN_NEG_TTL=(time_t)cfg->min_negative_ttl;} 629 else if(strcmp(opt, "cache-min-ttl:") == 0) 630 { IS_NUMBER_OR_ZERO; cfg->min_ttl = atoi(val); MIN_TTL=(time_t)cfg->min_ttl;} 631 else if(strcmp(opt, "infra-cache-min-rtt:") == 0) { 632 IS_NUMBER_OR_ZERO; cfg->infra_cache_min_rtt = atoi(val); 633 RTT_MIN_TIMEOUT=cfg->infra_cache_min_rtt; 634 } 635 else if(strcmp(opt, "infra-cache-max-rtt:") == 0) { 636 IS_NUMBER_OR_ZERO; cfg->infra_cache_max_rtt = atoi(val); 637 RTT_MAX_TIMEOUT=cfg->infra_cache_max_rtt; 638 USEFUL_SERVER_TOP_TIMEOUT = RTT_MAX_TIMEOUT; 639 BLACKLIST_PENALTY = USEFUL_SERVER_TOP_TIMEOUT*4; 640 } 641 else S_YNO("infra-keep-probing:", infra_keep_probing) 642 else S_NUMBER_OR_ZERO("infra-host-ttl:", host_ttl) 643 else S_POW2("infra-cache-slabs:", infra_cache_slabs) 644 else S_SIZET_NONZERO("infra-cache-numhosts:", infra_cache_numhosts) 645 else S_NUMBER_OR_ZERO("delay-close:", delay_close) 646 else S_YNO("udp-connect:", udp_connect) 647 else S_STR("chroot:", chrootdir) 648 else S_STR("username:", username) 649 else S_STR("directory:", directory) 650 else S_STR("pidfile:", pidfile) 651 else S_YNO("hide-identity:", hide_identity) 652 else S_YNO("hide-version:", hide_version) 653 else S_YNO("hide-trustanchor:", hide_trustanchor) 654 else S_YNO("hide-http-user-agent:", hide_http_user_agent) 655 else S_STR("identity:", identity) 656 else S_STR("version:", version) 657 else S_STR("http-user-agent:", http_user_agent) 658 else if(strcmp(opt, "nsid:") == 0) { 659 free(cfg->nsid_cfg_str); 660 if (!(cfg->nsid_cfg_str = strdup(val))) 661 return 0; 662 /* Empty string is just validly unsetting nsid */ 663 if (*val == 0) { 664 free(cfg->nsid); 665 cfg->nsid = NULL; 666 cfg->nsid_len = 0; 667 return 1; 668 } 669 cfg->nsid = cfg_parse_nsid(val, &cfg->nsid_len); 670 return cfg->nsid != NULL; 671 } 672 else S_STRLIST("root-hints:", root_hints) 673 else S_STR("target-fetch-policy:", target_fetch_policy) 674 else S_YNO("harden-glue:", harden_glue) 675 else S_YNO("harden-short-bufsize:", harden_short_bufsize) 676 else S_YNO("harden-large-queries:", harden_large_queries) 677 else S_YNO("harden-dnssec-stripped:", harden_dnssec_stripped) 678 else S_YNO("harden-below-nxdomain:", harden_below_nxdomain) 679 else S_YNO("harden-referral-path:", harden_referral_path) 680 else S_YNO("harden-algo-downgrade:", harden_algo_downgrade) 681 else S_YNO("harden-unknown-additional:", harden_unknown_additional) 682 else S_YNO("use-caps-for-id:", use_caps_bits_for_id) 683 else S_STRLIST("caps-whitelist:", caps_whitelist) 684 else S_SIZET_OR_ZERO("unwanted-reply-threshold:", unwanted_threshold) 685 else S_STRLIST("private-address:", private_address) 686 else S_STRLIST("private-domain:", private_domain) 687 else S_YNO("do-not-query-localhost:", donotquery_localhost) 688 else S_STRLIST("do-not-query-address:", donotqueryaddrs) 689 else S_STRLIST("auto-trust-anchor-file:", auto_trust_anchor_file_list) 690 else S_STRLIST("trust-anchor-file:", trust_anchor_file_list) 691 else S_STRLIST("trust-anchor:", trust_anchor_list) 692 else S_STRLIST("trusted-keys-file:", trusted_keys_file_list) 693 else S_YNO("trust-anchor-signaling:", trust_anchor_signaling) 694 else S_YNO("root-key-sentinel:", root_key_sentinel) 695 else S_STRLIST("domain-insecure:", domain_insecure) 696 else S_NUMBER_OR_ZERO("val-bogus-ttl:", bogus_ttl) 697 else S_YNO("val-clean-additional:", val_clean_additional) 698 else S_NUMBER_OR_ZERO("val-log-level:", val_log_level) 699 else S_YNO("val-log-squelch:", val_log_squelch) 700 else S_YNO("log-queries:", log_queries) 701 else S_YNO("log-replies:", log_replies) 702 else S_YNO("log-tag-queryreply:", log_tag_queryreply) 703 else S_YNO("log-local-actions:", log_local_actions) 704 else S_YNO("log-servfail:", log_servfail) 705 else S_YNO("log-destaddr:", log_destaddr) 706 else S_YNO("val-permissive-mode:", val_permissive_mode) 707 else S_YNO("aggressive-nsec:", aggressive_nsec) 708 else S_YNO("ignore-cd-flag:", ignore_cd) 709 else S_YNO("disable-edns-do:", disable_edns_do) 710 else if(strcmp(opt, "serve-expired:") == 0) 711 { IS_YES_OR_NO; cfg->serve_expired = (strcmp(val, "yes") == 0); 712 SERVE_EXPIRED = cfg->serve_expired; } 713 else if(strcmp(opt, "serve-expired-ttl:") == 0) 714 { IS_NUMBER_OR_ZERO; cfg->serve_expired_ttl = atoi(val); SERVE_EXPIRED_TTL=(time_t)cfg->serve_expired_ttl;} 715 else S_YNO("serve-expired-ttl-reset:", serve_expired_ttl_reset) 716 else if(strcmp(opt, "serve-expired-reply-ttl:") == 0) 717 { IS_NUMBER_OR_ZERO; cfg->serve_expired_reply_ttl = atoi(val); SERVE_EXPIRED_REPLY_TTL=(time_t)cfg->serve_expired_reply_ttl;} 718 else S_NUMBER_OR_ZERO("serve-expired-client-timeout:", serve_expired_client_timeout) 719 else S_YNO("ede:", ede) 720 else S_YNO("ede-serve-expired:", ede_serve_expired) 721 else S_YNO("serve-original-ttl:", serve_original_ttl) 722 else S_STR("val-nsec3-keysize-iterations:", val_nsec3_key_iterations) 723 else S_YNO("zonemd-permissive-mode:", zonemd_permissive_mode) 724 else S_UNSIGNED_OR_ZERO("add-holddown:", add_holddown) 725 else S_UNSIGNED_OR_ZERO("del-holddown:", del_holddown) 726 else S_UNSIGNED_OR_ZERO("keep-missing:", keep_missing) 727 else if(strcmp(opt, "permit-small-holddown:") == 0) 728 { IS_YES_OR_NO; cfg->permit_small_holddown = (strcmp(val, "yes") == 0); 729 autr_permit_small_holddown = cfg->permit_small_holddown; } 730 else S_MEMSIZE("key-cache-size:", key_cache_size) 731 else S_POW2("key-cache-slabs:", key_cache_slabs) 732 else S_MEMSIZE("neg-cache-size:", neg_cache_size) 733 else S_YNO("minimal-responses:", minimal_responses) 734 else S_YNO("rrset-roundrobin:", rrset_roundrobin) 735 else S_NUMBER_OR_ZERO("unknown-server-time-limit:", unknown_server_time_limit) 736 else S_NUMBER_OR_ZERO("discard-timeout:", discard_timeout) 737 else S_NUMBER_OR_ZERO("wait-limit:", wait_limit) 738 else S_NUMBER_OR_ZERO("wait-limit-cookie:", wait_limit_cookie) 739 else S_STRLIST("local-data:", local_data) 740 else S_YNO("unblock-lan-zones:", unblock_lan_zones) 741 else S_YNO("insecure-lan-zones:", insecure_lan_zones) 742 else S_YNO("control-enable:", remote_control_enable) 743 else S_STRLIST_APPEND("control-interface:", control_ifs) 744 else S_NUMBER_NONZERO("control-port:", control_port) 745 else S_STR("server-key-file:", server_key_file) 746 else S_STR("server-cert-file:", server_cert_file) 747 else S_STR("control-key-file:", control_key_file) 748 else S_STR("control-cert-file:", control_cert_file) 749 else S_STR("module-config:", module_conf) 750 else S_STRLIST("python-script:", python_script) 751 else S_STRLIST("dynlib-file:", dynlib_file) 752 else S_YNO("disable-dnssec-lame-check:", disable_dnssec_lame_check) 753 #ifdef CLIENT_SUBNET 754 /* Can't set max subnet prefix here, since that value is used when 755 * generating the address tree. */ 756 /* No client-subnet-always-forward here, module registration depends on 757 * this option. */ 758 #endif 759 #ifdef USE_DNSTAP 760 else S_YNO("dnstap-enable:", dnstap) 761 else S_YNO("dnstap-bidirectional:", dnstap_bidirectional) 762 else S_STR("dnstap-socket-path:", dnstap_socket_path) 763 else S_STR("dnstap-ip:", dnstap_ip) 764 else S_YNO("dnstap-tls:", dnstap_tls) 765 else S_STR("dnstap-tls-server-name:", dnstap_tls_server_name) 766 else S_STR("dnstap-tls-cert-bundle:", dnstap_tls_cert_bundle) 767 else S_STR("dnstap-tls-client-key-file:", dnstap_tls_client_key_file) 768 else S_STR("dnstap-tls-client-cert-file:", 769 dnstap_tls_client_cert_file) 770 else S_YNO("dnstap-send-identity:", dnstap_send_identity) 771 else S_YNO("dnstap-send-version:", dnstap_send_version) 772 else S_STR("dnstap-identity:", dnstap_identity) 773 else S_STR("dnstap-version:", dnstap_version) 774 else S_NUMBER_OR_ZERO("dnstap-sample-rate:", dnstap_sample_rate) 775 else S_YNO("dnstap-log-resolver-query-messages:", 776 dnstap_log_resolver_query_messages) 777 else S_YNO("dnstap-log-resolver-response-messages:", 778 dnstap_log_resolver_response_messages) 779 else S_YNO("dnstap-log-client-query-messages:", 780 dnstap_log_client_query_messages) 781 else S_YNO("dnstap-log-client-response-messages:", 782 dnstap_log_client_response_messages) 783 else S_YNO("dnstap-log-forwarder-query-messages:", 784 dnstap_log_forwarder_query_messages) 785 else S_YNO("dnstap-log-forwarder-response-messages:", 786 dnstap_log_forwarder_response_messages) 787 #endif 788 #ifdef USE_DNSCRYPT 789 else S_YNO("dnscrypt-enable:", dnscrypt) 790 else S_NUMBER_NONZERO("dnscrypt-port:", dnscrypt_port) 791 else S_STR("dnscrypt-provider:", dnscrypt_provider) 792 else S_STRLIST_UNIQ("dnscrypt-provider-cert:", dnscrypt_provider_cert) 793 else S_STRLIST("dnscrypt-provider-cert-rotated:", dnscrypt_provider_cert_rotated) 794 else S_STRLIST_UNIQ("dnscrypt-secret-key:", dnscrypt_secret_key) 795 else S_MEMSIZE("dnscrypt-shared-secret-cache-size:", 796 dnscrypt_shared_secret_cache_size) 797 else S_POW2("dnscrypt-shared-secret-cache-slabs:", 798 dnscrypt_shared_secret_cache_slabs) 799 else S_MEMSIZE("dnscrypt-nonce-cache-size:", 800 dnscrypt_nonce_cache_size) 801 else S_POW2("dnscrypt-nonce-cache-slabs:", 802 dnscrypt_nonce_cache_slabs) 803 #endif 804 else if(strcmp(opt, "ip-ratelimit-cookie:") == 0) { 805 IS_NUMBER_OR_ZERO; cfg->ip_ratelimit_cookie = atoi(val); 806 infra_ip_ratelimit_cookie=cfg->ip_ratelimit_cookie; 807 } 808 else if(strcmp(opt, "ip-ratelimit:") == 0) { 809 IS_NUMBER_OR_ZERO; cfg->ip_ratelimit = atoi(val); 810 infra_ip_ratelimit=cfg->ip_ratelimit; 811 } 812 else if(strcmp(opt, "ratelimit:") == 0) { 813 IS_NUMBER_OR_ZERO; cfg->ratelimit = atoi(val); 814 infra_dp_ratelimit=cfg->ratelimit; 815 } 816 else S_MEMSIZE("ip-ratelimit-size:", ip_ratelimit_size) 817 else S_MEMSIZE("ratelimit-size:", ratelimit_size) 818 else S_POW2("ip-ratelimit-slabs:", ip_ratelimit_slabs) 819 else S_POW2("ratelimit-slabs:", ratelimit_slabs) 820 else S_NUMBER_OR_ZERO("ip-ratelimit-factor:", ip_ratelimit_factor) 821 else S_NUMBER_OR_ZERO("ratelimit-factor:", ratelimit_factor) 822 else S_YNO("ip-ratelimit-backoff:", ip_ratelimit_backoff) 823 else S_YNO("ratelimit-backoff:", ratelimit_backoff) 824 else S_NUMBER_NONZERO("outbound-msg-retry:", outbound_msg_retry) 825 else S_NUMBER_NONZERO("max-sent-count:", max_sent_count) 826 else S_NUMBER_NONZERO("max-query-restarts:", max_query_restarts) 827 else S_SIZET_NONZERO("fast-server-num:", fast_server_num) 828 else S_NUMBER_OR_ZERO("fast-server-permil:", fast_server_permil) 829 else S_YNO("qname-minimisation:", qname_minimisation) 830 else S_YNO("qname-minimisation-strict:", qname_minimisation_strict) 831 else S_YNO("pad-responses:", pad_responses) 832 else S_SIZET_NONZERO("pad-responses-block-size:", pad_responses_block_size) 833 else S_YNO("pad-queries:", pad_queries) 834 else S_SIZET_NONZERO("pad-queries-block-size:", pad_queries_block_size) 835 else S_STRLIST("proxy-protocol-port:", proxy_protocol_port) 836 #ifdef USE_IPSECMOD 837 else S_YNO("ipsecmod-enabled:", ipsecmod_enabled) 838 else S_YNO("ipsecmod-ignore-bogus:", ipsecmod_ignore_bogus) 839 else if(strcmp(opt, "ipsecmod-max-ttl:") == 0) 840 { IS_NUMBER_OR_ZERO; cfg->ipsecmod_max_ttl = atoi(val); } 841 else S_YNO("ipsecmod-strict:", ipsecmod_strict) 842 #endif 843 else S_YNO("answer-cookie:", do_answer_cookie) 844 else S_STR("cookie-secret-file:", cookie_secret_file) 845 #ifdef USE_CACHEDB 846 else S_YNO("cachedb-no-store:", cachedb_no_store) 847 else S_YNO("cachedb-check-when-serve-expired:", cachedb_check_when_serve_expired) 848 #endif /* USE_CACHEDB */ 849 else if(strcmp(opt, "define-tag:") ==0) { 850 return config_add_tag(cfg, val); 851 /* val_sig_skew_min, max and val_max_restart are copied into val_env 852 * during init so this does not update val_env with set_option */ 853 } else if(strcmp(opt, "val-sig-skew-min:") == 0) 854 { IS_NUMBER_OR_ZERO; cfg->val_sig_skew_min = (int32_t)atoi(val); } 855 else if(strcmp(opt, "val-sig-skew-max:") == 0) 856 { IS_NUMBER_OR_ZERO; cfg->val_sig_skew_max = (int32_t)atoi(val); } 857 else if(strcmp(opt, "val-max-restart:") == 0) 858 { IS_NUMBER_OR_ZERO; cfg->val_max_restart = (int32_t)atoi(val); } 859 else if (strcmp(opt, "outgoing-interface:") == 0) { 860 char* d = strdup(val); 861 char** oi = 862 (char**)reallocarray(NULL, (size_t)cfg->num_out_ifs+1, sizeof(char*)); 863 if(!d || !oi) { free(d); free(oi); return -1; } 864 if(cfg->out_ifs && cfg->num_out_ifs) { 865 memmove(oi, cfg->out_ifs, cfg->num_out_ifs*sizeof(char*)); 866 free(cfg->out_ifs); 867 } 868 oi[cfg->num_out_ifs++] = d; 869 cfg->out_ifs = oi; 870 } else { 871 /* unknown or unsupported (from the set_option interface): 872 * interface, outgoing-interface, access-control, 873 * stub-zone, name, stub-addr, stub-host, stub-prime 874 * forward-first, stub-first, forward-ssl-upstream, 875 * stub-ssl-upstream, forward-zone, auth-zone 876 * name, forward-addr, forward-host, 877 * ratelimit-for-domain, ratelimit-below-domain, 878 * local-zone-tag, access-control-view, interface-*, 879 * send-client-subnet, client-subnet-always-forward, 880 * max-client-subnet-ipv4, max-client-subnet-ipv6, 881 * min-client-subnet-ipv4, min-client-subnet-ipv6, 882 * max-ecs-tree-size-ipv4, max-ecs-tree-size-ipv6, ipsecmod_hook, 883 * ipsecmod_whitelist. */ 884 return 0; 885 } 886 return 1; 887 } 888 889 void config_print_func(char* line, void* arg) 890 { 891 FILE* f = (FILE*)arg; 892 (void)fprintf(f, "%s\n", line); 893 } 894 895 /** collate func arg */ 896 struct config_collate_arg { 897 /** list of result items */ 898 struct config_strlist_head list; 899 /** if a malloc error occurred, 0 is OK */ 900 int status; 901 }; 902 903 void config_collate_func(char* line, void* arg) 904 { 905 struct config_collate_arg* m = (struct config_collate_arg*)arg; 906 if(m->status) 907 return; 908 if(!cfg_strlist_append(&m->list, strdup(line))) 909 m->status = 1; 910 } 911 912 int config_get_option_list(struct config_file* cfg, const char* opt, 913 struct config_strlist** list) 914 { 915 struct config_collate_arg m; 916 memset(&m, 0, sizeof(m)); 917 *list = NULL; 918 if(!config_get_option(cfg, opt, config_collate_func, &m)) 919 return 1; 920 if(m.status) { 921 config_delstrlist(m.list.first); 922 return 2; 923 } 924 *list = m.list.first; 925 return 0; 926 } 927 928 int 929 config_get_option_collate(struct config_file* cfg, const char* opt, char** str) 930 { 931 struct config_strlist* list = NULL; 932 int r; 933 *str = NULL; 934 if((r = config_get_option_list(cfg, opt, &list)) != 0) 935 return r; 936 *str = config_collate_cat(list); 937 config_delstrlist(list); 938 if(!*str) return 2; 939 return 0; 940 } 941 942 char* 943 config_collate_cat(struct config_strlist* list) 944 { 945 size_t total = 0, left; 946 struct config_strlist* s; 947 char *r, *w; 948 if(!list) /* no elements */ 949 return strdup(""); 950 if(list->next == NULL) /* one element , no newline at end. */ 951 return strdup(list->str); 952 /* count total length */ 953 for(s=list; s; s=s->next) 954 total += strlen(s->str) + 1; /* len + newline */ 955 left = total+1; /* one extra for nul at end */ 956 r = malloc(left); 957 if(!r) 958 return NULL; 959 w = r; 960 for(s=list; s; s=s->next) { 961 size_t this = strlen(s->str); 962 if(this+2 > left) { /* sanity check */ 963 free(r); 964 return NULL; 965 } 966 snprintf(w, left, "%s\n", s->str); 967 this = strlen(w); 968 w += this; 969 left -= this; 970 } 971 return r; 972 } 973 974 /** compare and print decimal option */ 975 #define O_DEC(opt, str, var) if(strcmp(opt, str)==0) \ 976 {snprintf(buf, len, "%d", (int)cfg->var); \ 977 func(buf, arg);} 978 /** compare and print unsigned option */ 979 #define O_UNS(opt, str, var) if(strcmp(opt, str)==0) \ 980 {snprintf(buf, len, "%u", (unsigned)cfg->var); \ 981 func(buf, arg);} 982 /** compare and print yesno option */ 983 #define O_YNO(opt, str, var) if(strcmp(opt, str)==0) \ 984 {func(cfg->var?"yes":"no", arg);} 985 /** compare and print string option */ 986 #define O_STR(opt, str, var) if(strcmp(opt, str)==0) \ 987 {func(cfg->var?cfg->var:"", arg);} 988 /** compare and print array option */ 989 #define O_IFC(opt, str, num, arr) if(strcmp(opt, str)==0) \ 990 {int i; for(i=0; i<cfg->num; i++) func(cfg->arr[i], arg);} 991 /** compare and print memorysize option */ 992 #define O_MEM(opt, str, var) if(strcmp(opt, str)==0) { \ 993 if(cfg->var > 1024*1024*1024) { \ 994 size_t f=cfg->var/(size_t)1000000, b=cfg->var%(size_t)1000000; \ 995 snprintf(buf, len, "%u%6.6u", (unsigned)f, (unsigned)b); \ 996 } else snprintf(buf, len, "%u", (unsigned)cfg->var); \ 997 func(buf, arg);} 998 /** compare and print list option */ 999 #define O_LST(opt, name, lst) if(strcmp(opt, name)==0) { \ 1000 struct config_strlist* p = cfg->lst; \ 1001 for(p = cfg->lst; p; p = p->next) \ 1002 func(p->str, arg); \ 1003 } 1004 /** compare and print list option */ 1005 #define O_LS2(opt, name, lst) if(strcmp(opt, name)==0) { \ 1006 struct config_str2list* p = cfg->lst; \ 1007 for(p = cfg->lst; p; p = p->next) { \ 1008 snprintf(buf, len, "%s %s", p->str, p->str2); \ 1009 func(buf, arg); \ 1010 } \ 1011 } 1012 /** compare and print list option */ 1013 #define O_LS3(opt, name, lst) if(strcmp(opt, name)==0) { \ 1014 struct config_str3list* p = cfg->lst; \ 1015 for(p = cfg->lst; p; p = p->next) { \ 1016 snprintf(buf, len, "%s %s %s", p->str, p->str2, p->str3); \ 1017 func(buf, arg); \ 1018 } \ 1019 } 1020 /** compare and print taglist option */ 1021 #define O_LTG(opt, name, lst) if(strcmp(opt, name)==0) { \ 1022 char* tmpstr = NULL; \ 1023 struct config_strbytelist *p = cfg->lst; \ 1024 for(p = cfg->lst; p; p = p->next) {\ 1025 tmpstr = config_taglist2str(cfg, p->str2, p->str2len); \ 1026 if(tmpstr) {\ 1027 snprintf(buf, len, "%s %s", p->str, tmpstr); \ 1028 func(buf, arg); \ 1029 free(tmpstr); \ 1030 } \ 1031 } \ 1032 } 1033 1034 int 1035 config_get_option(struct config_file* cfg, const char* opt, 1036 void (*func)(char*,void*), void* arg) 1037 { 1038 char buf[1024], nopt[64]; 1039 size_t len = sizeof(buf); 1040 if(!opt) return 0; 1041 if(opt && opt[strlen(opt)-1] == ':' && strlen(opt)<sizeof(nopt)) { 1042 memmove(nopt, opt, strlen(opt)); 1043 nopt[strlen(opt)-1] = 0; 1044 opt = nopt; 1045 } 1046 fptr_ok(fptr_whitelist_print_func(func)); 1047 O_DEC(opt, "verbosity", verbosity) 1048 else O_DEC(opt, "statistics-interval", stat_interval) 1049 else O_YNO(opt, "statistics-cumulative", stat_cumulative) 1050 else O_YNO(opt, "extended-statistics", stat_extended) 1051 else O_YNO(opt, "statistics-inhibit-zero", stat_inhibit_zero) 1052 else O_YNO(opt, "shm-enable", shm_enable) 1053 else O_DEC(opt, "shm-key", shm_key) 1054 else O_YNO(opt, "use-syslog", use_syslog) 1055 else O_STR(opt, "log-identity", log_identity) 1056 else O_YNO(opt, "log-time-ascii", log_time_ascii) 1057 else O_DEC(opt, "num-threads", num_threads) 1058 else O_IFC(opt, "interface", num_ifs, ifs) 1059 else O_IFC(opt, "outgoing-interface", num_out_ifs, out_ifs) 1060 else O_YNO(opt, "interface-automatic", if_automatic) 1061 else O_STR(opt, "interface-automatic-ports", if_automatic_ports) 1062 else O_DEC(opt, "port", port) 1063 else O_DEC(opt, "outgoing-range", outgoing_num_ports) 1064 else O_DEC(opt, "outgoing-num-tcp", outgoing_num_tcp) 1065 else O_DEC(opt, "incoming-num-tcp", incoming_num_tcp) 1066 else O_MEM(opt, "stream-wait-size", stream_wait_size) 1067 else O_DEC(opt, "edns-buffer-size", edns_buffer_size) 1068 else O_DEC(opt, "msg-buffer-size", msg_buffer_size) 1069 else O_MEM(opt, "msg-cache-size", msg_cache_size) 1070 else O_DEC(opt, "msg-cache-slabs", msg_cache_slabs) 1071 else O_DEC(opt, "num-queries-per-thread", num_queries_per_thread) 1072 else O_UNS(opt, "jostle-timeout", jostle_time) 1073 else O_MEM(opt, "so-rcvbuf", so_rcvbuf) 1074 else O_MEM(opt, "so-sndbuf", so_sndbuf) 1075 else O_YNO(opt, "so-reuseport", so_reuseport) 1076 else O_YNO(opt, "ip-transparent", ip_transparent) 1077 else O_YNO(opt, "ip-freebind", ip_freebind) 1078 else O_DEC(opt, "ip-dscp", ip_dscp) 1079 else O_MEM(opt, "rrset-cache-size", rrset_cache_size) 1080 else O_DEC(opt, "rrset-cache-slabs", rrset_cache_slabs) 1081 else O_YNO(opt, "prefetch-key", prefetch_key) 1082 else O_YNO(opt, "prefetch", prefetch) 1083 else O_YNO(opt, "deny-any", deny_any) 1084 else O_DEC(opt, "cache-max-ttl", max_ttl) 1085 else O_DEC(opt, "cache-max-negative-ttl", max_negative_ttl) 1086 else O_DEC(opt, "cache-min-negative-ttl", min_negative_ttl) 1087 else O_DEC(opt, "cache-min-ttl", min_ttl) 1088 else O_DEC(opt, "infra-host-ttl", host_ttl) 1089 else O_DEC(opt, "infra-cache-slabs", infra_cache_slabs) 1090 else O_DEC(opt, "infra-cache-min-rtt", infra_cache_min_rtt) 1091 else O_UNS(opt, "infra-cache-max-rtt", infra_cache_max_rtt) 1092 else O_YNO(opt, "infra-keep-probing", infra_keep_probing) 1093 else O_MEM(opt, "infra-cache-numhosts", infra_cache_numhosts) 1094 else O_UNS(opt, "delay-close", delay_close) 1095 else O_YNO(opt, "udp-connect", udp_connect) 1096 else O_YNO(opt, "do-ip4", do_ip4) 1097 else O_YNO(opt, "do-ip6", do_ip6) 1098 else O_YNO(opt, "do-udp", do_udp) 1099 else O_YNO(opt, "do-tcp", do_tcp) 1100 else O_YNO(opt, "prefer-ip4", prefer_ip4) 1101 else O_YNO(opt, "prefer-ip6", prefer_ip6) 1102 else O_YNO(opt, "tcp-upstream", tcp_upstream) 1103 else O_YNO(opt, "udp-upstream-without-downstream", udp_upstream_without_downstream) 1104 else O_DEC(opt, "tcp-mss", tcp_mss) 1105 else O_DEC(opt, "outgoing-tcp-mss", outgoing_tcp_mss) 1106 else O_DEC(opt, "tcp-auth-query-timeout", tcp_auth_query_timeout) 1107 else O_DEC(opt, "tcp-idle-timeout", tcp_idle_timeout) 1108 else O_DEC(opt, "max-reuse-tcp-queries", max_reuse_tcp_queries) 1109 else O_DEC(opt, "tcp-reuse-timeout", tcp_reuse_timeout) 1110 else O_YNO(opt, "edns-tcp-keepalive", do_tcp_keepalive) 1111 else O_DEC(opt, "edns-tcp-keepalive-timeout", tcp_keepalive_timeout) 1112 else O_DEC(opt, "sock-queue-timeout", sock_queue_timeout) 1113 else O_YNO(opt, "ssl-upstream", ssl_upstream) 1114 else O_YNO(opt, "tls-upstream", ssl_upstream) 1115 else O_STR(opt, "ssl-service-key", ssl_service_key) 1116 else O_STR(opt, "tls-service-key", ssl_service_key) 1117 else O_STR(opt, "ssl-service-pem", ssl_service_pem) 1118 else O_STR(opt, "tls-service-pem", ssl_service_pem) 1119 else O_DEC(opt, "ssl-port", ssl_port) 1120 else O_DEC(opt, "tls-port", ssl_port) 1121 else O_STR(opt, "ssl-cert-bundle", tls_cert_bundle) 1122 else O_STR(opt, "tls-cert-bundle", tls_cert_bundle) 1123 else O_YNO(opt, "tls-win-cert", tls_win_cert) 1124 else O_YNO(opt, "tls-system-cert", tls_win_cert) 1125 else O_LST(opt, "additional-ssl-port", tls_additional_port) 1126 else O_LST(opt, "additional-tls-port", tls_additional_port) 1127 else O_LST(opt, "tls-additional-ports", tls_additional_port) 1128 else O_LST(opt, "tls-additional-port", tls_additional_port) 1129 else O_LST(opt, "tls-session-ticket-keys", tls_session_ticket_keys.first) 1130 else O_STR(opt, "tls-ciphers", tls_ciphers) 1131 else O_STR(opt, "tls-ciphersuites", tls_ciphersuites) 1132 else O_YNO(opt, "tls-use-sni", tls_use_sni) 1133 else O_DEC(opt, "https-port", https_port) 1134 else O_STR(opt, "http-endpoint", http_endpoint) 1135 else O_UNS(opt, "http-max-streams", http_max_streams) 1136 else O_MEM(opt, "http-query-buffer-size", http_query_buffer_size) 1137 else O_MEM(opt, "http-response-buffer-size", http_response_buffer_size) 1138 else O_YNO(opt, "http-nodelay", http_nodelay) 1139 else O_YNO(opt, "http-notls-downstream", http_notls_downstream) 1140 else O_YNO(opt, "use-systemd", use_systemd) 1141 else O_YNO(opt, "do-daemonize", do_daemonize) 1142 else O_STR(opt, "chroot", chrootdir) 1143 else O_STR(opt, "username", username) 1144 else O_STR(opt, "directory", directory) 1145 else O_STR(opt, "logfile", logfile) 1146 else O_YNO(opt, "log-queries", log_queries) 1147 else O_YNO(opt, "log-replies", log_replies) 1148 else O_YNO(opt, "log-tag-queryreply", log_tag_queryreply) 1149 else O_YNO(opt, "log-local-actions", log_local_actions) 1150 else O_YNO(opt, "log-servfail", log_servfail) 1151 else O_YNO(opt, "log-destaddr", log_destaddr) 1152 else O_STR(opt, "pidfile", pidfile) 1153 else O_YNO(opt, "hide-identity", hide_identity) 1154 else O_YNO(opt, "hide-version", hide_version) 1155 else O_YNO(opt, "hide-trustanchor", hide_trustanchor) 1156 else O_YNO(opt, "hide-http-user-agent", hide_http_user_agent) 1157 else O_STR(opt, "identity", identity) 1158 else O_STR(opt, "version", version) 1159 else O_STR(opt, "http-user-agent", http_user_agent) 1160 else O_STR(opt, "nsid", nsid_cfg_str) 1161 else O_STR(opt, "target-fetch-policy", target_fetch_policy) 1162 else O_YNO(opt, "harden-short-bufsize", harden_short_bufsize) 1163 else O_YNO(opt, "harden-large-queries", harden_large_queries) 1164 else O_YNO(opt, "harden-glue", harden_glue) 1165 else O_YNO(opt, "harden-dnssec-stripped", harden_dnssec_stripped) 1166 else O_YNO(opt, "harden-below-nxdomain", harden_below_nxdomain) 1167 else O_YNO(opt, "harden-referral-path", harden_referral_path) 1168 else O_YNO(opt, "harden-algo-downgrade", harden_algo_downgrade) 1169 else O_YNO(opt, "harden-unknown-additional", harden_unknown_additional) 1170 else O_YNO(opt, "use-caps-for-id", use_caps_bits_for_id) 1171 else O_LST(opt, "caps-whitelist", caps_whitelist) 1172 else O_DEC(opt, "unwanted-reply-threshold", unwanted_threshold) 1173 else O_YNO(opt, "do-not-query-localhost", donotquery_localhost) 1174 else O_STR(opt, "module-config", module_conf) 1175 else O_DEC(opt, "val-bogus-ttl", bogus_ttl) 1176 else O_YNO(opt, "val-clean-additional", val_clean_additional) 1177 else O_DEC(opt, "val-log-level", val_log_level) 1178 else O_YNO(opt, "val-permissive-mode", val_permissive_mode) 1179 else O_YNO(opt, "aggressive-nsec", aggressive_nsec) 1180 else O_YNO(opt, "ignore-cd-flag", ignore_cd) 1181 else O_YNO(opt, "disable-edns-do", disable_edns_do) 1182 else O_YNO(opt, "serve-expired", serve_expired) 1183 else O_DEC(opt, "serve-expired-ttl", serve_expired_ttl) 1184 else O_YNO(opt, "serve-expired-ttl-reset", serve_expired_ttl_reset) 1185 else O_DEC(opt, "serve-expired-reply-ttl", serve_expired_reply_ttl) 1186 else O_DEC(opt, "serve-expired-client-timeout", serve_expired_client_timeout) 1187 else O_YNO(opt, "ede", ede) 1188 else O_YNO(opt, "ede-serve-expired", ede_serve_expired) 1189 else O_YNO(opt, "serve-original-ttl", serve_original_ttl) 1190 else O_STR(opt, "val-nsec3-keysize-iterations",val_nsec3_key_iterations) 1191 else O_YNO(opt, "zonemd-permissive-mode", zonemd_permissive_mode) 1192 else O_UNS(opt, "add-holddown", add_holddown) 1193 else O_UNS(opt, "del-holddown", del_holddown) 1194 else O_UNS(opt, "keep-missing", keep_missing) 1195 else O_YNO(opt, "permit-small-holddown", permit_small_holddown) 1196 else O_MEM(opt, "key-cache-size", key_cache_size) 1197 else O_DEC(opt, "key-cache-slabs", key_cache_slabs) 1198 else O_MEM(opt, "neg-cache-size", neg_cache_size) 1199 else O_YNO(opt, "control-enable", remote_control_enable) 1200 else O_DEC(opt, "control-port", control_port) 1201 else O_STR(opt, "server-key-file", server_key_file) 1202 else O_STR(opt, "server-cert-file", server_cert_file) 1203 else O_STR(opt, "control-key-file", control_key_file) 1204 else O_STR(opt, "control-cert-file", control_cert_file) 1205 else O_LST(opt, "root-hints", root_hints) 1206 else O_LS2(opt, "access-control", acls) 1207 else O_LS2(opt, "tcp-connection-limit", tcp_connection_limits) 1208 else O_LST(opt, "do-not-query-address", donotqueryaddrs) 1209 else O_LST(opt, "private-address", private_address) 1210 else O_LST(opt, "private-domain", private_domain) 1211 else O_LST(opt, "auto-trust-anchor-file", auto_trust_anchor_file_list) 1212 else O_LST(opt, "trust-anchor-file", trust_anchor_file_list) 1213 else O_LST(opt, "trust-anchor", trust_anchor_list) 1214 else O_LST(opt, "trusted-keys-file", trusted_keys_file_list) 1215 else O_YNO(opt, "trust-anchor-signaling", trust_anchor_signaling) 1216 else O_YNO(opt, "root-key-sentinel", root_key_sentinel) 1217 else O_LST(opt, "control-interface", control_ifs.first) 1218 else O_LST(opt, "domain-insecure", domain_insecure) 1219 else O_UNS(opt, "val-override-date", val_date_override) 1220 else O_YNO(opt, "minimal-responses", minimal_responses) 1221 else O_YNO(opt, "rrset-roundrobin", rrset_roundrobin) 1222 else O_DEC(opt, "unknown-server-time-limit", unknown_server_time_limit) 1223 else O_DEC(opt, "discard-timeout", discard_timeout) 1224 else O_DEC(opt, "wait-limit", wait_limit) 1225 else O_DEC(opt, "wait-limit-cookie", wait_limit_cookie) 1226 else O_LS2(opt, "wait-limit-netblock", wait_limit_netblock) 1227 else O_LS2(opt, "wait-limit-cookie-netblock", wait_limit_cookie_netblock) 1228 #ifdef CLIENT_SUBNET 1229 else O_LST(opt, "send-client-subnet", client_subnet) 1230 else O_LST(opt, "client-subnet-zone", client_subnet_zone) 1231 else O_DEC(opt, "max-client-subnet-ipv4", max_client_subnet_ipv4) 1232 else O_DEC(opt, "max-client-subnet-ipv6", max_client_subnet_ipv6) 1233 else O_DEC(opt, "min-client-subnet-ipv4", min_client_subnet_ipv4) 1234 else O_DEC(opt, "min-client-subnet-ipv6", min_client_subnet_ipv6) 1235 else O_DEC(opt, "max-ecs-tree-size-ipv4", max_ecs_tree_size_ipv4) 1236 else O_DEC(opt, "max-ecs-tree-size-ipv6", max_ecs_tree_size_ipv6) 1237 else O_YNO(opt, "client-subnet-always-forward:", 1238 client_subnet_always_forward) 1239 #endif 1240 #ifdef USE_DNSTAP 1241 else O_YNO(opt, "dnstap-enable", dnstap) 1242 else O_YNO(opt, "dnstap-bidirectional", dnstap_bidirectional) 1243 else O_STR(opt, "dnstap-socket-path", dnstap_socket_path) 1244 else O_STR(opt, "dnstap-ip", dnstap_ip) 1245 else O_YNO(opt, "dnstap-tls", dnstap_tls) 1246 else O_STR(opt, "dnstap-tls-server-name", dnstap_tls_server_name) 1247 else O_STR(opt, "dnstap-tls-cert-bundle", dnstap_tls_cert_bundle) 1248 else O_STR(opt, "dnstap-tls-client-key-file", 1249 dnstap_tls_client_key_file) 1250 else O_STR(opt, "dnstap-tls-client-cert-file", 1251 dnstap_tls_client_cert_file) 1252 else O_YNO(opt, "dnstap-send-identity", dnstap_send_identity) 1253 else O_YNO(opt, "dnstap-send-version", dnstap_send_version) 1254 else O_STR(opt, "dnstap-identity", dnstap_identity) 1255 else O_STR(opt, "dnstap-version", dnstap_version) 1256 else O_UNS(opt, "dnstap-sample-rate", dnstap_sample_rate) 1257 else O_YNO(opt, "dnstap-log-resolver-query-messages", 1258 dnstap_log_resolver_query_messages) 1259 else O_YNO(opt, "dnstap-log-resolver-response-messages", 1260 dnstap_log_resolver_response_messages) 1261 else O_YNO(opt, "dnstap-log-client-query-messages", 1262 dnstap_log_client_query_messages) 1263 else O_YNO(opt, "dnstap-log-client-response-messages", 1264 dnstap_log_client_response_messages) 1265 else O_YNO(opt, "dnstap-log-forwarder-query-messages", 1266 dnstap_log_forwarder_query_messages) 1267 else O_YNO(opt, "dnstap-log-forwarder-response-messages", 1268 dnstap_log_forwarder_response_messages) 1269 #endif 1270 #ifdef USE_DNSCRYPT 1271 else O_YNO(opt, "dnscrypt-enable", dnscrypt) 1272 else O_DEC(opt, "dnscrypt-port", dnscrypt_port) 1273 else O_STR(opt, "dnscrypt-provider", dnscrypt_provider) 1274 else O_LST(opt, "dnscrypt-provider-cert", dnscrypt_provider_cert) 1275 else O_LST(opt, "dnscrypt-provider-cert-rotated", dnscrypt_provider_cert_rotated) 1276 else O_LST(opt, "dnscrypt-secret-key", dnscrypt_secret_key) 1277 else O_MEM(opt, "dnscrypt-shared-secret-cache-size", 1278 dnscrypt_shared_secret_cache_size) 1279 else O_DEC(opt, "dnscrypt-shared-secret-cache-slabs", 1280 dnscrypt_shared_secret_cache_slabs) 1281 else O_MEM(opt, "dnscrypt-nonce-cache-size", 1282 dnscrypt_nonce_cache_size) 1283 else O_DEC(opt, "dnscrypt-nonce-cache-slabs", 1284 dnscrypt_nonce_cache_slabs) 1285 #endif 1286 else O_YNO(opt, "unblock-lan-zones", unblock_lan_zones) 1287 else O_YNO(opt, "insecure-lan-zones", insecure_lan_zones) 1288 else O_DEC(opt, "max-udp-size", max_udp_size) 1289 else O_LST(opt, "python-script", python_script) 1290 else O_LST(opt, "dynlib-file", dynlib_file) 1291 else O_YNO(opt, "disable-dnssec-lame-check", disable_dnssec_lame_check) 1292 else O_DEC(opt, "ip-ratelimit-cookie", ip_ratelimit_cookie) 1293 else O_DEC(opt, "ip-ratelimit", ip_ratelimit) 1294 else O_DEC(opt, "ratelimit", ratelimit) 1295 else O_MEM(opt, "ip-ratelimit-size", ip_ratelimit_size) 1296 else O_MEM(opt, "ratelimit-size", ratelimit_size) 1297 else O_DEC(opt, "ip-ratelimit-slabs", ip_ratelimit_slabs) 1298 else O_DEC(opt, "ratelimit-slabs", ratelimit_slabs) 1299 else O_LS2(opt, "ratelimit-for-domain", ratelimit_for_domain) 1300 else O_LS2(opt, "ratelimit-below-domain", ratelimit_below_domain) 1301 else O_DEC(opt, "ip-ratelimit-factor", ip_ratelimit_factor) 1302 else O_DEC(opt, "ratelimit-factor", ratelimit_factor) 1303 else O_YNO(opt, "ip-ratelimit-backoff", ip_ratelimit_backoff) 1304 else O_YNO(opt, "ratelimit-backoff", ratelimit_backoff) 1305 else O_UNS(opt, "outbound-msg-retry", outbound_msg_retry) 1306 else O_UNS(opt, "max-sent-count", max_sent_count) 1307 else O_UNS(opt, "max-query-restarts", max_query_restarts) 1308 else O_DEC(opt, "fast-server-num", fast_server_num) 1309 else O_DEC(opt, "fast-server-permil", fast_server_permil) 1310 else O_DEC(opt, "val-sig-skew-min", val_sig_skew_min) 1311 else O_DEC(opt, "val-sig-skew-max", val_sig_skew_max) 1312 else O_DEC(opt, "val-max-restart", val_max_restart) 1313 else O_YNO(opt, "qname-minimisation", qname_minimisation) 1314 else O_YNO(opt, "qname-minimisation-strict", qname_minimisation_strict) 1315 else O_IFC(opt, "define-tag", num_tags, tagname) 1316 else O_LTG(opt, "local-zone-tag", local_zone_tags) 1317 else O_LTG(opt, "access-control-tag", acl_tags) 1318 else O_LTG(opt, "response-ip-tag", respip_tags) 1319 else O_LS3(opt, "local-zone-override", local_zone_overrides) 1320 else O_LS3(opt, "access-control-tag-action", acl_tag_actions) 1321 else O_LS3(opt, "access-control-tag-data", acl_tag_datas) 1322 else O_LS2(opt, "access-control-view", acl_view) 1323 else O_LS2(opt, "interface-action", interface_actions) 1324 else O_LTG(opt, "interface-tag", interface_tags) 1325 else O_LS3(opt, "interface-tag-action", interface_tag_actions) 1326 else O_LS3(opt, "interface-tag-data", interface_tag_datas) 1327 else O_LS2(opt, "interface-view", interface_view) 1328 else O_YNO(opt, "pad-responses", pad_responses) 1329 else O_DEC(opt, "pad-responses-block-size", pad_responses_block_size) 1330 else O_YNO(opt, "pad-queries", pad_queries) 1331 else O_DEC(opt, "pad-queries-block-size", pad_queries_block_size) 1332 else O_LS2(opt, "edns-client-strings", edns_client_strings) 1333 else O_LST(opt, "proxy-protocol-port", proxy_protocol_port) 1334 #ifdef USE_IPSECMOD 1335 else O_YNO(opt, "ipsecmod-enabled", ipsecmod_enabled) 1336 else O_YNO(opt, "ipsecmod-ignore-bogus", ipsecmod_ignore_bogus) 1337 else O_STR(opt, "ipsecmod-hook", ipsecmod_hook) 1338 else O_DEC(opt, "ipsecmod-max-ttl", ipsecmod_max_ttl) 1339 else O_LST(opt, "ipsecmod-whitelist", ipsecmod_whitelist) 1340 else O_YNO(opt, "ipsecmod-strict", ipsecmod_strict) 1341 #endif 1342 else O_YNO(opt, "answer-cookie", do_answer_cookie) 1343 else O_STR(opt, "cookie-secret-file", cookie_secret_file) 1344 #ifdef USE_CACHEDB 1345 else O_STR(opt, "backend", cachedb_backend) 1346 else O_STR(opt, "secret-seed", cachedb_secret) 1347 else O_YNO(opt, "cachedb-no-store", cachedb_no_store) 1348 else O_YNO(opt, "cachedb-check-when-serve-expired", cachedb_check_when_serve_expired) 1349 #ifdef USE_REDIS 1350 else O_STR(opt, "redis-server-host", redis_server_host) 1351 else O_DEC(opt, "redis-server-port", redis_server_port) 1352 else O_STR(opt, "redis-server-path", redis_server_path) 1353 else O_STR(opt, "redis-server-password", redis_server_password) 1354 else O_DEC(opt, "redis-timeout", redis_timeout) 1355 else O_YNO(opt, "redis-expire-records", redis_expire_records) 1356 else O_DEC(opt, "redis-logical-db", redis_logical_db) 1357 #endif /* USE_REDIS */ 1358 #endif /* USE_CACHEDB */ 1359 #ifdef USE_IPSET 1360 else O_STR(opt, "name-v4", ipset_name_v4) 1361 else O_STR(opt, "name-v6", ipset_name_v6) 1362 #endif 1363 /* not here: 1364 * outgoing-permit, outgoing-avoid - have list of ports 1365 * local-zone - zones and nodefault variables 1366 * local-data - see below 1367 * local-data-ptr - converted to local-data entries 1368 * stub-zone, name, stub-addr, stub-host, stub-prime 1369 * forward-zone, name, forward-addr, forward-host 1370 */ 1371 else return 0; 1372 return 1; 1373 } 1374 1375 /** initialize the global cfg_parser object */ 1376 static void 1377 create_cfg_parser(struct config_file* cfg, char* filename, const char* chroot) 1378 { 1379 static struct config_parser_state st; 1380 cfg_parser = &st; 1381 cfg_parser->filename = filename; 1382 cfg_parser->line = 1; 1383 cfg_parser->errors = 0; 1384 cfg_parser->cfg = cfg; 1385 cfg_parser->chroot = chroot; 1386 cfg_parser->started_toplevel = 0; 1387 init_cfg_parse(); 1388 } 1389 1390 int 1391 config_read(struct config_file* cfg, const char* filename, const char* chroot) 1392 { 1393 FILE *in; 1394 char *fname = (char*)filename; 1395 #ifdef HAVE_GLOB 1396 glob_t g; 1397 size_t i; 1398 int r, flags; 1399 #endif 1400 if(!fname) 1401 return 1; 1402 1403 /* check for wildcards */ 1404 #ifdef HAVE_GLOB 1405 if(!(!strchr(fname, '*') && !strchr(fname, '?') && !strchr(fname, '[') && 1406 !strchr(fname, '{') && !strchr(fname, '~'))) { 1407 verbose(VERB_QUERY, "wildcard found, processing %s", fname); 1408 flags = 0 1409 #ifdef GLOB_ERR 1410 | GLOB_ERR 1411 #endif 1412 #ifdef GLOB_NOSORT 1413 | GLOB_NOSORT 1414 #endif 1415 #ifdef GLOB_BRACE 1416 | GLOB_BRACE 1417 #endif 1418 #ifdef GLOB_TILDE 1419 | GLOB_TILDE 1420 #endif 1421 ; 1422 memset(&g, 0, sizeof(g)); 1423 r = glob(fname, flags, NULL, &g); 1424 if(r) { 1425 /* some error */ 1426 globfree(&g); 1427 if(r == GLOB_NOMATCH) { 1428 verbose(VERB_QUERY, "include: " 1429 "no matches for %s", fname); 1430 return 1; 1431 } else if(r == GLOB_NOSPACE) { 1432 log_err("include: %s: " 1433 "fnametern out of memory", fname); 1434 } else if(r == GLOB_ABORTED) { 1435 log_err("wildcard include: %s: expansion " 1436 "aborted (%s)", fname, strerror(errno)); 1437 } else { 1438 log_err("wildcard include: %s: expansion " 1439 "failed (%s)", fname, strerror(errno)); 1440 } 1441 /* ignore globs that yield no files */ 1442 return 1; 1443 } 1444 /* process files found, if any */ 1445 for(i=0; i<(size_t)g.gl_pathc; i++) { 1446 if(!config_read(cfg, g.gl_pathv[i], chroot)) { 1447 log_err("error reading wildcard " 1448 "include: %s", g.gl_pathv[i]); 1449 globfree(&g); 1450 return 0; 1451 } 1452 } 1453 globfree(&g); 1454 return 1; 1455 } 1456 #endif /* HAVE_GLOB */ 1457 1458 in = fopen(fname, "r"); 1459 if(!in) { 1460 log_err("Could not open %s: %s", fname, strerror(errno)); 1461 return 0; 1462 } 1463 create_cfg_parser(cfg, fname, chroot); 1464 ub_c_in = in; 1465 ub_c_parse(); 1466 fclose(in); 1467 1468 if(!cfg->dnscrypt) cfg->dnscrypt_port = 0; 1469 1470 if(cfg_parser->errors != 0) { 1471 fprintf(stderr, "read %s failed: %d errors in configuration file\n", 1472 fname, cfg_parser->errors); 1473 errno=EINVAL; 1474 return 0; 1475 } 1476 1477 return 1; 1478 } 1479 1480 struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm) 1481 { 1482 struct config_stub* p = *(*pp); 1483 while(p) { 1484 if(strcmp(p->name, nm) == 0) 1485 return p; 1486 (*pp) = &p->next; 1487 p = p->next; 1488 } 1489 return NULL; 1490 } 1491 1492 void 1493 config_delstrlist(struct config_strlist* p) 1494 { 1495 struct config_strlist *np; 1496 while(p) { 1497 np = p->next; 1498 free(p->str); 1499 free(p); 1500 p = np; 1501 } 1502 } 1503 1504 void 1505 config_deldblstrlist(struct config_str2list* p) 1506 { 1507 struct config_str2list *np; 1508 while(p) { 1509 np = p->next; 1510 free(p->str); 1511 free(p->str2); 1512 free(p); 1513 p = np; 1514 } 1515 } 1516 1517 void 1518 config_deltrplstrlist(struct config_str3list* p) 1519 { 1520 struct config_str3list *np; 1521 while(p) { 1522 np = p->next; 1523 free(p->str); 1524 free(p->str2); 1525 free(p->str3); 1526 free(p); 1527 p = np; 1528 } 1529 } 1530 1531 void 1532 config_delauth(struct config_auth* p) 1533 { 1534 if(!p) return; 1535 free(p->name); 1536 config_delstrlist(p->masters); 1537 config_delstrlist(p->urls); 1538 config_delstrlist(p->allow_notify); 1539 free(p->zonefile); 1540 free(p->rpz_taglist); 1541 free(p->rpz_action_override); 1542 free(p->rpz_cname); 1543 free(p->rpz_log_name); 1544 free(p); 1545 } 1546 1547 void 1548 config_delauths(struct config_auth* p) 1549 { 1550 struct config_auth* np; 1551 while(p) { 1552 np = p->next; 1553 config_delauth(p); 1554 p = np; 1555 } 1556 } 1557 1558 void 1559 config_delstub(struct config_stub* p) 1560 { 1561 if(!p) return; 1562 free(p->name); 1563 config_delstrlist(p->hosts); 1564 config_delstrlist(p->addrs); 1565 free(p); 1566 } 1567 1568 void 1569 config_delstubs(struct config_stub* p) 1570 { 1571 struct config_stub* np; 1572 while(p) { 1573 np = p->next; 1574 config_delstub(p); 1575 p = np; 1576 } 1577 } 1578 1579 void 1580 config_delview(struct config_view* p) 1581 { 1582 if(!p) return; 1583 free(p->name); 1584 config_deldblstrlist(p->local_zones); 1585 config_delstrlist(p->local_zones_nodefault); 1586 #ifdef USE_IPSET 1587 config_delstrlist(p->local_zones_ipset); 1588 #endif 1589 config_delstrlist(p->local_data); 1590 free(p); 1591 } 1592 1593 void 1594 config_delviews(struct config_view* p) 1595 { 1596 struct config_view* np; 1597 while(p) { 1598 np = p->next; 1599 config_delview(p); 1600 p = np; 1601 } 1602 } 1603 1604 void 1605 config_del_strarray(char** array, int num) 1606 { 1607 int i; 1608 if(!array) 1609 return; 1610 for(i=0; i<num; i++) { 1611 free(array[i]); 1612 } 1613 free(array); 1614 } 1615 1616 void 1617 config_del_strbytelist(struct config_strbytelist* p) 1618 { 1619 struct config_strbytelist* np; 1620 while(p) { 1621 np = p->next; 1622 free(p->str); 1623 free(p->str2); 1624 free(p); 1625 p = np; 1626 } 1627 } 1628 1629 void 1630 config_delete(struct config_file* cfg) 1631 { 1632 if(!cfg) return; 1633 free(cfg->username); 1634 free(cfg->chrootdir); 1635 free(cfg->directory); 1636 free(cfg->logfile); 1637 free(cfg->pidfile); 1638 free(cfg->if_automatic_ports); 1639 free(cfg->target_fetch_policy); 1640 free(cfg->ssl_service_key); 1641 free(cfg->ssl_service_pem); 1642 free(cfg->tls_cert_bundle); 1643 config_delstrlist(cfg->tls_additional_port); 1644 config_delstrlist(cfg->tls_session_ticket_keys.first); 1645 free(cfg->tls_ciphers); 1646 free(cfg->tls_ciphersuites); 1647 free(cfg->http_endpoint); 1648 if(cfg->log_identity) { 1649 log_ident_revert_to_default(); 1650 free(cfg->log_identity); 1651 } 1652 config_del_strarray(cfg->ifs, cfg->num_ifs); 1653 config_del_strarray(cfg->out_ifs, cfg->num_out_ifs); 1654 config_delstubs(cfg->stubs); 1655 config_delstubs(cfg->forwards); 1656 config_delauths(cfg->auths); 1657 config_delviews(cfg->views); 1658 config_delstrlist(cfg->donotqueryaddrs); 1659 config_delstrlist(cfg->root_hints); 1660 #ifdef CLIENT_SUBNET 1661 config_delstrlist(cfg->client_subnet); 1662 config_delstrlist(cfg->client_subnet_zone); 1663 #endif 1664 free(cfg->identity); 1665 free(cfg->version); 1666 free(cfg->http_user_agent); 1667 free(cfg->nsid_cfg_str); 1668 free(cfg->nsid); 1669 free(cfg->module_conf); 1670 free(cfg->outgoing_avail_ports); 1671 config_delstrlist(cfg->caps_whitelist); 1672 config_delstrlist(cfg->private_address); 1673 config_delstrlist(cfg->private_domain); 1674 config_delstrlist(cfg->auto_trust_anchor_file_list); 1675 config_delstrlist(cfg->trust_anchor_file_list); 1676 config_delstrlist(cfg->trusted_keys_file_list); 1677 config_delstrlist(cfg->trust_anchor_list); 1678 config_delstrlist(cfg->domain_insecure); 1679 config_deldblstrlist(cfg->acls); 1680 config_deldblstrlist(cfg->tcp_connection_limits); 1681 free(cfg->val_nsec3_key_iterations); 1682 config_deldblstrlist(cfg->local_zones); 1683 config_delstrlist(cfg->local_zones_nodefault); 1684 #ifdef USE_IPSET 1685 config_delstrlist(cfg->local_zones_ipset); 1686 #endif 1687 config_delstrlist(cfg->local_data); 1688 config_deltrplstrlist(cfg->local_zone_overrides); 1689 config_del_strarray(cfg->tagname, cfg->num_tags); 1690 config_del_strbytelist(cfg->local_zone_tags); 1691 config_del_strbytelist(cfg->respip_tags); 1692 config_deldblstrlist(cfg->acl_view); 1693 config_del_strbytelist(cfg->acl_tags); 1694 config_deltrplstrlist(cfg->acl_tag_actions); 1695 config_deltrplstrlist(cfg->acl_tag_datas); 1696 config_deldblstrlist(cfg->interface_actions); 1697 config_deldblstrlist(cfg->interface_view); 1698 config_del_strbytelist(cfg->interface_tags); 1699 config_deltrplstrlist(cfg->interface_tag_actions); 1700 config_deltrplstrlist(cfg->interface_tag_datas); 1701 config_delstrlist(cfg->control_ifs.first); 1702 config_deldblstrlist(cfg->wait_limit_netblock); 1703 config_deldblstrlist(cfg->wait_limit_cookie_netblock); 1704 free(cfg->server_key_file); 1705 free(cfg->server_cert_file); 1706 free(cfg->control_key_file); 1707 free(cfg->control_cert_file); 1708 free(cfg->nat64_prefix); 1709 free(cfg->dns64_prefix); 1710 config_delstrlist(cfg->dns64_ignore_aaaa); 1711 free(cfg->dnstap_socket_path); 1712 free(cfg->dnstap_ip); 1713 free(cfg->dnstap_tls_server_name); 1714 free(cfg->dnstap_tls_cert_bundle); 1715 free(cfg->dnstap_tls_client_key_file); 1716 free(cfg->dnstap_tls_client_cert_file); 1717 free(cfg->dnstap_identity); 1718 free(cfg->dnstap_version); 1719 config_deldblstrlist(cfg->ratelimit_for_domain); 1720 config_deldblstrlist(cfg->ratelimit_below_domain); 1721 config_delstrlist(cfg->python_script); 1722 config_delstrlist(cfg->dynlib_file); 1723 config_deldblstrlist(cfg->edns_client_strings); 1724 config_delstrlist(cfg->proxy_protocol_port); 1725 #ifdef USE_IPSECMOD 1726 free(cfg->ipsecmod_hook); 1727 config_delstrlist(cfg->ipsecmod_whitelist); 1728 #endif 1729 free(cfg->cookie_secret_file); 1730 #ifdef USE_CACHEDB 1731 free(cfg->cachedb_backend); 1732 free(cfg->cachedb_secret); 1733 #ifdef USE_REDIS 1734 free(cfg->redis_server_host); 1735 free(cfg->redis_server_path); 1736 free(cfg->redis_server_password); 1737 #endif /* USE_REDIS */ 1738 #endif /* USE_CACHEDB */ 1739 #ifdef USE_IPSET 1740 free(cfg->ipset_name_v4); 1741 free(cfg->ipset_name_v6); 1742 #endif 1743 free(cfg); 1744 } 1745 1746 static void 1747 init_cookie_secret(uint8_t* cookie_secret, size_t cookie_secret_len) 1748 { 1749 struct ub_randstate *rand = ub_initstate(NULL); 1750 1751 if (!rand) 1752 fatal_exit("could not init random generator"); 1753 while (cookie_secret_len) { 1754 *cookie_secret++ = (uint8_t)ub_random(rand); 1755 cookie_secret_len--; 1756 } 1757 ub_randfree(rand); 1758 } 1759 1760 static void 1761 init_outgoing_availports(int* a, int num) 1762 { 1763 /* generated with make iana_update */ 1764 const int iana_assigned[] = { 1765 #include "util/iana_ports.inc" 1766 -1 }; /* end marker to put behind trailing comma */ 1767 1768 int i; 1769 /* do not use <1024, that could be trouble with the system, privs */ 1770 for(i=1024; i<num; i++) { 1771 a[i] = i; 1772 } 1773 /* create empty spot at 49152 to keep ephemeral ports available 1774 * to other programs */ 1775 for(i=49152; i<49152+256; i++) 1776 a[i] = 0; 1777 /* pick out all the IANA assigned ports */ 1778 for(i=0; iana_assigned[i]!=-1; i++) { 1779 if(iana_assigned[i] < num) 1780 a[iana_assigned[i]] = 0; 1781 } 1782 } 1783 1784 static int 1785 extract_port_from_str(const char* str, int max_port) { 1786 char* endptr; 1787 long int value; 1788 if (str == NULL || *str == '\0') { 1789 log_err("str: '%s' is invalid", (str?str:"NULL")); 1790 return -1; 1791 } 1792 1793 value = strtol(str, &endptr, 10); 1794 if ((endptr == str) || (*endptr != '\0')) { 1795 log_err("cannot parse port number '%s'", str); 1796 return -1; 1797 } 1798 1799 if (errno == ERANGE) { 1800 log_err("overflow occurred when parsing '%s'", str); 1801 return -1; 1802 } 1803 1804 if (value == 0 && strcmp(str, "0") != 0) { 1805 log_err("cannot parse port number '%s'", str); 1806 return -1; 1807 } 1808 1809 if (value < 0 || value >= max_port) { 1810 log_err(" '%s' is out of bounds [0, %d)", str, max_port); 1811 return -1; 1812 } 1813 1814 return (int)value; 1815 } 1816 1817 int 1818 cfg_mark_ports(const char* str, int allow, int* avail, int num) 1819 { 1820 char* mid = strchr(str, '-'); 1821 #ifdef DISABLE_EXPLICIT_PORT_RANDOMISATION 1822 log_warn("Explicit port randomisation disabled, ignoring " 1823 "outgoing-port-permit and outgoing-port-avoid configuration " 1824 "options"); 1825 #endif 1826 if(!mid) { 1827 int port = extract_port_from_str(str, num); 1828 if (port < 0) { 1829 log_err("Failed to parse the port number"); 1830 return 0; 1831 } 1832 if(port < num) 1833 avail[port] = (allow?port:0); 1834 } else { 1835 char buf[16]; 1836 int i, low; 1837 int high = extract_port_from_str(mid+1, num); 1838 if (high < 0) { 1839 log_err("Failed to parse the port number"); 1840 return 0; 1841 } 1842 1843 if( (int)(mid-str)+1 >= (int)sizeof(buf) ) { 1844 log_err("cannot parse port number '%s'", str); 1845 return 0; 1846 } 1847 1848 if(mid > str) 1849 memcpy(buf, str, (size_t)(mid-str)); 1850 buf[mid-str] = 0; 1851 low = extract_port_from_str(buf, num); 1852 if (low < 0) { 1853 log_err("Failed to parse the port number"); 1854 return 0; 1855 } 1856 1857 if (low > high) { 1858 log_err("Low value is greater than high value"); 1859 return 0; 1860 } 1861 1862 for(i=low; i<=high; i++) { 1863 if(i < num) 1864 avail[i] = (allow?i:0); 1865 } 1866 } 1867 return 1; 1868 } 1869 1870 int 1871 cfg_scan_ports(int* avail, int num) 1872 { 1873 int i; 1874 int count = 0; 1875 for(i=0; i<num; i++) { 1876 if(avail[i]) 1877 count++; 1878 } 1879 return count; 1880 } 1881 1882 int cfg_condense_ports(struct config_file* cfg, int** avail) 1883 { 1884 int num = cfg_scan_ports(cfg->outgoing_avail_ports, 65536); 1885 int i, at = 0; 1886 *avail = NULL; 1887 if(num == 0) 1888 return 0; 1889 *avail = (int*)reallocarray(NULL, (size_t)num, sizeof(int)); 1890 if(!*avail) 1891 return 0; 1892 for(i=0; i<65536; i++) { 1893 if(cfg->outgoing_avail_ports[i]) 1894 (*avail)[at++] = cfg->outgoing_avail_ports[i]; 1895 } 1896 log_assert(at == num); 1897 return num; 1898 } 1899 1900 void cfg_apply_local_port_policy(struct config_file* cfg, int num) { 1901 (void)cfg; 1902 (void)num; 1903 #ifdef USE_LINUX_IP_LOCAL_PORT_RANGE 1904 { 1905 int i = 0; 1906 FILE* range_fd; 1907 if ((range_fd = fopen(LINUX_IP_LOCAL_PORT_RANGE_PATH, "r")) != NULL) { 1908 int min_port = 0; 1909 int max_port = num - 1; 1910 if (fscanf(range_fd, "%d %d", &min_port, &max_port) == 2) { 1911 for(i=0; i<min_port; i++) { 1912 cfg->outgoing_avail_ports[i] = 0; 1913 } 1914 for(i=max_port+1; i<num; i++) { 1915 cfg->outgoing_avail_ports[i] = 0; 1916 } 1917 } else { 1918 log_err("unexpected port range in %s", 1919 LINUX_IP_LOCAL_PORT_RANGE_PATH); 1920 } 1921 fclose(range_fd); 1922 } else { 1923 log_err("failed to read from file: %s (%s)", 1924 LINUX_IP_LOCAL_PORT_RANGE_PATH, 1925 strerror(errno)); 1926 } 1927 } 1928 #endif 1929 } 1930 1931 /** print error with file and line number */ 1932 static void ub_c_error_va_list(const char *fmt, va_list args) 1933 { 1934 cfg_parser->errors++; 1935 fprintf(stderr, "%s:%d: error: ", cfg_parser->filename, 1936 cfg_parser->line); 1937 vfprintf(stderr, fmt, args); 1938 fprintf(stderr, "\n"); 1939 } 1940 1941 /** print error with file and line number */ 1942 void ub_c_error_msg(const char* fmt, ...) 1943 { 1944 va_list args; 1945 va_start(args, fmt); 1946 ub_c_error_va_list(fmt, args); 1947 va_end(args); 1948 } 1949 1950 void ub_c_error(const char *str) 1951 { 1952 cfg_parser->errors++; 1953 if(strcmp(str, "syntax error")==0 && cfg_parser->started_toplevel ==0) 1954 str = "syntax error, is there no section start after an " 1955 "include-toplevel directive perhaps."; 1956 fprintf(stderr, "%s:%d: error: %s\n", cfg_parser->filename, 1957 cfg_parser->line, str); 1958 } 1959 1960 int ub_c_wrap(void) 1961 { 1962 return 1; 1963 } 1964 1965 int cfg_strlist_append(struct config_strlist_head* list, char* item) 1966 { 1967 struct config_strlist *s; 1968 if(!item || !list) { 1969 free(item); 1970 return 0; 1971 } 1972 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 1973 if(!s) { 1974 free(item); 1975 return 0; 1976 } 1977 s->str = item; 1978 s->next = NULL; 1979 if(list->last) 1980 list->last->next = s; 1981 else 1982 list->first = s; 1983 list->last = s; 1984 return 1; 1985 } 1986 1987 int 1988 cfg_region_strlist_insert(struct regional* region, 1989 struct config_strlist** head, char* item) 1990 { 1991 struct config_strlist *s; 1992 if(!item || !head) 1993 return 0; 1994 s = (struct config_strlist*)regional_alloc_zero(region, 1995 sizeof(struct config_strlist)); 1996 if(!s) 1997 return 0; 1998 s->str = item; 1999 s->next = *head; 2000 *head = s; 2001 return 1; 2002 } 2003 2004 struct config_strlist* 2005 cfg_strlist_find(struct config_strlist* head, const char *item) 2006 { 2007 struct config_strlist *s = head; 2008 if(!head){ 2009 return NULL; 2010 } 2011 while(s) { 2012 if(strcmp(s->str, item) == 0) { 2013 return s; 2014 } 2015 s = s->next; 2016 } 2017 return NULL; 2018 } 2019 2020 int 2021 cfg_strlist_insert(struct config_strlist** head, char* item) 2022 { 2023 struct config_strlist *s; 2024 if(!item || !head) { 2025 free(item); 2026 return 0; 2027 } 2028 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 2029 if(!s) { 2030 free(item); 2031 return 0; 2032 } 2033 s->str = item; 2034 s->next = *head; 2035 *head = s; 2036 return 1; 2037 } 2038 2039 int 2040 cfg_strlist_append_ex(struct config_strlist** head, char* item) 2041 { 2042 struct config_strlist *s; 2043 if(!item || !head) 2044 return 0; 2045 s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); 2046 if(!s) 2047 return 0; 2048 s->str = item; 2049 s->next = NULL; 2050 2051 if (*head==NULL) { 2052 *head = s; 2053 } else { 2054 struct config_strlist *last = *head; 2055 while (last->next!=NULL) { 2056 last = last->next; 2057 } 2058 last->next = s; 2059 } 2060 2061 return 1; 2062 } 2063 2064 int 2065 cfg_str2list_insert(struct config_str2list** head, char* item, char* i2) 2066 { 2067 struct config_str2list *s; 2068 if(!item || !i2 || !head) { 2069 free(item); 2070 free(i2); 2071 return 0; 2072 } 2073 s = (struct config_str2list*)calloc(1, sizeof(struct config_str2list)); 2074 if(!s) { 2075 free(item); 2076 free(i2); 2077 return 0; 2078 } 2079 s->str = item; 2080 s->str2 = i2; 2081 s->next = *head; 2082 *head = s; 2083 return 1; 2084 } 2085 2086 int 2087 cfg_str3list_insert(struct config_str3list** head, char* item, char* i2, 2088 char* i3) 2089 { 2090 struct config_str3list *s; 2091 if(!item || !i2 || !i3 || !head) 2092 return 0; 2093 s = (struct config_str3list*)calloc(1, sizeof(struct config_str3list)); 2094 if(!s) 2095 return 0; 2096 s->str = item; 2097 s->str2 = i2; 2098 s->str3 = i3; 2099 s->next = *head; 2100 *head = s; 2101 return 1; 2102 } 2103 2104 int 2105 cfg_strbytelist_insert(struct config_strbytelist** head, char* item, 2106 uint8_t* i2, size_t i2len) 2107 { 2108 struct config_strbytelist* s; 2109 if(!item || !i2 || !head) 2110 return 0; 2111 s = (struct config_strbytelist*)calloc(1, sizeof(*s)); 2112 if(!s) 2113 return 0; 2114 s->str = item; 2115 s->str2 = i2; 2116 s->str2len = i2len; 2117 s->next = *head; 2118 *head = s; 2119 return 1; 2120 } 2121 2122 time_t 2123 cfg_convert_timeval(const char* str) 2124 { 2125 time_t t; 2126 struct tm tm; 2127 memset(&tm, 0, sizeof(tm)); 2128 if(strlen(str) < 14) 2129 return 0; 2130 if(sscanf(str, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, 2131 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) 2132 return 0; 2133 tm.tm_year -= 1900; 2134 tm.tm_mon--; 2135 /* Check values */ 2136 if (tm.tm_year < 70) return 0; 2137 if (tm.tm_mon < 0 || tm.tm_mon > 11) return 0; 2138 if (tm.tm_mday < 1 || tm.tm_mday > 31) return 0; 2139 if (tm.tm_hour < 0 || tm.tm_hour > 23) return 0; 2140 if (tm.tm_min < 0 || tm.tm_min > 59) return 0; 2141 if (tm.tm_sec < 0 || tm.tm_sec > 59) return 0; 2142 /* call ldns conversion function */ 2143 t = sldns_mktime_from_utc(&tm); 2144 return t; 2145 } 2146 2147 int 2148 cfg_count_numbers(const char* s) 2149 { 2150 /* format ::= (sp num)+ sp */ 2151 /* num ::= [-](0-9)+ */ 2152 /* sp ::= (space|tab)* */ 2153 int num = 0; 2154 while(*s) { 2155 while(*s && isspace((unsigned char)*s)) 2156 s++; 2157 if(!*s) /* end of string */ 2158 break; 2159 if(*s == '-') 2160 s++; 2161 if(!*s) /* only - not allowed */ 2162 return 0; 2163 if(!isdigit((unsigned char)*s)) /* bad character */ 2164 return 0; 2165 while(*s && isdigit((unsigned char)*s)) 2166 s++; 2167 num++; 2168 } 2169 return num; 2170 } 2171 2172 /** all digit number */ 2173 static int isalldigit(const char* str, size_t l) 2174 { 2175 size_t i; 2176 for(i=0; i<l; i++) 2177 if(!isdigit((unsigned char)str[i])) 2178 return 0; 2179 return 1; 2180 } 2181 2182 int 2183 cfg_parse_memsize(const char* str, size_t* res) 2184 { 2185 size_t len; 2186 size_t mult = 1; 2187 if(!str || (len=(size_t)strlen(str)) == 0) { 2188 log_err("not a size: '%s'", str); 2189 return 0; 2190 } 2191 if(isalldigit(str, len)) { 2192 *res = (size_t)atol(str); 2193 return 1; 2194 } 2195 /* check appended num */ 2196 while(len>0 && str[len-1]==' ') 2197 len--; 2198 if(len > 1 && str[len-1] == 'b') 2199 len--; 2200 else if(len > 1 && str[len-1] == 'B') 2201 len--; 2202 2203 if(len > 1 && tolower((unsigned char)str[len-1]) == 'g') 2204 mult = 1024*1024*1024; 2205 else if(len > 1 && tolower((unsigned char)str[len-1]) == 'm') 2206 mult = 1024*1024; 2207 else if(len > 1 && tolower((unsigned char)str[len-1]) == 'k') 2208 mult = 1024; 2209 else if(len > 0 && isdigit((unsigned char)str[len-1])) 2210 mult = 1; 2211 else { 2212 log_err("unknown size specifier: '%s'", str); 2213 return 0; 2214 } 2215 while(len>1 && str[len-2]==' ') 2216 len--; 2217 2218 if(!isalldigit(str, len-1)) { 2219 log_err("unknown size specifier: '%s'", str); 2220 return 0; 2221 } 2222 *res = ((size_t)atol(str)) * mult; 2223 return 1; 2224 } 2225 2226 int 2227 find_tag_id(struct config_file* cfg, const char* tag) 2228 { 2229 int i; 2230 for(i=0; i<cfg->num_tags; i++) { 2231 if(strcmp(cfg->tagname[i], tag) == 0) 2232 return i; 2233 } 2234 return -1; 2235 } 2236 2237 int 2238 config_add_tag(struct config_file* cfg, const char* tag) 2239 { 2240 char** newarray; 2241 char* newtag; 2242 if(find_tag_id(cfg, tag) != -1) 2243 return 1; /* nothing to do */ 2244 newarray = (char**)malloc(sizeof(char*)*(cfg->num_tags+1)); 2245 if(!newarray) 2246 return 0; 2247 newtag = strdup(tag); 2248 if(!newtag) { 2249 free(newarray); 2250 return 0; 2251 } 2252 if(cfg->tagname) { 2253 memcpy(newarray, cfg->tagname, sizeof(char*)*cfg->num_tags); 2254 free(cfg->tagname); 2255 } 2256 newarray[cfg->num_tags++] = newtag; 2257 cfg->tagname = newarray; 2258 return 1; 2259 } 2260 2261 /** set a bit in a bit array */ 2262 static void 2263 cfg_set_bit(uint8_t* bitlist, size_t len, int id) 2264 { 2265 int pos = id/8; 2266 log_assert((size_t)pos < len); 2267 (void)len; 2268 bitlist[pos] |= 1<<(id%8); 2269 } 2270 2271 uint8_t* config_parse_taglist(struct config_file* cfg, char* str, 2272 size_t* listlen) 2273 { 2274 uint8_t* taglist = NULL; 2275 size_t len = 0; 2276 char* p, *s; 2277 2278 /* allocate */ 2279 if(cfg->num_tags == 0) { 2280 log_err("parse taglist, but no tags defined"); 2281 return 0; 2282 } 2283 len = (size_t)(cfg->num_tags+7)/8; 2284 taglist = calloc(1, len); 2285 if(!taglist) { 2286 log_err("out of memory"); 2287 return 0; 2288 } 2289 2290 /* parse */ 2291 s = str; 2292 while((p=strsep(&s, " \t\n")) != NULL) { 2293 if(*p) { 2294 int id = find_tag_id(cfg, p); 2295 /* set this bit in the bitlist */ 2296 if(id == -1) { 2297 log_err("unknown tag: %s", p); 2298 free(taglist); 2299 return 0; 2300 } 2301 cfg_set_bit(taglist, len, id); 2302 } 2303 } 2304 2305 *listlen = len; 2306 return taglist; 2307 } 2308 2309 uint8_t* cfg_parse_nsid(const char* str, uint16_t* nsid_len) 2310 { 2311 uint8_t* nsid = NULL; 2312 2313 if (strncasecmp(str, "ascii_", 6) == 0) { 2314 if ((nsid = (uint8_t *)strdup(str + 6))) 2315 *nsid_len = strlen(str + 6); 2316 2317 } else if (strlen(str) % 2) { 2318 ; /* hex string has even number of characters */ 2319 } 2320 2321 else if (*str && (nsid = calloc(1, strlen(str) / 2))) { 2322 const char *ch; 2323 uint8_t *dp; 2324 2325 for ( ch = str, dp = nsid 2326 ; isxdigit(ch[0]) && isxdigit(ch[1]) 2327 ; ch += 2, dp++) { 2328 *dp = (uint8_t)sldns_hexdigit_to_int(ch[0]) * 16; 2329 *dp += (uint8_t)sldns_hexdigit_to_int(ch[1]); 2330 } 2331 if (*ch) { 2332 free(nsid); 2333 nsid = NULL; 2334 } else 2335 *nsid_len = strlen(str) / 2; 2336 } 2337 return nsid; 2338 } 2339 2340 2341 char* config_taglist2str(struct config_file* cfg, uint8_t* taglist, 2342 size_t taglen) 2343 { 2344 char buf[10240]; 2345 size_t i, j, len = 0; 2346 buf[0] = 0; 2347 for(i=0; i<taglen; i++) { 2348 if(taglist[i] == 0) 2349 continue; 2350 for(j=0; j<8; j++) { 2351 if((taglist[i] & (1<<j)) != 0) { 2352 size_t id = i*8 + j; 2353 snprintf(buf+len, sizeof(buf)-len, "%s%s", 2354 (len==0?"":" "), cfg->tagname[id]); 2355 len += strlen(buf+len); 2356 } 2357 } 2358 } 2359 return strdup(buf); 2360 } 2361 2362 int taglist_intersect(uint8_t* list1, size_t list1len, const uint8_t* list2, 2363 size_t list2len) 2364 { 2365 size_t i; 2366 if(!list1 || !list2) 2367 return 0; 2368 for(i=0; i<list1len && i<list2len; i++) { 2369 if((list1[i] & list2[i]) != 0) 2370 return 1; 2371 } 2372 return 0; 2373 } 2374 2375 void 2376 config_apply(struct config_file* config) 2377 { 2378 MAX_TTL = (time_t)config->max_ttl; 2379 MIN_TTL = (time_t)config->min_ttl; 2380 SERVE_EXPIRED = config->serve_expired; 2381 SERVE_EXPIRED_TTL = (time_t)config->serve_expired_ttl; 2382 SERVE_EXPIRED_REPLY_TTL = (time_t)config->serve_expired_reply_ttl; 2383 SERVE_ORIGINAL_TTL = config->serve_original_ttl; 2384 MAX_NEG_TTL = (time_t)config->max_negative_ttl; 2385 MIN_NEG_TTL = (time_t)config->min_negative_ttl; 2386 RTT_MIN_TIMEOUT = config->infra_cache_min_rtt; 2387 RTT_MAX_TIMEOUT = config->infra_cache_max_rtt; 2388 EDNS_ADVERTISED_SIZE = (uint16_t)config->edns_buffer_size; 2389 MINIMAL_RESPONSES = config->minimal_responses; 2390 RRSET_ROUNDROBIN = config->rrset_roundrobin; 2391 LOG_TAG_QUERYREPLY = config->log_tag_queryreply; 2392 UNKNOWN_SERVER_NICENESS = config->unknown_server_time_limit; 2393 USEFUL_SERVER_TOP_TIMEOUT = RTT_MAX_TIMEOUT; 2394 BLACKLIST_PENALTY = USEFUL_SERVER_TOP_TIMEOUT*4; 2395 log_set_time_asc(config->log_time_ascii); 2396 autr_permit_small_holddown = config->permit_small_holddown; 2397 stream_wait_max = config->stream_wait_size; 2398 http2_query_buffer_max = config->http_query_buffer_size; 2399 http2_response_buffer_max = config->http_response_buffer_size; 2400 } 2401 2402 void config_lookup_uid(struct config_file* cfg) 2403 { 2404 #ifdef HAVE_GETPWNAM 2405 /* translate username into uid and gid */ 2406 if(cfg->username && cfg->username[0]) { 2407 struct passwd *pwd; 2408 if((pwd = getpwnam(cfg->username)) != NULL) { 2409 cfg_uid = pwd->pw_uid; 2410 cfg_gid = pwd->pw_gid; 2411 } 2412 } 2413 #else 2414 (void)cfg; 2415 #endif 2416 } 2417 2418 /** 2419 * Calculate string length of full pathname in original filesys 2420 * @param fname: the path name to convert. 2421 * Must not be null or empty. 2422 * @param cfg: config struct for chroot and chdir (if set). 2423 * @param use_chdir: if false, only chroot is applied. 2424 * @return length of string. 2425 * remember to allocate one more for 0 at end in mallocs. 2426 */ 2427 static size_t 2428 strlen_after_chroot(const char* fname, struct config_file* cfg, int use_chdir) 2429 { 2430 size_t len = 0; 2431 int slashit = 0; 2432 if(cfg->chrootdir && cfg->chrootdir[0] && 2433 strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) { 2434 /* already full pathname, return it */ 2435 return strlen(fname); 2436 } 2437 /* chroot */ 2438 if(cfg->chrootdir && cfg->chrootdir[0]) { 2439 /* start with chrootdir */ 2440 len += strlen(cfg->chrootdir); 2441 slashit = 1; 2442 } 2443 /* chdir */ 2444 #ifdef UB_ON_WINDOWS 2445 if(fname[0] != 0 && fname[1] == ':') { 2446 /* full path, no chdir */ 2447 } else 2448 #endif 2449 if(fname[0] == '/' || !use_chdir) { 2450 /* full path, no chdir */ 2451 } else if(cfg->directory && cfg->directory[0]) { 2452 /* prepend chdir */ 2453 if(slashit && cfg->directory[0] != '/') 2454 len++; 2455 if(cfg->chrootdir && cfg->chrootdir[0] && 2456 strncmp(cfg->chrootdir, cfg->directory, 2457 strlen(cfg->chrootdir)) == 0) 2458 len += strlen(cfg->directory)-strlen(cfg->chrootdir); 2459 else len += strlen(cfg->directory); 2460 slashit = 1; 2461 } 2462 /* fname */ 2463 if(slashit && fname[0] != '/') 2464 len++; 2465 len += strlen(fname); 2466 return len; 2467 } 2468 2469 char* 2470 fname_after_chroot(const char* fname, struct config_file* cfg, int use_chdir) 2471 { 2472 size_t len = strlen_after_chroot(fname, cfg, use_chdir)+1; 2473 int slashit = 0; 2474 char* buf = (char*)malloc(len); 2475 if(!buf) 2476 return NULL; 2477 buf[0] = 0; 2478 /* is fname already in chroot ? */ 2479 if(cfg->chrootdir && cfg->chrootdir[0] && 2480 strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) { 2481 /* already full pathname, return it */ 2482 (void)strlcpy(buf, fname, len); 2483 buf[len-1] = 0; 2484 return buf; 2485 } 2486 /* chroot */ 2487 if(cfg->chrootdir && cfg->chrootdir[0]) { 2488 /* start with chrootdir */ 2489 (void)strlcpy(buf, cfg->chrootdir, len); 2490 slashit = 1; 2491 } 2492 #ifdef UB_ON_WINDOWS 2493 if(fname[0] != 0 && fname[1] == ':') { 2494 /* full path, no chdir */ 2495 } else 2496 #endif 2497 /* chdir */ 2498 if(fname[0] == '/' || !use_chdir) { 2499 /* full path, no chdir */ 2500 } else if(cfg->directory && cfg->directory[0]) { 2501 /* prepend chdir */ 2502 if(slashit && cfg->directory[0] != '/') 2503 (void)strlcat(buf, "/", len); 2504 /* is the directory already in the chroot? */ 2505 if(cfg->chrootdir && cfg->chrootdir[0] && 2506 strncmp(cfg->chrootdir, cfg->directory, 2507 strlen(cfg->chrootdir)) == 0) 2508 (void)strlcat(buf, cfg->directory+strlen(cfg->chrootdir), 2509 len); 2510 else (void)strlcat(buf, cfg->directory, len); 2511 slashit = 1; 2512 } 2513 /* fname */ 2514 if(slashit && fname[0] != '/') 2515 (void)strlcat(buf, "/", len); 2516 (void)strlcat(buf, fname, len); 2517 buf[len-1] = 0; 2518 return buf; 2519 } 2520 2521 /** return next space character in string */ 2522 static char* next_space_pos(const char* str) 2523 { 2524 char* sp = strchr(str, ' '); 2525 char* tab = strchr(str, '\t'); 2526 if(!tab && !sp) 2527 return NULL; 2528 if(!sp) return tab; 2529 if(!tab) return sp; 2530 return (sp<tab)?sp:tab; 2531 } 2532 2533 /** return last space character in string */ 2534 static char* last_space_pos(const char* str) 2535 { 2536 char* sp = strrchr(str, ' '); 2537 char* tab = strrchr(str, '\t'); 2538 if(!tab && !sp) 2539 return NULL; 2540 if(!sp) return tab; 2541 if(!tab) return sp; 2542 return (sp>tab)?sp:tab; 2543 } 2544 2545 int 2546 cfg_parse_local_zone(struct config_file* cfg, const char* val) 2547 { 2548 const char *type, *name_end, *name; 2549 char buf[256]; 2550 2551 /* parse it as: [zone_name] [between stuff] [zone_type] */ 2552 name = val; 2553 while(*name && isspace((unsigned char)*name)) 2554 name++; 2555 if(!*name) { 2556 log_err("syntax error: too short: %s", val); 2557 return 0; 2558 } 2559 name_end = next_space_pos(name); 2560 if(!name_end || !*name_end) { 2561 log_err("syntax error: expected zone type: %s", val); 2562 return 0; 2563 } 2564 if (name_end - name > 255) { 2565 log_err("syntax error: bad zone name: %s", val); 2566 return 0; 2567 } 2568 (void)strlcpy(buf, name, sizeof(buf)); 2569 buf[name_end-name] = '\0'; 2570 2571 type = last_space_pos(name_end); 2572 while(type && *type && isspace((unsigned char)*type)) 2573 type++; 2574 if(!type || !*type) { 2575 log_err("syntax error: expected zone type: %s", val); 2576 return 0; 2577 } 2578 2579 if(strcmp(type, "nodefault")==0) { 2580 return cfg_strlist_insert(&cfg->local_zones_nodefault, 2581 strdup(name)); 2582 #ifdef USE_IPSET 2583 } else if(strcmp(type, "ipset")==0) { 2584 return cfg_strlist_insert(&cfg->local_zones_ipset, 2585 strdup(name)); 2586 #endif 2587 } else { 2588 return cfg_str2list_insert(&cfg->local_zones, strdup(buf), 2589 strdup(type)); 2590 } 2591 } 2592 2593 char* cfg_ptr_reverse(char* str) 2594 { 2595 char* ip, *ip_end; 2596 char* name; 2597 char* result; 2598 char buf[1024]; 2599 struct sockaddr_storage addr; 2600 socklen_t addrlen; 2601 2602 /* parse it as: [IP] [between stuff] [name] */ 2603 ip = str; 2604 while(*ip && isspace((unsigned char)*ip)) 2605 ip++; 2606 if(!*ip) { 2607 log_err("syntax error: too short: %s", str); 2608 return NULL; 2609 } 2610 ip_end = next_space_pos(ip); 2611 if(!ip_end || !*ip_end) { 2612 log_err("syntax error: expected name: %s", str); 2613 return NULL; 2614 } 2615 2616 name = last_space_pos(ip_end); 2617 if(!name || !*name) { 2618 log_err("syntax error: expected name: %s", str); 2619 return NULL; 2620 } 2621 2622 sscanf(ip, "%100s", buf); 2623 buf[sizeof(buf)-1]=0; 2624 2625 if(!ipstrtoaddr(buf, UNBOUND_DNS_PORT, &addr, &addrlen)) { 2626 log_err("syntax error: cannot parse address: %s", str); 2627 return NULL; 2628 } 2629 2630 /* reverse IPv4: 2631 * ddd.ddd.ddd.ddd.in-addr-arpa. 2632 * IPv6: (h.){32}.ip6.arpa. */ 2633 2634 if(addr_is_ip6(&addr, addrlen)) { 2635 uint8_t ad[16]; 2636 const char* hex = "0123456789abcdef"; 2637 char *p = buf; 2638 int i; 2639 memmove(ad, &((struct sockaddr_in6*)&addr)->sin6_addr, 2640 sizeof(ad)); 2641 for(i=15; i>=0; i--) { 2642 uint8_t b = ad[i]; 2643 *p++ = hex[ (b&0x0f) ]; 2644 *p++ = '.'; 2645 *p++ = hex[ (b&0xf0) >> 4 ]; 2646 *p++ = '.'; 2647 } 2648 snprintf(buf+16*4, sizeof(buf)-16*4, "ip6.arpa. "); 2649 } else { 2650 uint8_t ad[4]; 2651 memmove(ad, &((struct sockaddr_in*)&addr)->sin_addr, 2652 sizeof(ad)); 2653 snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa. ", 2654 (unsigned)ad[3], (unsigned)ad[2], 2655 (unsigned)ad[1], (unsigned)ad[0]); 2656 } 2657 2658 /* printed the reverse address, now the between goop and name on end */ 2659 while(*ip_end && isspace((unsigned char)*ip_end)) 2660 ip_end++; 2661 if(name>ip_end) { 2662 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%.*s", 2663 (int)(name-ip_end), ip_end); 2664 } 2665 snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), " PTR %s", name); 2666 2667 result = strdup(buf); 2668 if(!result) { 2669 log_err("out of memory parsing %s", str); 2670 return NULL; 2671 } 2672 return result; 2673 } 2674 2675 #ifdef UB_ON_WINDOWS 2676 char* 2677 w_lookup_reg_str(const char* key, const char* name) 2678 { 2679 HKEY hk = NULL; 2680 DWORD type = 0; 2681 BYTE buf[1024]; 2682 DWORD len = (DWORD)sizeof(buf); 2683 LONG ret; 2684 char* result = NULL; 2685 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hk); 2686 if(ret == ERROR_FILE_NOT_FOUND) 2687 return NULL; /* key does not exist */ 2688 else if(ret != ERROR_SUCCESS) { 2689 log_err("RegOpenKeyEx failed"); 2690 return NULL; 2691 } 2692 ret = RegQueryValueEx(hk, (LPCTSTR)name, 0, &type, buf, &len); 2693 if(RegCloseKey(hk)) 2694 log_err("RegCloseKey"); 2695 if(ret == ERROR_FILE_NOT_FOUND) 2696 return NULL; /* name does not exist */ 2697 else if(ret != ERROR_SUCCESS) { 2698 log_err("RegQueryValueEx failed"); 2699 return NULL; 2700 } 2701 if(type == REG_SZ || type == REG_MULTI_SZ || type == REG_EXPAND_SZ) { 2702 buf[sizeof(buf)-1] = 0; 2703 buf[sizeof(buf)-2] = 0; /* for multi_sz */ 2704 result = strdup((char*)buf); 2705 if(!result) log_err("out of memory"); 2706 } 2707 return result; 2708 } 2709 2710 void w_config_adjust_directory(struct config_file* cfg) 2711 { 2712 if(cfg->directory && cfg->directory[0]) { 2713 TCHAR dirbuf[2*MAX_PATH+4]; 2714 if(strcmp(cfg->directory, "%EXECUTABLE%") == 0) { 2715 /* get executable path, and if that contains 2716 * directories, snip off the filename part */ 2717 dirbuf[0] = 0; 2718 if(!GetModuleFileName(NULL, dirbuf, MAX_PATH)) 2719 log_err("could not GetModuleFileName"); 2720 if(strrchr(dirbuf, '\\')) { 2721 (strrchr(dirbuf, '\\'))[0] = 0; 2722 } else log_err("GetModuleFileName had no path"); 2723 if(dirbuf[0]) { 2724 /* adjust directory for later lookups to work*/ 2725 free(cfg->directory); 2726 cfg->directory = memdup(dirbuf, strlen(dirbuf)+1); 2727 } 2728 } 2729 } 2730 } 2731 #endif /* UB_ON_WINDOWS */ 2732 2733 int options_remote_is_address(struct config_file* cfg) 2734 { 2735 if(!cfg->remote_control_enable) return 0; 2736 if(!cfg->control_ifs.first) return 1; 2737 if(!cfg->control_ifs.first->str) return 1; 2738 if(cfg->control_ifs.first->str[0] == 0) return 1; 2739 return (cfg->control_ifs.first->str[0] != '/'); 2740 } 2741 2742 /** see if interface is https, its port number == the https port number */ 2743 int 2744 if_is_https(const char* ifname, const char* port, int https_port) 2745 { 2746 char* p = strchr(ifname, '@'); 2747 if(!p && atoi(port) == https_port) 2748 return 1; 2749 if(p && atoi(p+1) == https_port) 2750 return 1; 2751 return 0; 2752 } 2753 2754 /** see if config contains https turned on */ 2755 int cfg_has_https(struct config_file* cfg) 2756 { 2757 int i; 2758 char portbuf[32]; 2759 snprintf(portbuf, sizeof(portbuf), "%d", cfg->port); 2760 for(i = 0; i<cfg->num_ifs; i++) { 2761 if(if_is_https(cfg->ifs[i], portbuf, cfg->https_port)) 2762 return 1; 2763 } 2764 return 0; 2765 } 2766 2767 /** see if interface is PROXYv2, its port number == the proxy port number */ 2768 int 2769 if_is_pp2(const char* ifname, const char* port, 2770 struct config_strlist* proxy_protocol_port) 2771 { 2772 struct config_strlist* s; 2773 char* p = strchr(ifname, '@'); 2774 for(s = proxy_protocol_port; s; s = s->next) { 2775 if(p && atoi(p+1) == atoi(s->str)) 2776 return 1; 2777 if(!p && atoi(port) == atoi(s->str)) 2778 return 1; 2779 } 2780 return 0; 2781 } 2782 2783 /** see if interface is DNSCRYPT, its port number == the dnscrypt port number */ 2784 int 2785 if_is_dnscrypt(const char* ifname, const char* port, int dnscrypt_port) 2786 { 2787 #ifdef USE_DNSCRYPT 2788 return ((strchr(ifname, '@') && 2789 atoi(strchr(ifname, '@')+1) == dnscrypt_port) || 2790 (!strchr(ifname, '@') && atoi(port) == dnscrypt_port)); 2791 #else 2792 (void)ifname; 2793 (void)port; 2794 (void)dnscrypt_port; 2795 return 0; 2796 #endif 2797 } 2798