Code

store_lookup: Added sdb_store_inv_matcher().
[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);
82 static int
83 match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj);
85 /* specific matchers */
87 static int
88 match_name(name_matcher_t *m, const char *name)
89 {
90         assert(m);
92         if ((! m->name) && (! m->name_re))
93                 return 1;
95         if (! name)
96                 name = "";
98         if (m->name && strcasecmp(m->name, name))
99                 return 0;
100         if (m->name_re && regexec(m->name_re, name,
101                                         /* matches */ 0, NULL, /* flags = */ 0))
102                 return 0;
103         return 1;
104 } /* match_name */
106 /* match attribute specific values;
107  * always call this function through match_obj() */
108 static int
109 match_attr(attr_matcher_t *m, sdb_store_base_t *obj)
111         assert(m && obj);
113         if (obj->type != SDB_ATTRIBUTE)
114                 return 0;
116         {
117                 sdb_attribute_t *attr = SDB_ATTR(obj);
118                 char buf[sdb_data_strlen(&attr->value) + 1];
120                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
121                         return 0;
122                 return match_name(&m->value, buf);
123         }
124 } /* match_attr */
126 /* match service specific values;
127  * always call this function through match_obj() */
128 static int
129 match_service(service_matcher_t *m, sdb_store_base_t *obj)
131         sdb_llist_iter_t *iter;
133         assert(m && obj);
135         if (obj->type != SDB_SERVICE)
136                 return 0;
138         if (! m->attr)
139                 return 1;
141         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
142         while (sdb_llist_iter_has_next(iter)) {
143                 sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
145                 /* if any of the attributes matches we found a matching service */
146                 if (match_obj(M(m->attr), attr)) {
147                         sdb_llist_iter_destroy(iter);
148                         return 1;
149                 }
150         }
151         sdb_llist_iter_destroy(iter);
152         return 0;
153 } /* match_service */
155 /* match host specific values;
156  * always call this function through match_obj() */
157 static int
158 match_host(host_matcher_t *m, sdb_store_base_t *obj)
160         sdb_llist_iter_t *iter;
161         int status;
163         assert(m && obj);
165         if (obj->type != SDB_HOST)
166                 return 0;
168         if (m->service) {
169                 iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
170                 status = 0;
171         }
172         else {
173                 iter = NULL;
174                 status = 1;
175         }
176         while (sdb_llist_iter_has_next(iter)) {
177                 sdb_store_base_t *service = STORE_BASE(sdb_llist_iter_get_next(iter));
179                 /* found a matching service */
180                 if (match_obj(M(m->service), service)) {
181                         status = 1;
182                         break;
183                 }
184         }
185         sdb_llist_iter_destroy(iter);
187         if (! status)
188                 return status;
189         else if (! m->attr)
190                 return 1;
192         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
193         while (sdb_llist_iter_has_next(iter)) {
194                 sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
196                 /* if any attribute matches, we found a matching host */
197                 if (match_obj(M(m->attr), attr)) {
198                         sdb_llist_iter_destroy(iter);
199                         return 1;
200                 }
201         }
202         sdb_llist_iter_destroy(iter);
203         return 0;
204 } /* match_host */
206 /* generic matchers */
208 typedef int (*matcher_cb)(sdb_store_matcher_t *, sdb_store_base_t *);
210 /* this array needs to be indexable by the matcher types;
211  * -> update the enum in store-private.h when updating this */
212 static matcher_cb matchers[] = {
213         match_logical,
214         match_logical,
215         match_unary,
216         match_obj,
217         match_obj,
218         match_obj,
219 };
221 static int
222 match_logical(sdb_store_matcher_t *m, sdb_store_base_t *obj)
224         int status;
226         assert(m && obj);
227         assert(OP_M(m)->left && OP_M(m)->right);
229         status = sdb_store_matcher_matches(OP_M(m)->left, obj);
230         /* lazy evaluation */
231         if ((! status) && (m->type == MATCHER_AND))
232                 return status;
233         else if (status && (m->type == MATCHER_OR))
234                 return status;
236         return sdb_store_matcher_matches(OP_M(m)->right, obj);
237 } /* match_logical */
239 static int
240 match_unary(sdb_store_matcher_t *m, sdb_store_base_t *obj)
242         assert(m && obj);
243         assert(UOP_M(m)->op);
245         return !sdb_store_matcher_matches(UOP_M(m)->op, obj);
246 } /* match_unary */
248 static int
249 match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
251         int status;
253         assert(m && obj);
255         status = match_name(&OBJ_M(m)->name, obj->super.name);
256         if (! status)
257                 return status;
259         switch (m->type) {
260                 case MATCHER_ATTR:
261                         return match_attr(ATTR_M(m), obj);
262                         break;
263                 case MATCHER_SERVICE:
264                         return match_service(SERVICE_M(m), obj);
265                         break;
266                 case MATCHER_HOST:
267                         return match_host(HOST_M(m), obj);
268                         break;
269         }
270         return 0;
271 } /* match_obj */
273 /*
274  * private matcher types
275  */
277 /* initializes a name matcher consuming two elements from ap */
278 static int
279 name_matcher_init(name_matcher_t *m, va_list ap)
281         const char *name = va_arg(ap, const char *);
282         const char *name_re = va_arg(ap, const char *);
284         if (name) {
285                 m->name = strdup(name);
286                 if (! m->name)
287                         return -1;
288         }
289         if (name_re) {
290                 m->name_re = malloc(sizeof(*m->name_re));
291                 if (! m->name_re)
292                         return -1;
293                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
294                         return -1;
295         }
296         return 0;
297 } /* name_matcher_init */
299 static void
300 name_matcher_destroy(name_matcher_t *m)
302         if (m->name)
303                 free(m->name);
304         if (m->name_re) {
305                 regfree(m->name_re);
306                 free(m->name_re);
307         }
308 } /* name_matcher_destroy */
310 /* initializes an object matcher consuming two elements from ap */
311 static int
312 obj_matcher_init(sdb_object_t *obj, va_list ap)
314         obj_matcher_t *m = OBJ_M(obj);
315         return name_matcher_init(&m->name, ap);
316 } /* obj_matcher_init */
318 static void
319 obj_matcher_destroy(sdb_object_t *obj)
321         obj_matcher_t *m = OBJ_M(obj);
322         name_matcher_destroy(&m->name);
323 } /* obj_matcher_destroy */
325 static int
326 attr_matcher_init(sdb_object_t *obj, va_list ap)
328         attr_matcher_t *attr = ATTR_M(obj);
329         int status;
331         M(obj)->type = MATCHER_ATTR;
333         status = obj_matcher_init(obj, ap);
334         if (! status)
335                 status = name_matcher_init(&attr->value, ap);
336         return status;
337 } /* attr_matcher_init */
339 static void
340 attr_matcher_destroy(sdb_object_t *obj)
342         attr_matcher_t *attr = ATTR_M(obj);
344         obj_matcher_destroy(obj);
345         name_matcher_destroy(&attr->value);
346 } /* attr_matcher_destroy */
348 static int
349 service_matcher_init(sdb_object_t *obj, va_list ap)
351         attr_matcher_t *attr;
352         int status;
354         M(obj)->type = MATCHER_SERVICE;
356         status = obj_matcher_init(obj, ap);
357         if (status)
358                 return status;
360         attr = va_arg(ap, attr_matcher_t *);
362         sdb_object_ref(SDB_OBJ(attr));
363         SERVICE_M(obj)->attr = attr;
364         return 0;
365 } /* service_matcher_init */
367 static void
368 service_matcher_destroy(sdb_object_t *obj)
370         obj_matcher_destroy(obj);
371         sdb_object_deref(SDB_OBJ(SERVICE_M(obj)->attr));
372 } /* service_matcher_destroy */
374 static int
375 host_matcher_init(sdb_object_t *obj, va_list ap)
377         service_matcher_t *service;
378         attr_matcher_t *attr;
379         int status;
381         M(obj)->type = MATCHER_HOST;
383         status = obj_matcher_init(obj, ap);
384         if (status)
385                 return status;
387         service = va_arg(ap, service_matcher_t *);
388         attr = va_arg(ap, attr_matcher_t *);
390         sdb_object_ref(SDB_OBJ(service));
391         HOST_M(obj)->service = service;
392         sdb_object_ref(SDB_OBJ(attr));
393         HOST_M(obj)->attr = attr;
394         return 0;
395 } /* host_matcher_init */
397 static void
398 host_matcher_destroy(sdb_object_t *obj)
400         obj_matcher_destroy(obj);
401         sdb_object_deref(SDB_OBJ(HOST_M(obj)->service));
402         sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
403 } /* host_matcher_destroy */
405 static int
406 op_matcher_init(sdb_object_t *obj, va_list ap)
408         M(obj)->type = va_arg(ap, int);
409         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
410                 return -1;
412         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
413         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
414         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
415         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
417         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
418                 return -1;
419         return 0;
420 } /* op_matcher_init */
422 static void
423 op_matcher_destroy(sdb_object_t *obj)
425         if (OP_M(obj)->left)
426                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
427         if (OP_M(obj)->right)
428                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
429 } /* op_matcher_destroy */
431 static int
432 uop_matcher_init(sdb_object_t *obj, va_list ap)
434         M(obj)->type = va_arg(ap, int);
435         if (M(obj)->type != MATCHER_NOT)
436                 return -1;
438         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
439         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
441         if (! UOP_M(obj)->op)
442                 return -1;
443         return 0;
444 } /* uop_matcher_init */
446 static void
447 uop_matcher_destroy(sdb_object_t *obj)
449         if (UOP_M(obj)->op)
450                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
451 } /* uop_matcher_destroy */
453 static sdb_type_t attr_type = {
454         /* size = */ sizeof(attr_matcher_t),
455         /* init = */ attr_matcher_init,
456         /* destroy = */ attr_matcher_destroy,
457 };
459 static sdb_type_t service_type = {
460         /* size = */ sizeof(service_matcher_t),
461         /* init = */ service_matcher_init,
462         /* destroy = */ service_matcher_destroy,
463 };
465 static sdb_type_t host_type = {
466         /* size = */ sizeof(host_matcher_t),
467         /* init = */ host_matcher_init,
468         /* destroy = */ host_matcher_destroy,
469 };
471 static sdb_type_t op_type = {
472         /* size = */ sizeof(op_matcher_t),
473         /* init = */ op_matcher_init,
474         /* destroy = */ op_matcher_destroy,
475 };
477 static sdb_type_t uop_type = {
478         /* size = */ sizeof(uop_matcher_t),
479         /* init = */ uop_matcher_init,
480         /* destroy = */ uop_matcher_destroy,
481 };
483 /*
484  * public API
485  */
487 sdb_store_matcher_t *
488 sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
489                 const char *attr_value, const char *attr_value_re)
491         return M(sdb_object_create("attr-matcher", attr_type,
492                                 attr_name, attr_name_re, attr_value, attr_value_re));
493 } /* sdb_store_attr_matcher */
495 sdb_store_matcher_t *
496 sdb_store_service_matcher(const char *service_name, const char *service_name_re,
497                 sdb_store_matcher_t *attr_matcher)
499         return M(sdb_object_create("service-matcher", service_type,
500                                 service_name, service_name_re, attr_matcher));
501 } /* sdb_store_service_matcher */
503 sdb_store_matcher_t *
504 sdb_store_host_matcher(const char *host_name, const char *host_name_re,
505                 sdb_store_matcher_t *service_matcher,
506                 sdb_store_matcher_t *attr_matcher)
508         return M(sdb_object_create("host-matcher", host_type,
509                                 host_name, host_name_re, service_matcher, attr_matcher));
510 } /* sdb_store_host_matcher */
512 sdb_store_matcher_t *
513 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
514                 const char *op, const char *value)
516         int typ = -1;
518         const char *matcher = NULL;
519         const char *matcher_re = NULL;
521         if (! strcasecmp(obj_type, "host"))
522                 typ = SDB_HOST;
523         else if (! strcasecmp(obj_type, "service"))
524                 typ = SDB_SERVICE;
525         else if (! strcasecmp(obj_type, "attribute"))
526                 typ = SDB_ATTRIBUTE;
528         /* TODO: support other operators */
529         if (! strcasecmp(op, "="))
530                 matcher = value;
531         else if (! strcasecmp(op, "=~"))
532                 matcher_re = value;
533         else
534                 return NULL;
536         if (! strcasecmp(attr, "name")) {
537                 /* accept */
538         }
539         else if (typ == SDB_ATTRIBUTE)
540                 return sdb_store_attr_matcher(attr, NULL, matcher, matcher_re);
541         else
542                 return NULL;
544         if (typ == SDB_HOST)
545                 return sdb_store_host_matcher(matcher, matcher_re, NULL, NULL);
546         else if (typ == SDB_SERVICE)
547                 return sdb_store_service_matcher(matcher, matcher_re, NULL);
548         else if (typ == SDB_ATTRIBUTE)
549                 return sdb_store_attr_matcher(matcher, matcher_re, NULL, NULL);
550         return NULL;
551 } /* sdb_store_matcher_parse_cmp */
553 sdb_store_matcher_t *
554 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
556         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
557                                 left, right));
558 } /* sdb_store_dis_matcher */
560 sdb_store_matcher_t *
561 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
563         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
564                                 left, right));
565 } /* sdb_store_con_matcher */
567 sdb_store_matcher_t *
568 sdb_store_inv_matcher(sdb_store_matcher_t *m)
570         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
571 } /* sdb_store_inv_matcher */
573 int
574 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
576         /* "NULL" always matches */
577         if ((! m) || (! obj))
578                 return 1;
580         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
581                 return 0;
583         return matchers[m->type](m, obj);
584 } /* sdb_store_matcher_matches */
586 int
587 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
588                 void *user_data)
590         lookup_iter_data_t data = { m, cb, user_data };
592         if (! cb)
593                 return -1;
594         return sdb_store_iterate(lookup_iter, &data);
595 } /* sdb_store_lookup */
597 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */