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 static sdb_store_base_t *
75 attr_get(sdb_store_base_t *host, const char *name)
76 {
77 sdb_llist_iter_t *iter = NULL;
78 sdb_store_base_t *attr = NULL;
80 assert(host->type == SDB_HOST);
82 iter = sdb_llist_get_iter(SDB_STORE_OBJ(host)->attributes);
83 while (sdb_llist_iter_has_next(iter)) {
84 sdb_attribute_t *a = SDB_ATTR(sdb_llist_iter_get_next(iter));
86 if (strcasecmp(name, SDB_OBJ(a)->name))
87 continue;
88 attr = STORE_BASE(a);
89 break;
90 }
91 sdb_llist_iter_destroy(iter);
92 return attr;
93 } /* attr_get */
95 /*
96 * matcher implementations
97 */
99 static int
100 match_string(string_matcher_t *m, const char *name)
101 {
102 if ((! m->name) && (! m->name_re))
103 return 1;
105 if (! name)
106 name = "";
108 if (m->name && strcasecmp(m->name, name))
109 return 0;
110 if (m->name_re && regexec(m->name_re, name,
111 /* matches */ 0, NULL, /* flags = */ 0))
112 return 0;
113 return 1;
114 } /* match_string */
116 static int
117 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
118 {
119 int status;
121 assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
122 assert(OP_M(m)->left && OP_M(m)->right);
124 status = sdb_store_matcher_matches(OP_M(m)->left, obj);
125 /* lazy evaluation */
126 if ((! status) && (m->type == MATCHER_AND))
127 return status;
128 else if (status && (m->type == MATCHER_OR))
129 return status;
131 return sdb_store_matcher_matches(OP_M(m)->right, obj);
132 } /* match_logical */
134 static int
135 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
136 {
137 assert(m->type == MATCHER_NOT);
138 assert(UOP_M(m)->op);
140 return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
141 } /* match_unary */
143 static int
144 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
145 {
146 sdb_llist_iter_t *iter = NULL;
147 int status = 0;
149 assert(m->type == MATCHER_NAME);
151 switch (NAME_M(m)->obj_type) {
152 case SDB_HOST:
153 return match_string(&NAME_M(m)->name, obj->super.name);
154 break;
155 case SDB_SERVICE:
156 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
157 break;
158 case SDB_ATTRIBUTE:
159 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
160 break;
161 }
163 while (sdb_llist_iter_has_next(iter)) {
164 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
165 if (match_string(&NAME_M(m)->name, child->super.name)) {
166 status = 1;
167 break;
168 }
169 }
170 sdb_llist_iter_destroy(iter);
171 return status;
172 } /* match_name */
174 static int
175 match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
176 {
177 sdb_attribute_t *attr;
179 assert(m->type == MATCHER_ATTR);
180 assert(ATTR_M(m)->name);
182 attr = SDB_ATTR(attr_get(obj, ATTR_M(m)->name));
183 if (attr) {
184 char buf[sdb_data_strlen(&attr->value) + 1];
185 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
186 return 0;
187 if (match_string(&ATTR_M(m)->value, buf))
188 return 1;
189 }
190 return 0;
191 } /* match_attr */
193 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
195 /* this array needs to be indexable by the matcher types;
196 * -> update the enum in store-private.h when updating this */
197 static matcher_cb matchers[] = {
198 match_logical,
199 match_logical,
200 match_unary,
201 match_name,
202 match_attr,
203 };
205 /*
206 * private matcher types
207 */
209 /* initializes a string matcher consuming two elements from ap */
210 static int
211 string_matcher_init(string_matcher_t *m, va_list ap)
212 {
213 const char *name = va_arg(ap, const char *);
214 const char *name_re = va_arg(ap, const char *);
216 if (name) {
217 m->name = strdup(name);
218 if (! m->name)
219 return -1;
220 }
221 if (name_re) {
222 m->name_re = malloc(sizeof(*m->name_re));
223 if (! m->name_re)
224 return -1;
225 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
226 return -1;
227 }
228 return 0;
229 } /* string_matcher_init */
231 static void
232 string_matcher_destroy(string_matcher_t *m)
233 {
234 if (m->name)
235 free(m->name);
236 if (m->name_re) {
237 regfree(m->name_re);
238 free(m->name_re);
239 }
240 } /* string_matcher_destroy */
242 static char *
243 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
244 {
245 snprintf(buf, buflen, "{ %s%s%s, %p }",
246 m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
247 m->name_re);
248 return buf;
249 } /* string_tostring */
251 /* initializes a name matcher */
252 static int
253 name_matcher_init(sdb_object_t *obj, va_list ap)
254 {
255 name_matcher_t *m = NAME_M(obj);
256 M(obj)->type = MATCHER_NAME;
257 return string_matcher_init(&m->name, ap);
258 } /* name_matcher_init */
260 static void
261 name_matcher_destroy(sdb_object_t *obj)
262 {
263 name_matcher_t *m = NAME_M(obj);
264 string_matcher_destroy(&m->name);
265 } /* name_matcher_destroy */
267 static char *
268 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
269 {
270 char name[buflen + 1];
271 assert(m->type == MATCHER_NAME);
272 snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
273 SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
274 string_tostring(&NAME_M(m)->name, name, sizeof(name)));
275 return buf;
276 } /* name_tostring */
278 static int
279 attr_matcher_init(sdb_object_t *obj, va_list ap)
280 {
281 attr_matcher_t *attr = ATTR_M(obj);
282 const char *name = va_arg(ap, const char *);
284 M(obj)->type = MATCHER_ATTR;
285 if (name) {
286 attr->name = strdup(name);
287 if (! attr->name)
288 return -1;
289 }
290 return string_matcher_init(&attr->value, ap);
291 } /* attr_matcher_init */
293 static void
294 attr_matcher_destroy(sdb_object_t *obj)
295 {
296 attr_matcher_t *attr = ATTR_M(obj);
297 if (attr->name)
298 free(attr->name);
299 attr->name = NULL;
300 string_matcher_destroy(&attr->value);
301 } /* attr_matcher_destroy */
303 static char *
304 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
305 {
306 char value[buflen + 1];
308 if (! m) {
309 snprintf(buf, buflen, "ATTR{}");
310 return buf;
311 }
313 assert(m->type == MATCHER_ATTR);
314 snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
315 string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
316 return buf;
317 } /* attr_tostring */
319 static int
320 op_matcher_init(sdb_object_t *obj, va_list ap)
321 {
322 M(obj)->type = va_arg(ap, int);
323 if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
324 return -1;
326 OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
327 sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
328 OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
329 sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
331 if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
332 return -1;
333 return 0;
334 } /* op_matcher_init */
336 static void
337 op_matcher_destroy(sdb_object_t *obj)
338 {
339 if (OP_M(obj)->left)
340 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
341 if (OP_M(obj)->right)
342 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
343 } /* op_matcher_destroy */
345 static char *
346 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
347 {
348 char left[buflen + 1], right[buflen + 1];
350 if (! m) {
351 /* this should not happen */
352 snprintf(buf, buflen, "()");
353 return buf;
354 }
356 assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
357 snprintf(buf, buflen, "(%s, %s, %s)",
358 m->type == MATCHER_OR ? "OR" : "AND",
359 sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
360 sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
361 return buf;
362 } /* op_tostring */
364 static int
365 uop_matcher_init(sdb_object_t *obj, va_list ap)
366 {
367 M(obj)->type = va_arg(ap, int);
368 if (M(obj)->type != MATCHER_NOT)
369 return -1;
371 UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
372 sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
374 if (! UOP_M(obj)->op)
375 return -1;
376 return 0;
377 } /* uop_matcher_init */
379 static void
380 uop_matcher_destroy(sdb_object_t *obj)
381 {
382 if (UOP_M(obj)->op)
383 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
384 } /* uop_matcher_destroy */
386 static char *
387 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
388 {
389 char op[buflen + 1];
391 if (! m) {
392 /* this should not happen */
393 snprintf(buf, buflen, "()");
394 return buf;
395 }
397 assert(m->type == MATCHER_NOT);
398 snprintf(buf, buflen, "(NOT, %s)",
399 sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
400 return buf;
401 } /* uop_tostring */
403 static sdb_type_t name_type = {
404 /* size = */ sizeof(name_matcher_t),
405 /* init = */ name_matcher_init,
406 /* destroy = */ name_matcher_destroy,
407 };
409 static sdb_type_t attr_type = {
410 /* size = */ sizeof(attr_matcher_t),
411 /* init = */ attr_matcher_init,
412 /* destroy = */ attr_matcher_destroy,
413 };
415 static sdb_type_t op_type = {
416 /* size = */ sizeof(op_matcher_t),
417 /* init = */ op_matcher_init,
418 /* destroy = */ op_matcher_destroy,
419 };
421 static sdb_type_t uop_type = {
422 /* size = */ sizeof(uop_matcher_t),
423 /* init = */ uop_matcher_init,
424 /* destroy = */ uop_matcher_destroy,
425 };
427 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
429 /* this array needs to be indexable by the matcher types;
430 * -> update the enum in store-private.h when updating this */
431 static matcher_tostring_cb matchers_tostring[] = {
432 op_tostring,
433 op_tostring,
434 uop_tostring,
435 name_tostring,
436 attr_tostring,
437 };
439 /*
440 * public API
441 */
443 sdb_store_matcher_t *
444 sdb_store_name_matcher(int type, const char *name, _Bool re)
445 {
446 sdb_store_matcher_t *m;
448 if (re)
449 m = M(sdb_object_create("name-matcher", name_type,
450 NULL, name));
451 else
452 m = M(sdb_object_create("name-matcher", name_type,
453 name, NULL));
455 if (! m)
456 return NULL;
458 NAME_M(m)->obj_type = type;
459 return m;
460 } /* sdb_store_name_matcher */
462 sdb_store_matcher_t *
463 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
464 {
465 if (! name)
466 return NULL;
468 if (re)
469 return M(sdb_object_create("attr-matcher", attr_type,
470 name, NULL, value));
471 return M(sdb_object_create("attr-matcher", attr_type,
472 name, value, NULL));
473 } /* sdb_store_attr_matcher */
475 sdb_store_matcher_t *
476 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
477 const char *op, const char *value)
478 {
479 int type = -1;
480 _Bool inv = 0;
481 _Bool re = 0;
483 sdb_store_matcher_t *m = NULL;
485 if (! strcasecmp(obj_type, "host"))
486 type = SDB_HOST;
487 else if (! strcasecmp(obj_type, "service"))
488 type = SDB_SERVICE;
489 else if (! strcasecmp(obj_type, "attribute"))
490 type = SDB_ATTRIBUTE;
492 /* TODO: support other operators */
493 if (! strcasecmp(op, "=")) {
494 /* nothing to do */
495 }
496 else if (! strcasecmp(op, "!=")) {
497 inv = 1;
498 }
499 else if (! strcasecmp(op, "=~")) {
500 re = 1;
501 }
502 else if (! strcasecmp(op, "!~")) {
503 inv = 1;
504 re = 1;
505 }
506 else
507 return NULL;
509 if (! strcasecmp(attr, "name"))
510 m = sdb_store_name_matcher(type, value, re);
511 else if (type == SDB_ATTRIBUTE)
512 m = sdb_store_attr_matcher(attr, value, re);
514 if (! m)
515 return NULL;
517 if (inv) {
518 sdb_store_matcher_t *tmp;
519 tmp = sdb_store_inv_matcher(m);
520 /* pass ownership to the inverse matcher */
521 sdb_object_deref(SDB_OBJ(m));
522 m = tmp;
523 }
524 return m;
525 } /* sdb_store_matcher_parse_cmp */
527 sdb_store_matcher_t *
528 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
529 {
530 return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
531 left, right));
532 } /* sdb_store_dis_matcher */
534 sdb_store_matcher_t *
535 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
536 {
537 return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
538 left, right));
539 } /* sdb_store_con_matcher */
541 sdb_store_matcher_t *
542 sdb_store_inv_matcher(sdb_store_matcher_t *m)
543 {
544 return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
545 } /* sdb_store_inv_matcher */
547 int
548 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
549 {
550 if (obj->type != SDB_HOST)
551 return 0;
553 /* "NULL" always matches */
554 if ((! m) || (! obj))
555 return 1;
557 if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
558 return 0;
560 return matchers[m->type](m, obj);
561 } /* sdb_store_matcher_matches */
563 char *
564 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
565 {
566 if (! m)
567 return NULL;
569 if ((m->type < 0)
570 || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
571 return NULL;
572 return matchers_tostring[m->type](m, buf, buflen);
573 } /* sdb_store_matcher_tostring */
575 int
576 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
577 void *user_data)
578 {
579 lookup_iter_data_t data = { m, cb, user_data };
581 if (! cb)
582 return -1;
583 return sdb_store_iterate(lookup_iter, &data);
584 } /* sdb_store_lookup */
586 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */