1238106Sdes/*
2238106Sdes * util/config_file.h - reads and stores the config file for unbound.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains functions for the config file.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef UTIL_CONFIG_FILE_H
43238106Sdes#define UTIL_CONFIG_FILE_H
44238106Sdesstruct config_stub;
45238106Sdesstruct config_strlist;
46238106Sdesstruct config_str2list;
47238106Sdesstruct module_qstate;
48238106Sdesstruct sock_list;
49238106Sdesstruct ub_packed_rrset_key;
50238106Sdes
51238106Sdes/**
52238106Sdes * The configuration options.
53238106Sdes * Strings are malloced.
54238106Sdes */
55238106Sdesstruct config_file {
56238106Sdes	/** verbosity level as specified in the config file */
57238106Sdes	int verbosity;
58238106Sdes
59238106Sdes	/** statistics interval (in seconds) */
60238106Sdes	int stat_interval;
61238106Sdes	/** if false, statistics values are reset after printing them */
62238106Sdes	int stat_cumulative;
63238106Sdes	/** if true, the statistics are kept in greater detail */
64238106Sdes	int stat_extended;
65238106Sdes
66238106Sdes	/** number of threads to create */
67238106Sdes	int num_threads;
68238106Sdes
69238106Sdes	/** port on which queries are answered. */
70238106Sdes	int port;
71238106Sdes	/** do ip4 query support. */
72238106Sdes	int do_ip4;
73238106Sdes	/** do ip6 query support. */
74238106Sdes	int do_ip6;
75238106Sdes	/** do udp query support. */
76238106Sdes	int do_udp;
77238106Sdes	/** do tcp query support. */
78238106Sdes	int do_tcp;
79238106Sdes	/** tcp upstream queries (no UDP upstream queries) */
80238106Sdes	int tcp_upstream;
81238106Sdes
82238106Sdes	/** private key file for dnstcp-ssl service (enabled if not NULL) */
83238106Sdes	char* ssl_service_key;
84238106Sdes	/** public key file for dnstcp-ssl service */
85238106Sdes	char* ssl_service_pem;
86238106Sdes	/** port on which to provide ssl service */
87238106Sdes	int ssl_port;
88238106Sdes	/** if outgoing tcp connections use SSL */
89238106Sdes	int ssl_upstream;
90238106Sdes
91238106Sdes	/** outgoing port range number of ports (per thread) */
92238106Sdes	int outgoing_num_ports;
93238106Sdes	/** number of outgoing tcp buffers per (per thread) */
94238106Sdes	size_t outgoing_num_tcp;
95238106Sdes	/** number of incoming tcp buffers per (per thread) */
96238106Sdes	size_t incoming_num_tcp;
97238106Sdes	/** allowed udp port numbers, array with 0 if not allowed */
98238106Sdes	int* outgoing_avail_ports;
99238106Sdes
100238106Sdes	/** EDNS buffer size to use */
101238106Sdes	size_t edns_buffer_size;
102238106Sdes	/** number of bytes buffer size for DNS messages */
103238106Sdes	size_t msg_buffer_size;
104238106Sdes	/** size of the message cache */
105238106Sdes	size_t msg_cache_size;
106238106Sdes	/** slabs in the message cache. */
107238106Sdes	size_t msg_cache_slabs;
108238106Sdes	/** number of queries every thread can service */
109238106Sdes	size_t num_queries_per_thread;
110238106Sdes	/** number of msec to wait before items can be jostled out */
111238106Sdes	size_t jostle_time;
112238106Sdes	/** size of the rrset cache */
113238106Sdes	size_t rrset_cache_size;
114238106Sdes	/** slabs in the rrset cache */
115238106Sdes	size_t rrset_cache_slabs;
116238106Sdes	/** host cache ttl in seconds */
117238106Sdes	int host_ttl;
118238106Sdes	/** number of slabs in the infra host cache */
119238106Sdes	size_t infra_cache_slabs;
120238106Sdes	/** max number of hosts in the infra cache */
121238106Sdes	size_t infra_cache_numhosts;
122285206Sdes	/** min value for infra cache rtt */
123285206Sdes	int infra_cache_min_rtt;
124269257Sdes	/** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
125269257Sdes	int delay_close;
126238106Sdes
127238106Sdes	/** the target fetch policy for the iterator */
128238106Sdes	char* target_fetch_policy;
129238106Sdes
130238106Sdes	/** automatic interface for incoming messages. Uses ipv6 remapping,
131238106Sdes	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
132238106Sdes	int if_automatic;
133238106Sdes	/** SO_RCVBUF size to set on port 53 UDP socket */
134238106Sdes	size_t so_rcvbuf;
135238106Sdes	/** SO_SNDBUF size to set on port 53 UDP socket */
136238106Sdes	size_t so_sndbuf;
137269257Sdes	/** SO_REUSEPORT requested on port 53 sockets */
138269257Sdes	int so_reuseport;
139291767Sdes	/** IP_TRANSPARENT socket option requested on port 53 sockets */
140291767Sdes	int ip_transparent;
141238106Sdes
142238106Sdes	/** number of interfaces to open. If 0 default all interfaces. */
143238106Sdes	int num_ifs;
144238106Sdes	/** interface description strings (IP addresses) */
145238106Sdes	char **ifs;
146238106Sdes
147238106Sdes	/** number of outgoing interfaces to open.
148238106Sdes	 * If 0 default all interfaces. */
149238106Sdes	int num_out_ifs;
150238106Sdes	/** outgoing interface description strings (IP addresses) */
151238106Sdes	char **out_ifs;
152238106Sdes
153238106Sdes	/** the root hints */
154238106Sdes	struct config_strlist* root_hints;
155238106Sdes	/** the stub definitions, linked list */
156238106Sdes	struct config_stub* stubs;
157238106Sdes	/** the forward zone definitions, linked list */
158238106Sdes	struct config_stub* forwards;
159238106Sdes	/** list of donotquery addresses, linked list */
160238106Sdes	struct config_strlist* donotqueryaddrs;
161238106Sdes	/** list of access control entries, linked list */
162238106Sdes	struct config_str2list* acls;
163238106Sdes	/** use default localhost donotqueryaddr entries */
164238106Sdes	int donotquery_localhost;
165238106Sdes
166238106Sdes	/** harden against very small edns buffer sizes */
167238106Sdes	int harden_short_bufsize;
168238106Sdes	/** harden against very large query sizes */
169238106Sdes	int harden_large_queries;
170238106Sdes	/** harden against spoofed glue (out of zone data) */
171238106Sdes	int harden_glue;
172238106Sdes	/** harden against receiving no DNSSEC data for trust anchor */
173238106Sdes	int harden_dnssec_stripped;
174238106Sdes	/** harden against queries that fall under known nxdomain names */
175238106Sdes	int harden_below_nxdomain;
176238106Sdes	/** harden the referral path, query for NS,A,AAAA and validate */
177238106Sdes	int harden_referral_path;
178291767Sdes	/** harden against algorithm downgrade */
179291767Sdes	int harden_algo_downgrade;
180238106Sdes	/** use 0x20 bits in query as random ID bits */
181238106Sdes	int use_caps_bits_for_id;
182291767Sdes	/** 0x20 whitelist, domains that do not use capsforid */
183291767Sdes	struct config_strlist* caps_whitelist;
184238106Sdes	/** strip away these private addrs from answers, no DNS Rebinding */
185238106Sdes	struct config_strlist* private_address;
186238106Sdes	/** allow domain (and subdomains) to use private address space */
187238106Sdes	struct config_strlist* private_domain;
188238106Sdes	/** what threshold for unwanted action. */
189238106Sdes	size_t unwanted_threshold;
190238106Sdes	/** the number of seconds maximal TTL used for RRsets and messages */
191238106Sdes	int max_ttl;
192238106Sdes	/** the number of seconds minimum TTL used for RRsets and messages */
193238106Sdes	int min_ttl;
194291767Sdes	/** the number of seconds maximal negative TTL for SOA in auth */
195291767Sdes	int max_negative_ttl;
196238106Sdes	/** if prefetching of messages should be performed. */
197238106Sdes	int prefetch;
198238106Sdes	/** if prefetching of DNSKEYs should be performed. */
199238106Sdes	int prefetch_key;
200238106Sdes
201238106Sdes	/** chrootdir, if not "" or chroot will be done */
202238106Sdes	char* chrootdir;
203238106Sdes	/** username to change to, if not "". */
204238106Sdes	char* username;
205238106Sdes	/** working directory */
206238106Sdes	char* directory;
207238106Sdes	/** filename to log to. */
208238106Sdes	char* logfile;
209238106Sdes	/** pidfile to write pid to. */
210238106Sdes	char* pidfile;
211238106Sdes
212238106Sdes	/** should log messages be sent to syslogd */
213238106Sdes	int use_syslog;
214238106Sdes	/** log timestamp in ascii UTC */
215238106Sdes	int log_time_ascii;
216238106Sdes	/** log queries with one line per query */
217238106Sdes	int log_queries;
218238106Sdes
219238106Sdes	/** do not report identity (id.server, hostname.bind) */
220238106Sdes	int hide_identity;
221238106Sdes	/** do not report version (version.server, version.bind) */
222238106Sdes	int hide_version;
223238106Sdes	/** identity, hostname is returned if "". */
224238106Sdes	char* identity;
225238106Sdes	/** version, package version returned if "". */
226238106Sdes	char* version;
227238106Sdes
228238106Sdes	/** the module configuration string */
229238106Sdes	char* module_conf;
230238106Sdes
231238106Sdes	/** files with trusted DS and DNSKEYs in zonefile format, list */
232238106Sdes	struct config_strlist* trust_anchor_file_list;
233238106Sdes	/** list of trustanchor keys, linked list */
234238106Sdes	struct config_strlist* trust_anchor_list;
235238106Sdes	/** files with 5011 autotrust tracked keys */
236238106Sdes	struct config_strlist* auto_trust_anchor_file_list;
237238106Sdes	/** files with trusted DNSKEYs in named.conf format, list */
238238106Sdes	struct config_strlist* trusted_keys_file_list;
239238106Sdes	/** DLV anchor file */
240238106Sdes	char* dlv_anchor_file;
241238106Sdes	/** DLV anchor inline */
242238106Sdes	struct config_strlist* dlv_anchor_list;
243238106Sdes	/** insecure domain list */
244238106Sdes	struct config_strlist* domain_insecure;
245238106Sdes
246238106Sdes	/** if not 0, this value is the validation date for RRSIGs */
247238106Sdes	int32_t val_date_override;
248238106Sdes	/** the minimum for signature clock skew */
249238106Sdes	int32_t val_sig_skew_min;
250238106Sdes	/** the maximum for signature clock skew */
251238106Sdes	int32_t val_sig_skew_max;
252238106Sdes	/** this value sets the number of seconds before revalidating bogus */
253238106Sdes	int bogus_ttl;
254238106Sdes	/** should validator clean additional section for secure msgs */
255238106Sdes	int val_clean_additional;
256238106Sdes	/** log bogus messages by the validator */
257238106Sdes	int val_log_level;
258238106Sdes	/** squelch val_log_level to log - this is library goes to callback */
259238106Sdes	int val_log_squelch;
260238106Sdes	/** should validator allow bogus messages to go through */
261238106Sdes	int val_permissive_mode;
262238106Sdes	/** ignore the CD flag in incoming queries and refuse them bogus data */
263238106Sdes	int ignore_cd;
264238106Sdes	/** nsec3 maximum iterations per key size, string */
265238106Sdes	char* val_nsec3_key_iterations;
266238106Sdes	/** autotrust add holddown time, in seconds */
267238106Sdes	unsigned int add_holddown;
268238106Sdes	/** autotrust del holddown time, in seconds */
269238106Sdes	unsigned int del_holddown;
270238106Sdes	/** autotrust keep_missing time, in seconds. 0 is forever. */
271238106Sdes	unsigned int keep_missing;
272291767Sdes	/** permit small holddown values, allowing 5011 rollover very fast */
273291767Sdes	int permit_small_holddown;
274238106Sdes
275238106Sdes	/** size of the key cache */
276238106Sdes	size_t key_cache_size;
277238106Sdes	/** slabs in the key cache. */
278238106Sdes	size_t key_cache_slabs;
279238106Sdes	/** size of the neg cache */
280238106Sdes	size_t neg_cache_size;
281238106Sdes
282238106Sdes	/** local zones config */
283238106Sdes	struct config_str2list* local_zones;
284238106Sdes	/** local zones nodefault list */
285238106Sdes	struct config_strlist* local_zones_nodefault;
286294190Sdes	/** local data RRs configured */
287238106Sdes	struct config_strlist* local_data;
288295691Sdes	/** unblock lan zones (reverse lookups for AS112 zones) */
289269257Sdes	int unblock_lan_zones;
290295691Sdes	/** insecure lan zones (don't validate AS112 zones) */
291295691Sdes	int insecure_lan_zones;
292238106Sdes
293238106Sdes	/** remote control section. enable toggle. */
294238106Sdes	int remote_control_enable;
295238106Sdes	/** the interfaces the remote control should listen on */
296238106Sdes	struct config_strlist* control_ifs;
297238106Sdes	/** port number for the control port */
298238106Sdes	int control_port;
299285206Sdes	/** use certificates for remote control */
300285206Sdes	int remote_control_use_cert;
301238106Sdes	/** private key file for server */
302238106Sdes	char* server_key_file;
303238106Sdes	/** certificate file for server */
304238106Sdes	char* server_cert_file;
305238106Sdes	/** private key file for unbound-control */
306238106Sdes	char* control_key_file;
307238106Sdes	/** certificate file for unbound-control */
308238106Sdes	char* control_cert_file;
309238106Sdes
310238106Sdes	/** Python script file */
311238106Sdes	char* python_script;
312238106Sdes
313238106Sdes	/** daemonize, i.e. fork into the background. */
314238106Sdes	int do_daemonize;
315238106Sdes
316238106Sdes	/* minimal response when positive answer */
317238106Sdes	int minimal_responses;
318238106Sdes
319238106Sdes	/* RRSet roundrobin */
320238106Sdes	int rrset_roundrobin;
321269257Sdes
322269257Sdes	/* maximum UDP response size */
323269257Sdes	size_t max_udp_size;
324285206Sdes
325285206Sdes	/* DNS64 prefix */
326285206Sdes	char* dns64_prefix;
327285206Sdes
328285206Sdes	/* Synthetize all AAAA record despite the presence of an authoritative one */
329285206Sdes	int dns64_synthall;
330285206Sdes
331285206Sdes	/** true to enable dnstap support */
332285206Sdes	int dnstap;
333285206Sdes	/** dnstap socket path */
334285206Sdes	char* dnstap_socket_path;
335285206Sdes	/** true to send "identity" via dnstap */
336285206Sdes	int dnstap_send_identity;
337285206Sdes	/** true to send "version" via dnstap */
338285206Sdes	int dnstap_send_version;
339285206Sdes	/** dnstap "identity", hostname is used if "". */
340285206Sdes	char* dnstap_identity;
341285206Sdes	/** dnstap "version", package version is used if "". */
342285206Sdes	char* dnstap_version;
343285206Sdes
344285206Sdes	/** true to log dnstap RESOLVER_QUERY message events */
345285206Sdes	int dnstap_log_resolver_query_messages;
346285206Sdes	/** true to log dnstap RESOLVER_RESPONSE message events */
347285206Sdes	int dnstap_log_resolver_response_messages;
348285206Sdes	/** true to log dnstap CLIENT_QUERY message events */
349285206Sdes	int dnstap_log_client_query_messages;
350285206Sdes	/** true to log dnstap CLIENT_RESPONSE message events */
351285206Sdes	int dnstap_log_client_response_messages;
352285206Sdes	/** true to log dnstap FORWARDER_QUERY message events */
353285206Sdes	int dnstap_log_forwarder_query_messages;
354285206Sdes	/** true to log dnstap FORWARDER_RESPONSE message events */
355285206Sdes	int dnstap_log_forwarder_response_messages;
356291767Sdes
357291767Sdes	/** ratelimit 0 is off, otherwise qps (unless overridden) */
358291767Sdes	int ratelimit;
359291767Sdes	/** number of slabs for ratelimit cache */
360291767Sdes	size_t ratelimit_slabs;
361291767Sdes	/** memory size in bytes for ratelimit cache */
362291767Sdes	size_t ratelimit_size;
363291767Sdes	/** ratelimits for domain (exact match) */
364291767Sdes	struct config_str2list* ratelimit_for_domain;
365291767Sdes	/** ratelimits below domain */
366291767Sdes	struct config_str2list* ratelimit_below_domain;
367291767Sdes	/** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
368291767Sdes	int ratelimit_factor;
369294190Sdes	/** minimise outgoing QNAME and hide original QTYPE if possible */
370294190Sdes	int qname_minimisation;
371238106Sdes};
372238106Sdes
373285206Sdes/** from cfg username, after daemonise setup performed */
374285206Sdesextern uid_t cfg_uid;
375285206Sdes/** from cfg username, after daemonise setup performed */
376285206Sdesextern gid_t cfg_gid;
377291767Sdes/** debug and enable small timeouts */
378291767Sdesextern int autr_permit_small_holddown;
379285206Sdes
380238106Sdes/**
381238106Sdes * Stub config options
382238106Sdes */
383238106Sdesstruct config_stub {
384238106Sdes	/** next in list */
385238106Sdes	struct config_stub* next;
386238106Sdes	/** domain name (in text) of the stub apex domain */
387238106Sdes	char* name;
388238106Sdes	/** list of stub nameserver hosts (domain name) */
389238106Sdes	struct config_strlist* hosts;
390238106Sdes	/** list of stub nameserver addresses (IP address) */
391238106Sdes	struct config_strlist* addrs;
392238106Sdes	/** if stub-prime is set */
393238106Sdes	int isprime;
394238106Sdes	/** if forward-first is set (failover to without if fails) */
395238106Sdes	int isfirst;
396238106Sdes};
397238106Sdes
398238106Sdes/**
399238106Sdes * List of strings for config options
400238106Sdes */
401238106Sdesstruct config_strlist {
402238106Sdes	/** next item in list */
403238106Sdes	struct config_strlist* next;
404238106Sdes	/** config option string */
405238106Sdes	char* str;
406238106Sdes};
407238106Sdes
408238106Sdes/**
409238106Sdes * List of two strings for config options
410238106Sdes */
411238106Sdesstruct config_str2list {
412238106Sdes	/** next item in list */
413238106Sdes	struct config_str2list* next;
414238106Sdes	/** first string */
415238106Sdes	char* str;
416238106Sdes	/** second string */
417238106Sdes	char* str2;
418238106Sdes};
419238106Sdes
420238106Sdes/** List head for strlist processing, used for append operation. */
421238106Sdesstruct config_strlist_head {
422238106Sdes	/** first in list of text items */
423238106Sdes	struct config_strlist* first;
424238106Sdes	/** last in list of text items */
425238106Sdes	struct config_strlist* last;
426238106Sdes};
427238106Sdes
428238106Sdes/**
429238106Sdes * Create config file structure. Filled with default values.
430238106Sdes * @return: the new structure or NULL on memory error.
431238106Sdes */
432238106Sdesstruct config_file* config_create(void);
433238106Sdes
434238106Sdes/**
435238106Sdes * Create config file structure for library use. Filled with default values.
436238106Sdes * @return: the new structure or NULL on memory error.
437238106Sdes */
438238106Sdesstruct config_file* config_create_forlib(void);
439238106Sdes
440238106Sdes/**
441238106Sdes * Read the config file from the specified filename.
442238106Sdes * @param config: where options are stored into, must be freshly created.
443238106Sdes * @param filename: name of configfile. If NULL nothing is done.
444238106Sdes * @param chroot: if not NULL, the chroot dir currently in use (for include).
445238106Sdes * @return: false on error. In that case errno is set, ENOENT means
446238106Sdes * 	file not found.
447238106Sdes */
448238106Sdesint config_read(struct config_file* config, const char* filename,
449238106Sdes	const char* chroot);
450238106Sdes
451238106Sdes/**
452238106Sdes * Destroy the config file structure.
453238106Sdes * @param config: to delete.
454238106Sdes */
455238106Sdesvoid config_delete(struct config_file* config);
456238106Sdes
457238106Sdes/**
458238106Sdes * Apply config to global constants; this routine is called in single thread.
459238106Sdes * @param config: to apply. Side effect: global constants change.
460238106Sdes */
461238106Sdesvoid config_apply(struct config_file* config);
462238106Sdes
463238106Sdes/**
464285206Sdes * Find username, sets cfg_uid and cfg_gid.
465285206Sdes * @param config: the config structure.
466285206Sdes */
467285206Sdesvoid config_lookup_uid(struct config_file* config);
468285206Sdes
469285206Sdes/**
470238106Sdes * Set the given keyword to the given value.
471238106Sdes * @param config: where to store config
472238106Sdes * @param option: option name, including the ':' character.
473238106Sdes * @param value: value, this string is copied if needed, or parsed.
474238106Sdes * 	The caller owns the value string.
475238106Sdes * @return 0 on error (malloc or syntax error).
476238106Sdes */
477238106Sdesint config_set_option(struct config_file* config, const char* option,
478238106Sdes	const char* value);
479238106Sdes
480238106Sdes/**
481238106Sdes * Call print routine for the given option.
482238106Sdes * @param cfg: config.
483238106Sdes * @param opt: option name without trailing :.
484238106Sdes *	This is different from config_set_option.
485238106Sdes * @param func: print func, called as (str, arg) for every data element.
486238106Sdes * @param arg: user argument for print func.
487238106Sdes * @return false if the option name is not supported (syntax error).
488238106Sdes */
489238106Sdesint config_get_option(struct config_file* cfg, const char* opt,
490238106Sdes	void (*func)(char*,void*), void* arg);
491238106Sdes
492238106Sdes/**
493238106Sdes * Get an option and return strlist
494238106Sdes * @param cfg: config file
495238106Sdes * @param opt: option name.
496238106Sdes * @param list: list is returned here. malloced, caller must free it.
497238106Sdes * @return 0=OK, 1=syntax error, 2=malloc failed.
498238106Sdes */
499238106Sdesint config_get_option_list(struct config_file* cfg, const char* opt,
500238106Sdes	struct config_strlist** list);
501238106Sdes
502238106Sdes/**
503238106Sdes * Get an option and collate results into string
504238106Sdes * @param cfg: config file
505238106Sdes * @param opt: option name.
506238106Sdes * @param str: string. malloced, caller must free it.
507238106Sdes * @return 0=OK, 1=syntax error, 2=malloc failed.
508238106Sdes */
509238106Sdesint config_get_option_collate(struct config_file* cfg, const char* opt,
510238106Sdes	char** str);
511238106Sdes
512238106Sdes/**
513238106Sdes * function to print to a file, use as func with config_get_option.
514238106Sdes * @param line: text to print. \n appended.
515238106Sdes * @param arg: pass a FILE*, like stdout.
516238106Sdes */
517238106Sdesvoid config_print_func(char* line, void* arg);
518238106Sdes
519238106Sdes/**
520238106Sdes * function to collate the text strings into a strlist_head.
521238106Sdes * @param line: text to append.
522238106Sdes * @param arg: pass a strlist_head structure. zeroed on start.
523238106Sdes */
524238106Sdesvoid config_collate_func(char* line, void* arg);
525238106Sdes
526238106Sdes/**
527238106Sdes * take a strlist_head list and return a malloc string. separated with newline.
528238106Sdes * @param list: strlist first to collate. zeroes return "".
529238106Sdes * @return NULL on malloc failure. Or if malloc failure happened in strlist.
530238106Sdes */
531238106Sdeschar* config_collate_cat(struct config_strlist* list);
532238106Sdes
533238106Sdes/**
534238106Sdes * Append text at end of list.
535238106Sdes * @param list: list head. zeroed at start.
536238106Sdes * @param item: new item. malloced by caller. if NULL the insertion fails.
537238106Sdes * @return true on success.
538238106Sdes */
539238106Sdesint cfg_strlist_append(struct config_strlist_head* list, char* item);
540238106Sdes
541238106Sdes/**
542238106Sdes * Insert string into strlist.
543238106Sdes * @param head: pointer to strlist head variable.
544238106Sdes * @param item: new item. malloced by caller. If NULL the insertion fails.
545238106Sdes * @return: true on success.
546238106Sdes */
547238106Sdesint cfg_strlist_insert(struct config_strlist** head, char* item);
548238106Sdes
549238106Sdes/**
550238106Sdes * Insert string into str2list.
551238106Sdes * @param head: pointer to str2list head variable.
552238106Sdes * @param item: new item. malloced by caller. If NULL the insertion fails.
553238106Sdes * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
554238106Sdes * @return: true on success.
555238106Sdes */
556238106Sdesint cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
557238106Sdes
558238106Sdes/**
559238106Sdes * Delete items in config string list.
560238106Sdes * @param list: list.
561238106Sdes */
562238106Sdesvoid config_delstrlist(struct config_strlist* list);
563238106Sdes
564238106Sdes/**
565238106Sdes * Delete items in config double string list.
566238106Sdes * @param list: list.
567238106Sdes */
568238106Sdesvoid config_deldblstrlist(struct config_str2list* list);
569238106Sdes
570238106Sdes/**
571238106Sdes * Delete items in config stub list.
572238106Sdes * @param list: list.
573238106Sdes */
574238106Sdesvoid config_delstubs(struct config_stub* list);
575238106Sdes
576238106Sdes/**
577238106Sdes * Convert 14digit to time value
578238106Sdes * @param str: string of 14 digits
579238106Sdes * @return time value or 0 for error.
580238106Sdes */
581269257Sdestime_t cfg_convert_timeval(const char* str);
582238106Sdes
583238106Sdes/**
584238106Sdes * Count number of values in the string.
585238106Sdes * format ::= (sp num)+ sp
586238106Sdes * num ::= [-](0-9)+
587238106Sdes * sp ::= (space|tab)*
588238106Sdes *
589238106Sdes * @param str: string
590238106Sdes * @return: 0 on parse error, or empty string, else
591238106Sdes *	number of integer values in the string.
592238106Sdes */
593238106Sdesint cfg_count_numbers(const char* str);
594238106Sdes
595238106Sdes/**
596238106Sdes * Convert a 'nice' memory or file size into a bytecount
597238106Sdes * From '100k' to 102400. and so on. Understands kKmMgG.
598238106Sdes * k=1024, m=1024*1024, g=1024*1024*1024.
599238106Sdes * @param str: string
600238106Sdes * @param res: result is stored here, size in bytes.
601238106Sdes * @return: true if parsed correctly, or 0 on a parse error (and an error
602238106Sdes * is logged).
603238106Sdes */
604238106Sdesint cfg_parse_memsize(const char* str, size_t* res);
605238106Sdes
606238106Sdes/**
607238106Sdes * Parse local-zone directive into two strings and register it in the config.
608238106Sdes * @param cfg: to put it in.
609238106Sdes * @param val: argument strings to local-zone, "example.com nodefault".
610238106Sdes * @return: false on failure
611238106Sdes */
612238106Sdesint cfg_parse_local_zone(struct config_file* cfg, const char* val);
613238106Sdes
614238106Sdes/**
615238106Sdes * Mark "number" or "low-high" as available or not in ports array.
616238106Sdes * @param str: string in input
617238106Sdes * @param allow: give true if this range is permitted.
618238106Sdes * @param avail: the array from cfg.
619238106Sdes * @param num: size of the array (65536).
620238106Sdes * @return: true if parsed correctly, or 0 on a parse error (and an error
621238106Sdes * is logged).
622238106Sdes */
623238106Sdesint cfg_mark_ports(const char* str, int allow, int* avail, int num);
624238106Sdes
625238106Sdes/**
626238106Sdes * Get a condensed list of ports returned. allocated.
627238106Sdes * @param cfg: config file.
628238106Sdes * @param avail: the available ports array is returned here.
629238106Sdes * @return: number of ports in array or 0 on error.
630238106Sdes */
631238106Sdesint cfg_condense_ports(struct config_file* cfg, int** avail);
632238106Sdes
633238106Sdes/**
634238106Sdes * Scan ports available
635238106Sdes * @param avail: the array from cfg.
636238106Sdes * @param num: size of the array (65536).
637238106Sdes * @return the number of ports available for use.
638238106Sdes */
639238106Sdesint cfg_scan_ports(int* avail, int num);
640238106Sdes
641238106Sdes/**
642238106Sdes * Convert a filename to full pathname in original filesys
643238106Sdes * @param fname: the path name to convert.
644238106Sdes *      Must not be null or empty.
645238106Sdes * @param cfg: config struct for chroot and chdir (if set).
646238106Sdes * @param use_chdir: if false, only chroot is applied.
647238106Sdes * @return pointer to malloced buffer which is: [chroot][chdir]fname
648238106Sdes *      or NULL on malloc failure.
649238106Sdes */
650238106Sdeschar* fname_after_chroot(const char* fname, struct config_file* cfg,
651238106Sdes	int use_chdir);
652238106Sdes
653238106Sdes/**
654238106Sdes * Convert a ptr shorthand into a full reverse-notation PTR record.
655238106Sdes * @param str: input string, "IP name"
656238106Sdes * @return: malloced string "reversed-ip-name PTR name"
657238106Sdes */
658238106Sdeschar* cfg_ptr_reverse(char* str);
659238106Sdes
660238106Sdes/**
661238106Sdes * Append text to the error info for validation.
662238106Sdes * @param qstate: query state.
663238106Sdes * @param str: copied into query region and appended.
664238106Sdes * Failures to allocate are logged.
665238106Sdes */
666238106Sdesvoid errinf(struct module_qstate* qstate, const char* str);
667238106Sdes
668238106Sdes/**
669238106Sdes * Append text to error info:  from 1.2.3.4
670238106Sdes * @param qstate: query state.
671238106Sdes * @param origin: sock list with origin of trouble.
672238106Sdes *	Every element added.
673238106Sdes *	If NULL: nothing is added.
674238106Sdes *	if 0len element: 'from cache' is added.
675238106Sdes */
676238106Sdesvoid errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
677238106Sdes
678238106Sdes/**
679238106Sdes * Append text to error info:  for RRset name type class
680238106Sdes * @param qstate: query state.
681238106Sdes * @param rr: rrset_key.
682238106Sdes */
683238106Sdesvoid errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
684238106Sdes
685238106Sdes/**
686238106Sdes * Append text to error info:  str dname
687238106Sdes * @param qstate: query state.
688238106Sdes * @param str: explanation string
689238106Sdes * @param dname: the dname.
690238106Sdes */
691238106Sdesvoid errinf_dname(struct module_qstate* qstate, const char* str,
692238106Sdes	uint8_t* dname);
693238106Sdes
694238106Sdes/**
695238106Sdes * Create error info in string
696238106Sdes * @param qstate: query state.
697238106Sdes * @return string or NULL on malloc failure (already logged).
698238106Sdes *    This string is malloced and has to be freed by caller.
699238106Sdes */
700238106Sdeschar* errinf_to_str(struct module_qstate* qstate);
701238106Sdes
702238106Sdes/**
703238106Sdes * Used during options parsing
704238106Sdes */
705238106Sdesstruct config_parser_state {
706238106Sdes	/** name of file being parser */
707238106Sdes	char* filename;
708238106Sdes	/** line number in the file, starts at 1 */
709238106Sdes	int line;
710238106Sdes	/** number of errors encountered */
711238106Sdes	int errors;
712238106Sdes	/** the result of parsing is stored here. */
713238106Sdes	struct config_file* cfg;
714238106Sdes	/** the current chroot dir (or NULL if none) */
715238106Sdes	const char* chroot;
716238106Sdes};
717238106Sdes
718238106Sdes/** global config parser object used during config parsing */
719238106Sdesextern struct config_parser_state* cfg_parser;
720269257Sdes/** init lex state */
721269257Sdesvoid init_cfg_parse(void);
722255578Sdes/** lex in file */
723255578Sdesextern FILE* ub_c_in;
724255578Sdes/** lex out file */
725255578Sdesextern FILE* ub_c_out;
726255578Sdes/** the yacc lex generated parse function */
727255578Sdesint ub_c_parse(void);
728255578Sdes/** the lexer function */
729255578Sdesint ub_c_lex(void);
730255578Sdes/** wrap function */
731255578Sdesint ub_c_wrap(void);
732238106Sdes/** parsing helpers: print error with file and line numbers */
733238106Sdesvoid ub_c_error(const char* msg);
734238106Sdes/** parsing helpers: print error with file and line numbers */
735238106Sdesvoid ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
736238106Sdes
737238106Sdes#ifdef UB_ON_WINDOWS
738238106Sdes/**
739238106Sdes * Obtain registry string (if it exists).
740238106Sdes * @param key: key string
741238106Sdes * @param name: name of value to fetch.
742238106Sdes * @return malloced string with the result or NULL if it did not
743238106Sdes * 	exist on an error (logged with log_err) was encountered.
744238106Sdes */
745238106Sdeschar* w_lookup_reg_str(const char* key, const char* name);
746294190Sdes
747294190Sdes/** Modify directory in options for module file name */
748294190Sdesvoid w_config_adjust_directory(struct config_file* cfg);
749238106Sdes#endif /* UB_ON_WINDOWS */
750238106Sdes
751238106Sdes#endif /* UTIL_CONFIG_FILE_H */
752