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
24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33238106Sdes * 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;
122238106Sdes
123238106Sdes	/** the target fetch policy for the iterator */
124238106Sdes	char* target_fetch_policy;
125238106Sdes
126238106Sdes	/** automatic interface for incoming messages. Uses ipv6 remapping,
127238106Sdes	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
128238106Sdes	int if_automatic;
129238106Sdes	/** SO_RCVBUF size to set on port 53 UDP socket */
130238106Sdes	size_t so_rcvbuf;
131238106Sdes	/** SO_SNDBUF size to set on port 53 UDP socket */
132238106Sdes	size_t so_sndbuf;
133238106Sdes
134238106Sdes	/** number of interfaces to open. If 0 default all interfaces. */
135238106Sdes	int num_ifs;
136238106Sdes	/** interface description strings (IP addresses) */
137238106Sdes	char **ifs;
138238106Sdes
139238106Sdes	/** number of outgoing interfaces to open.
140238106Sdes	 * If 0 default all interfaces. */
141238106Sdes	int num_out_ifs;
142238106Sdes	/** outgoing interface description strings (IP addresses) */
143238106Sdes	char **out_ifs;
144238106Sdes
145238106Sdes	/** the root hints */
146238106Sdes	struct config_strlist* root_hints;
147238106Sdes	/** the stub definitions, linked list */
148238106Sdes	struct config_stub* stubs;
149238106Sdes	/** the forward zone definitions, linked list */
150238106Sdes	struct config_stub* forwards;
151238106Sdes	/** list of donotquery addresses, linked list */
152238106Sdes	struct config_strlist* donotqueryaddrs;
153238106Sdes	/** list of access control entries, linked list */
154238106Sdes	struct config_str2list* acls;
155238106Sdes	/** use default localhost donotqueryaddr entries */
156238106Sdes	int donotquery_localhost;
157238106Sdes
158238106Sdes	/** harden against very small edns buffer sizes */
159238106Sdes	int harden_short_bufsize;
160238106Sdes	/** harden against very large query sizes */
161238106Sdes	int harden_large_queries;
162238106Sdes	/** harden against spoofed glue (out of zone data) */
163238106Sdes	int harden_glue;
164238106Sdes	/** harden against receiving no DNSSEC data for trust anchor */
165238106Sdes	int harden_dnssec_stripped;
166238106Sdes	/** harden against queries that fall under known nxdomain names */
167238106Sdes	int harden_below_nxdomain;
168238106Sdes	/** harden the referral path, query for NS,A,AAAA and validate */
169238106Sdes	int harden_referral_path;
170238106Sdes	/** use 0x20 bits in query as random ID bits */
171238106Sdes	int use_caps_bits_for_id;
172238106Sdes	/** strip away these private addrs from answers, no DNS Rebinding */
173238106Sdes	struct config_strlist* private_address;
174238106Sdes	/** allow domain (and subdomains) to use private address space */
175238106Sdes	struct config_strlist* private_domain;
176238106Sdes	/** what threshold for unwanted action. */
177238106Sdes	size_t unwanted_threshold;
178238106Sdes	/** the number of seconds maximal TTL used for RRsets and messages */
179238106Sdes	int max_ttl;
180238106Sdes	/** the number of seconds minimum TTL used for RRsets and messages */
181238106Sdes	int min_ttl;
182238106Sdes	/** if prefetching of messages should be performed. */
183238106Sdes	int prefetch;
184238106Sdes	/** if prefetching of DNSKEYs should be performed. */
185238106Sdes	int prefetch_key;
186238106Sdes
187238106Sdes	/** chrootdir, if not "" or chroot will be done */
188238106Sdes	char* chrootdir;
189238106Sdes	/** username to change to, if not "". */
190238106Sdes	char* username;
191238106Sdes	/** working directory */
192238106Sdes	char* directory;
193238106Sdes	/** filename to log to. */
194238106Sdes	char* logfile;
195238106Sdes	/** pidfile to write pid to. */
196238106Sdes	char* pidfile;
197238106Sdes
198238106Sdes	/** should log messages be sent to syslogd */
199238106Sdes	int use_syslog;
200238106Sdes	/** log timestamp in ascii UTC */
201238106Sdes	int log_time_ascii;
202238106Sdes	/** log queries with one line per query */
203238106Sdes	int log_queries;
204238106Sdes
205238106Sdes	/** do not report identity (id.server, hostname.bind) */
206238106Sdes	int hide_identity;
207238106Sdes	/** do not report version (version.server, version.bind) */
208238106Sdes	int hide_version;
209238106Sdes	/** identity, hostname is returned if "". */
210238106Sdes	char* identity;
211238106Sdes	/** version, package version returned if "". */
212238106Sdes	char* version;
213238106Sdes
214238106Sdes	/** the module configuration string */
215238106Sdes	char* module_conf;
216238106Sdes
217238106Sdes	/** files with trusted DS and DNSKEYs in zonefile format, list */
218238106Sdes	struct config_strlist* trust_anchor_file_list;
219238106Sdes	/** list of trustanchor keys, linked list */
220238106Sdes	struct config_strlist* trust_anchor_list;
221238106Sdes	/** files with 5011 autotrust tracked keys */
222238106Sdes	struct config_strlist* auto_trust_anchor_file_list;
223238106Sdes	/** files with trusted DNSKEYs in named.conf format, list */
224238106Sdes	struct config_strlist* trusted_keys_file_list;
225238106Sdes	/** DLV anchor file */
226238106Sdes	char* dlv_anchor_file;
227238106Sdes	/** DLV anchor inline */
228238106Sdes	struct config_strlist* dlv_anchor_list;
229238106Sdes	/** insecure domain list */
230238106Sdes	struct config_strlist* domain_insecure;
231238106Sdes
232238106Sdes	/** if not 0, this value is the validation date for RRSIGs */
233238106Sdes	int32_t val_date_override;
234238106Sdes	/** the minimum for signature clock skew */
235238106Sdes	int32_t val_sig_skew_min;
236238106Sdes	/** the maximum for signature clock skew */
237238106Sdes	int32_t val_sig_skew_max;
238238106Sdes	/** this value sets the number of seconds before revalidating bogus */
239238106Sdes	int bogus_ttl;
240238106Sdes	/** should validator clean additional section for secure msgs */
241238106Sdes	int val_clean_additional;
242238106Sdes	/** log bogus messages by the validator */
243238106Sdes	int val_log_level;
244238106Sdes	/** squelch val_log_level to log - this is library goes to callback */
245238106Sdes	int val_log_squelch;
246238106Sdes	/** should validator allow bogus messages to go through */
247238106Sdes	int val_permissive_mode;
248238106Sdes	/** ignore the CD flag in incoming queries and refuse them bogus data */
249238106Sdes	int ignore_cd;
250238106Sdes	/** nsec3 maximum iterations per key size, string */
251238106Sdes	char* val_nsec3_key_iterations;
252238106Sdes	/** autotrust add holddown time, in seconds */
253238106Sdes	unsigned int add_holddown;
254238106Sdes	/** autotrust del holddown time, in seconds */
255238106Sdes	unsigned int del_holddown;
256238106Sdes	/** autotrust keep_missing time, in seconds. 0 is forever. */
257238106Sdes	unsigned int keep_missing;
258238106Sdes
259238106Sdes	/** size of the key cache */
260238106Sdes	size_t key_cache_size;
261238106Sdes	/** slabs in the key cache. */
262238106Sdes	size_t key_cache_slabs;
263238106Sdes	/** size of the neg cache */
264238106Sdes	size_t neg_cache_size;
265238106Sdes
266238106Sdes	/** local zones config */
267238106Sdes	struct config_str2list* local_zones;
268238106Sdes	/** local zones nodefault list */
269238106Sdes	struct config_strlist* local_zones_nodefault;
270238106Sdes	/** local data RRs configged */
271238106Sdes	struct config_strlist* local_data;
272238106Sdes
273238106Sdes	/** remote control section. enable toggle. */
274238106Sdes	int remote_control_enable;
275238106Sdes	/** the interfaces the remote control should listen on */
276238106Sdes	struct config_strlist* control_ifs;
277238106Sdes	/** port number for the control port */
278238106Sdes	int control_port;
279238106Sdes	/** private key file for server */
280238106Sdes	char* server_key_file;
281238106Sdes	/** certificate file for server */
282238106Sdes	char* server_cert_file;
283238106Sdes	/** private key file for unbound-control */
284238106Sdes	char* control_key_file;
285238106Sdes	/** certificate file for unbound-control */
286238106Sdes	char* control_cert_file;
287238106Sdes
288238106Sdes	/** Python script file */
289238106Sdes	char* python_script;
290238106Sdes
291238106Sdes	/** daemonize, i.e. fork into the background. */
292238106Sdes	int do_daemonize;
293238106Sdes
294238106Sdes	/* minimal response when positive answer */
295238106Sdes	int minimal_responses;
296238106Sdes
297238106Sdes	/* RRSet roundrobin */
298238106Sdes	int rrset_roundrobin;
299238106Sdes};
300238106Sdes
301238106Sdes/**
302238106Sdes * Stub config options
303238106Sdes */
304238106Sdesstruct config_stub {
305238106Sdes	/** next in list */
306238106Sdes	struct config_stub* next;
307238106Sdes	/** domain name (in text) of the stub apex domain */
308238106Sdes	char* name;
309238106Sdes	/** list of stub nameserver hosts (domain name) */
310238106Sdes	struct config_strlist* hosts;
311238106Sdes	/** list of stub nameserver addresses (IP address) */
312238106Sdes	struct config_strlist* addrs;
313238106Sdes	/** if stub-prime is set */
314238106Sdes	int isprime;
315238106Sdes	/** if forward-first is set (failover to without if fails) */
316238106Sdes	int isfirst;
317238106Sdes};
318238106Sdes
319238106Sdes/**
320238106Sdes * List of strings for config options
321238106Sdes */
322238106Sdesstruct config_strlist {
323238106Sdes	/** next item in list */
324238106Sdes	struct config_strlist* next;
325238106Sdes	/** config option string */
326238106Sdes	char* str;
327238106Sdes};
328238106Sdes
329238106Sdes/**
330238106Sdes * List of two strings for config options
331238106Sdes */
332238106Sdesstruct config_str2list {
333238106Sdes	/** next item in list */
334238106Sdes	struct config_str2list* next;
335238106Sdes	/** first string */
336238106Sdes	char* str;
337238106Sdes	/** second string */
338238106Sdes	char* str2;
339238106Sdes};
340238106Sdes
341238106Sdes/** List head for strlist processing, used for append operation. */
342238106Sdesstruct config_strlist_head {
343238106Sdes	/** first in list of text items */
344238106Sdes	struct config_strlist* first;
345238106Sdes	/** last in list of text items */
346238106Sdes	struct config_strlist* last;
347238106Sdes};
348238106Sdes
349238106Sdes/**
350238106Sdes * Create config file structure. Filled with default values.
351238106Sdes * @return: the new structure or NULL on memory error.
352238106Sdes */
353238106Sdesstruct config_file* config_create(void);
354238106Sdes
355238106Sdes/**
356238106Sdes * Create config file structure for library use. Filled with default values.
357238106Sdes * @return: the new structure or NULL on memory error.
358238106Sdes */
359238106Sdesstruct config_file* config_create_forlib(void);
360238106Sdes
361238106Sdes/**
362238106Sdes * Read the config file from the specified filename.
363238106Sdes * @param config: where options are stored into, must be freshly created.
364238106Sdes * @param filename: name of configfile. If NULL nothing is done.
365238106Sdes * @param chroot: if not NULL, the chroot dir currently in use (for include).
366238106Sdes * @return: false on error. In that case errno is set, ENOENT means
367238106Sdes * 	file not found.
368238106Sdes */
369238106Sdesint config_read(struct config_file* config, const char* filename,
370238106Sdes	const char* chroot);
371238106Sdes
372238106Sdes/**
373238106Sdes * Destroy the config file structure.
374238106Sdes * @param config: to delete.
375238106Sdes */
376238106Sdesvoid config_delete(struct config_file* config);
377238106Sdes
378238106Sdes/**
379238106Sdes * Apply config to global constants; this routine is called in single thread.
380238106Sdes * @param config: to apply. Side effect: global constants change.
381238106Sdes */
382238106Sdesvoid config_apply(struct config_file* config);
383238106Sdes
384238106Sdes/**
385238106Sdes * Set the given keyword to the given value.
386238106Sdes * @param config: where to store config
387238106Sdes * @param option: option name, including the ':' character.
388238106Sdes * @param value: value, this string is copied if needed, or parsed.
389238106Sdes * 	The caller owns the value string.
390238106Sdes * @return 0 on error (malloc or syntax error).
391238106Sdes */
392238106Sdesint config_set_option(struct config_file* config, const char* option,
393238106Sdes	const char* value);
394238106Sdes
395238106Sdes/**
396238106Sdes * Call print routine for the given option.
397238106Sdes * @param cfg: config.
398238106Sdes * @param opt: option name without trailing :.
399238106Sdes *	This is different from config_set_option.
400238106Sdes * @param func: print func, called as (str, arg) for every data element.
401238106Sdes * @param arg: user argument for print func.
402238106Sdes * @return false if the option name is not supported (syntax error).
403238106Sdes */
404238106Sdesint config_get_option(struct config_file* cfg, const char* opt,
405238106Sdes	void (*func)(char*,void*), void* arg);
406238106Sdes
407238106Sdes/**
408238106Sdes * Get an option and return strlist
409238106Sdes * @param cfg: config file
410238106Sdes * @param opt: option name.
411238106Sdes * @param list: list is returned here. malloced, caller must free it.
412238106Sdes * @return 0=OK, 1=syntax error, 2=malloc failed.
413238106Sdes */
414238106Sdesint config_get_option_list(struct config_file* cfg, const char* opt,
415238106Sdes	struct config_strlist** list);
416238106Sdes
417238106Sdes/**
418238106Sdes * Get an option and collate results into string
419238106Sdes * @param cfg: config file
420238106Sdes * @param opt: option name.
421238106Sdes * @param str: string. malloced, caller must free it.
422238106Sdes * @return 0=OK, 1=syntax error, 2=malloc failed.
423238106Sdes */
424238106Sdesint config_get_option_collate(struct config_file* cfg, const char* opt,
425238106Sdes	char** str);
426238106Sdes
427238106Sdes/**
428238106Sdes * function to print to a file, use as func with config_get_option.
429238106Sdes * @param line: text to print. \n appended.
430238106Sdes * @param arg: pass a FILE*, like stdout.
431238106Sdes */
432238106Sdesvoid config_print_func(char* line, void* arg);
433238106Sdes
434238106Sdes/**
435238106Sdes * function to collate the text strings into a strlist_head.
436238106Sdes * @param line: text to append.
437238106Sdes * @param arg: pass a strlist_head structure. zeroed on start.
438238106Sdes */
439238106Sdesvoid config_collate_func(char* line, void* arg);
440238106Sdes
441238106Sdes/**
442238106Sdes * take a strlist_head list and return a malloc string. separated with newline.
443238106Sdes * @param list: strlist first to collate. zeroes return "".
444238106Sdes * @return NULL on malloc failure. Or if malloc failure happened in strlist.
445238106Sdes */
446238106Sdeschar* config_collate_cat(struct config_strlist* list);
447238106Sdes
448238106Sdes/**
449238106Sdes * Append text at end of list.
450238106Sdes * @param list: list head. zeroed at start.
451238106Sdes * @param item: new item. malloced by caller. if NULL the insertion fails.
452238106Sdes * @return true on success.
453238106Sdes */
454238106Sdesint cfg_strlist_append(struct config_strlist_head* list, char* item);
455238106Sdes
456238106Sdes/**
457238106Sdes * Insert string into strlist.
458238106Sdes * @param head: pointer to strlist head variable.
459238106Sdes * @param item: new item. malloced by caller. If NULL the insertion fails.
460238106Sdes * @return: true on success.
461238106Sdes */
462238106Sdesint cfg_strlist_insert(struct config_strlist** head, char* item);
463238106Sdes
464238106Sdes/**
465238106Sdes * Insert string into str2list.
466238106Sdes * @param head: pointer to str2list head variable.
467238106Sdes * @param item: new item. malloced by caller. If NULL the insertion fails.
468238106Sdes * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
469238106Sdes * @return: true on success.
470238106Sdes */
471238106Sdesint cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
472238106Sdes
473238106Sdes/**
474238106Sdes * Delete items in config string list.
475238106Sdes * @param list: list.
476238106Sdes */
477238106Sdesvoid config_delstrlist(struct config_strlist* list);
478238106Sdes
479238106Sdes/**
480238106Sdes * Delete items in config double string list.
481238106Sdes * @param list: list.
482238106Sdes */
483238106Sdesvoid config_deldblstrlist(struct config_str2list* list);
484238106Sdes
485238106Sdes/**
486238106Sdes * Delete items in config stub list.
487238106Sdes * @param list: list.
488238106Sdes */
489238106Sdesvoid config_delstubs(struct config_stub* list);
490238106Sdes
491238106Sdes/**
492238106Sdes * Convert 14digit to time value
493238106Sdes * @param str: string of 14 digits
494238106Sdes * @return time value or 0 for error.
495238106Sdes */
496238106Sdesuint32_t cfg_convert_timeval(const char* str);
497238106Sdes
498238106Sdes/**
499238106Sdes * Count number of values in the string.
500238106Sdes * format ::= (sp num)+ sp
501238106Sdes * num ::= [-](0-9)+
502238106Sdes * sp ::= (space|tab)*
503238106Sdes *
504238106Sdes * @param str: string
505238106Sdes * @return: 0 on parse error, or empty string, else
506238106Sdes *	number of integer values in the string.
507238106Sdes */
508238106Sdesint cfg_count_numbers(const char* str);
509238106Sdes
510238106Sdes/**
511238106Sdes * Convert a 'nice' memory or file size into a bytecount
512238106Sdes * From '100k' to 102400. and so on. Understands kKmMgG.
513238106Sdes * k=1024, m=1024*1024, g=1024*1024*1024.
514238106Sdes * @param str: string
515238106Sdes * @param res: result is stored here, size in bytes.
516238106Sdes * @return: true if parsed correctly, or 0 on a parse error (and an error
517238106Sdes * is logged).
518238106Sdes */
519238106Sdesint cfg_parse_memsize(const char* str, size_t* res);
520238106Sdes
521238106Sdes/**
522238106Sdes * Parse local-zone directive into two strings and register it in the config.
523238106Sdes * @param cfg: to put it in.
524238106Sdes * @param val: argument strings to local-zone, "example.com nodefault".
525238106Sdes * @return: false on failure
526238106Sdes */
527238106Sdesint cfg_parse_local_zone(struct config_file* cfg, const char* val);
528238106Sdes
529238106Sdes/**
530238106Sdes * Mark "number" or "low-high" as available or not in ports array.
531238106Sdes * @param str: string in input
532238106Sdes * @param allow: give true if this range is permitted.
533238106Sdes * @param avail: the array from cfg.
534238106Sdes * @param num: size of the array (65536).
535238106Sdes * @return: true if parsed correctly, or 0 on a parse error (and an error
536238106Sdes * is logged).
537238106Sdes */
538238106Sdesint cfg_mark_ports(const char* str, int allow, int* avail, int num);
539238106Sdes
540238106Sdes/**
541238106Sdes * Get a condensed list of ports returned. allocated.
542238106Sdes * @param cfg: config file.
543238106Sdes * @param avail: the available ports array is returned here.
544238106Sdes * @return: number of ports in array or 0 on error.
545238106Sdes */
546238106Sdesint cfg_condense_ports(struct config_file* cfg, int** avail);
547238106Sdes
548238106Sdes/**
549238106Sdes * Scan ports available
550238106Sdes * @param avail: the array from cfg.
551238106Sdes * @param num: size of the array (65536).
552238106Sdes * @return the number of ports available for use.
553238106Sdes */
554238106Sdesint cfg_scan_ports(int* avail, int num);
555238106Sdes
556238106Sdes/**
557238106Sdes * Convert a filename to full pathname in original filesys
558238106Sdes * @param fname: the path name to convert.
559238106Sdes *      Must not be null or empty.
560238106Sdes * @param cfg: config struct for chroot and chdir (if set).
561238106Sdes * @param use_chdir: if false, only chroot is applied.
562238106Sdes * @return pointer to malloced buffer which is: [chroot][chdir]fname
563238106Sdes *      or NULL on malloc failure.
564238106Sdes */
565238106Sdeschar* fname_after_chroot(const char* fname, struct config_file* cfg,
566238106Sdes	int use_chdir);
567238106Sdes
568238106Sdes/**
569238106Sdes * Convert a ptr shorthand into a full reverse-notation PTR record.
570238106Sdes * @param str: input string, "IP name"
571238106Sdes * @return: malloced string "reversed-ip-name PTR name"
572238106Sdes */
573238106Sdeschar* cfg_ptr_reverse(char* str);
574238106Sdes
575238106Sdes/**
576238106Sdes * Append text to the error info for validation.
577238106Sdes * @param qstate: query state.
578238106Sdes * @param str: copied into query region and appended.
579238106Sdes * Failures to allocate are logged.
580238106Sdes */
581238106Sdesvoid errinf(struct module_qstate* qstate, const char* str);
582238106Sdes
583238106Sdes/**
584238106Sdes * Append text to error info:  from 1.2.3.4
585238106Sdes * @param qstate: query state.
586238106Sdes * @param origin: sock list with origin of trouble.
587238106Sdes *	Every element added.
588238106Sdes *	If NULL: nothing is added.
589238106Sdes *	if 0len element: 'from cache' is added.
590238106Sdes */
591238106Sdesvoid errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
592238106Sdes
593238106Sdes/**
594238106Sdes * Append text to error info:  for RRset name type class
595238106Sdes * @param qstate: query state.
596238106Sdes * @param rr: rrset_key.
597238106Sdes */
598238106Sdesvoid errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
599238106Sdes
600238106Sdes/**
601238106Sdes * Append text to error info:  str dname
602238106Sdes * @param qstate: query state.
603238106Sdes * @param str: explanation string
604238106Sdes * @param dname: the dname.
605238106Sdes */
606238106Sdesvoid errinf_dname(struct module_qstate* qstate, const char* str,
607238106Sdes	uint8_t* dname);
608238106Sdes
609238106Sdes/**
610238106Sdes * Create error info in string
611238106Sdes * @param qstate: query state.
612238106Sdes * @return string or NULL on malloc failure (already logged).
613238106Sdes *    This string is malloced and has to be freed by caller.
614238106Sdes */
615238106Sdeschar* errinf_to_str(struct module_qstate* qstate);
616238106Sdes
617238106Sdes/**
618238106Sdes * Used during options parsing
619238106Sdes */
620238106Sdesstruct config_parser_state {
621238106Sdes	/** name of file being parser */
622238106Sdes	char* filename;
623238106Sdes	/** line number in the file, starts at 1 */
624238106Sdes	int line;
625238106Sdes	/** number of errors encountered */
626238106Sdes	int errors;
627238106Sdes	/** the result of parsing is stored here. */
628238106Sdes	struct config_file* cfg;
629238106Sdes	/** the current chroot dir (or NULL if none) */
630238106Sdes	const char* chroot;
631238106Sdes};
632238106Sdes
633238106Sdes/** global config parser object used during config parsing */
634238106Sdesextern struct config_parser_state* cfg_parser;
635255578Sdes/** lex in file */
636255578Sdesextern FILE* ub_c_in;
637255578Sdes/** lex out file */
638255578Sdesextern FILE* ub_c_out;
639255578Sdes/** the yacc lex generated parse function */
640255578Sdesint ub_c_parse(void);
641255578Sdes/** the lexer function */
642255578Sdesint ub_c_lex(void);
643255578Sdes/** wrap function */
644255578Sdesint ub_c_wrap(void);
645238106Sdes/** parsing helpers: print error with file and line numbers */
646238106Sdesvoid ub_c_error(const char* msg);
647238106Sdes/** parsing helpers: print error with file and line numbers */
648238106Sdesvoid ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
649238106Sdes
650238106Sdes#ifdef UB_ON_WINDOWS
651238106Sdes/**
652238106Sdes * Obtain registry string (if it exists).
653238106Sdes * @param key: key string
654238106Sdes * @param name: name of value to fetch.
655238106Sdes * @return malloced string with the result or NULL if it did not
656238106Sdes * 	exist on an error (logged with log_err) was encountered.
657238106Sdes */
658238106Sdeschar* w_lookup_reg_str(const char* key, const char* name);
659238106Sdes#endif /* UB_ON_WINDOWS */
660238106Sdes
661238106Sdes#endif /* UTIL_CONFIG_FILE_H */
662