token.l revision 78064
162583Sitojun/* $FreeBSD: head/sbin/setkey/token.l 78064 2001-06-11 12:39:29Z ume $ */ 278064Sume/* $KAME: token.l,v 1.21 2001/05/18 05:35:01 sakane Exp $ */ 362583Sitojun 455505Sshin/* 555505Sshin * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 655505Sshin * All rights reserved. 762583Sitojun * 855505Sshin * Redistribution and use in source and binary forms, with or without 955505Sshin * modification, are permitted provided that the following conditions 1055505Sshin * are met: 1155505Sshin * 1. Redistributions of source code must retain the above copyright 1255505Sshin * notice, this list of conditions and the following disclaimer. 1355505Sshin * 2. Redistributions in binary form must reproduce the above copyright 1455505Sshin * notice, this list of conditions and the following disclaimer in the 1555505Sshin * documentation and/or other materials provided with the distribution. 1655505Sshin * 3. Neither the name of the project nor the names of its contributors 1755505Sshin * may be used to endorse or promote products derived from this software 1855505Sshin * without specific prior written permission. 1962583Sitojun * 2055505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2155505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2255505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2355505Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2455505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2555505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2655505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2755505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2855505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2955505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3055505Sshin * SUCH DAMAGE. 3155505Sshin */ 3255505Sshin 3355505Sshin%{ 3455505Sshin#include <sys/types.h> 3555505Sshin#include <sys/param.h> 3655505Sshin#include <sys/socket.h> 3755505Sshin#include <net/route.h> 3855505Sshin#include <net/pfkeyv2.h> 3955505Sshin#include <netkey/keydb.h> 4055505Sshin#include <netkey/key_debug.h> 4155505Sshin#include <netinet/in.h> 4255505Sshin#include <netinet6/ipsec.h> 4355505Sshin 4455505Sshin#include <stdlib.h> 4555505Sshin#include <limits.h> 4655505Sshin#include <string.h> 4755505Sshin#include <unistd.h> 4855505Sshin#include <errno.h> 4955505Sshin#include "vchar.h" 5078064Sume#ifdef __NetBSD__ 5178064Sume#include "parse.h" 5278064Sume#else 5355505Sshin#include "y.tab.h" 5478064Sume#endif 5555505Sshin 5655505Sshin#define DECHO \ 5755505Sshin if (f_debug) {printf("<%d>", yy_start); ECHO ; printf("\n"); } 5855505Sshin 5955505Sshin#define CMDARG \ 6055505Sshin{ \ 6155505Sshin char *__buf__ = strdup(yytext), *__p__; \ 6255505Sshin for (__p__ = __buf__; *__p__ != NULL; __p__++) \ 6355505Sshin if (*__p__ == '\n' || *__p__ == '\t') \ 6455505Sshin *__p__ = ' '; \ 6555505Sshin strcat(cmdarg, __buf__); \ 6655505Sshin free(__buf__); \ 6755505Sshin} 6855505Sshin 6955505Sshin#define PREPROC DECHO CMDARG 7055505Sshin 7162583Sitojunint lineno = 1; 7262583Sitojunchar cmdarg[8192]; /* XXX: BUFSIZ is the better ? */ 7355505Sshin 7462583Sitojunextern u_char m_buf[BUFSIZ]; 7562583Sitojunextern u_int m_len; 7662583Sitojunextern int f_debug; 7755505Sshin 7862583Sitojunint yylex __P((void)); 7962583Sitojunvoid yyfatal __P((const char *s)); 8062583Sitojunvoid yyerror __P((const char *s)); 8162583Sitojunextern void parse_init __P((void)); 8262583Sitojunint parse __P((FILE **)); 8362583Sitojunint yyparse __P((void)); 8455505Sshin 8555505Sshin%} 8655505Sshin 8755505Sshin/* common section */ 8855505Sshinnl \n 8955505Sshinws [ \t]+ 9055505Sshindigit [0-9] 9155505Sshinletter [0-9A-Za-z] 9255505Sshinhexdigit [0-9A-Fa-f] 9355505Sshin/*octet (([01]?{digit}?{digit})|((2([0-4]{digit}))|(25[0-5])))*/ 9455505Sshinspecial [()+\|\?\*,] 9555505Sshindot \. 9655505Sshincomma \, 9755505Sshinhyphen \- 9855505Sshincolon \: 9955505Sshinslash \/ 10055505Sshinbcl \{ 10155505Sshinecl \} 10255505Sshinblcl \[ 10355505Sshinelcl \] 10455505Sshinpercent \% 10555505Sshinsemi \; 10655505Sshinusec {dot}{digit}{1,6} 10755505Sshincomment \#.* 10855505Sshinccomment "/*" 10955505Sshinbracketstring \<[^>]*\> 11055505Sshinquotedstring \"[^"]*\" 11155505Sshindecstring {digit}+ 11255505Sshinhexpair {hexdigit}{hexdigit} 11355505Sshinhexstring 0[xX]{hexdigit}+ 11455505Sshinoctetstring {octet}({dot}{octet})+ 11562583Sitojunipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*) 11655505Sshinipaddrmask {slash}{digit}{1,3} 11755505Sshinipaddrport {blcl}{decstring}{elcl} 11855505Sshinkeyword {letter}{letter}+ 11955505Sshinname {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))* 12055505Sshinhostname {name}(({dot}{name})+{dot}?)? 12155505Sshin 12255505Sshin%s S_PL 12355505Sshin 12455505Sshin%% 12555505Sshin 12655505Sshinadd { PREPROC; return(ADD); } 12755505Sshindelete { PREPROC; return(DELETE); } 12878064Sumedeleteall { PREPROC; return(DELETEALL); } 12955505Sshinget { PREPROC; return(GET); } 13055505Sshinflush { PREPROC; return(FLUSH); } 13155505Sshindump { PREPROC; return(DUMP); } 13255505Sshin 13355505Sshin /* for management SPD */ 13455505Sshinspdadd { PREPROC; return(SPDADD); } 13555505Sshinspddelete { PREPROC; return(SPDDELETE); } 13655505Sshinspddump { PREPROC; return(SPDDUMP); } 13755505Sshinspdflush { PREPROC; return(SPDFLUSH); } 13855505Sshin{hyphen}P { BEGIN S_PL; PREPROC; return(F_POLICY); } 13955505Sshin<S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.\-_/ \n\t]* { 14055505Sshin yymore(); 14155505Sshin 14255505Sshin /* count up for nl */ 14355505Sshin { 14455505Sshin char *p; 14555505Sshin for (p = yytext; *p != NULL; p++) 14655505Sshin if (*p == '\n') 14755505Sshin lineno++; 14855505Sshin } 14955505Sshin 15055505Sshin yylval.val.len = strlen(yytext); 15155505Sshin yylval.val.buf = strdup(yytext); 15255505Sshin 15355505Sshin return(PL_REQUESTS); 15455505Sshin} 15555505Sshin<S_PL>{semi} { PREPROC; BEGIN INITIAL; return(EOT); } 15655505Sshin 15755505Sshin /* security protocols */ 15855505Sshinah { PREPROC; yylval.num = 0; return(PR_AH); } 15955505Sshinesp { PREPROC; yylval.num = 0; return(PR_ESP); } 16055505Sshinah-old { PREPROC; yylval.num = 1; return(PR_AH); } 16155505Sshinesp-old { PREPROC; yylval.num = 1; return(PR_ESP); } 16255505Sshinipcomp { PREPROC; yylval.num = 0; return(PR_IPCOMP); } 16355505Sshin 16455505Sshin /* authentication alogorithm */ 16555505Sshin{hyphen}A { PREPROC; return(F_AUTH); } 16655505Sshinhmac-md5 { PREPROC; yylval.num = SADB_AALG_MD5HMAC; return(ALG_AUTH); } 16755505Sshinhmac-sha1 { PREPROC; yylval.num = SADB_AALG_SHA1HMAC; return(ALG_AUTH); } 16878064Sumekeyed-md5 { PREPROC; yylval.num = SADB_X_AALG_MD5; return(ALG_AUTH); } 16978064Sumekeyed-sha1 { PREPROC; yylval.num = SADB_X_AALG_SHA; return(ALG_AUTH); } 17078064Sumehmac-sha2-256 { PREPROC; yylval.num = SADB_X_AALG_SHA2_256; return(ALG_AUTH); } 17178064Sumehmac-sha2-384 { PREPROC; yylval.num = SADB_X_AALG_SHA2_384; return(ALG_AUTH); } 17278064Sumehmac-sha2-512 { PREPROC; yylval.num = SADB_X_AALG_SHA2_512; return(ALG_AUTH); } 17378064Sumenull { PREPROC; yylval.num = SADB_X_AALG_NULL; return(ALG_AUTH); } 17455505Sshin 17555505Sshin /* encryption alogorithm */ 17655505Sshin{hyphen}E { PREPROC; return(F_ENC); } 17755505Sshindes-cbc { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC); } 17855505Sshin3des-cbc { PREPROC; yylval.num = SADB_EALG_3DESCBC; return(ALG_ENC); } 17955505Sshinsimple { PREPROC; yylval.num = SADB_EALG_NULL; return(ALG_ENC); } 18078064Sumeblowfish-cbc { PREPROC; yylval.num = SADB_X_EALG_BLOWFISHCBC; return(ALG_ENC); } 18178064Sumecast128-cbc { PREPROC; yylval.num = SADB_X_EALG_CAST128CBC; return(ALG_ENC); } 18255505Sshindes-deriv { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DESDERIV); } 18355505Sshindes-32iv { PREPROC; yylval.num = SADB_EALG_DESCBC; return(ALG_ENC_DES32IV); } 18478064Sumerijndael-cbc { PREPROC; yylval.num = SADB_X_EALG_RIJNDAELCBC; return(ALG_ENC); } 18555505Sshin 18655505Sshin /* compression algorithms */ 18755505Sshin{hyphen}C { PREPROC; return(F_COMP); } 18855505Sshinoui { PREPROC; yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); } 18955505Sshindeflate { PREPROC; yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); } 19055505Sshinlzs { PREPROC; yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); } 19155505Sshin{hyphen}R { PREPROC; return(F_RAWCPI); } 19255505Sshin 19355505Sshin /* extension */ 19455505Sshin{hyphen}m { PREPROC; return(F_MODE); } 19555505Sshintransport { PREPROC; yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); } 19655505Sshintunnel { PREPROC; yylval.num = IPSEC_MODE_TUNNEL; return(MODE); } 19762583Sitojun{hyphen}u { PREPROC; return(F_REQID); } 19855505Sshin{hyphen}f { PREPROC; return(F_EXT); } 19955505Sshinrandom-pad { PREPROC; yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); } 20055505Sshinseq-pad { PREPROC; yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); } 20155505Sshinzero-pad { PREPROC; yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); } 20262583Sitojunnocyclic-seq { PREPROC; return(NOCYCLICSEQ); } 20355505Sshin{hyphen}r { PREPROC; return(F_REPLAY); } 20455505Sshin{hyphen}lh { PREPROC; return(F_LIFETIME_HARD); } 20555505Sshin{hyphen}ls { PREPROC; return(F_LIFETIME_SOFT); } 20655505Sshin 20755505Sshin /* ... */ 20855505Sshinany { PREPROC; return(ANY); } 20955505Sshin{ws} { PREPROC; } 21055505Sshin{nl} { lineno++; } 21155505Sshin{comment} 21255505Sshin{semi} { PREPROC; return(EOT); } 21355505Sshin 21455505Sshin /* parameter */ 21555505Sshin{decstring} { 21655505Sshin char *bp; 21755505Sshin 21855505Sshin PREPROC; 21962583Sitojun yylval.num = strtoul(yytext, &bp, 10); 22055505Sshin return(DECSTRING); 22155505Sshin } 22255505Sshin 22362583Sitojun{ipaddress} { 22455505Sshin PREPROC; 22555505Sshin 22662583Sitojun yylval.val.len = yyleng; 22755505Sshin yylval.val.buf = strdup(yytext); 22855505Sshin 22962583Sitojun return(ADDRESS); 23055505Sshin } 23155505Sshin 23255505Sshin{ipaddrmask} { 23355505Sshin PREPROC; 23455505Sshin yytext++; 23555505Sshin yylval.num = atoi(yytext); 23655505Sshin return(PREFIX); 23755505Sshin } 23855505Sshin 23955505Sshin{ipaddrport} { 24055505Sshin char *p = yytext; 24155505Sshin PREPROC; 24255505Sshin while (*++p != ']') ; 24355505Sshin *p = NULL; 24455505Sshin yytext++; 24555505Sshin yylval.num = atoi(yytext); 24655505Sshin return(PORT); 24755505Sshin } 24855505Sshin 24955505Sshin{blcl}any{elcl} { 25055505Sshin PREPROC; 25155505Sshin return(PORTANY); 25255505Sshin } 25355505Sshin 25455505Sshin{hexstring} { 25555505Sshin int len = yyleng - 2; /* (str - "0x") */ 25655505Sshin PREPROC; 25755505Sshin yylval.val.len = (len & 1) + (len / 2); 25855505Sshin /* fixed string if length is odd. */ 25955505Sshin if (len & 1) { 26055505Sshin yytext[1] = '0'; 26155505Sshin yylval.val.buf = strdup(yytext + 1); 26255505Sshin } else 26355505Sshin yylval.val.buf = strdup(yytext + 2); 26455505Sshin 26555505Sshin return(HEXSTRING); 26655505Sshin } 26755505Sshin 26855505Sshin{quotedstring} { 26955505Sshin char *p = yytext; 27055505Sshin PREPROC; 27155505Sshin while (*++p != '"') ; 27255505Sshin *p = NULL; 27355505Sshin yytext++; 27455505Sshin yylval.val.len = yyleng - 2; 27555505Sshin yylval.val.buf = strdup(yytext); 27655505Sshin 27755505Sshin return(QUOTEDSTRING); 27855505Sshin } 27955505Sshin 28078064Sume[a-z0-9.\-]* { 28178064Sume yylval.val.len = yyleng; 28278064Sume yylval.val.buf = strdup(yytext); 28378064Sume return(STRING); 28478064Sume } 28578064Sume 28662583Sitojun. { 28762583Sitojun yyfatal("Syntax error"); 28862583Sitojun /*NOTREACHED*/ 28962583Sitojun } 29055505Sshin 29155505Sshin%% 29255505Sshin 29355505Sshinvoid 29462583Sitojunyyfatal(s) 29562583Sitojun const char *s; 29655505Sshin{ 29762583Sitojun yyerror(s); 29862583Sitojun exit(1); 29962583Sitojun} 30062583Sitojun 30162583Sitojunvoid 30262583Sitojunyyerror(s) 30362583Sitojun const char *s; 30462583Sitojun{ 30555505Sshin printf("line %d: %s at [%s]\n", lineno, s, yytext); 30655505Sshin} 30755505Sshin 30855505Sshinint 30955505Sshinparse(fp) 31055505Sshin FILE **fp; 31155505Sshin{ 31255505Sshin yyin = *fp; 31355505Sshin 31455505Sshin parse_init(); 31555505Sshin 31655505Sshin if (yyparse()) { 31755505Sshin printf("parse failed, line %d.\n", lineno); 31855505Sshin return(-1); 31955505Sshin } 32055505Sshin 32155505Sshin return(0); 32255505Sshin} 323