1226031Sstas/*
2226031Sstas * Copyright (c) 2008 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Redistribution and use in source and binary forms, with or without
7226031Sstas * modification, are permitted provided that the following conditions
8226031Sstas * are met:
9226031Sstas *
10226031Sstas * 1. Redistributions of source code must retain the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer.
12226031Sstas *
13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
14226031Sstas *    notice, this list of conditions and the following disclaimer in the
15226031Sstas *    documentation and/or other materials provided with the distribution.
16226031Sstas *
17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#include "hx_locl.h"
35226031Sstas
36226031Sstasstruct hx_expr *
37226031Sstas_hx509_make_expr(enum hx_expr_op op, void *arg1, void *arg2)
38226031Sstas{
39226031Sstas    struct hx_expr *expr;
40226031Sstas
41226031Sstas    expr = malloc(sizeof(*expr));
42226031Sstas    if (expr == NULL)
43226031Sstas	return NULL;
44226031Sstas    expr->op = op;
45226031Sstas    expr->arg1 = arg1;
46226031Sstas    expr->arg2 = arg2;
47226031Sstas
48226031Sstas    return expr;
49226031Sstas}
50226031Sstas
51226031Sstasstatic const char *
52226031Sstaseval_word(hx509_context context, hx509_env env, struct hx_expr *word)
53226031Sstas{
54226031Sstas    switch (word->op) {
55226031Sstas    case expr_STRING:
56226031Sstas	return word->arg1;
57226031Sstas    case expr_VAR:
58226031Sstas	if (word->arg2 == NULL)
59226031Sstas	    return hx509_env_find(context, env, word->arg1);
60226031Sstas
61226031Sstas	env = hx509_env_find_binding(context, env, word->arg1);
62226031Sstas	if (env == NULL)
63226031Sstas	    return NULL;
64226031Sstas
65226031Sstas	return eval_word(context, env, word->arg2);
66226031Sstas    default:
67226031Sstas	return NULL;
68226031Sstas    }
69226031Sstas}
70226031Sstas
71226031Sstasstatic hx509_env
72226031Sstasfind_variable(hx509_context context, hx509_env env, struct hx_expr *word)
73226031Sstas{
74226031Sstas    assert(word->op == expr_VAR);
75226031Sstas
76226031Sstas    if (word->arg2 == NULL)
77226031Sstas	return hx509_env_find_binding(context, env, word->arg1);
78226031Sstas
79226031Sstas    env = hx509_env_find_binding(context, env, word->arg1);
80226031Sstas    if (env == NULL)
81226031Sstas	return NULL;
82226031Sstas    return find_variable(context, env, word->arg2);
83226031Sstas}
84226031Sstas
85226031Sstasstatic int
86226031Sstaseval_comp(hx509_context context, hx509_env env, struct hx_expr *expr)
87226031Sstas{
88226031Sstas    switch (expr->op) {
89226031Sstas    case comp_NE:
90226031Sstas    case comp_EQ:
91226031Sstas    case comp_TAILEQ: {
92226031Sstas	const char *s1, *s2;
93226031Sstas	int ret;
94226031Sstas
95226031Sstas	s1 = eval_word(context, env, expr->arg1);
96226031Sstas	s2 = eval_word(context, env, expr->arg2);
97226031Sstas
98226031Sstas	if (s1 == NULL || s2 == NULL)
99226031Sstas	    return FALSE;
100226031Sstas
101226031Sstas	if (expr->op == comp_TAILEQ) {
102226031Sstas	    size_t len1 = strlen(s1);
103226031Sstas	    size_t len2 = strlen(s2);
104226031Sstas
105226031Sstas	    if (len1 < len2)
106226031Sstas		return 0;
107226031Sstas	    ret = strcmp(s1 + (len1 - len2), s2) == 0;
108226031Sstas	} else {
109226031Sstas	    ret = strcmp(s1, s2) == 0;
110226031Sstas	    if (expr->op == comp_NE)
111226031Sstas		ret = !ret;
112226031Sstas	}
113226031Sstas	return ret;
114226031Sstas    }
115226031Sstas    case comp_IN: {
116226031Sstas	struct hx_expr *subexpr;
117226031Sstas	const char *w, *s1;
118226031Sstas
119226031Sstas	w = eval_word(context, env, expr->arg1);
120226031Sstas
121226031Sstas	subexpr = expr->arg2;
122226031Sstas
123226031Sstas	if (subexpr->op == expr_WORDS) {
124226031Sstas	    while (subexpr) {
125226031Sstas		s1 = eval_word(context, env, subexpr->arg1);
126226031Sstas		if (strcmp(w, s1) == 0)
127226031Sstas		    return TRUE;
128226031Sstas		subexpr = subexpr->arg2;
129226031Sstas	    }
130226031Sstas	} else if (subexpr->op == expr_VAR) {
131226031Sstas	    hx509_env subenv;
132226031Sstas
133226031Sstas	    subenv = find_variable(context, env, subexpr);
134226031Sstas	    if (subenv == NULL)
135226031Sstas		return FALSE;
136226031Sstas
137226031Sstas	    while (subenv) {
138226031Sstas		if (subenv->type != env_string)
139226031Sstas		    continue;
140226031Sstas		if (strcmp(w, subenv->name) == 0)
141226031Sstas		    return TRUE;
142226031Sstas		if (strcmp(w, subenv->u.string) == 0)
143226031Sstas		    return TRUE;
144226031Sstas		subenv = subenv->next;
145226031Sstas	    }
146226031Sstas
147226031Sstas	} else
148226031Sstas	    _hx509_abort("hx509 eval IN unknown op: %d", (int)subexpr->op);
149226031Sstas
150226031Sstas	return FALSE;
151226031Sstas    }
152226031Sstas    default:
153226031Sstas	_hx509_abort("hx509 eval expr with unknown op: %d", (int)expr->op);
154226031Sstas    }
155226031Sstas    return FALSE;
156226031Sstas}
157226031Sstas
158226031Sstasint
159226031Sstas_hx509_expr_eval(hx509_context context, hx509_env env, struct hx_expr *expr)
160226031Sstas{
161226031Sstas    switch (expr->op) {
162226031Sstas    case op_TRUE:
163226031Sstas	return 1;
164226031Sstas    case op_FALSE:
165226031Sstas	return 0;
166226031Sstas    case op_NOT:
167226031Sstas	return ! _hx509_expr_eval(context, env, expr->arg1);
168226031Sstas    case op_AND:
169226031Sstas	return _hx509_expr_eval(context, env, expr->arg1) &&
170226031Sstas	    _hx509_expr_eval(context, env, expr->arg2);
171226031Sstas    case op_OR:
172226031Sstas	return _hx509_expr_eval(context, env, expr->arg1) ||
173226031Sstas	    _hx509_expr_eval(context, env, expr->arg2);
174226031Sstas    case op_COMP:
175226031Sstas	return eval_comp(context, env, expr->arg1);
176226031Sstas    default:
177226031Sstas	_hx509_abort("hx509 eval expr with unknown op: %d", (int)expr->op);
178226031Sstas	UNREACHABLE(return 0);
179226031Sstas    }
180226031Sstas}
181226031Sstas
182226031Sstasvoid
183226031Sstas_hx509_expr_free(struct hx_expr *expr)
184226031Sstas{
185226031Sstas    switch (expr->op) {
186226031Sstas    case expr_STRING:
187226031Sstas    case expr_NUMBER:
188226031Sstas	free(expr->arg1);
189226031Sstas	break;
190226031Sstas    case expr_WORDS:
191226031Sstas    case expr_FUNCTION:
192226031Sstas    case expr_VAR:
193226031Sstas	free(expr->arg1);
194226031Sstas	if (expr->arg2)
195226031Sstas	    _hx509_expr_free(expr->arg2);
196226031Sstas	break;
197226031Sstas    default:
198226031Sstas	if (expr->arg1)
199226031Sstas	    _hx509_expr_free(expr->arg1);
200226031Sstas	if (expr->arg2)
201226031Sstas	    _hx509_expr_free(expr->arg2);
202226031Sstas	break;
203226031Sstas    }
204226031Sstas    free(expr);
205226031Sstas}
206226031Sstas
207226031Sstasstruct hx_expr *
208226031Sstas_hx509_expr_parse(const char *buf)
209226031Sstas{
210226031Sstas    _hx509_expr_input.buf = buf;
211226031Sstas    _hx509_expr_input.length = strlen(buf);
212226031Sstas    _hx509_expr_input.offset = 0;
213226031Sstas    _hx509_expr_input.expr = NULL;
214226031Sstas
215226031Sstas    if (_hx509_expr_input.error) {
216226031Sstas	free(_hx509_expr_input.error);
217226031Sstas	_hx509_expr_input.error = NULL;
218226031Sstas    }
219226031Sstas
220226031Sstas    yyparse();
221226031Sstas
222226031Sstas    return _hx509_expr_input.expr;
223226031Sstas}
224226031Sstas
225226031Sstasvoid
226226031Sstas_hx509_sel_yyerror (const char *s)
227226031Sstas{
228226031Sstas     if (_hx509_expr_input.error)
229226031Sstas         free(_hx509_expr_input.error);
230226031Sstas
231226031Sstas     _hx509_expr_input.error = strdup(s);
232226031Sstas}
233226031Sstas
234