1 /*
2 * SysDB - src/core/store_lookup.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 /*
29 * This module implements operators which may be used to select contents of
30 * the store by matching various attributes of the stored objects. For now, a
31 * simple full table scan is supported only.
32 */
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif /* HAVE_CONFIG_H */
38 #include "sysdb.h"
39 #include "core/store-private.h"
40 #include "core/object.h"
42 #include <assert.h>
44 #include <sys/types.h>
45 #include <regex.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <limits.h>
52 /*
53 * private data types
54 */
56 typedef struct {
57 sdb_store_matcher_t *m;
58 sdb_store_matcher_t *filter;
59 sdb_store_lookup_cb cb;
60 void *user_data;
61 } scan_iter_data_t;
63 /*
64 * private helper functions
65 */
67 static int
68 scan_iter(sdb_store_obj_t *obj, void *user_data)
69 {
70 scan_iter_data_t *d = user_data;
72 if (sdb_store_matcher_matches(d->m, obj, d->filter))
73 return d->cb(obj, d->user_data);
74 return 0;
75 } /* scan_iter */
77 static sdb_attribute_t *
78 attr_get(sdb_host_t *host, const char *name, sdb_store_matcher_t *filter)
79 {
80 sdb_avltree_iter_t *iter = NULL;
81 sdb_attribute_t *attr = NULL;
83 iter = sdb_avltree_get_iter(host->attributes);
84 while (sdb_avltree_iter_has_next(iter)) {
85 sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter));
87 if (strcasecmp(name, SDB_OBJ(a)->name))
88 continue;
90 assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
91 attr = a;
92 break;
93 }
94 sdb_avltree_iter_destroy(iter);
96 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(attr),
97 NULL)))
98 return NULL;
99 return attr;
100 } /* attr_get */
102 /*
103 * conditional implementations
104 */
106 static int
107 attr_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
108 sdb_store_matcher_t *filter)
109 {
110 sdb_attribute_t *attr;
111 sdb_data_t value = SDB_DATA_INIT;
112 int status;
114 if (obj->type != SDB_HOST)
115 return INT_MAX;
117 if (sdb_store_expr_eval(ATTR_C(cond)->expr, obj, &value))
118 return INT_MAX;
120 attr = attr_get(HOST(obj), ATTR_C(cond)->name, filter);
121 if (! attr)
122 status = INT_MAX;
123 else if (attr->value.type != value.type)
124 status = INT_MAX;
125 else
126 status = sdb_data_cmp(&attr->value, &value);
127 sdb_data_free_datum(&value);
128 return status;
129 } /* attr_cmp */
131 static int
132 obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond,
133 sdb_store_matcher_t __attribute__((unused)) *filter)
134 {
135 sdb_data_t obj_value = SDB_DATA_INIT;
136 sdb_data_t value = SDB_DATA_INIT;
137 int status;
139 if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value))
140 return INT_MAX;
141 if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value))
142 return INT_MAX;
144 if (obj_value.type != value.type) {
145 sdb_data_free_datum(&value);
146 return INT_MAX;
147 }
148 else if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) {
149 /* this implementation is not actually a conditional but rather checks
150 * for equality (or rather, existence) only */
151 size_t i;
152 status = INT_MAX;
153 for (i = 0; i < obj->backends_num; ++i) {
154 if (! strcasecmp(obj->backends[i], value.data.string)) {
155 status = 0;
156 break;
157 }
158 }
159 }
160 else {
161 status = sdb_data_cmp(&obj_value, &value);
162 }
163 sdb_data_free_datum(&value);
164 return status;
165 } /* obj_cmp */
167 /*
168 * matcher implementations
169 */
171 static int
172 match_string(string_matcher_t *m, const char *name)
173 {
174 if ((! m->name) && (! m->name_re))
175 return 1;
177 if (! name)
178 name = "";
180 if (m->name && strcasecmp(m->name, name))
181 return 0;
182 if (m->name_re && regexec(m->name_re, name,
183 /* matches */ 0, NULL, /* flags = */ 0))
184 return 0;
185 return 1;
186 } /* match_string */
188 static int
189 match_logical(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
190 sdb_store_matcher_t *filter)
191 {
192 int status;
194 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
195 assert(OP_M(m)->left && OP_M(m)->right);
197 status = sdb_store_matcher_matches(OP_M(m)->left, obj, filter);
199 /* lazy evaluation */
200 if ((! status) && (m->type == MATCHER_AND))
201 return status;
202 else if (status && (m->type == MATCHER_OR))
203 return status;
205 return sdb_store_matcher_matches(OP_M(m)->right, obj, filter);
206 } /* match_logical */
208 static int
209 match_unary(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
210 sdb_store_matcher_t *filter)
211 {
212 assert(m->type == MATCHER_NOT);
213 assert(UOP_M(m)->op);
215 return !sdb_store_matcher_matches(UOP_M(m)->op, obj, filter);
216 } /* match_unary */
218 static int
219 match_name(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
220 sdb_store_matcher_t *filter)
221 {
222 sdb_avltree_iter_t *iter = NULL;
223 int status = 0;
225 assert(m->type == MATCHER_NAME);
227 if (obj->type == NAME_M(m)->obj_type)
228 return match_string(&NAME_M(m)->name, SDB_OBJ(obj)->name);
229 else if (obj->type != SDB_HOST)
230 return 0;
232 switch (NAME_M(m)->obj_type) {
233 case SDB_SERVICE:
234 iter = sdb_avltree_get_iter(HOST(obj)->services);
235 break;
236 case SDB_ATTRIBUTE:
237 iter = sdb_avltree_get_iter(HOST(obj)->attributes);
238 break;
239 }
241 while (sdb_avltree_iter_has_next(iter)) {
242 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
243 if (filter && (! sdb_store_matcher_matches(filter, STORE_OBJ(child),
244 NULL)))
245 continue;
246 if (match_string(&NAME_M(m)->name, child->name)) {
247 status = 1;
248 break;
249 }
250 }
251 sdb_avltree_iter_destroy(iter);
252 return status;
253 } /* match_name */
255 static int
256 match_attr(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
257 sdb_store_matcher_t *filter)
258 {
259 sdb_attribute_t *attr;
261 assert(m->type == MATCHER_ATTR);
262 assert(ATTR_M(m)->name);
264 if (obj->type != SDB_HOST)
265 return 0;
267 attr = attr_get(HOST(obj), ATTR_M(m)->name, filter);
268 if (attr) {
269 char buf[sdb_data_strlen(&attr->value) + 1];
270 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
271 return 0;
272 if (match_string(&ATTR_M(m)->value, buf))
273 return 1;
274 }
275 return 0;
276 } /* match_attr */
278 static int
279 match_lt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
280 sdb_store_matcher_t *filter)
281 {
282 int status;
283 assert(m->type == MATCHER_LT);
284 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
285 return (status != INT_MAX) && (status < 0);
286 } /* match_lt */
288 static int
289 match_le(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
290 sdb_store_matcher_t *filter)
291 {
292 int status;
293 assert(m->type == MATCHER_LE);
294 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
295 return (status != INT_MAX) && (status <= 0);
296 } /* match_le */
298 static int
299 match_eq(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
300 sdb_store_matcher_t *filter)
301 {
302 int status;
303 assert(m->type == MATCHER_EQ);
304 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
305 return (status != INT_MAX) && (! status);
306 } /* match_eq */
308 static int
309 match_ge(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
310 sdb_store_matcher_t *filter)
311 {
312 int status;
313 assert(m->type == MATCHER_GE);
314 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
315 return (status != INT_MAX) && (status >= 0);
316 } /* match_ge */
318 static int
319 match_gt(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
320 sdb_store_matcher_t *filter)
321 {
322 int status;
323 assert(m->type == MATCHER_GT);
324 status = COND_M(m)->cond->cmp(obj, COND_M(m)->cond, filter);
325 return (status != INT_MAX) && (status > 0);
326 } /* match_gt */
328 static int
329 match_isnull(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
330 sdb_store_matcher_t *filter)
331 {
332 assert(m->type == MATCHER_ISNULL);
333 if (obj->type != SDB_HOST)
334 return 0;
335 return attr_get(HOST(obj), ISNULL_M(m)->attr_name, filter) == NULL;
336 } /* match_isnull */
338 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_obj_t *,
339 sdb_store_matcher_t *);
341 /* this array needs to be indexable by the matcher types;
342 * -> update the enum in store-private.h when updating this */
343 static matcher_cb
344 matchers[] = {
345 match_logical,
346 match_logical,
347 match_unary,
348 match_name,
349 match_attr,
350 match_lt,
351 match_le,
352 match_eq,
353 match_ge,
354 match_gt,
355 match_isnull,
356 };
358 /*
359 * private conditional types
360 */
362 static int
363 attr_cond_init(sdb_object_t *obj, va_list ap)
364 {
365 const char *name = va_arg(ap, const char *);
366 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
368 if (! name)
369 return -1;
371 SDB_STORE_COND(obj)->cmp = attr_cmp;
373 ATTR_C(obj)->name = strdup(name);
374 if (! ATTR_C(obj)->name)
375 return -1;
376 ATTR_C(obj)->expr = expr;
377 sdb_object_ref(SDB_OBJ(expr));
378 return 0;
379 } /* attr_cond_init */
381 static void
382 attr_cond_destroy(sdb_object_t *obj)
383 {
384 if (ATTR_C(obj)->name)
385 free(ATTR_C(obj)->name);
386 sdb_object_deref(SDB_OBJ(ATTR_C(obj)->expr));
387 } /* attr_cond_destroy */
389 static sdb_type_t attr_cond_type = {
390 /* size = */ sizeof(attr_cond_t),
391 /* init = */ attr_cond_init,
392 /* destroy = */ attr_cond_destroy,
393 };
395 static int
396 obj_cond_init(sdb_object_t *obj, va_list ap)
397 {
398 int field = va_arg(ap, int);
399 sdb_store_expr_t *expr = va_arg(ap, sdb_store_expr_t *);
401 SDB_STORE_COND(obj)->cmp = obj_cmp;
403 OBJ_C(obj)->field = field;
404 OBJ_C(obj)->expr = expr;
405 sdb_object_ref(SDB_OBJ(expr));
406 return 0;
407 } /* obj_cond_init */
409 static void
410 obj_cond_destroy(sdb_object_t *obj)
411 {
412 sdb_object_deref(SDB_OBJ(OBJ_C(obj)->expr));
413 } /* obj_cond_destroy */
415 static sdb_type_t obj_cond_type = {
416 /* size = */ sizeof(obj_cond_t),
417 /* init = */ obj_cond_init,
418 /* destroy = */ obj_cond_destroy,
419 };
421 /*
422 * private matcher types
423 */
425 /* initializes a string matcher consuming two elements from ap */
426 static int
427 string_matcher_init(string_matcher_t *m, va_list ap)
428 {
429 const char *name = va_arg(ap, const char *);
430 const char *name_re = va_arg(ap, const char *);
432 if (name) {
433 m->name = strdup(name);
434 if (! m->name)
435 return -1;
436 }
437 if (name_re) {
438 m->name_re = malloc(sizeof(*m->name_re));
439 if (! m->name_re)
440 return -1;
441 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
442 return -1;
443 }
444 return 0;
445 } /* string_matcher_init */
447 static void
448 string_matcher_destroy(string_matcher_t *m)
449 {
450 if (m->name)
451 free(m->name);
452 if (m->name_re) {
453 regfree(m->name_re);
454 free(m->name_re);
455 }
456 } /* string_matcher_destroy */
458 static char *
459 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
460 {
461 snprintf(buf, buflen, "{ %s%s%s, %p }",
462 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
463 m->name_re);
464 return buf;
465 } /* string_tostring */
467 /* initializes a name matcher */
468 static int
469 name_matcher_init(sdb_object_t *obj, va_list ap)
470 {
471 name_matcher_t *m = NAME_M(obj);
472 M(obj)->type = MATCHER_NAME;
473 return string_matcher_init(&m->name, ap);
474 } /* name_matcher_init */
476 static void
477 name_matcher_destroy(sdb_object_t *obj)
478 {
479 name_matcher_t *m = NAME_M(obj);
480 string_matcher_destroy(&m->name);
481 } /* name_matcher_destroy */
483 static char *
484 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
485 {
486 char name[buflen + 1];
487 assert(m->type == MATCHER_NAME);
488 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
489 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
490 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
491 return buf;
492 } /* name_tostring */
494 static int
495 attr_matcher_init(sdb_object_t *obj, va_list ap)
496 {
497 attr_matcher_t *attr = ATTR_M(obj);
498 const char *name = va_arg(ap, const char *);
500 M(obj)->type = MATCHER_ATTR;
501 if (name) {
502 attr->name = strdup(name);
503 if (! attr->name)
504 return -1;
505 }
506 return string_matcher_init(&attr->value, ap);
507 } /* attr_matcher_init */
509 static void
510 attr_matcher_destroy(sdb_object_t *obj)
511 {
512 attr_matcher_t *attr = ATTR_M(obj);
513 if (attr->name)
514 free(attr->name);
515 attr->name = NULL;
516 string_matcher_destroy(&attr->value);
517 } /* attr_matcher_destroy */
519 static char *
520 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
521 {
522 char value[buflen + 1];
524 if (! m) {
525 snprintf(buf, buflen, "ATTR{}");
526 return buf;
527 }
529 assert(m->type == MATCHER_ATTR);
530 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
531 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
532 return buf;
533 } /* attr_tostring */
535 static int
536 cond_matcher_init(sdb_object_t *obj, va_list ap)
537 {
538 int type = va_arg(ap, int);
539 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
541 if (! cond)
542 return -1;
544 sdb_object_ref(SDB_OBJ(cond));
546 M(obj)->type = type;
547 COND_M(obj)->cond = cond;
548 return 0;
549 } /* cond_matcher_init */
551 static void
552 cond_matcher_destroy(sdb_object_t *obj)
553 {
554 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
555 } /* cond_matcher_destroy */
557 static char *
558 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
559 {
560 const char *type, *id;
561 sdb_data_t value = SDB_DATA_INIT;
562 char value_str[buflen];
563 sdb_store_expr_t *expr;
565 if (COND_M(m)->cond->cmp == attr_cmp) {
566 type = "ATTR";
567 id = ATTR_C(COND_M(m)->cond)->name;
568 expr = ATTR_C(COND_M(m)->cond)->expr;
569 }
570 else if (COND_M(m)->cond->cmp == obj_cmp) {
571 type = "OBJ";
572 id = SDB_FIELD_TO_NAME(OBJ_C(COND_M(m)->cond)->field);
573 expr = OBJ_C(COND_M(m)->cond)->expr;
574 }
575 else {
576 snprintf(buf, buflen, "<unknown>");
577 return buf;
578 }
580 if (sdb_store_expr_eval(expr, NULL, &value))
581 snprintf(value_str, sizeof(value_str), "ERR");
582 else if (sdb_data_format(&value, value_str, sizeof(value_str),
583 SDB_SINGLE_QUOTED) < 0)
584 snprintf(value_str, sizeof(value_str), "ERR");
585 snprintf(buf, buflen, "%s[%s]{ %s %s }", type, id,
586 MATCHER_SYM(m->type), value_str);
587 sdb_data_free_datum(&value);
588 return buf;
589 } /* cond_tostring */
591 static int
592 op_matcher_init(sdb_object_t *obj, va_list ap)
593 {
594 M(obj)->type = va_arg(ap, int);
595 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
596 return -1;
598 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
599 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
600 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
601 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
603 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
604 return -1;
605 return 0;
606 } /* op_matcher_init */
608 static void
609 op_matcher_destroy(sdb_object_t *obj)
610 {
611 if (OP_M(obj)->left)
612 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
613 if (OP_M(obj)->right)
614 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
615 } /* op_matcher_destroy */
617 static char *
618 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
619 {
620 char left[buflen + 1], right[buflen + 1];
622 if (! m) {
623 /* this should not happen */
624 snprintf(buf, buflen, "()");
625 return buf;
626 }
628 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
629 snprintf(buf, buflen, "(%s, %s, %s)",
630 m->type == MATCHER_OR ? "OR" : "AND",
631 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
632 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
633 return buf;
634 } /* op_tostring */
636 static int
637 uop_matcher_init(sdb_object_t *obj, va_list ap)
638 {
639 M(obj)->type = va_arg(ap, int);
640 if (M(obj)->type != MATCHER_NOT)
641 return -1;
643 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
644 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
646 if (! UOP_M(obj)->op)
647 return -1;
648 return 0;
649 } /* uop_matcher_init */
651 static void
652 uop_matcher_destroy(sdb_object_t *obj)
653 {
654 if (UOP_M(obj)->op)
655 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
656 } /* uop_matcher_destroy */
658 static char *
659 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
660 {
661 char op[buflen + 1];
663 if (! m) {
664 /* this should not happen */
665 snprintf(buf, buflen, "()");
666 return buf;
667 }
669 assert(m->type == MATCHER_NOT);
670 snprintf(buf, buflen, "(NOT, %s)",
671 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
672 return buf;
673 } /* uop_tostring */
675 static int
676 isnull_matcher_init(sdb_object_t *obj, va_list ap)
677 {
678 const char *name;
680 M(obj)->type = va_arg(ap, int);
681 if (M(obj)->type != MATCHER_ISNULL)
682 return -1;
684 name = va_arg(ap, const char *);
685 if (! name)
686 return -1;
687 ISNULL_M(obj)->attr_name = strdup(name);
688 if (! ISNULL_M(obj)->attr_name)
689 return -1;
690 return 0;
691 } /* isnull_matcher_init */
693 static void
694 isnull_matcher_destroy(sdb_object_t *obj)
695 {
696 if (ISNULL_M(obj)->attr_name)
697 free(ISNULL_M(obj)->attr_name);
698 ISNULL_M(obj)->attr_name = NULL;
699 } /* isnull_matcher_destroy */
701 static char *
702 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
703 {
704 snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
705 return buf;
706 } /* isnull_tostring */
708 static sdb_type_t name_type = {
709 /* size = */ sizeof(name_matcher_t),
710 /* init = */ name_matcher_init,
711 /* destroy = */ name_matcher_destroy,
712 };
714 static sdb_type_t attr_type = {
715 /* size = */ sizeof(attr_matcher_t),
716 /* init = */ attr_matcher_init,
717 /* destroy = */ attr_matcher_destroy,
718 };
720 static sdb_type_t cond_type = {
721 /* size = */ sizeof(cond_matcher_t),
722 /* init = */ cond_matcher_init,
723 /* destroy = */ cond_matcher_destroy,
724 };
726 static sdb_type_t op_type = {
727 /* size = */ sizeof(op_matcher_t),
728 /* init = */ op_matcher_init,
729 /* destroy = */ op_matcher_destroy,
730 };
732 static sdb_type_t uop_type = {
733 /* size = */ sizeof(uop_matcher_t),
734 /* init = */ uop_matcher_init,
735 /* destroy = */ uop_matcher_destroy,
736 };
738 static sdb_type_t isnull_type = {
739 /* size = */ sizeof(isnull_matcher_t),
740 /* init = */ isnull_matcher_init,
741 /* destroy = */ isnull_matcher_destroy,
742 };
744 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
746 /* this array needs to be indexable by the matcher types;
747 * -> update the enum in store-private.h when updating this */
748 static matcher_tostring_cb
749 matchers_tostring[] = {
750 op_tostring,
751 op_tostring,
752 uop_tostring,
753 name_tostring,
754 attr_tostring,
755 cond_tostring,
756 cond_tostring,
757 cond_tostring,
758 cond_tostring,
759 cond_tostring,
760 isnull_tostring,
761 };
763 /*
764 * public API
765 */
767 sdb_store_cond_t *
768 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr)
769 {
770 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
771 name, expr));
772 } /* sdb_store_attr_cond */
774 sdb_store_cond_t *
775 sdb_store_obj_cond(int field, sdb_store_expr_t *expr)
776 {
777 return SDB_STORE_COND(sdb_object_create("obj-cond", obj_cond_type,
778 field, expr));
779 } /* sdb_store_obj_cond */
781 sdb_store_matcher_t *
782 sdb_store_name_matcher(int type, const char *name, _Bool re)
783 {
784 sdb_store_matcher_t *m;
786 if (re)
787 m = M(sdb_object_create("name-matcher", name_type, NULL, name));
788 else
789 m = M(sdb_object_create("name-matcher", name_type, name, NULL));
791 if (! m)
792 return NULL;
794 NAME_M(m)->obj_type = type;
795 return m;
796 } /* sdb_store_name_matcher */
798 sdb_store_matcher_t *
799 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
800 {
801 sdb_store_matcher_t *m;
803 if (! name)
804 return NULL;
806 if (re)
807 m = M(sdb_object_create("attr-matcher", attr_type,
808 name, NULL, value));
809 else
810 m = M(sdb_object_create("attr-matcher", attr_type,
811 name, value, NULL));
812 return m;
813 } /* sdb_store_attr_matcher */
815 sdb_store_matcher_t *
816 sdb_store_lt_matcher(sdb_store_cond_t *cond)
817 {
818 return M(sdb_object_create("lt-matcher", cond_type,
819 MATCHER_LT, cond));
820 } /* sdb_store_lt_matcher */
822 sdb_store_matcher_t *
823 sdb_store_le_matcher(sdb_store_cond_t *cond)
824 {
825 return M(sdb_object_create("le-matcher", cond_type,
826 MATCHER_LE, cond));
827 } /* sdb_store_le_matcher */
829 sdb_store_matcher_t *
830 sdb_store_eq_matcher(sdb_store_cond_t *cond)
831 {
832 return M(sdb_object_create("eq-matcher", cond_type,
833 MATCHER_EQ, cond));
834 } /* sdb_store_eq_matcher */
836 sdb_store_matcher_t *
837 sdb_store_ge_matcher(sdb_store_cond_t *cond)
838 {
839 return M(sdb_object_create("ge-matcher", cond_type,
840 MATCHER_GE, cond));
841 } /* sdb_store_ge_matcher */
843 sdb_store_matcher_t *
844 sdb_store_gt_matcher(sdb_store_cond_t *cond)
845 {
846 return M(sdb_object_create("gt-matcher", cond_type,
847 MATCHER_GT, cond));
848 } /* sdb_store_gt_matcher */
850 sdb_store_matcher_t *
851 sdb_store_isnull_matcher(const char *attr_name)
852 {
853 return M(sdb_object_create("isnull-matcher", isnull_type,
854 MATCHER_ISNULL, attr_name));
855 } /* sdb_store_isnull_matcher */
857 int
858 sdb_store_parse_field_name(const char *name)
859 {
860 if (! strcasecmp(name, "last_update"))
861 return SDB_FIELD_LAST_UPDATE;
862 else if (! strcasecmp(name, "age"))
863 return SDB_FIELD_AGE;
864 else if (! strcasecmp(name, "interval"))
865 return SDB_FIELD_INTERVAL;
866 else if (! strcasecmp(name, "backend"))
867 return SDB_FIELD_BACKEND;
868 return -1;
869 } /* sdb_store_parse_field_name */
871 static sdb_store_matcher_t *
872 maybe_inv_matcher(sdb_store_matcher_t *m, _Bool inv)
873 {
874 sdb_store_matcher_t *tmp;
876 if ((! m) || (! inv))
877 return m;
879 tmp = sdb_store_inv_matcher(m);
880 /* pass ownership to the inverse matcher */
881 sdb_object_deref(SDB_OBJ(m));
882 return tmp;
883 } /* maybe_inv_matcher */
885 static int
886 parse_cond_op(const char *op,
887 sdb_store_matcher_t *(**matcher)(sdb_store_cond_t *), _Bool *inv)
888 {
889 *inv = 0;
890 if (! strcasecmp(op, "<"))
891 *matcher = sdb_store_lt_matcher;
892 else if (! strcasecmp(op, "<="))
893 *matcher = sdb_store_le_matcher;
894 else if (! strcasecmp(op, "="))
895 *matcher = sdb_store_eq_matcher;
896 else if (! strcasecmp(op, ">="))
897 *matcher = sdb_store_ge_matcher;
898 else if (! strcasecmp(op, ">"))
899 *matcher = sdb_store_gt_matcher;
900 else if (! strcasecmp(op, "!=")) {
901 *matcher = sdb_store_eq_matcher;
902 *inv = 1;
903 }
904 else
905 return -1;
906 return 0;
907 } /* parse_cond_op */
909 static sdb_store_matcher_t *
910 parse_attr_cmp(const char *attr, const char *op, sdb_store_expr_t *expr)
911 {
912 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
913 sdb_store_matcher_t *m;
914 sdb_store_cond_t *cond;
915 _Bool inv = 0;
917 if (! attr)
918 return NULL;
920 if (! strcasecmp(op, "IS")) {
921 if (! expr)
922 return sdb_store_isnull_matcher(attr);
923 else
924 return NULL;
925 }
926 else if (! expr)
927 return NULL;
928 else if (parse_cond_op(op, &matcher, &inv))
929 return NULL;
931 cond = sdb_store_attr_cond(attr, expr);
932 if (! cond)
933 return NULL;
935 m = matcher(cond);
936 /* pass ownership to 'm' or destroy in case of an error */
937 sdb_object_deref(SDB_OBJ(cond));
938 return maybe_inv_matcher(m, inv);
939 } /* parse_attr_cmp */
941 sdb_store_matcher_t *
942 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
943 const char *op, sdb_store_expr_t *expr)
944 {
945 int type = -1;
946 _Bool inv = 0;
947 _Bool re = 0;
949 sdb_data_t value = SDB_DATA_INIT;
950 sdb_store_matcher_t *m = NULL;
952 if (! strcasecmp(obj_type, "host"))
953 type = SDB_HOST;
954 else if (! strcasecmp(obj_type, "service"))
955 type = SDB_SERVICE;
956 else if (! strcasecmp(obj_type, "attribute"))
957 type = SDB_ATTRIBUTE;
958 else
959 return NULL;
961 /* XXX: this code sucks! */
962 if (! strcasecmp(op, "=")) {
963 /* nothing to do */
964 }
965 else if (! strcasecmp(op, "!=")) {
966 inv = 1;
967 }
968 else if (! strcasecmp(op, "=~")) {
969 re = 1;
970 }
971 else if (! strcasecmp(op, "!~")) {
972 inv = 1;
973 re = 1;
974 }
975 else if (type == SDB_ATTRIBUTE)
976 return parse_attr_cmp(attr, op, expr);
977 else
978 return NULL;
980 if (! expr)
981 return NULL;
983 if (sdb_store_expr_eval(expr, NULL, &value))
984 return NULL;
985 if (value.type != SDB_TYPE_STRING) {
986 sdb_data_free_datum(&value);
987 return parse_attr_cmp(attr, op, expr);
988 }
990 if (! attr)
991 m = sdb_store_name_matcher(type, value.data.string, re);
992 else if (type == SDB_ATTRIBUTE)
993 m = sdb_store_attr_matcher(attr, value.data.string, re);
995 sdb_data_free_datum(&value);
996 return maybe_inv_matcher(m, inv);
997 } /* sdb_store_matcher_parse_cmp */
999 sdb_store_matcher_t *
1000 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
1001 sdb_store_expr_t *expr)
1002 {
1003 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
1004 sdb_store_matcher_t *m;
1005 sdb_store_cond_t *cond;
1006 _Bool inv = 0;
1008 int field;
1010 if (! expr)
1011 return NULL;
1013 field = sdb_store_parse_field_name(name);
1014 if (field < 0)
1015 return NULL;
1017 if (parse_cond_op(op, &matcher, &inv))
1018 return NULL;
1019 cond = sdb_store_obj_cond(field, expr);
1020 if (! cond)
1021 return NULL;
1023 assert(matcher);
1024 m = matcher(cond);
1025 /* pass ownership to 'm' or destroy in case of an error */
1026 sdb_object_deref(SDB_OBJ(cond));
1027 return maybe_inv_matcher(m, inv);
1028 } /* sdb_store_matcher_parse_field_cmp */
1030 sdb_store_matcher_t *
1031 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1032 {
1033 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
1034 left, right));
1035 } /* sdb_store_dis_matcher */
1037 sdb_store_matcher_t *
1038 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
1039 {
1040 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
1041 left, right));
1042 } /* sdb_store_con_matcher */
1044 sdb_store_matcher_t *
1045 sdb_store_inv_matcher(sdb_store_matcher_t *m)
1046 {
1047 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
1048 } /* sdb_store_inv_matcher */
1050 int
1051 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
1052 sdb_store_matcher_t *filter)
1053 {
1054 if (filter && (! sdb_store_matcher_matches(filter, obj, NULL)))
1055 return 0;
1057 /* "NULL" always matches */
1058 if ((! m) || (! obj))
1059 return 1;
1061 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
1062 return 0;
1064 return matchers[m->type](m, obj, filter);
1065 } /* sdb_store_matcher_matches */
1067 char *
1068 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
1069 {
1070 if (! m)
1071 return NULL;
1073 if ((m->type < 0)
1074 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
1075 return NULL;
1076 return matchers_tostring[m->type](m, buf, buflen);
1077 } /* sdb_store_matcher_tostring */
1079 int
1080 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1081 sdb_store_lookup_cb cb, void *user_data)
1082 {
1083 scan_iter_data_t data = { m, filter, cb, user_data };
1085 if (! cb)
1086 return -1;
1087 return sdb_store_iterate(scan_iter, &data);
1088 } /* sdb_store_scan */
1090 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */