f2d8cc7dd03bc2fd2cdc044af397ec9fecd0cfda
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 /*
51 * private data types
52 */
54 typedef struct {
55 sdb_store_matcher_t *m;
56 sdb_store_lookup_cb cb;
57 void *user_data;
58 } lookup_iter_data_t;
60 /*
61 * private helper functions
62 */
64 static int
65 lookup_iter(sdb_store_base_t *obj, void *user_data)
66 {
67 lookup_iter_data_t *d = user_data;
69 if (sdb_store_matcher_matches(d->m, obj))
70 return d->cb(obj, d->user_data);
71 return 0;
72 } /* lookup_iter */
74 /*
75 * matcher implementations
76 */
78 static int
79 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj);
80 static int
81 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj);
83 /* specific matchers */
85 static int
86 match_obj_name(name_matcher_t *m, const char *name)
87 {
88 assert(m);
90 if ((! m->name) && (! m->name_re))
91 return 1;
93 if (! name)
94 name = "";
96 if (m->name && strcasecmp(m->name, name))
97 return 0;
98 if (m->name_re && regexec(m->name_re, name,
99 /* matches */ 0, NULL, /* flags = */ 0))
100 return 0;
101 return 1;
102 } /* match_obj_name */
104 static char *
105 obj_name_tostring(name_matcher_t *m, char *buf, size_t buflen)
106 {
107 snprintf(buf, buflen, "{ %s%s%s, %p }",
108 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
109 m->name_re);
110 return buf;
111 } /* obj_name_tostring */
113 static char *
114 logical_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
115 {
116 char left[buflen + 1], right[buflen + 1];
118 if (! m) {
119 /* this should not happen */
120 snprintf(buf, buflen, "()");
121 return buf;
122 }
124 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
125 snprintf(buf, buflen, "(%s, %s, %s)",
126 m->type == MATCHER_OR ? "OR" : "AND",
127 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
128 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
129 return buf;
130 } /* logical_tostring */
132 static char *
133 unary_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
134 {
135 char op[buflen + 1];
137 if (! m) {
138 /* this should not happen */
139 snprintf(buf, buflen, "()");
140 return buf;
141 }
143 assert(m->type == MATCHER_NOT);
144 snprintf(buf, buflen, "(NOT, %s)",
145 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
146 return buf;
147 } /* unary_tostring */
149 static char *
150 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
151 {
152 char name[buflen + 1];
153 assert(m->type == MATCHER_NAME);
154 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
155 SDB_STORE_TYPE_TO_NAME(OBJ_M(m)->obj_type),
156 obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)));
157 return buf;
158 } /* name_tostring */
160 static char *
161 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
162 {
163 char value[buflen + 1];
165 if (! m) {
166 snprintf(buf, buflen, "ATTR{}");
167 return buf;
168 }
170 assert(m->type == MATCHER_ATTR);
171 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
172 obj_name_tostring(&ATTR_M(m)->value, value, sizeof(value)));
173 return buf;
174 } /* attr_tostring */
176 static char *
177 host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
178 {
179 char name[buflen + 1], attr[buflen + 1];
181 if (! m) {
182 snprintf(buf, buflen, "HOST{}");
183 return buf;
184 }
186 assert(m->type == MATCHER_HOST);
187 snprintf(buf, buflen, "HOST{ NAME%s, %s }",
188 obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
189 attr_tostring(SDB_STORE_MATCHER(HOST_M(m)->attr),
190 attr, sizeof(attr)));
191 return buf;
192 } /* host_tostring */
194 static int
195 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
196 {
197 sdb_llist_iter_t *iter = NULL;
198 int status = 0;
200 assert(m && obj);
201 assert(m->type == MATCHER_NAME);
203 if (obj->type != SDB_HOST)
204 return 0;
206 switch (OBJ_M(m)->obj_type) {
207 case SDB_HOST:
208 return match_obj_name(&OBJ_M(m)->name, obj->super.name);
209 break;
210 case SDB_SERVICE:
211 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
212 break;
213 case SDB_ATTRIBUTE:
214 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
215 break;
216 }
218 while (sdb_llist_iter_has_next(iter)) {
219 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
220 if (match_obj_name(&OBJ_M(m)->name, child->super.name)) {
221 status = 1;
222 break;
223 }
224 }
225 sdb_llist_iter_destroy(iter);
226 return status;
227 } /* match_name */
229 static int
230 match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
231 {
232 sdb_llist_iter_t *iter = NULL;
233 int status = 0;
235 assert(m && obj);
236 assert(m->type == MATCHER_ATTR);
238 if (obj->type != SDB_HOST)
239 return 0;
241 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
242 while (sdb_llist_iter_has_next(iter)) {
243 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
244 char buf[sdb_data_strlen(&attr->value) + 1];
246 if (ATTR_M(m)->name && strcasecmp(ATTR_M(m)->name, SDB_OBJ(attr)->name))
247 continue;
249 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
250 return 0;
251 if (match_obj_name(&ATTR_M(m)->value, buf)) {
252 status = 1;
253 break;
254 }
255 }
256 sdb_llist_iter_destroy(iter);
257 return status;
258 } /* match_attr */
260 /* match host specific values;
261 * always call this function through match_obj() */
262 static int
263 match_host(sdb_store_matcher_t *m, sdb_store_base_t *obj)
264 {
265 int status;
267 assert(m && obj);
268 assert(m->type == MATCHER_HOST);
270 if (obj->type != SDB_HOST)
271 return 0;
273 status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
274 if (! status)
275 return status;
277 if (! HOST_M(m)->attr)
278 return 1;
280 return match_attr(M(HOST_M(m)->attr), obj);
281 } /* match_host */
283 /* generic matchers */
285 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
287 /* this array needs to be indexable by the matcher types;
288 * -> update the enum in store-private.h when updating this */
289 static matcher_cb matchers[] = {
290 match_logical,
291 match_logical,
292 match_unary,
293 match_name,
294 match_attr,
295 match_host,
296 };
298 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
300 static matcher_tostring_cb matchers_tostring[] = {
301 logical_tostring,
302 logical_tostring,
303 unary_tostring,
304 name_tostring,
305 attr_tostring,
306 host_tostring,
307 };
309 static int
310 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
311 {
312 int status;
314 assert(m && obj);
315 assert(OP_M(m)->left && OP_M(m)->right);
317 status = sdb_store_matcher_matches(OP_M(m)->left, obj);
318 /* lazy evaluation */
319 if ((! status) && (m->type == MATCHER_AND))
320 return status;
321 else if (status && (m->type == MATCHER_OR))
322 return status;
324 return sdb_store_matcher_matches(OP_M(m)->right, obj);
325 } /* match_logical */
327 static int
328 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
329 {
330 assert(m && obj);
331 assert(UOP_M(m)->op);
333 return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
334 } /* match_unary */
336 /*
337 * private matcher types
338 */
340 /* initializes a name matcher consuming two elements from ap */
341 static int
342 name_matcher_init(name_matcher_t *m, va_list ap)
343 {
344 const char *name = va_arg(ap, const char *);
345 const char *name_re = va_arg(ap, const char *);
347 if (name) {
348 m->name = strdup(name);
349 if (! m->name)
350 return -1;
351 }
352 if (name_re) {
353 m->name_re = malloc(sizeof(*m->name_re));
354 if (! m->name_re)
355 return -1;
356 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
357 return -1;
358 }
359 return 0;
360 } /* name_matcher_init */
362 static void
363 name_matcher_destroy(name_matcher_t *m)
364 {
365 if (m->name)
366 free(m->name);
367 if (m->name_re) {
368 regfree(m->name_re);
369 free(m->name_re);
370 }
371 } /* name_matcher_destroy */
373 /* initializes an object matcher consuming two elements from ap */
374 static int
375 obj_matcher_init(sdb_object_t *obj, va_list ap)
376 {
377 obj_matcher_t *m = OBJ_M(obj);
378 M(obj)->type = MATCHER_NAME;
379 return name_matcher_init(&m->name, ap);
380 } /* obj_matcher_init */
382 static void
383 obj_matcher_destroy(sdb_object_t *obj)
384 {
385 obj_matcher_t *m = OBJ_M(obj);
386 name_matcher_destroy(&m->name);
387 } /* obj_matcher_destroy */
389 static int
390 attr_matcher_init(sdb_object_t *obj, va_list ap)
391 {
392 attr_matcher_t *attr = ATTR_M(obj);
393 const char *name = va_arg(ap, const char *);
395 M(obj)->type = MATCHER_ATTR;
396 if (name) {
397 attr->name = strdup(name);
398 if (! attr->name)
399 return -1;
400 }
401 return name_matcher_init(&attr->value, ap);
402 } /* attr_matcher_init */
404 static void
405 attr_matcher_destroy(sdb_object_t *obj)
406 {
407 attr_matcher_t *attr = ATTR_M(obj);
408 if (attr->name)
409 free(attr->name);
410 attr->name = NULL;
411 name_matcher_destroy(&attr->value);
412 } /* attr_matcher_destroy */
414 static int
415 host_matcher_init(sdb_object_t *obj, va_list ap)
416 {
417 attr_matcher_t *attr;
418 int status;
420 status = obj_matcher_init(obj, ap);
421 if (status)
422 return status;
424 attr = va_arg(ap, attr_matcher_t *);
425 sdb_object_ref(SDB_OBJ(attr));
426 HOST_M(obj)->attr = attr;
428 M(obj)->type = MATCHER_HOST;
429 return 0;
430 } /* host_matcher_init */
432 static void
433 host_matcher_destroy(sdb_object_t *obj)
434 {
435 obj_matcher_destroy(obj);
436 sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
437 } /* host_matcher_destroy */
439 static int
440 op_matcher_init(sdb_object_t *obj, va_list ap)
441 {
442 M(obj)->type = va_arg(ap, int);
443 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
444 return -1;
446 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
447 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
448 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
449 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
451 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
452 return -1;
453 return 0;
454 } /* op_matcher_init */
456 static void
457 op_matcher_destroy(sdb_object_t *obj)
458 {
459 if (OP_M(obj)->left)
460 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
461 if (OP_M(obj)->right)
462 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
463 } /* op_matcher_destroy */
465 static int
466 uop_matcher_init(sdb_object_t *obj, va_list ap)
467 {
468 M(obj)->type = va_arg(ap, int);
469 if (M(obj)->type != MATCHER_NOT)
470 return -1;
472 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
473 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
475 if (! UOP_M(obj)->op)
476 return -1;
477 return 0;
478 } /* uop_matcher_init */
480 static void
481 uop_matcher_destroy(sdb_object_t *obj)
482 {
483 if (UOP_M(obj)->op)
484 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
485 } /* uop_matcher_destroy */
487 static sdb_type_t name_type = {
488 /* size = */ sizeof(obj_matcher_t),
489 /* init = */ obj_matcher_init,
490 /* destroy = */ obj_matcher_destroy,
491 };
493 static sdb_type_t attr_type = {
494 /* size = */ sizeof(attr_matcher_t),
495 /* init = */ attr_matcher_init,
496 /* destroy = */ attr_matcher_destroy,
497 };
499 static sdb_type_t host_type = {
500 /* size = */ sizeof(host_matcher_t),
501 /* init = */ host_matcher_init,
502 /* destroy = */ host_matcher_destroy,
503 };
505 static sdb_type_t op_type = {
506 /* size = */ sizeof(op_matcher_t),
507 /* init = */ op_matcher_init,
508 /* destroy = */ op_matcher_destroy,
509 };
511 static sdb_type_t uop_type = {
512 /* size = */ sizeof(uop_matcher_t),
513 /* init = */ uop_matcher_init,
514 /* destroy = */ uop_matcher_destroy,
515 };
517 /*
518 * public API
519 */
521 sdb_store_matcher_t *
522 sdb_store_name_matcher(int type, const char *name, _Bool re)
523 {
524 sdb_store_matcher_t *m;
526 if (re)
527 m = M(sdb_object_create("name-matcher", name_type,
528 NULL, name));
529 else
530 m = M(sdb_object_create("name-matcher", name_type,
531 name, NULL));
533 if (! m)
534 return NULL;
536 OBJ_M(m)->obj_type = type;
537 return m;
538 } /* sdb_store_name_matcher */
540 sdb_store_matcher_t *
541 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
542 {
543 if (re)
544 return M(sdb_object_create("attr-matcher", attr_type,
545 name, NULL, value));
546 return M(sdb_object_create("attr-matcher", attr_type,
547 name, value, NULL));
548 } /* sdb_store_attr_matcher */
550 sdb_store_matcher_t *
551 sdb_store_host_matcher(const char *host_name, const char *host_name_re,
552 sdb_store_matcher_t *attr_matcher)
553 {
554 return M(sdb_object_create("host-matcher", host_type,
555 host_name, host_name_re, attr_matcher));
556 } /* sdb_store_host_matcher */
558 sdb_store_matcher_t *
559 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
560 const char *op, const char *value)
561 {
562 int type = -1;
563 _Bool inv = 0;
564 _Bool re = 0;
566 sdb_store_matcher_t *m = NULL;
568 if (! strcasecmp(obj_type, "host"))
569 type = SDB_HOST;
570 else if (! strcasecmp(obj_type, "service"))
571 type = SDB_SERVICE;
572 else if (! strcasecmp(obj_type, "attribute"))
573 type = SDB_ATTRIBUTE;
575 /* TODO: support other operators */
576 if (! strcasecmp(op, "=")) {
577 /* nothing to do */
578 }
579 else if (! strcasecmp(op, "!=")) {
580 inv = 1;
581 }
582 else if (! strcasecmp(op, "=~")) {
583 re = 1;
584 }
585 else if (! strcasecmp(op, "!~")) {
586 inv = 1;
587 re = 1;
588 }
589 else
590 return NULL;
592 if (! strcasecmp(attr, "name"))
593 m = sdb_store_name_matcher(type, value, re);
594 else if (type == SDB_ATTRIBUTE)
595 m = sdb_store_attr_matcher(attr, value, re);
597 if (! m)
598 return NULL;
600 if (inv) {
601 sdb_store_matcher_t *tmp;
602 tmp = sdb_store_inv_matcher(m);
603 /* pass ownership to the inverse matcher */
604 sdb_object_deref(SDB_OBJ(m));
605 m = tmp;
606 }
607 return m;
608 } /* sdb_store_matcher_parse_cmp */
610 sdb_store_matcher_t *
611 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
612 {
613 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
614 left, right));
615 } /* sdb_store_dis_matcher */
617 sdb_store_matcher_t *
618 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
619 {
620 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
621 left, right));
622 } /* sdb_store_con_matcher */
624 sdb_store_matcher_t *
625 sdb_store_inv_matcher(sdb_store_matcher_t *m)
626 {
627 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
628 } /* sdb_store_inv_matcher */
630 int
631 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
632 {
633 /* "NULL" always matches */
634 if ((! m) || (! obj))
635 return 1;
637 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
638 return 0;
640 return matchers[m->type](m, obj);
641 } /* sdb_store_matcher_matches */
643 char *
644 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
645 {
646 if (! m)
647 return NULL;
649 if ((m->type < 0)
650 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
651 return NULL;
652 return matchers_tostring[m->type](m, buf, buflen);
653 } /* sdb_store_matcher_tostring */
655 int
656 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
657 void *user_data)
658 {
659 lookup_iter_data_t data = { m, cb, user_data };
661 if (! cb)
662 return -1;
663 return sdb_store_iterate(lookup_iter, &data);
664 } /* sdb_store_lookup */
666 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */