Code

fe98e91dd62c7a72e3509a61e07e0ee610ea223a
[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_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)
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)
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)
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)
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)
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)
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)
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)
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 (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
247                         return 0;
248                 if (match_obj_name(&ATTR_M(m)->value, buf)) {
249                         status = 1;
250                         break;
251                 }
252         }
253         sdb_llist_iter_destroy(iter);
254         return status;
255 } /* match_attr */
257 /* match host specific values;
258  * always call this function through match_obj() */
259 static int
260 match_host(sdb_store_matcher_t *m, sdb_store_base_t *obj)
262         int status;
264         assert(m && obj);
265         assert(m->type == MATCHER_HOST);
267         if (obj->type != SDB_HOST)
268                 return 0;
270         status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
271         if (! status)
272                 return status;
274         if (! HOST_M(m)->attr)
275                 return 1;
277         return match_attr(M(HOST_M(m)->attr), obj);
278 } /* match_host */
280 /* generic matchers */
282 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
284 /* this array needs to be indexable by the matcher types;
285  * -> update the enum in store-private.h when updating this */
286 static matcher_cb matchers[] = {
287         match_logical,
288         match_logical,
289         match_unary,
290         match_name,
291         match_attr,
292         match_host,
293 };
295 typedef char *(*matcher_tostring_cb)(sdb_store_matcher_t *, char *, size_t);
297 static matcher_tostring_cb matchers_tostring[] = {
298         logical_tostring,
299         logical_tostring,
300         unary_tostring,
301         name_tostring,
302         attr_tostring,
303         host_tostring,
304 };
306 static int
307 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
309         int status;
311         assert(m && obj);
312         assert(OP_M(m)->left && OP_M(m)->right);
314         status = sdb_store_matcher_matches(OP_M(m)->left, obj);
315         /* lazy evaluation */
316         if ((! status) && (m->type == MATCHER_AND))
317                 return status;
318         else if (status && (m->type == MATCHER_OR))
319                 return status;
321         return sdb_store_matcher_matches(OP_M(m)->right, obj);
322 } /* match_logical */
324 static int
325 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
327         assert(m && obj);
328         assert(UOP_M(m)->op);
330         return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
331 } /* match_unary */
333 /*
334  * private matcher types
335  */
337 /* initializes a name matcher consuming two elements from ap */
338 static int
339 name_matcher_init(name_matcher_t *m, va_list ap)
341         const char *name = va_arg(ap, const char *);
342         const char *name_re = va_arg(ap, const char *);
344         if (name) {
345                 m->name = strdup(name);
346                 if (! m->name)
347                         return -1;
348         }
349         if (name_re) {
350                 m->name_re = malloc(sizeof(*m->name_re));
351                 if (! m->name_re)
352                         return -1;
353                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
354                         return -1;
355         }
356         return 0;
357 } /* name_matcher_init */
359 static void
360 name_matcher_destroy(name_matcher_t *m)
362         if (m->name)
363                 free(m->name);
364         if (m->name_re) {
365                 regfree(m->name_re);
366                 free(m->name_re);
367         }
368 } /* name_matcher_destroy */
370 /* initializes an object matcher consuming two elements from ap */
371 static int
372 obj_matcher_init(sdb_object_t *obj, va_list ap)
374         obj_matcher_t *m = OBJ_M(obj);
375         M(obj)->type = MATCHER_NAME;
376         return name_matcher_init(&m->name, ap);
377 } /* obj_matcher_init */
379 static void
380 obj_matcher_destroy(sdb_object_t *obj)
382         obj_matcher_t *m = OBJ_M(obj);
383         name_matcher_destroy(&m->name);
384 } /* obj_matcher_destroy */
386 static int
387 attr_matcher_init(sdb_object_t *obj, va_list ap)
389         attr_matcher_t *attr = ATTR_M(obj);
390         const char *name = va_arg(ap, const char *);
392         M(obj)->type = MATCHER_ATTR;
393         if (name) {
394                 attr->name = strdup(name);
395                 if (! attr->name)
396                         return -1;
397         }
398         return name_matcher_init(&attr->value, ap);
399 } /* attr_matcher_init */
401 static void
402 attr_matcher_destroy(sdb_object_t *obj)
404         attr_matcher_t *attr = ATTR_M(obj);
405         if (attr->name)
406                 free(attr->name);
407         attr->name = NULL;
408         name_matcher_destroy(&attr->value);
409 } /* attr_matcher_destroy */
411 static int
412 host_matcher_init(sdb_object_t *obj, va_list ap)
414         attr_matcher_t *attr;
415         int status;
417         status = obj_matcher_init(obj, ap);
418         if (status)
419                 return status;
421         attr = va_arg(ap, attr_matcher_t *);
422         sdb_object_ref(SDB_OBJ(attr));
423         HOST_M(obj)->attr = attr;
425         M(obj)->type = MATCHER_HOST;
426         return 0;
427 } /* host_matcher_init */
429 static void
430 host_matcher_destroy(sdb_object_t *obj)
432         obj_matcher_destroy(obj);
433         sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
434 } /* host_matcher_destroy */
436 static int
437 op_matcher_init(sdb_object_t *obj, va_list ap)
439         M(obj)->type = va_arg(ap, int);
440         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
441                 return -1;
443         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
444         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
445         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
446         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
448         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
449                 return -1;
450         return 0;
451 } /* op_matcher_init */
453 static void
454 op_matcher_destroy(sdb_object_t *obj)
456         if (OP_M(obj)->left)
457                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
458         if (OP_M(obj)->right)
459                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
460 } /* op_matcher_destroy */
462 static int
463 uop_matcher_init(sdb_object_t *obj, va_list ap)
465         M(obj)->type = va_arg(ap, int);
466         if (M(obj)->type != MATCHER_NOT)
467                 return -1;
469         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
470         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
472         if (! UOP_M(obj)->op)
473                 return -1;
474         return 0;
475 } /* uop_matcher_init */
477 static void
478 uop_matcher_destroy(sdb_object_t *obj)
480         if (UOP_M(obj)->op)
481                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
482 } /* uop_matcher_destroy */
484 static sdb_type_t name_type = {
485         /* size = */ sizeof(obj_matcher_t),
486         /* init = */ obj_matcher_init,
487         /* destroy = */ obj_matcher_destroy,
488 };
490 static sdb_type_t attr_type = {
491         /* size = */ sizeof(attr_matcher_t),
492         /* init = */ attr_matcher_init,
493         /* destroy = */ attr_matcher_destroy,
494 };
496 static sdb_type_t host_type = {
497         /* size = */ sizeof(host_matcher_t),
498         /* init = */ host_matcher_init,
499         /* destroy = */ host_matcher_destroy,
500 };
502 static sdb_type_t op_type = {
503         /* size = */ sizeof(op_matcher_t),
504         /* init = */ op_matcher_init,
505         /* destroy = */ op_matcher_destroy,
506 };
508 static sdb_type_t uop_type = {
509         /* size = */ sizeof(uop_matcher_t),
510         /* init = */ uop_matcher_init,
511         /* destroy = */ uop_matcher_destroy,
512 };
514 /*
515  * public API
516  */
518 sdb_store_matcher_t *
519 sdb_store_name_matcher(int type, const char *name, _Bool re)
521         sdb_store_matcher_t *m;
523         if (re)
524                 m = M(sdb_object_create("name-matcher", name_type,
525                                         NULL, name));
526         else
527                 m = M(sdb_object_create("name-matcher", name_type,
528                                         name, NULL));
530         if (! m)
531                 return NULL;
533         OBJ_M(m)->obj_type = type;
534         return m;
535 } /* sdb_store_name_matcher */
537 sdb_store_matcher_t *
538 sdb_store_attr_matcher(const char *name, const char *value, _Bool re)
540         if (re)
541                 return M(sdb_object_create("attr-matcher", attr_type,
542                                         name, NULL, value));
543         return M(sdb_object_create("attr-matcher", attr_type,
544                                 name, value, NULL));
545 } /* sdb_store_attr_matcher */
547 sdb_store_matcher_t *
548 sdb_store_host_matcher(const char *host_name, const char *host_name_re,
549                 sdb_store_matcher_t *attr_matcher)
551         return M(sdb_object_create("host-matcher", host_type,
552                                 host_name, host_name_re, attr_matcher));
553 } /* sdb_store_host_matcher */
555 sdb_store_matcher_t *
556 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
557                 const char *op, const char *value)
559         int type = -1;
560         _Bool inv = 0;
561         _Bool re = 0;
563         sdb_store_matcher_t *m = NULL;
565         if (! strcasecmp(obj_type, "host"))
566                 type = SDB_HOST;
567         else if (! strcasecmp(obj_type, "service"))
568                 type = SDB_SERVICE;
569         else if (! strcasecmp(obj_type, "attribute"))
570                 type = SDB_ATTRIBUTE;
572         /* TODO: support other operators */
573         if (! strcasecmp(op, "=")) {
574                 /* nothing to do */
575         }
576         else if (! strcasecmp(op, "!=")) {
577                 inv = 1;
578         }
579         else if (! strcasecmp(op, "=~")) {
580                 re = 1;
581         }
582         else if (! strcasecmp(op, "!~")) {
583                 inv = 1;
584                 re = 1;
585         }
586         else
587                 return NULL;
589         if (! strcasecmp(attr, "name"))
590                 m = sdb_store_name_matcher(type, value, re);
591         else if (type == SDB_ATTRIBUTE)
592                 m = sdb_store_attr_matcher(attr, value, re);
594         if (! m)
595                 return NULL;
597         if (inv) {
598                 sdb_store_matcher_t *tmp;
599                 tmp = sdb_store_inv_matcher(m);
600                 /* pass ownership to the inverse matcher */
601                 sdb_object_deref(SDB_OBJ(m));
602                 m = tmp;
603         }
604         return m;
605 } /* sdb_store_matcher_parse_cmp */
607 sdb_store_matcher_t *
608 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
610         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
611                                 left, right));
612 } /* sdb_store_dis_matcher */
614 sdb_store_matcher_t *
615 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
617         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
618                                 left, right));
619 } /* sdb_store_con_matcher */
621 sdb_store_matcher_t *
622 sdb_store_inv_matcher(sdb_store_matcher_t *m)
624         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
625 } /* sdb_store_inv_matcher */
627 int
628 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
630         /* "NULL" always matches */
631         if ((! m) || (! obj))
632                 return 1;
634         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
635                 return 0;
637         return matchers[m->type](m, obj);
638 } /* sdb_store_matcher_matches */
640 char *
641 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
643         if (! m)
644                 return NULL;
646         if ((m->type < 0)
647                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
648                 return NULL;
649         return matchers_tostring[m->type](m, buf, buflen);
650 } /* sdb_store_matcher_tostring */
652 int
653 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
654                 void *user_data)
656         lookup_iter_data_t data = { m, cb, user_data };
658         if (! cb)
659                 return -1;
660         return sdb_store_iterate(lookup_iter, &data);
661 } /* sdb_store_lookup */
663 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */