cel_sqlite3_custom.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #define SQLITE_BACKEND_NAME "CEL sqlite3 custom backend"
  54. AST_MUTEX_DEFINE_STATIC(lock);
  55. static const char config_file[] = "cel_sqlite3_custom.conf";
  56. static const char name[] = "cel_sqlite3_custom";
  57. static sqlite3 *db = NULL;
  58. static char table[80];
  59. /*!
  60. * \bug Handling of this var is crash prone on reloads
  61. */
  62. static char *columns;
  63. struct values {
  64. char *expression;
  65. AST_LIST_ENTRY(values) list;
  66. };
  67. static AST_LIST_HEAD_STATIC(sql_values, values);
  68. static void free_config(void);
  69. static int load_column_config(const char *tmp)
  70. {
  71. char *col = NULL;
  72. char *cols = NULL, *save = NULL;
  73. char *escaped = NULL;
  74. struct ast_str *column_string = NULL;
  75. if (ast_strlen_zero(tmp)) {
  76. ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
  77. return -1;
  78. }
  79. if (!(column_string = ast_str_create(1024))) {
  80. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  81. return -1;
  82. }
  83. if (!(save = cols = ast_strdup(tmp))) {
  84. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  85. ast_free(column_string);
  86. return -1;
  87. }
  88. while ((col = strsep(&cols, ","))) {
  89. col = ast_strip(col);
  90. escaped = sqlite3_mprintf("%q", col);
  91. if (!escaped) {
  92. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
  93. ast_free(column_string);
  94. ast_free(save);
  95. return -1;
  96. }
  97. ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
  98. sqlite3_free(escaped);
  99. }
  100. if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
  101. ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
  102. ast_free(column_string);
  103. ast_free(save);
  104. return -1;
  105. }
  106. ast_free(column_string);
  107. ast_free(save);
  108. return 0;
  109. }
  110. static int load_values_config(const char *tmp)
  111. {
  112. char *val = NULL;
  113. char *vals = NULL, *save = NULL;
  114. struct values *value = NULL;
  115. if (ast_strlen_zero(tmp)) {
  116. ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
  117. return -1;
  118. }
  119. if (!(save = vals = ast_strdup(tmp))) {
  120. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
  121. return -1;
  122. }
  123. while ((val = strsep(&vals, ","))) {
  124. /* Strip the single quotes off if they are there */
  125. val = ast_strip_quoted(val, "'", "'");
  126. value = ast_calloc(sizeof(char), sizeof(*value) + strlen(val) + 1);
  127. if (!value) {
  128. ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", val);
  129. ast_free(save);
  130. return -1;
  131. }
  132. value->expression = (char *) value + sizeof(*value);
  133. ast_copy_string(value->expression, val, strlen(val) + 1);
  134. AST_LIST_INSERT_TAIL(&sql_values, value, list);
  135. }
  136. ast_free(save);
  137. return 0;
  138. }
  139. static int load_config(int reload)
  140. {
  141. struct ast_config *cfg;
  142. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  143. struct ast_variable *mappingvar;
  144. const char *tmp;
  145. if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  146. ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n",
  147. reload ? "re" : "", reload ? "" : "Module not activated.");
  148. return -1;
  149. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  150. return 0;
  151. }
  152. if (reload) {
  153. free_config();
  154. }
  155. if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
  156. /* Nothing configured */
  157. ast_config_destroy(cfg);
  158. return -1;
  159. }
  160. /* Mapping must have a table name */
  161. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
  162. ast_copy_string(table, tmp, sizeof(table));
  163. } else {
  164. ast_log(LOG_WARNING, "Table name not specified. Assuming cel.\n");
  165. strcpy(table, "cel");
  166. }
  167. /* Columns */
  168. if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
  169. ast_config_destroy(cfg);
  170. free_config();
  171. return -1;
  172. }
  173. /* Values */
  174. if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
  175. ast_config_destroy(cfg);
  176. free_config();
  177. return -1;
  178. }
  179. ast_verb(3, "Logging CEL records to table '%s' in 'master.db'\n", table);
  180. ast_config_destroy(cfg);
  181. return 0;
  182. }
  183. static void free_config(void)
  184. {
  185. struct values *value;
  186. if (db) {
  187. sqlite3_close(db);
  188. db = NULL;
  189. }
  190. if (columns) {
  191. ast_free(columns);
  192. columns = NULL;
  193. }
  194. while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
  195. ast_free(value);
  196. }
  197. }
  198. static void write_cel(struct ast_event *event)
  199. {
  200. char *error = NULL;
  201. char *sql = NULL;
  202. if (db == NULL) {
  203. /* Should not have loaded, but be failsafe. */
  204. return;
  205. }
  206. ast_mutex_lock(&lock);
  207. { /* Make it obvious that only sql should be used outside of this block */
  208. char *escaped;
  209. char subst_buf[2048];
  210. struct values *value;
  211. struct ast_channel *dummy;
  212. struct ast_str *value_string = ast_str_create(1024);
  213. dummy = ast_cel_fabricate_channel_from_event(event);
  214. if (!dummy) {
  215. ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
  216. ast_free(value_string);
  217. ast_mutex_unlock(&lock);
  218. return;
  219. }
  220. AST_LIST_TRAVERSE(&sql_values, value, list) {
  221. pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
  222. escaped = sqlite3_mprintf("%q", subst_buf);
  223. ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
  224. sqlite3_free(escaped);
  225. }
  226. sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
  227. ast_debug(1, "About to log: %s\n", sql);
  228. dummy = ast_channel_unref(dummy);
  229. ast_free(value_string);
  230. }
  231. if (sqlite3_exec(db, sql, NULL, NULL, &error) != SQLITE_OK) {
  232. ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
  233. sqlite3_free(error);
  234. }
  235. if (sql) {
  236. sqlite3_free(sql);
  237. }
  238. ast_mutex_unlock(&lock);
  239. return;
  240. }
  241. static int unload_module(void)
  242. {
  243. ast_cel_backend_unregister(SQLITE_BACKEND_NAME);
  244. free_config();
  245. return 0;
  246. }
  247. static int load_module(void)
  248. {
  249. char *error;
  250. char filename[PATH_MAX];
  251. int res;
  252. char *sql;
  253. if (load_config(0)) {
  254. return AST_MODULE_LOAD_DECLINE;
  255. }
  256. /* is the database there? */
  257. snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
  258. res = sqlite3_open(filename, &db);
  259. if (res != SQLITE_OK) {
  260. ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
  261. free_config();
  262. return AST_MODULE_LOAD_DECLINE;
  263. }
  264. sqlite3_busy_timeout(db, 1000);
  265. /* is the table there? */
  266. sql = sqlite3_mprintf("SELECT COUNT(*) FROM %q;", table);
  267. res = sqlite3_exec(db, sql, NULL, NULL, NULL);
  268. sqlite3_free(sql);
  269. if (res != SQLITE_OK) {
  270. /* We don't use %q for the column list here since we already escaped when building it */
  271. sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
  272. res = sqlite3_exec(db, sql, NULL, NULL, &error);
  273. sqlite3_free(sql);
  274. if (res != SQLITE_OK) {
  275. ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
  276. sqlite3_free(error);
  277. free_config();
  278. return AST_MODULE_LOAD_DECLINE;
  279. }
  280. }
  281. if (ast_cel_backend_register(SQLITE_BACKEND_NAME, write_cel)) {
  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. );