xref: /freebsd/sbin/setkey/token.l (revision b42ac57f4f2c183fda94189f1516ae3c1eff851f)
1 /*	$FreeBSD$	*/
2 /*	$KAME: token.l,v 1.21 2001/05/18 05:35:01 sakane Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 %{
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <net/route.h>
38 #include <net/pfkeyv2.h>
39 #include <netkey/keydb.h>
40 #include <netkey/key_debug.h>
41 #include <netinet/in.h>
42 #include <netinet6/ipsec.h>
43 
44 #include <stdlib.h>
45 #include <limits.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include "vchar.h"
50 #ifdef __NetBSD__
51 #include "parse.h"
52 #else
53 #include "y.tab.h"
54 #endif
55 
56 #define DECHO \
57 	if (f_debug) {printf("<%d>", yy_start); ECHO ; printf("\n"); }
58 
59 #define CMDARG \
60 { \
61 	char *__buf__ = strdup(yytext), *__p__; \
62 	for (__p__ = __buf__; *__p__ != NULL; __p__++) \
63 		if (*__p__ == '\n' || *__p__ == '\t') \
64 			*__p__ = ' '; \
65 	strcat(cmdarg, __buf__); \
66 	free(__buf__); \
67 }
68 
69 #define PREPROC	DECHO CMDARG
70 
71 int lineno = 1;
72 char cmdarg[8192]; /* XXX: BUFSIZ is the better ? */
73 
74 extern u_char m_buf[BUFSIZ];
75 extern u_int m_len;
76 extern int f_debug;
77 
78 int yylex __P((void));
79 void yyfatal __P((const char *s));
80 void yyerror __P((const char *s));
81 extern void parse_init __P((void));
82 int parse __P((FILE **));
83 int yyparse __P((void));
84 
85 %}
86 
87 /* common section */
88 nl		\n
89 ws		[ \t]+
90 digit		[0-9]
91 letter		[0-9A-Za-z]
92 hexdigit	[0-9A-Fa-f]
93 /*octet		(([01]?{digit}?{digit})|((2([0-4]{digit}))|(25[0-5])))*/
94 special		[()+\|\?\*,]
95 dot		\.
96 comma		\,
97 hyphen		\-
98 colon		\:
99 slash		\/
100 bcl		\{
101 ecl		\}
102 blcl		\[
103 elcl		\]
104 percent		\%
105 semi		\;
106 usec		{dot}{digit}{1,6}
107 comment		\#.*
108 ccomment	"/*"
109 bracketstring	\<[^>]*\>
110 quotedstring	\"[^"]*\"
111 decstring	{digit}+
112 hexpair		{hexdigit}{hexdigit}
113 hexstring	0[xX]{hexdigit}+
114 octetstring	{octet}({dot}{octet})+
115 ipaddress	[a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*)
116 ipaddrmask	{slash}{digit}{1,3}
117 ipaddrport	{blcl}{decstring}{elcl}
118 keyword		{letter}{letter}+
119 name		{letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
120 hostname	{name}(({dot}{name})+{dot}?)?
121 
122 %s S_PL
123 
124 %%
125 
126 add		{ PREPROC; return(ADD); }
127 delete		{ PREPROC; return(DELETE); }
128 deleteall	{ PREPROC; return(DELETEALL); }
129 get		{ PREPROC; return(GET); }
130 flush		{ PREPROC; return(FLUSH); }
131 dump		{ PREPROC; return(DUMP); }
132 
133 	/* for management SPD */
134 spdadd		{ PREPROC; return(SPDADD); }
135 spddelete	{ PREPROC; return(SPDDELETE); }
136 spddump		{ PREPROC; return(SPDDUMP); }
137 spdflush	{ PREPROC; return(SPDFLUSH); }
138 {hyphen}P	{ BEGIN S_PL; PREPROC; return(F_POLICY); }
139 <S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.\-_/ \n\t]* {
140 		yymore();
141 
142 		/* count up for nl */
143 		    {
144 			char *p;
145 			for (p = yytext; *p != NULL; p++)
146 				if (*p == '\n')
147 					lineno++;
148 		    }
149 
150 		yylval.val.len = strlen(yytext);
151 		yylval.val.buf = strdup(yytext);
152 
153 		return(PL_REQUESTS);
154 }
155 <S_PL>{semi}	{ PREPROC; BEGIN INITIAL; return(EOT); }
156 
157 	/* security protocols */
158 ah		{ PREPROC; yylval.num = 0; return(PR_AH); }
159 esp		{ PREPROC; yylval.num = 0; return(PR_ESP); }
160 ah-old		{ PREPROC; yylval.num = 1; return(PR_AH); }
161 esp-old		{ PREPROC; yylval.num = 1; return(PR_ESP); }
162 ipcomp		{ PREPROC; yylval.num = 0; return(PR_IPCOMP); }
163 
164 	/* authentication alogorithm */
165 {hyphen}A	{ PREPROC; return(F_AUTH); }
166 hmac-md5	{ PREPROC; yylval.num = SADB_AALG_MD5HMAC; return(ALG_AUTH); }
167 hmac-sha1	{ PREPROC; yylval.num = SADB_AALG_SHA1HMAC; return(ALG_AUTH); }
168 keyed-md5	{ PREPROC; yylval.num = SADB_X_AALG_MD5; return(ALG_AUTH); }
169 keyed-sha1	{ PREPROC; yylval.num = SADB_X_AALG_SHA; return(ALG_AUTH); }
170 hmac-sha2-256	{ PREPROC; yylval.num = SADB_X_AALG_SHA2_256; return(ALG_AUTH); }
171 hmac-sha2-384	{ PREPROC; yylval.num = SADB_X_AALG_SHA2_384; return(ALG_AUTH); }
172 hmac-sha2-512	{ PREPROC; yylval.num = SADB_X_AALG_SHA2_512; return(ALG_AUTH); }
173 hmac-ripemd160	{ PREPROC; yylval.num = SADB_X_AALG_RIPEMD160HMAC; return(ALG_AUTH); }
174 aes-xcbc-mac	{ PREPROC; yylval.num = SADB_X_AALG_AES_XCBC_MAC; return(ALG_AUTH); }
175 null		{ PREPROC; yylval.num = SADB_X_AALG_NULL; return(ALG_AUTH); }
176 
177 	/* encryption alogorithm */
178 {hyphen}E	{ PREPROC; return(F_ENC); }
179 des-cbc		{ PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC); }
180 3des-cbc	{ PREPROC; yylval.num = SADB_EALG_3DESCBC; return(ALG_ENC); }
181 simple		{ PREPROC; yylval.num = SADB_EALG_NULL; return(ALG_ENC); }
182 blowfish-cbc	{ PREPROC; yylval.num = SADB_X_EALG_BLOWFISHCBC; return(ALG_ENC); }
183 cast128-cbc	{ PREPROC; yylval.num = SADB_X_EALG_CAST128CBC; return(ALG_ENC); }
184 des-deriv	{ PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DESDERIV); }
185 des-32iv	{ PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DES32IV); }
186 rijndael-cbc	{ PREPROC; yylval.num = SADB_X_EALG_RIJNDAELCBC; return(ALG_ENC); }
187 aes-ctr		{ PREPROC; yylval.num = SADB_X_EALG_AESCTR; return(ALG_ENC); }
188 
189 	/* compression algorithms */
190 {hyphen}C	{ PREPROC; return(F_COMP); }
191 oui		{ PREPROC; yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); }
192 deflate		{ PREPROC; yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); }
193 lzs		{ PREPROC; yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); }
194 {hyphen}R	{ PREPROC; return(F_RAWCPI); }
195 
196 	/* extension */
197 {hyphen}m	{ PREPROC; return(F_MODE); }
198 transport	{ PREPROC; yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); }
199 tunnel		{ PREPROC; yylval.num = IPSEC_MODE_TUNNEL; return(MODE); }
200 {hyphen}u	{ PREPROC; return(F_REQID); }
201 {hyphen}f	{ PREPROC; return(F_EXT); }
202 random-pad	{ PREPROC; yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); }
203 seq-pad		{ PREPROC; yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); }
204 zero-pad	{ PREPROC; yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); }
205 nocyclic-seq	{ PREPROC; return(NOCYCLICSEQ); }
206 {hyphen}r	{ PREPROC; return(F_REPLAY); }
207 {hyphen}lh	{ PREPROC; return(F_LIFETIME_HARD); }
208 {hyphen}ls	{ PREPROC; return(F_LIFETIME_SOFT); }
209 
210 	/* ... */
211 any		{ PREPROC; return(ANY); }
212 {ws}		{ PREPROC; }
213 {nl}		{ lineno++; }
214 {comment}
215 {semi}		{ PREPROC; return(EOT); }
216 
217 	/* parameter */
218 {decstring}	{
219 			char *bp;
220 
221 			PREPROC;
222 			yylval.num = strtoul(yytext, &bp, 10);
223 			return(DECSTRING);
224 		}
225 
226 {ipaddress}	{
227 			PREPROC;
228 
229 			yylval.val.len = yyleng;
230 			yylval.val.buf = strdup(yytext);
231 
232 			return(ADDRESS);
233 		}
234 
235 {ipaddrmask}	{
236 			PREPROC;
237 			yytext++;
238 			yylval.num = atoi(yytext);
239 			return(PREFIX);
240 		}
241 
242 {ipaddrport}	{
243 			char *p = yytext;
244 			PREPROC;
245 			while (*++p != ']') ;
246 			*p = NULL;
247 			yytext++;
248 			yylval.num = atoi(yytext);
249 			return(PORT);
250 		}
251 
252 {blcl}any{elcl}	{
253 			PREPROC;
254 			return(PORTANY);
255 		}
256 
257 {hexstring}	{
258 			int len = yyleng - 2; /* (str - "0x") */
259 			PREPROC;
260 			yylval.val.len = (len & 1) + (len / 2);
261 			/* fixed string if length is odd. */
262 			if (len & 1) {
263 				yytext[1] = '0';
264 				yylval.val.buf = strdup(yytext + 1);
265 			} else
266 				yylval.val.buf = strdup(yytext + 2);
267 
268 			return(HEXSTRING);
269 		}
270 
271 {quotedstring}	{
272 			char *p = yytext;
273 			PREPROC;
274 			while (*++p != '"') ;
275 			*p = NULL;
276 			yytext++;
277 			yylval.val.len = yyleng - 2;
278 			yylval.val.buf = strdup(yytext);
279 
280 			return(QUOTEDSTRING);
281 		}
282 
283 [a-z0-9.\-]*	{
284 			yylval.val.len = yyleng;
285 			yylval.val.buf = strdup(yytext);
286 			return(STRING);
287 		}
288 
289 .		{
290 			yyfatal("Syntax error");
291 			/*NOTREACHED*/
292 		}
293 
294 %%
295 
296 void
297 yyfatal(s)
298 	const char *s;
299 {
300 	yyerror(s);
301 	exit(1);
302 }
303 
304 void
305 yyerror(s)
306 	const char *s;
307 {
308 	printf("line %d: %s at [%s]\n", lineno, s, yytext);
309 }
310 
311 int
312 parse(fp)
313 	FILE **fp;
314 {
315 	yyin = *fp;
316 
317 	parse_init();
318 
319 	if (yyparse()) {
320 		printf("parse failed, line %d.\n", lineno);
321 		return(-1);
322 	}
323 
324 	return(0);
325 }
326