summaryrefslogtreecommitdiff
path: root/sbin/cgdconfig/cgdparse.y
blob: feb1f9e9e05c0b82825b0d83f235f1e56254bfba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
%{
/* $NetBSD: cgdparse.y,v 1.1 2003/03/24 02:02:50 elric Exp $ */

/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Roland C. Dowdeswell.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: cgdparse.y,v 1.1 2003/03/24 02:02:50 elric Exp $");
#endif

#include <stdio.h>

#include "params.h"
#include "utils.h"

extern char *yytext;
extern int yylineno;

int yylex(void);
int yyerror(char *);

struct params *cgdparsefile(FILE *);

struct params *yy_global_params;

%}
%union {
	int	 	 integer;
	struct {
		char	*text;
		int	 length;
	} token;
	string_t	*string;
	bits_t		*bits;
	struct params	*params;
	struct keygen	*keygen;
}

%type <params>	entry rules rule
%type <keygen>	kgrule kgbody kgvars kgvar deprecated
%type <string>	stringlit base64 intstr tokstr
%type <bits>	bits
%type <token>	token deptoken

%token <integer> INTEGER
%token <string> STRINGLIT

%token <token> ALGORITHM KEYLENGTH IVMETHOD VERIFY_METHOD
%token <token> KEYGEN SALT ITERATIONS KEY

%token EOL

/* Deprecated tokens */
%token <token> KEYGEN_METHOD KEYGEN_SALT KEYGEN_ITERATIONS XOR_KEY

%%

entry:	  rules				{ yy_global_params = $$; }

rules:	/* empty */			{ $$ = NULL; }
	| rules rule			{ $$ = params_combine($$, $2); }

rule:	  ALGORITHM stringlit EOL	{ $$ = params_algorithm($2); }
	| KEYLENGTH INTEGER EOL		{ $$ = params_keylen($2); }
	| IVMETHOD stringlit EOL	{ $$ = params_ivmeth($2); }
	| VERIFY_METHOD stringlit EOL	{ $$ = params_verify_method($2); }
	| kgrule			{ $$ = params_keygen($1); }
	| deprecated			{ $$ = params_dep_keygen($1); }
	| EOL				{ $$ = NULL; }

kgrule:	  KEYGEN stringlit kgbody EOL	{ $$ = keygen_set_method($3, $2); }

kgbody:	  kgvar				{ $$ = $1; }
	| '{' kgvars '}'		{ $$ = $2; }

kgvars: /* empty */			{ $$ = NULL; }
	| kgvars kgvar			{ $$ = keygen_combine($$, $2); }

kgvar:	  SALT bits EOL			{ $$ = keygen_salt($2); }
	| ITERATIONS INTEGER EOL	{ $$ = keygen_iterations($2); }
	| KEY bits EOL			{ $$ = keygen_key($2); }
	| EOL				{ $$ = NULL; }

stringlit:  STRINGLIT | tokstr | intstr

tokstr:	  token				{ $$ = string_new($1.text, $1.length); }

token:	  ALGORITHM | KEYLENGTH
	| IVMETHOD | VERIFY_METHOD
	| KEYGEN | SALT | ITERATIONS
	| KEY
	| deptoken

intstr:   INTEGER			{ $$ = string_fromint($1); }

bits:	  base64			{ $$ = bits_decode_d($1); }

base64:   stringlit
	| base64 stringlit		{ $$ = string_add_d($1, $2); }

/* The following rules are deprecated */

deprecated:
	  KEYGEN_METHOD stringlit EOL	{ $$ = keygen_method($2); }
	| KEYGEN_SALT bits EOL		{ $$ = keygen_salt($2); }
	| KEYGEN_ITERATIONS INTEGER EOL	{ $$ = keygen_iterations($2); }
	| XOR_KEY bits EOL		{ $$ = keygen_key($2); }

deptoken: KEYGEN_METHOD | KEYGEN_SALT
	| KEYGEN_ITERATIONS | XOR_KEY

%%

extern FILE *yyin;

struct params *
cgdparsefile(FILE *f)
{

	yyin = f;
	yy_global_params = NULL;
	if (yyparse()) {
		fprintf(stderr, "%s: failed to parse parameters file\n",
		    getprogname());
		params_free(yy_global_params);
		return NULL;
	}
	return yy_global_params;
}