xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hx509/sel.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1*afab4e30Schristos /*	$NetBSD: sel.c,v 1.4 2023/06/19 21:41:44 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2008 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "hx_locl.h"
37ca1c9b0cSelric 
38ca1c9b0cSelric struct hx_expr *
_hx509_make_expr(enum hx_expr_op op,void * arg1,void * arg2)39ca1c9b0cSelric _hx509_make_expr(enum hx_expr_op op, void *arg1, void *arg2)
40ca1c9b0cSelric {
41ca1c9b0cSelric     struct hx_expr *expr;
42ca1c9b0cSelric 
43ca1c9b0cSelric     expr = malloc(sizeof(*expr));
44ca1c9b0cSelric     if (expr == NULL)
45ca1c9b0cSelric 	return NULL;
46ca1c9b0cSelric     expr->op = op;
47ca1c9b0cSelric     expr->arg1 = arg1;
48ca1c9b0cSelric     expr->arg2 = arg2;
49ca1c9b0cSelric 
50ca1c9b0cSelric     return expr;
51ca1c9b0cSelric }
52ca1c9b0cSelric 
53ca1c9b0cSelric static const char *
eval_word(hx509_context context,hx509_env env,struct hx_expr * word)54ca1c9b0cSelric eval_word(hx509_context context, hx509_env env, struct hx_expr *word)
55ca1c9b0cSelric {
56ca1c9b0cSelric     switch (word->op) {
57ca1c9b0cSelric     case expr_STRING:
58ca1c9b0cSelric 	return word->arg1;
59ca1c9b0cSelric     case expr_VAR:
60ca1c9b0cSelric 	if (word->arg2 == NULL)
61ca1c9b0cSelric 	    return hx509_env_find(context, env, word->arg1);
62ca1c9b0cSelric 
63ca1c9b0cSelric 	env = hx509_env_find_binding(context, env, word->arg1);
64ca1c9b0cSelric 	if (env == NULL)
65ca1c9b0cSelric 	    return NULL;
66ca1c9b0cSelric 
67ca1c9b0cSelric 	return eval_word(context, env, word->arg2);
68ca1c9b0cSelric     default:
69ca1c9b0cSelric 	return NULL;
70ca1c9b0cSelric     }
71ca1c9b0cSelric }
72ca1c9b0cSelric 
73ca1c9b0cSelric static hx509_env
find_variable(hx509_context context,hx509_env env,struct hx_expr * word)74ca1c9b0cSelric find_variable(hx509_context context, hx509_env env, struct hx_expr *word)
75ca1c9b0cSelric {
76ca1c9b0cSelric     assert(word->op == expr_VAR);
77ca1c9b0cSelric 
78ca1c9b0cSelric     if (word->arg2 == NULL)
79ca1c9b0cSelric 	return hx509_env_find_binding(context, env, word->arg1);
80ca1c9b0cSelric 
81ca1c9b0cSelric     env = hx509_env_find_binding(context, env, word->arg1);
82ca1c9b0cSelric     if (env == NULL)
83ca1c9b0cSelric 	return NULL;
84ca1c9b0cSelric     return find_variable(context, env, word->arg2);
85ca1c9b0cSelric }
86ca1c9b0cSelric 
87ca1c9b0cSelric static int
eval_comp(hx509_context context,hx509_env env,struct hx_expr * expr)88ca1c9b0cSelric eval_comp(hx509_context context, hx509_env env, struct hx_expr *expr)
89ca1c9b0cSelric {
90ca1c9b0cSelric     switch (expr->op) {
91ca1c9b0cSelric     case comp_NE:
92ca1c9b0cSelric     case comp_EQ:
93ca1c9b0cSelric     case comp_TAILEQ: {
94ca1c9b0cSelric 	const char *s1, *s2;
95ca1c9b0cSelric 	int ret;
96ca1c9b0cSelric 
97ca1c9b0cSelric 	s1 = eval_word(context, env, expr->arg1);
98ca1c9b0cSelric 	s2 = eval_word(context, env, expr->arg2);
99ca1c9b0cSelric 
100ca1c9b0cSelric 	if (s1 == NULL || s2 == NULL)
101ca1c9b0cSelric 	    return FALSE;
102ca1c9b0cSelric 
103ca1c9b0cSelric 	if (expr->op == comp_TAILEQ) {
104ca1c9b0cSelric 	    size_t len1 = strlen(s1);
105ca1c9b0cSelric 	    size_t len2 = strlen(s2);
106ca1c9b0cSelric 
107ca1c9b0cSelric 	    if (len1 < len2)
108ca1c9b0cSelric 		return 0;
109ca1c9b0cSelric 	    ret = strcmp(s1 + (len1 - len2), s2) == 0;
110ca1c9b0cSelric 	} else {
111ca1c9b0cSelric 	    ret = strcmp(s1, s2) == 0;
112ca1c9b0cSelric 	    if (expr->op == comp_NE)
113ca1c9b0cSelric 		ret = !ret;
114ca1c9b0cSelric 	}
115ca1c9b0cSelric 	return ret;
116ca1c9b0cSelric     }
117ca1c9b0cSelric     case comp_IN: {
118ca1c9b0cSelric 	struct hx_expr *subexpr;
119ca1c9b0cSelric 	const char *w, *s1;
120ca1c9b0cSelric 
121ca1c9b0cSelric 	w = eval_word(context, env, expr->arg1);
122ca1c9b0cSelric 
123ca1c9b0cSelric 	subexpr = expr->arg2;
124ca1c9b0cSelric 
125ca1c9b0cSelric 	if (subexpr->op == expr_WORDS) {
126ca1c9b0cSelric 	    while (subexpr) {
127ca1c9b0cSelric 		s1 = eval_word(context, env, subexpr->arg1);
128ca1c9b0cSelric 		if (strcmp(w, s1) == 0)
129ca1c9b0cSelric 		    return TRUE;
130ca1c9b0cSelric 		subexpr = subexpr->arg2;
131ca1c9b0cSelric 	    }
132ca1c9b0cSelric 	} else if (subexpr->op == expr_VAR) {
133ca1c9b0cSelric 	    hx509_env subenv;
134ca1c9b0cSelric 
135ca1c9b0cSelric 	    subenv = find_variable(context, env, subexpr);
136ca1c9b0cSelric 	    if (subenv == NULL)
137ca1c9b0cSelric 		return FALSE;
138ca1c9b0cSelric 
139ca1c9b0cSelric 	    while (subenv) {
140ca1c9b0cSelric 		if (subenv->type != env_string)
141ca1c9b0cSelric 		    continue;
142ca1c9b0cSelric 		if (strcmp(w, subenv->name) == 0)
143ca1c9b0cSelric 		    return TRUE;
144ca1c9b0cSelric 		if (strcmp(w, subenv->u.string) == 0)
145ca1c9b0cSelric 		    return TRUE;
146ca1c9b0cSelric 		subenv = subenv->next;
147ca1c9b0cSelric 	    }
148ca1c9b0cSelric 
149ca1c9b0cSelric 	} else
150ca1c9b0cSelric 	    _hx509_abort("hx509 eval IN unknown op: %d", (int)subexpr->op);
151ca1c9b0cSelric 
152ca1c9b0cSelric 	return FALSE;
153ca1c9b0cSelric     }
154ca1c9b0cSelric     default:
155ca1c9b0cSelric 	_hx509_abort("hx509 eval expr with unknown op: %d", (int)expr->op);
156ca1c9b0cSelric     }
157ca1c9b0cSelric     return FALSE;
158ca1c9b0cSelric }
159ca1c9b0cSelric 
160ca1c9b0cSelric int
_hx509_expr_eval(hx509_context context,hx509_env env,struct hx_expr * expr)161ca1c9b0cSelric _hx509_expr_eval(hx509_context context, hx509_env env, struct hx_expr *expr)
162ca1c9b0cSelric {
163ca1c9b0cSelric     switch (expr->op) {
164ca1c9b0cSelric     case op_TRUE:
165ca1c9b0cSelric 	return 1;
166ca1c9b0cSelric     case op_FALSE:
167ca1c9b0cSelric 	return 0;
168ca1c9b0cSelric     case op_NOT:
169ca1c9b0cSelric 	return ! _hx509_expr_eval(context, env, expr->arg1);
170ca1c9b0cSelric     case op_AND:
171ca1c9b0cSelric 	return _hx509_expr_eval(context, env, expr->arg1) &&
172ca1c9b0cSelric 	    _hx509_expr_eval(context, env, expr->arg2);
173ca1c9b0cSelric     case op_OR:
174ca1c9b0cSelric 	return _hx509_expr_eval(context, env, expr->arg1) ||
175ca1c9b0cSelric 	    _hx509_expr_eval(context, env, expr->arg2);
176ca1c9b0cSelric     case op_COMP:
177ca1c9b0cSelric 	return eval_comp(context, env, expr->arg1);
178ca1c9b0cSelric     default:
179ca1c9b0cSelric 	_hx509_abort("hx509 eval expr with unknown op: %d", (int)expr->op);
180ca1c9b0cSelric 	UNREACHABLE(return 0);
181ca1c9b0cSelric     }
182ca1c9b0cSelric }
183ca1c9b0cSelric 
184ca1c9b0cSelric void
_hx509_expr_free(struct hx_expr * expr)185ca1c9b0cSelric _hx509_expr_free(struct hx_expr *expr)
186ca1c9b0cSelric {
187ca1c9b0cSelric     switch (expr->op) {
188ca1c9b0cSelric     case expr_STRING:
189ca1c9b0cSelric     case expr_NUMBER:
190ca1c9b0cSelric 	free(expr->arg1);
191ca1c9b0cSelric 	break;
192ca1c9b0cSelric     case expr_WORDS:
193ca1c9b0cSelric     case expr_FUNCTION:
194ca1c9b0cSelric     case expr_VAR:
195ca1c9b0cSelric 	free(expr->arg1);
196ca1c9b0cSelric 	if (expr->arg2)
197ca1c9b0cSelric 	    _hx509_expr_free(expr->arg2);
198ca1c9b0cSelric 	break;
199ca1c9b0cSelric     default:
200ca1c9b0cSelric 	if (expr->arg1)
201ca1c9b0cSelric 	    _hx509_expr_free(expr->arg1);
202ca1c9b0cSelric 	if (expr->arg2)
203ca1c9b0cSelric 	    _hx509_expr_free(expr->arg2);
204ca1c9b0cSelric 	break;
205ca1c9b0cSelric     }
206ca1c9b0cSelric     free(expr);
207ca1c9b0cSelric }
208ca1c9b0cSelric 
209ca1c9b0cSelric struct hx_expr *
_hx509_expr_parse(const char * buf)210ca1c9b0cSelric _hx509_expr_parse(const char *buf)
211ca1c9b0cSelric {
212ca1c9b0cSelric     _hx509_expr_input.buf = buf;
213ca1c9b0cSelric     _hx509_expr_input.length = strlen(buf);
214ca1c9b0cSelric     _hx509_expr_input.offset = 0;
215ca1c9b0cSelric     _hx509_expr_input.expr = NULL;
216ca1c9b0cSelric 
217ca1c9b0cSelric     if (_hx509_expr_input.error) {
218ca1c9b0cSelric 	free(_hx509_expr_input.error);
219ca1c9b0cSelric 	_hx509_expr_input.error = NULL;
220ca1c9b0cSelric     }
221ca1c9b0cSelric 
222378db5a2Schristos     _hx509_sel_yyparse();
223ca1c9b0cSelric 
224ca1c9b0cSelric     return _hx509_expr_input.expr;
225ca1c9b0cSelric }
226ca1c9b0cSelric 
227ca1c9b0cSelric void
_hx509_sel_yyerror(const char * s)2284f77a458Spettai _hx509_sel_yyerror (const char *s)
229ca1c9b0cSelric {
230ca1c9b0cSelric      if (_hx509_expr_input.error)
231ca1c9b0cSelric          free(_hx509_expr_input.error);
232ca1c9b0cSelric 
233ca1c9b0cSelric      _hx509_expr_input.error = strdup(s);
234ca1c9b0cSelric }
235ca1c9b0cSelric 
236