res_sorcery_realtime.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 Realtime 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. /*! \brief They key field used to store the unique identifier for the object */
  34. #define UUID_FIELD "id"
  35. enum unqualified_fetch {
  36. UNQUALIFIED_FETCH_NO,
  37. UNQUALIFIED_FETCH_WARN,
  38. UNQUALIFIED_FETCH_YES,
  39. UNQUALIFIED_FETCH_ERROR,
  40. };
  41. struct sorcery_config {
  42. enum unqualified_fetch fetch;
  43. char family[];
  44. };
  45. static void *sorcery_realtime_open(const char *data);
  46. static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object);
  47. static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
  48. static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
  49. static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
  50. const struct ast_variable *fields);
  51. static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
  52. static void sorcery_realtime_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type,
  53. struct ao2_container *objects, const char *prefix, const size_t prefix_len);
  54. static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object);
  55. static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object);
  56. static void sorcery_realtime_close(void *data);
  57. static struct ast_sorcery_wizard realtime_object_wizard = {
  58. .name = "realtime",
  59. .open = sorcery_realtime_open,
  60. .create = sorcery_realtime_create,
  61. .retrieve_id = sorcery_realtime_retrieve_id,
  62. .retrieve_fields = sorcery_realtime_retrieve_fields,
  63. .retrieve_multiple = sorcery_realtime_retrieve_multiple,
  64. .retrieve_regex = sorcery_realtime_retrieve_regex,
  65. .retrieve_prefix = sorcery_realtime_retrieve_prefix,
  66. .update = sorcery_realtime_update,
  67. .delete = sorcery_realtime_delete,
  68. .close = sorcery_realtime_close,
  69. };
  70. static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object)
  71. {
  72. struct sorcery_config *config = data;
  73. RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
  74. struct ast_variable *id = ast_variable_new(UUID_FIELD, ast_sorcery_object_get_id(object), "");
  75. if (!fields || !id) {
  76. ast_variables_destroy(id);
  77. return -1;
  78. }
  79. /* Place the identifier at the front for sanity sake */
  80. id->next = fields;
  81. fields = id;
  82. return (ast_store_realtime_fields(config->family, fields) <= 0) ? -1 : 0;
  83. }
  84. /*! \brief Internal helper function which returns a filtered objectset.
  85. *
  86. * The following are filtered out of the objectset:
  87. * \li The id field. This is returned to the caller in an out parameter.
  88. * \li Fields that are not registered with sorcery.
  89. *
  90. * \param objectset Objectset to filter.
  91. * \param[out] id The ID of the sorcery object, as found in the objectset.
  92. * \param sorcery The sorcery instance that is requesting an objectset.
  93. * \param type The object type
  94. *
  95. * \return The filtered objectset
  96. */
  97. static struct ast_variable *sorcery_realtime_filter_objectset(struct ast_variable *objectset, struct ast_variable **id,
  98. const struct ast_sorcery *sorcery, const char *type)
  99. {
  100. struct ast_variable *previous = NULL, *field = objectset;
  101. struct ast_sorcery_object_type *object_type;
  102. object_type = ast_sorcery_get_object_type(sorcery, type);
  103. if (!object_type) {
  104. ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
  105. /* Continue since we still want to filter out the id */
  106. }
  107. while (field) {
  108. int remove_field = 0;
  109. int delete_field = 0;
  110. if (!strcmp(field->name, UUID_FIELD)) {
  111. *id = field;
  112. remove_field = 1;
  113. } else if (object_type &&
  114. !ast_sorcery_is_object_field_registered(object_type, field->name)) {
  115. ast_debug(1, "Filtering out realtime field '%s' from retrieval\n", field->name);
  116. remove_field = 1;
  117. delete_field = 1;
  118. }
  119. if (remove_field) {
  120. struct ast_variable *removed;
  121. if (previous) {
  122. previous->next = field->next;
  123. } else {
  124. objectset = field->next;
  125. }
  126. removed = field;
  127. field = field->next;
  128. removed->next = NULL;
  129. if (delete_field) {
  130. ast_variables_destroy(removed);
  131. }
  132. } else {
  133. previous = field;
  134. field = field->next;
  135. }
  136. }
  137. ao2_cleanup(object_type);
  138. return objectset;
  139. }
  140. static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
  141. {
  142. struct sorcery_config *config = data;
  143. RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
  144. RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
  145. void *object = NULL;
  146. if (!(objectset = ast_load_realtime_fields(config->family, fields))) {
  147. return NULL;
  148. }
  149. objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
  150. if (!id
  151. || !(object = ast_sorcery_alloc(sorcery, type, id->value))
  152. || ast_sorcery_objectset_apply(sorcery, object, objectset)) {
  153. ao2_cleanup(object);
  154. return NULL;
  155. }
  156. return object;
  157. }
  158. static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
  159. {
  160. RAII_VAR(struct ast_variable *, fields, ast_variable_new(UUID_FIELD, id, ""), ast_variables_destroy);
  161. return sorcery_realtime_retrieve_fields(sorcery, data, type, fields);
  162. }
  163. static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
  164. {
  165. struct sorcery_config *config = data;
  166. RAII_VAR(struct ast_config *, rows, NULL, ast_config_destroy);
  167. RAII_VAR(struct ast_variable *, all, NULL, ast_variables_destroy);
  168. struct ast_category *row = NULL;
  169. if (!fields) {
  170. char field[strlen(UUID_FIELD) + 6], value[2];
  171. if (config->fetch == UNQUALIFIED_FETCH_NO) {
  172. return;
  173. }
  174. if (config->fetch == UNQUALIFIED_FETCH_ERROR) {
  175. ast_log(LOG_ERROR, "Unqualified fetch prevented on %s\n", config->family);
  176. return;
  177. }
  178. if (config->fetch == UNQUALIFIED_FETCH_WARN) {
  179. ast_log(LOG_WARNING, "Unqualified fetch requested on %s\n", config->family);
  180. }
  181. /* If no fields have been specified we want all rows, so trick realtime into doing it */
  182. snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
  183. snprintf(value, sizeof(value), "%%");
  184. if (!(all = ast_variable_new(field, value, ""))) {
  185. return;
  186. }
  187. fields = all;
  188. }
  189. if (!(rows = ast_load_realtime_multientry_fields(config->family, fields))) {
  190. return;
  191. }
  192. while ((row = ast_category_browse_filtered(rows, NULL, row, NULL))) {
  193. struct ast_variable *objectset = ast_category_detach_variables(row);
  194. RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
  195. RAII_VAR(void *, object, NULL, ao2_cleanup);
  196. objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);
  197. if (id
  198. && (object = ast_sorcery_alloc(sorcery, type, id->value))
  199. && !ast_sorcery_objectset_apply(sorcery, object, objectset)) {
  200. ao2_link(objects, object);
  201. }
  202. ast_variables_destroy(objectset);
  203. }
  204. }
  205. static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
  206. {
  207. char field[strlen(UUID_FIELD) + 6], value[strlen(regex) + 3];
  208. RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
  209. if (!ast_strlen_zero(regex)) {
  210. /* The realtime API provides no direct ability to do regex so for now we support a limited subset using pattern matching */
  211. snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
  212. if (regex[0] == '^') {
  213. snprintf(value, sizeof(value), "%s%%", regex + 1);
  214. } else {
  215. snprintf(value, sizeof(value), "%%%s%%", regex);
  216. }
  217. if (!(fields = ast_variable_new(field, value, ""))) {
  218. return;
  219. }
  220. }
  221. sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
  222. }
  223. static void sorcery_realtime_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type,
  224. struct ao2_container *objects, const char *prefix, const size_t prefix_len)
  225. {
  226. char field[strlen(UUID_FIELD) + 6], value[prefix_len + 2];
  227. RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);
  228. if (prefix_len) {
  229. snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
  230. snprintf(value, sizeof(value), "%.*s%%", (int) prefix_len, prefix);
  231. if (!(fields = ast_variable_new(field, value, ""))) {
  232. return;
  233. }
  234. }
  235. sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
  236. }
  237. static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object)
  238. {
  239. struct sorcery_config *config = data;
  240. RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
  241. if (!fields) {
  242. return -1;
  243. }
  244. return (ast_update_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), fields) < 0) ? -1 : 0;
  245. }
  246. static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object)
  247. {
  248. struct sorcery_config *config = data;
  249. return (ast_destroy_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), NULL) <= 0) ? -1 : 0;
  250. }
  251. static void *sorcery_realtime_open(const char *data)
  252. {
  253. struct sorcery_config *config;
  254. char *tmp;
  255. char *family;
  256. char *option;
  257. /* We require a prefix for family string generation, or else stuff could mix together */
  258. if (ast_strlen_zero(data)) {
  259. return NULL;
  260. }
  261. tmp = ast_strdupa(data);
  262. family = strsep(&tmp, ",");
  263. if (!ast_realtime_is_mapping_defined(family)) {
  264. return NULL;
  265. }
  266. config = ast_calloc(1, sizeof(*config) + strlen(family) + 1);
  267. if (!config) {
  268. return NULL;
  269. }
  270. strcpy(config->family, family); /* Safe */
  271. config->fetch = UNQUALIFIED_FETCH_YES;
  272. while ((option = strsep(&tmp, ","))) {
  273. char *name = strsep(&option, "=");
  274. char *value = option;
  275. if (!strcasecmp(name, "allow_unqualified_fetch")) {
  276. if (ast_strlen_zero(value) || !strcasecmp(value, "yes")) {
  277. config->fetch = UNQUALIFIED_FETCH_YES;
  278. } else if (!strcasecmp(value, "no")) {
  279. config->fetch = UNQUALIFIED_FETCH_NO;
  280. } else if (!strcasecmp(value, "warn")) {
  281. config->fetch = UNQUALIFIED_FETCH_WARN;
  282. } else if (!strcasecmp(value, "error")) {
  283. config->fetch = UNQUALIFIED_FETCH_ERROR;
  284. } else {
  285. ast_log(LOG_ERROR, "Unrecognized value in %s:%s: '%s'\n", family, name, value);
  286. return NULL;
  287. }
  288. } else {
  289. ast_log(LOG_ERROR, "Unrecognized option in %s: '%s'\n", family, name);
  290. return NULL;
  291. }
  292. }
  293. return config;
  294. }
  295. static void sorcery_realtime_close(void *data)
  296. {
  297. ast_free(data);
  298. }
  299. static int load_module(void)
  300. {
  301. if (ast_sorcery_wizard_register(&realtime_object_wizard)) {
  302. return AST_MODULE_LOAD_DECLINE;
  303. }
  304. return AST_MODULE_LOAD_SUCCESS;
  305. }
  306. static int unload_module(void)
  307. {
  308. ast_sorcery_wizard_unregister(&realtime_object_wizard);
  309. return 0;
  310. }
  311. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Realtime Object Wizard",
  312. .support_level = AST_MODULE_SUPPORT_CORE,
  313. .load = load_module,
  314. .unload = unload_module,
  315. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  316. );