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