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