cdr_sqlite3_custom.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2007, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com> and others.
  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. * \brief Custom SQLite3 CDR records.
  21. *
  22. * \author Adapted by Alejandro Rios <alejandro.rios@avatar.com.co> and
  23. * Russell Bryant <russell@digium.com> from
  24. * cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
  25. * and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
  26. *
  27. *
  28. * \arg See also \ref AstCDR
  29. *
  30. *
  31. * \ingroup cdr_drivers
  32. */
  33. /*** MODULEINFO
  34. <depend>sqlite3</depend>
  35. <support_level>extended</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <sqlite3.h>
  40. #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
  41. #include "asterisk/channel.h"
  42. #include "asterisk/cdr.h"
  43. #include "asterisk/module.h"
  44. #include "asterisk/config.h"
  45. #include "asterisk/pbx.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/cli.h"
  48. #include "asterisk/app.h"
  49. AST_MUTEX_DEFINE_STATIC(lock);
  50. static const char config_file[] = "cdr_sqlite3_custom.conf";
  51. static const char desc[] = "Customizable SQLite3 CDR Backend";
  52. static const char name[] = "cdr_sqlite3_custom";
  53. static sqlite3 *db = NULL;
  54. static char table[80];
  55. static char *columns;
  56. struct values {
  57. AST_LIST_ENTRY(values) list;
  58. char expression[1];
  59. };
  60. static AST_LIST_HEAD_STATIC(sql_values, values);
  61. static void free_config(int reload);
  62. static int load_column_config(const char *tmp)
  63. {
  64. char *col = NULL;
  65. char *cols = NULL, *save = NULL;
  66. char *escaped = NULL;
  67. struct ast_str *column_string = NULL;
  68. if (ast_strlen_zero(tmp)) {
  69. ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
  70. return -1;
  71. }
  72. if (!(column_string = ast_str_create(1024))) {
  73. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  74. return -1;
  75. }
  76. if (!(save = cols = ast_strdup(tmp))) {
  77. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
  78. ast_free(column_string);
  79. return -1;
  80. }
  81. while ((col = strsep(&cols, ","))) {
  82. col = ast_strip(col);
  83. escaped = sqlite3_mprintf("%q", col);
  84. if (!escaped) {
  85. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
  86. ast_free(column_string);
  87. ast_free(save);
  88. return -1;
  89. }
  90. ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
  91. sqlite3_free(escaped);
  92. }
  93. if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
  94. ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
  95. ast_free(column_string);
  96. ast_free(save);
  97. return -1;
  98. }
  99. ast_free(column_string);
  100. ast_free(save);
  101. return 0;
  102. }
  103. static int load_values_config(const char *tmp)
  104. {
  105. char *vals = NULL, *save = NULL;
  106. struct values *value = NULL;
  107. int i;
  108. AST_DECLARE_APP_ARGS(val,
  109. AST_APP_ARG(ues)[200]; /* More than 200 columns in this CDR? Yeah, right... */
  110. );
  111. if (ast_strlen_zero(tmp)) {
  112. ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
  113. return -1;
  114. }
  115. if (!(save = vals = ast_strdup(tmp))) {
  116. ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
  117. return -1;
  118. }
  119. AST_STANDARD_RAW_ARGS(val, vals);
  120. for (i = 0; i < val.argc; i++) {
  121. /* Strip the single quotes off if they are there */
  122. char *v = ast_strip_quoted(val.ues[i], "'", "'");
  123. value = ast_calloc(sizeof(char), sizeof(*value) + strlen(v));
  124. if (!value) {
  125. ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", v);
  126. ast_free(save);
  127. return -1;
  128. }
  129. strcpy(value->expression, v); /* SAFE */
  130. AST_LIST_INSERT_TAIL(&sql_values, value, list);
  131. }
  132. ast_free(save);
  133. return 0;
  134. }
  135. static int load_config(int reload)
  136. {
  137. struct ast_config *cfg;
  138. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  139. const char *tmp;
  140. if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  141. ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n", reload ? "re" : "", reload ? "" : "Module not activated.");
  142. return -1;
  143. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  144. return 0;
  145. }
  146. if (reload) {
  147. free_config(1);
  148. }
  149. if (!ast_variable_browse(cfg, "master")) {
  150. /* Nothing configured */
  151. ast_config_destroy(cfg);
  152. return -1;
  153. }
  154. /* Mapping must have a table name */
  155. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
  156. ast_copy_string(table, tmp, sizeof(table));
  157. } else {
  158. ast_log(LOG_WARNING, "Table name not specified. Assuming cdr.\n");
  159. strcpy(table, "cdr");
  160. }
  161. /* Columns */
  162. if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
  163. ast_config_destroy(cfg);
  164. free_config(0);
  165. return -1;
  166. }
  167. /* Values */
  168. if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
  169. ast_config_destroy(cfg);
  170. free_config(0);
  171. return -1;
  172. }
  173. ast_verb(4, "cdr_sqlite3_custom: Logging CDR records to table '%s' in 'master.db'\n", table);
  174. ast_config_destroy(cfg);
  175. return 0;
  176. }
  177. static void free_config(int reload)
  178. {
  179. struct values *value;
  180. if (!reload && db) {
  181. sqlite3_close(db);
  182. db = NULL;
  183. }
  184. if (columns) {
  185. ast_free(columns);
  186. columns = NULL;
  187. }
  188. while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
  189. ast_free(value);
  190. }
  191. }
  192. static int write_cdr(struct ast_cdr *cdr)
  193. {
  194. int res = 0;
  195. char *error = NULL;
  196. char *sql = NULL;
  197. if (db == NULL) {
  198. /* Should not have loaded, but be failsafe. */
  199. return 0;
  200. }
  201. ast_mutex_lock(&lock);
  202. { /* Make it obvious that only sql should be used outside of this block */
  203. char *escaped;
  204. char subst_buf[2048];
  205. struct values *value;
  206. struct ast_channel *dummy;
  207. struct ast_str *value_string = ast_str_create(1024);
  208. dummy = ast_dummy_channel_alloc();
  209. if (!dummy) {
  210. ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
  211. ast_free(value_string);
  212. ast_mutex_unlock(&lock);
  213. return 0;
  214. }
  215. ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
  216. AST_LIST_TRAVERSE(&sql_values, value, list) {
  217. pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
  218. escaped = sqlite3_mprintf("%q", subst_buf);
  219. ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
  220. sqlite3_free(escaped);
  221. }
  222. sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
  223. ast_debug(1, "About to log: %s\n", sql);
  224. ast_channel_unref(dummy);
  225. ast_free(value_string);
  226. }
  227. if (sqlite3_exec(db, sql, NULL, NULL, &error) != SQLITE_OK) {
  228. ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
  229. sqlite3_free(error);
  230. }
  231. if (sql) {
  232. sqlite3_free(sql);
  233. }
  234. ast_mutex_unlock(&lock);
  235. return res;
  236. }
  237. static int unload_module(void)
  238. {
  239. if (ast_cdr_unregister(name)) {
  240. return -1;
  241. }
  242. free_config(0);
  243. return 0;
  244. }
  245. static int load_module(void)
  246. {
  247. char *error;
  248. char filename[PATH_MAX];
  249. int res;
  250. char *sql;
  251. if (load_config(0)) {
  252. return AST_MODULE_LOAD_DECLINE;
  253. }
  254. /* is the database there? */
  255. snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
  256. res = sqlite3_open(filename, &db);
  257. if (res != SQLITE_OK) {
  258. ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
  259. free_config(0);
  260. return AST_MODULE_LOAD_DECLINE;
  261. }
  262. sqlite3_busy_timeout(db, 1000);
  263. /* is the table there? */
  264. sql = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
  265. res = sqlite3_exec(db, sql, NULL, NULL, NULL);
  266. sqlite3_free(sql);
  267. if (res != SQLITE_OK) {
  268. /* We don't use %q for the column list here since we already escaped when building it */
  269. sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
  270. res = sqlite3_exec(db, sql, NULL, NULL, &error);
  271. sqlite3_free(sql);
  272. if (res != SQLITE_OK) {
  273. ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
  274. sqlite3_free(error);
  275. free_config(0);
  276. return AST_MODULE_LOAD_DECLINE;
  277. }
  278. }
  279. res = ast_cdr_register(name, desc, write_cdr);
  280. if (res) {
  281. ast_log(LOG_ERROR, "Unable to register custom SQLite3 CDR handling\n");
  282. free_config(0);
  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 CDR 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. );