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