token.l revision 171135
11573Srgrimes/*	$FreeBSD: head/sbin/setkey/token.l 171135 2007-07-01 12:08:08Z gnn $	*/
21573Srgrimes/*	$KAME: token.l,v 1.43 2003/07/25 09:35:28 itojun Exp $	*/
31573Srgrimes
41573Srgrimes/*
51573Srgrimes * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
61573Srgrimes * All rights reserved.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. Neither the name of the project nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes%{
341573Srgrimes#include <sys/types.h>
351573Srgrimes#include <sys/param.h>
361573Srgrimes#include <sys/socket.h>
371573Srgrimes#include <net/route.h>
3816586Sjraynard#include <net/pfkeyv2.h>
391573Srgrimes#include <netipsec/keydb.h>
4016586Sjraynard#include <netipsec/key_debug.h>
4116586Sjraynard#include <netinet/in.h>
4250476Speter#include <netipsec/ipsec.h>
431573Srgrimes
441573Srgrimes#include <stdlib.h>
4571579Sdeischen#include <limits.h>
461573Srgrimes#include <string.h>
471573Srgrimes#include <unistd.h>
4881730Sache#include <errno.h>
491573Srgrimes#include <netdb.h>
5081730Sache
511573Srgrimes#include "vchar.h"
521573Srgrimes#include "y.tab.h"
5371579Sdeischen
541573Srgrimesint lineno = 1;
5535129Sjb
561573Srgrimesextern u_char m_buf[BUFSIZ];
571573Srgrimesextern u_int m_len;
581573Srgrimesextern int f_debug;
5943782Sdt
6043782Sdtint yylex __P((void));
6143782Sdtvoid yyfatal __P((const char *s));
6243782Sdtvoid yyerror __P((const char *s));
6343782Sdtextern void parse_init __P((void));
6443782Sdtint parse __P((FILE **));
6581817Sacheint yyparse __P((void));
6682743Sache%}
6781817Sache
6881817Sache/* common section */
6981817Sachenl		\n
7081817Sachews		[ \t]+
7181817Sachedigit		[0-9]
7281817Sacheletter		[0-9A-Za-z]
7381817Sachehexdigit	[0-9A-Fa-f]
7481817Sachedot		\.
7582743Sachehyphen		\-
7682743Sacheslash		\/
7781817Sacheblcl		\[
7843782Sdtelcl		\]
7943782Sdtsemi		\;
8071579Sdeischencomment		\#.*
8171579Sdeischenquotedstring	\"[^"]*\"
8271579Sdeischendecstring	{digit}+
8371579Sdeischenhexstring	0[xX]{hexdigit}+
8471579Sdeischenipaddress	[a-fA-F0-9:]([a-fA-F0-9:\.]*|[a-fA-F0-9:\.]*%[a-zA-Z0-9]*)
8571579Sdeischenipaddrmask	{slash}{digit}{1,3}
8671579Sdeischenname		{letter}(({letter}|{digit}|{hyphen})*({letter}|{digit}))*
8782743Sachehostname	{name}(({dot}{name})+{dot}?)?
8871579Sdeischen
8971579Sdeischen%s S_PL S_AUTHALG S_ENCALG
9071579Sdeischen
9171579Sdeischen%%
9271579Sdeischen
9371579Sdeischenadd		{ return(ADD); }
9481817Sachedelete		{ return(DELETE); }
9571579Sdeischendeleteall	{ return(DELETEALL); }
9682743Sacheget		{ return(GET); }
9782743Sacheflush		{ return(FLUSH); }
9871579Sdeischendump		{ return(DUMP); }
9971579Sdeischen
10071579Sdeischen	/* for management SPD */
1011573Srgrimesspdadd		{ return(SPDADD); }
1021573Srgrimesspddelete	{ return(SPDDELETE); }
1031573Srgrimesspddump		{ return(SPDDUMP); }
1041573Srgrimesspdflush	{ return(SPDFLUSH); }
1051573Srgrimestagged		{ return(TAGGED); }
10681817Sache{hyphen}P	{ BEGIN S_PL; return(F_POLICY); }
10771579Sdeischen<S_PL>[a-zA-Z0-9:\.\-_/ \n\t][a-zA-Z0-9:\.%\-_/ \n\t]* {
10843782Sdt			yymore();
1091573Srgrimes
11081817Sache			/* count up for nl */
1111573Srgrimes			    {
1121573Srgrimes				char *p;
11385513Sache				for (p = yytext; *p != '\0'; p++)
1141573Srgrimes					if (*p == '\n')
1151573Srgrimes						lineno++;
1161573Srgrimes			    }
1171573Srgrimes
1181573Srgrimes			yylval.val.len = strlen(yytext);
1191573Srgrimes			yylval.val.buf = strdup(yytext);
1201573Srgrimes			if (!yylval.val.buf)
1211573Srgrimes				yyfatal("insufficient memory");
12271579Sdeischen
12382588Sache			return(PL_REQUESTS);
1241573Srgrimes		}
1251573Srgrimes<S_PL>{semi}	{ BEGIN INITIAL; return(EOT); }
1261573Srgrimes
1271573Srgrimes	/* address resolution flags */
1281573Srgrimes{hyphen}[n46][n46]*	{
1291573Srgrimes			yylval.val.len = strlen(yytext);
1301573Srgrimes			yylval.val.buf = strdup(yytext);
1311573Srgrimes			if (!yylval.val.buf)
1321573Srgrimes				yyfatal("insufficient memory");
1331573Srgrimes			return(F_AIFLAGS);
1341573Srgrimes		}
13582668Sache
1361573Srgrimes	/* security protocols */
1371573Srgrimesah		{ yylval.num = 0; return(PR_AH); }
13882709Sacheesp		{ yylval.num = 0; return(PR_ESP); }
13982668Sacheah-old		{ yylval.num = 1; return(PR_AH); }
14082740Sacheesp-old		{ yylval.num = 1; return(PR_ESP); }
14182740Sacheipcomp		{ yylval.num = 0; return(PR_IPCOMP); }
14282740Sachetcp		{ yylval.num = 0; return(PR_TCP); }
14382740Sache
14482740Sache	/* authentication alogorithm */
14582740Sache{hyphen}A	{ BEGIN S_AUTHALG; return(F_AUTH); }
14681666Sache<S_AUTHALG>hmac-md5	{ yylval.num = SADB_AALG_MD5HMAC; BEGIN INITIAL; return(ALG_AUTH); }
14782588Sache<S_AUTHALG>hmac-sha1	{ yylval.num = SADB_AALG_SHA1HMAC; BEGIN INITIAL; return(ALG_AUTH); }
14881666Sache<S_AUTHALG>keyed-md5	{ yylval.num = SADB_X_AALG_MD5; BEGIN INITIAL; return(ALG_AUTH); }
1491573Srgrimes<S_AUTHALG>keyed-sha1	{ yylval.num = SADB_X_AALG_SHA; BEGIN INITIAL; return(ALG_AUTH); }
15081666Sache<S_AUTHALG>hmac-sha2-256 { yylval.num = SADB_X_AALG_SHA2_256; BEGIN INITIAL; return(ALG_AUTH); }
15181666Sache<S_AUTHALG>hmac-sha2-384 { yylval.num = SADB_X_AALG_SHA2_384; BEGIN INITIAL; return(ALG_AUTH); }
15282588Sache<S_AUTHALG>hmac-sha2-512 { yylval.num = SADB_X_AALG_SHA2_512; BEGIN INITIAL; return(ALG_AUTH); }
15381666Sache<S_AUTHALG>hmac-ripemd160 { yylval.num = SADB_X_AALG_RIPEMD160HMAC; BEGIN INITIAL; return(ALG_AUTH); }
15481819Sache<S_AUTHALG>aes-xcbc-mac { yylval.num = SADB_X_AALG_AES_XCBC_MAC; BEGIN INITIAL; return(ALG_AUTH); }
15581819Sache<S_AUTHALG>tcp-md5	{ yylval.num = SADB_X_AALG_TCP_MD5; BEGIN INITIAL; return(ALG_AUTH); }
15682588Sache<S_AUTHALG>null { yylval.num = SADB_X_AALG_NULL; BEGIN INITIAL; return(ALG_AUTH_NOKEY); }
15781819Sache
1581573Srgrimes	/* encryption alogorithm */
1591573Srgrimes{hyphen}E	{ BEGIN S_ENCALG; return(F_ENC); }
1601573Srgrimes<S_ENCALG>des-cbc	{ yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC); }
1611573Srgrimes<S_ENCALG>3des-cbc	{ yylval.num = SADB_EALG_3DESCBC; BEGIN INITIAL; return(ALG_ENC); }
1621573Srgrimes<S_ENCALG>null		{ yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_NOKEY); }
16381666Sache<S_ENCALG>simple	{ yylval.num = SADB_EALG_NULL; BEGIN INITIAL; return(ALG_ENC_OLD); }
16481666Sache<S_ENCALG>blowfish-cbc	{ yylval.num = SADB_X_EALG_BLOWFISHCBC; BEGIN INITIAL; return(ALG_ENC); }
16582588Sache<S_ENCALG>cast128-cbc	{ yylval.num = SADB_X_EALG_CAST128CBC; BEGIN INITIAL; return(ALG_ENC); }
16681666Sache<S_ENCALG>des-deriv	{ yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DESDERIV); }
1671573Srgrimes<S_ENCALG>des-32iv	{ yylval.num = SADB_EALG_DESCBC; BEGIN INITIAL; return(ALG_ENC_DES32IV); }
1681573Srgrimes<S_ENCALG>rijndael-cbc	{ yylval.num = SADB_X_EALG_RIJNDAELCBC; BEGIN INITIAL; return(ALG_ENC); }
1691573Srgrimes<S_ENCALG>aes-ctr	{ yylval.num = SADB_X_EALG_AESCTR; BEGIN INITIAL; return(ALG_ENC); }
1701573Srgrimes<S_ENCALG>camellia-cbc	{ yylval.num = SADB_X_EALG_CAMELLIACBC; BEGIN INITIAL; return(ALG_ENC); }
1711573Srgrimes
1721573Srgrimes	/* compression algorithms */
1731573Srgrimes{hyphen}C	{ return(F_COMP); }
17482588Sacheoui		{ yylval.num = SADB_X_CALG_OUI; return(ALG_COMP); }
1751573Srgrimesdeflate		{ yylval.num = SADB_X_CALG_DEFLATE; return(ALG_COMP); }
1761573Srgrimeslzs		{ yylval.num = SADB_X_CALG_LZS; return(ALG_COMP); }
1771573Srgrimes{hyphen}R	{ return(F_RAWCPI); }
1781573Srgrimes
1791573Srgrimes	/* extension */
1801573Srgrimes{hyphen}m	{ return(F_MODE); }
1811573Srgrimestransport	{ yylval.num = IPSEC_MODE_TRANSPORT; return(MODE); }
1821573Srgrimestunnel		{ yylval.num = IPSEC_MODE_TUNNEL; return(MODE); }
1831573Srgrimes{hyphen}u	{ return(F_REQID); }
1841573Srgrimes{hyphen}f	{ return(F_EXT); }
1851573Srgrimesrandom-pad	{ yylval.num = SADB_X_EXT_PRAND; return(EXTENSION); }
1861573Srgrimesseq-pad		{ yylval.num = SADB_X_EXT_PSEQ; return(EXTENSION); }
1871573Srgrimeszero-pad	{ yylval.num = SADB_X_EXT_PZERO; return(EXTENSION); }
1881573Srgrimesnocyclic-seq	{ return(NOCYCLICSEQ); }
1891573Srgrimes{hyphen}r	{ return(F_REPLAY); }
1901573Srgrimes{hyphen}lh	{ return(F_LIFETIME_HARD); }
19171579Sdeischen{hyphen}ls	{ return(F_LIFETIME_SOFT); }
1921573Srgrimes
1931573Srgrimes	/* ... */
1941573Srgrimesany		{ return(ANY); }
1951573Srgrimes{ws}		{ }
1961573Srgrimes{nl}		{ lineno++; }
1971573Srgrimes{comment}
1981573Srgrimes{semi}		{ return(EOT); }
1991573Srgrimes
2001573Srgrimes	/* for address parameters: /prefix, [port] */
2011573Srgrimes{slash}		{ return SLASH; }
2021573Srgrimes{blcl}		{ return BLCL; }
2031573Srgrimes{elcl}		{ return ELCL; }
2041573Srgrimes
2051573Srgrimes	/* parameter */
2061573Srgrimes{decstring}	{
20771579Sdeischen			char *bp;
2081573Srgrimes
20981822Sache			yylval.ulnum = strtoul(yytext, &bp, 10);
21081666Sache			return(DECSTRING);
21182588Sache		}
21281666Sache
2131573Srgrimes{hexstring}	{
21481666Sache			yylval.val.buf = strdup(yytext + 2);
21581666Sache			if (!yylval.val.buf)
21682588Sache				yyfatal("insufficient memory");
21781666Sache			yylval.val.len = strlen(yylval.val.buf);
21881819Sache
21981819Sache			return(HEXSTRING);
22082588Sache		}
22181819Sache
2221573Srgrimes{quotedstring}	{
2231573Srgrimes			char *p = yytext;
22482709Sache			while (*++p != '"') ;
22582668Sache			*p = '\0';
2261573Srgrimes			yytext++;
2271573Srgrimes			yylval.val.len = yyleng - 2;
22885396Sache			yylval.val.buf = strdup(yytext);
22985396Sache			if (!yylval.val.buf)
23082709Sache				yyfatal("insufficient memory");
23182709Sache
23282709Sache			return(QUOTEDSTRING);
23382709Sache		}
23482709Sache
2351573Srgrimes[A-Za-z0-9:][A-Za-z0-9:%\.-]* {
2361573Srgrimes			yylval.val.len = yyleng;
2371573Srgrimes			yylval.val.buf = strdup(yytext);
2381573Srgrimes			if (!yylval.val.buf)
2391573Srgrimes				yyfatal("insufficient memory");
2401573Srgrimes			return(STRING);
2411573Srgrimes		}
24272529Simp
2431573Srgrimes[0-9,]+ {
2441573Srgrimes			yylval.val.len = yyleng;
2451573Srgrimes			yylval.val.buf = strdup(yytext);
2461573Srgrimes			if (!yylval.val.buf)
2471573Srgrimes				yyfatal("insufficient memory");
2481573Srgrimes			return(STRING);
2491573Srgrimes		}
2501573Srgrimes
2511573Srgrimes.		{
2521573Srgrimes			yyfatal("Syntax error");
2531573Srgrimes			/*NOTREACHED*/
25482709Sache		}
2551573Srgrimes
25682742Sache%%
25782588Sache
2581573Srgrimesvoid
2591573Srgrimesyyfatal(s)
2601573Srgrimes	const char *s;
2611573Srgrimes{
2621573Srgrimes	yyerror(s);
2631573Srgrimes	exit(1);
2641573Srgrimes}
2651573Srgrimes
2661573Srgrimesvoid
26782588Sacheyyerror(s)
2681573Srgrimes	const char *s;
2691573Srgrimes{
2701573Srgrimes	printf("line %d: %s at [%s]\n", lineno, s, yytext);
2711573Srgrimes}
2721573Srgrimes
2731573Srgrimesint
2741573Srgrimesparse(fp)
2751573Srgrimes	FILE **fp;
2761573Srgrimes{
27782807Sache	yyin = *fp;
2781573Srgrimes
2791573Srgrimes	parse_init();
2804169Snate
2811573Srgrimes	if (yyparse()) {
2821573Srgrimes		printf("parse failed, line %d.\n", lineno);
2831573Srgrimes		return(-1);
2841573Srgrimes	}
2851573Srgrimes
2861573Srgrimes	return(0);
2871573Srgrimes}
2881573Srgrimes