res_sorcery_astdb.c 12 KB

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