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