cel_sqlite3_custom.c 8.8 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. #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 sqlite3 *db = NULL;
  57. static char table[80];
  58. /*!
  59. * \bug Handling of this var is crash prone on reloads
  60. */
  61. static char *columns;
  62. struct values {
  63. char *expression;
  64. AST_LIST_ENTRY(values) list;
  65. };
  66. static AST_LIST_HEAD_STATIC(sql_values, values);
  67. static void free_config(void);
  68. static int load_column_config(const char *tmp)
  69. {
  70. char *col = NULL;
  71. char *cols = NULL, *save = NULL;
  72. char *escaped = NULL;
  73. struct ast_str *column_string = NULL;
  74. if (ast_strlen_zero(tmp)) {
  75. ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
  76. return -1;
  77. }
  78. if (!(column_string = ast_str_create(1024))) {
  79. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  80. return -1;
  81. }
  82. if (!(save = cols = ast_strdup(tmp))) {
  83. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  84. ast_free(column_string);
  85. return -1;
  86. }
  87. while ((col = strsep(&cols, ","))) {
  88. col = ast_strip(col);
  89. escaped = sqlite3_mprintf("%q", col);
  90. if (!escaped) {
  91. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
  92. ast_free(column_string);
  93. ast_free(save);
  94. return -1;
  95. }
  96. ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
  97. sqlite3_free(escaped);
  98. }
  99. if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
  100. ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
  101. ast_free(column_string);
  102. ast_free(save);
  103. return -1;
  104. }
  105. ast_free(column_string);
  106. ast_free(save);
  107. return 0;
  108. }
  109. static int load_values_config(const char *tmp)
  110. {
  111. char *val = NULL;
  112. char *vals = NULL, *save = NULL;
  113. struct values *value = NULL;
  114. if (ast_strlen_zero(tmp)) {
  115. ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
  116. return -1;
  117. }
  118. if (!(save = vals = ast_strdup(tmp))) {
  119. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
  120. return -1;
  121. }
  122. while ((val = strsep(&vals, ","))) {
  123. /* Strip the single quotes off if they are there */
  124. val = ast_strip_quoted(val, "'", "'");
  125. value = ast_calloc(sizeof(char), sizeof(*value) + strlen(val) + 1);
  126. if (!value) {
  127. ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", val);
  128. ast_free(save);
  129. return -1;
  130. }
  131. value->expression = (char *) value + sizeof(*value);
  132. ast_copy_string(value->expression, val, strlen(val) + 1);
  133. AST_LIST_INSERT_TAIL(&sql_values, value, list);
  134. }
  135. ast_free(save);
  136. return 0;
  137. }
  138. static int load_config(int reload)
  139. {
  140. struct ast_config *cfg;
  141. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  142. struct ast_variable *mappingvar;
  143. const char *tmp;
  144. if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  145. ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n",
  146. reload ? "re" : "", reload ? "" : "Module not activated.");
  147. return -1;
  148. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  149. return 0;
  150. }
  151. if (reload) {
  152. free_config();
  153. }
  154. if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
  155. /* Nothing configured */
  156. ast_config_destroy(cfg);
  157. return -1;
  158. }
  159. /* Mapping must have a table name */
  160. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
  161. ast_copy_string(table, tmp, sizeof(table));
  162. } else {
  163. ast_log(LOG_WARNING, "Table name not specified. Assuming cel.\n");
  164. strcpy(table, "cel");
  165. }
  166. /* Columns */
  167. if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
  168. ast_config_destroy(cfg);
  169. free_config();
  170. return -1;
  171. }
  172. /* Values */
  173. if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
  174. ast_config_destroy(cfg);
  175. free_config();
  176. return -1;
  177. }
  178. ast_verb(3, "Logging CEL records to table '%s' in 'master.db'\n", table);
  179. ast_config_destroy(cfg);
  180. return 0;
  181. }
  182. static void free_config(void)
  183. {
  184. struct values *value;
  185. if (db) {
  186. sqlite3_close(db);
  187. db = NULL;
  188. }
  189. if (columns) {
  190. ast_free(columns);
  191. columns = NULL;
  192. }
  193. while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
  194. ast_free(value);
  195. }
  196. }
  197. static void write_cel(struct ast_event *event)
  198. {
  199. char *error = NULL;
  200. char *sql = NULL;
  201. if (db == NULL) {
  202. /* Should not have loaded, but be failsafe. */
  203. return;
  204. }
  205. ast_mutex_lock(&lock);
  206. { /* Make it obvious that only sql should be used outside of this block */
  207. char *escaped;
  208. char subst_buf[2048];
  209. struct values *value;
  210. struct ast_channel *dummy;
  211. struct ast_str *value_string = ast_str_create(1024);
  212. dummy = ast_cel_fabricate_channel_from_event(event);
  213. if (!dummy) {
  214. ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
  215. ast_free(value_string);
  216. ast_mutex_unlock(&lock);
  217. return;
  218. }
  219. AST_LIST_TRAVERSE(&sql_values, value, list) {
  220. pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
  221. escaped = sqlite3_mprintf("%q", subst_buf);
  222. ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
  223. sqlite3_free(escaped);
  224. }
  225. sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
  226. ast_debug(1, "About to log: %s\n", sql);
  227. dummy = ast_channel_unref(dummy);
  228. ast_free(value_string);
  229. }
  230. if (sqlite3_exec(db, sql, NULL, NULL, &error) != SQLITE_OK) {
  231. ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
  232. sqlite3_free(error);
  233. }
  234. if (sql) {
  235. sqlite3_free(sql);
  236. }
  237. ast_mutex_unlock(&lock);
  238. return;
  239. }
  240. static int unload_module(void)
  241. {
  242. ast_cel_backend_unregister(SQLITE_BACKEND_NAME);
  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. if (ast_cel_backend_register(SQLITE_BACKEND_NAME, write_cel)) {
  281. ast_log(LOG_ERROR, "Unable to register custom SQLite3 CEL handling\n");
  282. free_config();
  283. return AST_MODULE_LOAD_DECLINE;
  284. }
  285. return AST_MODULE_LOAD_SUCCESS;
  286. }
  287. static int reload(void)
  288. {
  289. int res = 0;
  290. ast_mutex_lock(&lock);
  291. res = load_config(1);
  292. ast_mutex_unlock(&lock);
  293. return res;
  294. }
  295. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite3 Custom CEL Module",
  296. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  297. .load = load_module,
  298. .unload = unload_module,
  299. .reload = reload,
  300. .load_pri = AST_MODPRI_CDR_DRIVER,
  301. );