1 /* $NetBSD: token.l,v 1.26 2020/05/12 16:17:58 christos Exp $ */ 2 /* $KAME: token.l,v 1.44 2003/10/21 07:20:58 itojun 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 35 #ifdef HAVE_CONFIG_H 36 #include "config.h" 37 #endif 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/socket.h> 42 #include <net/pfkeyv2.h> 43 #include <netinet/in.h> 44 #include PATH_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 #include <err.h> 53 54 #include "vchar.h" 55 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || \ 56 (defined(__APPLE__) && defined(__MACH__)) 57 #include "parse.h" 58 #else 59 #include "y.tab.h" 60 #endif 61 62 #include "extern.h" 63 64 /* make the code compile on *BSD-current */ 65 #ifndef SADB_X_AALG_SHA2_256 66 #define SADB_X_AALG_SHA2_256 (-1) 67 #endif 68 #ifndef SADB_X_AALG_SHA2_384 69 #define SADB_X_AALG_SHA2_384 (-1) 70 #endif 71 #ifndef SADB_X_AALG_SHA2_512 72 #define SADB_X_AALG_SHA2_512 (-1) 73 #endif 74 #ifndef SADB_X_AALG_RIPEMD160HMAC 75 #define SADB_X_AALG_RIPEMD160HMAC (-1) 76 #endif 77 #ifndef SADB_X_AALG_AES_XCBC_MAC 78 #define SADB_X_AALG_AES_XCBC_MAC (-1) 79 #endif 80 #ifndef SADB_X_EALG_TWOFISHCBC 81 #define SADB_X_EALG_TWOFISHCBC (-1) 82 #endif 83 #ifndef SADB_X_EALG_AESCTR 84 #define SADB_X_EALG_AESCTR (-1) 85 #endif 86 #if defined(SADB_X_EALG_AES) && ! defined(SADB_X_EALG_AESCBC) 87 #define SADB_X_EALG_AESCBC SADB_X_EALG_AES 88 #endif 89 %} 90 91 /* common section */ 92 nl \n 93 ws [ \t]+ 94 digit [0-9] 95 letter [0-9A-Za-z] 96 hexdigit [0-9A-Fa-f] 97 dot \. 98 hyphen \- 99 slash \/ 100 blcl \[ 101 elcl \] 102 semi \; 103 comment \#.* 104 quotedstring \"[^"]*\" 105 decstring {digit}+ 106 hexstring 0[xX]{hexdigit}+ 107 ipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*) 108 ipaddrmask {slash}{digit}{1,3} 109 name {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))* 110 hostname {name}(({dot}{name})+{dot}?)? 111 112 %s S_PL S_AUTHALG S_ENCALG 113 114 %option noinput nounput 115 %% 116 117 118 add { return(ADD); } 119 update { return(UPDATE); } 120 delete { return(DELETE); } 121 deleteall { return(DELETEALL); } 122 get { return(GET); } 123 getspi { return(GETSPI); } 124 flush { return(FLUSH); } 125 dump { return(DUMP); } 126 exit { return(EXIT); } 127 quit { return(EXIT); } 128 bye { return(EXIT); } 129 130 /* for management SPD */ 131 spdadd { return(SPDADD); } 132 spdupdate { return(SPDUPDATE); } 133 spddelete { return(SPDDELETE); } 134 spddump { return(SPDDUMP); } 135 spdflush { return(SPDFLUSH); } 136 tagged { return(TAGGED); } 137 {hyphen}P { BEGIN S_PL; return(F_POLICY); } 138 <S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.%\-+_/ \n\t\]\[]* { 139 yymore(); 140 141 /* count up for nl */ 142 { 143 char *p; 144 for (p = yytext; *p != '\0'; p++) 145 if (*p == '\n') 146 lineno++; 147 } 148 149 yylval.val.len = strlen(yytext); 150 yylval.val.buf = strdup(yytext); 151 if (!yylval.val.buf) 152 yyfatal("insufficient memory"); 153 154 return(PL_REQUESTS); 155 } 156 <S_PL>{semi} { BEGIN INITIAL; return(EOT); } 157 158 /* address resolution flags */ 159 {hyphen}[n46][n46]* { 160 yylval.val.len = strlen(yytext); 161 yylval.val.buf = strdup(yytext); 162 if (!yylval.val.buf) 163 yyfatal("insufficient memory"); 164 return(F_AIFLAGS); 165 } 166 167 /* security protocols */ 168 ah { yylval.num = 0; return(PR_AH); } 169 esp { yylval.num = 0; return(PR_ESP); } 170 ah-old { yylval.num = 1; return(PR_AH); } 171 esp-old { yylval.num = 1; return(PR_ESP); } 172 esp-udp { yylval.num = 0; return(PR_ESPUDP); } 173 ipcomp { yylval.num = 0; return(PR_IPCOMP); } 174 tcp { 175 yylval.num = 0; return(PR_TCP); 176 } 177 178 /* authentication alogorithm */ 179 {hyphen}A { BEGIN S_AUTHALG; return(F_AUTH); } 180 <S_AUTHALG>hmac-md5 { yylval.num = SADB_AALG_MD5HMAC; BEGIN INITIAL; return(ALG_AUTH); } 181 <S_AUTHALG>hmac-sha1 { yylval.num = SADB_AALG_SHA1HMAC; BEGIN INITIAL; return(ALG_AUTH); } 182 <S_AUTHALG>keyed-md5 { yylval.num = SADB_X_AALG_MD5; BEGIN INITIAL; return(ALG_AUTH); } 183 <S_AUTHALG>keyed-sha1 { yylval.num = SADB_X_AALG_SHA; BEGIN INITIAL; return(ALG_AUTH); } 184 <S_AUTHALG>hmac-sha2-256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); } 185 <S_AUTHALG>hmac-sha256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); } 186 <S_AUTHALG>hmac-sha2-384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); } 187 <S_AUTHALG>hmac-sha384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); } 188 <S_AUTHALG>hmac-sha2-512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); } 189 <S_AUTHALG>hmac-sha512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); } 190 <S_AUTHALG>hmac-ripemd160 { yylval.num = SADB_X_AALG_RIPEMD160HMAC; BEGIN INITIAL; return(ALG_AUTH); } 191 <S_AUTHALG>aes-xcbc-mac { yylval.num = SADB_X_AALG_AES_XCBC_MAC; BEGIN INITIAL; return(ALG_AUTH); } 192 <S_AUTHALG>tcp-md5 { 193 #ifdef SADB_X_AALG_TCP_MD5 194 yylval.num = SADB_X_AALG_TCP_MD5; 195 BEGIN INITIAL; 196 return(ALG_AUTH); 197 #endif 198 } 199 <S_AUTHALG>null { yylval.num = SADB_X_AALG_NULL; BEGIN INITIAL; return(ALG_AUTH_NOKEY); } 200 201 /* encryption alogorithm */ 202 {hyphen}E { BEGIN S_ENCALG; return(F_ENC); } 203 <S_ENCALG>des-cbc { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC); } 204 <S_ENCALG>3des-cbc { yylval.num = SADB_EALG_3DESCBC; BEGIN INITIAL; return(ALG_ENC); } 205 <S_ENCALG>null { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_NOKEY); } 206 <S_ENCALG>simple { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_OLD); } 207 <S_ENCALG>blowfish-cbc { yylval.num = SADB_X_EALG_BLOWFISHCBC; BEGIN INITIAL; return(ALG_ENC); } 208 <S_ENCALG>cast128-cbc { yylval.num = SADB_X_EALG_CAST128CBC; BEGIN INITIAL; return(ALG_ENC); } 209 <S_ENCALG>des-deriv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DESDERIV); } 210 <S_ENCALG>des-32iv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DES32IV); } 211 <S_ENCALG>twofish-cbc { yylval.num = SADB_X_EALG_TWOFISHCBC; BEGIN INITIAL; return(ALG_ENC); } 212 <S_ENCALG>aes-cbc { 213 #ifdef SADB_X_EALG_AESCBC 214 yylval.num = SADB_X_EALG_AESCBC; BEGIN INITIAL; return(ALG_ENC); 215 #endif 216 } 217 <S_ENCALG>rijndael-cbc { 218 #ifdef SADB_X_EALG_AESCBC 219 yylval.num = SADB_X_EALG_AESCBC; BEGIN INITIAL; return(ALG_ENC); 220 #endif 221 } 222 <S_ENCALG>aes-ctr { yylval.num = SADB_X_EALG_AESCTR; BEGIN INITIAL; return(ALG_ENC); } 223 <S_ENCALG>camellia-cbc { 224 #ifdef SADB_X_EALG_CAMELLIACBC 225 yylval.num = SADB_X_EALG_CAMELLIACBC; BEGIN INITIAL; return(ALG_ENC); 226 #endif 227 } 228 <S_ENCALG>aes-gcm-16 { 229 #ifdef SADB_X_EALG_AESGCM16 230 yylval.num = SADB_X_EALG_AESGCM16; BEGIN INITIAL; return(ALG_ENC); 231 #endif 232 } 233 <S_ENCALG>aes-gmac { 234 #ifdef SADB_X_EALG_AESGMAC 235 yylval.num = SADB_X_EALG_AESGMAC; BEGIN INITIAL; return(ALG_ENC); 236 #endif 237 } 238 239 /* compression algorithms */ 240 {hyphen}C { return(F_COMP); } 241 oui { yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); } 242 deflate { yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); } 243 lzs { yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); } 244 {hyphen}R { return(F_RAWCPI); } 245 246 /* extension */ 247 {hyphen}m { return(F_MODE); } 248 transport { yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); } 249 tunnel { yylval.num = IPSEC_MODE_TUNNEL; return(MODE); } 250 {hyphen}u { return(F_REQID); } 251 {hyphen}f { return(F_EXT); } 252 random-pad { yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); } 253 seq-pad { yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); } 254 zero-pad { yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); } 255 nocyclic-seq { return(NOCYCLICSEQ); } 256 {hyphen}r { return(F_REPLAY); } 257 {hyphen}lh { return(F_LIFETIME_HARD); } 258 {hyphen}ls { return(F_LIFETIME_SOFT); } 259 {hyphen}bh { return(F_LIFEBYTE_HARD); } 260 {hyphen}bs { return(F_LIFEBYTE_SOFT); } 261 {hyphen}ctx { return(SECURITY_CTX); } 262 {hyphen}esp_frag { return(F_ESPFRAG); } 263 264 /* ... */ 265 any { return(ANY); } 266 {ws} { } 267 {nl} { lineno++; } 268 {comment} 269 {semi} { return(EOT); } 270 271 /* for address parameters: /prefix, [port] */ 272 {slash} { return SLASH; } 273 {blcl} { return BLCL; } 274 {elcl} { return ELCL; } 275 276 /* parameter */ 277 {decstring} { 278 char *bp; 279 280 yylval.ulnum = strtoul(yytext, &bp, 10); 281 return(DECSTRING); 282 } 283 284 {hexstring} { 285 yylval.val.buf = strdup(yytext + 2); 286 if (!yylval.val.buf) 287 yyfatal("insufficient memory"); 288 yylval.val.len = strlen(yylval.val.buf); 289 290 return(HEXSTRING); 291 } 292 293 {quotedstring} { 294 char *p = yytext; 295 while (*++p != '"') ; 296 *p = '\0'; 297 yytext++; 298 yylval.val.len = yyleng - 2; 299 yylval.val.buf = strdup(yytext); 300 if (!yylval.val.buf) 301 yyfatal("insufficient memory"); 302 303 return(QUOTEDSTRING); 304 } 305 306 [A-Za-z0-9:][A-Za-z0-9:%\.-]* { 307 yylval.val.len = yyleng; 308 yylval.val.buf = strdup(yytext); 309 if (!yylval.val.buf) 310 yyfatal("insufficient memory"); 311 return(STRING); 312 } 313 314 [0-9,]+ { 315 yylval.val.len = yyleng; 316 yylval.val.buf = strdup(yytext); 317 if (!yylval.val.buf) 318 yyfatal("insufficient memory"); 319 return(STRING); 320 } 321 322 . { 323 yyfatal("Syntax error"); 324 /*NOTREACHED*/ 325 } 326 327 %% 328 329 void 330 yyfatal(const char *s) 331 { 332 yyerror(s); 333 exit(1); 334 } 335 336 void 337 yyerror(const char *s) 338 { 339 if (f_mode == MODE_SCRIPT) 340 warnx("%s,%d: %s at `%s'", filename, lineno, s, yytext); 341 else 342 printf("%s at `%s'", s, yytext); 343 } 344 345 int 346 parse(const char *fname, FILE *fp) 347 { 348 yyin = fp; 349 filename = fname; 350 lineno = 1; 351 parse_init(); 352 353 if (yyparse()) { 354 warnx("%s,%d: parse failed", filename, lineno); 355 return -1; 356 } 357 358 return 0; 359 } 360 361 int 362 parse_string(char *src) 363 { 364 int result; 365 YY_BUFFER_STATE buf_state; 366 367 buf_state = yy_scan_string(src); 368 result = yyparse(); 369 yy_delete_buffer(buf_state); 370 return result; 371 } 372 373