xref: /netbsd-src/sbin/cgdconfig/cgdparse.y (revision 920e28df6585dfb4225870a6b5307bd2775eab35)
1 %{
2 /* $NetBSD: cgdparse.y,v 1.7 2022/08/12 10:49:17 riastradh Exp $ */
3 
4 /*-
5  * Copyright (c) 2003 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Roland C. Dowdeswell.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: cgdparse.y,v 1.7 2022/08/12 10:49:17 riastradh Exp $");
36 #endif
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include "params.h"
42 #include "utils.h"
43 #include "extern.h"
44 
45 static struct params *yy_global_params;
46 
47 %}
48 %union {
49 	int	 	 integer;
50 	struct {
51 		char	*text;
52 		int	 length;
53 	} token;
54 	string_t	*string;
55 	bits_t		*bits;
56 	struct params	*params;
57 	struct keygen	*keygen;
58 }
59 
60 %type <params>	entry rules rule
61 %type <keygen>	kgrule kgbody kgvars kgvar deprecated
62 %type <string>	stringlit base64 intstr tokstr
63 %type <bits>	bits
64 %type <token>	token deptoken
65 
66 %token <integer> INTEGER
67 %token <string> STRINGLIT
68 
69 %token <token> ALGORITHM KEYLENGTH IVMETHOD VERIFY_METHOD
70 %token <token> KEYGEN SALT ITERATIONS MEMORY PARALLELISM VERSION KEY CMD SHARED
71 %token <token> SUBKEY
72 
73 %token EOL
74 
75 /* Deprecated tokens */
76 %token <token> KEYGEN_METHOD KEYGEN_SALT KEYGEN_ITERATIONS XOR_KEY
77 
78 %%
79 
80 entry:	  rules				{ yy_global_params = $$; }
81 
82 rules:	/* empty */			{ $$ = NULL; }
83 	| rules rule			{ $$ = params_combine($$, $2); }
84 
85 rule:	  ALGORITHM stringlit EOL	{ $$ = params_algorithm($2); }
86 	| KEYLENGTH INTEGER EOL		{ $$ = params_keylen($2); }
87 	| IVMETHOD stringlit EOL	{ $$ = params_ivmeth($2); }
88 	| VERIFY_METHOD stringlit EOL	{ $$ = params_verify_method($2); }
89 	| kgrule			{ $$ = params_keygen($1); }
90 	| deprecated			{ $$ = params_dep_keygen($1); }
91 	| EOL				{ $$ = NULL; }
92 
93 kgrule:	  KEYGEN stringlit kgbody EOL	{ $$ = keygen_set_method($3, $2); }
94 
95 kgbody:	  kgvar				{ $$ = $1; }
96 	| '{' kgvars '}'		{ $$ = $2; }
97 
98 kgvars: /* empty */			{ $$ = NULL; }
99 	| kgvars kgvar			{ $$ = keygen_combine($$, $2); }
100 
101 kgvar:	  SALT bits EOL			{ $$ = keygen_salt($2); }
102 	| ITERATIONS INTEGER EOL	{ $$ = keygen_iterations($2); }
103 	| MEMORY INTEGER EOL		{ $$ = keygen_memory($2); }
104 	| PARALLELISM INTEGER EOL	{ $$ = keygen_parallelism($2); }
105 	| VERSION INTEGER EOL		{ $$ = keygen_version($2); }
106 	| KEY bits EOL			{ $$ = keygen_key($2); }
107 	| CMD stringlit EOL		{ $$ = keygen_cmd($2); }
108 	| SHARED stringlit ALGORITHM stringlit SUBKEY bits EOL
109 					{ $$ = keygen_shared($2, $4, $6); }
110 	| EOL				{ $$ = NULL; }
111 
112 stringlit:  STRINGLIT | tokstr | intstr
113 
114 tokstr:	  token				{ $$ = string_new($1.text, $1.length); }
115 
116 token:	  ALGORITHM | KEYLENGTH
117 	| IVMETHOD | VERIFY_METHOD
118 	| KEYGEN | SALT | ITERATIONS
119 	| KEY
120 	| deptoken
121 
122 intstr:   INTEGER			{ $$ = string_fromint($1); }
123 
124 bits:	  base64			{ $$ = bits_decode_d($1); }
125 
126 base64:   stringlit
127 	| base64 stringlit		{ $$ = string_add_d($1, $2); }
128 
129 /* The following rules are deprecated */
130 
131 deprecated:
132 	  KEYGEN_METHOD stringlit EOL	{ $$ = keygen_method($2); }
133 	| KEYGEN_SALT bits EOL		{ $$ = keygen_salt($2); }
134 	| KEYGEN_ITERATIONS INTEGER EOL	{ $$ = keygen_iterations($2); }
135 	| XOR_KEY bits EOL		{ $$ = keygen_key($2); }
136 
137 deptoken: KEYGEN_METHOD | KEYGEN_SALT
138 	| KEYGEN_ITERATIONS | XOR_KEY
139 
140 %%
141 
142 struct params *
143 cgdparsefile(FILE *f)
144 {
145 
146 	yyin = f;
147 	yy_global_params = NULL;
148 	if (yyparse()) {
149 		fprintf(stderr, "%s: failed to parse parameters file\n",
150 		    getprogname());
151 		params_free(yy_global_params);
152 		return NULL;
153 	}
154 	return yy_global_params;
155 }
156