1 /*
2 * SysDB - src/frontend/analyzer.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
30 #include "core/store-private.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
36 #include <assert.h>
38 /*
39 * private helper functions
40 */
42 static void
43 iter_error(sdb_strbuf_t *errbuf, int op, int oper, int context)
44 {
45 sdb_strbuf_sprintf(errbuf, "Cannot use %s %s in %s context",
46 MATCHER_SYM(op), SDB_STORE_TYPE_TO_NAME(oper),
47 SDB_STORE_TYPE_TO_NAME(context));
48 } /* iter_error */
50 static void
51 iter_array_error(sdb_strbuf_t *errbuf, int op,
52 int array_type, int cmp, int value_type)
53 {
54 sdb_strbuf_sprintf(errbuf, "Invalid array iterator %s %s %s %s",
55 MATCHER_SYM(op), SDB_TYPE_TO_STRING(array_type),
56 MATCHER_SYM(cmp), SDB_TYPE_TO_STRING(value_type));
57 if ((array_type & 0xff) != value_type)
58 sdb_strbuf_append(errbuf, " (type mismatch)");
59 else
60 sdb_strbuf_append(errbuf, " (invalid operator)");
61 } /* iter_array_error */
63 static void
64 cmp_error(sdb_strbuf_t *errbuf, int op, int left, int right)
65 {
66 sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
67 MATCHER_SYM(op), SDB_TYPE_TO_STRING(left),
68 SDB_TYPE_TO_STRING(right));
69 } /* cmp_error */
71 static int
72 analyze_expr(int context, sdb_store_expr_t *e, sdb_strbuf_t *errbuf)
73 {
74 if (! e)
75 return 0;
77 if ((e->type < TYPED_EXPR) || (SDB_DATA_CONCAT < e->type)) {
78 sdb_strbuf_sprintf(errbuf, "Invalid expression of type %d", e->type);
79 return -1;
80 }
82 switch (e->type) {
83 case TYPED_EXPR:
84 if (analyze_expr((int)e->data.data.integer, e->left, errbuf))
85 return -1;
86 if (context == (int)e->data.data.integer)
87 return 0;
88 if ((e->data.data.integer == SDB_HOST) &&
89 ((context == SDB_SERVICE) || (context == SDB_METRIC)))
90 return 0;
91 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s "
92 "in %s context",
93 SDB_STORE_TYPE_TO_NAME(e->data.data.integer),
94 EXPR_TO_STRING(e->left), SDB_STORE_TYPE_TO_NAME(context));
95 return -1;
97 case ATTR_VALUE:
98 case FIELD_VALUE:
99 case 0:
100 break;
102 default:
103 if (analyze_expr(context, e->left, errbuf))
104 return -1;
105 if (analyze_expr(context, e->right, errbuf))
106 return -1;
107 break;
108 }
109 return 0;
110 } /* analyze_expr */
112 static int
113 analyze_matcher(int context, sdb_store_matcher_t *m, sdb_strbuf_t *errbuf)
114 {
115 if (! m)
116 return 0;
118 switch (m->type) {
119 case MATCHER_OR:
120 case MATCHER_AND:
121 assert(OP_M(m)->left && OP_M(m)->right);
122 if (analyze_matcher(context, OP_M(m)->left, errbuf))
123 return -1;
124 if (analyze_matcher(context, OP_M(m)->right, errbuf))
125 return -1;
126 break;
128 case MATCHER_NOT:
129 assert(UOP_M(m)->op);
130 if (analyze_matcher(context, UOP_M(m)->op, errbuf))
131 return -1;
132 break;
134 case MATCHER_ANY:
135 case MATCHER_ALL:
136 assert(ITER_M(m)->m);
137 if ((context != SDB_HOST)
138 && (context != SDB_SERVICE)
139 && (context != SDB_METRIC)) {
140 iter_error(errbuf, m->type, ITER_M(m)->type, context);
141 return -1;
142 }
143 if (ITER_M(m)->type == context) {
144 iter_error(errbuf, m->type, ITER_M(m)->type, context);
145 return -1;
146 }
147 if ((ITER_M(m)->type != SDB_SERVICE)
148 && (ITER_M(m)->type != SDB_METRIC)
149 && (ITER_M(m)->type != SDB_ATTRIBUTE)
150 && (ITER_M(m)->type != SDB_FIELD_BACKEND)) {
151 iter_error(errbuf, m->type, ITER_M(m)->type, context);
152 return -1;
153 }
154 if ((context == SDB_SERVICE)
155 && (ITER_M(m)->type == SDB_METRIC)) {
156 iter_error(errbuf, m->type, ITER_M(m)->type, context);
157 return -1;
158 }
159 else if ((context == SDB_METRIC)
160 && (ITER_M(m)->type == SDB_SERVICE)) {
161 iter_error(errbuf, m->type, ITER_M(m)->type, context);
162 return -1;
163 }
164 if (ITER_M(m)->type == SDB_FIELD_BACKEND) {
165 /* array iterators only support simple comparison atm */
166 if ((ITER_M(m)->m->type != MATCHER_LT)
167 && (ITER_M(m)->m->type != MATCHER_LE)
168 && (ITER_M(m)->m->type != MATCHER_EQ)
169 && (ITER_M(m)->m->type != MATCHER_NE)
170 && (ITER_M(m)->m->type != MATCHER_GE)
171 && (ITER_M(m)->m->type != MATCHER_GT)
172 && (ITER_M(m)->m->type != MATCHER_REGEX)
173 && (ITER_M(m)->m->type != MATCHER_NREGEX)) {
174 iter_array_error(errbuf, m->type,
175 CMP_M(ITER_M(m)->m)->left->data_type,
176 ITER_M(m)->m->type,
177 CMP_M(ITER_M(m)->m)->right->data_type);
178 return -1;
179 }
180 if (CMP_M(ITER_M(m)->m)->right->data_type < 0)
181 return 0; /* skip further type checks */
182 if (CMP_M(ITER_M(m)->m)->right->data_type & SDB_TYPE_ARRAY) {
183 iter_array_error(errbuf, m->type,
184 CMP_M(ITER_M(m)->m)->left->data_type,
185 ITER_M(m)->m->type,
186 CMP_M(ITER_M(m)->m)->right->data_type);
187 return -1;
188 }
189 if ((CMP_M(ITER_M(m)->m)->left->data_type & 0xff)
190 != CMP_M(ITER_M(m)->m)->right->data_type) {
191 iter_array_error(errbuf, m->type,
192 CMP_M(ITER_M(m)->m)->left->data_type,
193 ITER_M(m)->m->type,
194 CMP_M(ITER_M(m)->m)->right->data_type);
195 return -1;
196 }
197 }
198 else if (analyze_matcher(ITER_M(m)->type, ITER_M(m)->m, errbuf))
199 return -1;
200 break;
202 case MATCHER_LT:
203 case MATCHER_LE:
204 case MATCHER_EQ:
205 case MATCHER_NE:
206 case MATCHER_GE:
207 case MATCHER_GT:
208 assert(CMP_M(m)->left && CMP_M(m)->right);
209 if (analyze_expr(context, CMP_M(m)->left, errbuf))
210 return -1;
211 if (analyze_expr(context, CMP_M(m)->right, errbuf))
212 return -1;
214 if ((CMP_M(m)->left->data_type > 0)
215 && (CMP_M(m)->right->data_type > 0)) {
216 if (CMP_M(m)->left->data_type == CMP_M(m)->right->data_type)
217 return 0;
218 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
219 CMP_M(m)->right->data_type);
220 return -1;
221 }
222 if ((CMP_M(m)->left->data_type > 0)
223 && (CMP_M(m)->left->data_type & SDB_TYPE_ARRAY)) {
224 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
225 CMP_M(m)->right->data_type);
226 return -1;
227 }
228 if ((CMP_M(m)->right->data_type > 0)
229 && (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY)) {
230 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
231 CMP_M(m)->right->data_type);
232 return -1;
233 }
234 break;
236 case MATCHER_IN:
237 if (analyze_expr(context, CMP_M(m)->left, errbuf))
238 return -1;
239 if (analyze_expr(context, CMP_M(m)->right, errbuf))
240 return -1;
242 if ((CMP_M(m)->left->data_type > 0)
243 && (CMP_M(m)->left->data_type & SDB_TYPE_ARRAY)) {
244 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
245 CMP_M(m)->right->data_type);
246 return -1;
247 }
248 if ((CMP_M(m)->right->data_type > 0)
249 && (! (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY))) {
250 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
251 CMP_M(m)->right->data_type);
252 return -1;
253 }
254 break;
256 case MATCHER_REGEX:
257 case MATCHER_NREGEX:
258 if (analyze_expr(context, CMP_M(m)->left, errbuf))
259 return -1;
260 if (analyze_expr(context, CMP_M(m)->right, errbuf))
261 return -1;
263 /* all types are supported for the left operand */
264 if ((CMP_M(m)->right->data_type > 0)
265 && (CMP_M(m)->right->data_type != SDB_TYPE_REGEX)
266 && (CMP_M(m)->right->data_type != SDB_TYPE_STRING)) {
267 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
268 CMP_M(m)->right->data_type);
269 return -1;
270 }
271 break;
273 case MATCHER_ISNULL:
274 case MATCHER_ISNNULL:
275 if (analyze_expr(context, ISNULL_M(m)->expr, errbuf))
276 return -1;
277 break;
279 default:
280 sdb_strbuf_sprintf(errbuf, "Unknown matcher type %d", m->type);
281 return -1;
282 }
283 return 0;
284 } /* analyze_matcher */
286 /*
287 * public API
288 */
290 int
291 sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
292 {
293 sdb_store_matcher_t *m = NULL, *filter = NULL;
294 int context = -1;
295 int status = 0;
297 if (! node)
298 return -1;
300 /* For now, this function checks basic matcher attributes only;
301 * later, this may be turned into one of multiple AST visitors. */
302 if (node->cmd == SDB_CONNECTION_FETCH) {
303 conn_fetch_t *fetch = CONN_FETCH(node);
304 if ((fetch->type == SDB_HOST) && fetch->name) {
305 sdb_strbuf_sprintf(errbuf, "Unexpected STRING '%s'", fetch->name);
306 return -1;
307 }
308 if ((fetch->type != SDB_HOST) && (! fetch->name)) {
309 sdb_strbuf_sprintf(errbuf, "Missing %s name",
310 SDB_STORE_TYPE_TO_NAME(fetch->type));
311 return -1;
312 }
313 if (fetch->filter)
314 filter = fetch->filter->matcher;
315 context = fetch->type;
316 }
317 else if (node->cmd == SDB_CONNECTION_LIST) {
318 if (CONN_LIST(node)->filter)
319 filter = CONN_LIST(node)->filter->matcher;
320 context = CONN_LIST(node)->type;
321 }
322 else if (node->cmd == SDB_CONNECTION_LOOKUP) {
323 if (CONN_LOOKUP(node)->matcher)
324 m = CONN_LOOKUP(node)->matcher->matcher;
325 if (CONN_LOOKUP(node)->filter)
326 filter = CONN_LOOKUP(node)->filter->matcher;
327 context = CONN_LOOKUP(node)->type;
328 }
329 else if ((node->cmd == SDB_CONNECTION_STORE_HOST)
330 || (node->cmd == SDB_CONNECTION_STORE_SERVICE)
331 || (node->cmd == SDB_CONNECTION_STORE_METRIC)
332 || (node->cmd == SDB_CONNECTION_STORE_ATTRIBUTE)) {
333 return 0;
334 }
335 else if (node->cmd == SDB_CONNECTION_TIMESERIES) {
336 return 0;
337 }
338 else {
339 sdb_strbuf_sprintf(errbuf,
340 "Don't know how to analyze command %#x", node->cmd);
341 return -1;
342 }
344 if (analyze_matcher(context, m, errbuf))
345 status = -1;
346 if (analyze_matcher(-1, filter, errbuf))
347 status = -1;
348 return status;
349 } /* sdb_fe_analyze */
351 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */