Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / setkey / token.l
CommitLineData
984263bc
MD
1/* $FreeBSD: src/usr.sbin/setkey/token.l,v 1.2.2.3 2001/07/03 11:02:17 ume Exp $ */
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
71int lineno = 1;
72char cmdarg[8192]; /* XXX: BUFSIZ is the better ? */
73
74extern u_char m_buf[BUFSIZ];
75extern u_int m_len;
76extern int f_debug;
77
78int yylex __P((void));
79void yyfatal __P((const char *s));
80void yyerror __P((const char *s));
81extern void parse_init __P((void));
82int parse __P((FILE **));
83int yyparse __P((void));
84
85%}
86
87/* common section */
88nl \n
89ws [ \t]+
90digit [0-9]
91letter [0-9A-Za-z]
92hexdigit [0-9A-Fa-f]
93/*octet (([01]?{digit}?{digit})|((2([0-4]{digit}))|(25[0-5])))*/
94special [()+\|\?\*,]
95dot \.
96comma \,
97hyphen \-
98colon \:
99slash \/
100bcl \{
101ecl \}
102blcl \[
103elcl \]
104percent \%
105semi \;
106usec {dot}{digit}{1,6}
107comment \#.*
108ccomment "/*"
109bracketstring \<[^>]*\>
110quotedstring \"[^"]*\"
111decstring {digit}+
112hexpair {hexdigit}{hexdigit}
113hexstring 0[xX]{hexdigit}+
114octetstring {octet}({dot}{octet})+
115ipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*)
116ipaddrmask {slash}{digit}{1,3}
117ipaddrport {blcl}{decstring}{elcl}
118keyword {letter}{letter}+
119name {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
120hostname {name}(({dot}{name})+{dot}?)?
121
122%s S_PL
123
124%%
125
126add { PREPROC; return(ADD); }
127delete { PREPROC; return(DELETE); }
128deleteall { PREPROC; return(DELETEALL); }
129get { PREPROC; return(GET); }
130flush { PREPROC; return(FLUSH); }
131dump { PREPROC; return(DUMP); }
132
133 /* for management SPD */
134spdadd { PREPROC; return(SPDADD); }
135spddelete { PREPROC; return(SPDDELETE); }
136spddump { PREPROC; return(SPDDUMP); }
137spdflush { 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 */
158ah { PREPROC; yylval.num = 0; return(PR_AH); }
159esp { PREPROC; yylval.num = 0; return(PR_ESP); }
160ah-old { PREPROC; yylval.num = 1; return(PR_AH); }
161esp-old { PREPROC; yylval.num = 1; return(PR_ESP); }
162ipcomp { PREPROC; yylval.num = 0; return(PR_IPCOMP); }
163
164 /* authentication alogorithm */
165{hyphen}A { PREPROC; return(F_AUTH); }
166hmac-md5 { PREPROC; yylval.num = SADB_AALG_MD5HMAC; return(ALG_AUTH); }
167hmac-sha1 { PREPROC; yylval.num = SADB_AALG_SHA1HMAC; return(ALG_AUTH); }
168keyed-md5 { PREPROC; yylval.num = SADB_X_AALG_MD5; return(ALG_AUTH); }
169keyed-sha1 { PREPROC; yylval.num = SADB_X_AALG_SHA; return(ALG_AUTH); }
170hmac-sha2-256 { PREPROC; yylval.num = SADB_X_AALG_SHA2_256; return(ALG_AUTH); }
171hmac-sha2-384 { PREPROC; yylval.num = SADB_X_AALG_SHA2_384; return(ALG_AUTH); }
172hmac-sha2-512 { PREPROC; yylval.num = SADB_X_AALG_SHA2_512; return(ALG_AUTH); }
173null { PREPROC; yylval.num = SADB_X_AALG_NULL; return(ALG_AUTH); }
174
175 /* encryption alogorithm */
176{hyphen}E { PREPROC; return(F_ENC); }
177des-cbc { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC); }
1783des-cbc { PREPROC; yylval.num = SADB_EALG_3DESCBC; return(ALG_ENC); }
179simple { PREPROC; yylval.num = SADB_EALG_NULL; return(ALG_ENC); }
180blowfish-cbc { PREPROC; yylval.num = SADB_X_EALG_BLOWFISHCBC; return(ALG_ENC); }
181cast128-cbc { PREPROC; yylval.num = SADB_X_EALG_CAST128CBC; return(ALG_ENC); }
182des-deriv { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DESDERIV); }
183des-32iv { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DES32IV); }
184rijndael-cbc { PREPROC; yylval.num = SADB_X_EALG_RIJNDAELCBC; return(ALG_ENC); }
185
186 /* compression algorithms */
187{hyphen}C { PREPROC; return(F_COMP); }
188oui { PREPROC; yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); }
189deflate { PREPROC; yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); }
190lzs { PREPROC; yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); }
191{hyphen}R { PREPROC; return(F_RAWCPI); }
192
193 /* extension */
194{hyphen}m { PREPROC; return(F_MODE); }
195transport { PREPROC; yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); }
196tunnel { PREPROC; yylval.num = IPSEC_MODE_TUNNEL; return(MODE); }
197{hyphen}u { PREPROC; return(F_REQID); }
198{hyphen}f { PREPROC; return(F_EXT); }
199random-pad { PREPROC; yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); }
200seq-pad { PREPROC; yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); }
201zero-pad { PREPROC; yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); }
202nocyclic-seq { PREPROC; return(NOCYCLICSEQ); }
203{hyphen}r { PREPROC; return(F_REPLAY); }
204{hyphen}lh { PREPROC; return(F_LIFETIME_HARD); }
205{hyphen}ls { PREPROC; return(F_LIFETIME_SOFT); }
206
207 /* ... */
208any { PREPROC; return(ANY); }
209{ws} { PREPROC; }
210{nl} { lineno++; }
211{comment}
212{semi} { PREPROC; return(EOT); }
213
214 /* parameter */
215{decstring} {
216 char *bp;
217
218 PREPROC;
219 yylval.num = strtoul(yytext, &bp, 10);
220 return(DECSTRING);
221 }
222
223{ipaddress} {
224 PREPROC;
225
226 yylval.val.len = yyleng;
227 yylval.val.buf = strdup(yytext);
228
229 return(ADDRESS);
230 }
231
232{ipaddrmask} {
233 PREPROC;
234 yytext++;
235 yylval.num = atoi(yytext);
236 return(PREFIX);
237 }
238
239{ipaddrport} {
240 char *p = yytext;
241 PREPROC;
242 while (*++p != ']') ;
243 *p = NULL;
244 yytext++;
245 yylval.num = atoi(yytext);
246 return(PORT);
247 }
248
249{blcl}any{elcl} {
250 PREPROC;
251 return(PORTANY);
252 }
253
254{hexstring} {
255 int len = yyleng - 2; /* (str - "0x") */
256 PREPROC;
257 yylval.val.len = (len & 1) + (len / 2);
258 /* fixed string if length is odd. */
259 if (len & 1) {
260 yytext[1] = '0';
261 yylval.val.buf = strdup(yytext + 1);
262 } else
263 yylval.val.buf = strdup(yytext + 2);
264
265 return(HEXSTRING);
266 }
267
268{quotedstring} {
269 char *p = yytext;
270 PREPROC;
271 while (*++p != '"') ;
272 *p = NULL;
273 yytext++;
274 yylval.val.len = yyleng - 2;
275 yylval.val.buf = strdup(yytext);
276
277 return(QUOTEDSTRING);
278 }
279
280[a-z0-9.\-]* {
281 yylval.val.len = yyleng;
282 yylval.val.buf = strdup(yytext);
283 return(STRING);
284 }
285
286. {
287 yyfatal("Syntax error");
288 /*NOTREACHED*/
289 }
290
291%%
292
293void
294yyfatal(s)
295 const char *s;
296{
297 yyerror(s);
298 exit(1);
299}
300
301void
302yyerror(s)
303 const char *s;
304{
305 printf("line %d: %s at [%s]\n", lineno, s, yytext);
306}
307
308int
309parse(fp)
310 FILE **fp;
311{
312 yyin = *fp;
313
314 parse_init();
315
316 if (yyparse()) {
317 printf("parse failed, line %d.\n", lineno);
318 return(-1);
319 }
320
321 return(0);
322}