cel_sqlite3_custom.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Digium, Inc.
  5. *
  6. * Steve Murphy <murf@digium.com> borrowed code from cdr,
  7. * Mark Spencer <markster@digium.com> and others.
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Custom SQLite3 CEL records.
  22. *
  23. * \author Adapted by Steve Murphy <murf@digium.com> from
  24. * Alejandro Rios <alejandro.rios@avatar.com.co> and
  25. * Russell Bryant <russell@digium.com> from
  26. * cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
  27. * and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
  28. *
  29. *
  30. * \arg See also \ref AstCEL
  31. *
  32. *
  33. * \ingroup cel_drivers
  34. */
  35. /*** MODULEINFO
  36. <depend>sqlite3</depend>
  37. <support_level>extended</support_level>
  38. ***/
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include <sqlite3.h>
  42. #include "asterisk/paths.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/cel.h"
  45. #include "asterisk/module.h"
  46. #include "asterisk/config.h"
  47. #include "asterisk/pbx.h"
  48. #include "asterisk/logger.h"
  49. #include "asterisk/utils.h"
  50. #include "asterisk/cli.h"
  51. #include "asterisk/options.h"
  52. #include "asterisk/stringfields.h"
  53. AST_MUTEX_DEFINE_STATIC(lock);
  54. static const char config_file[] = "cel_sqlite3_custom.conf";
  55. static sqlite3 *db = NULL;
  56. static char table[80];
  57. /*! XXX \bug Handling of this var is crash prone on reloads */
  58. static char *columns;
  59. static struct ast_event_sub *event_sub = NULL;
  60. struct values {
  61. char *expression;
  62. AST_LIST_ENTRY(values) list;
  63. };
  64. static AST_LIST_HEAD_STATIC(sql_values, values);
  65. static void free_config(void);
  66. static int load_column_config(const char *tmp)
  67. {
  68. char *col = NULL;
  69. char *cols = NULL, *save = NULL;
  70. char *escaped = NULL;
  71. struct ast_str *column_string = NULL;
  72. if (ast_strlen_zero(tmp)) {
  73. ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
  74. return -1;
  75. }
  76. if (!(column_string = ast_str_create(1024))) {
  77. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  78. return -1;
  79. }
  80. if (!(save = cols = ast_strdup(tmp))) {
  81. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  82. ast_free(column_string);
  83. return -1;
  84. }
  85. while ((col = strsep(&cols, ","))) {
  86. col = ast_strip(col);
  87. escaped = sqlite3_mprintf("%q", col);
  88. if (!escaped) {
  89. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
  90. ast_free(column_string);
  91. ast_free(save);
  92. return -1;
  93. }
  94. ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
  95. sqlite3_free(escaped);
  96. }
  97. if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
  98. ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
  99. ast_free(column_string);
  100. ast_free(save);
  101. return -1;
  102. }
  103. ast_free(column_string);
  104. ast_free(save);
  105. return 0;
  106. }
  107. static int load_values_config(const char *tmp)
  108. {
  109. char *val = NULL;
  110. char *vals = NULL, *save = NULL;
  111. struct values *value = NULL;
  112. if (ast_strlen_zero(tmp)) {
  113. ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
  114. return -1;
  115. }
  116. if (!(save = vals = ast_strdup(tmp))) {
  117. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
  118. return -1;
  119. }
  120. while ((val = strsep(&vals, ","))) {
  121. /* Strip the single quotes off if they are there */
  122. val = ast_strip_quoted(val, "'", "'");
  123. value = ast_calloc(sizeof(char), sizeof(*value) + strlen(val) + 1);
  124. if (!value) {
  125. ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", val);
  126. ast_free(save);
  127. return -1;
  128. }
  129. value->expression = (char *) value + sizeof(*value);
  130. ast_copy_string(value->expression, val, strlen(val) + 1);
  131. AST_LIST_INSERT_TAIL(&sql_values, value, list);
  132. }
  133. ast_free(save);
  134. return 0;
  135. }
  136. static int load_config(int reload)
  137. {
  138. struct ast_config *cfg;
  139. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  140. struct ast_variable *mappingvar;
  141. const char *tmp;
  142. if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  143. ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n",
  144. reload ? "re" : "", reload ? "" : "Module not activated.");
  145. return -1;
  146. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  147. return 0;
  148. }
  149. if (reload) {
  150. free_config();
  151. }
  152. if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
  153. /* Nothing configured */
  154. ast_config_destroy(cfg);
  155. return -1;
  156. }
  157. /* Mapping must have a table name */
  158. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
  159. ast_copy_string(table, tmp, sizeof(table));
  160. } else {
  161. ast_log(LOG_WARNING, "Table name not specified. Assuming cel.\n");
  162. strcpy(table, "cel");
  163. }
  164. /* Columns */
  165. if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
  166. ast_config_destroy(cfg);
  167. free_config();
  168. return -1;
  169. }
  170. /* Values */
  171. if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
  172. ast_config_destroy(cfg);
  173. free_config();
  174. return -1;
  175. }
  176. ast_verb(3, "Logging CEL records to table '%s' in 'master.db'\n", table);
  177. ast_config_destroy(cfg);
  178. return 0;
  179. }
  180. static void free_config(void)
  181. {
  182. struct values *value;
  183. if (db) {
  184. sqlite3_close(db);
  185. db = NULL;
  186. }
  187. if (columns) {
  188. ast_free(columns);
  189. columns = NULL;
  190. }
  191. while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
  192. ast_free(value);
  193. }
  194. }
  195. static void write_cel(const struct ast_event *event, void *userdata)
  196. {
  197. char *error = NULL;
  198. char *sql = NULL;
  199. if (db == NULL) {
  200. /* Should not have loaded, but be failsafe. */
  201. return;
  202. }
  203. ast_mutex_lock(&lock);
  204. { /* Make it obvious that only sql should be used outside of this block */
  205. char *escaped;
  206. char subst_buf[2048];
  207. struct values *value;
  208. struct ast_channel *dummy;
  209. struct ast_str *value_string = ast_str_create(1024);
  210. dummy = ast_cel_fabricate_channel_from_event(event);
  211. if (!dummy) {
  212. ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
  213. ast_free(value_string);
  214. ast_mutex_unlock(&lock);
  215. return;
  216. }
  217. AST_LIST_TRAVERSE(&sql_values, value, list) {
  218. pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
  219. escaped = sqlite3_mprintf("%q", subst_buf);
  220. ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
  221. sqlite3_free(escaped);
  222. }
  223. sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
  224. ast_debug(1, "About to log: %s\n", sql);
  225. dummy = ast_channel_unref(dummy);
  226. ast_free(value_string);
  227. }
  228. if (sqlite3_exec(db, sql, NULL, NULL, &error) != SQLITE_OK) {
  229. ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
  230. sqlite3_free(error);
  231. }
  232. if (sql) {
  233. sqlite3_free(sql);
  234. }
  235. ast_mutex_unlock(&lock);
  236. return;
  237. }
  238. static int unload_module(void)
  239. {
  240. if (event_sub) {
  241. event_sub = ast_event_unsubscribe(event_sub);
  242. }
  243. free_config();
  244. return 0;
  245. }
  246. static int load_module(void)
  247. {
  248. char *error;
  249. char filename[PATH_MAX];
  250. int res;
  251. char *sql;
  252. if (load_config(0)) {
  253. return AST_MODULE_LOAD_DECLINE;
  254. }
  255. /* is the database there? */
  256. snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
  257. res = sqlite3_open(filename, &db);
  258. if (res != SQLITE_OK) {
  259. ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
  260. free_config();
  261. return AST_MODULE_LOAD_DECLINE;
  262. }
  263. sqlite3_busy_timeout(db, 1000);
  264. /* is the table there? */
  265. sql = sqlite3_mprintf("SELECT COUNT(*) FROM %q;", table);
  266. res = sqlite3_exec(db, sql, NULL, NULL, NULL);
  267. sqlite3_free(sql);
  268. if (res != SQLITE_OK) {
  269. /* We don't use %q for the column list here since we already escaped when building it */
  270. sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
  271. res = sqlite3_exec(db, sql, NULL, NULL, &error);
  272. sqlite3_free(sql);
  273. if (res != SQLITE_OK) {
  274. ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
  275. sqlite3_free(error);
  276. free_config();
  277. return AST_MODULE_LOAD_DECLINE;
  278. }
  279. }
  280. event_sub = ast_event_subscribe(AST_EVENT_CEL, write_cel, "CEL sqlite3 custom backend", NULL, AST_EVENT_IE_END);
  281. if (!event_sub) {
  282. ast_log(LOG_ERROR, "Unable to register custom SQLite3 CEL handling\n");
  283. free_config();
  284. return AST_MODULE_LOAD_DECLINE;
  285. }
  286. return AST_MODULE_LOAD_SUCCESS;
  287. }
  288. static int reload(void)
  289. {
  290. int res = 0;
  291. ast_mutex_lock(&lock);
  292. res = load_config(1);
  293. ast_mutex_unlock(&lock);
  294. return res;
  295. }
  296. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite3 Custom CEL Module",
  297. .load = load_module,
  298. .unload = unload_module,
  299. .reload = reload,
  300. .load_pri = AST_MODPRI_CDR_DRIVER,
  301. );