aboutsummaryrefslogtreecommitdiff
path: root/cftoken.l
blob: 6afda5ffb5e29180914f6cad3fa34101b8b4cef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*	$KAME: cftoken.l,v 1.35 2005/01/12 06:06:11 suz Exp $	*/

%{
/*
 * Copyright (C) 2002 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <netinet/in.h>

#include <errno.h>
#include <syslog.h>
#include <string.h>
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#include "dhcp6.h"
#include "config.h"
#include "common.h"
#include "y.tab.h"

#define YY_NO_UNPUT 1

char *configfilename;
int lineno = 1;


/* Recursion limit for includes */
#define MAX_INCLUDE_DEPTH 10

static struct include_stack {
	char *path;
	YY_BUFFER_STATE state;
	int lineno;
} incstack[MAX_INCLUDE_DEPTH];
int incstackp = 0;


static int yy_first_time = 1;
static int yyerrorcount = 0;
 
#ifndef NOCONFIG_DEBUG
#define YYDEBUG 1

int cfdebug = 1;
#else
int cfdebug = 0;
#endif

extern int yyparse __P((void));
extern int cf_post_config __P((void));

static void cfdebug_print __P((char *, char *, int));

#define DP(str) if (cfdebug) cfdebug_print(str, yytext, yyleng)
#define DECHO if (cfdebug) cfdebug_print(NULL, yytext, yyleng);
#define DNOECHO if (cfdebug) cfdebug_print(NULL, "****", yyleng);
%}

/* abbreviations */
nl		\n
ws		[ \t]+
comma		,
comment		\#.*
semi		\;
quotedstring	\"[^\"]*\"
string		[a-zA-Z0-9:\._\-][a-zA-Z0-9:\._\-]*
digit		[0-9]
integer 	{digit}+
number		{integer}|({digit}*\.{integer})
hexdigit	[0-9A-Fa-f]
hexpair		{hexdigit}{hexdigit}
hexstring	0[xX]{hexpair}+
duid 		{hexpair}(:{hexpair})*
ifname		{string}
slash		\/
bcl		\{
ecl		\}

%s S_CNF
%s S_IFACE
%s S_PROFILE
%s S_PREF
%s S_HOST
%s S_DUID
%s S_IA
%s S_AUTH
%s S_KEY
%s S_SECRET
%s S_ADDRPOOL
%s S_INCL

%%
%{
	if (yy_first_time) {
		BEGIN S_CNF;
		yy_first_time = 0;
	}
%}
	/* interface configuration */
<S_CNF>interface { DECHO; BEGIN S_IFACE; return (INTERFACE); }
<S_IFACE>{ifname} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (IFNAME);
}

	/* profile configuration */
<S_CNF>profile { DECHO; BEGIN S_PROFILE; return (PROFILE); }
<S_PROFILE>{string} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (PROFILENAME);
}

	/* host configuration */
<S_CNF>host { DECHO; BEGIN S_HOST; return (HOST); }
<S_HOST>{string} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (HOSTNAME);
}

	/* address pool configuration */
<S_CNF>pool { DECHO; BEGIN S_ADDRPOOL; return (ADDRPOOL); }  

<S_ADDRPOOL>{string} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (POOLNAME);
}

<S_CNF>duid { DECHO; BEGIN S_DUID; return (DUID); }
<S_DUID>{duid} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (DUID_ID);
}

<S_CNF>address { DECHO; return (ADDRESS); }

<S_CNF>prefix { DECHO; return (PREFIX); }

<S_CNF>preference { DECHO; return (PREFERENCE); }

<S_CNF>script { DECHO; return (SCRIPT); }

<S_CNF>delayedkey { DECHO; return (DELAYEDKEY); }

	/* request */
<S_CNF>request { DECHO; return (REQUEST); }

	/* send */
<S_CNF>send { DECHO; return (SEND); }

	/* range */
<S_CNF>range { DECHO; return (RANGE); }
<S_CNF>to { DECHO; return (TO); }

	/* address-pool */
<S_CNF>address-pool { DECHO; return (ADDRESS_POOL); }

	/* DHCP options */
<S_CNF>option { DECHO; return (OPTION); }

<S_CNF>rapid-commit { DECHO; return (RAPID_COMMIT); }
<S_CNF>ia-pd { DECHO; return (IA_PD); }
<S_CNF>ia-na { DECHO; return (IA_NA); }
<S_CNF>domain-name-servers { DECHO; return (DNS_SERVERS); }
<S_CNF>domain-name { DECHO; return (DNS_NAME); }
<S_CNF>sip-server-address { DECHO; return (SIP_SERVERS); }
<S_CNF>sip-server-domain-name { DECHO; return (SIP_NAME); }
<S_CNF>ntp-servers { DECHO; return (NTP_SERVERS); }
<S_CNF>nis-server-address { DECHO; return (NIS_SERVERS); }
<S_CNF>nis-domain-name { DECHO; return (NIS_NAME); }
<S_CNF>nisp-server-address { DECHO; return (NISP_SERVERS); }
<S_CNF>nisp-domain-name { DECHO; return (NISP_NAME); }
<S_CNF>bcmcs-server-address { DECHO; return (BCMCS_SERVERS); }
<S_CNF>bcmcs-server-domain-name { DECHO; return (BCMCS_NAME); }
<S_CNF>refreshtime { DECHO; return (REFRESHTIME); }

	/* provided for a backward compatibility to WIDE-DHCPv6 before Oct 1 2006 */
<S_CNF>nis-server-domain-name { DECHO; return (NIS_NAME); }
<S_CNF>nisp-server-domain-name { DECHO; return (NISP_NAME); }	

	/* generic options */
<S_CNF>information-only { DECHO; return (INFO_ONLY); }

<S_CNF>allow { DECHO; return (ALLOW); }

	/* identity association */
<S_CNF>id-assoc { DECHO; BEGIN S_IA; return(ID_ASSOC); }
<S_IA>pd { DECHO; return(IA_PD); }
<S_IA>na { DECHO; return(IA_NA); }
<S_IA>{number} { DECHO; yylval.str = strdup(yytext); return(IAID); }
<S_IA>{bcl} { DP("begin of closure"); BEGIN S_CNF; return (BCL); }

	/*
	 * interface parameters for delegated prefix configuration.
	 * when lex reads an interface name, the state will be back to
	 * S_CNF.
	 */
<S_CNF>prefix-interface { DECHO; BEGIN S_IFACE; return (PREFIX_INTERFACE); }
<S_CNF>sla-id { DECHO; return (SLA_ID); }
<S_CNF>sla-len { DECHO; return (SLA_LEN); }
<S_CNF>ifid { DECHO; return (IFID); }

	/* duration */
<S_CNF>infinity { DECHO; return (INFINITY); }

	/* authentication option */
<S_CNF>authentication { DECHO; BEGIN S_AUTH; return (AUTHENTICATION); }
<S_AUTH>{string} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (AUTHNAME);
}

	/* authentication parameters */
<S_CNF>protocol { DECHO; return (PROTOCOL); };
<S_CNF>algorithm { DECHO; return (ALGORITHM); };
<S_CNF>rdm { DECHO; return (RDM); };
<S_CNF>key { DECHO; return (KEY); };

	/* authentication protocols */
<S_CNF>delayed { DECHO; return (DELAYED); };
<S_CNF>reconfig { DECHO; return (RECONFIG); };

	/* authentication algorithms */
<S_CNF>hmac-md5 { DECHO; return (HMACMD5); };
<S_CNF>HMAC-MD5 { DECHO; return (HMACMD5); };
<S_CNF>hmacmd5 { DECHO; return (HMACMD5); };
<S_CNF>HMACMD5 { DECHO; return (HMACMD5); };

	/* authentication RDM */
<S_CNF>monocounter { DECHO; return (MONOCOUNTER); };

	/* secret keys */
<S_CNF>keyinfo { DECHO; BEGIN S_KEY; return (KEYINFO); }
<S_KEY>{string} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (KEYNAME);
}

	/* key parameters */
<S_CNF>realm { DECHO; return (REALM); }
<S_CNF>keyid { DECHO; return (KEYID); }
<S_CNF>secret { DECHO; BEGIN S_SECRET; return (SECRET); }
<S_SECRET>{quotedstring} {
	DNOECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (QSTRING);
}
<S_CNF>expire { DECHO; return (EXPIRE); }

	/* include */
<S_CNF>include { DECHO; BEGIN S_INCL; return (INCLUDE); }
<S_INCL>{quotedstring} {
	DECHO;
	yylval.str = strdup(yytext);
	BEGIN S_CNF;
	return (QSTRING);
}

	/* quoted string */
{quotedstring} {
		DECHO;
		yylval.str = strdup(yytext);
		return (QSTRING);
	}

	/* misc */
{ws}		{ ; }
{nl}		{ lineno++; }
{comment}	{ DP("comment"); }
{number} 	{
	DECHO;
	yylval.num = strtoll(yytext, NULL, 10);
	return (NUMBER);
}
{slash} { DECHO; return (SLASH); }
{comma} { DECHO; return (COMMA); }
{semi} { DP("end of sentence"); return (EOS); }
{bcl} { DP("begin of closure"); return (BCL); }
{ecl} { DP("end of closure"); return (ECL); }

	/* generic string */
{string} {
		DECHO;
		yylval.str = strdup(yytext);
		return (STRING);
	}

<<EOF>>	{
		if (--incstackp < 0)
			yyterminate();
		else {
			yy_delete_buffer(YY_CURRENT_BUFFER);
			free(incstack[incstackp + 1].path);
			configfilename = incstack[incstackp].path;
			lineno = incstack[incstackp].lineno;
			yy_switch_to_buffer(incstack[incstackp].state);
		}
	}

%%
static void
cfdebug_print(w, t, l)
	char *w, *t;
	int l;
{
	if (w) {
		debug_printf(LOG_DEBUG, FNAME,
		    "<%d>%s [%s] (%d)", yy_start, w, t, l);
	} else {
		debug_printf(LOG_DEBUG, FNAME,
		    "<%d>[%s] (%d)", yy_start, t, l);
	}
}

static void
yyerror0(int level, char *s, va_list ap)
{
	char ebuf[BUFSIZ], *bp, *ep;

	bp = ebuf;
	ep = ebuf + sizeof(ebuf);
	bp += snprintf(bp, ep - bp, "%s %d: ", configfilename, lineno);
	if (bp < ep)
		bp += vsnprintf(bp, ep - bp, s, ap);

	debug_printf(level, FNAME, ebuf);
}

void
yyerror(char *s, ...)
{
	va_list ap;
#ifdef HAVE_STDARG_H
	va_start(ap, s);
#else
	va_start(ap);
#endif
	yyerror0(LOG_ERR, s, ap);
	va_end(ap);
	yyerrorcount++;
}

void
yywarn(char *s, ...)
{
	va_list ap;
#ifdef HAVE_STDARG_H
	va_start(ap, s);
#else
	va_start(ap);
#endif
	yyerror0(LOG_WARNING, s, ap);
	va_end(ap);
}

int
cfswitch_buffer(incl)
	char *incl;
{
	char *path = qstrdup(incl);
	FILE *fp;

	if (incstackp >= MAX_INCLUDE_DEPTH) {
		debug_printf(LOG_ERR, FNAME, "cfparse: includes nested too deeply");
		return (-1);
	}
	incstack[incstackp].path = configfilename;
	incstack[incstackp].state = YY_CURRENT_BUFFER;
	incstack[incstackp].lineno = lineno;

	fp = fopen(path, "r");
	if (fp == NULL) {
		debug_printf(LOG_ERR, FNAME, "cfparse: fopen(%s): %s",
			path, strerror(errno));
		if (errno == ENOENT)
			return (0);
		return (-1);
	}
	incstackp++;
	configfilename = path;
	lineno = 1;
	yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));

	BEGIN(S_CNF);

	return (0);
}

int
cfparse(conf)
	char *conf;
{
	configfilename = conf;
	if ((yyin = fopen(configfilename, "r")) == NULL) {
		debug_printf(LOG_ERR, FNAME, "cfparse: fopen(%s): %s",
			configfilename, strerror(errno));
		if (errno == ENOENT)
			return (0);
		return (-1);
	}

	if (yyparse() || yyerrorcount) {
		if (yyerrorcount) {
			yyerror("fatal parse failure: exiting (%d errors)",
				yyerrorcount);
		} else
			yyerror("fatal parse failure: exiting");
		return (-1);
	}

	return (cf_post_config());
}