cdr_sqlite3_custom.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. struct ast_variable *mappingvar;
  140. const char *tmp;
  141. if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  142. ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n", reload ? "re" : "", reload ? "" : "Module not activated.");
  143. return -1;
  144. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  145. return 0;
  146. }
  147. if (reload) {
  148. free_config(1);
  149. }
  150. if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
  151. /* Nothing configured */
  152. ast_config_destroy(cfg);
  153. return -1;
  154. }
  155. /* Mapping must have a table name */
  156. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
  157. ast_copy_string(table, tmp, sizeof(table));
  158. } else {
  159. ast_log(LOG_WARNING, "Table name not specified. Assuming cdr.\n");
  160. strcpy(table, "cdr");
  161. }
  162. /* Columns */
  163. if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
  164. ast_config_destroy(cfg);
  165. free_config(0);
  166. return -1;
  167. }
  168. /* Values */
  169. if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
  170. ast_config_destroy(cfg);
  171. free_config(0);
  172. return -1;
  173. }
  174. ast_verb(3, "cdr_sqlite3_custom: Logging CDR records to table '%s' in 'master.db'\n", table);
  175. ast_config_destroy(cfg);
  176. return 0;
  177. }
  178. static void free_config(int reload)
  179. {
  180. struct values *value;
  181. if (!reload && db) {
  182. sqlite3_close(db);
  183. db = NULL;
  184. }
  185. if (columns) {
  186. ast_free(columns);
  187. columns = NULL;
  188. }
  189. while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
  190. ast_free(value);
  191. }
  192. }
  193. static int write_cdr(struct ast_cdr *cdr)
  194. {
  195. int res = 0;
  196. char *error = NULL;
  197. char *sql = NULL;
  198. int count = 0;
  199. if (db == NULL) {
  200. /* Should not have loaded, but be failsafe. */
  201. return 0;
  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_dummy_channel_alloc();
  211. if (!dummy) {
  212. ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
  213. ast_free(value_string);
  214. ast_mutex_unlock(&lock);
  215. return 0;
  216. }
  217. dummy->cdr = ast_cdr_dup(cdr);
  218. AST_LIST_TRAVERSE(&sql_values, value, list) {
  219. pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
  220. escaped = sqlite3_mprintf("%q", subst_buf);
  221. ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
  222. sqlite3_free(escaped);
  223. }
  224. sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
  225. ast_debug(1, "About to log: %s\n", sql);
  226. ast_channel_unref(dummy);
  227. ast_free(value_string);
  228. }
  229. /* XXX This seems awful arbitrary... */
  230. for (count = 0; count < 5; count++) {
  231. res = sqlite3_exec(db, sql, NULL, NULL, &error);
  232. if (res != SQLITE_BUSY && res != SQLITE_LOCKED) {
  233. break;
  234. }
  235. usleep(200);
  236. }
  237. if (error) {
  238. ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
  239. sqlite3_free(error);
  240. }
  241. if (sql) {
  242. sqlite3_free(sql);
  243. }
  244. ast_mutex_unlock(&lock);
  245. return res;
  246. }
  247. static int unload_module(void)
  248. {
  249. ast_cdr_unregister(name);
  250. free_config(0);
  251. return 0;
  252. }
  253. static int load_module(void)
  254. {
  255. char *error;
  256. char filename[PATH_MAX];
  257. int res;
  258. char *sql;
  259. if (load_config(0)) {
  260. return AST_MODULE_LOAD_DECLINE;
  261. }
  262. /* is the database there? */
  263. snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
  264. res = sqlite3_open(filename, &db);
  265. if (res != SQLITE_OK) {
  266. ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
  267. free_config(0);
  268. return AST_MODULE_LOAD_DECLINE;
  269. }
  270. /* is the table there? */
  271. sql = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
  272. res = sqlite3_exec(db, sql, NULL, NULL, NULL);
  273. sqlite3_free(sql);
  274. if (res != SQLITE_OK) {
  275. /* We don't use %q for the column list here since we already escaped when building it */
  276. sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
  277. res = sqlite3_exec(db, sql, NULL, NULL, &error);
  278. sqlite3_free(sql);
  279. if (res != SQLITE_OK) {
  280. ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
  281. sqlite3_free(error);
  282. free_config(0);
  283. return AST_MODULE_LOAD_DECLINE;
  284. }
  285. }
  286. res = ast_cdr_register(name, desc, write_cdr);
  287. if (res) {
  288. ast_log(LOG_ERROR, "Unable to register custom SQLite3 CDR handling\n");
  289. free_config(0);
  290. return AST_MODULE_LOAD_DECLINE;
  291. }
  292. return AST_MODULE_LOAD_SUCCESS;
  293. }
  294. static int reload(void)
  295. {
  296. int res = 0;
  297. ast_mutex_lock(&lock);
  298. res = load_config(1);
  299. ast_mutex_unlock(&lock);
  300. return res;
  301. }
  302. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite3 Custom CDR Module",
  303. .load = load_module,
  304. .unload = unload_module,
  305. .reload = reload,
  306. .load_pri = AST_MODPRI_CDR_DRIVER,
  307. );