Debian Codegraph @

res/res_sorcery_config.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 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 Configuration File 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/astobj2.h"
38 #include "asterisk/config.h"
39 #include "asterisk/uuid.h"
40 #include "asterisk/hashtab.h"
41
42 /*! \brief Structure for storing configuration file sourced objects */
43 struct sorcery_config {
44 /*! \brief UUID for identifying us when opening a configuration file */
45 char uuid[AST_UUID_STR_LEN];
46
47 /*! \brief Objects retrieved from the configuration file */
48 struct ao2_global_obj objects;
49
50 /*! \brief Any specific variable criteria for considering a defined category for this object */
51 struct ast_variable *criteria;
52
53 /*! \brief An explicit name for the configuration section, with it there can be only one */
54 char *explicit_name;
55
56 /*! \brief Number of buckets to use for objects */
57 unsigned int buckets;
58
59 /*! \brief Enable file level integrity instead of object level */
60 unsigned int file_integrity:1;
61
62 /*! \brief Enable enforcement of a single configuration object of this type */
63 unsigned int single_object:1;
64
65 /*! \brief Configuration is invalid in some way, force reload */
66 unsigned int configuration_invalid:1;
67
68 /*! \brief Configuration contains at least one object with dynamic contents */
69 unsigned int has_dynamic_contents:1;
70
71 /*! \brief Filename of the configuration file */
72 char filename[];
73 };
74
75 /*! \brief Structure used for fields comparison */
76 struct sorcery_config_fields_cmp_params {
77 /*! \brief Pointer to the sorcery structure */
78 const struct ast_sorcery *sorcery;
79
80 /*! \brief Pointer to the fields to check */
81 const struct ast_variable *fields;
82
83 /*! \brief Regular expression for checking object id */
84 regex_t *regex;
85
86 /*! \brief Prefix for matching object id */
87 const char *prefix;
88
89 /*! \brief Prefix length in bytes for matching object id */
90 const size_t prefix_len;
91
92 /*! \brief Optional container to put object into */
93 struct ao2_container *container;
94 };
95
96 static void *sorcery_config_open(const char *data);
97 static void sorcery_config_load(void *data, const struct ast_sorcery *sorcery, const char *type);
98 static void sorcery_config_reload(void *data, const struct ast_sorcery *sorcery, const char *type);
99 static void *sorcery_config_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
100 static void *sorcery_config_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
101 static void sorcery_config_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
102 const struct ast_variable *fields);
103 static void sorcery_config_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
104 static void sorcery_config_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len);
105 static void sorcery_config_close(void *data);
106
107 static struct ast_sorcery_wizard config_object_wizard = {
108 .name = "config",
109 .open = sorcery_config_open,
110 .load = sorcery_config_load,
111 .reload = sorcery_config_reload,
112 .force_reload = sorcery_config_load,
113 .retrieve_id = sorcery_config_retrieve_id,
114 .retrieve_fields = sorcery_config_retrieve_fields,
115 .retrieve_multiple = sorcery_config_retrieve_multiple,
116 .retrieve_regex = sorcery_config_retrieve_regex,
117 .retrieve_prefix = sorcery_config_retrieve_prefix,
118 .close = sorcery_config_close,
119 };
120
121 /*! \brief Destructor function for sorcery config */
122 static void sorcery_config_destructor(void *obj)
123 {
124 struct sorcery_config *config = obj;
125
126 ao2_global_obj_release(config->objects);
127 ast_rwlock_destroy(&config->objects.lock);
128 ast_variables_destroy(config->criteria);
129 ast_free(config->explicit_name);
130 }
131
132 static int sorcery_config_fields_cmp(void *obj, void *arg, int flags)
133 {
134 const struct sorcery_config_fields_cmp_params *params = arg;
135 RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
136
137 if (params->regex) {
138 /* If a regular expression has been provided see if it matches, otherwise move on */
139 if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
140 ao2_link(params->container, obj);
141 }
142 return 0;
143 } else if (params->prefix) {
144 if (!strncmp(params->prefix, ast_sorcery_object_get_id(obj), params->prefix_len)) {
145 ao2_link(params->container, obj);
146 }
147 return 0;
148 } else if (params->fields &&
149 (!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
150 (!ast_variable_lists_match(objset, params->fields, 0)))) {
151 /* If we can't turn the object into an object set OR if differences exist between the fields
152 * passed in and what are present on the object they are not a match.
153 */
154 return 0;
155 }
156
157 /* We want this object */
158 if (params->container) {
159 /*
160 * We are putting the found objects into the given container instead
161 * of the normal container traversal return mechanism.
162 */
163 ao2_link(params->container, obj);
164 return 0;
165 } else {
166 return CMP_MATCH;
167 }
168 }
169
170 static void *sorcery_config_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
171 {
172 struct sorcery_config *config = data;
173 RAII_VAR(struct ao2_container *, objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
174 struct sorcery_config_fields_cmp_params params = {
175 .sorcery = sorcery,
176 .fields = fields,
177 .container = NULL,
178 };
179
180 /* If no fields are present return nothing, we require *something*, same goes if no objects exist yet */
181 if (!objects || !fields) {
182 return NULL;
183 }
184
185 return ao2_callback(objects, 0, sorcery_config_fields_cmp, &params);
186 }
187
188 static void *sorcery_config_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
189 {
190 struct sorcery_config *config = data;
191 RAII_VAR(struct ao2_container *, objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
192
193 return objects ? ao2_find(objects, id, OBJ_SEARCH_KEY) : NULL;
194 }
195
196 static void sorcery_config_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
197 {
198 struct sorcery_config *config = data;
199 RAII_VAR(struct ao2_container *, config_objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
200 struct sorcery_config_fields_cmp_params params = {
201 .sorcery = sorcery,
202 .fields = fields,
203 .container = objects,
204 };
205
206 if (!config_objects) {
207 return;
208 }
209
210 ao2_callback(config_objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_config_fields_cmp, &params);
211 }
212
213 static void sorcery_config_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
214 {
215 struct sorcery_config *config = data;
216 RAII_VAR(struct ao2_container *, config_objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
217 regex_t expression;
218 struct sorcery_config_fields_cmp_params params = {
219 .sorcery = sorcery,
220 .container = objects,
221 .regex = &expression,
222 };
223
224 if (ast_strlen_zero(regex)) {
225 regex = ".";
226 }
227
228 if (!config_objects || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
229 return;
230 }
231
232 ao2_callback(config_objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_config_fields_cmp, &params);
233 regfree(&expression);
234 }
235
236 static void sorcery_config_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len)
237 {
238 struct sorcery_config *config = data;
239 RAII_VAR(struct ao2_container *, config_objects, ao2_global_obj_ref(config->objects), ao2_cleanup);
240 struct sorcery_config_fields_cmp_params params = {
241 .sorcery = sorcery,
242 .container = objects,
243 .prefix = prefix,
244 .prefix_len = prefix_len,
245 };
246
247 if (!config_objects) {
248 return;
249 }
250
251 ao2_callback(config_objects, OBJ_NODATA | OBJ_MULTIPLE, sorcery_config_fields_cmp, &params);
252 }
253
254 /*! \brief Internal function which determines if a category matches based on explicit name */
255 static int sorcery_is_explicit_name_met(const struct ast_sorcery *sorcery, const char *type,
256 struct ast_category *category, struct sorcery_config *config)
257 {
258 struct ast_sorcery_object_type *object_type;
259 struct ast_variable *field;
260 int met = 1;
261
262 if (ast_strlen_zero(config->explicit_name) || strcmp(ast_category_get_name(category), config->explicit_name)) {
263 return 0;
264 }
265
266 object_type = ast_sorcery_get_object_type(sorcery, type);
267 if (!object_type) {
268 return 0;
269 }
270
271 /* We iterate the configured fields to see if we don't know any, if we don't then
272 * this is likely not for the given type and we skip it. If it actually is then criteria
273 * may pick it up in which case it would just get rejected as an invalid configuration later.
274 */
275 for (field = ast_category_first(category); field; field = field->next) {
276 if (!ast_sorcery_is_object_field_registered(object_type, field->name)) {
277 met = 0;
278 break;
279 }
280 }
281
282 ao2_ref(object_type, -1);
283
284 return met;
285 }
286
287 /*! \brief Internal function which determines if a category matches based on criteria */
288 static int sorcery_is_criteria_met(struct ast_category *category, struct sorcery_config *config)
289 {
290 RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy);
291
292 if (!config->criteria) {
293 return 0;
294 }
295
296 return (!ast_sorcery_changeset_create(ast_category_first(category), config->criteria, &diff) && !diff) ? 1 : 0;
297 }
298
299 /*! \brief Internal function which determines if criteria has been met for considering an object set applicable */
300 static int sorcery_is_configuration_met(const struct ast_sorcery *sorcery, const char *type,
301 struct ast_category *category, struct sorcery_config *config)
302 {
303 if (!config->criteria && ast_strlen_zero(config->explicit_name)) {
304 /* Nothing is configured to allow specific matching, so accept it! */
305 return 1;
306 } else if (sorcery_is_explicit_name_met(sorcery, type, category, config)) {
307 return 1;
308 } else if (sorcery_is_criteria_met(category, config)) {
309 return 1;
310 } else {
311 /* Nothing explicitly matched so reject */
312 return 0;
313 }
314 }
315
316 static void sorcery_config_internal_load(void *data, const struct ast_sorcery *sorcery, const char *type, unsigned int reload)
317 {
318 struct sorcery_config *config = data;
319 struct ast_flags flags = { reload && !config->configuration_invalid && !config->has_dynamic_contents ? CONFIG_FLAG_FILEUNCHANGED : 0 };
320 struct ast_config *cfg = ast_config_load2(config->filename, config->uuid, flags);
321 struct ast_category *category = NULL;
322 RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
323 const char *id = NULL;
324 unsigned int buckets = 0;
325 unsigned int has_dynamic_contents = 0;
326
327 if (!cfg) {
328 ast_log(LOG_ERROR, "Unable to load config file '%s'\n", config->filename);
329 return;
330 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
331 ast_debug(1, "Config file '%s' was unchanged\n", config->filename);
332 return;
333 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
334 ast_log(LOG_ERROR, "Contents of config file '%s' are invalid and cannot be parsed\n", config->filename);
335 return;
336 }
337
338 /* When parsing the configuration assume it is valid until proven otherwise */
339 config->configuration_invalid = 0;
340
341 if (!config->buckets) {
342 while ((category = ast_category_browse_filtered(cfg, NULL, category, NULL))) {
343
344 /* If given configuration has not been met skip the category, it is not applicable */
345 if (!sorcery_is_configuration_met(sorcery, type, category, config)) {
346 continue;
347 }
348
349 buckets++;
350 }
351
352 /* Determine the optimal number of buckets */
353 while (buckets && !ast_is_prime(buckets)) {
354 /* This purposely goes backwards to ensure that the container doesn't have a ton of
355 * empty buckets for objects that will never get added.
356 */
357 buckets--;
358 }
359
360 if (!buckets) {
361 buckets = 1;
362 }
363 } else {
364 buckets = config->buckets;
365 }
366
367 /* For single object configurations there can only ever be one bucket, if there's more than the single
368 * object requirement has been violated.
369 */
370 if (config->single_object && buckets > 1) {
371 ast_log(LOG_ERROR, "Config file '%s' could not be loaded; configuration contains more than one object of type '%s'\n",
372 config->filename, type);
373 ast_config_destroy(cfg);
374 config->configuration_invalid = 1;
375 return;
376 }
377
378 ast_debug(2, "Using bucket size of '%d' for objects of type '%s' from '%s'\n",
379 buckets, type, config->filename);
380
381 objects = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, buckets,
382 ast_sorcery_object_id_hash, NULL, ast_sorcery_object_id_compare);
383 if (!objects) {
384 ast_log(LOG_ERROR, "Could not create bucket for new objects from '%s', keeping existing objects\n",
385 config->filename);
386 ast_config_destroy(cfg);
387 config->configuration_invalid = 1; /* Not strictly invalid but we want to try next time */
388 return;
389 }
390
391 while ((category = ast_category_browse_filtered(cfg, NULL, category, NULL))) {
392 RAII_VAR(void *, obj, NULL, ao2_cleanup);
393 id = ast_category_get_name(category);
394
395 /* If given configuration has not been met skip the category, it is not applicable */
396 if (!sorcery_is_configuration_met(sorcery, type, category, config)) {
397 continue;
398 }
399
400 /* Confirm an object with this id does not already exist in the bucket.
401 * If it exists, however, the configuration is invalid so stop
402 * processing and destroy it. */
403 obj = ao2_find(objects, id, OBJ_SEARCH_KEY);
404 if (obj) {
405 ast_log(LOG_ERROR, "Config file '%s' could not be loaded; configuration contains a duplicate object: '%s' of type '%s'\n",
406 config->filename, id, type);
407 ast_config_destroy(cfg);
408 config->configuration_invalid = 1;
409 return;
410 }
411
412 if (!(obj = ast_sorcery_alloc(sorcery, type, id)) ||
413 ast_sorcery_objectset_apply(sorcery, obj, ast_category_first(category))) {
414
415 if (config->file_integrity) {
416 ast_log(LOG_ERROR, "Config file '%s' could not be loaded due to error with object '%s' of type '%s'\n",
417 config->filename, id, type);
418 ast_config_destroy(cfg);
419 config->configuration_invalid = 1;
420 return;
421 } else {
422 ast_log(LOG_ERROR, "Could not create an object of type '%s' with id '%s' from configuration file '%s'\n",
423 type, id, config->filename);
424 config->configuration_invalid = 1;
425 }
426
427 ao2_cleanup(obj);
428
429 /* To ensure we don't lose the object that already exists we retrieve it from the old objects container and add it to the new one */
430 if (!(obj = sorcery_config_retrieve_id(sorcery, data, type, id))) {
431 continue;
432 }
433
434 ast_log(LOG_NOTICE, "Retaining existing configuration for object of type '%s' with id '%s'\n", type, id);
435 }
436
437 /* We store the dynamic contents state until the end in case this reload or load
438 * gets rolled back.
439 */
440 has_dynamic_contents |= ast_sorcery_object_has_dynamic_contents(obj);
441
442 ao2_link(objects, obj);
443 }
444
445 config->has_dynamic_contents = has_dynamic_contents;
446 ao2_global_obj_replace_unref(config->objects, objects);
447 ast_config_destroy(cfg);
448 }
449
450 static void sorcery_config_load(void *data, const struct ast_sorcery *sorcery, const char *type)
451 {
452 sorcery_config_internal_load(data, sorcery, type, 0);
453 }
454
455 static void sorcery_config_reload(void *data, const struct ast_sorcery *sorcery, const char *type)
456 {
457 sorcery_config_internal_load(data, sorcery, type, 1);
458 }
459
460 static void *sorcery_config_open(const char *data)
461 {
462 char *tmp;
463 char *filename;
464 char *option;
465 struct sorcery_config *config;
466
467 if (ast_strlen_zero(data)) {
468 return NULL;
469 }
470
471 tmp = ast_strdupa(data);
472 filename = strsep(&tmp, ",");
473
474 if (ast_strlen_zero(filename) || !(config = ao2_alloc_options(sizeof(*config) + strlen(filename) + 1, sorcery_config_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK))) {
475 return NULL;
476 }
477
478 ast_uuid_generate_str(config->uuid, sizeof(config->uuid));
479
480 ast_rwlock_init(&config->objects.lock);
481 strcpy(config->filename, filename);
482
483 while ((option = strsep(&tmp, ","))) {
484 char *name = strsep(&option, "="), *value = option;
485
486 if (!strcasecmp(name, "buckets")) {
487 if (sscanf(value, "%30u", &config->buckets) != 1) {
488 ast_log(LOG_ERROR, "Unsupported bucket size of '%s' used for configuration file '%s', defaulting to automatic determination\n",
489 value, filename);
490 }
491 } else if (!strcasecmp(name, "integrity")) {
492 if (!strcasecmp(value, "file")) {
493 config->file_integrity = 1;
494 } else if (!strcasecmp(value, "object")) {
495 config->file_integrity = 0;
496 } else {
497 ast_log(LOG_ERROR, "Unsupported integrity value of '%s' used for configuration file '%s', defaulting to 'object'\n",
498 value, filename);
499 }
500 } else if (!strcasecmp(name, "criteria")) {
501 char *field = strsep(&value, "=");
502 struct ast_variable *criteria = ast_variable_new(field, value, "");
503
504 if (criteria) {
505 criteria->next = config->criteria;
506 config->criteria = criteria;
507 } else {
508 /* This is fatal since not following criteria would potentially yield invalid objects */
509 ast_log(LOG_ERROR, "Could not create criteria entry of field '%s' with value '%s' for configuration file '%s'\n",
510 field, value, filename);
511 ao2_ref(config, -1);
512 return NULL;
513 }
514 } else if (!strcasecmp(name, "explicit_name")) {
515 ast_free(config->explicit_name);
516 config->explicit_name = ast_strdup(value);
517 if (ast_strlen_zero(config->explicit_name)) {
518 /* This is fatal since it could stop a configuration section from getting applied */
519 ast_log(LOG_ERROR, "Could not create explicit name entry of '%s' for configuration file '%s'\n",
520 value, filename);
521 ao2_ref(config, -1);
522 return NULL;
523 }
524 } else if (!strcasecmp(name, "single_object")) {
525 if (ast_strlen_zero(value)) {
526 ast_log(LOG_ERROR, "Could not set single object value for configuration file '%s' as the value is empty\n",
527 filename);
528 ao2_ref(config, -1);
529 return NULL;
530 }
531 config->single_object = ast_true(value);
532 } else {
533 ast_log(LOG_ERROR, "Unsupported option '%s' used for configuration file '%s'\n", name, filename);
534 }
535 }
536
537 return config;
538 }
539
540 static void sorcery_config_close(void *data)
541 {
542 struct sorcery_config *config = data;
543
544 ao2_ref(config, -1);
545 }
546
547 static int load_module(void)
548 {
549 if (ast_sorcery_wizard_register(&config_object_wizard)) {
550 return AST_MODULE_LOAD_DECLINE;
551 }
552
553 return AST_MODULE_LOAD_SUCCESS;
554 }
555
556 static int unload_module(void)
557 {
558 ast_sorcery_wizard_unregister(&config_object_wizard);
559 return 0;
560 }
561
562 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Configuration File Object Wizard",
563 .support_level = AST_MODULE_SUPPORT_CORE,
564 .load = load_module,
565 .unload = unload_module,
566 .load_pri = AST_MODPRI_REALTIME_DRIVER,
567 );