52739a6a836155fd3ba465a688cb5db222f4aa91
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_string(string_matcher_t *m, const char *name)
80 {
81 assert(m);
83 if ((! m->name) && (! m->name_re))
84 return 1;
86 if (! name)
87 name = "";
89 if (m->name && strcasecmp(m->name, name))
90 return 0;
91 if (m->name_re && regexec(m->name_re, name,
92 /* matches */ 0, NULL, /* flags = */ 0))
93 return 0;
94 return 1;
95 } /* match_string */
97 static int
98 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
99 {
100 int status;
102 assert(m && obj);
103 assert(OP_M(m)->left && OP_M(m)->right);
105 status = sdb_store_matcher_matches(OP_M(m)->left, obj);
106 /* lazy evaluation */
107 if ((! status) && (m->type == MATCHER_AND))
108 return status;
109 else if (status && (m->type == MATCHER_OR))
110 return status;
112 return sdb_store_matcher_matches(OP_M(m)->right, obj);
113 } /* match_logical */
115 static int
116 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
117 {
118 assert(m && obj);
119 assert(UOP_M(m)->op);
121 assert(m->type == MATCHER_NOT);
123 return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
124 } /* match_unary */
126 static int
127 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
128 {
129 sdb_llist_iter_t *iter = NULL;
130 int status = 0;
132 assert(m && obj);
133 assert(m->type == MATCHER_NAME);
135 if (obj->type != SDB_HOST)
136 return 0;
138 switch (NAME_M(m)->obj_type) {
139 case SDB_HOST:
140 return match_string(&NAME_M(m)->name, obj->super.name);
141 break;
142 case SDB_SERVICE:
143 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
144 break;
145 case SDB_ATTRIBUTE:
146 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
147 break;
148 }
150 while (sdb_llist_iter_has_next(iter)) {
151 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
152 if (match_string(&NAME_M(m)->name, child->super.name)) {
153 status = 1;
154 break;
155 }
156 }
157 sdb_llist_iter_destroy(iter);
158 return status;
159 } /* match_name */
161 static int
162 match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
163 {
164 sdb_llist_iter_t *iter = NULL;
165 int status = 0;
167 assert(m && obj);
168 assert(m->type == MATCHER_ATTR);
170 if (obj->type != SDB_HOST)
171 return 0;
173 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
174 while (sdb_llist_iter_has_next(iter)) {
175 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
176 char buf[sdb_data_strlen(&attr->value) + 1];
178 if (ATTR_M(m)->name && strcasecmp(ATTR_M(m)->name, SDB_OBJ(attr)->name))
179 continue;
181 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
182 return 0;
183 if (match_string(&ATTR_M(m)->value, buf)) {
184 status = 1;
185 break;
186 }
187 }
188 sdb_llist_iter_destroy(iter);
189 return status;
190 } /* match_attr */
192 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
194 /* this array needs to be indexable by the matcher types;
195 * -> update the enum in store-private.h when updating this */
196 static matcher_cb matchers[] = {
197 match_logical,
198 match_logical,
199 match_unary,
200 match_name,
201 match_attr,
202 };
204 /*
205 * private matcher types
206 */
208 /* initializes a string matcher consuming two elements from ap */
209 static int
210 string_matcher_init(string_matcher_t *m, va_list ap)
211 {
212 const char *name = va_arg(ap, const char *);
213 const char *name_re = va_arg(ap, const char *);
215 if (name) {
216 m->name = strdup(name);
217 if (! m->name)
218 return -1;
219 }
220 if (name_re) {
221 m->name_re = malloc(sizeof(*m->name_re));
222 if (! m->name_re)
223 return -1;
224 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
225 return -1;
226 }
227 return 0;
228 } /* string_matcher_init */
230 static void
231 string_matcher_destroy(string_matcher_t *m)
232 {
233 if (m->name)
234 free(m->name);
235 if (m->name_re) {
236 regfree(m->name_re);
237 free(m->name_re);
238 }
239 } /* string_matcher_destroy */
241 static char *
242 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
243 {
244 snprintf(buf, buflen, "{ %s%s%s, %p }",
245 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
246 m->name_re);
247 return buf;
248 } /* string_tostring */
250 /* initializes a name matcher */
251 static int
252 name_matcher_init(sdb_object_t *obj, va_list ap)
253 {
254 name_matcher_t *m = NAME_M(obj);
255 M(obj)->type = MATCHER_NAME;
256 return string_matcher_init(&m->name, ap);
257 } /* name_matcher_init */
259 static void
260 name_matcher_destroy(sdb_object_t *obj)
261 {
262 name_matcher_t *m = NAME_M(obj);
263 string_matcher_destroy(&m->name);
264 } /* name_matcher_destroy */
266 static char *
267 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
268 {
269 char name[buflen + 1];
270 assert(m->type == MATCHER_NAME);
271 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
272 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
273 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
274 return buf;
275 } /* name_tostring */
277 static int
278 attr_matcher_init(sdb_object_t *obj, va_list ap)
279 {
280 attr_matcher_t *attr = ATTR_M(obj);
281 const char *name = va_arg(ap, const char *);
283 M(obj)->type = MATCHER_ATTR;
284 if (name) {
285 attr->name = strdup(name);
286 if (! attr->name)
287 return -1;
288 }
289 return string_matcher_init(&attr->value, ap);
290 } /* attr_matcher_init */
292 static void
293 attr_matcher_destroy(sdb_object_t *obj)
294 {
295 attr_matcher_t *attr = ATTR_M(obj);
296 if (attr->name)
297 free(attr->name);
298 attr->name = NULL;
299 string_matcher_destroy(&attr->value);
300 } /* attr_matcher_destroy */
302 static char *
303 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
304 {
305 char value[buflen + 1];
307 if (! m) {
308 snprintf(buf, buflen, "ATTR{}");
309 return buf;
310 }
312 assert(m->type == MATCHER_ATTR);
313 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
314 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
315 return buf;
316 } /* attr_tostring */
318 static int
319 op_matcher_init(sdb_object_t *obj, va_list ap)
320 {
321 M(obj)->type = va_arg(ap, int);
322 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
323 return -1;
325 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
326 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
327 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
328 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
330 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
331 return -1;
332 return 0;
333 } /* op_matcher_init */
335 static void
336 op_matcher_destroy(sdb_object_t *obj)
337 {
338 if (OP_M(obj)->left)
339 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
340 if (OP_M(obj)->right)
341 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
342 } /* op_matcher_destroy */
344 static char *
345 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
346 {
347 char left[buflen + 1], right[buflen + 1];
349 if (! m) {
350 /* this should not happen */
351 snprintf(buf, buflen, "()");
352 return buf;
353 }
355 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
356 snprintf(buf, buflen, "(%s, %s, %s)",
357 m->type == MATCHER_OR ? "OR" : "AND",
358 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
359 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
360 return buf;
361 } /* op_tostring */
363 static int
364 uop_matcher_init(sdb_object_t *obj, va_list ap)
365 {
366 M(obj)->type = va_arg(ap, int);
367 if (M(obj)->type != MATCHER_NOT)
368 return -1;
370 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
371 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
373 if (! UOP_M(obj)->op)
374 return -1;
375 return 0;
376 } /* uop_matcher_init */
378 static void
379 uop_matcher_destroy(sdb_object_t *obj)
380 {
381 if (UOP_M(obj)->op)
382 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
383 } /* uop_matcher_destroy */
385 static char *
386 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
387 {
388 char op[buflen + 1];
390 if (! m) {
391 /* this should not happen */
392 snprintf(buf, buflen, "()");
393 return buf;
394 }
396 assert(m->type == MATCHER_NOT);
397 snprintf(buf, buflen, "(NOT, %s)",
398 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
399 return buf;
400 } /* uop_tostring */
402 static sdb_type_t name_type = {
403 /* size = */ sizeof(name_matcher_t),
404 /* init = */ name_matcher_init,
405 /* destroy = */ name_matcher_destroy,
406 };
408 static sdb_type_t attr_type = {
409 /* size = */ sizeof(attr_matcher_t),
410 /* init = */ attr_matcher_init,
411 /* destroy = */ attr_matcher_destroy,
412 };
414 static sdb_type_t op_type = {
415 /* size = */ sizeof(op_matcher_t),
416 /* init = */ op_matcher_init,
417 /* destroy = */ op_matcher_destroy,
418 };
420 static sdb_type_t uop_type = {
421 /* size = */ sizeof(uop_matcher_t),
422 /* init = */ uop_matcher_init,
423 /* destroy = */ uop_matcher_destroy,
424 };
426 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
428 /* this array needs to be indexable by the matcher types;
429 * -> update the enum in store-private.h when updating this */
430 static matcher_tostring_cb matchers_tostring[] = {
431 op_tostring,
432 op_tostring,
433 uop_tostring,
434 name_tostring,
435 attr_tostring,
436 };
438 /*
439 * public API
440 */
442 sdb_store_matcher_t *
443 sdb_store_name_matcher(int type, const char *name, _Bool re)
444 {
445 sdb_store_matcher_t *m;
447 if (re)
448 m = M(sdb_object_create("name-matcher", name_type,
449 NULL, name));
450 else
451 m = M(sdb_object_create("name-matcher", name_type,
452 name, NULL));
454 if (! m)
455 return NULL;
457 NAME_M(m)->obj_type = type;
458 return m;
459 } /* sdb_store_name_matcher */
461 sdb_store_matcher_t *
462 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
463 {
464 if (re)
465 return M(sdb_object_create("attr-matcher", attr_type,
466 name, NULL, value));
467 return M(sdb_object_create("attr-matcher", attr_type,
468 name, value, NULL));
469 } /* sdb_store_attr_matcher */
471 sdb_store_matcher_t *
472 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
473 const char *op, const char *value)
474 {
475 int type = -1;
476 _Bool inv = 0;
477 _Bool re = 0;
479 sdb_store_matcher_t *m = NULL;
481 if (! strcasecmp(obj_type, "host"))
482 type = SDB_HOST;
483 else if (! strcasecmp(obj_type, "service"))
484 type = SDB_SERVICE;
485 else if (! strcasecmp(obj_type, "attribute"))
486 type = SDB_ATTRIBUTE;
488 /* TODO: support other operators */
489 if (! strcasecmp(op, "=")) {
490 /* nothing to do */
491 }
492 else if (! strcasecmp(op, "!=")) {
493 inv = 1;
494 }
495 else if (! strcasecmp(op, "=~")) {
496 re = 1;
497 }
498 else if (! strcasecmp(op, "!~")) {
499 inv = 1;
500 re = 1;
501 }
502 else
503 return NULL;
505 if (! strcasecmp(attr, "name"))
506 m = sdb_store_name_matcher(type, value, re);
507 else if (type == SDB_ATTRIBUTE)
508 m = sdb_store_attr_matcher(attr, value, re);
510 if (! m)
511 return NULL;
513 if (inv) {
514 sdb_store_matcher_t *tmp;
515 tmp = sdb_store_inv_matcher(m);
516 /* pass ownership to the inverse matcher */
517 sdb_object_deref(SDB_OBJ(m));
518 m = tmp;
519 }
520 return m;
521 } /* sdb_store_matcher_parse_cmp */
523 sdb_store_matcher_t *
524 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
525 {
526 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
527 left, right));
528 } /* sdb_store_dis_matcher */
530 sdb_store_matcher_t *
531 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
532 {
533 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
534 left, right));
535 } /* sdb_store_con_matcher */
537 sdb_store_matcher_t *
538 sdb_store_inv_matcher(sdb_store_matcher_t *m)
539 {
540 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
541 } /* sdb_store_inv_matcher */
543 int
544 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
545 {
546 /* "NULL" always matches */
547 if ((! m) || (! obj))
548 return 1;
550 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
551 return 0;
553 return matchers[m->type](m, obj);
554 } /* sdb_store_matcher_matches */
556 char *
557 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
558 {
559 if (! m)
560 return NULL;
562 if ((m->type < 0)
563 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
564 return NULL;
565 return matchers_tostring[m->type](m, buf, buflen);
566 } /* sdb_store_matcher_tostring */
568 int
569 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
570 void *user_data)
571 {
572 lookup_iter_data_t data = { m, cb, user_data };
574 if (! cb)
575 return -1;
576 return sdb_store_iterate(lookup_iter, &data);
577 } /* sdb_store_lookup */
579 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */