da9f55076f6d5a1fc33969bee7012d3fb6ea6856
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_lookup_cb cb;
59 void *user_data;
60 } lookup_iter_data_t;
62 /*
63 * private helper functions
64 */
66 static int
67 lookup_iter(sdb_store_obj_t *obj, void *user_data)
68 {
69 lookup_iter_data_t *d = user_data;
71 if (sdb_store_matcher_matches(d->m, obj))
72 return d->cb(obj, d->user_data);
73 return 0;
74 } /* lookup_iter */
76 static sdb_attribute_t *
77 attr_get(sdb_host_t *host, const char *name)
78 {
79 sdb_avltree_iter_t *iter = NULL;
80 sdb_attribute_t *attr = NULL;
82 iter = sdb_avltree_get_iter(host->attributes);
83 while (sdb_avltree_iter_has_next(iter)) {
84 sdb_attribute_t *a = ATTR(sdb_avltree_iter_get_next(iter));
86 if (strcasecmp(name, SDB_OBJ(a)->name))
87 continue;
89 assert(STORE_OBJ(a)->type == SDB_ATTRIBUTE);
90 attr = a;
91 break;
92 }
93 sdb_avltree_iter_destroy(iter);
94 return attr;
95 } /* attr_get */
97 /*
98 * conditional implementations
99 */
101 static int
102 attr_cmp(sdb_host_t *host, sdb_store_cond_t *cond)
103 {
104 sdb_attribute_t *attr;
106 attr = attr_get(host, ATTR_C(cond)->name);
107 if (! attr)
108 return INT_MAX;
109 if (attr->value.type != ATTR_C(cond)->value.type)
110 return INT_MAX;
111 return sdb_data_cmp(&attr->value, &ATTR_C(cond)->value);
112 } /* attr_cmp */
114 /*
115 * matcher implementations
116 */
118 static int
119 match_string(string_matcher_t *m, const char *name)
120 {
121 if ((! m->name) && (! m->name_re))
122 return 1;
124 if (! name)
125 name = "";
127 if (m->name && strcasecmp(m->name, name))
128 return 0;
129 if (m->name_re && regexec(m->name_re, name,
130 /* matches */ 0, NULL, /* flags = */ 0))
131 return 0;
132 return 1;
133 } /* match_string */
135 static int
136 match_logical(sdb_store_matcher_t *m, sdb_host_t *host)
137 {
138 int status;
140 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
141 assert(OP_M(m)->left && OP_M(m)->right);
143 status = sdb_store_matcher_matches(OP_M(m)->left, STORE_OBJ(host));
144 /* lazy evaluation */
145 if ((! status) && (m->type == MATCHER_AND))
146 return status;
147 else if (status && (m->type == MATCHER_OR))
148 return status;
150 return sdb_store_matcher_matches(OP_M(m)->right, STORE_OBJ(host));
151 } /* match_logical */
153 static int
154 match_unary(sdb_store_matcher_t *m, sdb_host_t *host)
155 {
156 assert(m->type == MATCHER_NOT);
157 assert(UOP_M(m)->op);
159 return !sdb_store_matcher_matches(UOP_M(m)->op, STORE_OBJ(host));
160 } /* match_unary */
162 static int
163 match_name(sdb_store_matcher_t *m, sdb_host_t *host)
164 {
165 sdb_avltree_iter_t *iter = NULL;
166 int status = 0;
168 assert(m->type == MATCHER_NAME);
170 switch (NAME_M(m)->obj_type) {
171 case SDB_HOST:
172 return match_string(&NAME_M(m)->name, SDB_OBJ(host)->name);
173 break;
174 case SDB_SERVICE:
175 iter = sdb_avltree_get_iter(host->services);
176 break;
177 case SDB_ATTRIBUTE:
178 iter = sdb_avltree_get_iter(host->attributes);
179 break;
180 }
182 while (sdb_avltree_iter_has_next(iter)) {
183 sdb_object_t *child = sdb_avltree_iter_get_next(iter);
184 if (match_string(&NAME_M(m)->name, child->name)) {
185 status = 1;
186 break;
187 }
188 }
189 sdb_avltree_iter_destroy(iter);
190 return status;
191 } /* match_name */
193 static int
194 match_attr(sdb_store_matcher_t *m, sdb_host_t *host)
195 {
196 sdb_attribute_t *attr;
198 assert(m->type == MATCHER_ATTR);
199 assert(ATTR_M(m)->name);
201 attr = attr_get(host, ATTR_M(m)->name);
202 if (attr) {
203 char buf[sdb_data_strlen(&attr->value) + 1];
204 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
205 return 0;
206 if (match_string(&ATTR_M(m)->value, buf))
207 return 1;
208 }
209 return 0;
210 } /* match_attr */
212 static int
213 match_lt(sdb_store_matcher_t *m, sdb_host_t *host)
214 {
215 int status;
216 assert(m->type == MATCHER_LT);
217 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
218 return (status != INT_MAX) && (status < 0);
219 } /* match_lt */
221 static int
222 match_le(sdb_store_matcher_t *m, sdb_host_t *host)
223 {
224 int status;
225 assert(m->type == MATCHER_LE);
226 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
227 return (status != INT_MAX) && (status <= 0);
228 } /* match_le */
230 static int
231 match_eq(sdb_store_matcher_t *m, sdb_host_t *host)
232 {
233 int status;
234 assert(m->type == MATCHER_EQ);
235 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
236 return (status != INT_MAX) && (! status);
237 } /* match_eq */
239 static int
240 match_ge(sdb_store_matcher_t *m, sdb_host_t *host)
241 {
242 int status;
243 assert(m->type == MATCHER_GE);
244 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
245 return (status != INT_MAX) && (status >= 0);
246 } /* match_ge */
248 static int
249 match_gt(sdb_store_matcher_t *m, sdb_host_t *host)
250 {
251 int status;
252 assert(m->type == MATCHER_GT);
253 status = COND_M(m)->cond->cmp(host, COND_M(m)->cond);
254 return (status != INT_MAX) && (status > 0);
255 } /* match_gt */
257 static int
258 match_isnull(sdb_store_matcher_t *m, sdb_host_t *host)
259 {
260 assert(m->type == MATCHER_ISNULL);
261 return attr_get(host, ISNULL_M(m)->attr_name) == NULL;
262 } /* match_isnull */
264 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_host_t *);
266 /* this array needs to be indexable by the matcher types;
267 * -> update the enum in store-private.h when updating this */
268 static matcher_cb
269 matchers[] = {
270 match_logical,
271 match_logical,
272 match_unary,
273 match_name,
274 match_attr,
275 match_lt,
276 match_le,
277 match_eq,
278 match_ge,
279 match_gt,
280 match_isnull,
281 };
283 /*
284 * private conditional types
285 */
287 static int
288 attr_cond_init(sdb_object_t *obj, va_list ap)
289 {
290 const char *name = va_arg(ap, const char *);
291 const sdb_data_t *value = va_arg(ap, const sdb_data_t *);
293 if (! name)
294 return -1;
296 SDB_STORE_COND(obj)->cmp = attr_cmp;
298 ATTR_C(obj)->name = strdup(name);
299 if (! ATTR_C(obj)->name)
300 return -1;
301 if (sdb_data_copy(&ATTR_C(obj)->value, value))
302 return -1;
303 return 0;
304 } /* attr_cond_init */
306 static void
307 attr_cond_destroy(sdb_object_t *obj)
308 {
309 if (ATTR_C(obj)->name)
310 free(ATTR_C(obj)->name);
311 sdb_data_free_datum(&ATTR_C(obj)->value);
312 } /* attr_cond_destroy */
314 static sdb_type_t attr_cond_type = {
315 /* size = */ sizeof(attr_cond_t),
316 /* init = */ attr_cond_init,
317 /* destroy = */ attr_cond_destroy,
318 };
320 /*
321 * private matcher types
322 */
324 /* initializes a string matcher consuming two elements from ap */
325 static int
326 string_matcher_init(string_matcher_t *m, va_list ap)
327 {
328 const char *name = va_arg(ap, const char *);
329 const char *name_re = va_arg(ap, const char *);
331 if (name) {
332 m->name = strdup(name);
333 if (! m->name)
334 return -1;
335 }
336 if (name_re) {
337 m->name_re = malloc(sizeof(*m->name_re));
338 if (! m->name_re)
339 return -1;
340 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
341 return -1;
342 }
343 return 0;
344 } /* string_matcher_init */
346 static void
347 string_matcher_destroy(string_matcher_t *m)
348 {
349 if (m->name)
350 free(m->name);
351 if (m->name_re) {
352 regfree(m->name_re);
353 free(m->name_re);
354 }
355 } /* string_matcher_destroy */
357 static char *
358 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
359 {
360 snprintf(buf, buflen, "{ %s%s%s, %p }",
361 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
362 m->name_re);
363 return buf;
364 } /* string_tostring */
366 /* initializes a name matcher */
367 static int
368 name_matcher_init(sdb_object_t *obj, va_list ap)
369 {
370 name_matcher_t *m = NAME_M(obj);
371 M(obj)->type = MATCHER_NAME;
372 return string_matcher_init(&m->name, ap);
373 } /* name_matcher_init */
375 static void
376 name_matcher_destroy(sdb_object_t *obj)
377 {
378 name_matcher_t *m = NAME_M(obj);
379 string_matcher_destroy(&m->name);
380 } /* name_matcher_destroy */
382 static char *
383 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
384 {
385 char name[buflen + 1];
386 assert(m->type == MATCHER_NAME);
387 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
388 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
389 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
390 return buf;
391 } /* name_tostring */
393 static int
394 attr_matcher_init(sdb_object_t *obj, va_list ap)
395 {
396 attr_matcher_t *attr = ATTR_M(obj);
397 const char *name = va_arg(ap, const char *);
399 M(obj)->type = MATCHER_ATTR;
400 if (name) {
401 attr->name = strdup(name);
402 if (! attr->name)
403 return -1;
404 }
405 return string_matcher_init(&attr->value, ap);
406 } /* attr_matcher_init */
408 static void
409 attr_matcher_destroy(sdb_object_t *obj)
410 {
411 attr_matcher_t *attr = ATTR_M(obj);
412 if (attr->name)
413 free(attr->name);
414 attr->name = NULL;
415 string_matcher_destroy(&attr->value);
416 } /* attr_matcher_destroy */
418 static char *
419 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
420 {
421 char value[buflen + 1];
423 if (! m) {
424 snprintf(buf, buflen, "ATTR{}");
425 return buf;
426 }
428 assert(m->type == MATCHER_ATTR);
429 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
430 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
431 return buf;
432 } /* attr_tostring */
434 static int
435 cond_matcher_init(sdb_object_t *obj, va_list ap)
436 {
437 int type = va_arg(ap, int);
438 sdb_store_cond_t *cond = va_arg(ap, sdb_store_cond_t *);
440 if (! cond)
441 return -1;
443 sdb_object_ref(SDB_OBJ(cond));
445 M(obj)->type = type;
446 COND_M(obj)->cond = cond;
447 return 0;
448 } /* cond_matcher_init */
450 static void
451 cond_matcher_destroy(sdb_object_t *obj)
452 {
453 sdb_object_deref(SDB_OBJ(COND_M(obj)->cond));
454 } /* cond_matcher_destroy */
456 static char *
457 cond_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
458 {
459 if (COND_M(m)->cond->cmp == attr_cmp) {
460 char value[buflen];
461 if (sdb_data_format(&ATTR_C(COND_M(m)->cond)->value,
462 value, sizeof(value), SDB_SINGLE_QUOTED) < 0)
463 snprintf(value, sizeof(value), "ERR");
464 snprintf(buf, buflen, "ATTR[%s]{ %s %s }",
465 ATTR_C(COND_M(m)->cond)->name, MATCHER_SYM(m->type), value);
466 }
467 return buf;
468 } /* cond_tostring */
470 static int
471 op_matcher_init(sdb_object_t *obj, va_list ap)
472 {
473 M(obj)->type = va_arg(ap, int);
474 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
475 return -1;
477 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
478 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
479 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
480 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
482 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
483 return -1;
484 return 0;
485 } /* op_matcher_init */
487 static void
488 op_matcher_destroy(sdb_object_t *obj)
489 {
490 if (OP_M(obj)->left)
491 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
492 if (OP_M(obj)->right)
493 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
494 } /* op_matcher_destroy */
496 static char *
497 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
498 {
499 char left[buflen + 1], right[buflen + 1];
501 if (! m) {
502 /* this should not happen */
503 snprintf(buf, buflen, "()");
504 return buf;
505 }
507 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
508 snprintf(buf, buflen, "(%s, %s, %s)",
509 m->type == MATCHER_OR ? "OR" : "AND",
510 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
511 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
512 return buf;
513 } /* op_tostring */
515 static int
516 uop_matcher_init(sdb_object_t *obj, va_list ap)
517 {
518 M(obj)->type = va_arg(ap, int);
519 if (M(obj)->type != MATCHER_NOT)
520 return -1;
522 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
523 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
525 if (! UOP_M(obj)->op)
526 return -1;
527 return 0;
528 } /* uop_matcher_init */
530 static void
531 uop_matcher_destroy(sdb_object_t *obj)
532 {
533 if (UOP_M(obj)->op)
534 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
535 } /* uop_matcher_destroy */
537 static char *
538 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
539 {
540 char op[buflen + 1];
542 if (! m) {
543 /* this should not happen */
544 snprintf(buf, buflen, "()");
545 return buf;
546 }
548 assert(m->type == MATCHER_NOT);
549 snprintf(buf, buflen, "(NOT, %s)",
550 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
551 return buf;
552 } /* uop_tostring */
554 static int
555 isnull_matcher_init(sdb_object_t *obj, va_list ap)
556 {
557 const char *name;
559 M(obj)->type = va_arg(ap, int);
560 if (M(obj)->type != MATCHER_ISNULL)
561 return -1;
563 name = va_arg(ap, const char *);
564 if (! name)
565 return -1;
566 ISNULL_M(obj)->attr_name = strdup(name);
567 if (! ISNULL_M(obj)->attr_name)
568 return -1;
569 return 0;
570 } /* isnull_matcher_init */
572 static void
573 isnull_matcher_destroy(sdb_object_t *obj)
574 {
575 if (ISNULL_M(obj)->attr_name)
576 free(ISNULL_M(obj)->attr_name);
577 ISNULL_M(obj)->attr_name = NULL;
578 } /* isnull_matcher_destroy */
580 static char *
581 isnull_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
582 {
583 snprintf(buf, buflen, "(IS NULL, ATTR[%s])", ISNULL_M(m)->attr_name);
584 return buf;
585 } /* isnull_tostring */
587 static sdb_type_t name_type = {
588 /* size = */ sizeof(name_matcher_t),
589 /* init = */ name_matcher_init,
590 /* destroy = */ name_matcher_destroy,
591 };
593 static sdb_type_t attr_type = {
594 /* size = */ sizeof(attr_matcher_t),
595 /* init = */ attr_matcher_init,
596 /* destroy = */ attr_matcher_destroy,
597 };
599 static sdb_type_t cond_type = {
600 /* size = */ sizeof(cond_matcher_t),
601 /* init = */ cond_matcher_init,
602 /* destroy = */ cond_matcher_destroy,
603 };
605 static sdb_type_t op_type = {
606 /* size = */ sizeof(op_matcher_t),
607 /* init = */ op_matcher_init,
608 /* destroy = */ op_matcher_destroy,
609 };
611 static sdb_type_t uop_type = {
612 /* size = */ sizeof(uop_matcher_t),
613 /* init = */ uop_matcher_init,
614 /* destroy = */ uop_matcher_destroy,
615 };
617 static sdb_type_t isnull_type = {
618 /* size = */ sizeof(isnull_matcher_t),
619 /* init = */ isnull_matcher_init,
620 /* destroy = */ isnull_matcher_destroy,
621 };
623 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
625 /* this array needs to be indexable by the matcher types;
626 * -> update the enum in store-private.h when updating this */
627 static matcher_tostring_cb
628 matchers_tostring[] = {
629 op_tostring,
630 op_tostring,
631 uop_tostring,
632 name_tostring,
633 attr_tostring,
634 cond_tostring,
635 cond_tostring,
636 cond_tostring,
637 cond_tostring,
638 cond_tostring,
639 isnull_tostring,
640 };
642 /*
643 * public API
644 */
646 sdb_store_cond_t *
647 sdb_store_attr_cond(const char *name, const sdb_data_t *value)
648 {
649 return SDB_STORE_COND(sdb_object_create("attr-cond", attr_cond_type,
650 name, value));
651 } /* sdb_store_attr_cond */
653 sdb_store_matcher_t *
654 sdb_store_name_matcher(int type, const char *name, _Bool re)
655 {
656 sdb_store_matcher_t *m;
658 if (re)
659 m = M(sdb_object_create("name-matcher", name_type,
660 NULL, name));
661 else
662 m = M(sdb_object_create("name-matcher", name_type,
663 name, NULL));
665 if (! m)
666 return NULL;
668 NAME_M(m)->obj_type = type;
669 return m;
670 } /* sdb_store_name_matcher */
672 sdb_store_matcher_t *
673 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
674 {
675 if (! name)
676 return NULL;
678 if (re)
679 return M(sdb_object_create("attr-matcher", attr_type,
680 name, NULL, value));
681 return M(sdb_object_create("attr-matcher", attr_type,
682 name, value, NULL));
683 } /* sdb_store_attr_matcher */
685 sdb_store_matcher_t *
686 sdb_store_lt_matcher(sdb_store_cond_t *cond)
687 {
688 return M(sdb_object_create("lt-matcher", cond_type,
689 MATCHER_LT, cond));
690 } /* sdb_store_lt_matcher */
692 sdb_store_matcher_t *
693 sdb_store_le_matcher(sdb_store_cond_t *cond)
694 {
695 return M(sdb_object_create("le-matcher", cond_type,
696 MATCHER_LE, cond));
697 } /* sdb_store_le_matcher */
699 sdb_store_matcher_t *
700 sdb_store_eq_matcher(sdb_store_cond_t *cond)
701 {
702 return M(sdb_object_create("eq-matcher", cond_type,
703 MATCHER_EQ, cond));
704 } /* sdb_store_eq_matcher */
706 sdb_store_matcher_t *
707 sdb_store_ge_matcher(sdb_store_cond_t *cond)
708 {
709 return M(sdb_object_create("ge-matcher", cond_type,
710 MATCHER_GE, cond));
711 } /* sdb_store_ge_matcher */
713 sdb_store_matcher_t *
714 sdb_store_gt_matcher(sdb_store_cond_t *cond)
715 {
716 return M(sdb_object_create("gt-matcher", cond_type,
717 MATCHER_GT, cond));
718 } /* sdb_store_gt_matcher */
720 sdb_store_matcher_t *
721 sdb_store_isnull_matcher(const char *attr_name)
722 {
723 return M(sdb_object_create("isnull-matcher", isnull_type,
724 MATCHER_ISNULL, attr_name));
725 } /* sdb_store_isnull_matcher */
727 static sdb_store_matcher_t *
728 parse_attr_cmp(const char *attr, const char *op, const sdb_data_t *value)
729 {
730 sdb_store_matcher_t *(*matcher)(sdb_store_cond_t *) = NULL;
731 sdb_store_matcher_t *m;
732 sdb_store_cond_t *cond;
733 _Bool inv = 0;
735 /* TODO: this will reject any attributes called "name";
736 * use a different syntax for querying objects by name */
737 if (! strcasecmp(attr, "name"))
738 return NULL;
740 if (! strcasecmp(op, "IS")) {
741 if (! value)
742 return sdb_store_isnull_matcher(attr);
743 else
744 return NULL;
745 }
746 else if (! value)
747 return NULL;
748 else if (! strcasecmp(op, "<"))
749 matcher = sdb_store_lt_matcher;
750 else if (! strcasecmp(op, "<="))
751 matcher = sdb_store_le_matcher;
752 else if (! strcasecmp(op, "="))
753 matcher = sdb_store_eq_matcher;
754 else if (! strcasecmp(op, ">="))
755 matcher = sdb_store_ge_matcher;
756 else if (! strcasecmp(op, ">"))
757 matcher = sdb_store_gt_matcher;
758 else if (! strcasecmp(op, "!=")) {
759 matcher = sdb_store_eq_matcher;
760 inv = 1;
761 }
762 else
763 return NULL;
765 cond = sdb_store_attr_cond(attr, value);
766 if (! cond)
767 return NULL;
769 m = matcher(cond);
770 /* pass ownership to 'm' or destroy in case of an error */
771 sdb_object_deref(SDB_OBJ(cond));
772 if (! m)
773 return NULL;
775 if (inv) {
776 sdb_store_matcher_t *tmp;
777 tmp = sdb_store_inv_matcher(m);
778 /* pass ownership to the inverse matcher */
779 sdb_object_deref(SDB_OBJ(m));
780 m = tmp;
781 }
782 return m;
783 } /* parse_attr_cmp */
785 sdb_store_matcher_t *
786 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
787 const char *op, const sdb_data_t *value)
788 {
789 int type = -1;
790 _Bool inv = 0;
791 _Bool re = 0;
793 sdb_store_matcher_t *m = NULL;
795 if (! strcasecmp(obj_type, "host"))
796 type = SDB_HOST;
797 else if (! strcasecmp(obj_type, "service"))
798 type = SDB_SERVICE;
799 else if (! strcasecmp(obj_type, "attribute"))
800 type = SDB_ATTRIBUTE;
801 else
802 return NULL;
804 /* XXX: this code sucks! */
805 if (! strcasecmp(op, "=")) {
806 /* nothing to do */
807 }
808 else if (! strcasecmp(op, "!=")) {
809 inv = 1;
810 }
811 else if (! strcasecmp(op, "=~")) {
812 re = 1;
813 }
814 else if (! strcasecmp(op, "!~")) {
815 inv = 1;
816 re = 1;
817 }
818 else if (type == SDB_ATTRIBUTE)
819 return parse_attr_cmp(attr, op, value);
820 else
821 return NULL;
823 if (value->type != SDB_TYPE_STRING) {
824 if (type == SDB_ATTRIBUTE)
825 return parse_attr_cmp(attr, op, value);
826 return NULL;
827 }
829 if (! strcasecmp(attr, "name"))
830 m = sdb_store_name_matcher(type, value->data.string, re);
831 else if (type == SDB_ATTRIBUTE)
832 m = sdb_store_attr_matcher(attr, value->data.string, re);
834 if (! m)
835 return NULL;
837 if (inv) {
838 sdb_store_matcher_t *tmp;
839 tmp = sdb_store_inv_matcher(m);
840 /* pass ownership to the inverse matcher */
841 sdb_object_deref(SDB_OBJ(m));
842 m = tmp;
843 }
844 return m;
845 } /* sdb_store_matcher_parse_cmp */
847 sdb_store_matcher_t *
848 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
849 {
850 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
851 left, right));
852 } /* sdb_store_dis_matcher */
854 sdb_store_matcher_t *
855 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
856 {
857 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
858 left, right));
859 } /* sdb_store_con_matcher */
861 sdb_store_matcher_t *
862 sdb_store_inv_matcher(sdb_store_matcher_t *m)
863 {
864 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
865 } /* sdb_store_inv_matcher */
867 int
868 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj)
869 {
870 if (obj->type != SDB_HOST)
871 return 0;
873 /* "NULL" always matches */
874 if ((! m) || (! obj))
875 return 1;
877 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
878 return 0;
880 return matchers[m->type](m, HOST(obj));
881 } /* sdb_store_matcher_matches */
883 char *
884 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
885 {
886 if (! m)
887 return NULL;
889 if ((m->type < 0)
890 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
891 return NULL;
892 return matchers_tostring[m->type](m, buf, buflen);
893 } /* sdb_store_matcher_tostring */
895 int
896 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
897 void *user_data)
898 {
899 lookup_iter_data_t data = { m, cb, user_data };
901 if (! cb)
902 return -1;
903 return sdb_store_iterate(lookup_iter, &data);
904 } /* sdb_store_lookup */
906 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */