xref: /freebsd/contrib/unbound/util/configlexer.lex (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 %{
2 /*
3  * configlexer.lex - lexical analyzer for unbound config file
4  *
5  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved
6  *
7  * See LICENSE for the license.
8  *
9  */
10 
11 #include "config.h"
12 
13 #include <ctype.h>
14 #include <string.h>
15 #include <strings.h>
16 #ifdef HAVE_GLOB_H
17 # include <glob.h>
18 #endif
19 
20 #include "util/config_file.h"
21 #include "configparser.h"
22 void ub_c_error(const char *message);
23 
24 #if 0
25 #define LEXOUT(s)  printf s /* used ONLY when debugging */
26 #else
27 #define LEXOUT(s)
28 #endif
29 
30 /** avoid warning in about fwrite return value */
31 #define ECHO ub_c_error_msg("syntax error at text: %s", ub_c_text)
32 
33 /** A parser variable, this is a statement in the config file which is
34  * of the form variable: value1 value2 ...  nargs is the number of values. */
35 #define YDVAR(nargs, var) \
36 	num_args=(nargs); \
37 	LEXOUT(("v(%s%d) ", ub_c_text, num_args)); \
38 	if(num_args > 0) { BEGIN(val); } \
39 	return (var);
40 
41 struct inc_state {
42 	char* filename;
43 	int line;
44 	YY_BUFFER_STATE buffer;
45 	struct inc_state* next;
46 };
47 static struct inc_state* config_include_stack = NULL;
48 static int inc_depth = 0;
49 static int inc_prev = 0;
50 static int num_args = 0;
51 
52 void init_cfg_parse(void)
53 {
54 	config_include_stack = NULL;
55 	inc_depth = 0;
56 	inc_prev = 0;
57 	num_args = 0;
58 }
59 
60 static void config_start_include(const char* filename)
61 {
62 	FILE *input;
63 	struct inc_state* s;
64 	char* nm;
65 	if(inc_depth++ > 100000) {
66 		ub_c_error_msg("too many include files");
67 		return;
68 	}
69 	if(strlen(filename) == 0) {
70 		ub_c_error_msg("empty include file name");
71 		return;
72 	}
73 	s = (struct inc_state*)malloc(sizeof(*s));
74 	if(!s) {
75 		ub_c_error_msg("include %s: malloc failure", filename);
76 		return;
77 	}
78 	if(cfg_parser->chroot && strncmp(filename, cfg_parser->chroot,
79 		strlen(cfg_parser->chroot)) == 0) {
80 		filename += strlen(cfg_parser->chroot);
81 	}
82 	nm = strdup(filename);
83 	if(!nm) {
84 		ub_c_error_msg("include %s: strdup failure", filename);
85 		free(s);
86 		return;
87 	}
88 	input = fopen(filename, "r");
89 	if(!input) {
90 		ub_c_error_msg("cannot open include file '%s': %s",
91 			filename, strerror(errno));
92 		free(s);
93 		free(nm);
94 		return;
95 	}
96 	LEXOUT(("switch_to_include_file(%s)\n", filename));
97 	s->filename = cfg_parser->filename;
98 	s->line = cfg_parser->line;
99 	s->buffer = YY_CURRENT_BUFFER;
100 	s->next = config_include_stack;
101 	config_include_stack = s;
102 	cfg_parser->filename = nm;
103 	cfg_parser->line = 1;
104 	yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));
105 }
106 
107 static void config_start_include_glob(const char* filename)
108 {
109 
110 	/* check for wildcards */
111 #ifdef HAVE_GLOB
112 	glob_t g;
113 	size_t i;
114 	int r, flags;
115 	if(!(!strchr(filename, '*') && !strchr(filename, '?') && !strchr(filename, '[') &&
116 		!strchr(filename, '{') && !strchr(filename, '~'))) {
117 		flags = 0
118 #ifdef GLOB_ERR
119 			| GLOB_ERR
120 #endif
121 #ifdef GLOB_NOSORT
122 			| GLOB_NOSORT
123 #endif
124 #ifdef GLOB_BRACE
125 			| GLOB_BRACE
126 #endif
127 #ifdef GLOB_TILDE
128 			| GLOB_TILDE
129 #endif
130 		;
131 		memset(&g, 0, sizeof(g));
132 		r = glob(filename, flags, NULL, &g);
133 		if(r) {
134 			/* some error */
135 			globfree(&g);
136 			if(r == GLOB_NOMATCH)
137 				return; /* no matches for pattern */
138 			config_start_include(filename); /* let original deal with it */
139 			return;
140 		}
141 		/* process files found, if any */
142 		for(i=0; i<(size_t)g.gl_pathc; i++) {
143 			config_start_include(g.gl_pathv[i]);
144 		}
145 		globfree(&g);
146 		return;
147 	}
148 #endif /* HAVE_GLOB */
149 
150 	config_start_include(filename);
151 }
152 
153 static void config_end_include(void)
154 {
155 	struct inc_state* s = config_include_stack;
156 	--inc_depth;
157 	if(!s) return;
158 	free(cfg_parser->filename);
159 	cfg_parser->filename = s->filename;
160 	cfg_parser->line = s->line;
161 	yy_delete_buffer(YY_CURRENT_BUFFER);
162 	yy_switch_to_buffer(s->buffer);
163 	config_include_stack = s->next;
164 	free(s);
165 }
166 
167 #ifndef yy_set_bol /* compat definition, for flex 2.4.6 */
168 #define yy_set_bol(at_bol) \
169         { \
170 	        if ( ! yy_current_buffer ) \
171 	                yy_current_buffer = yy_create_buffer( ub_c_in, YY_BUF_SIZE ); \
172 	        yy_current_buffer->yy_ch_buf[0] = ((at_bol)?'\n':' '); \
173         }
174 #endif
175 
176 %}
177 %option noinput
178 %option nounput
179 %{
180 #ifndef YY_NO_UNPUT
181 #define YY_NO_UNPUT 1
182 #endif
183 #ifndef YY_NO_INPUT
184 #define YY_NO_INPUT 1
185 #endif
186 %}
187 
188 SPACE   [ \t]
189 LETTER  [a-zA-Z]
190 UNQUOTEDLETTER [^\'\"\n\r \t\\]|\\.
191 UNQUOTEDLETTER_NOCOLON [^\:\'\"\n\r \t\\]|\\.
192 NEWLINE [\r\n]
193 COMMENT \#
194 COLON 	\:
195 DQANY     [^\"\n\r\\]|\\.
196 SQANY     [^\'\n\r\\]|\\.
197 
198 %x	quotedstring singlequotedstr include include_quoted val
199 
200 %%
201 <INITIAL,val>{SPACE}*	{
202 	LEXOUT(("SP ")); /* ignore */ }
203 <INITIAL,val>{SPACE}*{COMMENT}.*	{
204 	/* note that flex makes the longest match and '.' is any but not nl */
205 	LEXOUT(("comment(%s) ", ub_c_text)); /* ignore */ }
206 server{COLON}			{ YDVAR(0, VAR_SERVER) }
207 num-threads{COLON}		{ YDVAR(1, VAR_NUM_THREADS) }
208 verbosity{COLON}		{ YDVAR(1, VAR_VERBOSITY) }
209 port{COLON}			{ YDVAR(1, VAR_PORT) }
210 outgoing-range{COLON}		{ YDVAR(1, VAR_OUTGOING_RANGE) }
211 outgoing-port-permit{COLON}	{ YDVAR(1, VAR_OUTGOING_PORT_PERMIT) }
212 outgoing-port-avoid{COLON}	{ YDVAR(1, VAR_OUTGOING_PORT_AVOID) }
213 outgoing-num-tcp{COLON}		{ YDVAR(1, VAR_OUTGOING_NUM_TCP) }
214 incoming-num-tcp{COLON}		{ YDVAR(1, VAR_INCOMING_NUM_TCP) }
215 do-ip4{COLON}			{ YDVAR(1, VAR_DO_IP4) }
216 do-ip6{COLON}			{ YDVAR(1, VAR_DO_IP6) }
217 do-udp{COLON}			{ YDVAR(1, VAR_DO_UDP) }
218 do-tcp{COLON}			{ YDVAR(1, VAR_DO_TCP) }
219 tcp-upstream{COLON}		{ YDVAR(1, VAR_TCP_UPSTREAM) }
220 ssl-upstream{COLON}		{ YDVAR(1, VAR_SSL_UPSTREAM) }
221 ssl-service-key{COLON}		{ YDVAR(1, VAR_SSL_SERVICE_KEY) }
222 ssl-service-pem{COLON}		{ YDVAR(1, VAR_SSL_SERVICE_PEM) }
223 ssl-port{COLON}			{ YDVAR(1, VAR_SSL_PORT) }
224 do-daemonize{COLON}		{ YDVAR(1, VAR_DO_DAEMONIZE) }
225 interface{COLON}		{ YDVAR(1, VAR_INTERFACE) }
226 ip-address{COLON}		{ YDVAR(1, VAR_INTERFACE) }
227 outgoing-interface{COLON}	{ YDVAR(1, VAR_OUTGOING_INTERFACE) }
228 interface-automatic{COLON}	{ YDVAR(1, VAR_INTERFACE_AUTOMATIC) }
229 so-rcvbuf{COLON}		{ YDVAR(1, VAR_SO_RCVBUF) }
230 so-sndbuf{COLON}		{ YDVAR(1, VAR_SO_SNDBUF) }
231 so-reuseport{COLON}		{ YDVAR(1, VAR_SO_REUSEPORT) }
232 chroot{COLON}			{ YDVAR(1, VAR_CHROOT) }
233 username{COLON}			{ YDVAR(1, VAR_USERNAME) }
234 directory{COLON}		{ YDVAR(1, VAR_DIRECTORY) }
235 logfile{COLON}			{ YDVAR(1, VAR_LOGFILE) }
236 pidfile{COLON}			{ YDVAR(1, VAR_PIDFILE) }
237 root-hints{COLON}		{ YDVAR(1, VAR_ROOT_HINTS) }
238 edns-buffer-size{COLON}		{ YDVAR(1, VAR_EDNS_BUFFER_SIZE) }
239 msg-buffer-size{COLON}		{ YDVAR(1, VAR_MSG_BUFFER_SIZE) }
240 msg-cache-size{COLON}		{ YDVAR(1, VAR_MSG_CACHE_SIZE) }
241 msg-cache-slabs{COLON}		{ YDVAR(1, VAR_MSG_CACHE_SLABS) }
242 rrset-cache-size{COLON}		{ YDVAR(1, VAR_RRSET_CACHE_SIZE) }
243 rrset-cache-slabs{COLON}	{ YDVAR(1, VAR_RRSET_CACHE_SLABS) }
244 cache-max-ttl{COLON}     	{ YDVAR(1, VAR_CACHE_MAX_TTL) }
245 cache-min-ttl{COLON}     	{ YDVAR(1, VAR_CACHE_MIN_TTL) }
246 infra-host-ttl{COLON}		{ YDVAR(1, VAR_INFRA_HOST_TTL) }
247 infra-lame-ttl{COLON}		{ YDVAR(1, VAR_INFRA_LAME_TTL) }
248 infra-cache-slabs{COLON}	{ YDVAR(1, VAR_INFRA_CACHE_SLABS) }
249 infra-cache-numhosts{COLON}	{ YDVAR(1, VAR_INFRA_CACHE_NUMHOSTS) }
250 infra-cache-lame-size{COLON}	{ YDVAR(1, VAR_INFRA_CACHE_LAME_SIZE) }
251 num-queries-per-thread{COLON}	{ YDVAR(1, VAR_NUM_QUERIES_PER_THREAD) }
252 jostle-timeout{COLON}		{ YDVAR(1, VAR_JOSTLE_TIMEOUT) }
253 delay-close{COLON}		{ YDVAR(1, VAR_DELAY_CLOSE) }
254 target-fetch-policy{COLON}	{ YDVAR(1, VAR_TARGET_FETCH_POLICY) }
255 harden-short-bufsize{COLON}	{ YDVAR(1, VAR_HARDEN_SHORT_BUFSIZE) }
256 harden-large-queries{COLON}	{ YDVAR(1, VAR_HARDEN_LARGE_QUERIES) }
257 harden-glue{COLON}		{ YDVAR(1, VAR_HARDEN_GLUE) }
258 harden-dnssec-stripped{COLON}	{ YDVAR(1, VAR_HARDEN_DNSSEC_STRIPPED) }
259 harden-below-nxdomain{COLON}	{ YDVAR(1, VAR_HARDEN_BELOW_NXDOMAIN) }
260 harden-referral-path{COLON}	{ YDVAR(1, VAR_HARDEN_REFERRAL_PATH) }
261 use-caps-for-id{COLON}		{ YDVAR(1, VAR_USE_CAPS_FOR_ID) }
262 unwanted-reply-threshold{COLON}	{ YDVAR(1, VAR_UNWANTED_REPLY_THRESHOLD) }
263 private-address{COLON}		{ YDVAR(1, VAR_PRIVATE_ADDRESS) }
264 private-domain{COLON}		{ YDVAR(1, VAR_PRIVATE_DOMAIN) }
265 prefetch-key{COLON}		{ YDVAR(1, VAR_PREFETCH_KEY) }
266 prefetch{COLON}			{ YDVAR(1, VAR_PREFETCH) }
267 stub-zone{COLON}		{ YDVAR(0, VAR_STUB_ZONE) }
268 name{COLON}			{ YDVAR(1, VAR_NAME) }
269 stub-addr{COLON}		{ YDVAR(1, VAR_STUB_ADDR) }
270 stub-host{COLON}		{ YDVAR(1, VAR_STUB_HOST) }
271 stub-prime{COLON}		{ YDVAR(1, VAR_STUB_PRIME) }
272 stub-first{COLON}		{ YDVAR(1, VAR_STUB_FIRST) }
273 forward-zone{COLON}		{ YDVAR(0, VAR_FORWARD_ZONE) }
274 forward-addr{COLON}		{ YDVAR(1, VAR_FORWARD_ADDR) }
275 forward-host{COLON}		{ YDVAR(1, VAR_FORWARD_HOST) }
276 forward-first{COLON}		{ YDVAR(1, VAR_FORWARD_FIRST) }
277 do-not-query-address{COLON}	{ YDVAR(1, VAR_DO_NOT_QUERY_ADDRESS) }
278 do-not-query-localhost{COLON}	{ YDVAR(1, VAR_DO_NOT_QUERY_LOCALHOST) }
279 access-control{COLON}		{ YDVAR(2, VAR_ACCESS_CONTROL) }
280 hide-identity{COLON}		{ YDVAR(1, VAR_HIDE_IDENTITY) }
281 hide-version{COLON}		{ YDVAR(1, VAR_HIDE_VERSION) }
282 identity{COLON}			{ YDVAR(1, VAR_IDENTITY) }
283 version{COLON}			{ YDVAR(1, VAR_VERSION) }
284 module-config{COLON}     	{ YDVAR(1, VAR_MODULE_CONF) }
285 dlv-anchor{COLON}		{ YDVAR(1, VAR_DLV_ANCHOR) }
286 dlv-anchor-file{COLON}		{ YDVAR(1, VAR_DLV_ANCHOR_FILE) }
287 trust-anchor-file{COLON}	{ YDVAR(1, VAR_TRUST_ANCHOR_FILE) }
288 auto-trust-anchor-file{COLON}	{ YDVAR(1, VAR_AUTO_TRUST_ANCHOR_FILE) }
289 trusted-keys-file{COLON}	{ YDVAR(1, VAR_TRUSTED_KEYS_FILE) }
290 trust-anchor{COLON}		{ YDVAR(1, VAR_TRUST_ANCHOR) }
291 val-override-date{COLON}	{ YDVAR(1, VAR_VAL_OVERRIDE_DATE) }
292 val-sig-skew-min{COLON}		{ YDVAR(1, VAR_VAL_SIG_SKEW_MIN) }
293 val-sig-skew-max{COLON}		{ YDVAR(1, VAR_VAL_SIG_SKEW_MAX) }
294 val-bogus-ttl{COLON}		{ YDVAR(1, VAR_BOGUS_TTL) }
295 val-clean-additional{COLON}	{ YDVAR(1, VAR_VAL_CLEAN_ADDITIONAL) }
296 val-permissive-mode{COLON}	{ YDVAR(1, VAR_VAL_PERMISSIVE_MODE) }
297 ignore-cd-flag{COLON}		{ YDVAR(1, VAR_IGNORE_CD_FLAG) }
298 val-log-level{COLON}		{ YDVAR(1, VAR_VAL_LOG_LEVEL) }
299 key-cache-size{COLON}		{ YDVAR(1, VAR_KEY_CACHE_SIZE) }
300 key-cache-slabs{COLON}		{ YDVAR(1, VAR_KEY_CACHE_SLABS) }
301 neg-cache-size{COLON}		{ YDVAR(1, VAR_NEG_CACHE_SIZE) }
302 val-nsec3-keysize-iterations{COLON}	{
303 				  YDVAR(1, VAR_VAL_NSEC3_KEYSIZE_ITERATIONS) }
304 add-holddown{COLON}		{ YDVAR(1, VAR_ADD_HOLDDOWN) }
305 del-holddown{COLON}		{ YDVAR(1, VAR_DEL_HOLDDOWN) }
306 keep-missing{COLON}		{ YDVAR(1, VAR_KEEP_MISSING) }
307 use-syslog{COLON}		{ YDVAR(1, VAR_USE_SYSLOG) }
308 log-time-ascii{COLON}		{ YDVAR(1, VAR_LOG_TIME_ASCII) }
309 log-queries{COLON}		{ YDVAR(1, VAR_LOG_QUERIES) }
310 local-zone{COLON}		{ YDVAR(2, VAR_LOCAL_ZONE) }
311 local-data{COLON}		{ YDVAR(1, VAR_LOCAL_DATA) }
312 local-data-ptr{COLON}		{ YDVAR(1, VAR_LOCAL_DATA_PTR) }
313 unblock-lan-zones{COLON}	{ YDVAR(1, VAR_UNBLOCK_LAN_ZONES) }
314 statistics-interval{COLON}	{ YDVAR(1, VAR_STATISTICS_INTERVAL) }
315 statistics-cumulative{COLON}	{ YDVAR(1, VAR_STATISTICS_CUMULATIVE) }
316 extended-statistics{COLON}	{ YDVAR(1, VAR_EXTENDED_STATISTICS) }
317 remote-control{COLON}		{ YDVAR(0, VAR_REMOTE_CONTROL) }
318 control-enable{COLON}		{ YDVAR(1, VAR_CONTROL_ENABLE) }
319 control-interface{COLON}	{ YDVAR(1, VAR_CONTROL_INTERFACE) }
320 control-port{COLON}		{ YDVAR(1, VAR_CONTROL_PORT) }
321 server-key-file{COLON}		{ YDVAR(1, VAR_SERVER_KEY_FILE) }
322 server-cert-file{COLON}		{ YDVAR(1, VAR_SERVER_CERT_FILE) }
323 control-key-file{COLON}		{ YDVAR(1, VAR_CONTROL_KEY_FILE) }
324 control-cert-file{COLON}	{ YDVAR(1, VAR_CONTROL_CERT_FILE) }
325 python-script{COLON}		{ YDVAR(1, VAR_PYTHON_SCRIPT) }
326 python{COLON}			{ YDVAR(0, VAR_PYTHON) }
327 domain-insecure{COLON}		{ YDVAR(1, VAR_DOMAIN_INSECURE) }
328 minimal-responses{COLON}	{ YDVAR(1, VAR_MINIMAL_RESPONSES) }
329 rrset-roundrobin{COLON}		{ YDVAR(1, VAR_RRSET_ROUNDROBIN) }
330 max-udp-size{COLON}		{ YDVAR(1, VAR_MAX_UDP_SIZE) }
331 <INITIAL,val>{NEWLINE}		{ LEXOUT(("NL\n")); cfg_parser->line++; }
332 
333 	/* Quoted strings. Strip leading and ending quotes */
334 <val>\"			{ BEGIN(quotedstring); LEXOUT(("QS ")); }
335 <quotedstring><<EOF>>   {
336         ub_c_error("EOF inside quoted string");
337 	if(--num_args == 0) { BEGIN(INITIAL); }
338 	else		    { BEGIN(val); }
339 }
340 <quotedstring>{DQANY}*  { LEXOUT(("STR(%s) ", ub_c_text)); yymore(); }
341 <quotedstring>{NEWLINE} { ub_c_error("newline inside quoted string, no end \"");
342 			  cfg_parser->line++; BEGIN(INITIAL); }
343 <quotedstring>\" {
344         LEXOUT(("QE "));
345 	if(--num_args == 0) { BEGIN(INITIAL); }
346 	else		    { BEGIN(val); }
347         ub_c_text[ub_c_leng - 1] = '\0';
348 	ub_c_lval.str = strdup(ub_c_text);
349 	if(!ub_c_lval.str)
350 		ub_c_error("out of memory");
351         return STRING_ARG;
352 }
353 
354 	/* Single Quoted strings. Strip leading and ending quotes */
355 <val>\'			{ BEGIN(singlequotedstr); LEXOUT(("SQS ")); }
356 <singlequotedstr><<EOF>>   {
357         ub_c_error("EOF inside quoted string");
358 	if(--num_args == 0) { BEGIN(INITIAL); }
359 	else		    { BEGIN(val); }
360 }
361 <singlequotedstr>{SQANY}*  { LEXOUT(("STR(%s) ", ub_c_text)); yymore(); }
362 <singlequotedstr>{NEWLINE} { ub_c_error("newline inside quoted string, no end '");
363 			     cfg_parser->line++; BEGIN(INITIAL); }
364 <singlequotedstr>\' {
365         LEXOUT(("SQE "));
366 	if(--num_args == 0) { BEGIN(INITIAL); }
367 	else		    { BEGIN(val); }
368         ub_c_text[ub_c_leng - 1] = '\0';
369 	ub_c_lval.str = strdup(ub_c_text);
370 	if(!ub_c_lval.str)
371 		ub_c_error("out of memory");
372         return STRING_ARG;
373 }
374 
375 	/* include: directive */
376 <INITIAL,val>include{COLON}	{
377 	LEXOUT(("v(%s) ", ub_c_text)); inc_prev = YYSTATE; BEGIN(include); }
378 <include><<EOF>>	{
379         ub_c_error("EOF inside include directive");
380         BEGIN(inc_prev);
381 }
382 <include>{SPACE}*	{ LEXOUT(("ISP ")); /* ignore */ }
383 <include>{NEWLINE}	{ LEXOUT(("NL\n")); cfg_parser->line++;}
384 <include>\"		{ LEXOUT(("IQS ")); BEGIN(include_quoted); }
385 <include>{UNQUOTEDLETTER}*	{
386 	LEXOUT(("Iunquotedstr(%s) ", ub_c_text));
387 	config_start_include_glob(ub_c_text);
388 	BEGIN(inc_prev);
389 }
390 <include_quoted><<EOF>>	{
391         ub_c_error("EOF inside quoted string");
392         BEGIN(inc_prev);
393 }
394 <include_quoted>{DQANY}*	{ LEXOUT(("ISTR(%s) ", ub_c_text)); yymore(); }
395 <include_quoted>{NEWLINE}	{ ub_c_error("newline before \" in include name");
396 				  cfg_parser->line++; BEGIN(inc_prev); }
397 <include_quoted>\"	{
398 	LEXOUT(("IQE "));
399 	ub_c_text[ub_c_leng - 1] = '\0';
400 	config_start_include_glob(ub_c_text);
401 	BEGIN(inc_prev);
402 }
403 <INITIAL,val><<EOF>>	{
404 	LEXOUT(("LEXEOF "));
405 	yy_set_bol(1); /* Set beginning of line, so "^" rules match.  */
406 	if (!config_include_stack) {
407 		yyterminate();
408 	} else {
409 		fclose(ub_c_in);
410 		config_end_include();
411 	}
412 }
413 
414 <val>{UNQUOTEDLETTER}*	{ LEXOUT(("unquotedstr(%s) ", ub_c_text));
415 			if(--num_args == 0) { BEGIN(INITIAL); }
416 			ub_c_lval.str = strdup(ub_c_text); return STRING_ARG; }
417 
418 {UNQUOTEDLETTER_NOCOLON}*	{
419 	ub_c_error_msg("unknown keyword '%s'", ub_c_text);
420 	}
421 
422 <*>.	{
423 	ub_c_error_msg("stray '%s'", ub_c_text);
424 	}
425 
426 %%
427