config_file.h revision 307729
1/*
2 * util/config_file.h - 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#ifndef UTIL_CONFIG_FILE_H
43#define UTIL_CONFIG_FILE_H
44struct config_stub;
45struct config_strlist;
46struct config_str2list;
47struct config_str3list;
48struct config_strbytelist;
49struct module_qstate;
50struct sock_list;
51struct ub_packed_rrset_key;
52struct regional;
53
54/**
55 * The configuration options.
56 * Strings are malloced.
57 */
58struct config_file {
59	/** verbosity level as specified in the config file */
60	int verbosity;
61
62	/** statistics interval (in seconds) */
63	int stat_interval;
64	/** if false, statistics values are reset after printing them */
65	int stat_cumulative;
66	/** if true, the statistics are kept in greater detail */
67	int stat_extended;
68
69	/** number of threads to create */
70	int num_threads;
71
72	/** port on which queries are answered. */
73	int port;
74	/** do ip4 query support. */
75	int do_ip4;
76	/** do ip6 query support. */
77	int do_ip6;
78	/** prefer ip6 upstream queries. */
79	int prefer_ip6;
80	/** do udp query support. */
81	int do_udp;
82	/** do tcp query support. */
83	int do_tcp;
84	/** tcp upstream queries (no UDP upstream queries) */
85	int tcp_upstream;
86	/** maximum segment size of tcp socket which queries are answered */
87	int tcp_mss;
88	/** maximum segment size of tcp socket for outgoing queries */
89	int outgoing_tcp_mss;
90
91	/** private key file for dnstcp-ssl service (enabled if not NULL) */
92	char* ssl_service_key;
93	/** public key file for dnstcp-ssl service */
94	char* ssl_service_pem;
95	/** port on which to provide ssl service */
96	int ssl_port;
97	/** if outgoing tcp connections use SSL */
98	int ssl_upstream;
99
100	/** outgoing port range number of ports (per thread) */
101	int outgoing_num_ports;
102	/** number of outgoing tcp buffers per (per thread) */
103	size_t outgoing_num_tcp;
104	/** number of incoming tcp buffers per (per thread) */
105	size_t incoming_num_tcp;
106	/** allowed udp port numbers, array with 0 if not allowed */
107	int* outgoing_avail_ports;
108
109	/** EDNS buffer size to use */
110	size_t edns_buffer_size;
111	/** number of bytes buffer size for DNS messages */
112	size_t msg_buffer_size;
113	/** size of the message cache */
114	size_t msg_cache_size;
115	/** slabs in the message cache. */
116	size_t msg_cache_slabs;
117	/** number of queries every thread can service */
118	size_t num_queries_per_thread;
119	/** number of msec to wait before items can be jostled out */
120	size_t jostle_time;
121	/** size of the rrset cache */
122	size_t rrset_cache_size;
123	/** slabs in the rrset cache */
124	size_t rrset_cache_slabs;
125	/** host cache ttl in seconds */
126	int host_ttl;
127	/** number of slabs in the infra host cache */
128	size_t infra_cache_slabs;
129	/** max number of hosts in the infra cache */
130	size_t infra_cache_numhosts;
131	/** min value for infra cache rtt */
132	int infra_cache_min_rtt;
133	/** delay close of udp-timeouted ports, if 0 no delayclose. in msec */
134	int delay_close;
135
136	/** the target fetch policy for the iterator */
137	char* target_fetch_policy;
138
139	/** automatic interface for incoming messages. Uses ipv6 remapping,
140	 * and recvmsg/sendmsg ancillary data to detect interfaces, boolean */
141	int if_automatic;
142	/** SO_RCVBUF size to set on port 53 UDP socket */
143	size_t so_rcvbuf;
144	/** SO_SNDBUF size to set on port 53 UDP socket */
145	size_t so_sndbuf;
146	/** SO_REUSEPORT requested on port 53 sockets */
147	int so_reuseport;
148	/** IP_TRANSPARENT socket option requested on port 53 sockets */
149	int ip_transparent;
150	/** IP_FREEBIND socket option request on port 53 sockets */
151	int ip_freebind;
152
153	/** number of interfaces to open. If 0 default all interfaces. */
154	int num_ifs;
155	/** interface description strings (IP addresses) */
156	char **ifs;
157
158	/** number of outgoing interfaces to open.
159	 * If 0 default all interfaces. */
160	int num_out_ifs;
161	/** outgoing interface description strings (IP addresses) */
162	char **out_ifs;
163
164	/** the root hints */
165	struct config_strlist* root_hints;
166	/** the stub definitions, linked list */
167	struct config_stub* stubs;
168	/** the forward zone definitions, linked list */
169	struct config_stub* forwards;
170	/** list of donotquery addresses, linked list */
171	struct config_strlist* donotqueryaddrs;
172	/** list of access control entries, linked list */
173	struct config_str2list* acls;
174	/** use default localhost donotqueryaddr entries */
175	int donotquery_localhost;
176
177	/** harden against very small edns buffer sizes */
178	int harden_short_bufsize;
179	/** harden against very large query sizes */
180	int harden_large_queries;
181	/** harden against spoofed glue (out of zone data) */
182	int harden_glue;
183	/** harden against receiving no DNSSEC data for trust anchor */
184	int harden_dnssec_stripped;
185	/** harden against queries that fall under known nxdomain names */
186	int harden_below_nxdomain;
187	/** harden the referral path, query for NS,A,AAAA and validate */
188	int harden_referral_path;
189	/** harden against algorithm downgrade */
190	int harden_algo_downgrade;
191	/** use 0x20 bits in query as random ID bits */
192	int use_caps_bits_for_id;
193	/** 0x20 whitelist, domains that do not use capsforid */
194	struct config_strlist* caps_whitelist;
195	/** strip away these private addrs from answers, no DNS Rebinding */
196	struct config_strlist* private_address;
197	/** allow domain (and subdomains) to use private address space */
198	struct config_strlist* private_domain;
199	/** what threshold for unwanted action. */
200	size_t unwanted_threshold;
201	/** the number of seconds maximal TTL used for RRsets and messages */
202	int max_ttl;
203	/** the number of seconds minimum TTL used for RRsets and messages */
204	int min_ttl;
205	/** the number of seconds maximal negative TTL for SOA in auth */
206	int max_negative_ttl;
207	/** if prefetching of messages should be performed. */
208	int prefetch;
209	/** if prefetching of DNSKEYs should be performed. */
210	int prefetch_key;
211
212	/** chrootdir, if not "" or chroot will be done */
213	char* chrootdir;
214	/** username to change to, if not "". */
215	char* username;
216	/** working directory */
217	char* directory;
218	/** filename to log to. */
219	char* logfile;
220	/** pidfile to write pid to. */
221	char* pidfile;
222
223	/** should log messages be sent to syslogd */
224	int use_syslog;
225	/** log timestamp in ascii UTC */
226	int log_time_ascii;
227	/** log queries with one line per query */
228	int log_queries;
229
230	/** do not report identity (id.server, hostname.bind) */
231	int hide_identity;
232	/** do not report version (version.server, version.bind) */
233	int hide_version;
234	/** identity, hostname is returned if "". */
235	char* identity;
236	/** version, package version returned if "". */
237	char* version;
238
239	/** the module configuration string */
240	char* module_conf;
241
242	/** files with trusted DS and DNSKEYs in zonefile format, list */
243	struct config_strlist* trust_anchor_file_list;
244	/** list of trustanchor keys, linked list */
245	struct config_strlist* trust_anchor_list;
246	/** files with 5011 autotrust tracked keys */
247	struct config_strlist* auto_trust_anchor_file_list;
248	/** files with trusted DNSKEYs in named.conf format, list */
249	struct config_strlist* trusted_keys_file_list;
250	/** DLV anchor file */
251	char* dlv_anchor_file;
252	/** DLV anchor inline */
253	struct config_strlist* dlv_anchor_list;
254	/** insecure domain list */
255	struct config_strlist* domain_insecure;
256
257	/** if not 0, this value is the validation date for RRSIGs */
258	int32_t val_date_override;
259	/** the minimum for signature clock skew */
260	int32_t val_sig_skew_min;
261	/** the maximum for signature clock skew */
262	int32_t val_sig_skew_max;
263	/** this value sets the number of seconds before revalidating bogus */
264	int bogus_ttl;
265	/** should validator clean additional section for secure msgs */
266	int val_clean_additional;
267	/** log bogus messages by the validator */
268	int val_log_level;
269	/** squelch val_log_level to log - this is library goes to callback */
270	int val_log_squelch;
271	/** should validator allow bogus messages to go through */
272	int val_permissive_mode;
273	/** ignore the CD flag in incoming queries and refuse them bogus data */
274	int ignore_cd;
275	/** nsec3 maximum iterations per key size, string */
276	char* val_nsec3_key_iterations;
277	/** autotrust add holddown time, in seconds */
278	unsigned int add_holddown;
279	/** autotrust del holddown time, in seconds */
280	unsigned int del_holddown;
281	/** autotrust keep_missing time, in seconds. 0 is forever. */
282	unsigned int keep_missing;
283	/** permit small holddown values, allowing 5011 rollover very fast */
284	int permit_small_holddown;
285
286	/** size of the key cache */
287	size_t key_cache_size;
288	/** slabs in the key cache. */
289	size_t key_cache_slabs;
290	/** size of the neg cache */
291	size_t neg_cache_size;
292
293	/** local zones config */
294	struct config_str2list* local_zones;
295	/** local zones nodefault list */
296	struct config_strlist* local_zones_nodefault;
297	/** local data RRs configured */
298	struct config_strlist* local_data;
299	/** local zone override types per netblock */
300	struct config_str3list* local_zone_overrides;
301	/** unblock lan zones (reverse lookups for AS112 zones) */
302	int unblock_lan_zones;
303	/** insecure lan zones (don't validate AS112 zones) */
304	int insecure_lan_zones;
305	/** list of zonename, tagbitlist */
306	struct config_strbytelist* local_zone_tags;
307	/** list of aclname, tagbitlist */
308	struct config_strbytelist* acl_tags;
309	/** list of aclname, tagname, localzonetype */
310	struct config_str3list* acl_tag_actions;
311	/** list of aclname, tagname, redirectdata */
312	struct config_str3list* acl_tag_datas;
313	/** tag list, array with tagname[i] is malloced string */
314	char** tagname;
315	/** number of items in the taglist */
316	int num_tags;
317
318	/** remote control section. enable toggle. */
319	int remote_control_enable;
320	/** the interfaces the remote control should listen on */
321	struct config_strlist* control_ifs;
322	/** port number for the control port */
323	int control_port;
324	/** use certificates for remote control */
325	int remote_control_use_cert;
326	/** private key file for server */
327	char* server_key_file;
328	/** certificate file for server */
329	char* server_cert_file;
330	/** private key file for unbound-control */
331	char* control_key_file;
332	/** certificate file for unbound-control */
333	char* control_cert_file;
334
335	/** Python script file */
336	char* python_script;
337
338	/** daemonize, i.e. fork into the background. */
339	int do_daemonize;
340
341	/* minimal response when positive answer */
342	int minimal_responses;
343
344	/* RRSet roundrobin */
345	int rrset_roundrobin;
346
347	/* maximum UDP response size */
348	size_t max_udp_size;
349
350	/* DNS64 prefix */
351	char* dns64_prefix;
352
353	/* Synthetize all AAAA record despite the presence of an authoritative one */
354	int dns64_synthall;
355
356	/** true to enable dnstap support */
357	int dnstap;
358	/** dnstap socket path */
359	char* dnstap_socket_path;
360	/** true to send "identity" via dnstap */
361	int dnstap_send_identity;
362	/** true to send "version" via dnstap */
363	int dnstap_send_version;
364	/** dnstap "identity", hostname is used if "". */
365	char* dnstap_identity;
366	/** dnstap "version", package version is used if "". */
367	char* dnstap_version;
368
369	/** true to log dnstap RESOLVER_QUERY message events */
370	int dnstap_log_resolver_query_messages;
371	/** true to log dnstap RESOLVER_RESPONSE message events */
372	int dnstap_log_resolver_response_messages;
373	/** true to log dnstap CLIENT_QUERY message events */
374	int dnstap_log_client_query_messages;
375	/** true to log dnstap CLIENT_RESPONSE message events */
376	int dnstap_log_client_response_messages;
377	/** true to log dnstap FORWARDER_QUERY message events */
378	int dnstap_log_forwarder_query_messages;
379	/** true to log dnstap FORWARDER_RESPONSE message events */
380	int dnstap_log_forwarder_response_messages;
381
382	/** true to disable DNSSEC lameness check in iterator */
383	int disable_dnssec_lame_check;
384
385	/** ratelimit 0 is off, otherwise qps (unless overridden) */
386	int ratelimit;
387	/** number of slabs for ratelimit cache */
388	size_t ratelimit_slabs;
389	/** memory size in bytes for ratelimit cache */
390	size_t ratelimit_size;
391	/** ratelimits for domain (exact match) */
392	struct config_str2list* ratelimit_for_domain;
393	/** ratelimits below domain */
394	struct config_str2list* ratelimit_below_domain;
395	/** ratelimit factor, 0 blocks all, 10 allows 1/10 of traffic */
396	int ratelimit_factor;
397	/** minimise outgoing QNAME and hide original QTYPE if possible */
398	int qname_minimisation;
399};
400
401/** from cfg username, after daemonise setup performed */
402extern uid_t cfg_uid;
403/** from cfg username, after daemonise setup performed */
404extern gid_t cfg_gid;
405/** debug and enable small timeouts */
406extern int autr_permit_small_holddown;
407
408/**
409 * Stub config options
410 */
411struct config_stub {
412	/** next in list */
413	struct config_stub* next;
414	/** domain name (in text) of the stub apex domain */
415	char* name;
416	/** list of stub nameserver hosts (domain name) */
417	struct config_strlist* hosts;
418	/** list of stub nameserver addresses (IP address) */
419	struct config_strlist* addrs;
420	/** if stub-prime is set */
421	int isprime;
422	/** if forward-first is set (failover to without if fails) */
423	int isfirst;
424};
425
426/**
427 * List of strings for config options
428 */
429struct config_strlist {
430	/** next item in list */
431	struct config_strlist* next;
432	/** config option string */
433	char* str;
434};
435
436/**
437 * List of two strings for config options
438 */
439struct config_str2list {
440	/** next item in list */
441	struct config_str2list* next;
442	/** first string */
443	char* str;
444	/** second string */
445	char* str2;
446};
447
448/**
449 * List of three strings for config options
450 */
451struct config_str3list {
452	/** next item in list */
453	struct config_str3list* next;
454	/** first string */
455	char* str;
456	/** second string */
457	char* str2;
458	/** third string */
459	char* str3;
460};
461
462
463/**
464 * List of string, bytestring for config options
465 */
466struct config_strbytelist {
467	/** next item in list */
468	struct config_strbytelist* next;
469	/** first string */
470	char* str;
471	/** second bytestring */
472	uint8_t* str2;
473	size_t str2len;
474};
475
476/** List head for strlist processing, used for append operation. */
477struct config_strlist_head {
478	/** first in list of text items */
479	struct config_strlist* first;
480	/** last in list of text items */
481	struct config_strlist* last;
482};
483
484/**
485 * Create config file structure. Filled with default values.
486 * @return: the new structure or NULL on memory error.
487 */
488struct config_file* config_create(void);
489
490/**
491 * Create config file structure for library use. Filled with default values.
492 * @return: the new structure or NULL on memory error.
493 */
494struct config_file* config_create_forlib(void);
495
496/**
497 * Read the config file from the specified filename.
498 * @param config: where options are stored into, must be freshly created.
499 * @param filename: name of configfile. If NULL nothing is done.
500 * @param chroot: if not NULL, the chroot dir currently in use (for include).
501 * @return: false on error. In that case errno is set, ENOENT means
502 * 	file not found.
503 */
504int config_read(struct config_file* config, const char* filename,
505	const char* chroot);
506
507/**
508 * Destroy the config file structure.
509 * @param config: to delete.
510 */
511void config_delete(struct config_file* config);
512
513/**
514 * Apply config to global constants; this routine is called in single thread.
515 * @param config: to apply. Side effect: global constants change.
516 */
517void config_apply(struct config_file* config);
518
519/**
520 * Find username, sets cfg_uid and cfg_gid.
521 * @param config: the config structure.
522 */
523void config_lookup_uid(struct config_file* config);
524
525/**
526 * Set the given keyword to the given value.
527 * @param config: where to store config
528 * @param option: option name, including the ':' character.
529 * @param value: value, this string is copied if needed, or parsed.
530 * 	The caller owns the value string.
531 * @return 0 on error (malloc or syntax error).
532 */
533int config_set_option(struct config_file* config, const char* option,
534	const char* value);
535
536/**
537 * Call print routine for the given option.
538 * @param cfg: config.
539 * @param opt: option name without trailing :.
540 *	This is different from config_set_option.
541 * @param func: print func, called as (str, arg) for every data element.
542 * @param arg: user argument for print func.
543 * @return false if the option name is not supported (syntax error).
544 */
545int config_get_option(struct config_file* cfg, const char* opt,
546	void (*func)(char*,void*), void* arg);
547
548/**
549 * Get an option and return strlist
550 * @param cfg: config file
551 * @param opt: option name.
552 * @param list: list is returned here. malloced, caller must free it.
553 * @return 0=OK, 1=syntax error, 2=malloc failed.
554 */
555int config_get_option_list(struct config_file* cfg, const char* opt,
556	struct config_strlist** list);
557
558/**
559 * Get an option and collate results into string
560 * @param cfg: config file
561 * @param opt: option name.
562 * @param str: string. malloced, caller must free it.
563 * @return 0=OK, 1=syntax error, 2=malloc failed.
564 */
565int config_get_option_collate(struct config_file* cfg, const char* opt,
566	char** str);
567
568/**
569 * function to print to a file, use as func with config_get_option.
570 * @param line: text to print. \n appended.
571 * @param arg: pass a FILE*, like stdout.
572 */
573void config_print_func(char* line, void* arg);
574
575/**
576 * function to collate the text strings into a strlist_head.
577 * @param line: text to append.
578 * @param arg: pass a strlist_head structure. zeroed on start.
579 */
580void config_collate_func(char* line, void* arg);
581
582/**
583 * take a strlist_head list and return a malloc string. separated with newline.
584 * @param list: strlist first to collate. zeroes return "".
585 * @return NULL on malloc failure. Or if malloc failure happened in strlist.
586 */
587char* config_collate_cat(struct config_strlist* list);
588
589/**
590 * Append text at end of list.
591 * @param list: list head. zeroed at start.
592 * @param item: new item. malloced by caller. if NULL the insertion fails.
593 * @return true on success.
594 */
595int cfg_strlist_append(struct config_strlist_head* list, char* item);
596
597/**
598 * Insert string into strlist.
599 * @param head: pointer to strlist head variable.
600 * @param item: new item. malloced by caller. If NULL the insertion fails.
601 * @return: true on success.
602 */
603int cfg_strlist_insert(struct config_strlist** head, char* item);
604
605/** insert with region for allocation. */
606int cfg_region_strlist_insert(struct regional* region,
607	struct config_strlist** head, char* item);
608
609/**
610 * Insert string into str2list.
611 * @param head: pointer to str2list head variable.
612 * @param item: new item. malloced by caller. If NULL the insertion fails.
613 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
614 * @return: true on success.
615 */
616int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);
617
618/**
619 * Insert string into str3list.
620 * @param head: pointer to str3list head variable.
621 * @param item: new item. malloced by caller. If NULL the insertion fails.
622 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
623 * @param i3: 3rd string, malloced by caller. If NULL the insertion fails.
624 * @return: true on success.
625 */
626int cfg_str3list_insert(struct config_str3list** head, char* item, char* i2,
627	char* i3);
628
629/**
630 * Insert string into strbytelist.
631 * @param head: pointer to strbytelist head variable.
632 * @param item: new item. malloced by caller. If NULL the insertion fails.
633 * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
634 * @param i2len: length of the i2 bytestring.
635 * @return: true on success.
636 */
637int cfg_strbytelist_insert(struct config_strbytelist** head, char* item,
638	uint8_t* i2, size_t i2len);
639
640/**
641 * Find stub in config list, also returns prevptr (for deletion).
642 * @param pp: call routine with pointer to a pointer to the start of the list,
643 * 	if the stub is found, on exit, the value contains a pointer to the
644 * 	next pointer that points to the found element (or to the list start
645 * 	pointer if it is the first element).
646 * @param nm: name of stub to find.
647 * @return: pointer to config_stub if found, or NULL if not found.
648 */
649struct config_stub* cfg_stub_find(struct config_stub*** pp, const char* nm);
650
651/**
652 * Delete items in config string list.
653 * @param list: list.
654 */
655void config_delstrlist(struct config_strlist* list);
656
657/**
658 * Delete items in config double string list.
659 * @param list: list.
660 */
661void config_deldblstrlist(struct config_str2list* list);
662
663/**
664 * Delete items in config triple string list.
665 * @param list: list.
666 */
667void config_deltrplstrlist(struct config_str3list* list);
668
669/** delete stringbytelist */
670void config_del_strbytelist(struct config_strbytelist* list);
671
672/**
673 * Delete a stub item
674 * @param p: stub item
675 */
676void config_delstub(struct config_stub* p);
677
678/**
679 * Delete items in config stub list.
680 * @param list: list.
681 */
682void config_delstubs(struct config_stub* list);
683
684/**
685 * Convert 14digit to time value
686 * @param str: string of 14 digits
687 * @return time value or 0 for error.
688 */
689time_t cfg_convert_timeval(const char* str);
690
691/**
692 * Count number of values in the string.
693 * format ::= (sp num)+ sp
694 * num ::= [-](0-9)+
695 * sp ::= (space|tab)*
696 *
697 * @param str: string
698 * @return: 0 on parse error, or empty string, else
699 *	number of integer values in the string.
700 */
701int cfg_count_numbers(const char* str);
702
703/**
704 * Convert a 'nice' memory or file size into a bytecount
705 * From '100k' to 102400. and so on. Understands kKmMgG.
706 * k=1024, m=1024*1024, g=1024*1024*1024.
707 * @param str: string
708 * @param res: result is stored here, size in bytes.
709 * @return: true if parsed correctly, or 0 on a parse error (and an error
710 * is logged).
711 */
712int cfg_parse_memsize(const char* str, size_t* res);
713
714/**
715 * Add a tag name to the config.  It is added at the end with a new ID value.
716 * @param cfg: the config structure.
717 * @param tag: string (which is copied) with the name.
718 * @return: false on alloc failure.
719 */
720int config_add_tag(struct config_file* cfg, const char* tag);
721
722/**
723 * Find tag ID in the tag list.
724 * @param cfg: the config structure.
725 * @param tag: string with tag name to search for.
726 * @return: 0..(num_tags-1) with tag ID, or -1 if tagname is not found.
727 */
728int find_tag_id(struct config_file* cfg, const char* tag);
729
730/**
731 * parse taglist from string into bytestring with bitlist.
732 * @param cfg: the config structure (with tagnames)
733 * @param str: the string to parse.  Parse puts 0 bytes in string.
734 * @param listlen: returns length of in bytes.
735 * @return malloced bytes with a bitlist of the tags.  or NULL on parse error
736 * or malloc failure.
737 */
738uint8_t* config_parse_taglist(struct config_file* cfg, char* str,
739	size_t* listlen);
740
741/**
742 * convert tag bitlist to a malloced string with tag names.  For debug output.
743 * @param cfg: the config structure (with tagnames)
744 * @param taglist: the tag bitlist.
745 * @param len: length of the tag bitlist.
746 * @return malloced string or NULL.
747 */
748char* config_taglist2str(struct config_file* cfg, uint8_t* taglist,
749	size_t len);
750
751/**
752 * see if two taglists intersect (have tags in common).
753 * @param list1: first tag bitlist.
754 * @param list1len: length in bytes of first list.
755 * @param list2: second tag bitlist.
756 * @param list2len: length in bytes of second list.
757 * @return true if there are tags in common, 0 if not.
758 */
759int taglist_intersect(uint8_t* list1, size_t list1len, uint8_t* list2,
760	size_t list2len);
761
762/**
763 * Parse local-zone directive into two strings and register it in the config.
764 * @param cfg: to put it in.
765 * @param val: argument strings to local-zone, "example.com nodefault".
766 * @return: false on failure
767 */
768int cfg_parse_local_zone(struct config_file* cfg, const char* val);
769
770/**
771 * Mark "number" or "low-high" as available or not in ports array.
772 * @param str: string in input
773 * @param allow: give true if this range is permitted.
774 * @param avail: the array from cfg.
775 * @param num: size of the array (65536).
776 * @return: true if parsed correctly, or 0 on a parse error (and an error
777 * is logged).
778 */
779int cfg_mark_ports(const char* str, int allow, int* avail, int num);
780
781/**
782 * Get a condensed list of ports returned. allocated.
783 * @param cfg: config file.
784 * @param avail: the available ports array is returned here.
785 * @return: number of ports in array or 0 on error.
786 */
787int cfg_condense_ports(struct config_file* cfg, int** avail);
788
789/**
790 * Scan ports available
791 * @param avail: the array from cfg.
792 * @param num: size of the array (65536).
793 * @return the number of ports available for use.
794 */
795int cfg_scan_ports(int* avail, int num);
796
797/**
798 * Convert a filename to full pathname in original filesys
799 * @param fname: the path name to convert.
800 *      Must not be null or empty.
801 * @param cfg: config struct for chroot and chdir (if set).
802 * @param use_chdir: if false, only chroot is applied.
803 * @return pointer to malloced buffer which is: [chroot][chdir]fname
804 *      or NULL on malloc failure.
805 */
806char* fname_after_chroot(const char* fname, struct config_file* cfg,
807	int use_chdir);
808
809/**
810 * Convert a ptr shorthand into a full reverse-notation PTR record.
811 * @param str: input string, "IP name"
812 * @return: malloced string "reversed-ip-name PTR name"
813 */
814char* cfg_ptr_reverse(char* str);
815
816/**
817 * Append text to the error info for validation.
818 * @param qstate: query state.
819 * @param str: copied into query region and appended.
820 * Failures to allocate are logged.
821 */
822void errinf(struct module_qstate* qstate, const char* str);
823
824/**
825 * Append text to error info:  from 1.2.3.4
826 * @param qstate: query state.
827 * @param origin: sock list with origin of trouble.
828 *	Every element added.
829 *	If NULL: nothing is added.
830 *	if 0len element: 'from cache' is added.
831 */
832void errinf_origin(struct module_qstate* qstate, struct sock_list *origin);
833
834/**
835 * Append text to error info:  for RRset name type class
836 * @param qstate: query state.
837 * @param rr: rrset_key.
838 */
839void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr);
840
841/**
842 * Append text to error info:  str dname
843 * @param qstate: query state.
844 * @param str: explanation string
845 * @param dname: the dname.
846 */
847void errinf_dname(struct module_qstate* qstate, const char* str,
848	uint8_t* dname);
849
850/**
851 * Create error info in string
852 * @param qstate: query state.
853 * @return string or NULL on malloc failure (already logged).
854 *    This string is malloced and has to be freed by caller.
855 */
856char* errinf_to_str(struct module_qstate* qstate);
857
858/**
859 * Used during options parsing
860 */
861struct config_parser_state {
862	/** name of file being parser */
863	char* filename;
864	/** line number in the file, starts at 1 */
865	int line;
866	/** number of errors encountered */
867	int errors;
868	/** the result of parsing is stored here. */
869	struct config_file* cfg;
870	/** the current chroot dir (or NULL if none) */
871	const char* chroot;
872};
873
874/** global config parser object used during config parsing */
875extern struct config_parser_state* cfg_parser;
876/** init lex state */
877void init_cfg_parse(void);
878/** lex in file */
879extern FILE* ub_c_in;
880/** lex out file */
881extern FILE* ub_c_out;
882/** the yacc lex generated parse function */
883int ub_c_parse(void);
884/** the lexer function */
885int ub_c_lex(void);
886/** wrap function */
887int ub_c_wrap(void);
888/** parsing helpers: print error with file and line numbers */
889void ub_c_error(const char* msg);
890/** parsing helpers: print error with file and line numbers */
891void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
892
893#ifdef UB_ON_WINDOWS
894/**
895 * Obtain registry string (if it exists).
896 * @param key: key string
897 * @param name: name of value to fetch.
898 * @return malloced string with the result or NULL if it did not
899 * 	exist on an error (logged with log_err) was encountered.
900 */
901char* w_lookup_reg_str(const char* key, const char* name);
902
903/** Modify directory in options for module file name */
904void w_config_adjust_directory(struct config_file* cfg);
905#endif /* UB_ON_WINDOWS */
906
907#endif /* UTIL_CONFIG_FILE_H */
908