Debian Codegraph @

res/res_sorcery_astdb.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
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 <[email protected]>
25 */
26
27 /*** MODULEINFO
28 <support_level>core</support_level>
29 ***/
30
31 #include "asterisk.h"
32
33 #include <regex.h>
34
35 #include "asterisk/module.h"
36 #include "asterisk/sorcery.h"
37 #include "asterisk/astdb.h"
38 #include "asterisk/json.h"
39
40 static void *sorcery_astdb_open(const char *data);
41 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object);
42 static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
43 static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
44 static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
45 const struct ast_variable *fields);
46 static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
47 static void sorcery_astdb_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len);
48 static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object);
49 static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object);
50 static void sorcery_astdb_close(void *data);
51
52 static struct ast_sorcery_wizard astdb_object_wizard = {
53 .name = "astdb",
54 .open = sorcery_astdb_open,
55 .create = sorcery_astdb_create,
56 .retrieve_id = sorcery_astdb_retrieve_id,
57 .retrieve_fields = sorcery_astdb_retrieve_fields,
58 .retrieve_multiple = sorcery_astdb_retrieve_multiple,
59 .retrieve_regex = sorcery_astdb_retrieve_regex,
60 .retrieve_prefix = sorcery_astdb_retrieve_prefix,
61 .update = sorcery_astdb_update,
62 .delete = sorcery_astdb_delete,
63 .close = sorcery_astdb_close,
64 };
65
66 static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object)
67 {
68 RAII_VAR(struct ast_json *, objset, ast_sorcery_objectset_json_create(sorcery, object), ast_json_unref);
69 RAII_VAR(char *, value, NULL, ast_json_free);
70 const char *prefix = data;
71 char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
72
73 if (!objset || !(value = ast_json_dump_string(objset))) {
74 return -1;
75 }
76
77 snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
78
79 return ast_db_put(family, ast_sorcery_object_get_id(object), value);
80 }
81
82 /*! \brief Internal helper function which returns a filtered objectset.
83 *
84 * The following are filtered out of the objectset:
85 * \li Fields that are not registered with sorcery.
86 *
87 * \param objectset Objectset to filter.
88 * \param sorcery The sorcery instance that is requesting an objectset.
89 * \param type The object type
90 *
91 * \return The filtered objectset
92 */
93 static struct ast_variable *sorcery_astdb_filter_objectset(struct ast_variable *objectset, const struct ast_sorcery *sorcery,
94 const char *type)
95 {
96 struct ast_variable *previous = NULL, *field = objectset;
97 struct ast_sorcery_object_type *object_type;
98
99 object_type = ast_sorcery_get_object_type(sorcery, type);
100 if (!object_type) {
101 ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
102 return objectset;
103 }
104
105 while (field) {
106 struct ast_variable *removed;
107
108 if (ast_sorcery_is_object_field_registered(object_type, field->name)) {
109 previous = field;
110 field = field->next;
111 continue;
112 }
113
114 ast_debug(1, "Filtering out astdb field '%s' from retrieval\n", field->name);
115
116 if (previous) {
117 previous->next = field->next;
118 } else {
119 objectset = field->next;
120 }
121
122 removed = field;
123 field = field->next;
124 removed->next = NULL;
125
126 ast_variables_destroy(removed);
127 }
128
129 ao2_cleanup(object_type);
130
131 return objectset;
132 }
133
134 /*! \brief Internal helper function which retrieves an object, or multiple objects, using fields for criteria */
135 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)
136 {
137 const char *prefix = data;
138 char family[strlen(prefix) + strlen(type) + 2];
139 RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
140 struct ast_db_entry *entry;
141
142 snprintf(family, sizeof(family), "%s/%s", prefix, type);
143
144 if (!(entries = ast_db_gettree(family, NULL))) {
145 return NULL;
146 }
147
148 for (entry = entries; entry; entry = entry->next) {
149 const char *key = entry->key + strlen(family) + 2;
150 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
151 struct ast_json_error error;
152 RAII_VAR(struct ast_variable *, existing, NULL, ast_variables_destroy);
153 void *object = NULL;
154
155 if (!(json = ast_json_load_string(entry->data, &error))) {
156 return NULL;
157 }
158
159 if (ast_json_to_ast_variables(json, &existing) != AST_JSON_TO_AST_VARS_CODE_SUCCESS) {
160 return NULL;
161 }
162
163 existing = sorcery_astdb_filter_objectset(existing, sorcery, type);
164
165 if (fields && !ast_variable_lists_match(existing, fields, 0)) {
166 continue;
167 }
168
169 if (!(object = ast_sorcery_alloc(sorcery, type, key)) ||
170 ast_sorcery_objectset_apply(sorcery, object, existing)) {
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)
204 || !(json = ast_json_load_string(value, &error))
205 || (ast_json_to_ast_variables(json, &objset) != AST_JSON_TO_AST_VARS_CODE_SUCCESS)
206 || !(objset = sorcery_astdb_filter_objectset(objset, sorcery, type))
207 || !(object = ast_sorcery_alloc(sorcery, type, id))
208 || ast_sorcery_objectset_apply(sorcery, object, objset)) {
209 ast_debug(3, "Failed to retrieve object '%s' from astdb\n", id);
210 ao2_cleanup(object);
211 return NULL;
212 }
213
214 return object;
215 }
216
217 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)
218 {
219 sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, objects);
220 }
221
222 /*!
223 * \internal
224 * \brief Convert regex prefix pattern to astDB prefix pattern if possible.
225 *
226 * \param tree astDB prefix pattern buffer to fill.
227 * \param regex Extended regular expression with the start anchor character '^'.
228 *
229 * \note Since this is a helper function, the tree buffer is
230 * assumed to always be large enough.
231 *
232 * \retval 0 on success.
233 * \retval -1 on error. regex is invalid.
234 */
235 static int make_astdb_prefix_pattern(char *tree, const char *regex)
236 {
237 const char *src;
238 char *dst;
239
240 for (dst = tree, src = regex + 1; *src; ++src) {
241 if (*src == '\\') {
242 /* Escaped regex char. */
243 ++src;
244 if (!*src) {
245 /* Invalid regex. The caller escaped the string terminator. */
246 return -1;
247 }
248 } else if (*src == '$') {
249 if (!src[1]) {
250 /* Remove the tail anchor character. */
251 *dst = '\0';
252 return 0;
253 }
254 } else if (strchr(".?*+{[(|", *src)) {
255 /*
256 * The regex is not a simple prefix pattern.
257 *
258 * XXX With more logic, it is possible to simply
259 * use the current prefix pattern. The last character
260 * needs to be removed if possible when the current regex
261 * token is "?*{". Also the rest of the regex pattern
262 * would need to be checked for subgroup/alternation.
263 * Subgroup/alternation is too complex for a simple prefix
264 * match.
265 */
266 dst = tree;
267 break;
268 }
269 *dst++ = *src;
270 }
271 if (dst != tree) {
272 *dst++ = '%';
273 }
274 *dst = '\0';
275 return 0;
276 }
277
278 static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
279 {
280 const char *prefix = data;
281 char family[strlen(prefix) + strlen(type) + 2];
282 char tree[strlen(regex) + 1];
283 RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
284 regex_t expression;
285 struct ast_db_entry *entry;
286
287 snprintf(family, sizeof(family), "%s/%s", prefix, type);
288
289 if (regex[0] == '^') {
290 /*
291 * For performance reasons, try to create an astDB prefix
292 * pattern from the regex to reduce the number of entries
293 * retrieved from astDB for regex to then match.
294 */
295 if (make_astdb_prefix_pattern(tree, regex)) {
296 return;
297 }
298 } else {
299 tree[0] = '\0';
300 }
301
302 if (!(entries = ast_db_gettree(family, tree))
303 || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
304 return;
305 }
306
307 for (entry = entries; entry; entry = entry->next) {
308 /* The key in the entry includes the family, so we need to strip it out for regex purposes */
309 const char *key = entry->key + strlen(family) + 2;
310 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
311 struct ast_json_error error;
312 RAII_VAR(void *, object, NULL, ao2_cleanup);
313 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
314
315 if (regexec(&expression, key, 0, NULL, 0)) {
316 continue;
317 } else if (!(json = ast_json_load_string(entry->data, &error))
318 || (ast_json_to_ast_variables(json, &objset) != AST_JSON_TO_AST_VARS_CODE_SUCCESS)
319 || !(objset = sorcery_astdb_filter_objectset(objset, sorcery, type))
320 || !(object = ast_sorcery_alloc(sorcery, type, key))
321 || ast_sorcery_objectset_apply(sorcery, object, objset)) {
322 regfree(&expression);
323 return;
324 }
325
326 ao2_link(objects, object);
327 }
328
329 regfree(&expression);
330 }
331
332 static void sorcery_astdb_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len)
333 {
334 const char *family_prefix = data;
335 size_t family_len = strlen(family_prefix) + strlen(type) + 1; /* +1 for slash delimiter */
336 char family[family_len + 1];
337 char tree[prefix_len + 1];
338 RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
339 struct ast_db_entry *entry;
340
341 snprintf(tree, sizeof(tree), "%.*s", (int) prefix_len, prefix);
342 snprintf(family, sizeof(family), "%s/%s", family_prefix, type);
343
344 if (!(entries = ast_db_gettree_by_prefix(family, tree))) {
345 return;
346 }
347
348 for (entry = entries; entry; entry = entry->next) {
349 /* The key in the entry includes the family, so we need to strip it out */
350 const char *key = entry->key + family_len + 2;
351 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
352 struct ast_json_error error;
353 RAII_VAR(void *, object, NULL, ao2_cleanup);
354 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
355
356 if (!(json = ast_json_load_string(entry->data, &error))
357 || (ast_json_to_ast_variables(json, &objset) != AST_JSON_TO_AST_VARS_CODE_SUCCESS)
358 || !(objset = sorcery_astdb_filter_objectset(objset, sorcery, type))
359 || !(object = ast_sorcery_alloc(sorcery, type, key))
360 || ast_sorcery_objectset_apply(sorcery, object, objset)) {
361 return;
362 }
363
364 ao2_link(objects, object);
365 }
366 }
367
368 static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object)
369 {
370 const char *prefix = data;
371 char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2], value[2];
372
373 snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
374
375 /* It is okay for the value to be truncated, we are only checking that it exists */
376 if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value)) && !ast_sorcery_update_or_create_on_update_miss) {
377 return -1;
378 }
379
380 /* The only difference between update and create is that for update the object must already exist */
381 return sorcery_astdb_create(sorcery, data, object);
382 }
383
384 static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object)
385 {
386 const char *prefix = data;
387 char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
388 char value[2];
389
390 snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
391
392 if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
393 return -1;
394 }
395
396 return ast_db_del(family, ast_sorcery_object_get_id(object));
397 }
398
399 static void *sorcery_astdb_open(const char *data)
400 {
401 /* We require a prefix for family string generation, or else stuff could mix together */
402 if (ast_strlen_zero(data)) {
403 return NULL;
404 }
405
406 return ast_strdup(data);
407 }
408
409 static void sorcery_astdb_close(void *data)
410 {
411 ast_free(data);
412 }
413
414 static int load_module(void)
415 {
416 if (ast_sorcery_wizard_register(&astdb_object_wizard)) {
417 return AST_MODULE_LOAD_DECLINE;
418 }
419
420 return AST_MODULE_LOAD_SUCCESS;
421 }
422
423 static int unload_module(void)
424 {
425 ast_sorcery_wizard_unregister(&astdb_object_wizard);
426 return 0;
427 }
428
429 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Astdb Object Wizard",
430 .support_level = AST_MODULE_SUPPORT_CORE,
431 .load = load_module,
432 .unload = unload_module,
433 .load_pri = AST_MODPRI_REALTIME_DRIVER,
434 );