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 int
177 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
178 {
179 sdb_llist_iter_t *iter = NULL;
180 int status = 0;
182 assert(m && obj);
183 assert(m->type == MATCHER_NAME);
185 if (obj->type != SDB_HOST)
186 return 0;
188 switch (OBJ_M(m)->obj_type) {
189 case SDB_HOST:
190 return match_obj_name(&OBJ_M(m)->name, obj->super.name);
191 break;
192 case SDB_SERVICE:
193 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
194 break;
195 case SDB_ATTRIBUTE:
196 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
197 break;
198 }
200 while (sdb_llist_iter_has_next(iter)) {
201 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
202 if (match_obj_name(&OBJ_M(m)->name, child->super.name)) {
203 status = 1;
204 break;
205 }
206 }
207 sdb_llist_iter_destroy(iter);
208 return status;
209 } /* match_name */
211 static int
212 match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
213 {
214 sdb_llist_iter_t *iter = NULL;
215 int status = 0;
217 assert(m && obj);
218 assert(m->type == MATCHER_ATTR);
220 if (obj->type != SDB_HOST)
221 return 0;
223 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
224 while (sdb_llist_iter_has_next(iter)) {
225 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
226 char buf[sdb_data_strlen(&attr->value) + 1];
228 if (ATTR_M(m)->name && strcasecmp(ATTR_M(m)->name, SDB_OBJ(attr)->name))
229 continue;
231 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
232 return 0;
233 if (match_obj_name(&ATTR_M(m)->value, buf)) {
234 status = 1;
235 break;
236 }
237 }
238 sdb_llist_iter_destroy(iter);
239 return status;
240 } /* match_attr */
242 /* generic matchers */
244 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
246 /* this array needs to be indexable by the matcher types;
247 * -> update the enum in store-private.h when updating this */
248 static matcher_cb matchers[] = {
249 match_logical,
250 match_logical,
251 match_unary,
252 match_name,
253 match_attr,
254 };
256 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
258 static matcher_tostring_cb matchers_tostring[] = {
259 logical_tostring,
260 logical_tostring,
261 unary_tostring,
262 name_tostring,
263 attr_tostring,
264 };
266 static int
267 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
268 {
269 int status;
271 assert(m && obj);
272 assert(OP_M(m)->left && OP_M(m)->right);
274 status = sdb_store_matcher_matches(OP_M(m)->left, obj);
275 /* lazy evaluation */
276 if ((! status) && (m->type == MATCHER_AND))
277 return status;
278 else if (status && (m->type == MATCHER_OR))
279 return status;
281 return sdb_store_matcher_matches(OP_M(m)->right, obj);
282 } /* match_logical */
284 static int
285 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
286 {
287 assert(m && obj);
288 assert(UOP_M(m)->op);
290 return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
291 } /* match_unary */
293 /*
294 * private matcher types
295 */
297 /* initializes a name matcher consuming two elements from ap */
298 static int
299 name_matcher_init(name_matcher_t *m, va_list ap)
300 {
301 const char *name = va_arg(ap, const char *);
302 const char *name_re = va_arg(ap, const char *);
304 if (name) {
305 m->name = strdup(name);
306 if (! m->name)
307 return -1;
308 }
309 if (name_re) {
310 m->name_re = malloc(sizeof(*m->name_re));
311 if (! m->name_re)
312 return -1;
313 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
314 return -1;
315 }
316 return 0;
317 } /* name_matcher_init */
319 static void
320 name_matcher_destroy(name_matcher_t *m)
321 {
322 if (m->name)
323 free(m->name);
324 if (m->name_re) {
325 regfree(m->name_re);
326 free(m->name_re);
327 }
328 } /* name_matcher_destroy */
330 /* initializes an object matcher consuming two elements from ap */
331 static int
332 obj_matcher_init(sdb_object_t *obj, va_list ap)
333 {
334 obj_matcher_t *m = OBJ_M(obj);
335 M(obj)->type = MATCHER_NAME;
336 return name_matcher_init(&m->name, ap);
337 } /* obj_matcher_init */
339 static void
340 obj_matcher_destroy(sdb_object_t *obj)
341 {
342 obj_matcher_t *m = OBJ_M(obj);
343 name_matcher_destroy(&m->name);
344 } /* obj_matcher_destroy */
346 static int
347 attr_matcher_init(sdb_object_t *obj, va_list ap)
348 {
349 attr_matcher_t *attr = ATTR_M(obj);
350 const char *name = va_arg(ap, const char *);
352 M(obj)->type = MATCHER_ATTR;
353 if (name) {
354 attr->name = strdup(name);
355 if (! attr->name)
356 return -1;
357 }
358 return name_matcher_init(&attr->value, ap);
359 } /* attr_matcher_init */
361 static void
362 attr_matcher_destroy(sdb_object_t *obj)
363 {
364 attr_matcher_t *attr = ATTR_M(obj);
365 if (attr->name)
366 free(attr->name);
367 attr->name = NULL;
368 name_matcher_destroy(&attr->value);
369 } /* attr_matcher_destroy */
371 static int
372 op_matcher_init(sdb_object_t *obj, va_list ap)
373 {
374 M(obj)->type = va_arg(ap, int);
375 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
376 return -1;
378 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
379 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
380 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
381 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
383 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
384 return -1;
385 return 0;
386 } /* op_matcher_init */
388 static void
389 op_matcher_destroy(sdb_object_t *obj)
390 {
391 if (OP_M(obj)->left)
392 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
393 if (OP_M(obj)->right)
394 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
395 } /* op_matcher_destroy */
397 static int
398 uop_matcher_init(sdb_object_t *obj, va_list ap)
399 {
400 M(obj)->type = va_arg(ap, int);
401 if (M(obj)->type != MATCHER_NOT)
402 return -1;
404 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
405 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
407 if (! UOP_M(obj)->op)
408 return -1;
409 return 0;
410 } /* uop_matcher_init */
412 static void
413 uop_matcher_destroy(sdb_object_t *obj)
414 {
415 if (UOP_M(obj)->op)
416 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
417 } /* uop_matcher_destroy */
419 static sdb_type_t name_type = {
420 /* size = */ sizeof(obj_matcher_t),
421 /* init = */ obj_matcher_init,
422 /* destroy = */ obj_matcher_destroy,
423 };
425 static sdb_type_t attr_type = {
426 /* size = */ sizeof(attr_matcher_t),
427 /* init = */ attr_matcher_init,
428 /* destroy = */ attr_matcher_destroy,
429 };
431 static sdb_type_t op_type = {
432 /* size = */ sizeof(op_matcher_t),
433 /* init = */ op_matcher_init,
434 /* destroy = */ op_matcher_destroy,
435 };
437 static sdb_type_t uop_type = {
438 /* size = */ sizeof(uop_matcher_t),
439 /* init = */ uop_matcher_init,
440 /* destroy = */ uop_matcher_destroy,
441 };
443 /*
444 * public API
445 */
447 sdb_store_matcher_t *
448 sdb_store_name_matcher(int type, const char *name, _Bool re)
449 {
450 sdb_store_matcher_t *m;
452 if (re)
453 m = M(sdb_object_create("name-matcher", name_type,
454 NULL, name));
455 else
456 m = M(sdb_object_create("name-matcher", name_type,
457 name, NULL));
459 if (! m)
460 return NULL;
462 OBJ_M(m)->obj_type = type;
463 return m;
464 } /* sdb_store_name_matcher */
466 sdb_store_matcher_t *
467 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
468 {
469 if (re)
470 return M(sdb_object_create("attr-matcher", attr_type,
471 name, NULL, value));
472 return M(sdb_object_create("attr-matcher", attr_type,
473 name, value, NULL));
474 } /* sdb_store_attr_matcher */
476 sdb_store_matcher_t *
477 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
478 const char *op, const char *value)
479 {
480 int type = -1;
481 _Bool inv = 0;
482 _Bool re = 0;
484 sdb_store_matcher_t *m = NULL;
486 if (! strcasecmp(obj_type, "host"))
487 type = SDB_HOST;
488 else if (! strcasecmp(obj_type, "service"))
489 type = SDB_SERVICE;
490 else if (! strcasecmp(obj_type, "attribute"))
491 type = SDB_ATTRIBUTE;
493 /* TODO: support other operators */
494 if (! strcasecmp(op, "=")) {
495 /* nothing to do */
496 }
497 else if (! strcasecmp(op, "!=")) {
498 inv = 1;
499 }
500 else if (! strcasecmp(op, "=~")) {
501 re = 1;
502 }
503 else if (! strcasecmp(op, "!~")) {
504 inv = 1;
505 re = 1;
506 }
507 else
508 return NULL;
510 if (! strcasecmp(attr, "name"))
511 m = sdb_store_name_matcher(type, value, re);
512 else if (type == SDB_ATTRIBUTE)
513 m = sdb_store_attr_matcher(attr, value, re);
515 if (! m)
516 return NULL;
518 if (inv) {
519 sdb_store_matcher_t *tmp;
520 tmp = sdb_store_inv_matcher(m);
521 /* pass ownership to the inverse matcher */
522 sdb_object_deref(SDB_OBJ(m));
523 m = tmp;
524 }
525 return m;
526 } /* sdb_store_matcher_parse_cmp */
528 sdb_store_matcher_t *
529 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
530 {
531 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
532 left, right));
533 } /* sdb_store_dis_matcher */
535 sdb_store_matcher_t *
536 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
537 {
538 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
539 left, right));
540 } /* sdb_store_con_matcher */
542 sdb_store_matcher_t *
543 sdb_store_inv_matcher(sdb_store_matcher_t *m)
544 {
545 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
546 } /* sdb_store_inv_matcher */
548 int
549 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
550 {
551 /* "NULL" always matches */
552 if ((! m) || (! obj))
553 return 1;
555 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
556 return 0;
558 return matchers[m->type](m, obj);
559 } /* sdb_store_matcher_matches */
561 char *
562 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
563 {
564 if (! m)
565 return NULL;
567 if ((m->type < 0)
568 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
569 return NULL;
570 return matchers_tostring[m->type](m, buf, buflen);
571 } /* sdb_store_matcher_tostring */
573 int
574 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
575 void *user_data)
576 {
577 lookup_iter_data_t data = { m, cb, user_data };
579 if (! cb)
580 return -1;
581 return sdb_store_iterate(lookup_iter, &data);
582 } /* sdb_store_lookup */
584 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */