xref: /openbsd-src/usr.sbin/nsd/options.h (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
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* chroot;
59 	const char* username;
60 	const char* zonesdir;
61 	const char* difffile;
62 	const char* xfrdfile;
63 	const char* nsid;
64 	int xfrd_reload_timeout;
65 
66 	region_type* region;
67 };
68 
69 struct ipaddress_option {
70 	ip_address_option_t* next;
71 	char* address;
72 };
73 
74 /*
75  * Options for a zone
76  */
77 struct zone_options {
78 	/* key is dname of apex */
79 	rbnode_t node;
80 
81 	/* is apex of the zone */
82 	const char* name;
83 	const char* zonefile;
84 	acl_options_t* allow_notify;
85 	acl_options_t* request_xfr;
86 	acl_options_t* notify;
87 	acl_options_t* provide_xfr;
88 	acl_options_t* outgoing_interface;
89 	uint8_t allow_axfr_fallback;
90 	uint8_t notify_retry;
91 };
92 
93 union acl_addr_storage {
94 #ifdef INET6
95 	struct in_addr addr;
96 	struct in6_addr addr6;
97 #else
98 	struct in_addr addr;
99 #endif
100 };
101 
102 /*
103  * Access control list element
104  */
105 struct acl_options {
106 	acl_options_t* next;
107 
108 	/* options */
109 	uint8_t use_axfr_only;
110 	uint8_t allow_udp;
111 	time_t ixfr_disabled;
112 
113 	/* ip address range */
114 	const char* ip_address_spec;
115 	uint8_t is_ipv6;
116 	unsigned int port;	/* is 0(no port) or suffix @port value */
117 	union acl_addr_storage addr;
118 	union acl_addr_storage range_mask;
119 	enum {
120 		acl_range_single = 0,	/* single adress */
121 		acl_range_mask = 1,	/* 10.20.30.40&255.255.255.0 */
122 		acl_range_subnet = 2,	/* 10.20.30.40/28 */
123 		acl_range_minmax = 3	/* 10.20.30.40-10.20.30.60 (mask=max) */
124 	} rangetype;
125 
126 	/* key */
127 	uint8_t nokey;
128 	uint8_t blocked;
129 	const char* key_name;
130 	key_options_t* key_options;
131 };
132 
133 /*
134  * Key definition
135  */
136 struct key_options {
137 	key_options_t* next;
138 	const char* name;
139 	const char* algorithm;
140 	const char* secret;
141 	struct tsig_key* tsig_key;
142 };
143 
144 /*
145  * Used during options parsing
146  */
147 struct config_parser_state {
148 	const char* filename;
149 	int line;
150 	int errors;
151 	nsd_options_t* opt;
152 	zone_options_t* current_zone;
153 	key_options_t* current_key;
154 	ip_address_option_t* current_ip_address_option;
155 	acl_options_t* current_allow_notify;
156 	acl_options_t* current_request_xfr;
157 	acl_options_t* current_notify;
158 	acl_options_t* current_provide_xfr;
159 	acl_options_t* current_outgoing_interface;
160 };
161 
162 extern config_parser_state_t* cfg_parser;
163 
164 /* region will be put in nsd_options struct. Returns empty options struct. */
165 nsd_options_t* nsd_options_create(region_type* region);
166 /* the number of zones that are configured */
167 static inline size_t nsd_options_num_zones(nsd_options_t* opt)
168 { return opt->zone_options->count; }
169 /* insert a zone into the main options tree, returns 0 on error */
170 int nsd_options_insert_zone(nsd_options_t* opt, zone_options_t* zone);
171 
172 /* parses options file. Returns false on failure */
173 int parse_options_file(nsd_options_t* opt, const char* file);
174 zone_options_t* zone_options_create(region_type* region);
175 /* find a zone by apex domain name, or NULL if not found. */
176 zone_options_t* zone_options_find(nsd_options_t* opt, const struct dname* apex);
177 key_options_t* key_options_create(region_type* region);
178 key_options_t* key_options_find(nsd_options_t* opt, const char* name);
179 
180 #if defined(HAVE_SSL)
181 /* tsig must be inited, adds all keys in options to tsig. */
182 void key_options_tsig_add(nsd_options_t* opt);
183 #endif
184 
185 /* check acl list, acl number that matches if passed(0..),
186  * or failure (-1) if dropped */
187 /* the reason why (the acl) is returned too (or NULL) */
188 int acl_check_incoming(acl_options_t* acl, struct query* q,
189 	acl_options_t** reason);
190 int acl_addr_matches(acl_options_t* acl, struct query* q);
191 int acl_key_matches(acl_options_t* acl, struct query* q);
192 int acl_addr_match_mask(uint32_t* a, uint32_t* b, uint32_t* mask, size_t sz);
193 int acl_addr_match_range(uint32_t* minval, uint32_t* x, uint32_t* maxval, size_t sz);
194 
195 /* returns true if acls are both from the same host */
196 int acl_same_host(acl_options_t* a, acl_options_t* b);
197 /* find acl by number in the list */
198 acl_options_t* acl_find_num(acl_options_t* acl, int num);
199 
200 /* see if a zone is a slave or a master zone */
201 int zone_is_slave(zone_options_t* opt);
202 
203 /* parsing helpers */
204 void c_error(const char* msg);
205 void c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2);
206 acl_options_t* parse_acl_info(region_type* region, char* ip, const char* key);
207 /* true if ipv6 address, false if ipv4 */
208 int parse_acl_is_ipv6(const char* p);
209 /* returns range type. mask is the 2nd part of the range */
210 int parse_acl_range_type(char* ip, char** mask);
211 /* parses subnet mask, fills 0 mask as well */
212 void parse_acl_range_subnet(char* p, void* addr, int maxbits);
213 /* clean up options */
214 void nsd_options_destroy(nsd_options_t* opt);
215 
216 #endif /* OPTIONS_H */
217