res_sorcery_astdb.c: Fix regex handling and keep simple prefix matching performance.
[asterisk/asterisk.git] / res / res_sorcery_astdb.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  *
22  * \brief Sorcery Astdb Object Wizard
23  *
24  * \author Joshua Colp <jcolp@digium.com>
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <regex.h>
36
37 #include "asterisk/module.h"
38 #include "asterisk/sorcery.h"
39 #include "asterisk/astdb.h"
40 #include "asterisk/json.h"
41
42 static void *sorcery_astdb_open(const char *data);
43 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object);
44 static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
45 static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
46 static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
47                                              const struct ast_variable *fields);
48 static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
49 static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object);
50 static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object);
51 static void sorcery_astdb_close(void *data);
52
53 static struct ast_sorcery_wizard astdb_object_wizard = {
54         .name = "astdb",
55         .open = sorcery_astdb_open,
56         .create = sorcery_astdb_create,
57         .retrieve_id = sorcery_astdb_retrieve_id,
58         .retrieve_fields = sorcery_astdb_retrieve_fields,
59         .retrieve_multiple = sorcery_astdb_retrieve_multiple,
60         .retrieve_regex = sorcery_astdb_retrieve_regex,
61         .update = sorcery_astdb_update,
62         .delete = sorcery_astdb_delete,
63         .close = sorcery_astdb_close,
64 };
65
66 /*! \brief Helper function which converts from a sorcery object set to a json object */
67 static struct ast_json *sorcery_objectset_to_json(const struct ast_variable *objectset)
68 {
69         struct ast_json *json = ast_json_object_create();
70         const struct ast_variable *field;
71
72         for (field = objectset; field; field = field->next) {
73                 struct ast_json *value = ast_json_string_create(field->value);
74
75                 if (!value) {
76                         ast_json_unref(json);
77                         return NULL;
78                 } else if (ast_json_object_set(json, field->name, value)) {
79                         ast_json_unref(value);
80                         ast_json_unref(json);
81                         return NULL;
82                 }
83         }
84
85         return json;
86 }
87
88 /*! \brief Helper function which converts a json object to a sorcery object set */
89 static struct ast_variable *sorcery_json_to_objectset(struct ast_json *json)
90 {
91         struct ast_json_iter *field;
92         struct ast_variable *objset = NULL;
93
94         for (field = ast_json_object_iter(json); field; field = ast_json_object_iter_next(json, field)) {
95                 struct ast_json *value = ast_json_object_iter_value(field);
96                 struct ast_variable *variable = ast_variable_new(ast_json_object_iter_key(field), ast_json_string_get(value), "");
97
98                 if (!variable) {
99                         ast_variables_destroy(objset);
100                         return NULL;
101                 }
102
103                 variable->next = objset;
104                 objset = variable;
105         }
106
107         return objset;
108 }
109
110 /*! \brief Helper function which compares two json objects and sees if they are equal, but only looks at the criteria provided */
111 static int sorcery_json_equal(struct ast_json *object, struct ast_json *criteria)
112 {
113         struct ast_json_iter *field;
114
115         for (field = ast_json_object_iter(criteria); field; field = ast_json_object_iter_next(criteria, field)) {
116                 struct ast_json *object_field = ast_json_object_get(object, ast_json_object_iter_key(field));
117
118                 if (!object_field || !ast_json_equal(object_field, ast_json_object_iter_value(field))) {
119                         return 0;
120                 }
121         }
122
123         return 1;
124 }
125
126 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object)
127 {
128         RAII_VAR(struct ast_json *, objset, ast_sorcery_objectset_json_create(sorcery, object), ast_json_unref);
129         RAII_VAR(char *, value, NULL, ast_json_free);
130         const char *prefix = data;
131         char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
132
133         if (!objset || !(value = ast_json_dump_string(objset))) {
134                 return -1;
135         }
136
137         snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
138
139         return ast_db_put(family, ast_sorcery_object_get_id(object), value);
140 }
141
142 /*! \brief Internal helper function which retrieves an object, or multiple objects, using fields for criteria */
143 static void *sorcery_astdb_retrieve_fields_common(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields, struct ao2_container *objects)
144 {
145         const char *prefix = data;
146         char family[strlen(prefix) + strlen(type) + 2];
147         RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
148         RAII_VAR(struct ast_json *, criteria, NULL, ast_json_unref);
149         struct ast_db_entry *entry;
150
151         snprintf(family, sizeof(family), "%s/%s", prefix, type);
152
153         if (!(entries = ast_db_gettree(family, NULL)) || (fields && !(criteria = sorcery_objectset_to_json(fields)))) {
154                 return NULL;
155         }
156
157         for (entry = entries; entry; entry = entry->next) {
158                 const char *key = entry->key + strlen(family) + 2;
159                 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
160                 struct ast_json_error error;
161                 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
162                 void *object = NULL;
163
164                 if (!(json = ast_json_load_string(entry->data, &error))) {
165                         return NULL;
166                 } else if (criteria && !sorcery_json_equal(json, criteria)) {
167                         continue;
168                 } else if (!(objset = sorcery_json_to_objectset(json)) ||
169                         !(object = ast_sorcery_alloc(sorcery, type, key)) ||
170                         ast_sorcery_objectset_apply(sorcery, object, objset)) {
171                         ao2_cleanup(object);
172                         return NULL;
173                 }
174
175                 if (!objects) {
176                         return object;
177                 }
178
179                 ao2_link(objects, object);
180                 ao2_cleanup(object);
181         }
182
183         return NULL;
184 }
185
186 static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
187 {
188         return sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, NULL);
189 }
190
191 static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
192 {
193         const char *prefix = data;
194         char family[strlen(prefix) + strlen(type) + 2];
195         RAII_VAR(char *, value, NULL, ast_free_ptr);
196         RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
197         struct ast_json_error error;
198         RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
199         void *object = NULL;
200
201         snprintf(family, sizeof(family), "%s/%s", prefix, type);
202
203         if (ast_db_get_allocated(family, id, &value) || !(json = ast_json_load_string(value, &error)) ||
204                 !(objset = sorcery_json_to_objectset(json)) || !(object = ast_sorcery_alloc(sorcery, type, id)) ||
205                 ast_sorcery_objectset_apply(sorcery, object, objset)) {
206                 ao2_cleanup(object);
207                 return NULL;
208         }
209
210         return object;
211 }
212
213 static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
214 {
215         sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, objects);
216 }
217
218 /*!
219  * \internal
220  * \brief Convert regex prefix pattern to astDB prefix pattern if possible.
221  *
222  * \param tree astDB prefix pattern buffer to fill.
223  * \param regex Extended regular expression with the start anchor character '^'.
224  *
225  * \note Since this is a helper function, the tree buffer is
226  * assumed to always be large enough.
227  *
228  * \retval 0 on success.
229  * \retval -1 on error.  regex is invalid.
230  */
231 static int make_astdb_prefix_pattern(char *tree, const char *regex)
232 {
233         const char *src;
234         char *dst;
235
236         for (dst = tree, src = regex + 1; *src; ++src) {
237                 if (*src == '\\') {
238                         /* Escaped regex char. */
239                         ++src;
240                         if (!*src) {
241                                 /* Invalid regex.  The caller escaped the string terminator. */
242                                 return -1;
243                         }
244                 } else if (*src == '$') {
245                         if (!src[1]) {
246                                 /* Remove the tail anchor character. */
247                                 *dst = '\0';
248                                 return 0;
249                         }
250                 } else if (strchr(".?*+{[(|", *src)) {
251                         /*
252                          * The regex is not a simple prefix pattern.
253                          *
254                          * XXX With more logic, it is possible to simply
255                          * use the current prefix pattern.  The last character
256                          * needs to be removed if possible when the current regex
257                          * token is "?*{".  Also the rest of the regex pattern
258                          * would need to be checked for subgroup/alternation.
259                          * Subgroup/alternation is too complex for a simple prefix
260                          * match.
261                          */
262                         dst = tree;
263                         break;
264                 }
265                 *dst++ = *src;
266         }
267         if (dst != tree) {
268                 *dst++ = '%';
269         }
270         *dst = '\0';
271         return 0;
272 }
273
274 static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
275 {
276         const char *prefix = data;
277         char family[strlen(prefix) + strlen(type) + 2];
278         char tree[strlen(regex) + 1];
279         RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
280         regex_t expression;
281         struct ast_db_entry *entry;
282
283         snprintf(family, sizeof(family), "%s/%s", prefix, type);
284
285         if (regex[0] == '^') {
286                 /*
287                  * For performance reasons, try to create an astDB prefix
288                  * pattern from the regex to reduce the number of entries
289                  * retrieved from astDB for regex to then match.
290                  */
291                 if (make_astdb_prefix_pattern(tree, regex)) {
292                         return;
293                 }
294         } else {
295                 tree[0] = '\0';
296         }
297
298         if (!(entries = ast_db_gettree(family, tree))
299                 || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
300                 return;
301         }
302
303         for (entry = entries; entry; entry = entry->next) {
304                 /* The key in the entry includes the family, so we need to strip it out for regex purposes */
305                 const char *key = entry->key + strlen(family) + 2;
306                 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
307                 struct ast_json_error error;
308                 RAII_VAR(void *, object, NULL, ao2_cleanup);
309                 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
310
311                 if (regexec(&expression, key, 0, NULL, 0)) {
312                         continue;
313                 } else if (!(json = ast_json_load_string(entry->data, &error)) ||
314                         !(objset = sorcery_json_to_objectset(json)) ||
315                         !(object = ast_sorcery_alloc(sorcery, type, key)) ||
316                         ast_sorcery_objectset_apply(sorcery, object, objset)) {
317                         regfree(&expression);
318                         return;
319                 }
320
321                 ao2_link(objects, object);
322         }
323
324         regfree(&expression);
325 }
326
327 static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object)
328 {
329         const char *prefix = data;
330         char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2], value[2];
331
332         snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
333
334         /* It is okay for the value to be truncated, we are only checking that it exists */
335         if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
336                 return -1;
337         }
338
339         /* The only difference between update and create is that for update the object must already exist */
340         return sorcery_astdb_create(sorcery, data, object);
341 }
342
343 static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object)
344 {
345         const char *prefix = data;
346         char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
347         char value[2];
348
349         snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
350
351         if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
352                 return -1;
353         }
354
355         return ast_db_del(family, ast_sorcery_object_get_id(object));
356 }
357
358 static void *sorcery_astdb_open(const char *data)
359 {
360         /* We require a prefix for family string generation, or else stuff could mix together */
361         if (ast_strlen_zero(data)) {
362                 return NULL;
363         }
364
365         return ast_strdup(data);
366 }
367
368 static void sorcery_astdb_close(void *data)
369 {
370         ast_free(data);
371 }
372
373 static int load_module(void)
374 {
375         if (ast_sorcery_wizard_register(&astdb_object_wizard)) {
376                 return AST_MODULE_LOAD_DECLINE;
377         }
378
379         return AST_MODULE_LOAD_SUCCESS;
380 }
381
382 static int unload_module(void)
383 {
384         ast_sorcery_wizard_unregister(&astdb_object_wizard);
385         return 0;
386 }
387
388 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Astdb Object Wizard",
389         .load = load_module,
390         .unload = unload_module,
391         .load_pri = AST_MODPRI_REALTIME_DRIVER,
392 );