1 /* 2 * options.h -- nsd.conf options definitions and prototypes 3 * 4 * Copyright (c) 2001-2006, 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 struct buffer; 21 struct nsd; 22 23 typedef struct nsd_options nsd_options_type; 24 typedef struct pattern_options pattern_options_type; 25 typedef struct zone_options zone_options_type; 26 typedef struct ip_address_option ip_address_option_type; 27 typedef struct acl_options acl_options_type; 28 typedef struct key_options key_options_type; 29 typedef struct config_parser_state config_parser_state_type; 30 /* 31 * Options global for nsd. 32 */ 33 struct nsd_options { 34 /* config file name */ 35 char* configfile; 36 /* options for zones, by apex, contains zone_options */ 37 rbtree_type* zone_options; 38 /* patterns, by name, contains pattern_options */ 39 rbtree_type* patterns; 40 41 /* free space in zonelist file, contains zonelist_bucket */ 42 rbtree_type* zonefree; 43 /* number of free space lines in zonelist file */ 44 size_t zonefree_number; 45 /* zonelist file if open */ 46 FILE* zonelist; 47 /* last offset in file (or 0 if none) */ 48 off_t zonelist_off; 49 50 /* tree of zonestat names and their id values, entries are struct 51 * zonestatname with malloced key=stringname. The number of items 52 * is the max statnameid, no items are freed from this. 53 * kept correct in the xfrd process, and on startup. */ 54 rbtree_type* zonestatnames; 55 56 /* rbtree of keys defined, by name */ 57 rbtree_type* keys; 58 59 /* list of ip addresses to bind to (or NULL for all) */ 60 struct ip_address_option* ip_addresses; 61 62 int ip_transparent; 63 int ip_freebind; 64 int debug_mode; 65 int verbosity; 66 int hide_version; 67 int do_ip4; 68 int do_ip6; 69 const char* database; 70 const char* identity; 71 const char* version; 72 const char* logfile; 73 int server_count; 74 int tcp_count; 75 int tcp_query_count; 76 int tcp_timeout; 77 int tcp_mss; 78 int outgoing_tcp_mss; 79 size_t ipv4_edns_size; 80 size_t ipv6_edns_size; 81 const char* pidfile; 82 const char* port; 83 int statistics; 84 const char* chroot; 85 const char* username; 86 const char* zonesdir; 87 const char* xfrdfile; 88 const char* xfrdir; 89 const char* zonelistfile; 90 const char* nsid; 91 int xfrd_reload_timeout; 92 int zonefiles_check; 93 int zonefiles_write; 94 int log_time_ascii; 95 int round_robin; 96 int minimal_responses; 97 int reuseport; 98 99 /** remote control section. enable toggle. */ 100 int control_enable; 101 /** the interfaces the remote control should listen on */ 102 struct ip_address_option* control_interface; 103 /** port number for the control port */ 104 int control_port; 105 /** private key file for server */ 106 char* server_key_file; 107 /** certificate file for server */ 108 char* server_cert_file; 109 /** private key file for nsd-control */ 110 char* control_key_file; 111 /** certificate file for nsd-control */ 112 char* control_cert_file; 113 114 #ifdef RATELIMIT 115 /** number of buckets in rrl hashtable */ 116 size_t rrl_size; 117 /** max qps for queries, 0 is nolimit */ 118 size_t rrl_ratelimit; 119 /** ratio of slipped responses, 0 is noslip */ 120 size_t rrl_slip; 121 /** ip prefix length */ 122 size_t rrl_ipv4_prefix_length; 123 size_t rrl_ipv6_prefix_length; 124 /** max qps for whitelisted queries, 0 is nolimit */ 125 size_t rrl_whitelist_ratelimit; 126 #endif 127 128 region_type* region; 129 }; 130 131 struct ip_address_option { 132 struct ip_address_option* next; 133 char* address; 134 }; 135 136 /* 137 * Pattern of zone options, used to contain options for zone(s). 138 */ 139 struct pattern_options { 140 rbnode_type node; 141 const char* pname; /* name of the pattern, key of rbtree */ 142 const char* zonefile; 143 struct acl_options* allow_notify; 144 struct acl_options* request_xfr; 145 struct acl_options* notify; 146 struct acl_options* provide_xfr; 147 struct acl_options* outgoing_interface; 148 const char* zonestats; 149 #ifdef RATELIMIT 150 uint16_t rrl_whitelist; /* bitmap with rrl types */ 151 #endif 152 uint8_t allow_axfr_fallback; 153 uint8_t allow_axfr_fallback_is_default; 154 uint8_t notify_retry; 155 uint8_t notify_retry_is_default; 156 uint8_t implicit; /* pattern is implicit, part_of_config zone used */ 157 uint8_t xfrd_flags; 158 uint32_t max_refresh_time; 159 uint8_t max_refresh_time_is_default; 160 uint32_t min_refresh_time; 161 uint8_t min_refresh_time_is_default; 162 uint32_t max_retry_time; 163 uint8_t max_retry_time_is_default; 164 uint32_t min_retry_time; 165 uint8_t min_retry_time_is_default; 166 uint64_t size_limit_xfr; 167 uint8_t multi_master_check; 168 } ATTR_PACKED; 169 170 #define PATTERN_IMPLICIT_MARKER "_implicit_" 171 172 /* 173 * Options for a zone 174 */ 175 struct zone_options { 176 /* key is dname of apex */ 177 rbnode_type node; 178 179 /* is apex of the zone */ 180 const char* name; 181 /* if not part of config, the offset and linesize of zonelist entry */ 182 off_t off; 183 int linesize; 184 /* pattern for the zone options, if zone is part_of_config, this is 185 * a anonymous pattern created in-place */ 186 struct pattern_options* pattern; 187 /* zone is fixed into the main config, not in zonelist, cannot delete */ 188 uint8_t part_of_config; 189 } ATTR_PACKED; 190 191 union acl_addr_storage { 192 #ifdef INET6 193 struct in_addr addr; 194 struct in6_addr addr6; 195 #else 196 struct in_addr addr; 197 #endif 198 }; 199 200 /* 201 * Access control list element 202 */ 203 struct acl_options { 204 struct acl_options* next; 205 206 /* options */ 207 time_t ixfr_disabled; 208 int bad_xfr_count; 209 uint8_t use_axfr_only; 210 uint8_t allow_udp; 211 212 /* ip address range */ 213 const char* ip_address_spec; 214 uint8_t is_ipv6; 215 unsigned int port; /* is 0(no port) or suffix @port value */ 216 union acl_addr_storage addr; 217 union acl_addr_storage range_mask; 218 enum { 219 acl_range_single = 0, /* single address */ 220 acl_range_mask = 1, /* 10.20.30.40&255.255.255.0 */ 221 acl_range_subnet = 2, /* 10.20.30.40/28 */ 222 acl_range_minmax = 3 /* 10.20.30.40-10.20.30.60 (mask=max) */ 223 } rangetype; 224 225 /* key */ 226 uint8_t nokey; 227 uint8_t blocked; 228 const char* key_name; 229 struct key_options* key_options; 230 } ATTR_PACKED; 231 232 /* 233 * Key definition 234 */ 235 struct key_options { 236 rbnode_type node; /* key of tree is name */ 237 char* name; 238 char* algorithm; 239 char* secret; 240 struct tsig_key* tsig_key; 241 } ATTR_PACKED; 242 243 /** zone list free space */ 244 struct zonelist_free { 245 struct zonelist_free* next; 246 off_t off; 247 }; 248 /** zonelist free bucket for a particular line length */ 249 struct zonelist_bucket { 250 rbnode_type node; /* key is ptr to linesize */ 251 int linesize; 252 struct zonelist_free* list; 253 }; 254 255 /* default zonefile write interval if database is "", in seconds */ 256 #define ZONEFILES_WRITE_INTERVAL 3600 257 258 struct zonestatname { 259 rbnode_type node; /* key is malloced string with cooked zonestat name */ 260 unsigned id; /* index in nsd.zonestat array */ 261 }; 262 263 /* 264 * Used during options parsing 265 */ 266 struct config_parser_state { 267 char* filename; 268 const char* chroot; 269 int line; 270 int errors; 271 int server_settings_seen; 272 struct nsd_options* opt; 273 struct pattern_options* current_pattern; 274 struct zone_options* current_zone; 275 struct key_options* current_key; 276 struct ip_address_option* current_ip_address_option; 277 struct acl_options* current_allow_notify; 278 struct acl_options* current_request_xfr; 279 struct acl_options* current_notify; 280 struct acl_options* current_provide_xfr; 281 struct acl_options* current_outgoing_interface; 282 void (*err)(void*,const char*); 283 void* err_arg; 284 }; 285 286 extern config_parser_state_type* cfg_parser; 287 288 /* region will be put in nsd_options struct. Returns empty options struct. */ 289 struct nsd_options* nsd_options_create(region_type* region); 290 /* the number of zones that are configured */ 291 static inline size_t nsd_options_num_zones(struct nsd_options* opt) 292 { return opt->zone_options->count; } 293 /* insert a zone into the main options tree, returns 0 on error */ 294 int nsd_options_insert_zone(struct nsd_options* opt, struct zone_options* zone); 295 /* insert a pattern into the main options tree, returns 0 on error */ 296 int nsd_options_insert_pattern(struct nsd_options* opt, 297 struct pattern_options* pat); 298 299 /* parses options file. Returns false on failure. callback, if nonNULL, 300 * gets called with error strings, default prints. */ 301 int parse_options_file(struct nsd_options* opt, const char* file, 302 void (*err)(void*,const char*), void* err_arg); 303 struct zone_options* zone_options_create(region_type* region); 304 void zone_options_delete(struct nsd_options* opt, struct zone_options* zone); 305 /* find a zone by apex domain name, or NULL if not found. */ 306 struct zone_options* zone_options_find(struct nsd_options* opt, 307 const struct dname* apex); 308 struct pattern_options* pattern_options_create(region_type* region); 309 struct pattern_options* pattern_options_find(struct nsd_options* opt, const char* name); 310 int pattern_options_equal(struct pattern_options* p, struct pattern_options* q); 311 void pattern_options_remove(struct nsd_options* opt, const char* name); 312 void pattern_options_add_modify(struct nsd_options* opt, 313 struct pattern_options* p); 314 void pattern_options_marshal(struct buffer* buffer, struct pattern_options* p); 315 struct pattern_options* pattern_options_unmarshal(region_type* r, 316 struct buffer* b); 317 struct key_options* key_options_create(region_type* region); 318 void key_options_insert(struct nsd_options* opt, struct key_options* key); 319 struct key_options* key_options_find(struct nsd_options* opt, const char* name); 320 void key_options_remove(struct nsd_options* opt, const char* name); 321 int key_options_equal(struct key_options* p, struct key_options* q); 322 void key_options_add_modify(struct nsd_options* opt, struct key_options* key); 323 /* read in zone list file. Returns false on failure */ 324 int parse_zone_list_file(struct nsd_options* opt); 325 /* create zone entry and add to the zonelist file */ 326 struct zone_options* zone_list_add(struct nsd_options* opt, const char* zname, 327 const char* pname); 328 /* create zonelist entry, do not insert in file (called by _add) */ 329 struct zone_options* zone_list_zone_insert(struct nsd_options* opt, 330 const char* nm, const char* patnm, int linesize, off_t off); 331 void zone_list_del(struct nsd_options* opt, struct zone_options* zone); 332 void zone_list_compact(struct nsd_options* opt); 333 void zone_list_close(struct nsd_options* opt); 334 335 /* create zonestat name tree , for initially created zones */ 336 void options_zonestatnames_create(struct nsd_options* opt); 337 /* Get zonestat id for zone options, add new entry if necessary. 338 * instantiates the pattern's zonestat string */ 339 unsigned getzonestatid(struct nsd_options* opt, struct zone_options* zopt); 340 /* create string, same options as zonefile but no chroot changes */ 341 const char* config_cook_string(struct zone_options* zone, const char* input); 342 343 #if defined(HAVE_SSL) 344 /* tsig must be inited, adds all keys in options to tsig. */ 345 void key_options_tsig_add(struct nsd_options* opt); 346 #endif 347 348 /* check acl list, acl number that matches if passed(0..), 349 * or failure (-1) if dropped */ 350 /* the reason why (the acl) is returned too (or NULL) */ 351 int acl_check_incoming(struct acl_options* acl, struct query* q, 352 struct acl_options** reason); 353 int acl_addr_matches_host(struct acl_options* acl, struct acl_options* host); 354 int acl_addr_matches(struct acl_options* acl, struct query* q); 355 int acl_key_matches(struct acl_options* acl, struct query* q); 356 int acl_addr_match_mask(uint32_t* a, uint32_t* b, uint32_t* mask, size_t sz); 357 int acl_addr_match_range(uint32_t* minval, uint32_t* x, uint32_t* maxval, size_t sz); 358 359 /* returns true if acls are both from the same host */ 360 int acl_same_host(struct acl_options* a, struct acl_options* b); 361 /* find acl by number in the list */ 362 struct acl_options* acl_find_num(struct acl_options* acl, int num); 363 364 /* see if two acl lists are the same (same elements in same order, or empty) */ 365 int acl_list_equal(struct acl_options* p, struct acl_options* q); 366 /* see if two acl are the same */ 367 int acl_equal(struct acl_options* p, struct acl_options* q); 368 369 /* see if a zone is a slave or a master zone */ 370 int zone_is_slave(struct zone_options* opt); 371 /* create zonefile name, returns static pointer (perhaps to options data) */ 372 const char* config_make_zonefile(struct zone_options* zone, struct nsd* nsd); 373 374 #define ZONEC_PCT_TIME 5 /* seconds, then it starts to print pcts */ 375 #define ZONEC_PCT_COUNT 100000 /* elements before pct check is done */ 376 377 /* parsing helpers */ 378 void c_error(const char* msg); 379 void c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2); 380 struct acl_options* parse_acl_info(region_type* region, char* ip, 381 const char* key); 382 /* true if ipv6 address, false if ipv4 */ 383 int parse_acl_is_ipv6(const char* p); 384 /* returns range type. mask is the 2nd part of the range */ 385 int parse_acl_range_type(char* ip, char** mask); 386 /* parses subnet mask, fills 0 mask as well */ 387 void parse_acl_range_subnet(char* p, void* addr, int maxbits); 388 /* clean up options */ 389 void nsd_options_destroy(struct nsd_options* opt); 390 /* replace occurrences of one with two in buf, pass length of buffer */ 391 void replace_str(char* buf, size_t len, const char* one, const char* two); 392 /* apply pattern to the existing pattern in the parser */ 393 void config_apply_pattern(const char* name); 394 395 #endif /* OPTIONS_H */ 396