xref: /openbsd-src/usr.sbin/nsd/options.h (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*
2  * options.h -- nsd.conf options definitions and prototypes
3  *
4  * Copyright (c) 2001-2011, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  */
9 
10 #ifndef OPTIONS_H
11 #define OPTIONS_H
12 
13 #include "config.h"
14 #include <stdarg.h>
15 #include "region-allocator.h"
16 #include "rbtree.h"
17 struct query;
18 struct dname;
19 struct tsig_key;
20 
21 typedef struct nsd_options nsd_options_t;
22 typedef struct zone_options zone_options_t;
23 typedef struct ipaddress_option ip_address_option_t;
24 typedef struct acl_options acl_options_t;
25 typedef struct key_options key_options_t;
26 typedef struct config_parser_state config_parser_state_t;
27 /*
28  * Options global for nsd.
29  */
30 struct nsd_options {
31 	/* options for zones, by apex, contains zone_options_t */
32 	rbtree_t* zone_options;
33 
34 	/* list of keys defined */
35 	key_options_t* keys;
36 	size_t numkeys;
37 
38 	/* list of ip adresses to bind to (or NULL for all) */
39 	ip_address_option_t* ip_addresses;
40 
41 	int debug_mode;
42 	int verbosity;
43 	int hide_version;
44 	int ip4_only;
45 	int ip6_only;
46 	const char* database;
47 	const char* identity;
48 	const char* logfile;
49 	int server_count;
50 	int tcp_count;
51 	int tcp_query_count;
52 	int tcp_timeout;
53 	size_t ipv4_edns_size;
54 	size_t ipv6_edns_size;
55 	const char* pidfile;
56 	const char* port;
57 	int statistics;
58 	const char* zonestatsfile;
59 	const char* chroot;
60 	const char* username;
61 	const char* zonesdir;
62 	const char* difffile;
63 	const char* xfrdfile;
64 	const char* nsid;
65 	int xfrd_reload_timeout;
66 
67 	region_type* region;
68 };
69 
70 struct ipaddress_option {
71 	ip_address_option_t* next;
72 	char* address;
73 };
74 
75 /*
76  * Options for a zone
77  */
78 struct zone_options {
79 	/* key is dname of apex */
80 	rbnode_t node;
81 
82 	/* is apex of the zone */
83 	const char* name;
84 	const char* zonefile;
85 	acl_options_t* allow_notify;
86 	acl_options_t* request_xfr;
87 	acl_options_t* notify;
88 	acl_options_t* provide_xfr;
89 	acl_options_t* outgoing_interface;
90 	uint8_t allow_axfr_fallback;
91 	uint8_t notify_retry;
92 };
93 
94 union acl_addr_storage {
95 #ifdef INET6
96 	struct in_addr addr;
97 	struct in6_addr addr6;
98 #else
99 	struct in_addr addr;
100 #endif
101 };
102 
103 /*
104  * Access control list element
105  */
106 struct acl_options {
107 	acl_options_t* next;
108 
109 	/* options */
110 	uint8_t use_axfr_only;
111 	uint8_t allow_udp;
112 	time_t ixfr_disabled;
113 
114 	/* ip address range */
115 	const char* ip_address_spec;
116 	uint8_t is_ipv6;
117 	unsigned int port;	/* is 0(no port) or suffix @port value */
118 	union acl_addr_storage addr;
119 	union acl_addr_storage range_mask;
120 	enum {
121 		acl_range_single = 0,	/* single adress */
122 		acl_range_mask = 1,	/* 10.20.30.40&255.255.255.0 */
123 		acl_range_subnet = 2,	/* 10.20.30.40/28 */
124 		acl_range_minmax = 3	/* 10.20.30.40-10.20.30.60 (mask=max) */
125 	} rangetype;
126 
127 	/* key */
128 	uint8_t nokey;
129 	uint8_t blocked;
130 	const char* key_name;
131 	key_options_t* key_options;
132 };
133 
134 /*
135  * Key definition
136  */
137 struct key_options {
138 	key_options_t* next;
139 	const char* name;
140 	const char* algorithm;
141 	const char* secret;
142 	struct tsig_key* tsig_key;
143 };
144 
145 /*
146  * Used during options parsing
147  */
148 struct config_parser_state {
149 	const char* filename;
150 	int line;
151 	int errors;
152 	nsd_options_t* opt;
153 	zone_options_t* current_zone;
154 	key_options_t* current_key;
155 	ip_address_option_t* current_ip_address_option;
156 	acl_options_t* current_allow_notify;
157 	acl_options_t* current_request_xfr;
158 	acl_options_t* current_notify;
159 	acl_options_t* current_provide_xfr;
160 	acl_options_t* current_outgoing_interface;
161 };
162 
163 extern config_parser_state_t* cfg_parser;
164 
165 /* region will be put in nsd_options struct. Returns empty options struct. */
166 nsd_options_t* nsd_options_create(region_type* region);
167 /* the number of zones that are configured */
168 static inline size_t nsd_options_num_zones(nsd_options_t* opt)
169 { return opt->zone_options->count; }
170 /* insert a zone into the main options tree, returns 0 on error */
171 int nsd_options_insert_zone(nsd_options_t* opt, zone_options_t* zone);
172 
173 /* parses options file. Returns false on failure */
174 int parse_options_file(nsd_options_t* opt, const char* file);
175 zone_options_t* zone_options_create(region_type* region);
176 /* find a zone by apex domain name, or NULL if not found. */
177 zone_options_t* zone_options_find(nsd_options_t* opt, const struct dname* apex);
178 key_options_t* key_options_create(region_type* region);
179 key_options_t* key_options_find(nsd_options_t* opt, const char* name);
180 
181 #if defined(HAVE_SSL)
182 /* tsig must be inited, adds all keys in options to tsig. */
183 void key_options_tsig_add(nsd_options_t* opt);
184 #endif
185 
186 /* check acl list, acl number that matches if passed(0..),
187  * or failure (-1) if dropped */
188 /* the reason why (the acl) is returned too (or NULL) */
189 int acl_check_incoming(acl_options_t* acl, struct query* q,
190 	acl_options_t** reason);
191 int acl_addr_matches(acl_options_t* acl, struct query* q);
192 int acl_key_matches(acl_options_t* acl, struct query* q);
193 int acl_addr_match_mask(uint32_t* a, uint32_t* b, uint32_t* mask, size_t sz);
194 int acl_addr_match_range(uint32_t* minval, uint32_t* x, uint32_t* maxval, size_t sz);
195 
196 /* returns true if acls are both from the same host */
197 int acl_same_host(acl_options_t* a, acl_options_t* b);
198 /* find acl by number in the list */
199 acl_options_t* acl_find_num(acl_options_t* acl, int num);
200 
201 /* see if a zone is a slave or a master zone */
202 int zone_is_slave(zone_options_t* opt);
203 
204 /* parsing helpers */
205 void c_error(const char* msg);
206 void c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
207 acl_options_t* parse_acl_info(region_type* region, char* ip, const char* key);
208 /* true if ipv6 address, false if ipv4 */
209 int parse_acl_is_ipv6(const char* p);
210 /* returns range type. mask is the 2nd part of the range */
211 int parse_acl_range_type(char* ip, char** mask);
212 /* parses subnet mask, fills 0 mask as well */
213 void parse_acl_range_subnet(char* p, void* addr, int maxbits);
214 /* clean up options */
215 void nsd_options_destroy(nsd_options_t* opt);
216 
217 #endif /* OPTIONS_H */
218