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