xref: /openbsd-src/usr.sbin/unbound/util/config_file.c (revision 98bc733b08604094f4138174a0ee0bb9faaca4bd)
1933707f3Ssthen /*
2933707f3Ssthen  * util/config_file.c - reads and stores the config file for unbound.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains functions for the config file.
40933707f3Ssthen  */
41933707f3Ssthen 
42933707f3Ssthen #include "config.h"
43933707f3Ssthen #include <ctype.h>
445d76a658Ssthen #include <stdarg.h>
45*98bc733bSsthen #include <errno.h>
465d76a658Ssthen #ifdef HAVE_TIME_H
475d76a658Ssthen #include <time.h>
485d76a658Ssthen #endif
49933707f3Ssthen #include "util/log.h"
50933707f3Ssthen #include "util/configyyrename.h"
51933707f3Ssthen #include "util/config_file.h"
52933707f3Ssthen #include "util/configparser.h"
53933707f3Ssthen #include "util/net_help.h"
54933707f3Ssthen #include "util/data/msgparse.h"
55933707f3Ssthen #include "util/module.h"
56933707f3Ssthen #include "util/regional.h"
57933707f3Ssthen #include "util/fptr_wlist.h"
58933707f3Ssthen #include "util/data/dname.h"
598b7325afSsthen #include "util/random.h"
6031f127bbSsthen #include "util/rtt.h"
61a58bff56Ssthen #include "services/cache/infra.h"
62a58bff56Ssthen #include "sldns/wire2str.h"
63a58bff56Ssthen #include "sldns/parseutil.h"
643150e5f6Ssthen #include "iterator/iterator.h"
65229e174cSsthen #ifdef HAVE_GLOB_H
66229e174cSsthen # include <glob.h>
67229e174cSsthen #endif
682be9e038Ssthen #ifdef CLIENT_SUBNET
692be9e038Ssthen #include "edns-subnet/edns-subnet.h"
702be9e038Ssthen #endif
7131f127bbSsthen #ifdef HAVE_PWD_H
7231f127bbSsthen #include <pwd.h>
7331f127bbSsthen #endif
74229e174cSsthen 
75bdfc4d55Sflorian /** from cfg username, after daemonize setup performed */
7647dfde74Sflorian uid_t cfg_uid = (uid_t)-1;
77bdfc4d55Sflorian /** from cfg username, after daemonize setup performed */
7847dfde74Sflorian gid_t cfg_gid = (gid_t)-1;
79a961b961Ssthen /** for debug allow small timeout values for fast rollovers */
80a961b961Ssthen int autr_permit_small_holddown = 0;
81f6b99bafSsthen /** size (in bytes) of stream wait buffers max */
82f6b99bafSsthen size_t stream_wait_max = 4 * 1024 * 1024;
832c144df0Ssthen size_t http2_query_buffer_max = 4 * 1024 * 1024;
842c144df0Ssthen size_t http2_response_buffer_max = 4 * 1024 * 1024;
8547dfde74Sflorian 
86933707f3Ssthen /** global config during parsing */
87933707f3Ssthen struct config_parser_state* cfg_parser = 0;
88933707f3Ssthen 
89933707f3Ssthen /** init ports possible for use */
90933707f3Ssthen static void init_outgoing_availports(int* array, int num);
91933707f3Ssthen 
928b7325afSsthen /** init cookie with random data */
938b7325afSsthen static void init_cookie_secret(uint8_t* cookie_secret, size_t cookie_secret_len);
948b7325afSsthen 
95933707f3Ssthen struct config_file*
96933707f3Ssthen config_create(void)
97933707f3Ssthen {
98933707f3Ssthen 	struct config_file* cfg;
99933707f3Ssthen 	cfg = (struct config_file*)calloc(1, sizeof(struct config_file));
100933707f3Ssthen 	if(!cfg)
101933707f3Ssthen 		return NULL;
102933707f3Ssthen 	/* the defaults if no config is present */
103933707f3Ssthen 	cfg->verbosity = 1;
104933707f3Ssthen 	cfg->stat_interval = 0;
105933707f3Ssthen 	cfg->stat_cumulative = 0;
106933707f3Ssthen 	cfg->stat_extended = 0;
1078b7325afSsthen 	cfg->stat_inhibit_zero = 1;
108933707f3Ssthen 	cfg->num_threads = 1;
109933707f3Ssthen 	cfg->port = UNBOUND_DNS_PORT;
110933707f3Ssthen 	cfg->do_ip4 = 1;
111933707f3Ssthen 	cfg->do_ip6 = 1;
112933707f3Ssthen 	cfg->do_udp = 1;
113933707f3Ssthen 	cfg->do_tcp = 1;
114191f22c6Ssthen 	cfg->tcp_reuse_timeout = 60 * 1000; /* 60s in milisecs */
115191f22c6Ssthen 	cfg->max_reuse_tcp_queries = 200;
116933707f3Ssthen 	cfg->tcp_upstream = 0;
117bdfc4d55Sflorian 	cfg->udp_upstream_without_downstream = 0;
11832e31f52Ssthen 	cfg->tcp_mss = 0;
11932e31f52Ssthen 	cfg->outgoing_tcp_mss = 0;
1202308e98cSsthen 	cfg->tcp_idle_timeout = 30 * 1000; /* 30s in millisecs */
121191f22c6Ssthen 	cfg->tcp_auth_query_timeout = 3 * 1000; /* 3s in millisecs */
1222308e98cSsthen 	cfg->do_tcp_keepalive = 0;
1232308e98cSsthen 	cfg->tcp_keepalive_timeout = 120 * 1000; /* 120s in millisecs */
1248b7325afSsthen 	cfg->sock_queue_timeout = 0; /* do not check timeout */
125933707f3Ssthen 	cfg->ssl_service_key = NULL;
126933707f3Ssthen 	cfg->ssl_service_pem = NULL;
12720237c55Ssthen 	cfg->ssl_port = UNBOUND_DNS_OVER_TLS_PORT;
128933707f3Ssthen 	cfg->ssl_upstream = 0;
129938a3a5eSflorian 	cfg->tls_cert_bundle = NULL;
13020237c55Ssthen 	cfg->tls_win_cert = 0;
131a3167c07Ssthen 	cfg->tls_use_sni = 1;
1322c144df0Ssthen 	cfg->https_port = UNBOUND_DNS_OVER_HTTPS_PORT;
1332c144df0Ssthen 	if(!(cfg->http_endpoint = strdup("/dns-query"))) goto error_exit;
1342c144df0Ssthen 	cfg->http_max_streams = 100;
1352c144df0Ssthen 	cfg->http_query_buffer_size = 4*1024*1024;
1362c144df0Ssthen 	cfg->http_response_buffer_size = 4*1024*1024;
1372c144df0Ssthen 	cfg->http_nodelay = 1;
138933707f3Ssthen 	cfg->use_syslog = 1;
13977079be7Ssthen 	cfg->log_identity = NULL; /* changed later with argv[0] */
140933707f3Ssthen 	cfg->log_time_ascii = 0;
141933707f3Ssthen 	cfg->log_queries = 0;
14277079be7Ssthen 	cfg->log_replies = 0;
143f6b99bafSsthen 	cfg->log_tag_queryreply = 0;
1442308e98cSsthen 	cfg->log_local_actions = 0;
1452308e98cSsthen 	cfg->log_servfail = 0;
146f46c52bfSsthen 	cfg->log_destaddr = 0;
147933707f3Ssthen #ifndef USE_WINSOCK
148933707f3Ssthen #  ifdef USE_MINI_EVENT
149933707f3Ssthen 	/* select max 1024 sockets */
150933707f3Ssthen 	cfg->outgoing_num_ports = 960;
151933707f3Ssthen 	cfg->num_queries_per_thread = 512;
152933707f3Ssthen #  else
153933707f3Ssthen 	/* libevent can use many sockets */
154933707f3Ssthen 	cfg->outgoing_num_ports = 4096;
155933707f3Ssthen 	cfg->num_queries_per_thread = 1024;
156933707f3Ssthen #  endif
157933707f3Ssthen 	cfg->outgoing_num_tcp = 10;
158933707f3Ssthen 	cfg->incoming_num_tcp = 10;
159933707f3Ssthen #else
160933707f3Ssthen 	cfg->outgoing_num_ports = 48; /* windows is limited in num fds */
161933707f3Ssthen 	cfg->num_queries_per_thread = 24;
162933707f3Ssthen 	cfg->outgoing_num_tcp = 2; /* leaves 64-52=12 for: 4if,1stop,thread4 */
163933707f3Ssthen 	cfg->incoming_num_tcp = 2;
164933707f3Ssthen #endif
165f6b99bafSsthen 	cfg->stream_wait_size = 4 * 1024 * 1024;
1662c144df0Ssthen 	cfg->edns_buffer_size = 1232; /* from DNS flagday recommendation */
167933707f3Ssthen 	cfg->msg_buffer_size = 65552; /* 64 k + a small margin */
168933707f3Ssthen 	cfg->msg_cache_size = 4 * 1024 * 1024;
169933707f3Ssthen 	cfg->msg_cache_slabs = 4;
170933707f3Ssthen 	cfg->jostle_time = 200;
171933707f3Ssthen 	cfg->rrset_cache_size = 4 * 1024 * 1024;
172933707f3Ssthen 	cfg->rrset_cache_slabs = 4;
173933707f3Ssthen 	cfg->host_ttl = 900;
174933707f3Ssthen 	cfg->bogus_ttl = 60;
175933707f3Ssthen 	cfg->min_ttl = 0;
176933707f3Ssthen 	cfg->max_ttl = 3600 * 24;
177a58bff56Ssthen 	cfg->max_negative_ttl = 3600;
1782bdc0ed1Ssthen 	cfg->min_negative_ttl = 0;
179933707f3Ssthen 	cfg->prefetch = 0;
180933707f3Ssthen 	cfg->prefetch_key = 0;
1813150e5f6Ssthen 	cfg->deny_any = 0;
182933707f3Ssthen 	cfg->infra_cache_slabs = 4;
183933707f3Ssthen 	cfg->infra_cache_numhosts = 10000;
18431f127bbSsthen 	cfg->infra_cache_min_rtt = 50;
185d1e2768aSsthen 	cfg->infra_cache_max_rtt = 120000;
186eba819a2Ssthen 	cfg->infra_keep_probing = 0;
1875d76a658Ssthen 	cfg->delay_close = 0;
188eba819a2Ssthen 	cfg->udp_connect = 1;
189933707f3Ssthen 	if(!(cfg->outgoing_avail_ports = (int*)calloc(65536, sizeof(int))))
190933707f3Ssthen 		goto error_exit;
191933707f3Ssthen 	init_outgoing_availports(cfg->outgoing_avail_ports, 65536);
192933707f3Ssthen 	if(!(cfg->username = strdup(UB_USERNAME))) goto error_exit;
193933707f3Ssthen #ifdef HAVE_CHROOT
194933707f3Ssthen 	if(!(cfg->chrootdir = strdup(CHROOT_DIR))) goto error_exit;
195933707f3Ssthen #endif
196933707f3Ssthen 	if(!(cfg->directory = strdup(RUN_DIR))) goto error_exit;
197933707f3Ssthen 	if(!(cfg->logfile = strdup(""))) goto error_exit;
198933707f3Ssthen 	if(!(cfg->pidfile = strdup(PIDFILE))) goto error_exit;
199933707f3Ssthen 	if(!(cfg->target_fetch_policy = strdup("3 2 1 0 0"))) goto error_exit;
2003150e5f6Ssthen 	cfg->fast_server_permil = 0;
2013150e5f6Ssthen 	cfg->fast_server_num = 3;
202933707f3Ssthen 	cfg->donotqueryaddrs = NULL;
203933707f3Ssthen 	cfg->donotquery_localhost = 1;
204933707f3Ssthen 	cfg->root_hints = NULL;
20577079be7Ssthen 	cfg->use_systemd = 0;
206933707f3Ssthen 	cfg->do_daemonize = 1;
207933707f3Ssthen 	cfg->if_automatic = 0;
2080bdb4f62Ssthen 	cfg->if_automatic_ports = NULL;
209933707f3Ssthen 	cfg->so_rcvbuf = 0;
210933707f3Ssthen 	cfg->so_sndbuf = 0;
2113150e5f6Ssthen 	cfg->so_reuseport = REUSEPORT_DEFAULT;
212a58bff56Ssthen 	cfg->ip_transparent = 0;
2132ee382b6Ssthen 	cfg->ip_freebind = 0;
214a3167c07Ssthen 	cfg->ip_dscp = 0;
215933707f3Ssthen 	cfg->num_ifs = 0;
216933707f3Ssthen 	cfg->ifs = NULL;
217933707f3Ssthen 	cfg->num_out_ifs = 0;
218933707f3Ssthen 	cfg->out_ifs = NULL;
219933707f3Ssthen 	cfg->stubs = NULL;
220933707f3Ssthen 	cfg->forwards = NULL;
221bdfc4d55Sflorian 	cfg->auths = NULL;
2222be9e038Ssthen #ifdef CLIENT_SUBNET
2232be9e038Ssthen 	cfg->client_subnet = NULL;
2242be9e038Ssthen 	cfg->client_subnet_zone = NULL;
2252be9e038Ssthen 	cfg->client_subnet_opcode = LDNS_EDNS_CLIENT_SUBNET;
2262be9e038Ssthen 	cfg->client_subnet_always_forward = 0;
2272be9e038Ssthen 	cfg->max_client_subnet_ipv4 = 24;
2282be9e038Ssthen 	cfg->max_client_subnet_ipv6 = 56;
2293150e5f6Ssthen 	cfg->min_client_subnet_ipv4 = 0;
2303150e5f6Ssthen 	cfg->min_client_subnet_ipv6 = 0;
2313150e5f6Ssthen 	cfg->max_ecs_tree_size_ipv4 = 100;
2323150e5f6Ssthen 	cfg->max_ecs_tree_size_ipv6 = 100;
2332be9e038Ssthen #endif
23477079be7Ssthen 	cfg->views = NULL;
235933707f3Ssthen 	cfg->acls = NULL;
2362308e98cSsthen 	cfg->tcp_connection_limits = NULL;
2379982a05dSsthen 	cfg->harden_short_bufsize = 1;
238933707f3Ssthen 	cfg->harden_large_queries = 0;
239933707f3Ssthen 	cfg->harden_glue = 1;
240933707f3Ssthen 	cfg->harden_dnssec_stripped = 1;
2412308e98cSsthen 	cfg->harden_below_nxdomain = 1;
242933707f3Ssthen 	cfg->harden_referral_path = 0;
243a961b961Ssthen 	cfg->harden_algo_downgrade = 0;
2448b7325afSsthen 	cfg->harden_unknown_additional = 0;
245933707f3Ssthen 	cfg->use_caps_bits_for_id = 0;
246a58bff56Ssthen 	cfg->caps_whitelist = NULL;
247933707f3Ssthen 	cfg->private_address = NULL;
248933707f3Ssthen 	cfg->private_domain = NULL;
249933707f3Ssthen 	cfg->unwanted_threshold = 0;
250933707f3Ssthen 	cfg->hide_identity = 0;
251933707f3Ssthen 	cfg->hide_version = 0;
2522be9e038Ssthen 	cfg->hide_trustanchor = 0;
253191f22c6Ssthen 	cfg->hide_http_user_agent = 0;
254933707f3Ssthen 	cfg->identity = NULL;
255933707f3Ssthen 	cfg->version = NULL;
256191f22c6Ssthen 	cfg->http_user_agent = NULL;
2579982a05dSsthen 	cfg->nsid_cfg_str = NULL;
2589982a05dSsthen 	cfg->nsid = NULL;
2599982a05dSsthen 	cfg->nsid_len = 0;
260933707f3Ssthen 	cfg->auto_trust_anchor_file_list = NULL;
261933707f3Ssthen 	cfg->trust_anchor_file_list = NULL;
262933707f3Ssthen 	cfg->trust_anchor_list = NULL;
263933707f3Ssthen 	cfg->trusted_keys_file_list = NULL;
2642be9e038Ssthen 	cfg->trust_anchor_signaling = 0;
26520237c55Ssthen 	cfg->root_key_sentinel = 1;
266933707f3Ssthen 	cfg->domain_insecure = NULL;
267933707f3Ssthen 	cfg->val_date_override = 0;
268933707f3Ssthen 	cfg->val_sig_skew_min = 3600; /* at least daylight savings trouble */
269933707f3Ssthen 	cfg->val_sig_skew_max = 86400; /* at most timezone settings trouble */
270191f22c6Ssthen 	cfg->val_max_restart = 5;
271933707f3Ssthen 	cfg->val_clean_additional = 1;
272933707f3Ssthen 	cfg->val_log_level = 0;
273933707f3Ssthen 	cfg->val_log_squelch = 0;
274933707f3Ssthen 	cfg->val_permissive_mode = 0;
275e21c60efSsthen 	cfg->aggressive_nsec = 1;
276933707f3Ssthen 	cfg->ignore_cd = 0;
277d896b962Ssthen 	cfg->disable_edns_do = 0;
27877079be7Ssthen 	cfg->serve_expired = 0;
2792308e98cSsthen 	cfg->serve_expired_ttl = 0;
2802308e98cSsthen 	cfg->serve_expired_ttl_reset = 0;
281eaf2578eSsthen 	cfg->serve_expired_reply_ttl = 30;
282eaf2578eSsthen 	cfg->serve_expired_client_timeout = 0;
2830bdb4f62Ssthen 	cfg->ede_serve_expired = 0;
2849982a05dSsthen 	cfg->serve_original_ttl = 0;
285191f22c6Ssthen 	cfg->zonemd_permissive_mode = 0;
286933707f3Ssthen 	cfg->add_holddown = 30*24*3600;
287933707f3Ssthen 	cfg->del_holddown = 30*24*3600;
288933707f3Ssthen 	cfg->keep_missing = 366*24*3600; /* one year plus a little leeway */
289a961b961Ssthen 	cfg->permit_small_holddown = 0;
290933707f3Ssthen 	cfg->key_cache_size = 4 * 1024 * 1024;
291933707f3Ssthen 	cfg->key_cache_slabs = 4;
292933707f3Ssthen 	cfg->neg_cache_size = 1 * 1024 * 1024;
293933707f3Ssthen 	cfg->local_zones = NULL;
294933707f3Ssthen 	cfg->local_zones_nodefault = NULL;
2958240c1b9Ssthen #ifdef USE_IPSET
2968240c1b9Ssthen 	cfg->local_zones_ipset = NULL;
2978240c1b9Ssthen #endif
2982be9e038Ssthen 	cfg->local_zones_disable_default = 0;
299933707f3Ssthen 	cfg->local_data = NULL;
30077079be7Ssthen 	cfg->local_zone_overrides = NULL;
301e10d3884Sbrad 	cfg->unblock_lan_zones = 0;
30232e31f52Ssthen 	cfg->insecure_lan_zones = 0;
303933707f3Ssthen 	cfg->python_script = NULL;
304a3167c07Ssthen 	cfg->dynlib_file = NULL;
305933707f3Ssthen 	cfg->remote_control_enable = 0;
30620237c55Ssthen 	cfg->control_ifs.first = NULL;
30720237c55Ssthen 	cfg->control_ifs.last = NULL;
308933707f3Ssthen 	cfg->control_port = UNBOUND_CONTROL_PORT;
30920237c55Ssthen 	cfg->control_use_cert = 1;
3102308e98cSsthen 	cfg->minimal_responses = 1;
311a3167c07Ssthen 	cfg->rrset_roundrobin = 1;
3123150e5f6Ssthen 	cfg->unknown_server_time_limit = 376;
3132bdc0ed1Ssthen 	cfg->discard_timeout = 1900; /* msec */
3142bdc0ed1Ssthen 	cfg->wait_limit = 1000;
3152bdc0ed1Ssthen 	cfg->wait_limit_cookie = 10000;
3162bdc0ed1Ssthen 	cfg->wait_limit_netblock = NULL;
3172bdc0ed1Ssthen 	cfg->wait_limit_cookie_netblock = NULL;
3188b7325afSsthen 	cfg->max_udp_size = 1232; /* value taken from edns_buffer_size */
319933707f3Ssthen 	if(!(cfg->server_key_file = strdup(RUN_DIR"/unbound_server.key")))
320933707f3Ssthen 		goto error_exit;
321933707f3Ssthen 	if(!(cfg->server_cert_file = strdup(RUN_DIR"/unbound_server.pem")))
322933707f3Ssthen 		goto error_exit;
323933707f3Ssthen 	if(!(cfg->control_key_file = strdup(RUN_DIR"/unbound_control.key")))
324933707f3Ssthen 		goto error_exit;
325933707f3Ssthen 	if(!(cfg->control_cert_file = strdup(RUN_DIR"/unbound_control.pem")))
326933707f3Ssthen 		goto error_exit;
327933707f3Ssthen 
3282be9e038Ssthen #ifdef CLIENT_SUBNET
3292be9e038Ssthen 	if(!(cfg->module_conf = strdup("subnetcache validator iterator"))) goto error_exit;
3302be9e038Ssthen #else
331933707f3Ssthen 	if(!(cfg->module_conf = strdup("validator iterator"))) goto error_exit;
3322be9e038Ssthen #endif
333933707f3Ssthen 	if(!(cfg->val_nsec3_key_iterations =
334191f22c6Ssthen 		strdup("1024 150 2048 150 4096 150"))) goto error_exit;
335e10d3884Sbrad #if defined(DNSTAP_SOCKET_PATH)
336e10d3884Sbrad 	if(!(cfg->dnstap_socket_path = strdup(DNSTAP_SOCKET_PATH)))
337e10d3884Sbrad 		goto error_exit;
338e10d3884Sbrad #endif
339a3167c07Ssthen 	cfg->dnstap_bidirectional = 1;
340a3167c07Ssthen 	cfg->dnstap_tls = 1;
3412ee382b6Ssthen 	cfg->disable_dnssec_lame_check = 0;
3428b7325afSsthen 	cfg->ip_ratelimit_cookie = 0;
34377079be7Ssthen 	cfg->ip_ratelimit = 0;
344a58bff56Ssthen 	cfg->ratelimit = 0;
34577079be7Ssthen 	cfg->ip_ratelimit_slabs = 4;
346a58bff56Ssthen 	cfg->ratelimit_slabs = 4;
34777079be7Ssthen 	cfg->ip_ratelimit_size = 4*1024*1024;
348a58bff56Ssthen 	cfg->ratelimit_size = 4*1024*1024;
349a58bff56Ssthen 	cfg->ratelimit_for_domain = NULL;
350a58bff56Ssthen 	cfg->ratelimit_below_domain = NULL;
35177079be7Ssthen 	cfg->ip_ratelimit_factor = 10;
352a58bff56Ssthen 	cfg->ratelimit_factor = 10;
353e21c60efSsthen 	cfg->ip_ratelimit_backoff = 0;
354e21c60efSsthen 	cfg->ratelimit_backoff = 0;
355e21c60efSsthen 	cfg->outbound_msg_retry = 5;
3568b7325afSsthen 	cfg->max_sent_count = 32;
3578b7325afSsthen 	cfg->max_query_restarts = 11;
35820237c55Ssthen 	cfg->qname_minimisation = 1;
35977079be7Ssthen 	cfg->qname_minimisation_strict = 0;
3602be9e038Ssthen 	cfg->shm_enable = 0;
3612be9e038Ssthen 	cfg->shm_key = 11777;
362eba819a2Ssthen 	cfg->edns_client_strings = NULL;
363eba819a2Ssthen 	cfg->edns_client_string_opcode = 65001;
3642be9e038Ssthen 	cfg->dnscrypt = 0;
3652be9e038Ssthen 	cfg->dnscrypt_port = 0;
3662be9e038Ssthen 	cfg->dnscrypt_provider = NULL;
3672be9e038Ssthen 	cfg->dnscrypt_provider_cert = NULL;
368bdfc4d55Sflorian 	cfg->dnscrypt_provider_cert_rotated = NULL;
3692be9e038Ssthen 	cfg->dnscrypt_secret_key = NULL;
3707191de28Ssthen 	cfg->dnscrypt_shared_secret_cache_size = 4*1024*1024;
3717191de28Ssthen 	cfg->dnscrypt_shared_secret_cache_slabs = 4;
372bdfc4d55Sflorian 	cfg->dnscrypt_nonce_cache_size = 4*1024*1024;
373bdfc4d55Sflorian 	cfg->dnscrypt_nonce_cache_slabs = 4;
3749982a05dSsthen 	cfg->pad_responses = 1;
3759982a05dSsthen 	cfg->pad_responses_block_size = 468; /* from RFC8467 */
3769982a05dSsthen 	cfg->pad_queries = 1;
3779982a05dSsthen 	cfg->pad_queries_block_size = 128; /* from RFC8467 */
3782be9e038Ssthen #ifdef USE_IPSECMOD
3792be9e038Ssthen 	cfg->ipsecmod_enabled = 1;
3802be9e038Ssthen 	cfg->ipsecmod_ignore_bogus = 0;
3812be9e038Ssthen 	cfg->ipsecmod_hook = NULL;
3822be9e038Ssthen 	cfg->ipsecmod_max_ttl = 3600;
3832be9e038Ssthen 	cfg->ipsecmod_whitelist = NULL;
3842be9e038Ssthen 	cfg->ipsecmod_strict = 0;
3852be9e038Ssthen #endif
3868b7325afSsthen 	cfg->do_answer_cookie = 0;
3878b7325afSsthen 	memset(cfg->cookie_secret, 0, sizeof(cfg->cookie_secret));
3888b7325afSsthen 	cfg->cookie_secret_len = 16;
3898b7325afSsthen 	init_cookie_secret(cfg->cookie_secret, cfg->cookie_secret_len);
390*98bc733bSsthen 	cfg->cookie_secret_file = NULL;
3917191de28Ssthen #ifdef USE_CACHEDB
392eaf2578eSsthen 	if(!(cfg->cachedb_backend = strdup("testframe"))) goto error_exit;
393eaf2578eSsthen 	if(!(cfg->cachedb_secret = strdup("default"))) goto error_exit;
394d896b962Ssthen 	cfg->cachedb_no_store = 0;
3952bdc0ed1Ssthen 	cfg->cachedb_check_when_serve_expired = 1;
396eaf2578eSsthen #ifdef USE_REDIS
397eaf2578eSsthen 	if(!(cfg->redis_server_host = strdup("127.0.0.1"))) goto error_exit;
3988b7325afSsthen 	cfg->redis_server_path = NULL;
3998b7325afSsthen 	cfg->redis_server_password = NULL;
400eaf2578eSsthen 	cfg->redis_timeout = 100;
401eaf2578eSsthen 	cfg->redis_server_port = 6379;
402a3167c07Ssthen 	cfg->redis_expire_records = 0;
403d896b962Ssthen 	cfg->redis_logical_db = 0;
404eaf2578eSsthen #endif  /* USE_REDIS */
405eaf2578eSsthen #endif  /* USE_CACHEDB */
4068240c1b9Ssthen #ifdef USE_IPSET
4078240c1b9Ssthen 	cfg->ipset_name_v4 = NULL;
4088240c1b9Ssthen 	cfg->ipset_name_v6 = NULL;
4098240c1b9Ssthen #endif
4100bdb4f62Ssthen 	cfg->ede = 0;
411933707f3Ssthen 	return cfg;
412933707f3Ssthen error_exit:
413933707f3Ssthen 	config_delete(cfg);
414933707f3Ssthen 	return NULL;
415933707f3Ssthen }
416933707f3Ssthen 
417933707f3Ssthen struct config_file* config_create_forlib(void)
418933707f3Ssthen {
419933707f3Ssthen 	struct config_file* cfg = config_create();
420933707f3Ssthen 	if(!cfg) return NULL;
421933707f3Ssthen 	/* modifications for library use, less verbose, less memory */
422933707f3Ssthen 	free(cfg->chrootdir);
423933707f3Ssthen 	cfg->chrootdir = NULL;
424933707f3Ssthen 	cfg->verbosity = 0;
425933707f3Ssthen 	cfg->outgoing_num_ports = 16; /* in library use, this is 'reasonable'
426933707f3Ssthen 		and probably within the ulimit(maxfds) of the user */
427933707f3Ssthen 	cfg->outgoing_num_tcp = 2;
428933707f3Ssthen 	cfg->msg_cache_size = 1024*1024;
429933707f3Ssthen 	cfg->msg_cache_slabs = 1;
430933707f3Ssthen 	cfg->rrset_cache_size = 1024*1024;
431933707f3Ssthen 	cfg->rrset_cache_slabs = 1;
432933707f3Ssthen 	cfg->infra_cache_slabs = 1;
433933707f3Ssthen 	cfg->use_syslog = 0;
434933707f3Ssthen 	cfg->key_cache_size = 1024*1024;
435933707f3Ssthen 	cfg->key_cache_slabs = 1;
436933707f3Ssthen 	cfg->neg_cache_size = 100 * 1024;
437933707f3Ssthen 	cfg->donotquery_localhost = 0; /* allow, so that you can ask a
438933707f3Ssthen 		forward nameserver running on localhost */
439933707f3Ssthen 	cfg->val_log_level = 2; /* to fill why_bogus with */
440933707f3Ssthen 	cfg->val_log_squelch = 1;
4412308e98cSsthen 	cfg->minimal_responses = 0;
4429982a05dSsthen 	cfg->harden_short_bufsize = 1;
443933707f3Ssthen 	return cfg;
444933707f3Ssthen }
445933707f3Ssthen 
446933707f3Ssthen /** check that the value passed is >= 0 */
447933707f3Ssthen #define IS_NUMBER_OR_ZERO \
448933707f3Ssthen 	if(atoi(val) == 0 && strcmp(val, "0") != 0) return 0
449933707f3Ssthen /** check that the value passed is > 0 */
450933707f3Ssthen #define IS_NONZERO_NUMBER \
451933707f3Ssthen 	if(atoi(val) == 0) return 0
452933707f3Ssthen /** check that the value passed is not 0 and a power of 2 */
453933707f3Ssthen #define IS_POW2_NUMBER \
454933707f3Ssthen 	if(atoi(val) == 0 || !is_pow2((size_t)atoi(val))) return 0
455933707f3Ssthen /** check that the value passed is yes or no */
456933707f3Ssthen #define IS_YES_OR_NO \
457933707f3Ssthen 	if(strcmp(val, "yes") != 0 && strcmp(val, "no") != 0) return 0
458933707f3Ssthen /** put integer_or_zero into variable */
459933707f3Ssthen #define S_NUMBER_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \
460933707f3Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->var = atoi(val); }
461933707f3Ssthen /** put integer_nonzero into variable */
462933707f3Ssthen #define S_NUMBER_NONZERO(str, var) if(strcmp(opt, str) == 0) \
463933707f3Ssthen 	{ IS_NONZERO_NUMBER; cfg->var = atoi(val); }
464933707f3Ssthen /** put integer_or_zero into unsigned */
465933707f3Ssthen #define S_UNSIGNED_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \
466933707f3Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->var = (unsigned)atoi(val); }
467933707f3Ssthen /** put integer_or_zero into size_t */
468933707f3Ssthen #define S_SIZET_OR_ZERO(str, var) if(strcmp(opt, str) == 0) \
469933707f3Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->var = (size_t)atoi(val); }
470933707f3Ssthen /** put integer_nonzero into size_t */
471933707f3Ssthen #define S_SIZET_NONZERO(str, var) if(strcmp(opt, str) == 0) \
472933707f3Ssthen 	{ IS_NONZERO_NUMBER; cfg->var = (size_t)atoi(val); }
473933707f3Ssthen /** put yesno into variable */
474933707f3Ssthen #define S_YNO(str, var) if(strcmp(opt, str) == 0) \
475933707f3Ssthen 	{ IS_YES_OR_NO; cfg->var = (strcmp(val, "yes") == 0); }
476933707f3Ssthen /** put memsize into variable */
477933707f3Ssthen #define S_MEMSIZE(str, var) if(strcmp(opt, str)==0) \
478933707f3Ssthen 	{ return cfg_parse_memsize(val, &cfg->var); }
479933707f3Ssthen /** put pow2 number into variable */
480933707f3Ssthen #define S_POW2(str, var) if(strcmp(opt, str)==0) \
481933707f3Ssthen 	{ IS_POW2_NUMBER; cfg->var = (size_t)atoi(val); }
482933707f3Ssthen /** put string into variable */
483933707f3Ssthen #define S_STR(str, var) if(strcmp(opt, str)==0) \
484933707f3Ssthen 	{ free(cfg->var); return (cfg->var = strdup(val)) != NULL; }
485933707f3Ssthen /** put string into strlist */
486933707f3Ssthen #define S_STRLIST(str, var) if(strcmp(opt, str)==0) \
487933707f3Ssthen 	{ return cfg_strlist_insert(&cfg->var, strdup(val)); }
488bdfc4d55Sflorian /** put string into strlist if not present yet*/
489bdfc4d55Sflorian #define S_STRLIST_UNIQ(str, var) if(strcmp(opt, str)==0) \
490bdfc4d55Sflorian 	{ if(cfg_strlist_find(cfg->var, val)) { return 0;} \
491bdfc4d55Sflorian 	  return cfg_strlist_insert(&cfg->var, strdup(val)); }
49220237c55Ssthen /** append string to strlist */
49320237c55Ssthen #define S_STRLIST_APPEND(str, var) if(strcmp(opt, str)==0) \
49420237c55Ssthen 	{ return cfg_strlist_append(&cfg->var, strdup(val)); }
495933707f3Ssthen 
496933707f3Ssthen int config_set_option(struct config_file* cfg, const char* opt,
497933707f3Ssthen 	const char* val)
498933707f3Ssthen {
49920237c55Ssthen 	char buf[64];
50020237c55Ssthen 	if(!opt) return 0;
50120237c55Ssthen 	if(opt[strlen(opt)-1] != ':' && strlen(opt)+2<sizeof(buf)) {
50220237c55Ssthen 		snprintf(buf, sizeof(buf), "%s:", opt);
50320237c55Ssthen 		opt = buf;
50420237c55Ssthen 	}
505933707f3Ssthen 	S_NUMBER_OR_ZERO("verbosity:", verbosity)
506933707f3Ssthen 	else if(strcmp(opt, "statistics-interval:") == 0) {
507933707f3Ssthen 		if(strcmp(val, "0") == 0 || strcmp(val, "") == 0)
508933707f3Ssthen 			cfg->stat_interval = 0;
509933707f3Ssthen 		else if(atoi(val) == 0)
510933707f3Ssthen 			return 0;
511933707f3Ssthen 		else cfg->stat_interval = atoi(val);
5120bdb4f62Ssthen 	} else if(strcmp(opt, "num-threads:") == 0) {
513933707f3Ssthen 		/* not supported, library must have 1 thread in bgworker */
514933707f3Ssthen 		return 0;
515933707f3Ssthen 	} else if(strcmp(opt, "outgoing-port-permit:") == 0) {
516933707f3Ssthen 		return cfg_mark_ports(val, 1,
517933707f3Ssthen 			cfg->outgoing_avail_ports, 65536);
518933707f3Ssthen 	} else if(strcmp(opt, "outgoing-port-avoid:") == 0) {
519933707f3Ssthen 		return cfg_mark_ports(val, 0,
520933707f3Ssthen 			cfg->outgoing_avail_ports, 65536);
521933707f3Ssthen 	} else if(strcmp(opt, "local-zone:") == 0) {
522933707f3Ssthen 		return cfg_parse_local_zone(cfg, val);
523933707f3Ssthen 	} else if(strcmp(opt, "val-override-date:") == 0) {
524933707f3Ssthen 		if(strcmp(val, "") == 0 || strcmp(val, "0") == 0) {
525933707f3Ssthen 			cfg->val_date_override = 0;
526933707f3Ssthen 		} else if(strlen(val) == 14) {
527933707f3Ssthen 			cfg->val_date_override = cfg_convert_timeval(val);
528933707f3Ssthen 			return cfg->val_date_override != 0;
529933707f3Ssthen 		} else {
530933707f3Ssthen 			if(atoi(val) == 0) return 0;
531933707f3Ssthen 			cfg->val_date_override = (uint32_t)atoi(val);
532933707f3Ssthen 		}
533933707f3Ssthen 	} else if(strcmp(opt, "local-data-ptr:") == 0) {
534933707f3Ssthen 		char* ptr = cfg_ptr_reverse((char*)opt);
535933707f3Ssthen 		return cfg_strlist_insert(&cfg->local_data, ptr);
536933707f3Ssthen 	} else if(strcmp(opt, "logfile:") == 0) {
537933707f3Ssthen 		cfg->use_syslog = 0;
538933707f3Ssthen 		free(cfg->logfile);
539933707f3Ssthen 		return (cfg->logfile = strdup(val)) != NULL;
540933707f3Ssthen 	}
541229e174cSsthen 	else if(strcmp(opt, "log-time-ascii:") == 0)
542229e174cSsthen 	{ IS_YES_OR_NO; cfg->log_time_ascii = (strcmp(val, "yes") == 0);
543229e174cSsthen 	  log_set_time_asc(cfg->log_time_ascii); }
544229e174cSsthen 	else S_SIZET_NONZERO("max-udp-size:", max_udp_size)
545933707f3Ssthen 	else S_YNO("use-syslog:", use_syslog)
54677079be7Ssthen 	else S_STR("log-identity:", log_identity)
547933707f3Ssthen 	else S_YNO("extended-statistics:", stat_extended)
5488b7325afSsthen 	else S_YNO("statistics-inhibit-zero:", stat_inhibit_zero)
549933707f3Ssthen 	else S_YNO("statistics-cumulative:", stat_cumulative)
5502be9e038Ssthen 	else S_YNO("shm-enable:", shm_enable)
5512be9e038Ssthen 	else S_NUMBER_OR_ZERO("shm-key:", shm_key)
552933707f3Ssthen 	else S_YNO("do-ip4:", do_ip4)
553933707f3Ssthen 	else S_YNO("do-ip6:", do_ip6)
554933707f3Ssthen 	else S_YNO("do-udp:", do_udp)
555933707f3Ssthen 	else S_YNO("do-tcp:", do_tcp)
5562c144df0Ssthen 	else S_YNO("prefer-ip4:", prefer_ip4)
5572c144df0Ssthen 	else S_YNO("prefer-ip6:", prefer_ip6)
558933707f3Ssthen 	else S_YNO("tcp-upstream:", tcp_upstream)
559bdfc4d55Sflorian 	else S_YNO("udp-upstream-without-downstream:",
560bdfc4d55Sflorian 		udp_upstream_without_downstream)
56132e31f52Ssthen 	else S_NUMBER_NONZERO("tcp-mss:", tcp_mss)
56232e31f52Ssthen 	else S_NUMBER_NONZERO("outgoing-tcp-mss:", outgoing_tcp_mss)
563191f22c6Ssthen 	else S_NUMBER_NONZERO("tcp-auth-query-timeout:", tcp_auth_query_timeout)
5642308e98cSsthen 	else S_NUMBER_NONZERO("tcp-idle-timeout:", tcp_idle_timeout)
565191f22c6Ssthen 	else S_NUMBER_NONZERO("max-reuse-tcp-queries:", max_reuse_tcp_queries)
566191f22c6Ssthen 	else S_NUMBER_NONZERO("tcp-reuse-timeout:", tcp_reuse_timeout)
5672308e98cSsthen 	else S_YNO("edns-tcp-keepalive:", do_tcp_keepalive)
5682308e98cSsthen 	else S_NUMBER_NONZERO("edns-tcp-keepalive-timeout:", tcp_keepalive_timeout)
5698b7325afSsthen 	else S_NUMBER_OR_ZERO("sock-queue-timeout:", sock_queue_timeout)
570933707f3Ssthen 	else S_YNO("ssl-upstream:", ssl_upstream)
571e21c60efSsthen 	else S_YNO("tls-upstream:", ssl_upstream)
572933707f3Ssthen 	else S_STR("ssl-service-key:", ssl_service_key)
573e21c60efSsthen 	else S_STR("tls-service-key:", ssl_service_key)
574933707f3Ssthen 	else S_STR("ssl-service-pem:", ssl_service_pem)
575e21c60efSsthen 	else S_STR("tls-service-pem:", ssl_service_pem)
576933707f3Ssthen 	else S_NUMBER_NONZERO("ssl-port:", ssl_port)
577e21c60efSsthen 	else S_NUMBER_NONZERO("tls-port:", ssl_port)
578e21c60efSsthen 	else S_STR("ssl-cert-bundle:", tls_cert_bundle)
579938a3a5eSflorian 	else S_STR("tls-cert-bundle:", tls_cert_bundle)
58020237c55Ssthen 	else S_YNO("tls-win-cert:", tls_win_cert)
5810bdb4f62Ssthen 	else S_YNO("tls-system-cert:", tls_win_cert)
582e21c60efSsthen 	else S_STRLIST("additional-ssl-port:", tls_additional_port)
58320237c55Ssthen 	else S_STRLIST("additional-tls-port:", tls_additional_port)
58420237c55Ssthen 	else S_STRLIST("tls-additional-ports:", tls_additional_port)
58520237c55Ssthen 	else S_STRLIST("tls-additional-port:", tls_additional_port)
586f6b99bafSsthen 	else S_STRLIST_APPEND("tls-session-ticket-keys:", tls_session_ticket_keys)
587f6b99bafSsthen 	else S_STR("tls-ciphers:", tls_ciphers)
588f6b99bafSsthen 	else S_STR("tls-ciphersuites:", tls_ciphersuites)
589a3167c07Ssthen 	else S_YNO("tls-use-sni:", tls_use_sni)
5902c144df0Ssthen 	else S_NUMBER_NONZERO("https-port:", https_port)
591eba819a2Ssthen 	else S_STR("http-endpoint:", http_endpoint)
592eba819a2Ssthen 	else S_NUMBER_NONZERO("http-max-streams:", http_max_streams)
593eba819a2Ssthen 	else S_MEMSIZE("http-query-buffer-size:", http_query_buffer_size)
594eba819a2Ssthen 	else S_MEMSIZE("http-response-buffer-size:", http_response_buffer_size)
595eba819a2Ssthen 	else S_YNO("http-nodelay:", http_nodelay)
596eba819a2Ssthen 	else S_YNO("http-notls-downstream:", http_notls_downstream)
597933707f3Ssthen 	else S_YNO("interface-automatic:", if_automatic)
5980bdb4f62Ssthen 	else S_STR("interface-automatic-ports:", if_automatic_ports)
59977079be7Ssthen 	else S_YNO("use-systemd:", use_systemd)
600933707f3Ssthen 	else S_YNO("do-daemonize:", do_daemonize)
601933707f3Ssthen 	else S_NUMBER_NONZERO("port:", port)
602933707f3Ssthen 	else S_NUMBER_NONZERO("outgoing-range:", outgoing_num_ports)
603933707f3Ssthen 	else S_SIZET_OR_ZERO("outgoing-num-tcp:", outgoing_num_tcp)
604933707f3Ssthen 	else S_SIZET_OR_ZERO("incoming-num-tcp:", incoming_num_tcp)
605f6b99bafSsthen 	else S_MEMSIZE("stream-wait-size:", stream_wait_size)
606933707f3Ssthen 	else S_SIZET_NONZERO("edns-buffer-size:", edns_buffer_size)
607933707f3Ssthen 	else S_SIZET_NONZERO("msg-buffer-size:", msg_buffer_size)
608933707f3Ssthen 	else S_MEMSIZE("msg-cache-size:", msg_cache_size)
609933707f3Ssthen 	else S_POW2("msg-cache-slabs:", msg_cache_slabs)
610933707f3Ssthen 	else S_SIZET_NONZERO("num-queries-per-thread:",num_queries_per_thread)
611933707f3Ssthen 	else S_SIZET_OR_ZERO("jostle-timeout:", jostle_time)
612933707f3Ssthen 	else S_MEMSIZE("so-rcvbuf:", so_rcvbuf)
613933707f3Ssthen 	else S_MEMSIZE("so-sndbuf:", so_sndbuf)
6145d76a658Ssthen 	else S_YNO("so-reuseport:", so_reuseport)
615a58bff56Ssthen 	else S_YNO("ip-transparent:", ip_transparent)
6162ee382b6Ssthen 	else S_YNO("ip-freebind:", ip_freebind)
617a3167c07Ssthen 	else S_NUMBER_OR_ZERO("ip-dscp:", ip_dscp)
618933707f3Ssthen 	else S_MEMSIZE("rrset-cache-size:", rrset_cache_size)
619933707f3Ssthen 	else S_POW2("rrset-cache-slabs:", rrset_cache_slabs)
620933707f3Ssthen 	else S_YNO("prefetch:", prefetch)
621933707f3Ssthen 	else S_YNO("prefetch-key:", prefetch_key)
6223150e5f6Ssthen 	else S_YNO("deny-any:", deny_any)
623229e174cSsthen 	else if(strcmp(opt, "cache-max-ttl:") == 0)
624229e174cSsthen 	{ IS_NUMBER_OR_ZERO; cfg->max_ttl = atoi(val); MAX_TTL=(time_t)cfg->max_ttl;}
625a58bff56Ssthen 	else if(strcmp(opt, "cache-max-negative-ttl:") == 0)
626a58bff56Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->max_negative_ttl = atoi(val); MAX_NEG_TTL=(time_t)cfg->max_negative_ttl;}
6272bdc0ed1Ssthen 	else if(strcmp(opt, "cache-min-negative-ttl:") == 0)
6282bdc0ed1Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->min_negative_ttl = atoi(val); MIN_NEG_TTL=(time_t)cfg->min_negative_ttl;}
629229e174cSsthen 	else if(strcmp(opt, "cache-min-ttl:") == 0)
630229e174cSsthen 	{ IS_NUMBER_OR_ZERO; cfg->min_ttl = atoi(val); MIN_TTL=(time_t)cfg->min_ttl;}
63131f127bbSsthen 	else if(strcmp(opt, "infra-cache-min-rtt:") == 0) {
63231f127bbSsthen 	 	IS_NUMBER_OR_ZERO; cfg->infra_cache_min_rtt = atoi(val);
63331f127bbSsthen 		RTT_MIN_TIMEOUT=cfg->infra_cache_min_rtt;
63431f127bbSsthen 	}
635d1e2768aSsthen 	else if(strcmp(opt, "infra-cache-max-rtt:") == 0) {
636d1e2768aSsthen 		IS_NUMBER_OR_ZERO; cfg->infra_cache_max_rtt = atoi(val);
637d1e2768aSsthen 		RTT_MAX_TIMEOUT=cfg->infra_cache_max_rtt;
638d1e2768aSsthen 		USEFUL_SERVER_TOP_TIMEOUT = RTT_MAX_TIMEOUT;
639d1e2768aSsthen 		BLACKLIST_PENALTY = USEFUL_SERVER_TOP_TIMEOUT*4;
640d1e2768aSsthen 	}
641eba819a2Ssthen 	else S_YNO("infra-keep-probing:", infra_keep_probing)
642933707f3Ssthen 	else S_NUMBER_OR_ZERO("infra-host-ttl:", host_ttl)
643933707f3Ssthen 	else S_POW2("infra-cache-slabs:", infra_cache_slabs)
644933707f3Ssthen 	else S_SIZET_NONZERO("infra-cache-numhosts:", infra_cache_numhosts)
6455d76a658Ssthen 	else S_NUMBER_OR_ZERO("delay-close:", delay_close)
646eba819a2Ssthen 	else S_YNO("udp-connect:", udp_connect)
647933707f3Ssthen 	else S_STR("chroot:", chrootdir)
648933707f3Ssthen 	else S_STR("username:", username)
649933707f3Ssthen 	else S_STR("directory:", directory)
650933707f3Ssthen 	else S_STR("pidfile:", pidfile)
651933707f3Ssthen 	else S_YNO("hide-identity:", hide_identity)
652933707f3Ssthen 	else S_YNO("hide-version:", hide_version)
6532be9e038Ssthen 	else S_YNO("hide-trustanchor:", hide_trustanchor)
654191f22c6Ssthen 	else S_YNO("hide-http-user-agent:", hide_http_user_agent)
655933707f3Ssthen 	else S_STR("identity:", identity)
656933707f3Ssthen 	else S_STR("version:", version)
657191f22c6Ssthen 	else S_STR("http-user-agent:", http_user_agent)
6589982a05dSsthen 	else if(strcmp(opt, "nsid:") == 0) {
6599982a05dSsthen 		free(cfg->nsid_cfg_str);
6609982a05dSsthen 		if (!(cfg->nsid_cfg_str = strdup(val)))
6619982a05dSsthen 			return 0;
6629982a05dSsthen 		/* Empty string is just validly unsetting nsid */
6639982a05dSsthen 		if (*val == 0) {
6649982a05dSsthen 			free(cfg->nsid);
6659982a05dSsthen 			cfg->nsid = NULL;
6669982a05dSsthen 			cfg->nsid_len = 0;
6679982a05dSsthen 			return 1;
6689982a05dSsthen 		}
6699982a05dSsthen 		cfg->nsid = cfg_parse_nsid(val, &cfg->nsid_len);
6709982a05dSsthen 		return cfg->nsid != NULL;
6719982a05dSsthen 	}
672933707f3Ssthen 	else S_STRLIST("root-hints:", root_hints)
673933707f3Ssthen 	else S_STR("target-fetch-policy:", target_fetch_policy)
674933707f3Ssthen 	else S_YNO("harden-glue:", harden_glue)
675933707f3Ssthen 	else S_YNO("harden-short-bufsize:", harden_short_bufsize)
676933707f3Ssthen 	else S_YNO("harden-large-queries:", harden_large_queries)
677933707f3Ssthen 	else S_YNO("harden-dnssec-stripped:", harden_dnssec_stripped)
678933707f3Ssthen 	else S_YNO("harden-below-nxdomain:", harden_below_nxdomain)
679933707f3Ssthen 	else S_YNO("harden-referral-path:", harden_referral_path)
680a58bff56Ssthen 	else S_YNO("harden-algo-downgrade:", harden_algo_downgrade)
6818b7325afSsthen 	else S_YNO("harden-unknown-additional:", harden_unknown_additional)
68220237c55Ssthen 	else S_YNO("use-caps-for-id:", use_caps_bits_for_id)
683a58bff56Ssthen 	else S_STRLIST("caps-whitelist:", caps_whitelist)
684933707f3Ssthen 	else S_SIZET_OR_ZERO("unwanted-reply-threshold:", unwanted_threshold)
685933707f3Ssthen 	else S_STRLIST("private-address:", private_address)
686933707f3Ssthen 	else S_STRLIST("private-domain:", private_domain)
687933707f3Ssthen 	else S_YNO("do-not-query-localhost:", donotquery_localhost)
688933707f3Ssthen 	else S_STRLIST("do-not-query-address:", donotqueryaddrs)
689933707f3Ssthen 	else S_STRLIST("auto-trust-anchor-file:", auto_trust_anchor_file_list)
690933707f3Ssthen 	else S_STRLIST("trust-anchor-file:", trust_anchor_file_list)
691933707f3Ssthen 	else S_STRLIST("trust-anchor:", trust_anchor_list)
692933707f3Ssthen 	else S_STRLIST("trusted-keys-file:", trusted_keys_file_list)
6932be9e038Ssthen 	else S_YNO("trust-anchor-signaling:", trust_anchor_signaling)
69420237c55Ssthen 	else S_YNO("root-key-sentinel:", root_key_sentinel)
695933707f3Ssthen 	else S_STRLIST("domain-insecure:", domain_insecure)
696933707f3Ssthen 	else S_NUMBER_OR_ZERO("val-bogus-ttl:", bogus_ttl)
697933707f3Ssthen 	else S_YNO("val-clean-additional:", val_clean_additional)
698933707f3Ssthen 	else S_NUMBER_OR_ZERO("val-log-level:", val_log_level)
699933707f3Ssthen 	else S_YNO("val-log-squelch:", val_log_squelch)
700933707f3Ssthen 	else S_YNO("log-queries:", log_queries)
70177079be7Ssthen 	else S_YNO("log-replies:", log_replies)
702f6b99bafSsthen 	else S_YNO("log-tag-queryreply:", log_tag_queryreply)
7032308e98cSsthen 	else S_YNO("log-local-actions:", log_local_actions)
7042308e98cSsthen 	else S_YNO("log-servfail:", log_servfail)
705f46c52bfSsthen 	else S_YNO("log-destaddr:", log_destaddr)
706933707f3Ssthen 	else S_YNO("val-permissive-mode:", val_permissive_mode)
707938a3a5eSflorian 	else S_YNO("aggressive-nsec:", aggressive_nsec)
708933707f3Ssthen 	else S_YNO("ignore-cd-flag:", ignore_cd)
709d896b962Ssthen 	else S_YNO("disable-edns-do:", disable_edns_do)
710eaf2578eSsthen 	else if(strcmp(opt, "serve-expired:") == 0)
711eaf2578eSsthen 	{ IS_YES_OR_NO; cfg->serve_expired = (strcmp(val, "yes") == 0);
712eaf2578eSsthen 	  SERVE_EXPIRED = cfg->serve_expired; }
713eaf2578eSsthen 	else if(strcmp(opt, "serve-expired-ttl:") == 0)
7142308e98cSsthen 	{ IS_NUMBER_OR_ZERO; cfg->serve_expired_ttl = atoi(val); SERVE_EXPIRED_TTL=(time_t)cfg->serve_expired_ttl;}
7152308e98cSsthen 	else S_YNO("serve-expired-ttl-reset:", serve_expired_ttl_reset)
716eaf2578eSsthen 	else if(strcmp(opt, "serve-expired-reply-ttl:") == 0)
717eaf2578eSsthen 	{ IS_NUMBER_OR_ZERO; cfg->serve_expired_reply_ttl = atoi(val); SERVE_EXPIRED_REPLY_TTL=(time_t)cfg->serve_expired_reply_ttl;}
718eaf2578eSsthen 	else S_NUMBER_OR_ZERO("serve-expired-client-timeout:", serve_expired_client_timeout)
7190bdb4f62Ssthen 	else S_YNO("ede:", ede)
7200bdb4f62Ssthen 	else S_YNO("ede-serve-expired:", ede_serve_expired)
7219982a05dSsthen 	else S_YNO("serve-original-ttl:", serve_original_ttl)
722933707f3Ssthen 	else S_STR("val-nsec3-keysize-iterations:", val_nsec3_key_iterations)
723191f22c6Ssthen 	else S_YNO("zonemd-permissive-mode:", zonemd_permissive_mode)
724933707f3Ssthen 	else S_UNSIGNED_OR_ZERO("add-holddown:", add_holddown)
725933707f3Ssthen 	else S_UNSIGNED_OR_ZERO("del-holddown:", del_holddown)
726933707f3Ssthen 	else S_UNSIGNED_OR_ZERO("keep-missing:", keep_missing)
727a961b961Ssthen 	else if(strcmp(opt, "permit-small-holddown:") == 0)
728a961b961Ssthen 	{ IS_YES_OR_NO; cfg->permit_small_holddown = (strcmp(val, "yes") == 0);
729a961b961Ssthen 	  autr_permit_small_holddown = cfg->permit_small_holddown; }
730933707f3Ssthen 	else S_MEMSIZE("key-cache-size:", key_cache_size)
731933707f3Ssthen 	else S_POW2("key-cache-slabs:", key_cache_slabs)
732933707f3Ssthen 	else S_MEMSIZE("neg-cache-size:", neg_cache_size)
733d8d14d0cSsthen 	else S_YNO("minimal-responses:", minimal_responses)
734d8d14d0cSsthen 	else S_YNO("rrset-roundrobin:", rrset_roundrobin)
7353150e5f6Ssthen 	else S_NUMBER_OR_ZERO("unknown-server-time-limit:", unknown_server_time_limit)
7362bdc0ed1Ssthen 	else S_NUMBER_OR_ZERO("discard-timeout:", discard_timeout)
7372bdc0ed1Ssthen 	else S_NUMBER_OR_ZERO("wait-limit:", wait_limit)
7382bdc0ed1Ssthen 	else S_NUMBER_OR_ZERO("wait-limit-cookie:", wait_limit_cookie)
739933707f3Ssthen 	else S_STRLIST("local-data:", local_data)
740e10d3884Sbrad 	else S_YNO("unblock-lan-zones:", unblock_lan_zones)
74132e31f52Ssthen 	else S_YNO("insecure-lan-zones:", insecure_lan_zones)
742933707f3Ssthen 	else S_YNO("control-enable:", remote_control_enable)
74320237c55Ssthen 	else S_STRLIST_APPEND("control-interface:", control_ifs)
744933707f3Ssthen 	else S_NUMBER_NONZERO("control-port:", control_port)
745933707f3Ssthen 	else S_STR("server-key-file:", server_key_file)
746933707f3Ssthen 	else S_STR("server-cert-file:", server_cert_file)
747933707f3Ssthen 	else S_STR("control-key-file:", control_key_file)
748933707f3Ssthen 	else S_STR("control-cert-file:", control_cert_file)
749933707f3Ssthen 	else S_STR("module-config:", module_conf)
7508240c1b9Ssthen 	else S_STRLIST("python-script:", python_script)
751a3167c07Ssthen 	else S_STRLIST("dynlib-file:", dynlib_file)
7522ee382b6Ssthen 	else S_YNO("disable-dnssec-lame-check:", disable_dnssec_lame_check)
7532be9e038Ssthen #ifdef CLIENT_SUBNET
7542be9e038Ssthen 	/* Can't set max subnet prefix here, since that value is used when
7552be9e038Ssthen 	 * generating the address tree. */
7562be9e038Ssthen 	/* No client-subnet-always-forward here, module registration depends on
7572be9e038Ssthen 	 * this option. */
7582be9e038Ssthen #endif
7592be9e038Ssthen #ifdef USE_DNSTAP
7602be9e038Ssthen 	else S_YNO("dnstap-enable:", dnstap)
761a3167c07Ssthen 	else S_YNO("dnstap-bidirectional:", dnstap_bidirectional)
7622be9e038Ssthen 	else S_STR("dnstap-socket-path:", dnstap_socket_path)
763a3167c07Ssthen 	else S_STR("dnstap-ip:", dnstap_ip)
764a3167c07Ssthen 	else S_YNO("dnstap-tls:", dnstap_tls)
765a3167c07Ssthen 	else S_STR("dnstap-tls-server-name:", dnstap_tls_server_name)
766a3167c07Ssthen 	else S_STR("dnstap-tls-cert-bundle:", dnstap_tls_cert_bundle)
767a3167c07Ssthen 	else S_STR("dnstap-tls-client-key-file:", dnstap_tls_client_key_file)
768a3167c07Ssthen 	else S_STR("dnstap-tls-client-cert-file:",
769a3167c07Ssthen 		dnstap_tls_client_cert_file)
7702be9e038Ssthen 	else S_YNO("dnstap-send-identity:", dnstap_send_identity)
7712be9e038Ssthen 	else S_YNO("dnstap-send-version:", dnstap_send_version)
7722be9e038Ssthen 	else S_STR("dnstap-identity:", dnstap_identity)
7732be9e038Ssthen 	else S_STR("dnstap-version:", dnstap_version)
774*98bc733bSsthen 	else S_NUMBER_OR_ZERO("dnstap-sample-rate:", dnstap_sample_rate)
7752be9e038Ssthen 	else S_YNO("dnstap-log-resolver-query-messages:",
7762be9e038Ssthen 		dnstap_log_resolver_query_messages)
7772be9e038Ssthen 	else S_YNO("dnstap-log-resolver-response-messages:",
7782be9e038Ssthen 		dnstap_log_resolver_response_messages)
7792be9e038Ssthen 	else S_YNO("dnstap-log-client-query-messages:",
7802be9e038Ssthen 		dnstap_log_client_query_messages)
7812be9e038Ssthen 	else S_YNO("dnstap-log-client-response-messages:",
7822be9e038Ssthen 		dnstap_log_client_response_messages)
7832be9e038Ssthen 	else S_YNO("dnstap-log-forwarder-query-messages:",
7842be9e038Ssthen 		dnstap_log_forwarder_query_messages)
7852be9e038Ssthen 	else S_YNO("dnstap-log-forwarder-response-messages:",
7862be9e038Ssthen 		dnstap_log_forwarder_response_messages)
7872be9e038Ssthen #endif
7882be9e038Ssthen #ifdef USE_DNSCRYPT
7892be9e038Ssthen 	else S_YNO("dnscrypt-enable:", dnscrypt)
7902be9e038Ssthen 	else S_NUMBER_NONZERO("dnscrypt-port:", dnscrypt_port)
7912be9e038Ssthen 	else S_STR("dnscrypt-provider:", dnscrypt_provider)
792bdfc4d55Sflorian 	else S_STRLIST_UNIQ("dnscrypt-provider-cert:", dnscrypt_provider_cert)
793bdfc4d55Sflorian 	else S_STRLIST("dnscrypt-provider-cert-rotated:", dnscrypt_provider_cert_rotated)
794bdfc4d55Sflorian 	else S_STRLIST_UNIQ("dnscrypt-secret-key:", dnscrypt_secret_key)
7957191de28Ssthen 	else S_MEMSIZE("dnscrypt-shared-secret-cache-size:",
7967191de28Ssthen 		dnscrypt_shared_secret_cache_size)
7977191de28Ssthen 	else S_POW2("dnscrypt-shared-secret-cache-slabs:",
7987191de28Ssthen 		dnscrypt_shared_secret_cache_slabs)
799bdfc4d55Sflorian 	else S_MEMSIZE("dnscrypt-nonce-cache-size:",
800bdfc4d55Sflorian 		dnscrypt_nonce_cache_size)
801bdfc4d55Sflorian 	else S_POW2("dnscrypt-nonce-cache-slabs:",
802bdfc4d55Sflorian 		dnscrypt_nonce_cache_slabs)
8032be9e038Ssthen #endif
8048b7325afSsthen 	else if(strcmp(opt, "ip-ratelimit-cookie:") == 0) {
8058b7325afSsthen 	    IS_NUMBER_OR_ZERO; cfg->ip_ratelimit_cookie = atoi(val);
8068b7325afSsthen 	    infra_ip_ratelimit_cookie=cfg->ip_ratelimit_cookie;
8078b7325afSsthen 	}
80877079be7Ssthen 	else if(strcmp(opt, "ip-ratelimit:") == 0) {
80977079be7Ssthen 	    IS_NUMBER_OR_ZERO; cfg->ip_ratelimit = atoi(val);
81077079be7Ssthen 	    infra_ip_ratelimit=cfg->ip_ratelimit;
81177079be7Ssthen 	}
812a58bff56Ssthen 	else if(strcmp(opt, "ratelimit:") == 0) {
813a58bff56Ssthen 	    IS_NUMBER_OR_ZERO; cfg->ratelimit = atoi(val);
814a58bff56Ssthen 	    infra_dp_ratelimit=cfg->ratelimit;
815a58bff56Ssthen 	}
81677079be7Ssthen 	else S_MEMSIZE("ip-ratelimit-size:", ip_ratelimit_size)
817a58bff56Ssthen 	else S_MEMSIZE("ratelimit-size:", ratelimit_size)
81877079be7Ssthen 	else S_POW2("ip-ratelimit-slabs:", ip_ratelimit_slabs)
819a58bff56Ssthen 	else S_POW2("ratelimit-slabs:", ratelimit_slabs)
82077079be7Ssthen 	else S_NUMBER_OR_ZERO("ip-ratelimit-factor:", ip_ratelimit_factor)
821a58bff56Ssthen 	else S_NUMBER_OR_ZERO("ratelimit-factor:", ratelimit_factor)
822e21c60efSsthen 	else S_YNO("ip-ratelimit-backoff:", ip_ratelimit_backoff)
823e21c60efSsthen 	else S_YNO("ratelimit-backoff:", ratelimit_backoff)
824e21c60efSsthen 	else S_NUMBER_NONZERO("outbound-msg-retry:", outbound_msg_retry)
8258b7325afSsthen 	else S_NUMBER_NONZERO("max-sent-count:", max_sent_count)
8268b7325afSsthen 	else S_NUMBER_NONZERO("max-query-restarts:", max_query_restarts)
8273150e5f6Ssthen 	else S_SIZET_NONZERO("fast-server-num:", fast_server_num)
8283150e5f6Ssthen 	else S_NUMBER_OR_ZERO("fast-server-permil:", fast_server_permil)
82924893edcSsthen 	else S_YNO("qname-minimisation:", qname_minimisation)
83077079be7Ssthen 	else S_YNO("qname-minimisation-strict:", qname_minimisation_strict)
8319982a05dSsthen 	else S_YNO("pad-responses:", pad_responses)
8329982a05dSsthen 	else S_SIZET_NONZERO("pad-responses-block-size:", pad_responses_block_size)
8339982a05dSsthen 	else S_YNO("pad-queries:", pad_queries)
8349982a05dSsthen 	else S_SIZET_NONZERO("pad-queries-block-size:", pad_queries_block_size)
83545872187Ssthen 	else S_STRLIST("proxy-protocol-port:", proxy_protocol_port)
8362be9e038Ssthen #ifdef USE_IPSECMOD
8372be9e038Ssthen 	else S_YNO("ipsecmod-enabled:", ipsecmod_enabled)
8382be9e038Ssthen 	else S_YNO("ipsecmod-ignore-bogus:", ipsecmod_ignore_bogus)
8392be9e038Ssthen 	else if(strcmp(opt, "ipsecmod-max-ttl:") == 0)
8402be9e038Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->ipsecmod_max_ttl = atoi(val); }
8412be9e038Ssthen 	else S_YNO("ipsecmod-strict:", ipsecmod_strict)
8422be9e038Ssthen #endif
843*98bc733bSsthen 	else S_YNO("answer-cookie:", do_answer_cookie)
844*98bc733bSsthen 	else S_STR("cookie-secret-file:", cookie_secret_file)
845d896b962Ssthen #ifdef USE_CACHEDB
846d896b962Ssthen 	else S_YNO("cachedb-no-store:", cachedb_no_store)
8472bdc0ed1Ssthen 	else S_YNO("cachedb-check-when-serve-expired:", cachedb_check_when_serve_expired)
848d896b962Ssthen #endif /* USE_CACHEDB */
8492ee382b6Ssthen 	else if(strcmp(opt, "define-tag:") ==0) {
8502ee382b6Ssthen 		return config_add_tag(cfg, val);
851191f22c6Ssthen 	/* val_sig_skew_min, max and val_max_restart are copied into val_env
852191f22c6Ssthen 	 * during init so this does not update val_env with set_option */
8532ee382b6Ssthen 	} else if(strcmp(opt, "val-sig-skew-min:") == 0)
854229e174cSsthen 	{ IS_NUMBER_OR_ZERO; cfg->val_sig_skew_min = (int32_t)atoi(val); }
855229e174cSsthen 	else if(strcmp(opt, "val-sig-skew-max:") == 0)
856229e174cSsthen 	{ IS_NUMBER_OR_ZERO; cfg->val_sig_skew_max = (int32_t)atoi(val); }
857191f22c6Ssthen 	else if(strcmp(opt, "val-max-restart:") == 0)
858191f22c6Ssthen 	{ IS_NUMBER_OR_ZERO; cfg->val_max_restart = (int32_t)atoi(val); }
859933707f3Ssthen 	else if (strcmp(opt, "outgoing-interface:") == 0) {
860933707f3Ssthen 		char* d = strdup(val);
861a58bff56Ssthen 		char** oi =
862a58bff56Ssthen 		(char**)reallocarray(NULL, (size_t)cfg->num_out_ifs+1, sizeof(char*));
863933707f3Ssthen 		if(!d || !oi) { free(d); free(oi); return -1; }
864933707f3Ssthen 		if(cfg->out_ifs && cfg->num_out_ifs) {
865933707f3Ssthen 			memmove(oi, cfg->out_ifs, cfg->num_out_ifs*sizeof(char*));
866933707f3Ssthen 			free(cfg->out_ifs);
867933707f3Ssthen 		}
868933707f3Ssthen 		oi[cfg->num_out_ifs++] = d;
869933707f3Ssthen 		cfg->out_ifs = oi;
870933707f3Ssthen 	} else {
871933707f3Ssthen 		/* unknown or unsupported (from the set_option interface):
872933707f3Ssthen 		 * interface, outgoing-interface, access-control,
873933707f3Ssthen 		 * stub-zone, name, stub-addr, stub-host, stub-prime
87477079be7Ssthen 		 * forward-first, stub-first, forward-ssl-upstream,
875bdfc4d55Sflorian 		 * stub-ssl-upstream, forward-zone, auth-zone
87677079be7Ssthen 		 * name, forward-addr, forward-host,
8772ee382b6Ssthen 		 * ratelimit-for-domain, ratelimit-below-domain,
87845872187Ssthen 		 * local-zone-tag, access-control-view, interface-*,
8792be9e038Ssthen 		 * send-client-subnet, client-subnet-always-forward,
8803150e5f6Ssthen 		 * max-client-subnet-ipv4, max-client-subnet-ipv6,
8813150e5f6Ssthen 		 * min-client-subnet-ipv4, min-client-subnet-ipv6,
8823150e5f6Ssthen 		 * max-ecs-tree-size-ipv4, max-ecs-tree-size-ipv6, ipsecmod_hook,
8832be9e038Ssthen 		 * ipsecmod_whitelist. */
884933707f3Ssthen 		return 0;
885933707f3Ssthen 	}
886933707f3Ssthen 	return 1;
887933707f3Ssthen }
888933707f3Ssthen 
889933707f3Ssthen void config_print_func(char* line, void* arg)
890933707f3Ssthen {
891933707f3Ssthen 	FILE* f = (FILE*)arg;
892933707f3Ssthen 	(void)fprintf(f, "%s\n", line);
893933707f3Ssthen }
894933707f3Ssthen 
895933707f3Ssthen /** collate func arg */
896933707f3Ssthen struct config_collate_arg {
897933707f3Ssthen 	/** list of result items */
898933707f3Ssthen 	struct config_strlist_head list;
899933707f3Ssthen 	/** if a malloc error occurred, 0 is OK */
900933707f3Ssthen 	int status;
901933707f3Ssthen };
902933707f3Ssthen 
903933707f3Ssthen void config_collate_func(char* line, void* arg)
904933707f3Ssthen {
905933707f3Ssthen 	struct config_collate_arg* m = (struct config_collate_arg*)arg;
906933707f3Ssthen 	if(m->status)
907933707f3Ssthen 		return;
908933707f3Ssthen 	if(!cfg_strlist_append(&m->list, strdup(line)))
909933707f3Ssthen 		m->status = 1;
910933707f3Ssthen }
911933707f3Ssthen 
912933707f3Ssthen int config_get_option_list(struct config_file* cfg, const char* opt,
913933707f3Ssthen 	struct config_strlist** list)
914933707f3Ssthen {
915933707f3Ssthen 	struct config_collate_arg m;
916933707f3Ssthen 	memset(&m, 0, sizeof(m));
917933707f3Ssthen 	*list = NULL;
918933707f3Ssthen 	if(!config_get_option(cfg, opt, config_collate_func, &m))
919933707f3Ssthen 		return 1;
920933707f3Ssthen 	if(m.status) {
921933707f3Ssthen 		config_delstrlist(m.list.first);
922933707f3Ssthen 		return 2;
923933707f3Ssthen 	}
924933707f3Ssthen 	*list = m.list.first;
925933707f3Ssthen 	return 0;
926933707f3Ssthen }
927933707f3Ssthen 
928933707f3Ssthen int
929933707f3Ssthen config_get_option_collate(struct config_file* cfg, const char* opt, char** str)
930933707f3Ssthen {
931933707f3Ssthen 	struct config_strlist* list = NULL;
932933707f3Ssthen 	int r;
933933707f3Ssthen 	*str = NULL;
934933707f3Ssthen 	if((r = config_get_option_list(cfg, opt, &list)) != 0)
935933707f3Ssthen 		return r;
936933707f3Ssthen 	*str = config_collate_cat(list);
937933707f3Ssthen 	config_delstrlist(list);
938933707f3Ssthen 	if(!*str) return 2;
939933707f3Ssthen 	return 0;
940933707f3Ssthen }
941933707f3Ssthen 
942933707f3Ssthen char*
943933707f3Ssthen config_collate_cat(struct config_strlist* list)
944933707f3Ssthen {
945933707f3Ssthen 	size_t total = 0, left;
946933707f3Ssthen 	struct config_strlist* s;
947933707f3Ssthen 	char *r, *w;
948933707f3Ssthen 	if(!list) /* no elements */
949933707f3Ssthen 		return strdup("");
950933707f3Ssthen 	if(list->next == NULL) /* one element , no newline at end. */
951933707f3Ssthen 		return strdup(list->str);
952933707f3Ssthen 	/* count total length */
953933707f3Ssthen 	for(s=list; s; s=s->next)
954933707f3Ssthen 		total += strlen(s->str) + 1; /* len + newline */
955933707f3Ssthen 	left = total+1; /* one extra for nul at end */
956933707f3Ssthen 	r = malloc(left);
957933707f3Ssthen 	if(!r)
958933707f3Ssthen 		return NULL;
959933707f3Ssthen 	w = r;
960933707f3Ssthen 	for(s=list; s; s=s->next) {
961933707f3Ssthen 		size_t this = strlen(s->str);
962933707f3Ssthen 		if(this+2 > left) { /* sanity check */
963933707f3Ssthen 			free(r);
964933707f3Ssthen 			return NULL;
965933707f3Ssthen 		}
966933707f3Ssthen 		snprintf(w, left, "%s\n", s->str);
967229e174cSsthen 		this = strlen(w);
968229e174cSsthen 		w += this;
969229e174cSsthen 		left -= this;
970933707f3Ssthen 	}
971933707f3Ssthen 	return r;
972933707f3Ssthen }
973933707f3Ssthen 
974933707f3Ssthen /** compare and print decimal option */
975933707f3Ssthen #define O_DEC(opt, str, var) if(strcmp(opt, str)==0) \
976933707f3Ssthen 	{snprintf(buf, len, "%d", (int)cfg->var); \
977933707f3Ssthen 	func(buf, arg);}
978933707f3Ssthen /** compare and print unsigned option */
979933707f3Ssthen #define O_UNS(opt, str, var) if(strcmp(opt, str)==0) \
980933707f3Ssthen 	{snprintf(buf, len, "%u", (unsigned)cfg->var); \
981933707f3Ssthen 	func(buf, arg);}
982933707f3Ssthen /** compare and print yesno option */
983933707f3Ssthen #define O_YNO(opt, str, var) if(strcmp(opt, str)==0) \
984933707f3Ssthen 	{func(cfg->var?"yes":"no", arg);}
985933707f3Ssthen /** compare and print string option */
986933707f3Ssthen #define O_STR(opt, str, var) if(strcmp(opt, str)==0) \
987933707f3Ssthen 	{func(cfg->var?cfg->var:"", arg);}
988933707f3Ssthen /** compare and print array option */
989933707f3Ssthen #define O_IFC(opt, str, num, arr) if(strcmp(opt, str)==0) \
990933707f3Ssthen 	{int i; for(i=0; i<cfg->num; i++) func(cfg->arr[i], arg);}
991933707f3Ssthen /** compare and print memorysize option */
992933707f3Ssthen #define O_MEM(opt, str, var) if(strcmp(opt, str)==0) { \
993933707f3Ssthen 	if(cfg->var > 1024*1024*1024) {	\
994933707f3Ssthen 	  size_t f=cfg->var/(size_t)1000000, b=cfg->var%(size_t)1000000; \
995a58bff56Ssthen 	  snprintf(buf, len, "%u%6.6u", (unsigned)f, (unsigned)b); \
996a58bff56Ssthen 	} else snprintf(buf, len, "%u", (unsigned)cfg->var); \
997933707f3Ssthen 	func(buf, arg);}
998933707f3Ssthen /** compare and print list option */
999933707f3Ssthen #define O_LST(opt, name, lst) if(strcmp(opt, name)==0) { \
1000933707f3Ssthen 	struct config_strlist* p = cfg->lst; \
1001933707f3Ssthen 	for(p = cfg->lst; p; p = p->next) \
1002933707f3Ssthen 		func(p->str, arg); \
1003933707f3Ssthen 	}
1004933707f3Ssthen /** compare and print list option */
1005933707f3Ssthen #define O_LS2(opt, name, lst) if(strcmp(opt, name)==0) { \
1006933707f3Ssthen 	struct config_str2list* p = cfg->lst; \
10072ee382b6Ssthen 	for(p = cfg->lst; p; p = p->next) { \
10082ee382b6Ssthen 		snprintf(buf, len, "%s %s", p->str, p->str2); \
1009933707f3Ssthen 		func(buf, arg); \
10102ee382b6Ssthen 	} \
10112ee382b6Ssthen 	}
101277079be7Ssthen /** compare and print list option */
101377079be7Ssthen #define O_LS3(opt, name, lst) if(strcmp(opt, name)==0) { \
101477079be7Ssthen 	struct config_str3list* p = cfg->lst; \
101577079be7Ssthen 	for(p = cfg->lst; p; p = p->next) { \
101677079be7Ssthen 		snprintf(buf, len, "%s %s %s", p->str, p->str2, p->str3); \
101777079be7Ssthen 		func(buf, arg); \
101877079be7Ssthen 	} \
101977079be7Ssthen 	}
10202ee382b6Ssthen /** compare and print taglist option */
10212ee382b6Ssthen #define O_LTG(opt, name, lst) if(strcmp(opt, name)==0) { \
10222ee382b6Ssthen 	char* tmpstr = NULL; \
10232ee382b6Ssthen 	struct config_strbytelist *p = cfg->lst; \
10242ee382b6Ssthen 	for(p = cfg->lst; p; p = p->next) {\
10252ee382b6Ssthen 		tmpstr = config_taglist2str(cfg, p->str2, p->str2len); \
10262ee382b6Ssthen 		if(tmpstr) {\
10272ee382b6Ssthen 			snprintf(buf, len, "%s %s", p->str, tmpstr); \
10282ee382b6Ssthen 			func(buf, arg); \
10292ee382b6Ssthen 			free(tmpstr); \
10302ee382b6Ssthen 		} \
10312ee382b6Ssthen 	} \
1032933707f3Ssthen 	}
1033933707f3Ssthen 
1034933707f3Ssthen int
1035933707f3Ssthen config_get_option(struct config_file* cfg, const char* opt,
1036933707f3Ssthen 	void (*func)(char*,void*), void* arg)
1037933707f3Ssthen {
103820237c55Ssthen 	char buf[1024], nopt[64];
1039933707f3Ssthen 	size_t len = sizeof(buf);
1040452a1548Ssthen 	if(!opt) return 0;
104120237c55Ssthen 	if(opt && opt[strlen(opt)-1] == ':' && strlen(opt)<sizeof(nopt)) {
104220237c55Ssthen 		memmove(nopt, opt, strlen(opt));
104320237c55Ssthen 		nopt[strlen(opt)-1] = 0;
104420237c55Ssthen 		opt = nopt;
104520237c55Ssthen 	}
1046933707f3Ssthen 	fptr_ok(fptr_whitelist_print_func(func));
1047933707f3Ssthen 	O_DEC(opt, "verbosity", verbosity)
1048933707f3Ssthen 	else O_DEC(opt, "statistics-interval", stat_interval)
1049933707f3Ssthen 	else O_YNO(opt, "statistics-cumulative", stat_cumulative)
1050933707f3Ssthen 	else O_YNO(opt, "extended-statistics", stat_extended)
10518b7325afSsthen 	else O_YNO(opt, "statistics-inhibit-zero", stat_inhibit_zero)
10522be9e038Ssthen 	else O_YNO(opt, "shm-enable", shm_enable)
10532be9e038Ssthen 	else O_DEC(opt, "shm-key", shm_key)
1054933707f3Ssthen 	else O_YNO(opt, "use-syslog", use_syslog)
105577079be7Ssthen 	else O_STR(opt, "log-identity", log_identity)
1056229e174cSsthen 	else O_YNO(opt, "log-time-ascii", log_time_ascii)
1057933707f3Ssthen 	else O_DEC(opt, "num-threads", num_threads)
1058933707f3Ssthen 	else O_IFC(opt, "interface", num_ifs, ifs)
1059933707f3Ssthen 	else O_IFC(opt, "outgoing-interface", num_out_ifs, out_ifs)
1060933707f3Ssthen 	else O_YNO(opt, "interface-automatic", if_automatic)
10610bdb4f62Ssthen 	else O_STR(opt, "interface-automatic-ports", if_automatic_ports)
1062933707f3Ssthen 	else O_DEC(opt, "port", port)
1063933707f3Ssthen 	else O_DEC(opt, "outgoing-range", outgoing_num_ports)
1064933707f3Ssthen 	else O_DEC(opt, "outgoing-num-tcp", outgoing_num_tcp)
1065933707f3Ssthen 	else O_DEC(opt, "incoming-num-tcp", incoming_num_tcp)
1066f6b99bafSsthen 	else O_MEM(opt, "stream-wait-size", stream_wait_size)
1067933707f3Ssthen 	else O_DEC(opt, "edns-buffer-size", edns_buffer_size)
1068933707f3Ssthen 	else O_DEC(opt, "msg-buffer-size", msg_buffer_size)
1069933707f3Ssthen 	else O_MEM(opt, "msg-cache-size", msg_cache_size)
1070933707f3Ssthen 	else O_DEC(opt, "msg-cache-slabs", msg_cache_slabs)
1071933707f3Ssthen 	else O_DEC(opt, "num-queries-per-thread", num_queries_per_thread)
1072933707f3Ssthen 	else O_UNS(opt, "jostle-timeout", jostle_time)
1073933707f3Ssthen 	else O_MEM(opt, "so-rcvbuf", so_rcvbuf)
1074933707f3Ssthen 	else O_MEM(opt, "so-sndbuf", so_sndbuf)
10755d76a658Ssthen 	else O_YNO(opt, "so-reuseport", so_reuseport)
1076a58bff56Ssthen 	else O_YNO(opt, "ip-transparent", ip_transparent)
10772ee382b6Ssthen 	else O_YNO(opt, "ip-freebind", ip_freebind)
1078a3167c07Ssthen 	else O_DEC(opt, "ip-dscp", ip_dscp)
1079933707f3Ssthen 	else O_MEM(opt, "rrset-cache-size", rrset_cache_size)
1080933707f3Ssthen 	else O_DEC(opt, "rrset-cache-slabs", rrset_cache_slabs)
1081933707f3Ssthen 	else O_YNO(opt, "prefetch-key", prefetch_key)
1082933707f3Ssthen 	else O_YNO(opt, "prefetch", prefetch)
10833150e5f6Ssthen 	else O_YNO(opt, "deny-any", deny_any)
1084933707f3Ssthen 	else O_DEC(opt, "cache-max-ttl", max_ttl)
1085a58bff56Ssthen 	else O_DEC(opt, "cache-max-negative-ttl", max_negative_ttl)
10862bdc0ed1Ssthen 	else O_DEC(opt, "cache-min-negative-ttl", min_negative_ttl)
1087229e174cSsthen 	else O_DEC(opt, "cache-min-ttl", min_ttl)
1088933707f3Ssthen 	else O_DEC(opt, "infra-host-ttl", host_ttl)
1089933707f3Ssthen 	else O_DEC(opt, "infra-cache-slabs", infra_cache_slabs)
109031f127bbSsthen 	else O_DEC(opt, "infra-cache-min-rtt", infra_cache_min_rtt)
1091d1e2768aSsthen 	else O_UNS(opt, "infra-cache-max-rtt", infra_cache_max_rtt)
1092eba819a2Ssthen 	else O_YNO(opt, "infra-keep-probing", infra_keep_probing)
1093933707f3Ssthen 	else O_MEM(opt, "infra-cache-numhosts", infra_cache_numhosts)
10945d76a658Ssthen 	else O_UNS(opt, "delay-close", delay_close)
1095eba819a2Ssthen 	else O_YNO(opt, "udp-connect", udp_connect)
1096933707f3Ssthen 	else O_YNO(opt, "do-ip4", do_ip4)
1097933707f3Ssthen 	else O_YNO(opt, "do-ip6", do_ip6)
1098933707f3Ssthen 	else O_YNO(opt, "do-udp", do_udp)
1099933707f3Ssthen 	else O_YNO(opt, "do-tcp", do_tcp)
11002c144df0Ssthen 	else O_YNO(opt, "prefer-ip4", prefer_ip4)
11012c144df0Ssthen 	else O_YNO(opt, "prefer-ip6", prefer_ip6)
1102933707f3Ssthen 	else O_YNO(opt, "tcp-upstream", tcp_upstream)
1103bdfc4d55Sflorian 	else O_YNO(opt, "udp-upstream-without-downstream", udp_upstream_without_downstream)
110432e31f52Ssthen 	else O_DEC(opt, "tcp-mss", tcp_mss)
110532e31f52Ssthen 	else O_DEC(opt, "outgoing-tcp-mss", outgoing_tcp_mss)
1106191f22c6Ssthen 	else O_DEC(opt, "tcp-auth-query-timeout", tcp_auth_query_timeout)
11072308e98cSsthen 	else O_DEC(opt, "tcp-idle-timeout", tcp_idle_timeout)
1108191f22c6Ssthen 	else O_DEC(opt, "max-reuse-tcp-queries", max_reuse_tcp_queries)
1109191f22c6Ssthen 	else O_DEC(opt, "tcp-reuse-timeout", tcp_reuse_timeout)
11102308e98cSsthen 	else O_YNO(opt, "edns-tcp-keepalive", do_tcp_keepalive)
11112308e98cSsthen 	else O_DEC(opt, "edns-tcp-keepalive-timeout", tcp_keepalive_timeout)
11128b7325afSsthen 	else O_DEC(opt, "sock-queue-timeout", sock_queue_timeout)
1113933707f3Ssthen 	else O_YNO(opt, "ssl-upstream", ssl_upstream)
1114e21c60efSsthen 	else O_YNO(opt, "tls-upstream", ssl_upstream)
1115933707f3Ssthen 	else O_STR(opt, "ssl-service-key", ssl_service_key)
1116e21c60efSsthen 	else O_STR(opt, "tls-service-key", ssl_service_key)
1117933707f3Ssthen 	else O_STR(opt, "ssl-service-pem", ssl_service_pem)
1118e21c60efSsthen 	else O_STR(opt, "tls-service-pem", ssl_service_pem)
1119933707f3Ssthen 	else O_DEC(opt, "ssl-port", ssl_port)
1120e21c60efSsthen 	else O_DEC(opt, "tls-port", ssl_port)
1121e21c60efSsthen 	else O_STR(opt, "ssl-cert-bundle", tls_cert_bundle)
1122938a3a5eSflorian 	else O_STR(opt, "tls-cert-bundle", tls_cert_bundle)
112320237c55Ssthen 	else O_YNO(opt, "tls-win-cert", tls_win_cert)
11240bdb4f62Ssthen 	else O_YNO(opt, "tls-system-cert", tls_win_cert)
1125e21c60efSsthen 	else O_LST(opt, "additional-ssl-port", tls_additional_port)
1126e21c60efSsthen 	else O_LST(opt, "additional-tls-port", tls_additional_port)
1127e21c60efSsthen 	else O_LST(opt, "tls-additional-ports", tls_additional_port)
112820237c55Ssthen 	else O_LST(opt, "tls-additional-port", tls_additional_port)
1129f6b99bafSsthen 	else O_LST(opt, "tls-session-ticket-keys", tls_session_ticket_keys.first)
1130f6b99bafSsthen 	else O_STR(opt, "tls-ciphers", tls_ciphers)
1131f6b99bafSsthen 	else O_STR(opt, "tls-ciphersuites", tls_ciphersuites)
1132a3167c07Ssthen 	else O_YNO(opt, "tls-use-sni", tls_use_sni)
11332c144df0Ssthen 	else O_DEC(opt, "https-port", https_port)
11342c144df0Ssthen 	else O_STR(opt, "http-endpoint", http_endpoint)
11352c144df0Ssthen 	else O_UNS(opt, "http-max-streams", http_max_streams)
11362c144df0Ssthen 	else O_MEM(opt, "http-query-buffer-size", http_query_buffer_size)
11372c144df0Ssthen 	else O_MEM(opt, "http-response-buffer-size", http_response_buffer_size)
11382c144df0Ssthen 	else O_YNO(opt, "http-nodelay", http_nodelay)
1139eba819a2Ssthen 	else O_YNO(opt, "http-notls-downstream", http_notls_downstream)
114077079be7Ssthen 	else O_YNO(opt, "use-systemd", use_systemd)
1141933707f3Ssthen 	else O_YNO(opt, "do-daemonize", do_daemonize)
1142933707f3Ssthen 	else O_STR(opt, "chroot", chrootdir)
1143933707f3Ssthen 	else O_STR(opt, "username", username)
1144933707f3Ssthen 	else O_STR(opt, "directory", directory)
1145933707f3Ssthen 	else O_STR(opt, "logfile", logfile)
1146933707f3Ssthen 	else O_YNO(opt, "log-queries", log_queries)
114777079be7Ssthen 	else O_YNO(opt, "log-replies", log_replies)
1148f6b99bafSsthen 	else O_YNO(opt, "log-tag-queryreply", log_tag_queryreply)
11492308e98cSsthen 	else O_YNO(opt, "log-local-actions", log_local_actions)
11502308e98cSsthen 	else O_YNO(opt, "log-servfail", log_servfail)
1151f46c52bfSsthen 	else O_YNO(opt, "log-destaddr", log_destaddr)
1152933707f3Ssthen 	else O_STR(opt, "pidfile", pidfile)
1153933707f3Ssthen 	else O_YNO(opt, "hide-identity", hide_identity)
1154933707f3Ssthen 	else O_YNO(opt, "hide-version", hide_version)
11552be9e038Ssthen 	else O_YNO(opt, "hide-trustanchor", hide_trustanchor)
1156191f22c6Ssthen 	else O_YNO(opt, "hide-http-user-agent", hide_http_user_agent)
1157933707f3Ssthen 	else O_STR(opt, "identity", identity)
1158933707f3Ssthen 	else O_STR(opt, "version", version)
1159191f22c6Ssthen 	else O_STR(opt, "http-user-agent", http_user_agent)
11609982a05dSsthen 	else O_STR(opt, "nsid", nsid_cfg_str)
1161933707f3Ssthen 	else O_STR(opt, "target-fetch-policy", target_fetch_policy)
1162933707f3Ssthen 	else O_YNO(opt, "harden-short-bufsize", harden_short_bufsize)
1163933707f3Ssthen 	else O_YNO(opt, "harden-large-queries", harden_large_queries)
1164933707f3Ssthen 	else O_YNO(opt, "harden-glue", harden_glue)
1165933707f3Ssthen 	else O_YNO(opt, "harden-dnssec-stripped", harden_dnssec_stripped)
1166933707f3Ssthen 	else O_YNO(opt, "harden-below-nxdomain", harden_below_nxdomain)
1167933707f3Ssthen 	else O_YNO(opt, "harden-referral-path", harden_referral_path)
1168a58bff56Ssthen 	else O_YNO(opt, "harden-algo-downgrade", harden_algo_downgrade)
11698b7325afSsthen 	else O_YNO(opt, "harden-unknown-additional", harden_unknown_additional)
1170933707f3Ssthen 	else O_YNO(opt, "use-caps-for-id", use_caps_bits_for_id)
1171a58bff56Ssthen 	else O_LST(opt, "caps-whitelist", caps_whitelist)
1172933707f3Ssthen 	else O_DEC(opt, "unwanted-reply-threshold", unwanted_threshold)
1173933707f3Ssthen 	else O_YNO(opt, "do-not-query-localhost", donotquery_localhost)
1174933707f3Ssthen 	else O_STR(opt, "module-config", module_conf)
1175933707f3Ssthen 	else O_DEC(opt, "val-bogus-ttl", bogus_ttl)
1176933707f3Ssthen 	else O_YNO(opt, "val-clean-additional", val_clean_additional)
1177933707f3Ssthen 	else O_DEC(opt, "val-log-level", val_log_level)
1178933707f3Ssthen 	else O_YNO(opt, "val-permissive-mode", val_permissive_mode)
117920237c55Ssthen 	else O_YNO(opt, "aggressive-nsec", aggressive_nsec)
1180933707f3Ssthen 	else O_YNO(opt, "ignore-cd-flag", ignore_cd)
1181d896b962Ssthen 	else O_YNO(opt, "disable-edns-do", disable_edns_do)
118277079be7Ssthen 	else O_YNO(opt, "serve-expired", serve_expired)
11832308e98cSsthen 	else O_DEC(opt, "serve-expired-ttl", serve_expired_ttl)
11842308e98cSsthen 	else O_YNO(opt, "serve-expired-ttl-reset", serve_expired_ttl_reset)
1185eaf2578eSsthen 	else O_DEC(opt, "serve-expired-reply-ttl", serve_expired_reply_ttl)
1186eaf2578eSsthen 	else O_DEC(opt, "serve-expired-client-timeout", serve_expired_client_timeout)
11870bdb4f62Ssthen 	else O_YNO(opt, "ede", ede)
11880bdb4f62Ssthen 	else O_YNO(opt, "ede-serve-expired", ede_serve_expired)
11899982a05dSsthen 	else O_YNO(opt, "serve-original-ttl", serve_original_ttl)
1190933707f3Ssthen 	else O_STR(opt, "val-nsec3-keysize-iterations",val_nsec3_key_iterations)
1191191f22c6Ssthen 	else O_YNO(opt, "zonemd-permissive-mode", zonemd_permissive_mode)
1192933707f3Ssthen 	else O_UNS(opt, "add-holddown", add_holddown)
1193933707f3Ssthen 	else O_UNS(opt, "del-holddown", del_holddown)
1194933707f3Ssthen 	else O_UNS(opt, "keep-missing", keep_missing)
1195a961b961Ssthen 	else O_YNO(opt, "permit-small-holddown", permit_small_holddown)
1196933707f3Ssthen 	else O_MEM(opt, "key-cache-size", key_cache_size)
1197933707f3Ssthen 	else O_DEC(opt, "key-cache-slabs", key_cache_slabs)
1198933707f3Ssthen 	else O_MEM(opt, "neg-cache-size", neg_cache_size)
1199933707f3Ssthen 	else O_YNO(opt, "control-enable", remote_control_enable)
1200933707f3Ssthen 	else O_DEC(opt, "control-port", control_port)
1201933707f3Ssthen 	else O_STR(opt, "server-key-file", server_key_file)
1202933707f3Ssthen 	else O_STR(opt, "server-cert-file", server_cert_file)
1203933707f3Ssthen 	else O_STR(opt, "control-key-file", control_key_file)
1204933707f3Ssthen 	else O_STR(opt, "control-cert-file", control_cert_file)
1205933707f3Ssthen 	else O_LST(opt, "root-hints", root_hints)
1206933707f3Ssthen 	else O_LS2(opt, "access-control", acls)
12072308e98cSsthen 	else O_LS2(opt, "tcp-connection-limit", tcp_connection_limits)
1208933707f3Ssthen 	else O_LST(opt, "do-not-query-address", donotqueryaddrs)
1209933707f3Ssthen 	else O_LST(opt, "private-address", private_address)
1210933707f3Ssthen 	else O_LST(opt, "private-domain", private_domain)
1211933707f3Ssthen 	else O_LST(opt, "auto-trust-anchor-file", auto_trust_anchor_file_list)
1212933707f3Ssthen 	else O_LST(opt, "trust-anchor-file", trust_anchor_file_list)
1213933707f3Ssthen 	else O_LST(opt, "trust-anchor", trust_anchor_list)
1214933707f3Ssthen 	else O_LST(opt, "trusted-keys-file", trusted_keys_file_list)
12152be9e038Ssthen 	else O_YNO(opt, "trust-anchor-signaling", trust_anchor_signaling)
121620237c55Ssthen 	else O_YNO(opt, "root-key-sentinel", root_key_sentinel)
121720237c55Ssthen 	else O_LST(opt, "control-interface", control_ifs.first)
1218933707f3Ssthen 	else O_LST(opt, "domain-insecure", domain_insecure)
1219933707f3Ssthen 	else O_UNS(opt, "val-override-date", val_date_override)
1220d8d14d0cSsthen 	else O_YNO(opt, "minimal-responses", minimal_responses)
1221d8d14d0cSsthen 	else O_YNO(opt, "rrset-roundrobin", rrset_roundrobin)
12223150e5f6Ssthen 	else O_DEC(opt, "unknown-server-time-limit", unknown_server_time_limit)
12232bdc0ed1Ssthen 	else O_DEC(opt, "discard-timeout", discard_timeout)
12242bdc0ed1Ssthen 	else O_DEC(opt, "wait-limit", wait_limit)
12252bdc0ed1Ssthen 	else O_DEC(opt, "wait-limit-cookie", wait_limit_cookie)
12262bdc0ed1Ssthen 	else O_LS2(opt, "wait-limit-netblock", wait_limit_netblock)
12272bdc0ed1Ssthen 	else O_LS2(opt, "wait-limit-cookie-netblock", wait_limit_cookie_netblock)
12282be9e038Ssthen #ifdef CLIENT_SUBNET
12292be9e038Ssthen 	else O_LST(opt, "send-client-subnet", client_subnet)
12302be9e038Ssthen 	else O_LST(opt, "client-subnet-zone", client_subnet_zone)
12312be9e038Ssthen 	else O_DEC(opt, "max-client-subnet-ipv4", max_client_subnet_ipv4)
12322be9e038Ssthen 	else O_DEC(opt, "max-client-subnet-ipv6", max_client_subnet_ipv6)
12333150e5f6Ssthen 	else O_DEC(opt, "min-client-subnet-ipv4", min_client_subnet_ipv4)
12343150e5f6Ssthen 	else O_DEC(opt, "min-client-subnet-ipv6", min_client_subnet_ipv6)
12353150e5f6Ssthen 	else O_DEC(opt, "max-ecs-tree-size-ipv4", max_ecs_tree_size_ipv4)
12363150e5f6Ssthen 	else O_DEC(opt, "max-ecs-tree-size-ipv6", max_ecs_tree_size_ipv6)
12372be9e038Ssthen 	else O_YNO(opt, "client-subnet-always-forward:",
12382be9e038Ssthen 		client_subnet_always_forward)
12392be9e038Ssthen #endif
12402be9e038Ssthen #ifdef USE_DNSTAP
12412be9e038Ssthen 	else O_YNO(opt, "dnstap-enable", dnstap)
1242a3167c07Ssthen 	else O_YNO(opt, "dnstap-bidirectional", dnstap_bidirectional)
12432be9e038Ssthen 	else O_STR(opt, "dnstap-socket-path", dnstap_socket_path)
1244a3167c07Ssthen 	else O_STR(opt, "dnstap-ip", dnstap_ip)
1245a3167c07Ssthen 	else O_YNO(opt, "dnstap-tls", dnstap_tls)
1246a3167c07Ssthen 	else O_STR(opt, "dnstap-tls-server-name", dnstap_tls_server_name)
1247a3167c07Ssthen 	else O_STR(opt, "dnstap-tls-cert-bundle", dnstap_tls_cert_bundle)
1248a3167c07Ssthen 	else O_STR(opt, "dnstap-tls-client-key-file",
1249a3167c07Ssthen 		dnstap_tls_client_key_file)
1250a3167c07Ssthen 	else O_STR(opt, "dnstap-tls-client-cert-file",
1251a3167c07Ssthen 		dnstap_tls_client_cert_file)
12522be9e038Ssthen 	else O_YNO(opt, "dnstap-send-identity", dnstap_send_identity)
12532be9e038Ssthen 	else O_YNO(opt, "dnstap-send-version", dnstap_send_version)
12542be9e038Ssthen 	else O_STR(opt, "dnstap-identity", dnstap_identity)
12552be9e038Ssthen 	else O_STR(opt, "dnstap-version", dnstap_version)
1256*98bc733bSsthen 	else O_UNS(opt, "dnstap-sample-rate", dnstap_sample_rate)
12572be9e038Ssthen 	else O_YNO(opt, "dnstap-log-resolver-query-messages",
12582be9e038Ssthen 		dnstap_log_resolver_query_messages)
12592be9e038Ssthen 	else O_YNO(opt, "dnstap-log-resolver-response-messages",
12602be9e038Ssthen 		dnstap_log_resolver_response_messages)
12612be9e038Ssthen 	else O_YNO(opt, "dnstap-log-client-query-messages",
12622be9e038Ssthen 		dnstap_log_client_query_messages)
12632be9e038Ssthen 	else O_YNO(opt, "dnstap-log-client-response-messages",
12642be9e038Ssthen 		dnstap_log_client_response_messages)
12652be9e038Ssthen 	else O_YNO(opt, "dnstap-log-forwarder-query-messages",
12662be9e038Ssthen 		dnstap_log_forwarder_query_messages)
12672be9e038Ssthen 	else O_YNO(opt, "dnstap-log-forwarder-response-messages",
12682be9e038Ssthen 		dnstap_log_forwarder_response_messages)
12692be9e038Ssthen #endif
12702be9e038Ssthen #ifdef USE_DNSCRYPT
12712be9e038Ssthen 	else O_YNO(opt, "dnscrypt-enable", dnscrypt)
12722be9e038Ssthen 	else O_DEC(opt, "dnscrypt-port", dnscrypt_port)
12732be9e038Ssthen 	else O_STR(opt, "dnscrypt-provider", dnscrypt_provider)
12742be9e038Ssthen 	else O_LST(opt, "dnscrypt-provider-cert", dnscrypt_provider_cert)
1275bdfc4d55Sflorian 	else O_LST(opt, "dnscrypt-provider-cert-rotated", dnscrypt_provider_cert_rotated)
12762be9e038Ssthen 	else O_LST(opt, "dnscrypt-secret-key", dnscrypt_secret_key)
12777191de28Ssthen 	else O_MEM(opt, "dnscrypt-shared-secret-cache-size",
12787191de28Ssthen 		dnscrypt_shared_secret_cache_size)
12797191de28Ssthen 	else O_DEC(opt, "dnscrypt-shared-secret-cache-slabs",
12807191de28Ssthen 		dnscrypt_shared_secret_cache_slabs)
1281bdfc4d55Sflorian 	else O_MEM(opt, "dnscrypt-nonce-cache-size",
1282bdfc4d55Sflorian 		dnscrypt_nonce_cache_size)
1283bdfc4d55Sflorian 	else O_DEC(opt, "dnscrypt-nonce-cache-slabs",
1284bdfc4d55Sflorian 		dnscrypt_nonce_cache_slabs)
12852be9e038Ssthen #endif
1286e10d3884Sbrad 	else O_YNO(opt, "unblock-lan-zones", unblock_lan_zones)
128732e31f52Ssthen 	else O_YNO(opt, "insecure-lan-zones", insecure_lan_zones)
1288229e174cSsthen 	else O_DEC(opt, "max-udp-size", max_udp_size)
12898240c1b9Ssthen 	else O_LST(opt, "python-script", python_script)
1290a3167c07Ssthen 	else O_LST(opt, "dynlib-file", dynlib_file)
12912ee382b6Ssthen 	else O_YNO(opt, "disable-dnssec-lame-check", disable_dnssec_lame_check)
12928b7325afSsthen 	else O_DEC(opt, "ip-ratelimit-cookie", ip_ratelimit_cookie)
129377079be7Ssthen 	else O_DEC(opt, "ip-ratelimit", ip_ratelimit)
1294a58bff56Ssthen 	else O_DEC(opt, "ratelimit", ratelimit)
129577079be7Ssthen 	else O_MEM(opt, "ip-ratelimit-size", ip_ratelimit_size)
1296a58bff56Ssthen 	else O_MEM(opt, "ratelimit-size", ratelimit_size)
129777079be7Ssthen 	else O_DEC(opt, "ip-ratelimit-slabs", ip_ratelimit_slabs)
1298a58bff56Ssthen 	else O_DEC(opt, "ratelimit-slabs", ratelimit_slabs)
1299a58bff56Ssthen 	else O_LS2(opt, "ratelimit-for-domain", ratelimit_for_domain)
1300a58bff56Ssthen 	else O_LS2(opt, "ratelimit-below-domain", ratelimit_below_domain)
130177079be7Ssthen 	else O_DEC(opt, "ip-ratelimit-factor", ip_ratelimit_factor)
1302a58bff56Ssthen 	else O_DEC(opt, "ratelimit-factor", ratelimit_factor)
1303e21c60efSsthen 	else O_YNO(opt, "ip-ratelimit-backoff", ip_ratelimit_backoff)
1304e21c60efSsthen 	else O_YNO(opt, "ratelimit-backoff", ratelimit_backoff)
1305e21c60efSsthen 	else O_UNS(opt, "outbound-msg-retry", outbound_msg_retry)
13068b7325afSsthen 	else O_UNS(opt, "max-sent-count", max_sent_count)
13078b7325afSsthen 	else O_UNS(opt, "max-query-restarts", max_query_restarts)
13083150e5f6Ssthen 	else O_DEC(opt, "fast-server-num", fast_server_num)
13093150e5f6Ssthen 	else O_DEC(opt, "fast-server-permil", fast_server_permil)
1310229e174cSsthen 	else O_DEC(opt, "val-sig-skew-min", val_sig_skew_min)
1311229e174cSsthen 	else O_DEC(opt, "val-sig-skew-max", val_sig_skew_max)
1312191f22c6Ssthen 	else O_DEC(opt, "val-max-restart", val_max_restart)
131324893edcSsthen 	else O_YNO(opt, "qname-minimisation", qname_minimisation)
131477079be7Ssthen 	else O_YNO(opt, "qname-minimisation-strict", qname_minimisation_strict)
13152ee382b6Ssthen 	else O_IFC(opt, "define-tag", num_tags, tagname)
13162ee382b6Ssthen 	else O_LTG(opt, "local-zone-tag", local_zone_tags)
131777079be7Ssthen 	else O_LTG(opt, "access-control-tag", acl_tags)
13182be9e038Ssthen 	else O_LTG(opt, "response-ip-tag", respip_tags)
131977079be7Ssthen 	else O_LS3(opt, "local-zone-override", local_zone_overrides)
132077079be7Ssthen 	else O_LS3(opt, "access-control-tag-action", acl_tag_actions)
132177079be7Ssthen 	else O_LS3(opt, "access-control-tag-data", acl_tag_datas)
132277079be7Ssthen 	else O_LS2(opt, "access-control-view", acl_view)
132345872187Ssthen 	else O_LS2(opt, "interface-action", interface_actions)
132445872187Ssthen 	else O_LTG(opt, "interface-tag", interface_tags)
132545872187Ssthen 	else O_LS3(opt, "interface-tag-action", interface_tag_actions)
132645872187Ssthen 	else O_LS3(opt, "interface-tag-data", interface_tag_datas)
132745872187Ssthen 	else O_LS2(opt, "interface-view", interface_view)
13289982a05dSsthen 	else O_YNO(opt, "pad-responses", pad_responses)
13299982a05dSsthen 	else O_DEC(opt, "pad-responses-block-size", pad_responses_block_size)
13309982a05dSsthen 	else O_YNO(opt, "pad-queries", pad_queries)
13319982a05dSsthen 	else O_DEC(opt, "pad-queries-block-size", pad_queries_block_size)
1332eba819a2Ssthen 	else O_LS2(opt, "edns-client-strings", edns_client_strings)
133345872187Ssthen 	else O_LST(opt, "proxy-protocol-port", proxy_protocol_port)
13342be9e038Ssthen #ifdef USE_IPSECMOD
13352be9e038Ssthen 	else O_YNO(opt, "ipsecmod-enabled", ipsecmod_enabled)
13362be9e038Ssthen 	else O_YNO(opt, "ipsecmod-ignore-bogus", ipsecmod_ignore_bogus)
13372be9e038Ssthen 	else O_STR(opt, "ipsecmod-hook", ipsecmod_hook)
13382be9e038Ssthen 	else O_DEC(opt, "ipsecmod-max-ttl", ipsecmod_max_ttl)
13392be9e038Ssthen 	else O_LST(opt, "ipsecmod-whitelist", ipsecmod_whitelist)
13402be9e038Ssthen 	else O_YNO(opt, "ipsecmod-strict", ipsecmod_strict)
13412be9e038Ssthen #endif
1342*98bc733bSsthen 	else O_YNO(opt, "answer-cookie", do_answer_cookie)
1343*98bc733bSsthen 	else O_STR(opt, "cookie-secret-file", cookie_secret_file)
13447191de28Ssthen #ifdef USE_CACHEDB
13457191de28Ssthen 	else O_STR(opt, "backend", cachedb_backend)
13467191de28Ssthen 	else O_STR(opt, "secret-seed", cachedb_secret)
1347d896b962Ssthen 	else O_YNO(opt, "cachedb-no-store", cachedb_no_store)
13482bdc0ed1Ssthen 	else O_YNO(opt, "cachedb-check-when-serve-expired", cachedb_check_when_serve_expired)
1349eaf2578eSsthen #ifdef USE_REDIS
1350eaf2578eSsthen 	else O_STR(opt, "redis-server-host", redis_server_host)
1351eaf2578eSsthen 	else O_DEC(opt, "redis-server-port", redis_server_port)
13528b7325afSsthen 	else O_STR(opt, "redis-server-path", redis_server_path)
13538b7325afSsthen 	else O_STR(opt, "redis-server-password", redis_server_password)
1354eaf2578eSsthen 	else O_DEC(opt, "redis-timeout", redis_timeout)
1355a3167c07Ssthen 	else O_YNO(opt, "redis-expire-records", redis_expire_records)
1356d896b962Ssthen 	else O_DEC(opt, "redis-logical-db", redis_logical_db)
1357eaf2578eSsthen #endif  /* USE_REDIS */
1358eaf2578eSsthen #endif  /* USE_CACHEDB */
13598240c1b9Ssthen #ifdef USE_IPSET
13608240c1b9Ssthen 	else O_STR(opt, "name-v4", ipset_name_v4)
13618240c1b9Ssthen 	else O_STR(opt, "name-v6", ipset_name_v6)
13628240c1b9Ssthen #endif
1363933707f3Ssthen 	/* not here:
1364933707f3Ssthen 	 * outgoing-permit, outgoing-avoid - have list of ports
1365933707f3Ssthen 	 * local-zone - zones and nodefault variables
1366933707f3Ssthen 	 * local-data - see below
1367933707f3Ssthen 	 * local-data-ptr - converted to local-data entries
1368933707f3Ssthen 	 * stub-zone, name, stub-addr, stub-host, stub-prime
1369933707f3Ssthen 	 * forward-zone, name, forward-addr, forward-host
1370933707f3Ssthen 	 */
1371933707f3Ssthen 	else return 0;
1372933707f3Ssthen 	return 1;
1373933707f3Ssthen }
1374933707f3Ssthen 
1375933707f3Ssthen /** initialize the global cfg_parser object */
1376933707f3Ssthen static void
1377933707f3Ssthen create_cfg_parser(struct config_file* cfg, char* filename, const char* chroot)
1378933707f3Ssthen {
1379933707f3Ssthen 	static struct config_parser_state st;
1380933707f3Ssthen 	cfg_parser = &st;
1381933707f3Ssthen 	cfg_parser->filename = filename;
1382933707f3Ssthen 	cfg_parser->line = 1;
1383933707f3Ssthen 	cfg_parser->errors = 0;
1384933707f3Ssthen 	cfg_parser->cfg = cfg;
1385933707f3Ssthen 	cfg_parser->chroot = chroot;
138645872187Ssthen 	cfg_parser->started_toplevel = 0;
1387229e174cSsthen 	init_cfg_parse();
1388933707f3Ssthen }
1389933707f3Ssthen 
1390933707f3Ssthen int
1391933707f3Ssthen config_read(struct config_file* cfg, const char* filename, const char* chroot)
1392933707f3Ssthen {
1393933707f3Ssthen 	FILE *in;
1394933707f3Ssthen 	char *fname = (char*)filename;
1395229e174cSsthen #ifdef HAVE_GLOB
1396229e174cSsthen 	glob_t g;
1397229e174cSsthen 	size_t i;
1398229e174cSsthen 	int r, flags;
1399229e174cSsthen #endif
1400933707f3Ssthen 	if(!fname)
1401933707f3Ssthen 		return 1;
1402229e174cSsthen 
1403229e174cSsthen 	/* check for wildcards */
1404229e174cSsthen #ifdef HAVE_GLOB
1405229e174cSsthen 	if(!(!strchr(fname, '*') && !strchr(fname, '?') && !strchr(fname, '[') &&
1406229e174cSsthen 		!strchr(fname, '{') && !strchr(fname, '~'))) {
1407229e174cSsthen 		verbose(VERB_QUERY, "wildcard found, processing %s", fname);
1408229e174cSsthen 		flags = 0
1409229e174cSsthen #ifdef GLOB_ERR
1410229e174cSsthen 			| GLOB_ERR
1411229e174cSsthen #endif
1412229e174cSsthen #ifdef GLOB_NOSORT
1413229e174cSsthen 			| GLOB_NOSORT
1414229e174cSsthen #endif
1415229e174cSsthen #ifdef GLOB_BRACE
1416229e174cSsthen 			| GLOB_BRACE
1417229e174cSsthen #endif
1418229e174cSsthen #ifdef GLOB_TILDE
1419229e174cSsthen 			| GLOB_TILDE
1420229e174cSsthen #endif
1421229e174cSsthen 		;
1422229e174cSsthen 		memset(&g, 0, sizeof(g));
1423229e174cSsthen 		r = glob(fname, flags, NULL, &g);
1424229e174cSsthen 		if(r) {
1425229e174cSsthen 			/* some error */
1426229e174cSsthen 			globfree(&g);
1427229e174cSsthen 			if(r == GLOB_NOMATCH) {
1428229e174cSsthen 				verbose(VERB_QUERY, "include: "
1429229e174cSsthen 				"no matches for %s", fname);
1430229e174cSsthen 				return 1;
1431229e174cSsthen 			} else if(r == GLOB_NOSPACE) {
1432229e174cSsthen 				log_err("include: %s: "
1433229e174cSsthen 					"fnametern out of memory", fname);
1434229e174cSsthen 			} else if(r == GLOB_ABORTED) {
1435229e174cSsthen 				log_err("wildcard include: %s: expansion "
1436229e174cSsthen 					"aborted (%s)", fname, strerror(errno));
1437229e174cSsthen 			} else {
1438229e174cSsthen 				log_err("wildcard include: %s: expansion "
1439229e174cSsthen 					"failed (%s)", fname, strerror(errno));
1440229e174cSsthen 			}
1441229e174cSsthen 			/* ignore globs that yield no files */
1442229e174cSsthen 			return 1;
1443229e174cSsthen 		}
1444229e174cSsthen 		/* process files found, if any */
1445229e174cSsthen 		for(i=0; i<(size_t)g.gl_pathc; i++) {
1446229e174cSsthen 			if(!config_read(cfg, g.gl_pathv[i], chroot)) {
1447229e174cSsthen 				log_err("error reading wildcard "
1448229e174cSsthen 					"include: %s", g.gl_pathv[i]);
1449229e174cSsthen 				globfree(&g);
1450229e174cSsthen 				return 0;
1451229e174cSsthen 			}
1452229e174cSsthen 		}
1453229e174cSsthen 		globfree(&g);
1454229e174cSsthen 		return 1;
1455229e174cSsthen 	}
1456229e174cSsthen #endif /* HAVE_GLOB */
1457229e174cSsthen 
1458933707f3Ssthen 	in = fopen(fname, "r");
1459933707f3Ssthen 	if(!in) {
1460933707f3Ssthen 		log_err("Could not open %s: %s", fname, strerror(errno));
1461933707f3Ssthen 		return 0;
1462933707f3Ssthen 	}
1463933707f3Ssthen 	create_cfg_parser(cfg, fname, chroot);
1464933707f3Ssthen 	ub_c_in = in;
1465933707f3Ssthen 	ub_c_parse();
1466933707f3Ssthen 	fclose(in);
1467933707f3Ssthen 
14682be9e038Ssthen 	if(!cfg->dnscrypt) cfg->dnscrypt_port = 0;
14692be9e038Ssthen 
1470933707f3Ssthen 	if(cfg_parser->errors != 0) {
1471933707f3Ssthen 		fprintf(stderr, "read %s failed: %d errors in configuration file\n",
1472e1fa32daSsthen 			fname, cfg_parser->errors);
1473933707f3Ssthen 		errno=EINVAL;
1474933707f3Ssthen 		return 0;
1475933707f3Ssthen 	}
147631f127bbSsthen 
1477933707f3Ssthen 	return 1;
1478933707f3Ssthen }
1479933707f3Ssthen 
148032e31f52Ssthen struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm)
148132e31f52Ssthen {
148232e31f52Ssthen 	struct config_stub* p = *(*pp);
148332e31f52Ssthen 	while(p) {
148432e31f52Ssthen 		if(strcmp(p->name, nm) == 0)
148532e31f52Ssthen 			return p;
148632e31f52Ssthen 		(*pp) = &p->next;
148732e31f52Ssthen 		p = p->next;
148832e31f52Ssthen 	}
148932e31f52Ssthen 	return NULL;
149032e31f52Ssthen }
149132e31f52Ssthen 
1492933707f3Ssthen void
1493933707f3Ssthen config_delstrlist(struct config_strlist* p)
1494933707f3Ssthen {
1495933707f3Ssthen 	struct config_strlist *np;
1496933707f3Ssthen 	while(p) {
1497933707f3Ssthen 		np = p->next;
1498933707f3Ssthen 		free(p->str);
1499933707f3Ssthen 		free(p);
1500933707f3Ssthen 		p = np;
1501933707f3Ssthen 	}
1502933707f3Ssthen }
1503933707f3Ssthen 
1504933707f3Ssthen void
1505933707f3Ssthen config_deldblstrlist(struct config_str2list* p)
1506933707f3Ssthen {
1507933707f3Ssthen 	struct config_str2list *np;
1508933707f3Ssthen 	while(p) {
1509933707f3Ssthen 		np = p->next;
1510933707f3Ssthen 		free(p->str);
1511933707f3Ssthen 		free(p->str2);
1512933707f3Ssthen 		free(p);
1513933707f3Ssthen 		p = np;
1514933707f3Ssthen 	}
1515933707f3Ssthen }
1516933707f3Ssthen 
1517933707f3Ssthen void
151877079be7Ssthen config_deltrplstrlist(struct config_str3list* p)
151977079be7Ssthen {
152077079be7Ssthen 	struct config_str3list *np;
152177079be7Ssthen 	while(p) {
152277079be7Ssthen 		np = p->next;
152377079be7Ssthen 		free(p->str);
152477079be7Ssthen 		free(p->str2);
152577079be7Ssthen 		free(p->str3);
152677079be7Ssthen 		free(p);
152777079be7Ssthen 		p = np;
152877079be7Ssthen 	}
152977079be7Ssthen }
153077079be7Ssthen 
153177079be7Ssthen void
1532bdfc4d55Sflorian config_delauth(struct config_auth* p)
1533bdfc4d55Sflorian {
1534bdfc4d55Sflorian 	if(!p) return;
1535bdfc4d55Sflorian 	free(p->name);
1536bdfc4d55Sflorian 	config_delstrlist(p->masters);
1537bdfc4d55Sflorian 	config_delstrlist(p->urls);
153820237c55Ssthen 	config_delstrlist(p->allow_notify);
1539bdfc4d55Sflorian 	free(p->zonefile);
1540eaf2578eSsthen 	free(p->rpz_taglist);
1541eaf2578eSsthen 	free(p->rpz_action_override);
1542eaf2578eSsthen 	free(p->rpz_cname);
1543eaf2578eSsthen 	free(p->rpz_log_name);
1544bdfc4d55Sflorian 	free(p);
1545bdfc4d55Sflorian }
1546bdfc4d55Sflorian 
1547bdfc4d55Sflorian void
1548bdfc4d55Sflorian config_delauths(struct config_auth* p)
1549bdfc4d55Sflorian {
1550bdfc4d55Sflorian 	struct config_auth* np;
1551bdfc4d55Sflorian 	while(p) {
1552bdfc4d55Sflorian 		np = p->next;
1553bdfc4d55Sflorian 		config_delauth(p);
1554bdfc4d55Sflorian 		p = np;
1555bdfc4d55Sflorian 	}
1556bdfc4d55Sflorian }
1557bdfc4d55Sflorian 
1558bdfc4d55Sflorian void
155932e31f52Ssthen config_delstub(struct config_stub* p)
156032e31f52Ssthen {
156132e31f52Ssthen 	if(!p) return;
156232e31f52Ssthen 	free(p->name);
156332e31f52Ssthen 	config_delstrlist(p->hosts);
156432e31f52Ssthen 	config_delstrlist(p->addrs);
156532e31f52Ssthen 	free(p);
156632e31f52Ssthen }
156732e31f52Ssthen 
156832e31f52Ssthen void
1569933707f3Ssthen config_delstubs(struct config_stub* p)
1570933707f3Ssthen {
1571933707f3Ssthen 	struct config_stub* np;
1572933707f3Ssthen 	while(p) {
1573933707f3Ssthen 		np = p->next;
157432e31f52Ssthen 		config_delstub(p);
1575933707f3Ssthen 		p = np;
1576933707f3Ssthen 	}
1577933707f3Ssthen }
1578933707f3Ssthen 
157977079be7Ssthen void
158077079be7Ssthen config_delview(struct config_view* p)
158177079be7Ssthen {
158277079be7Ssthen 	if(!p) return;
158377079be7Ssthen 	free(p->name);
158477079be7Ssthen 	config_deldblstrlist(p->local_zones);
158577079be7Ssthen 	config_delstrlist(p->local_zones_nodefault);
15868240c1b9Ssthen #ifdef USE_IPSET
15878240c1b9Ssthen 	config_delstrlist(p->local_zones_ipset);
15888240c1b9Ssthen #endif
158977079be7Ssthen 	config_delstrlist(p->local_data);
159077079be7Ssthen 	free(p);
159177079be7Ssthen }
159277079be7Ssthen 
159377079be7Ssthen void
159477079be7Ssthen config_delviews(struct config_view* p)
159577079be7Ssthen {
159677079be7Ssthen 	struct config_view* np;
159777079be7Ssthen 	while(p) {
159877079be7Ssthen 		np = p->next;
159977079be7Ssthen 		config_delview(p);
160077079be7Ssthen 		p = np;
160177079be7Ssthen 	}
160277079be7Ssthen }
16032c144df0Ssthen 
16042c144df0Ssthen void
16052ee382b6Ssthen config_del_strarray(char** array, int num)
16062ee382b6Ssthen {
16072ee382b6Ssthen 	int i;
16082ee382b6Ssthen 	if(!array)
16092ee382b6Ssthen 		return;
16102ee382b6Ssthen 	for(i=0; i<num; i++) {
16112ee382b6Ssthen 		free(array[i]);
16122ee382b6Ssthen 	}
16132ee382b6Ssthen 	free(array);
16142ee382b6Ssthen }
16152ee382b6Ssthen 
161677079be7Ssthen void
16172ee382b6Ssthen config_del_strbytelist(struct config_strbytelist* p)
16182ee382b6Ssthen {
16192ee382b6Ssthen 	struct config_strbytelist* np;
16202ee382b6Ssthen 	while(p) {
16212ee382b6Ssthen 		np = p->next;
16222ee382b6Ssthen 		free(p->str);
16232ee382b6Ssthen 		free(p->str2);
16242ee382b6Ssthen 		free(p);
16252ee382b6Ssthen 		p = np;
16262ee382b6Ssthen 	}
16272ee382b6Ssthen }
16282ee382b6Ssthen 
1629933707f3Ssthen void
1630933707f3Ssthen config_delete(struct config_file* cfg)
1631933707f3Ssthen {
1632933707f3Ssthen 	if(!cfg) return;
1633933707f3Ssthen 	free(cfg->username);
1634933707f3Ssthen 	free(cfg->chrootdir);
1635933707f3Ssthen 	free(cfg->directory);
1636933707f3Ssthen 	free(cfg->logfile);
1637933707f3Ssthen 	free(cfg->pidfile);
16380bdb4f62Ssthen 	free(cfg->if_automatic_ports);
1639933707f3Ssthen 	free(cfg->target_fetch_policy);
1640933707f3Ssthen 	free(cfg->ssl_service_key);
1641933707f3Ssthen 	free(cfg->ssl_service_pem);
1642938a3a5eSflorian 	free(cfg->tls_cert_bundle);
164320237c55Ssthen 	config_delstrlist(cfg->tls_additional_port);
1644f6b99bafSsthen 	config_delstrlist(cfg->tls_session_ticket_keys.first);
1645f6b99bafSsthen 	free(cfg->tls_ciphers);
1646f6b99bafSsthen 	free(cfg->tls_ciphersuites);
16472c144df0Ssthen 	free(cfg->http_endpoint);
1648eaf2578eSsthen 	if(cfg->log_identity) {
1649eaf2578eSsthen 		log_ident_revert_to_default();
165077079be7Ssthen 		free(cfg->log_identity);
1651eaf2578eSsthen 	}
16522ee382b6Ssthen 	config_del_strarray(cfg->ifs, cfg->num_ifs);
16532ee382b6Ssthen 	config_del_strarray(cfg->out_ifs, cfg->num_out_ifs);
1654933707f3Ssthen 	config_delstubs(cfg->stubs);
1655933707f3Ssthen 	config_delstubs(cfg->forwards);
1656bdfc4d55Sflorian 	config_delauths(cfg->auths);
165777079be7Ssthen 	config_delviews(cfg->views);
1658933707f3Ssthen 	config_delstrlist(cfg->donotqueryaddrs);
1659933707f3Ssthen 	config_delstrlist(cfg->root_hints);
16602be9e038Ssthen #ifdef CLIENT_SUBNET
16612be9e038Ssthen 	config_delstrlist(cfg->client_subnet);
16622be9e038Ssthen 	config_delstrlist(cfg->client_subnet_zone);
16632be9e038Ssthen #endif
1664933707f3Ssthen 	free(cfg->identity);
1665933707f3Ssthen 	free(cfg->version);
1666191f22c6Ssthen 	free(cfg->http_user_agent);
16679982a05dSsthen 	free(cfg->nsid_cfg_str);
16689982a05dSsthen 	free(cfg->nsid);
1669933707f3Ssthen 	free(cfg->module_conf);
1670933707f3Ssthen 	free(cfg->outgoing_avail_ports);
1671a58bff56Ssthen 	config_delstrlist(cfg->caps_whitelist);
1672933707f3Ssthen 	config_delstrlist(cfg->private_address);
1673933707f3Ssthen 	config_delstrlist(cfg->private_domain);
1674933707f3Ssthen 	config_delstrlist(cfg->auto_trust_anchor_file_list);
1675933707f3Ssthen 	config_delstrlist(cfg->trust_anchor_file_list);
1676933707f3Ssthen 	config_delstrlist(cfg->trusted_keys_file_list);
1677933707f3Ssthen 	config_delstrlist(cfg->trust_anchor_list);
1678933707f3Ssthen 	config_delstrlist(cfg->domain_insecure);
1679933707f3Ssthen 	config_deldblstrlist(cfg->acls);
16802308e98cSsthen 	config_deldblstrlist(cfg->tcp_connection_limits);
1681933707f3Ssthen 	free(cfg->val_nsec3_key_iterations);
1682933707f3Ssthen 	config_deldblstrlist(cfg->local_zones);
1683933707f3Ssthen 	config_delstrlist(cfg->local_zones_nodefault);
16848240c1b9Ssthen #ifdef USE_IPSET
16858240c1b9Ssthen 	config_delstrlist(cfg->local_zones_ipset);
16868240c1b9Ssthen #endif
1687933707f3Ssthen 	config_delstrlist(cfg->local_data);
168877079be7Ssthen 	config_deltrplstrlist(cfg->local_zone_overrides);
16892ee382b6Ssthen 	config_del_strarray(cfg->tagname, cfg->num_tags);
16902ee382b6Ssthen 	config_del_strbytelist(cfg->local_zone_tags);
16912be9e038Ssthen 	config_del_strbytelist(cfg->respip_tags);
169245872187Ssthen 	config_deldblstrlist(cfg->acl_view);
169345872187Ssthen 	config_del_strbytelist(cfg->acl_tags);
169477079be7Ssthen 	config_deltrplstrlist(cfg->acl_tag_actions);
169577079be7Ssthen 	config_deltrplstrlist(cfg->acl_tag_datas);
169645872187Ssthen 	config_deldblstrlist(cfg->interface_actions);
169745872187Ssthen 	config_deldblstrlist(cfg->interface_view);
169845872187Ssthen 	config_del_strbytelist(cfg->interface_tags);
169945872187Ssthen 	config_deltrplstrlist(cfg->interface_tag_actions);
170045872187Ssthen 	config_deltrplstrlist(cfg->interface_tag_datas);
170120237c55Ssthen 	config_delstrlist(cfg->control_ifs.first);
17022bdc0ed1Ssthen 	config_deldblstrlist(cfg->wait_limit_netblock);
17032bdc0ed1Ssthen 	config_deldblstrlist(cfg->wait_limit_cookie_netblock);
1704933707f3Ssthen 	free(cfg->server_key_file);
1705933707f3Ssthen 	free(cfg->server_cert_file);
1706933707f3Ssthen 	free(cfg->control_key_file);
1707933707f3Ssthen 	free(cfg->control_cert_file);
17088b7325afSsthen 	free(cfg->nat64_prefix);
1709a58bff56Ssthen 	free(cfg->dns64_prefix);
17102308e98cSsthen 	config_delstrlist(cfg->dns64_ignore_aaaa);
1711e10d3884Sbrad 	free(cfg->dnstap_socket_path);
1712a3167c07Ssthen 	free(cfg->dnstap_ip);
1713a3167c07Ssthen 	free(cfg->dnstap_tls_server_name);
1714a3167c07Ssthen 	free(cfg->dnstap_tls_cert_bundle);
1715a3167c07Ssthen 	free(cfg->dnstap_tls_client_key_file);
1716a3167c07Ssthen 	free(cfg->dnstap_tls_client_cert_file);
1717e10d3884Sbrad 	free(cfg->dnstap_identity);
1718e10d3884Sbrad 	free(cfg->dnstap_version);
1719a58bff56Ssthen 	config_deldblstrlist(cfg->ratelimit_for_domain);
1720a58bff56Ssthen 	config_deldblstrlist(cfg->ratelimit_below_domain);
17218240c1b9Ssthen 	config_delstrlist(cfg->python_script);
1722a3167c07Ssthen 	config_delstrlist(cfg->dynlib_file);
1723eba819a2Ssthen 	config_deldblstrlist(cfg->edns_client_strings);
172445872187Ssthen 	config_delstrlist(cfg->proxy_protocol_port);
17252be9e038Ssthen #ifdef USE_IPSECMOD
17262be9e038Ssthen 	free(cfg->ipsecmod_hook);
17272be9e038Ssthen 	config_delstrlist(cfg->ipsecmod_whitelist);
17282be9e038Ssthen #endif
1729*98bc733bSsthen 	free(cfg->cookie_secret_file);
17307191de28Ssthen #ifdef USE_CACHEDB
17317191de28Ssthen 	free(cfg->cachedb_backend);
17327191de28Ssthen 	free(cfg->cachedb_secret);
1733eaf2578eSsthen #ifdef USE_REDIS
1734eaf2578eSsthen 	free(cfg->redis_server_host);
17358b7325afSsthen 	free(cfg->redis_server_path);
17368b7325afSsthen 	free(cfg->redis_server_password);
1737eaf2578eSsthen #endif  /* USE_REDIS */
1738eaf2578eSsthen #endif  /* USE_CACHEDB */
17398240c1b9Ssthen #ifdef USE_IPSET
17408240c1b9Ssthen 	free(cfg->ipset_name_v4);
17418240c1b9Ssthen 	free(cfg->ipset_name_v6);
17428240c1b9Ssthen #endif
1743933707f3Ssthen 	free(cfg);
1744933707f3Ssthen }
1745933707f3Ssthen 
1746933707f3Ssthen static void
17478b7325afSsthen init_cookie_secret(uint8_t* cookie_secret, size_t cookie_secret_len)
17488b7325afSsthen {
17498b7325afSsthen 	struct ub_randstate *rand = ub_initstate(NULL);
17508b7325afSsthen 
17518b7325afSsthen 	if (!rand)
17528b7325afSsthen 		fatal_exit("could not init random generator");
17538b7325afSsthen 	while (cookie_secret_len) {
17548b7325afSsthen 		*cookie_secret++ = (uint8_t)ub_random(rand);
17558b7325afSsthen 		cookie_secret_len--;
17568b7325afSsthen 	}
17578b7325afSsthen 	ub_randfree(rand);
17588b7325afSsthen }
17598b7325afSsthen 
17608b7325afSsthen static void
1761933707f3Ssthen init_outgoing_availports(int* a, int num)
1762933707f3Ssthen {
1763933707f3Ssthen 	/* generated with make iana_update */
1764933707f3Ssthen 	const int iana_assigned[] = {
1765933707f3Ssthen #include "util/iana_ports.inc"
1766933707f3Ssthen 		-1 }; /* end marker to put behind trailing comma */
1767933707f3Ssthen 
1768933707f3Ssthen 	int i;
1769933707f3Ssthen 	/* do not use <1024, that could be trouble with the system, privs */
1770933707f3Ssthen 	for(i=1024; i<num; i++) {
1771933707f3Ssthen 		a[i] = i;
1772933707f3Ssthen 	}
1773933707f3Ssthen 	/* create empty spot at 49152 to keep ephemeral ports available
1774933707f3Ssthen 	 * to other programs */
1775933707f3Ssthen 	for(i=49152; i<49152+256; i++)
1776933707f3Ssthen 		a[i] = 0;
1777933707f3Ssthen 	/* pick out all the IANA assigned ports */
1778933707f3Ssthen 	for(i=0; iana_assigned[i]!=-1; i++) {
1779933707f3Ssthen 		if(iana_assigned[i] < num)
1780933707f3Ssthen 			a[iana_assigned[i]] = 0;
1781933707f3Ssthen 	}
1782933707f3Ssthen }
1783933707f3Ssthen 
1784*98bc733bSsthen static int
1785*98bc733bSsthen extract_port_from_str(const char* str, int max_port) {
1786*98bc733bSsthen 	char* endptr;
1787*98bc733bSsthen 	long int value;
1788*98bc733bSsthen 	if (str == NULL || *str == '\0') {
1789*98bc733bSsthen 		log_err("str: '%s' is invalid", (str?str:"NULL"));
1790*98bc733bSsthen 		return -1;
1791*98bc733bSsthen 	}
1792*98bc733bSsthen 
1793*98bc733bSsthen 	value = strtol(str, &endptr, 10);
1794*98bc733bSsthen 	if ((endptr == str) || (*endptr != '\0'))  {
1795*98bc733bSsthen 		log_err("cannot parse port number '%s'", str);
1796*98bc733bSsthen 		return -1;
1797*98bc733bSsthen 	}
1798*98bc733bSsthen 
1799*98bc733bSsthen 	if (errno == ERANGE) {
1800*98bc733bSsthen                 log_err("overflow occurred when parsing '%s'", str);
1801*98bc733bSsthen 		return -1;
1802*98bc733bSsthen 	}
1803*98bc733bSsthen 
1804*98bc733bSsthen 	if (value == 0 && strcmp(str, "0") != 0) {
1805*98bc733bSsthen 		log_err("cannot parse port number '%s'", str);
1806*98bc733bSsthen 		return -1;
1807*98bc733bSsthen 	}
1808*98bc733bSsthen 
1809*98bc733bSsthen 	if (value < 0 || value >= max_port) {
1810*98bc733bSsthen 		log_err(" '%s' is out of bounds [0, %d)", str, max_port);
1811*98bc733bSsthen 		return -1;
1812*98bc733bSsthen 	}
1813*98bc733bSsthen 
1814*98bc733bSsthen 	return (int)value;
1815*98bc733bSsthen }
1816*98bc733bSsthen 
1817933707f3Ssthen int
1818933707f3Ssthen cfg_mark_ports(const char* str, int allow, int* avail, int num)
1819933707f3Ssthen {
1820933707f3Ssthen 	char* mid = strchr(str, '-');
1821a3167c07Ssthen #ifdef DISABLE_EXPLICIT_PORT_RANDOMISATION
1822a3167c07Ssthen 	log_warn("Explicit port randomisation disabled, ignoring "
1823a3167c07Ssthen 		"outgoing-port-permit and outgoing-port-avoid configuration "
1824a3167c07Ssthen 		"options");
1825a3167c07Ssthen #endif
1826933707f3Ssthen 	if(!mid) {
1827*98bc733bSsthen 		int port = extract_port_from_str(str, num);
18282bdc0ed1Ssthen 		if(port < 0) {
1829*98bc733bSsthen 			log_err("Failed to parse the port number");
1830933707f3Ssthen 			return 0;
1831933707f3Ssthen 		}
1832933707f3Ssthen 		if(port < num)
1833933707f3Ssthen 			avail[port] = (allow?port:0);
1834933707f3Ssthen 	} else {
1835933707f3Ssthen 		char buf[16];
1836*98bc733bSsthen 		int i, low;
1837*98bc733bSsthen 		int high = extract_port_from_str(mid+1, num);
18382bdc0ed1Ssthen 		if(high < 0) {
1839*98bc733bSsthen 			log_err("Failed to parse the port number");
18402bdc0ed1Ssthen 			return 0;
18412bdc0ed1Ssthen 		}
1842*98bc733bSsthen 
1843933707f3Ssthen 		if( (int)(mid-str)+1 >= (int)sizeof(buf) ) {
1844933707f3Ssthen 			log_err("cannot parse port number '%s'", str);
1845933707f3Ssthen 			return 0;
1846933707f3Ssthen 		}
1847*98bc733bSsthen 
1848933707f3Ssthen 		if(mid > str)
1849933707f3Ssthen 			memcpy(buf, str, (size_t)(mid-str));
1850933707f3Ssthen 		buf[mid-str] = 0;
1851*98bc733bSsthen 		low = extract_port_from_str(buf, num);
18522bdc0ed1Ssthen 		if(low < 0) {
1853*98bc733bSsthen 			log_err("Failed to parse the port number");
18542bdc0ed1Ssthen 			return 0;
18552bdc0ed1Ssthen 		}
1856*98bc733bSsthen 
1857*98bc733bSsthen 		if (low > high) {
1858*98bc733bSsthen 			log_err("Low value is greater than high value");
1859933707f3Ssthen 			return 0;
1860933707f3Ssthen 		}
1861*98bc733bSsthen 
1862933707f3Ssthen 		for(i=low; i<=high; i++) {
1863933707f3Ssthen 			if(i < num)
1864933707f3Ssthen 				avail[i] = (allow?i:0);
1865933707f3Ssthen 		}
1866933707f3Ssthen 	}
1867933707f3Ssthen 	return 1;
1868933707f3Ssthen }
1869933707f3Ssthen 
1870933707f3Ssthen int
1871933707f3Ssthen cfg_scan_ports(int* avail, int num)
1872933707f3Ssthen {
1873933707f3Ssthen 	int i;
1874933707f3Ssthen 	int count = 0;
1875933707f3Ssthen 	for(i=0; i<num; i++) {
1876933707f3Ssthen 		if(avail[i])
1877933707f3Ssthen 			count++;
1878933707f3Ssthen 	}
1879933707f3Ssthen 	return count;
1880933707f3Ssthen }
1881933707f3Ssthen 
1882933707f3Ssthen int cfg_condense_ports(struct config_file* cfg, int** avail)
1883933707f3Ssthen {
1884933707f3Ssthen 	int num = cfg_scan_ports(cfg->outgoing_avail_ports, 65536);
1885933707f3Ssthen 	int i, at = 0;
1886933707f3Ssthen 	*avail = NULL;
1887933707f3Ssthen 	if(num == 0)
1888933707f3Ssthen 		return 0;
1889a58bff56Ssthen 	*avail = (int*)reallocarray(NULL, (size_t)num, sizeof(int));
1890933707f3Ssthen 	if(!*avail)
1891933707f3Ssthen 		return 0;
1892933707f3Ssthen 	for(i=0; i<65536; i++) {
1893933707f3Ssthen 		if(cfg->outgoing_avail_ports[i])
1894933707f3Ssthen 			(*avail)[at++] = cfg->outgoing_avail_ports[i];
1895933707f3Ssthen 	}
1896933707f3Ssthen 	log_assert(at == num);
1897933707f3Ssthen 	return num;
1898933707f3Ssthen }
1899933707f3Ssthen 
1900191f22c6Ssthen void cfg_apply_local_port_policy(struct config_file* cfg, int num) {
1901191f22c6Ssthen (void)cfg;
1902191f22c6Ssthen (void)num;
1903191f22c6Ssthen #ifdef USE_LINUX_IP_LOCAL_PORT_RANGE
1904191f22c6Ssthen 	{
1905191f22c6Ssthen 		int i = 0;
1906191f22c6Ssthen 		FILE* range_fd;
1907191f22c6Ssthen 		if ((range_fd = fopen(LINUX_IP_LOCAL_PORT_RANGE_PATH, "r")) != NULL) {
1908191f22c6Ssthen 			int min_port = 0;
1909191f22c6Ssthen 			int max_port = num - 1;
1910191f22c6Ssthen 			if (fscanf(range_fd, "%d %d", &min_port, &max_port) == 2) {
1911191f22c6Ssthen 				for(i=0; i<min_port; i++) {
1912191f22c6Ssthen 					cfg->outgoing_avail_ports[i] = 0;
1913191f22c6Ssthen 				}
1914191f22c6Ssthen 				for(i=max_port+1; i<num; i++) {
1915191f22c6Ssthen 					cfg->outgoing_avail_ports[i] = 0;
1916191f22c6Ssthen 				}
1917191f22c6Ssthen 			} else {
1918191f22c6Ssthen 				log_err("unexpected port range in %s",
1919191f22c6Ssthen 						LINUX_IP_LOCAL_PORT_RANGE_PATH);
1920191f22c6Ssthen 			}
1921191f22c6Ssthen 			fclose(range_fd);
1922191f22c6Ssthen 		} else {
1923191f22c6Ssthen 			log_err("failed to read from file: %s (%s)",
1924191f22c6Ssthen 					LINUX_IP_LOCAL_PORT_RANGE_PATH,
1925191f22c6Ssthen 					strerror(errno));
1926191f22c6Ssthen 		}
1927191f22c6Ssthen 	}
1928191f22c6Ssthen #endif
1929191f22c6Ssthen }
1930191f22c6Ssthen 
1931933707f3Ssthen /** print error with file and line number */
1932933707f3Ssthen static void ub_c_error_va_list(const char *fmt, va_list args)
1933933707f3Ssthen {
1934933707f3Ssthen 	cfg_parser->errors++;
1935933707f3Ssthen 	fprintf(stderr, "%s:%d: error: ", cfg_parser->filename,
1936933707f3Ssthen 	cfg_parser->line);
1937933707f3Ssthen 	vfprintf(stderr, fmt, args);
1938933707f3Ssthen 	fprintf(stderr, "\n");
1939933707f3Ssthen }
1940933707f3Ssthen 
1941933707f3Ssthen /** print error with file and line number */
1942933707f3Ssthen void ub_c_error_msg(const char* fmt, ...)
1943933707f3Ssthen {
1944933707f3Ssthen 	va_list args;
1945933707f3Ssthen 	va_start(args, fmt);
1946933707f3Ssthen 	ub_c_error_va_list(fmt, args);
1947933707f3Ssthen 	va_end(args);
1948933707f3Ssthen }
1949933707f3Ssthen 
1950933707f3Ssthen void ub_c_error(const char *str)
1951933707f3Ssthen {
1952933707f3Ssthen 	cfg_parser->errors++;
195345872187Ssthen 	if(strcmp(str, "syntax error")==0 && cfg_parser->started_toplevel ==0)
195445872187Ssthen 		str = "syntax error, is there no section start after an "
195545872187Ssthen 			"include-toplevel directive perhaps.";
1956933707f3Ssthen 	fprintf(stderr, "%s:%d: error: %s\n", cfg_parser->filename,
1957933707f3Ssthen 		cfg_parser->line, str);
1958933707f3Ssthen }
1959933707f3Ssthen 
1960933707f3Ssthen int ub_c_wrap(void)
1961933707f3Ssthen {
1962933707f3Ssthen 	return 1;
1963933707f3Ssthen }
1964933707f3Ssthen 
1965933707f3Ssthen int cfg_strlist_append(struct config_strlist_head* list, char* item)
1966933707f3Ssthen {
1967933707f3Ssthen 	struct config_strlist *s;
1968452a1548Ssthen 	if(!item || !list) {
1969452a1548Ssthen 		free(item);
1970933707f3Ssthen 		return 0;
1971452a1548Ssthen 	}
1972933707f3Ssthen 	s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist));
1973452a1548Ssthen 	if(!s) {
1974452a1548Ssthen 		free(item);
1975933707f3Ssthen 		return 0;
1976452a1548Ssthen 	}
1977933707f3Ssthen 	s->str = item;
1978933707f3Ssthen 	s->next = NULL;
1979933707f3Ssthen 	if(list->last)
1980933707f3Ssthen 		list->last->next = s;
1981933707f3Ssthen 	else
1982933707f3Ssthen 		list->first = s;
1983933707f3Ssthen 	list->last = s;
1984933707f3Ssthen 	return 1;
1985933707f3Ssthen }
1986933707f3Ssthen 
1987933707f3Ssthen int
198877079be7Ssthen cfg_region_strlist_insert(struct regional* region,
198977079be7Ssthen 	struct config_strlist** head, char* item)
199077079be7Ssthen {
199177079be7Ssthen 	struct config_strlist *s;
199277079be7Ssthen 	if(!item || !head)
199377079be7Ssthen 		return 0;
199477079be7Ssthen 	s = (struct config_strlist*)regional_alloc_zero(region,
199577079be7Ssthen 		sizeof(struct config_strlist));
199677079be7Ssthen 	if(!s)
199777079be7Ssthen 		return 0;
199877079be7Ssthen 	s->str = item;
199977079be7Ssthen 	s->next = *head;
200077079be7Ssthen 	*head = s;
200177079be7Ssthen 	return 1;
200277079be7Ssthen }
200377079be7Ssthen 
2004bdfc4d55Sflorian struct config_strlist*
2005bdfc4d55Sflorian cfg_strlist_find(struct config_strlist* head, const char *item)
2006bdfc4d55Sflorian {
2007bdfc4d55Sflorian 	struct config_strlist *s = head;
2008bdfc4d55Sflorian 	if(!head){
2009bdfc4d55Sflorian 		return NULL;
2010bdfc4d55Sflorian 	}
2011bdfc4d55Sflorian 	while(s) {
2012bdfc4d55Sflorian 		if(strcmp(s->str, item) == 0) {
2013bdfc4d55Sflorian 			return s;
2014bdfc4d55Sflorian 		}
2015bdfc4d55Sflorian 		s = s->next;
2016bdfc4d55Sflorian 	}
2017bdfc4d55Sflorian 	return NULL;
2018bdfc4d55Sflorian }
2019bdfc4d55Sflorian 
202077079be7Ssthen int
2021933707f3Ssthen cfg_strlist_insert(struct config_strlist** head, char* item)
2022933707f3Ssthen {
2023933707f3Ssthen 	struct config_strlist *s;
2024452a1548Ssthen 	if(!item || !head) {
2025452a1548Ssthen 		free(item);
2026933707f3Ssthen 		return 0;
2027452a1548Ssthen 	}
2028933707f3Ssthen 	s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist));
2029452a1548Ssthen 	if(!s) {
2030452a1548Ssthen 		free(item);
2031933707f3Ssthen 		return 0;
2032452a1548Ssthen 	}
2033933707f3Ssthen 	s->str = item;
2034933707f3Ssthen 	s->next = *head;
2035933707f3Ssthen 	*head = s;
2036933707f3Ssthen 	return 1;
2037933707f3Ssthen }
2038933707f3Ssthen 
2039933707f3Ssthen int
20408240c1b9Ssthen cfg_strlist_append_ex(struct config_strlist** head, char* item)
20418240c1b9Ssthen {
20428240c1b9Ssthen 	struct config_strlist *s;
20438240c1b9Ssthen 	if(!item || !head)
20448240c1b9Ssthen 		return 0;
20458240c1b9Ssthen 	s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist));
20468240c1b9Ssthen 	if(!s)
20478240c1b9Ssthen 		return 0;
20488240c1b9Ssthen 	s->str = item;
20498240c1b9Ssthen 	s->next = NULL;
20508240c1b9Ssthen 
20518240c1b9Ssthen 	if (*head==NULL) {
20528240c1b9Ssthen 		*head = s;
20538240c1b9Ssthen 	} else {
20548240c1b9Ssthen 		struct config_strlist *last = *head;
20558240c1b9Ssthen 		while (last->next!=NULL) {
20568240c1b9Ssthen 		    last = last->next;
20578240c1b9Ssthen 		}
20588240c1b9Ssthen 		last->next = s;
20598240c1b9Ssthen 	}
20608240c1b9Ssthen 
20618240c1b9Ssthen 	return 1;
20628240c1b9Ssthen }
20638240c1b9Ssthen 
20648240c1b9Ssthen int
2065933707f3Ssthen cfg_str2list_insert(struct config_str2list** head, char* item, char* i2)
2066933707f3Ssthen {
2067933707f3Ssthen 	struct config_str2list *s;
2068452a1548Ssthen 	if(!item || !i2 || !head) {
2069452a1548Ssthen 		free(item);
2070452a1548Ssthen 		free(i2);
2071933707f3Ssthen 		return 0;
2072452a1548Ssthen 	}
2073933707f3Ssthen 	s = (struct config_str2list*)calloc(1, sizeof(struct config_str2list));
2074452a1548Ssthen 	if(!s) {
2075452a1548Ssthen 		free(item);
2076452a1548Ssthen 		free(i2);
2077933707f3Ssthen 		return 0;
2078452a1548Ssthen 	}
2079933707f3Ssthen 	s->str = item;
2080933707f3Ssthen 	s->str2 = i2;
2081933707f3Ssthen 	s->next = *head;
2082933707f3Ssthen 	*head = s;
2083933707f3Ssthen 	return 1;
2084933707f3Ssthen }
2085933707f3Ssthen 
20862ee382b6Ssthen int
208777079be7Ssthen cfg_str3list_insert(struct config_str3list** head, char* item, char* i2,
208877079be7Ssthen 	char* i3)
208977079be7Ssthen {
209077079be7Ssthen 	struct config_str3list *s;
209177079be7Ssthen 	if(!item || !i2 || !i3 || !head)
209277079be7Ssthen 		return 0;
209377079be7Ssthen 	s = (struct config_str3list*)calloc(1, sizeof(struct config_str3list));
209477079be7Ssthen 	if(!s)
209577079be7Ssthen 		return 0;
209677079be7Ssthen 	s->str = item;
209777079be7Ssthen 	s->str2 = i2;
209877079be7Ssthen 	s->str3 = i3;
209977079be7Ssthen 	s->next = *head;
210077079be7Ssthen 	*head = s;
210177079be7Ssthen 	return 1;
210277079be7Ssthen }
210377079be7Ssthen 
210477079be7Ssthen int
21052ee382b6Ssthen cfg_strbytelist_insert(struct config_strbytelist** head, char* item,
21062ee382b6Ssthen 	uint8_t* i2, size_t i2len)
21072ee382b6Ssthen {
21082ee382b6Ssthen 	struct config_strbytelist* s;
21092ee382b6Ssthen 	if(!item || !i2 || !head)
21102ee382b6Ssthen 		return 0;
21112ee382b6Ssthen 	s = (struct config_strbytelist*)calloc(1, sizeof(*s));
21122ee382b6Ssthen 	if(!s)
21132ee382b6Ssthen 		return 0;
21142ee382b6Ssthen 	s->str = item;
21152ee382b6Ssthen 	s->str2 = i2;
21162ee382b6Ssthen 	s->str2len = i2len;
21172ee382b6Ssthen 	s->next = *head;
21182ee382b6Ssthen 	*head = s;
21192ee382b6Ssthen 	return 1;
21202ee382b6Ssthen }
21212ee382b6Ssthen 
2122229e174cSsthen time_t
2123933707f3Ssthen cfg_convert_timeval(const char* str)
2124933707f3Ssthen {
2125229e174cSsthen 	time_t t;
2126933707f3Ssthen 	struct tm tm;
2127933707f3Ssthen 	memset(&tm, 0, sizeof(tm));
2128933707f3Ssthen 	if(strlen(str) < 14)
2129933707f3Ssthen 		return 0;
2130933707f3Ssthen 	if(sscanf(str, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
2131933707f3Ssthen 		&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6)
2132933707f3Ssthen 		return 0;
2133933707f3Ssthen 	tm.tm_year -= 1900;
2134933707f3Ssthen 	tm.tm_mon--;
2135933707f3Ssthen 	/* Check values */
2136933707f3Ssthen 	if (tm.tm_year < 70)	return 0;
2137933707f3Ssthen 	if (tm.tm_mon < 0 || tm.tm_mon > 11)	return 0;
2138933707f3Ssthen 	if (tm.tm_mday < 1 || tm.tm_mday > 31) 	return 0;
2139933707f3Ssthen 	if (tm.tm_hour < 0 || tm.tm_hour > 23)	return 0;
2140933707f3Ssthen 	if (tm.tm_min < 0 || tm.tm_min > 59)	return 0;
2141933707f3Ssthen 	if (tm.tm_sec < 0 || tm.tm_sec > 59)	return 0;
2142933707f3Ssthen 	/* call ldns conversion function */
21435d76a658Ssthen 	t = sldns_mktime_from_utc(&tm);
2144933707f3Ssthen 	return t;
2145933707f3Ssthen }
2146933707f3Ssthen 
2147933707f3Ssthen int
2148933707f3Ssthen cfg_count_numbers(const char* s)
2149933707f3Ssthen {
2150933707f3Ssthen 	/* format ::= (sp num)+ sp  */
2151933707f3Ssthen 	/* num ::= [-](0-9)+        */
2152933707f3Ssthen 	/* sp ::= (space|tab)*      */
2153933707f3Ssthen 	int num = 0;
2154933707f3Ssthen 	while(*s) {
2155e10d3884Sbrad 		while(*s && isspace((unsigned char)*s))
2156933707f3Ssthen 			s++;
2157933707f3Ssthen 		if(!*s) /* end of string */
2158933707f3Ssthen 			break;
2159933707f3Ssthen 		if(*s == '-')
2160933707f3Ssthen 			s++;
2161933707f3Ssthen 		if(!*s) /* only - not allowed */
2162933707f3Ssthen 			return 0;
2163e10d3884Sbrad 		if(!isdigit((unsigned char)*s)) /* bad character */
2164933707f3Ssthen 			return 0;
2165e10d3884Sbrad 		while(*s && isdigit((unsigned char)*s))
2166933707f3Ssthen 			s++;
2167933707f3Ssthen 		num++;
2168933707f3Ssthen 	}
2169933707f3Ssthen 	return num;
2170933707f3Ssthen }
2171933707f3Ssthen 
2172933707f3Ssthen /** all digit number */
2173933707f3Ssthen static int isalldigit(const char* str, size_t l)
2174933707f3Ssthen {
2175933707f3Ssthen 	size_t i;
2176933707f3Ssthen 	for(i=0; i<l; i++)
2177e10d3884Sbrad 		if(!isdigit((unsigned char)str[i]))
2178933707f3Ssthen 			return 0;
2179933707f3Ssthen 	return 1;
2180933707f3Ssthen }
2181933707f3Ssthen 
2182933707f3Ssthen int
2183933707f3Ssthen cfg_parse_memsize(const char* str, size_t* res)
2184933707f3Ssthen {
21853dcb24b8Ssthen 	size_t len;
2186933707f3Ssthen 	size_t mult = 1;
21873dcb24b8Ssthen 	if(!str || (len=(size_t)strlen(str)) == 0) {
2188933707f3Ssthen 		log_err("not a size: '%s'", str);
2189933707f3Ssthen 		return 0;
2190933707f3Ssthen 	}
2191933707f3Ssthen 	if(isalldigit(str, len)) {
2192933707f3Ssthen 		*res = (size_t)atol(str);
2193933707f3Ssthen 		return 1;
2194933707f3Ssthen 	}
2195933707f3Ssthen 	/* check appended num */
2196933707f3Ssthen 	while(len>0 && str[len-1]==' ')
2197933707f3Ssthen 		len--;
2198933707f3Ssthen 	if(len > 1 && str[len-1] == 'b')
2199933707f3Ssthen 		len--;
2200933707f3Ssthen 	else if(len > 1 && str[len-1] == 'B')
2201933707f3Ssthen 		len--;
2202933707f3Ssthen 
2203e10d3884Sbrad 	if(len > 1 && tolower((unsigned char)str[len-1]) == 'g')
2204933707f3Ssthen 		mult = 1024*1024*1024;
2205e10d3884Sbrad 	else if(len > 1 && tolower((unsigned char)str[len-1]) == 'm')
2206933707f3Ssthen 		mult = 1024*1024;
2207e10d3884Sbrad 	else if(len > 1 && tolower((unsigned char)str[len-1]) == 'k')
2208933707f3Ssthen 		mult = 1024;
2209e10d3884Sbrad 	else if(len > 0 && isdigit((unsigned char)str[len-1]))
2210933707f3Ssthen 		mult = 1;
2211933707f3Ssthen 	else {
2212933707f3Ssthen 		log_err("unknown size specifier: '%s'", str);
2213933707f3Ssthen 		return 0;
2214933707f3Ssthen 	}
2215933707f3Ssthen 	while(len>1 && str[len-2]==' ')
2216933707f3Ssthen 		len--;
2217933707f3Ssthen 
2218933707f3Ssthen 	if(!isalldigit(str, len-1)) {
2219933707f3Ssthen 		log_err("unknown size specifier: '%s'", str);
2220933707f3Ssthen 		return 0;
2221933707f3Ssthen 	}
2222933707f3Ssthen 	*res = ((size_t)atol(str)) * mult;
2223933707f3Ssthen 	return 1;
2224933707f3Ssthen }
2225933707f3Ssthen 
22262ee382b6Ssthen int
22272ee382b6Ssthen find_tag_id(struct config_file* cfg, const char* tag)
22282ee382b6Ssthen {
22292ee382b6Ssthen 	int i;
22302ee382b6Ssthen 	for(i=0; i<cfg->num_tags; i++) {
22312ee382b6Ssthen 		if(strcmp(cfg->tagname[i], tag) == 0)
22322ee382b6Ssthen 			return i;
22332ee382b6Ssthen 	}
22342ee382b6Ssthen 	return -1;
22352ee382b6Ssthen }
22362ee382b6Ssthen 
22372ee382b6Ssthen int
22382ee382b6Ssthen config_add_tag(struct config_file* cfg, const char* tag)
22392ee382b6Ssthen {
22402ee382b6Ssthen 	char** newarray;
22412ee382b6Ssthen 	char* newtag;
22422ee382b6Ssthen 	if(find_tag_id(cfg, tag) != -1)
22432ee382b6Ssthen 		return 1; /* nothing to do */
22442ee382b6Ssthen 	newarray = (char**)malloc(sizeof(char*)*(cfg->num_tags+1));
22452ee382b6Ssthen 	if(!newarray)
22462ee382b6Ssthen 		return 0;
22472ee382b6Ssthen 	newtag = strdup(tag);
22482ee382b6Ssthen 	if(!newtag) {
22492ee382b6Ssthen 		free(newarray);
22502ee382b6Ssthen 		return 0;
22512ee382b6Ssthen 	}
22522ee382b6Ssthen 	if(cfg->tagname) {
22532ee382b6Ssthen 		memcpy(newarray, cfg->tagname, sizeof(char*)*cfg->num_tags);
22542ee382b6Ssthen 		free(cfg->tagname);
22552ee382b6Ssthen 	}
22562ee382b6Ssthen 	newarray[cfg->num_tags++] = newtag;
22572ee382b6Ssthen 	cfg->tagname = newarray;
22582ee382b6Ssthen 	return 1;
22592ee382b6Ssthen }
22602ee382b6Ssthen 
22612ee382b6Ssthen /** set a bit in a bit array */
22622ee382b6Ssthen static void
22632ee382b6Ssthen cfg_set_bit(uint8_t* bitlist, size_t len, int id)
22642ee382b6Ssthen {
22652ee382b6Ssthen 	int pos = id/8;
22662ee382b6Ssthen 	log_assert((size_t)pos < len);
226777079be7Ssthen 	(void)len;
22682ee382b6Ssthen 	bitlist[pos] |= 1<<(id%8);
22692ee382b6Ssthen }
22702ee382b6Ssthen 
22712ee382b6Ssthen uint8_t* config_parse_taglist(struct config_file* cfg, char* str,
22722ee382b6Ssthen         size_t* listlen)
22732ee382b6Ssthen {
22742ee382b6Ssthen 	uint8_t* taglist = NULL;
22752ee382b6Ssthen 	size_t len = 0;
22762ee382b6Ssthen 	char* p, *s;
22772ee382b6Ssthen 
22782ee382b6Ssthen 	/* allocate */
22792ee382b6Ssthen 	if(cfg->num_tags == 0) {
22802ee382b6Ssthen 		log_err("parse taglist, but no tags defined");
22812ee382b6Ssthen 		return 0;
22822ee382b6Ssthen 	}
22832ee382b6Ssthen 	len = (size_t)(cfg->num_tags+7)/8;
22842ee382b6Ssthen 	taglist = calloc(1, len);
22852ee382b6Ssthen 	if(!taglist) {
22862ee382b6Ssthen 		log_err("out of memory");
22872ee382b6Ssthen 		return 0;
22882ee382b6Ssthen 	}
22892ee382b6Ssthen 
22902ee382b6Ssthen 	/* parse */
22912ee382b6Ssthen 	s = str;
22922ee382b6Ssthen 	while((p=strsep(&s, " \t\n")) != NULL) {
22932ee382b6Ssthen 		if(*p) {
22942ee382b6Ssthen 			int id = find_tag_id(cfg, p);
22952ee382b6Ssthen 			/* set this bit in the bitlist */
22962ee382b6Ssthen 			if(id == -1) {
22972ee382b6Ssthen 				log_err("unknown tag: %s", p);
22982ee382b6Ssthen 				free(taglist);
22992ee382b6Ssthen 				return 0;
23002ee382b6Ssthen 			}
23012ee382b6Ssthen 			cfg_set_bit(taglist, len, id);
23022ee382b6Ssthen 		}
23032ee382b6Ssthen 	}
23042ee382b6Ssthen 
23052ee382b6Ssthen 	*listlen = len;
23062ee382b6Ssthen 	return taglist;
23072ee382b6Ssthen }
23082ee382b6Ssthen 
23099982a05dSsthen uint8_t* cfg_parse_nsid(const char* str, uint16_t* nsid_len)
23109982a05dSsthen {
23119982a05dSsthen 	uint8_t* nsid = NULL;
23129982a05dSsthen 
23139982a05dSsthen 	if (strncasecmp(str, "ascii_", 6) == 0) {
23149982a05dSsthen 		if ((nsid = (uint8_t *)strdup(str + 6)))
23159982a05dSsthen 			*nsid_len = strlen(str + 6);
23169982a05dSsthen 
23179982a05dSsthen 	} else if (strlen(str) % 2) {
23189982a05dSsthen 		; /* hex string has even number of characters */
23199982a05dSsthen 	}
23209982a05dSsthen 
23219982a05dSsthen 	else if (*str && (nsid = calloc(1, strlen(str) / 2))) {
23229982a05dSsthen 		const char *ch;
23239982a05dSsthen 		uint8_t *dp;
23249982a05dSsthen 
23259982a05dSsthen 		for ( ch = str, dp = nsid
23269982a05dSsthen 		    ; isxdigit(ch[0]) && isxdigit(ch[1])
23279982a05dSsthen 		    ; ch += 2, dp++) {
23289982a05dSsthen 			*dp  = (uint8_t)sldns_hexdigit_to_int(ch[0]) * 16;
23299982a05dSsthen 			*dp += (uint8_t)sldns_hexdigit_to_int(ch[1]);
23309982a05dSsthen 		}
23319982a05dSsthen 		if (*ch) {
23329982a05dSsthen 			free(nsid);
23339982a05dSsthen 			nsid = NULL;
23349982a05dSsthen 		} else
23359982a05dSsthen 			*nsid_len = strlen(str) / 2;
23369982a05dSsthen 	}
23379982a05dSsthen 	return nsid;
23389982a05dSsthen }
23399982a05dSsthen 
23409982a05dSsthen 
23412ee382b6Ssthen char* config_taglist2str(struct config_file* cfg, uint8_t* taglist,
23422ee382b6Ssthen         size_t taglen)
23432ee382b6Ssthen {
23442ee382b6Ssthen 	char buf[10240];
23452ee382b6Ssthen 	size_t i, j, len = 0;
23462ee382b6Ssthen 	buf[0] = 0;
23472ee382b6Ssthen 	for(i=0; i<taglen; i++) {
23482ee382b6Ssthen 		if(taglist[i] == 0)
23492ee382b6Ssthen 			continue;
23502ee382b6Ssthen 		for(j=0; j<8; j++) {
23512ee382b6Ssthen 			if((taglist[i] & (1<<j)) != 0) {
23522ee382b6Ssthen 				size_t id = i*8 + j;
23532ee382b6Ssthen 				snprintf(buf+len, sizeof(buf)-len, "%s%s",
23542ee382b6Ssthen 					(len==0?"":" "), cfg->tagname[id]);
23552ee382b6Ssthen 				len += strlen(buf+len);
23562ee382b6Ssthen 			}
23572ee382b6Ssthen 		}
23582ee382b6Ssthen 	}
23592ee382b6Ssthen 	return strdup(buf);
23602ee382b6Ssthen }
23612ee382b6Ssthen 
2362eaf2578eSsthen int taglist_intersect(uint8_t* list1, size_t list1len, const uint8_t* list2,
23632ee382b6Ssthen 	size_t list2len)
23642ee382b6Ssthen {
23652ee382b6Ssthen 	size_t i;
23662ee382b6Ssthen 	if(!list1 || !list2)
23672ee382b6Ssthen 		return 0;
23682ee382b6Ssthen 	for(i=0; i<list1len && i<list2len; i++) {
23692ee382b6Ssthen 		if((list1[i] & list2[i]) != 0)
23702ee382b6Ssthen 			return 1;
23712ee382b6Ssthen 	}
23722ee382b6Ssthen 	return 0;
23732ee382b6Ssthen }
23742ee382b6Ssthen 
2375933707f3Ssthen void
2376933707f3Ssthen config_apply(struct config_file* config)
2377933707f3Ssthen {
2378229e174cSsthen 	MAX_TTL = (time_t)config->max_ttl;
2379229e174cSsthen 	MIN_TTL = (time_t)config->min_ttl;
2380eaf2578eSsthen 	SERVE_EXPIRED = config->serve_expired;
23812308e98cSsthen 	SERVE_EXPIRED_TTL = (time_t)config->serve_expired_ttl;
2382eaf2578eSsthen 	SERVE_EXPIRED_REPLY_TTL = (time_t)config->serve_expired_reply_ttl;
23839982a05dSsthen 	SERVE_ORIGINAL_TTL = config->serve_original_ttl;
2384a58bff56Ssthen 	MAX_NEG_TTL = (time_t)config->max_negative_ttl;
23852bdc0ed1Ssthen 	MIN_NEG_TTL = (time_t)config->min_negative_ttl;
238631f127bbSsthen 	RTT_MIN_TIMEOUT = config->infra_cache_min_rtt;
2387d1e2768aSsthen 	RTT_MAX_TIMEOUT = config->infra_cache_max_rtt;
2388933707f3Ssthen 	EDNS_ADVERTISED_SIZE = (uint16_t)config->edns_buffer_size;
2389d8d14d0cSsthen 	MINIMAL_RESPONSES = config->minimal_responses;
2390d8d14d0cSsthen 	RRSET_ROUNDROBIN = config->rrset_roundrobin;
2391f6b99bafSsthen 	LOG_TAG_QUERYREPLY = config->log_tag_queryreply;
23923150e5f6Ssthen 	UNKNOWN_SERVER_NICENESS = config->unknown_server_time_limit;
2393d1e2768aSsthen 	USEFUL_SERVER_TOP_TIMEOUT = RTT_MAX_TIMEOUT;
2394d1e2768aSsthen 	BLACKLIST_PENALTY = USEFUL_SERVER_TOP_TIMEOUT*4;
2395933707f3Ssthen 	log_set_time_asc(config->log_time_ascii);
2396a961b961Ssthen 	autr_permit_small_holddown = config->permit_small_holddown;
2397f6b99bafSsthen 	stream_wait_max = config->stream_wait_size;
23982c144df0Ssthen 	http2_query_buffer_max = config->http_query_buffer_size;
23992c144df0Ssthen 	http2_response_buffer_max = config->http_response_buffer_size;
2400933707f3Ssthen }
2401933707f3Ssthen 
240231f127bbSsthen void config_lookup_uid(struct config_file* cfg)
240331f127bbSsthen {
240431f127bbSsthen #ifdef HAVE_GETPWNAM
240531f127bbSsthen 	/* translate username into uid and gid */
240631f127bbSsthen 	if(cfg->username && cfg->username[0]) {
240731f127bbSsthen 		struct passwd *pwd;
2408a58bff56Ssthen 		if((pwd = getpwnam(cfg->username)) != NULL) {
240947dfde74Sflorian 			cfg_uid = pwd->pw_uid;
241047dfde74Sflorian 			cfg_gid = pwd->pw_gid;
241131f127bbSsthen 		}
2412a58bff56Ssthen 	}
241331f127bbSsthen #else
241431f127bbSsthen 	(void)cfg;
241531f127bbSsthen #endif
241631f127bbSsthen }
241731f127bbSsthen 
2418933707f3Ssthen /**
2419933707f3Ssthen  * Calculate string length of full pathname in original filesys
2420933707f3Ssthen  * @param fname: the path name to convert.
2421933707f3Ssthen  * 	Must not be null or empty.
2422933707f3Ssthen  * @param cfg: config struct for chroot and chdir (if set).
2423933707f3Ssthen  * @param use_chdir: if false, only chroot is applied.
2424933707f3Ssthen  * @return length of string.
2425933707f3Ssthen  *	remember to allocate one more for 0 at end in mallocs.
2426933707f3Ssthen  */
2427933707f3Ssthen static size_t
2428933707f3Ssthen strlen_after_chroot(const char* fname, struct config_file* cfg, int use_chdir)
2429933707f3Ssthen {
2430933707f3Ssthen 	size_t len = 0;
2431933707f3Ssthen 	int slashit = 0;
2432933707f3Ssthen 	if(cfg->chrootdir && cfg->chrootdir[0] &&
2433933707f3Ssthen 		strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) {
2434933707f3Ssthen 		/* already full pathname, return it */
2435933707f3Ssthen 		return strlen(fname);
2436933707f3Ssthen 	}
2437933707f3Ssthen 	/* chroot */
2438933707f3Ssthen 	if(cfg->chrootdir && cfg->chrootdir[0]) {
2439933707f3Ssthen 		/* start with chrootdir */
2440933707f3Ssthen 		len += strlen(cfg->chrootdir);
2441933707f3Ssthen 		slashit = 1;
2442933707f3Ssthen 	}
2443933707f3Ssthen 	/* chdir */
2444933707f3Ssthen #ifdef UB_ON_WINDOWS
2445933707f3Ssthen 	if(fname[0] != 0 && fname[1] == ':') {
2446933707f3Ssthen 		/* full path, no chdir */
2447933707f3Ssthen 	} else
2448933707f3Ssthen #endif
2449933707f3Ssthen 	if(fname[0] == '/' || !use_chdir) {
2450933707f3Ssthen 		/* full path, no chdir */
2451933707f3Ssthen 	} else if(cfg->directory && cfg->directory[0]) {
2452933707f3Ssthen 		/* prepend chdir */
2453933707f3Ssthen 		if(slashit && cfg->directory[0] != '/')
2454933707f3Ssthen 			len++;
2455933707f3Ssthen 		if(cfg->chrootdir && cfg->chrootdir[0] &&
2456933707f3Ssthen 			strncmp(cfg->chrootdir, cfg->directory,
2457933707f3Ssthen 			strlen(cfg->chrootdir)) == 0)
2458933707f3Ssthen 			len += strlen(cfg->directory)-strlen(cfg->chrootdir);
2459933707f3Ssthen 		else	len += strlen(cfg->directory);
2460933707f3Ssthen 		slashit = 1;
2461933707f3Ssthen 	}
2462933707f3Ssthen 	/* fname */
2463933707f3Ssthen 	if(slashit && fname[0] != '/')
2464933707f3Ssthen 		len++;
2465933707f3Ssthen 	len += strlen(fname);
2466933707f3Ssthen 	return len;
2467933707f3Ssthen }
2468933707f3Ssthen 
2469933707f3Ssthen char*
2470933707f3Ssthen fname_after_chroot(const char* fname, struct config_file* cfg, int use_chdir)
2471933707f3Ssthen {
24725d76a658Ssthen 	size_t len = strlen_after_chroot(fname, cfg, use_chdir)+1;
2473933707f3Ssthen 	int slashit = 0;
24745d76a658Ssthen 	char* buf = (char*)malloc(len);
2475933707f3Ssthen 	if(!buf)
2476933707f3Ssthen 		return NULL;
2477933707f3Ssthen 	buf[0] = 0;
2478933707f3Ssthen 	/* is fname already in chroot ? */
2479933707f3Ssthen 	if(cfg->chrootdir && cfg->chrootdir[0] &&
2480933707f3Ssthen 		strncmp(cfg->chrootdir, fname, strlen(cfg->chrootdir)) == 0) {
2481933707f3Ssthen 		/* already full pathname, return it */
24825d76a658Ssthen 		(void)strlcpy(buf, fname, len);
24835d76a658Ssthen 		buf[len-1] = 0;
2484933707f3Ssthen 		return buf;
2485933707f3Ssthen 	}
2486933707f3Ssthen 	/* chroot */
2487933707f3Ssthen 	if(cfg->chrootdir && cfg->chrootdir[0]) {
2488933707f3Ssthen 		/* start with chrootdir */
24895d76a658Ssthen 		(void)strlcpy(buf, cfg->chrootdir, len);
2490933707f3Ssthen 		slashit = 1;
2491933707f3Ssthen 	}
2492933707f3Ssthen #ifdef UB_ON_WINDOWS
2493933707f3Ssthen 	if(fname[0] != 0 && fname[1] == ':') {
2494933707f3Ssthen 		/* full path, no chdir */
2495933707f3Ssthen 	} else
2496933707f3Ssthen #endif
2497933707f3Ssthen 	/* chdir */
2498933707f3Ssthen 	if(fname[0] == '/' || !use_chdir) {
2499933707f3Ssthen 		/* full path, no chdir */
2500933707f3Ssthen 	} else if(cfg->directory && cfg->directory[0]) {
2501933707f3Ssthen 		/* prepend chdir */
2502933707f3Ssthen 		if(slashit && cfg->directory[0] != '/')
25035d76a658Ssthen 			(void)strlcat(buf, "/", len);
2504933707f3Ssthen 		/* is the directory already in the chroot? */
2505933707f3Ssthen 		if(cfg->chrootdir && cfg->chrootdir[0] &&
2506933707f3Ssthen 			strncmp(cfg->chrootdir, cfg->directory,
2507933707f3Ssthen 			strlen(cfg->chrootdir)) == 0)
25085d76a658Ssthen 			(void)strlcat(buf, cfg->directory+strlen(cfg->chrootdir),
25095d76a658Ssthen 				   len);
25105d76a658Ssthen 		else (void)strlcat(buf, cfg->directory, len);
2511933707f3Ssthen 		slashit = 1;
2512933707f3Ssthen 	}
2513933707f3Ssthen 	/* fname */
2514933707f3Ssthen 	if(slashit && fname[0] != '/')
25155d76a658Ssthen 		(void)strlcat(buf, "/", len);
25165d76a658Ssthen 	(void)strlcat(buf, fname, len);
25175d76a658Ssthen 	buf[len-1] = 0;
2518933707f3Ssthen 	return buf;
2519933707f3Ssthen }
2520933707f3Ssthen 
2521933707f3Ssthen /** return next space character in string */
2522933707f3Ssthen static char* next_space_pos(const char* str)
2523933707f3Ssthen {
2524933707f3Ssthen 	char* sp = strchr(str, ' ');
2525933707f3Ssthen 	char* tab = strchr(str, '\t');
2526933707f3Ssthen 	if(!tab && !sp)
2527933707f3Ssthen 		return NULL;
2528933707f3Ssthen 	if(!sp) return tab;
2529933707f3Ssthen 	if(!tab) return sp;
2530933707f3Ssthen 	return (sp<tab)?sp:tab;
2531933707f3Ssthen }
2532933707f3Ssthen 
2533933707f3Ssthen /** return last space character in string */
2534933707f3Ssthen static char* last_space_pos(const char* str)
2535933707f3Ssthen {
2536933707f3Ssthen 	char* sp = strrchr(str, ' ');
2537933707f3Ssthen 	char* tab = strrchr(str, '\t');
2538933707f3Ssthen 	if(!tab && !sp)
2539933707f3Ssthen 		return NULL;
2540933707f3Ssthen 	if(!sp) return tab;
2541933707f3Ssthen 	if(!tab) return sp;
2542933707f3Ssthen 	return (sp>tab)?sp:tab;
2543933707f3Ssthen }
2544933707f3Ssthen 
2545933707f3Ssthen int
2546933707f3Ssthen cfg_parse_local_zone(struct config_file* cfg, const char* val)
2547933707f3Ssthen {
2548933707f3Ssthen 	const char *type, *name_end, *name;
2549933707f3Ssthen 	char buf[256];
2550933707f3Ssthen 
2551933707f3Ssthen 	/* parse it as: [zone_name] [between stuff] [zone_type] */
2552933707f3Ssthen 	name = val;
2553e10d3884Sbrad 	while(*name && isspace((unsigned char)*name))
2554933707f3Ssthen 		name++;
2555933707f3Ssthen 	if(!*name) {
2556933707f3Ssthen 		log_err("syntax error: too short: %s", val);
2557933707f3Ssthen 		return 0;
2558933707f3Ssthen 	}
2559933707f3Ssthen 	name_end = next_space_pos(name);
2560933707f3Ssthen 	if(!name_end || !*name_end) {
2561933707f3Ssthen 		log_err("syntax error: expected zone type: %s", val);
2562933707f3Ssthen 		return 0;
2563933707f3Ssthen 	}
2564933707f3Ssthen 	if (name_end - name > 255) {
2565933707f3Ssthen 		log_err("syntax error: bad zone name: %s", val);
2566933707f3Ssthen 		return 0;
2567933707f3Ssthen 	}
25685d76a658Ssthen 	(void)strlcpy(buf, name, sizeof(buf));
2569933707f3Ssthen 	buf[name_end-name] = '\0';
2570933707f3Ssthen 
2571933707f3Ssthen 	type = last_space_pos(name_end);
2572e10d3884Sbrad 	while(type && *type && isspace((unsigned char)*type))
2573933707f3Ssthen 		type++;
2574933707f3Ssthen 	if(!type || !*type) {
2575933707f3Ssthen 		log_err("syntax error: expected zone type: %s", val);
2576933707f3Ssthen 		return 0;
2577933707f3Ssthen 	}
2578933707f3Ssthen 
2579933707f3Ssthen 	if(strcmp(type, "nodefault")==0) {
2580933707f3Ssthen 		return cfg_strlist_insert(&cfg->local_zones_nodefault,
2581933707f3Ssthen 			strdup(name));
25828240c1b9Ssthen #ifdef USE_IPSET
25838240c1b9Ssthen 	} else if(strcmp(type, "ipset")==0) {
25848240c1b9Ssthen 		return cfg_strlist_insert(&cfg->local_zones_ipset,
25858240c1b9Ssthen 			strdup(name));
25868240c1b9Ssthen #endif
2587933707f3Ssthen 	} else {
2588933707f3Ssthen 		return cfg_str2list_insert(&cfg->local_zones, strdup(buf),
2589933707f3Ssthen 			strdup(type));
2590933707f3Ssthen 	}
2591933707f3Ssthen }
2592933707f3Ssthen 
2593933707f3Ssthen char* cfg_ptr_reverse(char* str)
2594933707f3Ssthen {
2595933707f3Ssthen 	char* ip, *ip_end;
2596933707f3Ssthen 	char* name;
2597933707f3Ssthen 	char* result;
2598933707f3Ssthen 	char buf[1024];
2599933707f3Ssthen 	struct sockaddr_storage addr;
2600933707f3Ssthen 	socklen_t addrlen;
2601933707f3Ssthen 
2602933707f3Ssthen 	/* parse it as: [IP] [between stuff] [name] */
2603933707f3Ssthen 	ip = str;
2604e10d3884Sbrad 	while(*ip && isspace((unsigned char)*ip))
2605933707f3Ssthen 		ip++;
2606933707f3Ssthen 	if(!*ip) {
2607933707f3Ssthen 		log_err("syntax error: too short: %s", str);
2608933707f3Ssthen 		return NULL;
2609933707f3Ssthen 	}
2610933707f3Ssthen 	ip_end = next_space_pos(ip);
2611933707f3Ssthen 	if(!ip_end || !*ip_end) {
2612933707f3Ssthen 		log_err("syntax error: expected name: %s", str);
2613933707f3Ssthen 		return NULL;
2614933707f3Ssthen 	}
2615933707f3Ssthen 
2616933707f3Ssthen 	name = last_space_pos(ip_end);
2617933707f3Ssthen 	if(!name || !*name) {
2618933707f3Ssthen 		log_err("syntax error: expected name: %s", str);
2619933707f3Ssthen 		return NULL;
2620933707f3Ssthen 	}
2621933707f3Ssthen 
2622933707f3Ssthen 	sscanf(ip, "%100s", buf);
2623933707f3Ssthen 	buf[sizeof(buf)-1]=0;
2624933707f3Ssthen 
2625933707f3Ssthen 	if(!ipstrtoaddr(buf, UNBOUND_DNS_PORT, &addr, &addrlen)) {
2626933707f3Ssthen 		log_err("syntax error: cannot parse address: %s", str);
2627933707f3Ssthen 		return NULL;
2628933707f3Ssthen 	}
2629933707f3Ssthen 
2630933707f3Ssthen 	/* reverse IPv4:
2631933707f3Ssthen 	 * ddd.ddd.ddd.ddd.in-addr-arpa.
2632933707f3Ssthen 	 * IPv6: (h.){32}.ip6.arpa.  */
2633933707f3Ssthen 
2634933707f3Ssthen 	if(addr_is_ip6(&addr, addrlen)) {
2635933707f3Ssthen 		uint8_t ad[16];
2636933707f3Ssthen 		const char* hex = "0123456789abcdef";
2637933707f3Ssthen 		char *p = buf;
2638933707f3Ssthen 		int i;
2639933707f3Ssthen 		memmove(ad, &((struct sockaddr_in6*)&addr)->sin6_addr,
2640933707f3Ssthen 			sizeof(ad));
2641933707f3Ssthen 		for(i=15; i>=0; i--) {
2642933707f3Ssthen 			uint8_t b = ad[i];
2643933707f3Ssthen 			*p++ = hex[ (b&0x0f) ];
2644933707f3Ssthen 			*p++ = '.';
2645933707f3Ssthen 			*p++ = hex[ (b&0xf0) >> 4 ];
2646933707f3Ssthen 			*p++ = '.';
2647933707f3Ssthen 		}
2648933707f3Ssthen 		snprintf(buf+16*4, sizeof(buf)-16*4, "ip6.arpa. ");
2649933707f3Ssthen 	} else {
2650933707f3Ssthen 		uint8_t ad[4];
2651933707f3Ssthen 		memmove(ad, &((struct sockaddr_in*)&addr)->sin_addr,
2652933707f3Ssthen 			sizeof(ad));
2653933707f3Ssthen 		snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa. ",
2654933707f3Ssthen 			(unsigned)ad[3], (unsigned)ad[2],
2655933707f3Ssthen 			(unsigned)ad[1], (unsigned)ad[0]);
2656933707f3Ssthen 	}
2657933707f3Ssthen 
2658933707f3Ssthen 	/* printed the reverse address, now the between goop and name on end */
2659e10d3884Sbrad 	while(*ip_end && isspace((unsigned char)*ip_end))
2660933707f3Ssthen 		ip_end++;
2661933707f3Ssthen 	if(name>ip_end) {
2662933707f3Ssthen 		snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%.*s",
2663933707f3Ssthen 			(int)(name-ip_end), ip_end);
2664933707f3Ssthen 	}
2665933707f3Ssthen 	snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), " PTR %s", name);
2666933707f3Ssthen 
2667933707f3Ssthen 	result = strdup(buf);
2668933707f3Ssthen 	if(!result) {
2669933707f3Ssthen 		log_err("out of memory parsing %s", str);
2670933707f3Ssthen 		return NULL;
2671933707f3Ssthen 	}
2672933707f3Ssthen 	return result;
2673933707f3Ssthen }
2674933707f3Ssthen 
2675d8d14d0cSsthen #ifdef UB_ON_WINDOWS
2676d8d14d0cSsthen char*
2677d8d14d0cSsthen w_lookup_reg_str(const char* key, const char* name)
2678d8d14d0cSsthen {
2679d8d14d0cSsthen 	HKEY hk = NULL;
2680d8d14d0cSsthen 	DWORD type = 0;
2681d8d14d0cSsthen 	BYTE buf[1024];
2682d8d14d0cSsthen 	DWORD len = (DWORD)sizeof(buf);
2683d8d14d0cSsthen 	LONG ret;
2684d8d14d0cSsthen 	char* result = NULL;
2685d8d14d0cSsthen 	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hk);
2686d8d14d0cSsthen 	if(ret == ERROR_FILE_NOT_FOUND)
2687d8d14d0cSsthen 		return NULL; /* key does not exist */
2688d8d14d0cSsthen 	else if(ret != ERROR_SUCCESS) {
2689d8d14d0cSsthen 		log_err("RegOpenKeyEx failed");
2690d8d14d0cSsthen 		return NULL;
2691d8d14d0cSsthen 	}
2692d8d14d0cSsthen 	ret = RegQueryValueEx(hk, (LPCTSTR)name, 0, &type, buf, &len);
2693d8d14d0cSsthen 	if(RegCloseKey(hk))
2694d8d14d0cSsthen 		log_err("RegCloseKey");
2695d8d14d0cSsthen 	if(ret == ERROR_FILE_NOT_FOUND)
2696d8d14d0cSsthen 		return NULL; /* name does not exist */
2697d8d14d0cSsthen 	else if(ret != ERROR_SUCCESS) {
2698d8d14d0cSsthen 		log_err("RegQueryValueEx failed");
2699d8d14d0cSsthen 		return NULL;
2700d8d14d0cSsthen 	}
2701d8d14d0cSsthen 	if(type == REG_SZ || type == REG_MULTI_SZ || type == REG_EXPAND_SZ) {
2702d8d14d0cSsthen 		buf[sizeof(buf)-1] = 0;
2703d8d14d0cSsthen 		buf[sizeof(buf)-2] = 0; /* for multi_sz */
2704d8d14d0cSsthen 		result = strdup((char*)buf);
2705d8d14d0cSsthen 		if(!result) log_err("out of memory");
2706d8d14d0cSsthen 	}
2707d8d14d0cSsthen 	return result;
2708d8d14d0cSsthen }
270924893edcSsthen 
271024893edcSsthen void w_config_adjust_directory(struct config_file* cfg)
271124893edcSsthen {
271224893edcSsthen 	if(cfg->directory && cfg->directory[0]) {
271324893edcSsthen 		TCHAR dirbuf[2*MAX_PATH+4];
271424893edcSsthen 		if(strcmp(cfg->directory, "%EXECUTABLE%") == 0) {
271524893edcSsthen 			/* get executable path, and if that contains
271624893edcSsthen 			 * directories, snip off the filename part */
271724893edcSsthen 			dirbuf[0] = 0;
271824893edcSsthen 			if(!GetModuleFileName(NULL, dirbuf, MAX_PATH))
271924893edcSsthen 				log_err("could not GetModuleFileName");
272024893edcSsthen 			if(strrchr(dirbuf, '\\')) {
272124893edcSsthen 				(strrchr(dirbuf, '\\'))[0] = 0;
272224893edcSsthen 			} else log_err("GetModuleFileName had no path");
272324893edcSsthen 			if(dirbuf[0]) {
272424893edcSsthen 				/* adjust directory for later lookups to work*/
272524893edcSsthen 				free(cfg->directory);
272624893edcSsthen 				cfg->directory = memdup(dirbuf, strlen(dirbuf)+1);
272724893edcSsthen 			}
272824893edcSsthen 		}
272924893edcSsthen 	}
273024893edcSsthen }
2731d8d14d0cSsthen #endif /* UB_ON_WINDOWS */
2732d8d14d0cSsthen 
273320237c55Ssthen int options_remote_is_address(struct config_file* cfg)
273420237c55Ssthen {
273520237c55Ssthen 	if(!cfg->remote_control_enable) return 0;
273620237c55Ssthen 	if(!cfg->control_ifs.first) return 1;
273720237c55Ssthen 	if(!cfg->control_ifs.first->str) return 1;
273820237c55Ssthen 	if(cfg->control_ifs.first->str[0] == 0) return 1;
273920237c55Ssthen 	return (cfg->control_ifs.first->str[0] != '/');
274020237c55Ssthen }
27418240c1b9Ssthen 
2742191f22c6Ssthen /** see if interface is https, its port number == the https port number */
2743191f22c6Ssthen int
2744191f22c6Ssthen if_is_https(const char* ifname, const char* port, int https_port)
2745191f22c6Ssthen {
2746191f22c6Ssthen 	char* p = strchr(ifname, '@');
2747191f22c6Ssthen 	if(!p && atoi(port) == https_port)
2748191f22c6Ssthen 		return 1;
2749191f22c6Ssthen 	if(p && atoi(p+1) == https_port)
2750191f22c6Ssthen 		return 1;
2751191f22c6Ssthen 	return 0;
2752191f22c6Ssthen }
2753191f22c6Ssthen 
2754191f22c6Ssthen /** see if config contains https turned on */
2755191f22c6Ssthen int cfg_has_https(struct config_file* cfg)
2756191f22c6Ssthen {
2757191f22c6Ssthen 	int i;
2758191f22c6Ssthen 	char portbuf[32];
2759191f22c6Ssthen 	snprintf(portbuf, sizeof(portbuf), "%d", cfg->port);
2760191f22c6Ssthen 	for(i = 0; i<cfg->num_ifs; i++) {
2761191f22c6Ssthen 		if(if_is_https(cfg->ifs[i], portbuf, cfg->https_port))
2762191f22c6Ssthen 			return 1;
2763191f22c6Ssthen 	}
2764191f22c6Ssthen 	return 0;
2765191f22c6Ssthen }
276645872187Ssthen 
276745872187Ssthen /** see if interface is PROXYv2, its port number == the proxy port number */
276845872187Ssthen int
276945872187Ssthen if_is_pp2(const char* ifname, const char* port,
277045872187Ssthen 	struct config_strlist* proxy_protocol_port)
277145872187Ssthen {
277245872187Ssthen 	struct config_strlist* s;
277345872187Ssthen 	char* p = strchr(ifname, '@');
277445872187Ssthen 	for(s = proxy_protocol_port; s; s = s->next) {
277545872187Ssthen 		if(p && atoi(p+1) == atoi(s->str))
277645872187Ssthen 			return 1;
277745872187Ssthen 		if(!p && atoi(port) == atoi(s->str))
277845872187Ssthen 			return 1;
277945872187Ssthen 	}
278045872187Ssthen 	return 0;
278145872187Ssthen }
278245872187Ssthen 
278345872187Ssthen /** see if interface is DNSCRYPT, its port number == the dnscrypt port number */
278445872187Ssthen int
278545872187Ssthen if_is_dnscrypt(const char* ifname, const char* port, int dnscrypt_port)
278645872187Ssthen {
278745872187Ssthen #ifdef USE_DNSCRYPT
278845872187Ssthen 	return ((strchr(ifname, '@') &&
278945872187Ssthen 		atoi(strchr(ifname, '@')+1) == dnscrypt_port) ||
279045872187Ssthen 		(!strchr(ifname, '@') && atoi(port) == dnscrypt_port));
279145872187Ssthen #else
279245872187Ssthen 	(void)ifname;
279345872187Ssthen 	(void)port;
279445872187Ssthen 	(void)dnscrypt_port;
279545872187Ssthen 	return 0;
279645872187Ssthen #endif
279745872187Ssthen }
2798