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, sdb_store_expr_t *iter, int context)
44 {
45 sdb_strbuf_sprintf(errbuf, "Invalid %s iterator: %s %s "
46 "not iterable in %s context", MATCHER_SYM(op),
47 EXPR_TO_STRING(iter), SDB_STORE_TYPE_TO_NAME(iter->data_type),
48 context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
49 } /* iter_error */
51 static void
52 iter_op_error(sdb_strbuf_t *errbuf, int op,
53 int iter_type, int cmp, int value_type)
54 {
55 sdb_strbuf_sprintf(errbuf, "Invalid iterator %s %s %s %s",
56 MATCHER_SYM(op), SDB_TYPE_TO_STRING(iter_type),
57 MATCHER_SYM(cmp), SDB_TYPE_TO_STRING(value_type));
58 if ((iter_type & 0xff) != value_type)
59 sdb_strbuf_append(errbuf, " (type mismatch)");
60 else
61 sdb_strbuf_append(errbuf, " (invalid operator)");
62 } /* iter_op_error */
64 static void
65 cmp_error(sdb_strbuf_t *errbuf, int op, int left, int right)
66 {
67 sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
68 MATCHER_SYM(op), SDB_TYPE_TO_STRING(left),
69 SDB_TYPE_TO_STRING(right));
70 } /* cmp_error */
72 static void
73 op_error(sdb_strbuf_t *errbuf, int op, int left, int right)
74 {
75 sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
76 SDB_DATA_OP_TO_STRING(op), SDB_TYPE_TO_STRING(left),
77 SDB_TYPE_TO_STRING(right));
78 } /* cmp_error */
80 static int
81 analyze_expr(int context, sdb_store_expr_t *e, sdb_strbuf_t *errbuf)
82 {
83 if (! e)
84 return 0;
86 if ((e->type < TYPED_EXPR) || (SDB_DATA_CONCAT < e->type)) {
87 sdb_strbuf_sprintf(errbuf, "Invalid expression of type %d", e->type);
88 return -1;
89 }
91 switch (e->type) {
92 case TYPED_EXPR:
93 if (analyze_expr((int)e->data.data.integer, e->left, errbuf))
94 return -1;
95 if (context == (int)e->data.data.integer)
96 return 0;
97 if ((e->data.data.integer == SDB_HOST) &&
98 ((context == SDB_SERVICE) || (context == SDB_METRIC)))
99 return 0;
100 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s "
101 "in %s context",
102 SDB_STORE_TYPE_TO_NAME(e->data.data.integer),
103 EXPR_TO_STRING(e->left),
104 context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
105 return -1;
107 case ATTR_VALUE:
108 case 0:
109 break;
111 case FIELD_VALUE:
112 if ((e->data.data.integer == SDB_FIELD_VALUE)
113 && (context != SDB_ATTRIBUTE)) {
114 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.value "
115 "(only attributes have a value)",
116 SDB_STORE_TYPE_TO_NAME(context));
117 return -1;
118 }
119 break;
121 default:
122 if (analyze_expr(context, e->left, errbuf))
123 return -1;
124 if (analyze_expr(context, e->right, errbuf))
125 return -1;
127 if ((e->left->data_type > 0) && (e->right->data_type > 0)) {
128 if (sdb_data_expr_type(e->type, e->left->data_type,
129 e->right->data_type) < 0) {
130 op_error(errbuf, e->type, e->left->data_type,
131 e->right->data_type);
132 return -1;
133 }
134 }
135 break;
136 }
137 return 0;
138 } /* analyze_expr */
140 static int
141 analyze_matcher(int context, int parent_type,
142 sdb_store_matcher_t *m, sdb_strbuf_t *errbuf)
143 {
144 if (! m)
145 return 0;
147 switch (m->type) {
148 case MATCHER_OR:
149 case MATCHER_AND:
150 assert(OP_M(m)->left && OP_M(m)->right);
151 if (analyze_matcher(context, m->type, OP_M(m)->left, errbuf))
152 return -1;
153 if (analyze_matcher(context, m->type, OP_M(m)->right, errbuf))
154 return -1;
155 break;
157 case MATCHER_NOT:
158 assert(UOP_M(m)->op);
159 if (analyze_matcher(context, m->type, UOP_M(m)->op, errbuf))
160 return -1;
161 break;
163 case MATCHER_ANY:
164 case MATCHER_ALL:
165 {
166 int child_context = -1;
167 int left_type = -1;
168 int type = -1;
170 assert(ITER_M(m)->m);
172 if ((ITER_M(m)->iter->type == TYPED_EXPR)
173 || (ITER_M(m)->iter->type == FIELD_VALUE))
174 type = (int)ITER_M(m)->iter->data.data.integer;
176 if (context == -1) { /* inside a filter */
177 /* attributes are always iterable */
178 if ((ITER_M(m)->iter->type == TYPED_EXPR)
179 && (type != SDB_ATTRIBUTE)) {
180 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
181 return -1;
182 }
183 /* backends are always iterable */
184 if ((ITER_M(m)->iter->type == FIELD_VALUE)
185 && (! (type != SDB_FIELD_BACKEND))) {
186 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
187 return -1;
188 }
189 }
190 else if (! sdb_store_expr_iterable(ITER_M(m)->iter, context)) {
191 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
192 return -1;
193 }
195 if (ITER_M(m)->iter->type == TYPED_EXPR) {
196 child_context = type;
197 left_type = ITER_M(m)->iter->data_type;
198 }
199 else if (ITER_M(m)->iter->type == FIELD_VALUE) {
200 child_context = context;
201 /* element type of the field */
202 left_type = ITER_M(m)->iter->data_type & 0xff;
203 }
204 else if (! ITER_M(m)->iter->type) {
205 child_context = context;
206 /* elements of the array constant */
207 left_type = ITER_M(m)->iter->data.type & 0xff;
208 }
209 else {
210 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
211 return -1;
212 }
214 /* any ary operator will do but these are the once
215 * we currently support */
216 if ((ITER_M(m)->m->type != MATCHER_LT)
217 && (ITER_M(m)->m->type != MATCHER_LE)
218 && (ITER_M(m)->m->type != MATCHER_EQ)
219 && (ITER_M(m)->m->type != MATCHER_NE)
220 && (ITER_M(m)->m->type != MATCHER_GE)
221 && (ITER_M(m)->m->type != MATCHER_GT)
222 && (ITER_M(m)->m->type != MATCHER_REGEX)
223 && (ITER_M(m)->m->type != MATCHER_NREGEX)) {
224 iter_op_error(errbuf, m->type,
225 left_type, ITER_M(m)->m->type,
226 CMP_M(ITER_M(m)->m)->right->data_type);
227 return -1;
228 }
229 if ((left_type >= 0)
230 && (CMP_M(ITER_M(m)->m)->right->data_type >= 0)) {
231 if (left_type != CMP_M(ITER_M(m)->m)->right->data_type) {
232 iter_op_error(errbuf, m->type,
233 left_type, ITER_M(m)->m->type,
234 CMP_M(ITER_M(m)->m)->right->data_type);
235 return -1;
236 }
237 }
238 if (child_context <= 0) {
239 sdb_strbuf_sprintf(errbuf, "Unable to determine the context "
240 "(object type) of iterator %s %s %s %s",
241 MATCHER_SYM(m->type), SDB_TYPE_TO_STRING(left_type),
242 MATCHER_SYM(ITER_M(m)->m->type),
243 SDB_TYPE_TO_STRING(CMP_M(ITER_M(m)->m)->right->data_type));
244 }
245 if (analyze_matcher(child_context, m->type, ITER_M(m)->m, errbuf))
246 return -1;
247 break;
248 }
250 case MATCHER_LT:
251 case MATCHER_LE:
252 case MATCHER_EQ:
253 case MATCHER_NE:
254 case MATCHER_GE:
255 case MATCHER_GT:
256 {
257 int left_type = -1;
259 assert(CMP_M(m)->right);
260 if ((parent_type == MATCHER_ALL)
261 || (parent_type == MATCHER_ANY)) {
262 assert(! CMP_M(m)->left);
263 }
264 else {
265 assert(CMP_M(m)->left);
266 left_type = CMP_M(m)->left->data_type;
267 }
269 if (analyze_expr(context, CMP_M(m)->left, errbuf))
270 return -1;
271 if (analyze_expr(context, CMP_M(m)->right, errbuf))
272 return -1;
274 if ((left_type > 0) && (CMP_M(m)->right->data_type > 0)) {
275 if (left_type == CMP_M(m)->right->data_type)
276 return 0;
277 cmp_error(errbuf, m->type, left_type,
278 CMP_M(m)->right->data_type);
279 return -1;
280 }
281 if ((left_type > 0) && (left_type & SDB_TYPE_ARRAY)) {
282 cmp_error(errbuf, m->type, left_type,
283 CMP_M(m)->right->data_type);
284 return -1;
285 }
286 if ((CMP_M(m)->right->data_type > 0)
287 && (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY)) {
288 cmp_error(errbuf, m->type, left_type,
289 CMP_M(m)->right->data_type);
290 return -1;
291 }
292 break;
293 }
295 case MATCHER_IN:
296 case MATCHER_NIN:
297 if (analyze_expr(context, CMP_M(m)->left, errbuf))
298 return -1;
299 if (analyze_expr(context, CMP_M(m)->right, errbuf))
300 return -1;
302 /* the left operand may be a scalar or an array but the element
303 * type has to match */
304 if ((CMP_M(m)->right->data_type > 0)
305 && (! (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY))) {
306 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
307 CMP_M(m)->right->data_type);
308 return -1;
309 }
310 if ((CMP_M(m)->left->data_type > 0)
311 && (CMP_M(m)->right->data_type > 0)) {
312 if ((CMP_M(m)->left->data_type & 0xff)
313 != (CMP_M(m)->right->data_type & 0xff)) {
314 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
315 CMP_M(m)->right->data_type);
316 return -1;
317 }
318 }
319 break;
321 case MATCHER_REGEX:
322 case MATCHER_NREGEX:
323 if (analyze_expr(context, CMP_M(m)->left, errbuf))
324 return -1;
325 if (analyze_expr(context, CMP_M(m)->right, errbuf))
326 return -1;
328 /* all types are supported for the left operand */
329 if ((CMP_M(m)->right->data_type > 0)
330 && (CMP_M(m)->right->data_type != SDB_TYPE_REGEX)
331 && (CMP_M(m)->right->data_type != SDB_TYPE_STRING)) {
332 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
333 CMP_M(m)->right->data_type);
334 return -1;
335 }
336 break;
338 case MATCHER_ISNULL:
339 case MATCHER_ISNNULL:
340 if (analyze_expr(context, ISNULL_M(m)->expr, errbuf))
341 return -1;
342 break;
344 default:
345 sdb_strbuf_sprintf(errbuf, "Unknown matcher type %d", m->type);
346 return -1;
347 }
348 return 0;
349 } /* analyze_matcher */
351 /*
352 * public API
353 */
355 int
356 sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
357 {
358 sdb_store_matcher_t *m = NULL, *filter = NULL;
359 int context = -1;
360 int status = 0;
362 if (! node)
363 return -1;
365 /* For now, this function checks basic matcher attributes only;
366 * later, this may be turned into one of multiple AST visitors. */
367 if (node->cmd == SDB_CONNECTION_FETCH) {
368 conn_fetch_t *fetch = CONN_FETCH(node);
369 if ((fetch->type == SDB_HOST) && fetch->name) {
370 sdb_strbuf_sprintf(errbuf, "Unexpected STRING '%s'", fetch->name);
371 return -1;
372 }
373 if ((fetch->type != SDB_HOST) && (! fetch->name)) {
374 sdb_strbuf_sprintf(errbuf, "Missing %s name",
375 SDB_STORE_TYPE_TO_NAME(fetch->type));
376 return -1;
377 }
378 if (fetch->filter)
379 filter = fetch->filter->matcher;
380 context = fetch->type;
381 }
382 else if (node->cmd == SDB_CONNECTION_LIST) {
383 if (CONN_LIST(node)->filter)
384 filter = CONN_LIST(node)->filter->matcher;
385 context = CONN_LIST(node)->type;
386 }
387 else if (node->cmd == SDB_CONNECTION_LOOKUP) {
388 if (CONN_LOOKUP(node)->matcher)
389 m = CONN_LOOKUP(node)->matcher->matcher;
390 if (CONN_LOOKUP(node)->filter)
391 filter = CONN_LOOKUP(node)->filter->matcher;
392 context = CONN_LOOKUP(node)->type;
393 }
394 else if ((node->cmd == SDB_CONNECTION_STORE_HOST)
395 || (node->cmd == SDB_CONNECTION_STORE_SERVICE)
396 || (node->cmd == SDB_CONNECTION_STORE_METRIC)
397 || (node->cmd == SDB_CONNECTION_STORE_ATTRIBUTE)) {
398 return 0;
399 }
400 else if (node->cmd == SDB_CONNECTION_TIMESERIES) {
401 return 0;
402 }
403 else {
404 sdb_strbuf_sprintf(errbuf,
405 "Don't know how to analyze %s command (id=%#x)",
406 SDB_CONN_MSGTYPE_TO_STRING(node->cmd), node->cmd);
407 return -1;
408 }
410 if (context <= 0) {
411 sdb_strbuf_sprintf(errbuf, "Unable to determine the context "
412 "(object type) for %s command (id=%#x)",
413 SDB_CONN_MSGTYPE_TO_STRING(node->cmd), node->cmd);
414 return -1;
415 }
416 if (analyze_matcher(context, -1, m, errbuf))
417 status = -1;
418 if (analyze_matcher(-1, -1, filter, errbuf))
419 status = -1;
420 return status;
421 } /* sdb_fe_analyze */
423 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */