cel_odbc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008 Digium
  5. *
  6. * Adapted from cdr_adaptive_odbc:
  7. * Tilghman Lesher <tlesher AT digium DOT com>
  8. * by Steve Murphy
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief ODBC CEL backend
  23. *
  24. * \author Tilghman Lesher <tlesher AT digium DOT com>
  25. * \ingroup cel_drivers
  26. */
  27. /*** MODULEINFO
  28. <depend>res_odbc</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <sys/types.h>
  34. #include <time.h>
  35. #include <sql.h>
  36. #include <sqlext.h>
  37. #include <sqltypes.h>
  38. #include "asterisk/config.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/linkedlists.h"
  42. #include "asterisk/res_odbc.h"
  43. #include "asterisk/cel.h"
  44. #include "asterisk/module.h"
  45. #define CONFIG "cel_odbc.conf"
  46. static struct ast_event_sub *event_sub = NULL;
  47. /* Optimization to reduce number of memory allocations */
  48. static int maxsize = 512, maxsize2 = 512;
  49. struct columns {
  50. char *name;
  51. char *celname;
  52. char *filtervalue;
  53. char *staticvalue;
  54. SQLSMALLINT type;
  55. SQLINTEGER size;
  56. SQLSMALLINT decimals;
  57. SQLSMALLINT radix;
  58. SQLSMALLINT nullable;
  59. SQLINTEGER octetlen;
  60. AST_LIST_ENTRY(columns) list;
  61. };
  62. struct tables {
  63. char *connection;
  64. char *table;
  65. unsigned int usegmtime:1;
  66. AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
  67. AST_RWLIST_ENTRY(tables) list;
  68. };
  69. static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
  70. static int load_config(void)
  71. {
  72. struct ast_config *cfg;
  73. struct ast_variable *var;
  74. const char *tmp, *catg;
  75. struct tables *tableptr;
  76. struct columns *entry;
  77. struct odbc_obj *obj;
  78. char columnname[80];
  79. char connection[40];
  80. char table[40];
  81. int lenconnection, lentable, usegmtime = 0;
  82. SQLLEN sqlptr;
  83. int res = 0;
  84. SQLHSTMT stmt = NULL;
  85. struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
  86. cfg = ast_config_load(CONFIG, config_flags);
  87. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  88. ast_log(LOG_WARNING, "Unable to load " CONFIG ". No ODBC CEL records!\n");
  89. return -1;
  90. }
  91. for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
  92. var = ast_variable_browse(cfg, catg);
  93. if (!var)
  94. continue;
  95. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
  96. ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
  97. continue;
  98. }
  99. ast_copy_string(connection, tmp, sizeof(connection));
  100. lenconnection = strlen(connection);
  101. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) {
  102. usegmtime = ast_true(tmp);
  103. }
  104. /* When loading, we want to be sure we can connect. */
  105. obj = ast_odbc_request_obj(connection, 1);
  106. if (!obj) {
  107. ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
  108. continue;
  109. }
  110. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
  111. ast_log(LOG_NOTICE, "No table name found. Assuming 'cel'.\n");
  112. tmp = "cel";
  113. }
  114. ast_copy_string(table, tmp, sizeof(table));
  115. lentable = strlen(table);
  116. res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
  117. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  118. ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
  119. ast_odbc_release_obj(obj);
  120. continue;
  121. }
  122. res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
  123. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  124. ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
  125. ast_odbc_release_obj(obj);
  126. continue;
  127. }
  128. tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
  129. if (!tableptr) {
  130. ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
  131. ast_odbc_release_obj(obj);
  132. res = -1;
  133. break;
  134. }
  135. tableptr->usegmtime = usegmtime;
  136. tableptr->connection = (char *)tableptr + sizeof(*tableptr);
  137. tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
  138. ast_copy_string(tableptr->connection, connection, lenconnection + 1);
  139. ast_copy_string(tableptr->table, table, lentable + 1);
  140. ast_verb(3, "Found CEL table %s@%s.\n", tableptr->table, tableptr->connection);
  141. /* Check for filters first */
  142. for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
  143. if (strncmp(var->name, "filter", 6) == 0) {
  144. char *celvar = ast_strdupa(var->name + 6);
  145. celvar = ast_strip(celvar);
  146. ast_verb(3, "Found filter %s for cel variable %s in %s@%s\n", var->value, celvar, tableptr->table, tableptr->connection);
  147. entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(celvar) + 1 + strlen(var->value) + 1);
  148. if (!entry) {
  149. ast_log(LOG_ERROR, "Out of memory creating filter entry for CEL variable '%s' in table '%s' on connection '%s'\n", celvar, table, connection);
  150. res = -1;
  151. break;
  152. }
  153. /* NULL column entry means this isn't a column in the database */
  154. entry->name = NULL;
  155. entry->celname = (char *)entry + sizeof(*entry);
  156. entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(celvar) + 1;
  157. strcpy(entry->celname, celvar);
  158. strcpy(entry->filtervalue, var->value);
  159. AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
  160. }
  161. }
  162. while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
  163. char *celvar = "", *staticvalue = "";
  164. SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
  165. /* Is there an alias for this column? */
  166. /* NOTE: This seems like a non-optimal parse method, but I'm going
  167. * for user configuration readability, rather than fast parsing. We
  168. * really don't parse this file all that often, anyway.
  169. */
  170. for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
  171. if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) {
  172. char *alias = ast_strdupa(var->name + 5);
  173. celvar = ast_strip(alias);
  174. ast_verb(3, "Found alias %s for column %s in %s@%s\n", celvar, columnname, tableptr->table, tableptr->connection);
  175. break;
  176. } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) {
  177. char *item = ast_strdupa(var->name + 6);
  178. item = ast_strip(item);
  179. if (item[0] == '"' && item[strlen(item) - 1] == '"') {
  180. /* Remove surrounding quotes */
  181. item[strlen(item) - 1] = '\0';
  182. item++;
  183. }
  184. staticvalue = item;
  185. }
  186. }
  187. entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(celvar) + 1 + strlen(staticvalue) + 1);
  188. if (!entry) {
  189. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
  190. res = -1;
  191. break;
  192. }
  193. entry->name = (char *)entry + sizeof(*entry);
  194. strcpy(entry->name, columnname);
  195. if (!ast_strlen_zero(celvar)) {
  196. entry->celname = entry->name + strlen(columnname) + 1;
  197. strcpy(entry->celname, celvar);
  198. } else { /* Point to same place as the column name */
  199. entry->celname = (char *)entry + sizeof(*entry);
  200. }
  201. if (!ast_strlen_zero(staticvalue)) {
  202. entry->staticvalue = entry->celname + strlen(entry->celname) + 1;
  203. strcpy(entry->staticvalue, staticvalue);
  204. }
  205. SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
  206. SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
  207. SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
  208. SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
  209. SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
  210. SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
  211. /* Specification states that the octenlen should be the maximum number of bytes
  212. * returned in a char or binary column, but it seems that some drivers just set
  213. * it to NULL. (Bad Postgres! No biscuit!) */
  214. if (entry->octetlen == 0)
  215. entry->octetlen = entry->size;
  216. ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
  217. /* Insert column info into column list */
  218. AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
  219. res = 0;
  220. }
  221. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  222. ast_odbc_release_obj(obj);
  223. if (AST_LIST_FIRST(&(tableptr->columns)))
  224. AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
  225. else
  226. ast_free(tableptr);
  227. }
  228. return res;
  229. }
  230. static int free_config(void)
  231. {
  232. struct tables *table;
  233. struct columns *entry;
  234. while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
  235. while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
  236. ast_free(entry);
  237. }
  238. ast_free(table);
  239. }
  240. return 0;
  241. }
  242. static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
  243. {
  244. int res, i;
  245. char *sql = data;
  246. SQLHSTMT stmt;
  247. SQLINTEGER nativeerror = 0, numfields = 0;
  248. SQLSMALLINT diagbytes = 0;
  249. unsigned char state[10], diagnostic[256];
  250. res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
  251. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  252. ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
  253. return NULL;
  254. }
  255. res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
  256. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  257. ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
  258. SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
  259. for (i = 0; i < numfields; i++) {
  260. SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
  261. ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
  262. if (i > 10) {
  263. ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
  264. break;
  265. }
  266. }
  267. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  268. return NULL;
  269. }
  270. return stmt;
  271. }
  272. #define LENGTHEN_BUF1(size) \
  273. do { \
  274. /* Lengthen buffer, if necessary */ \
  275. if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
  276. if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 1) / 512 + 1) * 512) != 0) { \
  277. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
  278. ast_free(sql); \
  279. ast_free(sql2); \
  280. AST_RWLIST_UNLOCK(&odbc_tables); \
  281. return; \
  282. } \
  283. } \
  284. } while (0)
  285. #define LENGTHEN_BUF2(size) \
  286. do { \
  287. if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
  288. if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
  289. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
  290. ast_free(sql); \
  291. ast_free(sql2); \
  292. AST_RWLIST_UNLOCK(&odbc_tables); \
  293. return; \
  294. } \
  295. } \
  296. } while (0)
  297. static void odbc_log(const struct ast_event *event, void *userdata)
  298. {
  299. struct tables *tableptr;
  300. struct columns *entry;
  301. struct odbc_obj *obj;
  302. struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
  303. char *tmp;
  304. char colbuf[1024], *colptr;
  305. SQLHSTMT stmt = NULL;
  306. SQLLEN rows = 0;
  307. struct ast_cel_event_record record = {
  308. .version = AST_CEL_EVENT_RECORD_VERSION,
  309. };
  310. if (ast_cel_fill_record(event, &record)) {
  311. return;
  312. }
  313. if (!sql || !sql2) {
  314. if (sql)
  315. ast_free(sql);
  316. if (sql2)
  317. ast_free(sql2);
  318. return;
  319. }
  320. if (AST_RWLIST_RDLOCK(&odbc_tables)) {
  321. ast_log(LOG_ERROR, "Unable to lock table list. Insert CEL(s) failed.\n");
  322. ast_free(sql);
  323. ast_free(sql2);
  324. return;
  325. }
  326. AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
  327. int first = 1;
  328. ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
  329. ast_str_set(&sql2, 0, " VALUES (");
  330. /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
  331. if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
  332. ast_log(LOG_WARNING, "Unable to retrieve database handle for '%s:%s'. CEL failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
  333. continue;
  334. }
  335. AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
  336. int datefield = 0;
  337. if (strcasecmp(entry->celname, "eventtime") == 0) {
  338. datefield = 1;
  339. }
  340. /* Check if we have a similarly named variable */
  341. if (entry->staticvalue) {
  342. colptr = ast_strdupa(entry->staticvalue);
  343. } else if (datefield) {
  344. struct timeval date_tv = record.event_time;
  345. struct ast_tm tm = { 0, };
  346. ast_localtime(&date_tv, &tm, tableptr->usegmtime ? "UTC" : NULL);
  347. ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S", &tm);
  348. colptr = colbuf;
  349. } else {
  350. if (strcmp(entry->celname, "userdeftype") == 0) {
  351. ast_copy_string(colbuf, record.user_defined_name, sizeof(colbuf));
  352. } else if (strcmp(entry->celname, "cid_name") == 0) {
  353. ast_copy_string(colbuf, record.caller_id_name, sizeof(colbuf));
  354. } else if (strcmp(entry->celname, "cid_num") == 0) {
  355. ast_copy_string(colbuf, record.caller_id_num, sizeof(colbuf));
  356. } else if (strcmp(entry->celname, "cid_ani") == 0) {
  357. ast_copy_string(colbuf, record.caller_id_ani, sizeof(colbuf));
  358. } else if (strcmp(entry->celname, "cid_rdnis") == 0) {
  359. ast_copy_string(colbuf, record.caller_id_rdnis, sizeof(colbuf));
  360. } else if (strcmp(entry->celname, "cid_dnid") == 0) {
  361. ast_copy_string(colbuf, record.caller_id_dnid, sizeof(colbuf));
  362. } else if (strcmp(entry->celname, "exten") == 0) {
  363. ast_copy_string(colbuf, record.extension, sizeof(colbuf));
  364. } else if (strcmp(entry->celname, "context") == 0) {
  365. ast_copy_string(colbuf, record.context, sizeof(colbuf));
  366. } else if (strcmp(entry->celname, "channame") == 0) {
  367. ast_copy_string(colbuf, record.channel_name, sizeof(colbuf));
  368. } else if (strcmp(entry->celname, "appname") == 0) {
  369. ast_copy_string(colbuf, record.application_name, sizeof(colbuf));
  370. } else if (strcmp(entry->celname, "appdata") == 0) {
  371. ast_copy_string(colbuf, record.application_data, sizeof(colbuf));
  372. } else if (strcmp(entry->celname, "accountcode") == 0) {
  373. ast_copy_string(colbuf, record.account_code, sizeof(colbuf));
  374. } else if (strcmp(entry->celname, "peeraccount") == 0) {
  375. ast_copy_string(colbuf, record.peer_account, sizeof(colbuf));
  376. } else if (strcmp(entry->celname, "uniqueid") == 0) {
  377. ast_copy_string(colbuf, record.unique_id, sizeof(colbuf));
  378. } else if (strcmp(entry->celname, "linkedid") == 0) {
  379. ast_copy_string(colbuf, record.linked_id, sizeof(colbuf));
  380. } else if (strcmp(entry->celname, "userfield") == 0) {
  381. ast_copy_string(colbuf, record.user_field, sizeof(colbuf));
  382. } else if (strcmp(entry->celname, "peer") == 0) {
  383. ast_copy_string(colbuf, record.peer, sizeof(colbuf));
  384. } else if (strcmp(entry->celname, "amaflags") == 0) {
  385. snprintf(colbuf, sizeof(colbuf), "%d", record.amaflag);
  386. } else if (strcmp(entry->celname, "extra") == 0) {
  387. ast_copy_string(colbuf, record.extra, sizeof(colbuf));
  388. } else {
  389. colbuf[0] = 0;
  390. }
  391. colptr = colbuf;
  392. }
  393. if (colptr) {
  394. /* Check first if the column filters this entry. Note that this
  395. * is very specifically NOT ast_strlen_zero(), because the filter
  396. * could legitimately specify that the field is blank, which is
  397. * different from the field being unspecified (NULL). */
  398. if (entry->filtervalue && strcasecmp(colptr, entry->filtervalue) != 0) {
  399. ast_verb(4, "CEL column '%s' with value '%s' does not match filter of"
  400. " '%s'. Cancelling this CEL.\n",
  401. entry->celname, colptr, entry->filtervalue);
  402. goto early_release;
  403. }
  404. /* Only a filter? */
  405. if (ast_strlen_zero(entry->name))
  406. continue;
  407. LENGTHEN_BUF1(strlen(entry->name));
  408. switch (entry->type) {
  409. case SQL_CHAR:
  410. case SQL_VARCHAR:
  411. case SQL_LONGVARCHAR:
  412. case SQL_BINARY:
  413. case SQL_VARBINARY:
  414. case SQL_LONGVARBINARY:
  415. case SQL_GUID:
  416. /* For these two field names, get the rendered form, instead of the raw
  417. * form (but only when we're dealing with a character-based field).
  418. */
  419. if (strcasecmp(entry->name, "eventtype") == 0) {
  420. snprintf(colbuf, sizeof(colbuf), "%s", record.event_name);
  421. }
  422. /* Truncate too-long fields */
  423. if (entry->type != SQL_GUID) {
  424. if (strlen(colptr) > entry->octetlen) {
  425. colptr[entry->octetlen] = '\0';
  426. }
  427. }
  428. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  429. LENGTHEN_BUF2(strlen(colptr));
  430. /* Encode value, with escaping */
  431. ast_str_append(&sql2, 0, "%s'", first ? "" : ",");
  432. for (tmp = colptr; *tmp; tmp++) {
  433. if (*tmp == '\'') {
  434. ast_str_append(&sql2, 0, "''");
  435. } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
  436. ast_str_append(&sql2, 0, "\\\\");
  437. } else {
  438. ast_str_append(&sql2, 0, "%c", *tmp);
  439. }
  440. }
  441. ast_str_append(&sql2, 0, "'");
  442. break;
  443. case SQL_TYPE_DATE:
  444. if (ast_strlen_zero(colptr)) {
  445. continue;
  446. } else {
  447. int year = 0, month = 0, day = 0;
  448. if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 ||
  449. month <= 0 || month > 12 || day < 0 || day > 31 ||
  450. ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
  451. (month == 2 && year % 400 == 0 && day > 29) ||
  452. (month == 2 && year % 100 == 0 && day > 28) ||
  453. (month == 2 && year % 4 == 0 && day > 29) ||
  454. (month == 2 && year % 4 != 0 && day > 28)) {
  455. ast_log(LOG_WARNING, "CEL variable %s is not a valid date ('%s').\n", entry->name, colptr);
  456. continue;
  457. }
  458. if (year > 0 && year < 100) {
  459. year += 2000;
  460. }
  461. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  462. LENGTHEN_BUF2(17);
  463. ast_str_append(&sql2, 0, "%s{ d '%04d-%02d-%02d' }", first ? "" : ",", year, month, day);
  464. }
  465. break;
  466. case SQL_TYPE_TIME:
  467. if (ast_strlen_zero(colptr)) {
  468. continue;
  469. } else {
  470. int hour = 0, minute = 0, second = 0;
  471. int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second);
  472. if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
  473. ast_log(LOG_WARNING, "CEL variable %s is not a valid time ('%s').\n", entry->name, colptr);
  474. continue;
  475. }
  476. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  477. LENGTHEN_BUF2(15);
  478. ast_str_append(&sql2, 0, "%s{ t '%02d:%02d:%02d' }", first ? "" : ",", hour, minute, second);
  479. }
  480. break;
  481. case SQL_TYPE_TIMESTAMP:
  482. case SQL_TIMESTAMP:
  483. if (ast_strlen_zero(colptr)) {
  484. continue;
  485. } else {
  486. int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
  487. int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second);
  488. if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
  489. month <= 0 || month > 12 || day < 0 || day > 31 ||
  490. ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
  491. (month == 2 && year % 400 == 0 && day > 29) ||
  492. (month == 2 && year % 100 == 0 && day > 28) ||
  493. (month == 2 && year % 4 == 0 && day > 29) ||
  494. (month == 2 && year % 4 != 0 && day > 28) ||
  495. hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
  496. ast_log(LOG_WARNING, "CEL variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
  497. continue;
  498. }
  499. if (year > 0 && year < 100) {
  500. year += 2000;
  501. }
  502. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  503. LENGTHEN_BUF2(26);
  504. ast_str_append(&sql2, 0, "%s{ ts '%04d-%02d-%02d %02d:%02d:%02d' }", first ? "" : ",", year, month, day, hour, minute, second);
  505. }
  506. break;
  507. case SQL_INTEGER:
  508. {
  509. int integer = 0;
  510. if (strcasecmp(entry->name, "eventtype") == 0) {
  511. integer = (int) record.event_type;
  512. } else if (ast_strlen_zero(colptr)) {
  513. continue;
  514. } else if (sscanf(colptr, "%30d", &integer) != 1) {
  515. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  516. continue;
  517. }
  518. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  519. LENGTHEN_BUF2(12);
  520. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  521. }
  522. break;
  523. case SQL_BIGINT:
  524. {
  525. long long integer = 0;
  526. if (strcasecmp(entry->name, "eventtype") == 0) {
  527. integer = (long long) record.event_type;
  528. } else if (ast_strlen_zero(colptr)) {
  529. continue;
  530. } else if (sscanf(colptr, "%30lld", &integer) != 1) {
  531. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  532. continue;
  533. }
  534. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  535. LENGTHEN_BUF2(24);
  536. ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", integer);
  537. }
  538. break;
  539. case SQL_SMALLINT:
  540. {
  541. short integer = 0;
  542. if (strcasecmp(entry->name, "eventtype") == 0) {
  543. integer = (short) record.event_type;
  544. } else if (ast_strlen_zero(colptr)) {
  545. continue;
  546. } else if (sscanf(colptr, "%30hd", &integer) != 1) {
  547. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  548. continue;
  549. }
  550. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  551. LENGTHEN_BUF2(6);
  552. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  553. }
  554. break;
  555. case SQL_TINYINT:
  556. {
  557. char integer = 0;
  558. if (strcasecmp(entry->name, "eventtype") == 0) {
  559. integer = (char) record.event_type;
  560. } else if (ast_strlen_zero(colptr)) {
  561. continue;
  562. } else if (sscanf(colptr, "%30hhd", &integer) != 1) {
  563. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  564. continue;
  565. }
  566. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  567. LENGTHEN_BUF2(4);
  568. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  569. }
  570. break;
  571. case SQL_BIT:
  572. {
  573. char integer = 0;
  574. if (strcasecmp(entry->name, "eventtype") == 0) {
  575. integer = (char) record.event_type;
  576. } else if (ast_strlen_zero(colptr)) {
  577. continue;
  578. } else if (sscanf(colptr, "%30hhd", &integer) != 1) {
  579. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  580. continue;
  581. }
  582. if (integer != 0)
  583. integer = 1;
  584. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  585. LENGTHEN_BUF2(2);
  586. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  587. }
  588. break;
  589. case SQL_NUMERIC:
  590. case SQL_DECIMAL:
  591. {
  592. double number = 0.0;
  593. if (strcasecmp(entry->name, "eventtype") == 0) {
  594. number = (double)record.event_type;
  595. } else if (ast_strlen_zero(colptr)) {
  596. continue;
  597. } else if (sscanf(colptr, "%30lf", &number) != 1) {
  598. ast_log(LOG_WARNING, "CEL variable %s is not an numeric type.\n", entry->name);
  599. continue;
  600. }
  601. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  602. LENGTHEN_BUF2(entry->decimals);
  603. ast_str_append(&sql2, 0, "%s%*.*lf", first ? "" : ",", entry->decimals, entry->radix, number);
  604. }
  605. break;
  606. case SQL_FLOAT:
  607. case SQL_REAL:
  608. case SQL_DOUBLE:
  609. {
  610. double number = 0.0;
  611. if (strcasecmp(entry->name, "eventtype") == 0) {
  612. number = (double) record.event_type;
  613. } else if (ast_strlen_zero(colptr)) {
  614. continue;
  615. } else if (sscanf(colptr, "%30lf", &number) != 1) {
  616. ast_log(LOG_WARNING, "CEL variable %s is not an numeric type.\n", entry->name);
  617. continue;
  618. }
  619. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  620. LENGTHEN_BUF2(entry->decimals);
  621. ast_str_append(&sql2, 0, "%s%lf", first ? "" : ",", number);
  622. }
  623. break;
  624. default:
  625. ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name);
  626. continue;
  627. }
  628. first = 0;
  629. }
  630. }
  631. /* Concatenate the two constructed buffers */
  632. LENGTHEN_BUF1(ast_str_strlen(sql2));
  633. ast_str_append(&sql, 0, ")");
  634. ast_str_append(&sql2, 0, ")");
  635. ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2));
  636. ast_verb(11, "[%s]\n", ast_str_buffer(sql));
  637. stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, ast_str_buffer(sql));
  638. if (stmt) {
  639. SQLRowCount(stmt, &rows);
  640. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  641. }
  642. if (rows == 0) {
  643. ast_log(LOG_WARNING, "Insert failed on '%s:%s'. CEL failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
  644. }
  645. early_release:
  646. ast_odbc_release_obj(obj);
  647. }
  648. AST_RWLIST_UNLOCK(&odbc_tables);
  649. /* Next time, just allocate buffers that are that big to start with. */
  650. if (ast_str_strlen(sql) > maxsize) {
  651. maxsize = ast_str_strlen(sql);
  652. }
  653. if (ast_str_strlen(sql2) > maxsize2) {
  654. maxsize2 = ast_str_strlen(sql2);
  655. }
  656. ast_free(sql);
  657. ast_free(sql2);
  658. }
  659. static int unload_module(void)
  660. {
  661. if (event_sub) {
  662. event_sub = ast_event_unsubscribe(event_sub);
  663. }
  664. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  665. event_sub = ast_event_subscribe(AST_EVENT_CEL, odbc_log, "ODBC CEL backend", NULL, AST_EVENT_IE_END);
  666. if (!event_sub) {
  667. ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n");
  668. }
  669. ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
  670. return -1;
  671. }
  672. free_config();
  673. AST_RWLIST_UNLOCK(&odbc_tables);
  674. AST_RWLIST_HEAD_DESTROY(&odbc_tables);
  675. return 0;
  676. }
  677. static int load_module(void)
  678. {
  679. AST_RWLIST_HEAD_INIT(&odbc_tables);
  680. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  681. ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
  682. return 0;
  683. }
  684. load_config();
  685. AST_RWLIST_UNLOCK(&odbc_tables);
  686. event_sub = ast_event_subscribe(AST_EVENT_CEL, odbc_log, "ODBC CEL backend", NULL, AST_EVENT_IE_END);
  687. if (!event_sub) {
  688. ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n");
  689. }
  690. return AST_MODULE_LOAD_SUCCESS;
  691. }
  692. static int reload(void)
  693. {
  694. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  695. ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
  696. return -1;
  697. }
  698. free_config();
  699. load_config();
  700. AST_RWLIST_UNLOCK(&odbc_tables);
  701. return AST_MODULE_LOAD_SUCCESS;
  702. }
  703. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CEL backend",
  704. .load = load_module,
  705. .unload = unload_module,
  706. .reload = reload,
  707. .load_pri = AST_MODPRI_CDR_DRIVER,
  708. );