token.l revision 125681
162583Sitojun/* $FreeBSD: head/sbin/setkey/token.l 125681 2004-02-11 04:34:34Z bms $ */ 2122108Sume/* $KAME: token.l,v 1.43 2003/07/25 09:35:28 itojun 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> 49122108Sume#include <netdb.h> 50122108Sume 5155505Sshin#include "vchar.h" 5255505Sshin#include "y.tab.h" 5355505Sshin 5462583Sitojunint lineno = 1; 5555505Sshin 5662583Sitojunextern u_char m_buf[BUFSIZ]; 5762583Sitojunextern u_int m_len; 5862583Sitojunextern int f_debug; 5955505Sshin 6062583Sitojunint yylex __P((void)); 6162583Sitojunvoid yyfatal __P((const char *s)); 6262583Sitojunvoid yyerror __P((const char *s)); 6362583Sitojunextern void parse_init __P((void)); 6462583Sitojunint parse __P((FILE **)); 6562583Sitojunint yyparse __P((void)); 6655505Sshin%} 6755505Sshin 6855505Sshin/* common section */ 6955505Sshinnl \n 7055505Sshinws [ \t]+ 7155505Sshindigit [0-9] 7255505Sshinletter [0-9A-Za-z] 7355505Sshinhexdigit [0-9A-Fa-f] 7455505Sshindot \. 7555505Sshinhyphen \- 7655505Sshinslash \/ 7755505Sshinblcl \[ 7855505Sshinelcl \] 7955505Sshinsemi \; 8055505Sshincomment \#.* 8155505Sshinquotedstring \"[^"]*\" 8255505Sshindecstring {digit}+ 8355505Sshinhexstring 0[xX]{hexdigit}+ 8462583Sitojunipaddress [a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*) 8555505Sshinipaddrmask {slash}{digit}{1,3} 8655505Sshinname {letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))* 8755505Sshinhostname {name}(({dot}{name})+{dot}?)? 8855505Sshin 89122108Sume%s S_PL S_AUTHALG S_ENCALG 9055505Sshin 9155505Sshin%% 9255505Sshin 93122108Sumeadd { return(ADD); } 94122108Sumedelete { return(DELETE); } 95122108Sumedeleteall { return(DELETEALL); } 96122108Sumeget { return(GET); } 97122108Sumeflush { return(FLUSH); } 98122108Sumedump { return(DUMP); } 9955505Sshin 10055505Sshin /* for management SPD */ 101122108Sumespdadd { return(SPDADD); } 102122108Sumespddelete { return(SPDDELETE); } 103122108Sumespddump { return(SPDDUMP); } 104122108Sumespdflush { return(SPDFLUSH); } 105122108Sumetagged { return(TAGGED); } 106122108Sume{hyphen}P { BEGIN S_PL; return(F_POLICY); } 107122108Sume<S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.%\-_/ \n\t]* { 108122108Sume yymore(); 10955505Sshin 110122108Sume /* count up for nl */ 111122108Sume { 112122108Sume char *p; 113122108Sume for (p = yytext; *p != NULL; p++) 114122108Sume if (*p == '\n') 115122108Sume lineno++; 116122108Sume } 11755505Sshin 118122108Sume yylval.val.len = strlen(yytext); 119122108Sume yylval.val.buf = strdup(yytext); 120122108Sume if (!yylval.val.buf) 121122108Sume yyfatal("insufficient memory"); 12255505Sshin 123122108Sume return(PL_REQUESTS); 124122108Sume } 125122108Sume<S_PL>{semi} { BEGIN INITIAL; return(EOT); } 12655505Sshin 127122108Sume /* address resolution flags */ 128122108Sume{hyphen}[n46][n46]* { 129122108Sume yylval.val.len = strlen(yytext); 130122108Sume yylval.val.buf = strdup(yytext); 131122108Sume if (!yylval.val.buf) 132122108Sume yyfatal("insufficient memory"); 133122108Sume return(F_AIFLAGS); 134122108Sume } 135122108Sume 13655505Sshin /* security protocols */ 137122108Sumeah { yylval.num = 0; return(PR_AH); } 138122108Sumeesp { yylval.num = 0; return(PR_ESP); } 139122108Sumeah-old { yylval.num = 1; return(PR_AH); } 140122108Sumeesp-old { yylval.num = 1; return(PR_ESP); } 141122108Sumeipcomp { yylval.num = 0; return(PR_IPCOMP); } 142125681Sbmstcp { yylval.num = 0; return(PR_TCP); } 14355505Sshin 14455505Sshin /* authentication alogorithm */ 145122108Sume{hyphen}A { BEGIN S_AUTHALG; return(F_AUTH); } 146122108Sume<S_AUTHALG>hmac-md5 { yylval.num = SADB_AALG_MD5HMAC; BEGIN INITIAL; return(ALG_AUTH); } 147122108Sume<S_AUTHALG>hmac-sha1 { yylval.num = SADB_AALG_SHA1HMAC; BEGIN INITIAL; return(ALG_AUTH); } 148122108Sume<S_AUTHALG>keyed-md5 { yylval.num = SADB_X_AALG_MD5; BEGIN INITIAL; return(ALG_AUTH); } 149122108Sume<S_AUTHALG>keyed-sha1 { yylval.num = SADB_X_AALG_SHA; BEGIN INITIAL; return(ALG_AUTH); } 150122108Sume<S_AUTHALG>hmac-sha2-256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); } 151122108Sume<S_AUTHALG>hmac-sha2-384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); } 152122108Sume<S_AUTHALG>hmac-sha2-512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); } 153122108Sume<S_AUTHALG>hmac-ripemd160 { yylval.num = SADB_X_AALG_RIPEMD160HMAC; BEGIN INITIAL; return(ALG_AUTH); } 154122108Sume<S_AUTHALG>aes-xcbc-mac { yylval.num = SADB_X_AALG_AES_XCBC_MAC; BEGIN INITIAL; return(ALG_AUTH); } 155125681Sbms<S_AUTHALG>tcp-md5 { yylval.num = SADB_X_AALG_TCP_MD5; BEGIN INITIAL; return(ALG_AUTH); } 156122108Sume<S_AUTHALG>null { yylval.num = SADB_X_AALG_NULL; BEGIN INITIAL; return(ALG_AUTH_NOKEY); } 15755505Sshin 15855505Sshin /* encryption alogorithm */ 159122108Sume{hyphen}E { BEGIN S_ENCALG; return(F_ENC); } 160122108Sume<S_ENCALG>des-cbc { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC); } 161122108Sume<S_ENCALG>3des-cbc { yylval.num = SADB_EALG_3DESCBC; BEGIN INITIAL; return(ALG_ENC); } 162122108Sume<S_ENCALG>null { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_NOKEY); } 163122108Sume<S_ENCALG>simple { yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_OLD); } 164122108Sume<S_ENCALG>blowfish-cbc { yylval.num = SADB_X_EALG_BLOWFISHCBC; BEGIN INITIAL; return(ALG_ENC); } 165122108Sume<S_ENCALG>cast128-cbc { yylval.num = SADB_X_EALG_CAST128CBC; BEGIN INITIAL; return(ALG_ENC); } 166122108Sume<S_ENCALG>des-deriv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DESDERIV); } 167122108Sume<S_ENCALG>des-32iv { yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DES32IV); } 168122108Sume<S_ENCALG>rijndael-cbc { yylval.num = SADB_X_EALG_RIJNDAELCBC; BEGIN INITIAL; return(ALG_ENC); } 169122108Sume<S_ENCALG>aes-ctr { yylval.num = SADB_X_EALG_AESCTR; BEGIN INITIAL; return(ALG_ENC); } 17055505Sshin 17155505Sshin /* compression algorithms */ 172122108Sume{hyphen}C { return(F_COMP); } 173122108Sumeoui { yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); } 174122108Sumedeflate { yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); } 175122108Sumelzs { yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); } 176122108Sume{hyphen}R { return(F_RAWCPI); } 17755505Sshin 17855505Sshin /* extension */ 179122108Sume{hyphen}m { return(F_MODE); } 180122108Sumetransport { yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); } 181122108Sumetunnel { yylval.num = IPSEC_MODE_TUNNEL; return(MODE); } 182122108Sume{hyphen}u { return(F_REQID); } 183122108Sume{hyphen}f { return(F_EXT); } 184122108Sumerandom-pad { yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); } 185122108Sumeseq-pad { yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); } 186122108Sumezero-pad { yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); } 187122108Sumenocyclic-seq { return(NOCYCLICSEQ); } 188122108Sume{hyphen}r { return(F_REPLAY); } 189122108Sume{hyphen}lh { return(F_LIFETIME_HARD); } 190122108Sume{hyphen}ls { return(F_LIFETIME_SOFT); } 19155505Sshin 19255505Sshin /* ... */ 193122108Sumeany { return(ANY); } 194122108Sume{ws} { } 19555505Sshin{nl} { lineno++; } 19655505Sshin{comment} 197122108Sume{semi} { return(EOT); } 19855505Sshin 199122108Sume /* for address parameters: /prefix, [port] */ 200122108Sume{slash} { return SLASH; } 201122108Sume{blcl} { return BLCL; } 202122108Sume{elcl} { return ELCL; } 203122108Sume 20455505Sshin /* parameter */ 20555505Sshin{decstring} { 20655505Sshin char *bp; 20755505Sshin 208122108Sume yylval.ulnum = strtoul(yytext, &bp, 10); 20955505Sshin return(DECSTRING); 21055505Sshin } 21155505Sshin 21255505Sshin{hexstring} { 213122108Sume yylval.val.buf = strdup(yytext + 2); 214122108Sume if (!yylval.val.buf) 215122108Sume yyfatal("insufficient memory"); 216122108Sume yylval.val.len = strlen(yylval.val.buf); 21755505Sshin 21855505Sshin return(HEXSTRING); 21955505Sshin } 22055505Sshin 22155505Sshin{quotedstring} { 22255505Sshin char *p = yytext; 22355505Sshin while (*++p != '"') ; 22455505Sshin *p = NULL; 22555505Sshin yytext++; 22655505Sshin yylval.val.len = yyleng - 2; 22755505Sshin yylval.val.buf = strdup(yytext); 228122108Sume if (!yylval.val.buf) 229122108Sume yyfatal("insufficient memory"); 23055505Sshin 23155505Sshin return(QUOTEDSTRING); 23255505Sshin } 23355505Sshin 234122108Sume[A-Za-z0-9:][A-Za-z0-9:%\.-]* { 23578064Sume yylval.val.len = yyleng; 23678064Sume yylval.val.buf = strdup(yytext); 237122108Sume if (!yylval.val.buf) 238122108Sume yyfatal("insufficient memory"); 23978064Sume return(STRING); 24078064Sume } 24178064Sume 242122108Sume[0-9,]+ { 243122108Sume yylval.val.len = yyleng; 244122108Sume yylval.val.buf = strdup(yytext); 245122108Sume if (!yylval.val.buf) 246122108Sume yyfatal("insufficient memory"); 247122108Sume return(STRING); 248122108Sume } 249122108Sume 25062583Sitojun. { 25162583Sitojun yyfatal("Syntax error"); 25262583Sitojun /*NOTREACHED*/ 25362583Sitojun } 25455505Sshin 25555505Sshin%% 25655505Sshin 25755505Sshinvoid 25862583Sitojunyyfatal(s) 25962583Sitojun const char *s; 26055505Sshin{ 26162583Sitojun yyerror(s); 26262583Sitojun exit(1); 26362583Sitojun} 26462583Sitojun 26562583Sitojunvoid 26662583Sitojunyyerror(s) 26762583Sitojun const char *s; 26862583Sitojun{ 26955505Sshin printf("line %d: %s at [%s]\n", lineno, s, yytext); 27055505Sshin} 27155505Sshin 27255505Sshinint 27355505Sshinparse(fp) 27455505Sshin FILE **fp; 27555505Sshin{ 27655505Sshin yyin = *fp; 27755505Sshin 27855505Sshin parse_init(); 27955505Sshin 28055505Sshin if (yyparse()) { 28155505Sshin printf("parse failed, line %d.\n", lineno); 28255505Sshin return(-1); 28355505Sshin } 28455505Sshin 28555505Sshin return(0); 28655505Sshin} 287