Code

store_lookup: Simplified some internal checks.
[sysdb.git] / src / core / store_lookup.c
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         if ((! m->name) && (! m->name_re))
82                 return 1;
84         if (! name)
85                 name = "";
87         if (m->name && strcasecmp(m->name, name))
88                 return 0;
89         if (m->name_re && regexec(m->name_re, name,
90                                         /* matches */ 0, NULL, /* flags = */ 0))
91                 return 0;
92         return 1;
93 } /* match_string */
95 static int
96 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
97 {
98         int status;
100         assert((m->type == MATCHER_AND) || (m->type == MATCHER_OR));
101         assert(OP_M(m)->left && OP_M(m)->right);
103         status = sdb_store_matcher_matches(OP_M(m)->left, obj);
104         /* lazy evaluation */
105         if ((! status) && (m->type == MATCHER_AND))
106                 return status;
107         else if (status && (m->type == MATCHER_OR))
108                 return status;
110         return sdb_store_matcher_matches(OP_M(m)->right, obj);
111 } /* match_logical */
113 static int
114 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
116         assert(m->type == MATCHER_NOT);
117         assert(UOP_M(m)->op);
119         return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
120 } /* match_unary */
122 static int
123 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
125         sdb_llist_iter_t *iter = NULL;
126         int status = 0;
128         assert(m->type == MATCHER_NAME);
130         switch (NAME_M(m)->obj_type) {
131                 case SDB_HOST:
132                         return match_string(&NAME_M(m)->name, obj->super.name);
133                         break;
134                 case SDB_SERVICE:
135                         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
136                         break;
137                 case SDB_ATTRIBUTE:
138                         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
139                         break;
140         }
142         while (sdb_llist_iter_has_next(iter)) {
143                 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
144                 if (match_string(&NAME_M(m)->name, child->super.name)) {
145                         status = 1;
146                         break;
147                 }
148         }
149         sdb_llist_iter_destroy(iter);
150         return status;
151 } /* match_name */
153 static int
154 match_attr(sdb_store_matcher_t *m, sdb_store_base_t *obj)
156         sdb_llist_iter_t *iter = NULL;
157         int status = 0;
159         assert(m->type == MATCHER_ATTR);
161         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
162         while (sdb_llist_iter_has_next(iter)) {
163                 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(iter));
164                 char buf[sdb_data_strlen(&attr->value) + 1];
166                 if (ATTR_M(m)->name && strcasecmp(ATTR_M(m)->name, SDB_OBJ(attr)->name))
167                         continue;
169                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
170                         return 0;
171                 if (match_string(&ATTR_M(m)->value, buf)) {
172                         status = 1;
173                         break;
174                 }
175         }
176         sdb_llist_iter_destroy(iter);
177         return status;
178 } /* match_attr */
180 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
182 /* this array needs to be indexable by the matcher types;
183  * -> update the enum in store-private.h when updating this */
184 static matcher_cb matchers[] = {
185         match_logical,
186         match_logical,
187         match_unary,
188         match_name,
189         match_attr,
190 };
192 /*
193  * private matcher types
194  */
196 /* initializes a string matcher consuming two elements from ap */
197 static int
198 string_matcher_init(string_matcher_t *m, va_list ap)
200         const char *name = va_arg(ap, const char *);
201         const char *name_re = va_arg(ap, const char *);
203         if (name) {
204                 m->name = strdup(name);
205                 if (! m->name)
206                         return -1;
207         }
208         if (name_re) {
209                 m->name_re = malloc(sizeof(*m->name_re));
210                 if (! m->name_re)
211                         return -1;
212                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
213                         return -1;
214         }
215         return 0;
216 } /* string_matcher_init */
218 static void
219 string_matcher_destroy(string_matcher_t *m)
221         if (m->name)
222                 free(m->name);
223         if (m->name_re) {
224                 regfree(m->name_re);
225                 free(m->name_re);
226         }
227 } /* string_matcher_destroy */
229 static char *
230 string_tostring(string_matcher_t *m, char *buf, size_t buflen)
232         snprintf(buf, buflen, "{ %s%s%s, %p }",
233                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
234                         m->name_re);
235         return buf;
236 } /* string_tostring */
238 /* initializes a name matcher */
239 static int
240 name_matcher_init(sdb_object_t *obj, va_list ap)
242         name_matcher_t *m = NAME_M(obj);
243         M(obj)->type = MATCHER_NAME;
244         return string_matcher_init(&m->name, ap);
245 } /* name_matcher_init */
247 static void
248 name_matcher_destroy(sdb_object_t *obj)
250         name_matcher_t *m = NAME_M(obj);
251         string_matcher_destroy(&m->name);
252 } /* name_matcher_destroy */
254 static char *
255 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
257         char name[buflen + 1];
258         assert(m->type == MATCHER_NAME);
259         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
260                         SDB_STORE_TYPE_TO_NAME(NAME_M(m)->obj_type),
261                         string_tostring(&NAME_M(m)->name, name, sizeof(name)));
262         return buf;
263 } /* name_tostring */
265 static int
266 attr_matcher_init(sdb_object_t *obj, va_list ap)
268         attr_matcher_t *attr = ATTR_M(obj);
269         const char *name = va_arg(ap, const char *);
271         M(obj)->type = MATCHER_ATTR;
272         if (name) {
273                 attr->name = strdup(name);
274                 if (! attr->name)
275                         return -1;
276         }
277         return string_matcher_init(&attr->value, ap);
278 } /* attr_matcher_init */
280 static void
281 attr_matcher_destroy(sdb_object_t *obj)
283         attr_matcher_t *attr = ATTR_M(obj);
284         if (attr->name)
285                 free(attr->name);
286         attr->name = NULL;
287         string_matcher_destroy(&attr->value);
288 } /* attr_matcher_destroy */
290 static char *
291 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
293         char value[buflen + 1];
295         if (! m) {
296                 snprintf(buf, buflen, "ATTR{}");
297                 return buf;
298         }
300         assert(m->type == MATCHER_ATTR);
301         snprintf(buf, buflen, "ATTR[%s]{ VALUE%s }", ATTR_M(m)->name,
302                         string_tostring(&ATTR_M(m)->value, value, sizeof(value)));
303         return buf;
304 } /* attr_tostring */
306 static int
307 op_matcher_init(sdb_object_t *obj, va_list ap)
309         M(obj)->type = va_arg(ap, int);
310         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
311                 return -1;
313         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
314         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
315         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
316         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
318         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
319                 return -1;
320         return 0;
321 } /* op_matcher_init */
323 static void
324 op_matcher_destroy(sdb_object_t *obj)
326         if (OP_M(obj)->left)
327                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
328         if (OP_M(obj)->right)
329                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
330 } /* op_matcher_destroy */
332 static char *
333 op_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
335         char left[buflen + 1], right[buflen + 1];
337         if (! m) {
338                 /* this should not happen */
339                 snprintf(buf, buflen, "()");
340                 return buf;
341         }
343         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
344         snprintf(buf, buflen, "(%s, %s, %s)",
345                         m->type == MATCHER_OR ? "OR" : "AND",
346                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
347                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
348         return buf;
349 } /* op_tostring */
351 static int
352 uop_matcher_init(sdb_object_t *obj, va_list ap)
354         M(obj)->type = va_arg(ap, int);
355         if (M(obj)->type != MATCHER_NOT)
356                 return -1;
358         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
359         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
361         if (! UOP_M(obj)->op)
362                 return -1;
363         return 0;
364 } /* uop_matcher_init */
366 static void
367 uop_matcher_destroy(sdb_object_t *obj)
369         if (UOP_M(obj)->op)
370                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
371 } /* uop_matcher_destroy */
373 static char *
374 uop_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
376         char op[buflen + 1];
378         if (! m) {
379                 /* this should not happen */
380                 snprintf(buf, buflen, "()");
381                 return buf;
382         }
384         assert(m->type == MATCHER_NOT);
385         snprintf(buf, buflen, "(NOT, %s)",
386                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
387         return buf;
388 } /* uop_tostring */
390 static sdb_type_t name_type = {
391         /* size = */ sizeof(name_matcher_t),
392         /* init = */ name_matcher_init,
393         /* destroy = */ name_matcher_destroy,
394 };
396 static sdb_type_t attr_type = {
397         /* size = */ sizeof(attr_matcher_t),
398         /* init = */ attr_matcher_init,
399         /* destroy = */ attr_matcher_destroy,
400 };
402 static sdb_type_t op_type = {
403         /* size = */ sizeof(op_matcher_t),
404         /* init = */ op_matcher_init,
405         /* destroy = */ op_matcher_destroy,
406 };
408 static sdb_type_t uop_type = {
409         /* size = */ sizeof(uop_matcher_t),
410         /* init = */ uop_matcher_init,
411         /* destroy = */ uop_matcher_destroy,
412 };
414 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
416 /* this array needs to be indexable by the matcher types;
417  * -> update the enum in store-private.h when updating this */
418 static matcher_tostring_cb matchers_tostring[] = {
419         op_tostring,
420         op_tostring,
421         uop_tostring,
422         name_tostring,
423         attr_tostring,
424 };
426 /*
427  * public API
428  */
430 sdb_store_matcher_t *
431 sdb_store_name_matcher(int type, const char *name, _Bool re)
433         sdb_store_matcher_t *m;
435         if (re)
436                 m = M(sdb_object_create("name-matcher", name_type,
437                                         NULL, name));
438         else
439                 m = M(sdb_object_create("name-matcher", name_type,
440                                         name, NULL));
442         if (! m)
443                 return NULL;
445         NAME_M(m)->obj_type = type;
446         return m;
447 } /* sdb_store_name_matcher */
449 sdb_store_matcher_t *
450 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
452         if (re)
453                 return M(sdb_object_create("attr-matcher", attr_type,
454                                         name, NULL, value));
455         return M(sdb_object_create("attr-matcher", attr_type,
456                                 name, value, NULL));
457 } /* sdb_store_attr_matcher */
459 sdb_store_matcher_t *
460 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
461                 const char *op, const char *value)
463         int type = -1;
464         _Bool inv = 0;
465         _Bool re = 0;
467         sdb_store_matcher_t *m = NULL;
469         if (! strcasecmp(obj_type, "host"))
470                 type = SDB_HOST;
471         else if (! strcasecmp(obj_type, "service"))
472                 type = SDB_SERVICE;
473         else if (! strcasecmp(obj_type, "attribute"))
474                 type = SDB_ATTRIBUTE;
476         /* TODO: support other operators */
477         if (! strcasecmp(op, "=")) {
478                 /* nothing to do */
479         }
480         else if (! strcasecmp(op, "!=")) {
481                 inv = 1;
482         }
483         else if (! strcasecmp(op, "=~")) {
484                 re = 1;
485         }
486         else if (! strcasecmp(op, "!~")) {
487                 inv = 1;
488                 re = 1;
489         }
490         else
491                 return NULL;
493         if (! strcasecmp(attr, "name"))
494                 m = sdb_store_name_matcher(type, value, re);
495         else if (type == SDB_ATTRIBUTE)
496                 m = sdb_store_attr_matcher(attr, value, re);
498         if (! m)
499                 return NULL;
501         if (inv) {
502                 sdb_store_matcher_t *tmp;
503                 tmp = sdb_store_inv_matcher(m);
504                 /* pass ownership to the inverse matcher */
505                 sdb_object_deref(SDB_OBJ(m));
506                 m = tmp;
507         }
508         return m;
509 } /* sdb_store_matcher_parse_cmp */
511 sdb_store_matcher_t *
512 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
514         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
515                                 left, right));
516 } /* sdb_store_dis_matcher */
518 sdb_store_matcher_t *
519 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
521         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
522                                 left, right));
523 } /* sdb_store_con_matcher */
525 sdb_store_matcher_t *
526 sdb_store_inv_matcher(sdb_store_matcher_t *m)
528         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
529 } /* sdb_store_inv_matcher */
531 int
532 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
534         if (obj->type != SDB_HOST)
535                 return 0;
537         /* "NULL" always matches */
538         if ((! m) || (! obj))
539                 return 1;
541         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
542                 return 0;
544         return matchers[m->type](m, obj);
545 } /* sdb_store_matcher_matches */
547 char *
548 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
550         if (! m)
551                 return NULL;
553         if ((m->type < 0)
554                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
555                 return NULL;
556         return matchers_tostring[m->type](m, buf, buflen);
557 } /* sdb_store_matcher_tostring */
559 int
560 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
561                 void *user_data)
563         lookup_iter_data_t data = { m, cb, user_data };
565         if (! cb)
566                 return -1;
567         return sdb_store_iterate(lookup_iter, &data);
568 } /* sdb_store_lookup */
570 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */