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