1
/* Copyright (c) 2007-2014 Sam Trenholme
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
15
* This software is provided 'as is' with no guarantees of correctness or
16
* fitness for purpose.
19
#ifndef __MARARC_H_DEFINED__
20
#define __MARARC_H_DEFINED__
25
/* Some constants pointing to mararc parameters */
27
/* mararc string parameters */
28
#define DWM_S_bind_address 0
29
#define DWM_S_ipv4_bind_addresses 1
30
#define DWM_S_chroot_dir 2
31
#define DWM_S_recursive_acl 3
32
#define DWM_S_random_seed_file 4
33
#define DWM_S_cache_file 5
34
#define DWM_S_ip_blacklist 6
36
/* mararc dictionary parameters */
37
#define DWM_D_upstream_servers 0
38
#define DWM_D_root_servers 1
40
/* mararc numeric parameters */
41
#define DWM_N_maxprocs 0
42
#define DWM_N_timeout_seconds 1
43
#define DWM_N_dns_port 2
44
#define DWM_N_upstream_port 3
45
#define DWM_N_handle_overload 4
46
#define DWM_N_handle_noreply 5
47
#define DWM_N_recurse_min_bind_port 6
48
#define DWM_N_recurse_number_ports 7
49
#define DWM_N_hash_magic_number 8
50
#define DWM_N_maximum_cache_elements 9
51
#define DWM_N_maradns_uid 10
52
#define DWM_N_maradns_gid 11
53
#define DWM_N_resurrections 12
54
#define DWM_N_num_retries 13
55
#define DWM_N_verbose_level 14
56
#define DWM_N_max_tcp_procs 15
57
#define DWM_N_timeout_seconds_tcp 16
58
#define DWM_N_tcp_listen 17
59
#define DWM_N_max_ar_chain 18
60
#define DWM_N_ttl_age 19
61
#define DWM_N_max_inflights 20
62
#define DWM_N_deliver_all 21
63
#define DWM_N_filter_rfc1918 22
64
#define DWM_N_ns_glueless_type 23
65
#define DWM_N_reject_aaaa 24
66
#define DWM_N_reject_mx 25
67
#define DWM_N_truncation_hack 26
68
#define DWM_N_reject_ptr 27
69
#define DWM_N_min_ttl_incomplete_cname 28
70
#define DWM_N_max_ttl 29
72
/* Number of string parameters in the mararc file */
74
/* Number of dictionary parameters in the mararc file */
76
/* Number of numeric parameters in the mararc file */
77
#define KEY_N_COUNT 30
80
/* Location of files we read when we run execfile("foo") */
81
#define EXECFILE_DIR "/etc/deadwood/execfile/"
85
dw_str *key_s[KEY_S_COUNT + 1]; /* All of the string dwood2rc parameters */
86
dwd_dict *key_d[KEY_D_COUNT + 1]; /* The dictionary dwood2rc parameters */
87
int32_t key_n[KEY_N_COUNT + 1]; /* The numeric dwood2rc parameters */
89
char *key_s_names[KEY_S_COUNT + 1] = {
90
"bind_address", /* IP addresses to bind to */
91
"ipv4_bind_addresses", /* IP Addresses to bind to (newer name) */
92
"chroot_dir", /* Where we run Deadwood from */
93
"recursive_acl", /* IPs that we allow recursive queries from */
94
"random_seed_file", /* File with seed/key for random number
96
"cache_file", /* File with a copy of Deadwood's cache */
97
"ip_blacklist", /* If an answer has any of these IPs, make it a
98
* "not there" answer */
101
char *key_d_names[KEY_D_COUNT + 1] = {
102
"upstream_servers", /* Recursive upstream DNS servers */
103
"root_servers", /* Non-recursive root DNS servers */
106
char *key_n_names[KEY_N_COUNT + 1] = {
107
"maxprocs", /* The maximum number of outstanding queries we can
109
"timeout_seconds", /* How long we wait for a reply from the remote
111
"dns_port", /* The port we bind to */
112
"upstream_port", /* The port we connect to when connecting upstream */
113
"handle_overload", /* Reply when overloaded */
114
"handle_noreply", /* Reply when no reply from upstream */
115
"recurse_min_bind_port", /* The lowest numbered port deadwood will
117
"recurse_number_ports", /* The number of ports deadwood is allowed
119
"hash_magic_number", /* A large 31-bit prime number that the hash
120
* compression function uses */
121
"maximum_cache_elements", /* Maximum number of elements in cache */
122
"maradns_uid", /* Numeric User ID Deadwood runs as */
123
"maradns_gid", /* Numeric Group ID Deadwood runs as */
124
"resurrections", /* Whether to look up expired records if we can't
125
* connect to upstream */
126
"num_retries", /* Number of times we try to connect to an upstream
127
* server before giving up */
128
"verbose_level", /* How verbose our logging should be */
129
"max_tcp_procs", /* The maximum number of pending TCP queries we
131
"timeout_seconds_tcp", /* Timeout in seconds for active TCP
133
"tcp_listen", /* Whether to enable DNS-over-TCP */
134
"max_ar_chain", /* Is RR rotation enabled (1) or disabled (2) */
135
"ttl_age", /* Whether to enable (1) or disable (0) TTL aging */
136
"max_inflights", /* Maximum number of in-flight requests we allow
137
* a single upstream query to have. */
138
"deliver_all", /* Deliver non-cachable replies */
139
"filter_rfc1918", /* Don't allow RFC1918 IPs in DNS replies */
140
"ns_glueless_type", /* Query type to find NS glue */
141
"reject_aaaa", /* Whether to reply to AAAA queries with a
142
* synthetic "not there" reply */
143
"reject_mx", /* Whether to reject MX queries with no reply and
144
* logging the query (to help find spam zombies) */
145
"truncation_hack", /* Whether to use 1st answer in truncated UDP
146
* replies (if present) */
147
"reject_ptr", /* Whether to reject PTR queries and send out a
148
synthetic "not there" reply */
149
"min_ttl_incomplete_cname", /* How long to store incomplete CNAME
150
* records in the cache, in seconds */
151
"max_ttl", /* Maximum allowed TTL */
154
#endif /* MARARC_C */
156
/* Various character classes used by the Mararc parser's finite state
159
#define dwm_is_alpha(c) (c >= 'a' && c <= 'z') || \
160
(c >= 'A' && c <= 'Z') || \
163
#define dwm_is_alphanum(c) (c >= 'a' && c <= 'z') || \
164
(c >= 'A' && c <= 'Z') || \
165
(c >= '0' && c <= '9') || \
168
#define dwm_is_alphastart(c) (c >= 'a' && c <= 'z') || \
169
(c >= 'A' && c <= 'Z')
171
#define dwm_is_dname(c) (c >= 'a' && c <= 'z') || \
172
(c >= 'A' && c <= 'Z') || \
173
(c >= '0' && c <= '9') || \
174
c == '-' || c == '.' || c == '_'
176
#define dwm_is_instring(c) (c >= ' ' && c <= '~' && c != '#' && c != '"')
178
#define dwm_is_whitespace(c) (c == ' ' || c == '\t')
180
#define dwm_is_any(c) (c >= ' ' && c <= '~') || c == '\t' || c > 127
182
#define dwm_is_dnamestart(c) (c >= 'a' && c <= 'z') || \
183
(c >= 'A' && c <= 'Z') || \
184
(c >= '0' && c <= '9')
186
#define dwm_is_number(c) (c >= '0' && c <= '9')
188
/* Limits to the number of states, actions, and pattern per state we can
191
/* Maximum number of states */
192
#define DWM_MAX_STATES 52
193
/* Maximum number of patterns per state */
194
#define DWM_MAX_PATTERNS 8
196
/* The actual state machine that we use to parse a MaraRC file; this is
197
* described in the file doc/internals/MARARC.parser */
199
#define dwm_machine "a Hb Y1c Wxb Rxp T;\n" \
201
"c B1c Wd =e [f +g (y\n" \
203
"e We N4h Qi {6w\n" \
206
"h N4h Wk Hb Rxp T;\n" \
224
"xb Hb Wxb Y8xb Rxp T;\n"
226
/* A tokenized single state in the finite state machine */
228
char pattern[DWM_MAX_PATTERNS + 1];
229
char action[DWM_MAX_PATTERNS + 1];
230
char newstate[DWM_MAX_PATTERNS + 1];
233
/* Parse a mararc file; this should only be called once when executing
234
* deadwood. Note that this is the *only* public method in this entire
235
* file; all other functions in this file should only be called from
236
* other functions in this file.
237
* Input: c string that points to mararc file
238
* Output: 1 on success; program exits on mararc parse error */
239
int dwm_parse_mararc(char *name);
241
/* Fetch a value from a given dictionary variable (num is the number for
242
* the dictionary variable we are seeking a given value for, given the
243
* dictionary variable and the key inside that variable) */
244
dw_str *dwm_dict_fetch(int num, dw_str *key);
246
/* For a given dictionary variable, and a key, return (as a *copied* dw_str
247
* object) the next key or 0 if we're at the last key. If the key given to
248
* this function is 0, return the first key. */
249
dw_str *dwm_dict_nextkey(int num, dw_str *key);
251
/* Private function: Parse a single file */
252
int dwm_parse_file(char *name);
254
#endif /* __MARARC_H_DEFINED__ */