Code

store_lookup: Removed the (now unused) service 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_obj_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_obj_name */
106 static char *
107 obj_name_tostring(name_matcher_t *m, char *buf, size_t buflen)
109         snprintf(buf, buflen, "{ %s%s%s, %p }",
110                         m->name ? "'" : "", m->name ? m->name : "NULL", m->name ? "'" : "",
111                         m->name_re);
112         return buf;
113 } /* obj_name_tostring */
115 static char *
116 logical_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
118         char left[buflen + 1], right[buflen + 1];
120         if (! m) {
121                 /* this should not happen */
122                 snprintf(buf, buflen, "()");
123                 return buf;
124         }
126         assert((m->type == MATCHER_OR) || (m->type == MATCHER_AND));
127         snprintf(buf, buflen, "(%s, %s, %s)",
128                         m->type == MATCHER_OR ? "OR" : "AND",
129                         sdb_store_matcher_tostring(OP_M(m)->left, left, sizeof(left)),
130                         sdb_store_matcher_tostring(OP_M(m)->right, right, sizeof(right)));
131         return buf;
132 } /* logical_tostring */
134 static char *
135 unary_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
137         char op[buflen + 1];
139         if (! m) {
140                 /* this should not happen */
141                 snprintf(buf, buflen, "()");
142                 return buf;
143         }
145         assert(m->type == MATCHER_NOT);
146         snprintf(buf, buflen, "(NOT, %s)",
147                         sdb_store_matcher_tostring(UOP_M(m)->op, op, sizeof(op)));
148         return buf;
149 } /* unary_tostring */
151 static char *
152 name_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
154         char name[buflen + 1];
155         assert(m->type == MATCHER_NAME);
156         snprintf(buf, buflen, "OBJ[%s]{ NAME%s }",
157                         SDB_STORE_TYPE_TO_NAME(OBJ_M(m)->obj_type),
158                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)));
159         return buf;
160 } /* name_tostring */
162 static char *
163 attr_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
165         char name[buflen + 1], value[buflen + 1];
167         if (! m) {
168                 snprintf(buf, buflen, "ATTR{}");
169                 return buf;
170         }
172         assert(m->type == MATCHER_ATTR);
173         snprintf(buf, buflen, "ATTR{ NAME%s, VALUE%s }",
174                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
175                         obj_name_tostring(&ATTR_M(m)->value, value, sizeof(value)));
176         return buf;
177 } /* attr_tostring */
179 static char *
180 host_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
182         char name[buflen + 1], attr[buflen + 1];
184         if (! m) {
185                 snprintf(buf, buflen, "HOST{}");
186                 return buf;
187         }
189         assert(m->type == MATCHER_HOST);
190         snprintf(buf, buflen, "HOST{ NAME%s, %s }",
191                         obj_name_tostring(&OBJ_M(m)->name, name, sizeof(name)),
192                         attr_tostring(SDB_STORE_MATCHER(HOST_M(m)->attr),
193                                 attr, sizeof(attr)));
194         return buf;
195 } /* host_tostring */
197 static int
198 match_name(sdb_store_matcher_t *m, sdb_store_base_t *obj)
200         sdb_llist_iter_t *iter = NULL;
201         int status = 0;
203         assert(m && obj);
205         if (obj->type != SDB_HOST)
206                 return 0;
208         switch (OBJ_M(m)->obj_type) {
209                 case SDB_HOST:
210                         return match_obj_name(&OBJ_M(m)->name, obj->super.name);
211                         break;
212                 case SDB_SERVICE:
213                         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->children);
214                         break;
215                 case SDB_ATTRIBUTE:
216                         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
217                         break;
218         }
220         while (sdb_llist_iter_has_next(iter)) {
221                 sdb_store_base_t *child = STORE_BASE(sdb_llist_iter_get_next(iter));
222                 if (match_obj_name(&OBJ_M(m)->name, child->super.name)) {
223                         status = 1;
224                         break;
225                 }
226         }
227         sdb_llist_iter_destroy(iter);
228         return status;
229 } /* match_name */
231 /* match attribute specific values;
232  * always call this function through match_obj() */
233 static int
234 match_attr(attr_matcher_t *m, sdb_store_base_t *obj)
236         assert(m && obj);
238         if (obj->type != SDB_ATTRIBUTE)
239                 return 0;
241         {
242                 sdb_attribute_t *attr = SDB_ATTR(obj);
243                 char buf[sdb_data_strlen(&attr->value) + 1];
245                 if (sdb_data_format(&attr->value, buf, sizeof(buf), SDB_UNQUOTED) <= 0)
246                         return 0;
247                 return match_obj_name(&m->value, buf);
248         }
249 } /* match_attr */
251 /* match host specific values;
252  * always call this function through match_obj() */
253 static int
254 match_host(host_matcher_t *m, sdb_store_base_t *obj)
256         sdb_llist_iter_t *iter;
258         assert(m && obj);
260         if (obj->type != SDB_HOST)
261                 return 0;
263         if (! m->attr)
264                 return 1;
266         iter = sdb_llist_get_iter(SDB_STORE_OBJ(obj)->attributes);
267         while (sdb_llist_iter_has_next(iter)) {
268                 sdb_store_base_t *attr = STORE_BASE(sdb_llist_iter_get_next(iter));
270                 /* if any attribute matches, we found a matching host */
271                 if (match_obj(M(m->attr), attr)) {
272                         sdb_llist_iter_destroy(iter);
273                         return 1;
274                 }
275         }
276         sdb_llist_iter_destroy(iter);
277         return 0;
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_obj,
292         match_obj,
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 static int
334 match_obj(sdb_store_matcher_t *m, sdb_store_base_t *obj)
336         int status;
338         assert(m && obj);
340         status = match_obj_name(&OBJ_M(m)->name, obj->super.name);
341         if (! status)
342                 return status;
344         switch (m->type) {
345                 case MATCHER_ATTR:
346                         return match_attr(ATTR_M(m), obj);
347                         break;
348                 case MATCHER_HOST:
349                         return match_host(HOST_M(m), obj);
350                         break;
351                 default:
352                         assert(m->type != m->type);
353                         break;
354         }
355         return 0;
356 } /* match_obj */
358 /*
359  * private matcher types
360  */
362 /* initializes a name matcher consuming two elements from ap */
363 static int
364 name_matcher_init(name_matcher_t *m, va_list ap)
366         const char *name = va_arg(ap, const char *);
367         const char *name_re = va_arg(ap, const char *);
369         if (name) {
370                 m->name = strdup(name);
371                 if (! m->name)
372                         return -1;
373         }
374         if (name_re) {
375                 m->name_re = malloc(sizeof(*m->name_re));
376                 if (! m->name_re)
377                         return -1;
378                 if (regcomp(m->name_re, name_re, REG_EXTENDED | REG_ICASE | REG_NOSUB))
379                         return -1;
380         }
381         return 0;
382 } /* name_matcher_init */
384 static void
385 name_matcher_destroy(name_matcher_t *m)
387         if (m->name)
388                 free(m->name);
389         if (m->name_re) {
390                 regfree(m->name_re);
391                 free(m->name_re);
392         }
393 } /* name_matcher_destroy */
395 /* initializes an object matcher consuming two elements from ap */
396 static int
397 obj_matcher_init(sdb_object_t *obj, va_list ap)
399         obj_matcher_t *m = OBJ_M(obj);
400         M(obj)->type = MATCHER_NAME;
401         return name_matcher_init(&m->name, ap);
402 } /* obj_matcher_init */
404 static void
405 obj_matcher_destroy(sdb_object_t *obj)
407         obj_matcher_t *m = OBJ_M(obj);
408         name_matcher_destroy(&m->name);
409 } /* obj_matcher_destroy */
411 static int
412 attr_matcher_init(sdb_object_t *obj, va_list ap)
414         attr_matcher_t *attr = ATTR_M(obj);
415         int status;
417         status = obj_matcher_init(obj, ap);
418         if (! status)
419                 status = name_matcher_init(&attr->value, ap);
421         M(obj)->type = MATCHER_ATTR;
422         return status;
423 } /* attr_matcher_init */
425 static void
426 attr_matcher_destroy(sdb_object_t *obj)
428         attr_matcher_t *attr = ATTR_M(obj);
430         obj_matcher_destroy(obj);
431         name_matcher_destroy(&attr->value);
432 } /* attr_matcher_destroy */
434 static int
435 host_matcher_init(sdb_object_t *obj, va_list ap)
437         attr_matcher_t *attr;
438         int status;
440         status = obj_matcher_init(obj, ap);
441         if (status)
442                 return status;
444         attr = va_arg(ap, attr_matcher_t *);
445         sdb_object_ref(SDB_OBJ(attr));
446         HOST_M(obj)->attr = attr;
448         M(obj)->type = MATCHER_HOST;
449         return 0;
450 } /* host_matcher_init */
452 static void
453 host_matcher_destroy(sdb_object_t *obj)
455         obj_matcher_destroy(obj);
456         sdb_object_deref(SDB_OBJ(HOST_M(obj)->attr));
457 } /* host_matcher_destroy */
459 static int
460 op_matcher_init(sdb_object_t *obj, va_list ap)
462         M(obj)->type = va_arg(ap, int);
463         if ((M(obj)->type != MATCHER_OR) && (M(obj)->type != MATCHER_AND))
464                 return -1;
466         OP_M(obj)->left = va_arg(ap, sdb_store_matcher_t *);
467         sdb_object_ref(SDB_OBJ(OP_M(obj)->left));
468         OP_M(obj)->right = va_arg(ap, sdb_store_matcher_t *);
469         sdb_object_ref(SDB_OBJ(OP_M(obj)->right));
471         if ((! OP_M(obj)->left) || (! OP_M(obj)->right))
472                 return -1;
473         return 0;
474 } /* op_matcher_init */
476 static void
477 op_matcher_destroy(sdb_object_t *obj)
479         if (OP_M(obj)->left)
480                 sdb_object_deref(SDB_OBJ(OP_M(obj)->left));
481         if (OP_M(obj)->right)
482                 sdb_object_deref(SDB_OBJ(OP_M(obj)->right));
483 } /* op_matcher_destroy */
485 static int
486 uop_matcher_init(sdb_object_t *obj, va_list ap)
488         M(obj)->type = va_arg(ap, int);
489         if (M(obj)->type != MATCHER_NOT)
490                 return -1;
492         UOP_M(obj)->op = va_arg(ap, sdb_store_matcher_t *);
493         sdb_object_ref(SDB_OBJ(UOP_M(obj)->op));
495         if (! UOP_M(obj)->op)
496                 return -1;
497         return 0;
498 } /* uop_matcher_init */
500 static void
501 uop_matcher_destroy(sdb_object_t *obj)
503         if (UOP_M(obj)->op)
504                 sdb_object_deref(SDB_OBJ(UOP_M(obj)->op));
505 } /* uop_matcher_destroy */
507 static sdb_type_t name_type = {
508         /* size = */ sizeof(obj_matcher_t),
509         /* init = */ obj_matcher_init,
510         /* destroy = */ obj_matcher_destroy,
511 };
513 static sdb_type_t attr_type = {
514         /* size = */ sizeof(attr_matcher_t),
515         /* init = */ attr_matcher_init,
516         /* destroy = */ attr_matcher_destroy,
517 };
519 static sdb_type_t host_type = {
520         /* size = */ sizeof(host_matcher_t),
521         /* init = */ host_matcher_init,
522         /* destroy = */ host_matcher_destroy,
523 };
525 static sdb_type_t op_type = {
526         /* size = */ sizeof(op_matcher_t),
527         /* init = */ op_matcher_init,
528         /* destroy = */ op_matcher_destroy,
529 };
531 static sdb_type_t uop_type = {
532         /* size = */ sizeof(uop_matcher_t),
533         /* init = */ uop_matcher_init,
534         /* destroy = */ uop_matcher_destroy,
535 };
537 /*
538  * public API
539  */
541 sdb_store_matcher_t *
542 sdb_store_name_matcher(int type, const char *name, _Bool re)
544         sdb_store_matcher_t *m;
546         if (re)
547                 m = M(sdb_object_create("name-matcher", name_type,
548                                         NULL, name));
549         else
550                 m = M(sdb_object_create("name-matcher", name_type,
551                                         name, NULL));
553         if (! m)
554                 return NULL;
556         OBJ_M(m)->obj_type = type;
557         return m;
558 } /* sdb_store_name_matcher */
560 sdb_store_matcher_t *
561 sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
562                 const char *attr_value, const char *attr_value_re)
564         return M(sdb_object_create("attr-matcher", attr_type,
565                                 attr_name, attr_name_re, attr_value, attr_value_re));
566 } /* sdb_store_attr_matcher */
568 sdb_store_matcher_t *
569 sdb_store_host_matcher(const char *host_name, const char *host_name_re,
570                 sdb_store_matcher_t *attr_matcher)
572         return M(sdb_object_create("host-matcher", host_type,
573                                 host_name, host_name_re, attr_matcher));
574 } /* sdb_store_host_matcher */
576 sdb_store_matcher_t *
577 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
578                 const char *op, const char *value)
580         int type = -1;
581         _Bool inv = 0;
582         _Bool re = 0;
584         sdb_store_matcher_t *m = NULL;
586         const char *matcher = NULL;
587         const char *matcher_re = NULL;
589         if (! strcasecmp(obj_type, "host"))
590                 type = SDB_HOST;
591         else if (! strcasecmp(obj_type, "service"))
592                 type = SDB_SERVICE;
593         else if (! strcasecmp(obj_type, "attribute"))
594                 type = SDB_ATTRIBUTE;
596         /* TODO: support other operators */
597         if (! strcasecmp(op, "=")) {
598                 matcher = value;
599         }
600         else if (! strcasecmp(op, "!=")) {
601                 matcher = value;
602                 inv = 1;
603         }
604         else if (! strcasecmp(op, "=~")) {
605                 matcher_re = value;
606                 re = 1;
607         }
608         else if (! strcasecmp(op, "!~")) {
609                 matcher_re = value;
610                 inv = 1;
611                 re = 1;
612         }
613         else
614                 return NULL;
616         if (! strcasecmp(attr, "name"))
617                 m = sdb_store_name_matcher(type, value, re);
618         else if (type == SDB_ATTRIBUTE) {
619                 m = sdb_store_host_matcher(/* name = */ NULL, NULL,
620                                 sdb_store_attr_matcher(attr, NULL, matcher, matcher_re));
622                 /* pass ownership to the host matcher */
623                 if (m)
624                         sdb_object_deref(SDB_OBJ(HOST_M(m)->attr));
625         }
627         if (! m)
628                 return NULL;
630         if (inv) {
631                 sdb_store_matcher_t *tmp;
632                 tmp = sdb_store_inv_matcher(m);
633                 /* pass ownership to the inverse matcher */
634                 sdb_object_deref(SDB_OBJ(m));
635                 m = tmp;
636         }
637         return m;
638 } /* sdb_store_matcher_parse_cmp */
640 sdb_store_matcher_t *
641 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
643         return M(sdb_object_create("dis-matcher", op_type, MATCHER_OR,
644                                 left, right));
645 } /* sdb_store_dis_matcher */
647 sdb_store_matcher_t *
648 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right)
650         return M(sdb_object_create("con-matcher", op_type, MATCHER_AND,
651                                 left, right));
652 } /* sdb_store_con_matcher */
654 sdb_store_matcher_t *
655 sdb_store_inv_matcher(sdb_store_matcher_t *m)
657         return M(sdb_object_create("inv-matcher", uop_type, MATCHER_NOT, m));
658 } /* sdb_store_inv_matcher */
660 int
661 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj)
663         /* "NULL" always matches */
664         if ((! m) || (! obj))
665                 return 1;
667         if ((m->type < 0) || ((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers)))
668                 return 0;
670         return matchers[m->type](m, obj);
671 } /* sdb_store_matcher_matches */
673 char *
674 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen)
676         if (! m)
677                 return NULL;
679         if ((m->type < 0)
680                         || (((size_t)m->type >= SDB_STATIC_ARRAY_LEN(matchers_tostring))))
681                 return NULL;
682         return matchers_tostring[m->type](m, buf, buflen);
683 } /* sdb_store_matcher_tostring */
685 int
686 sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
687                 void *user_data)
689         lookup_iter_data_t data = { m, cb, user_data };
691         if (! cb)
692                 return -1;
693         return sdb_store_iterate(lookup_iter, &data);
694 } /* sdb_store_lookup */
696 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */