cel_odbc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. #ifdef HAVE_ODBC_WCHAR
  413. case SQL_WCHAR:
  414. case SQL_WVARCHAR:
  415. case SQL_WLONGVARCHAR:
  416. #endif
  417. case SQL_BINARY:
  418. case SQL_VARBINARY:
  419. case SQL_LONGVARBINARY:
  420. case SQL_GUID:
  421. /* For these two field names, get the rendered form, instead of the raw
  422. * form (but only when we're dealing with a character-based field).
  423. */
  424. if (strcasecmp(entry->name, "eventtype") == 0) {
  425. snprintf(colbuf, sizeof(colbuf), "%s", record.event_name);
  426. }
  427. /* Truncate too-long fields */
  428. if (entry->type != SQL_GUID) {
  429. if (strlen(colptr) > entry->octetlen) {
  430. colptr[entry->octetlen] = '\0';
  431. }
  432. }
  433. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  434. LENGTHEN_BUF2(strlen(colptr));
  435. /* Encode value, with escaping */
  436. ast_str_append(&sql2, 0, "%s'", first ? "" : ",");
  437. for (tmp = colptr; *tmp; tmp++) {
  438. if (*tmp == '\'') {
  439. ast_str_append(&sql2, 0, "''");
  440. } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
  441. ast_str_append(&sql2, 0, "\\\\");
  442. } else {
  443. ast_str_append(&sql2, 0, "%c", *tmp);
  444. }
  445. }
  446. ast_str_append(&sql2, 0, "'");
  447. break;
  448. case SQL_TYPE_DATE:
  449. if (ast_strlen_zero(colptr)) {
  450. continue;
  451. } else {
  452. int year = 0, month = 0, day = 0;
  453. if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 ||
  454. month <= 0 || month > 12 || day < 0 || day > 31 ||
  455. ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
  456. (month == 2 && year % 400 == 0 && day > 29) ||
  457. (month == 2 && year % 100 == 0 && day > 28) ||
  458. (month == 2 && year % 4 == 0 && day > 29) ||
  459. (month == 2 && year % 4 != 0 && day > 28)) {
  460. ast_log(LOG_WARNING, "CEL variable %s is not a valid date ('%s').\n", entry->name, colptr);
  461. continue;
  462. }
  463. if (year > 0 && year < 100) {
  464. year += 2000;
  465. }
  466. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  467. LENGTHEN_BUF2(17);
  468. ast_str_append(&sql2, 0, "%s{ d '%04d-%02d-%02d' }", first ? "" : ",", year, month, day);
  469. }
  470. break;
  471. case SQL_TYPE_TIME:
  472. if (ast_strlen_zero(colptr)) {
  473. continue;
  474. } else {
  475. int hour = 0, minute = 0, second = 0;
  476. int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second);
  477. if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
  478. ast_log(LOG_WARNING, "CEL variable %s is not a valid time ('%s').\n", entry->name, colptr);
  479. continue;
  480. }
  481. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  482. LENGTHEN_BUF2(15);
  483. ast_str_append(&sql2, 0, "%s{ t '%02d:%02d:%02d' }", first ? "" : ",", hour, minute, second);
  484. }
  485. break;
  486. case SQL_TYPE_TIMESTAMP:
  487. case SQL_TIMESTAMP:
  488. if (ast_strlen_zero(colptr)) {
  489. continue;
  490. } else {
  491. int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
  492. int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second);
  493. if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
  494. month <= 0 || month > 12 || day < 0 || day > 31 ||
  495. ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
  496. (month == 2 && year % 400 == 0 && day > 29) ||
  497. (month == 2 && year % 100 == 0 && day > 28) ||
  498. (month == 2 && year % 4 == 0 && day > 29) ||
  499. (month == 2 && year % 4 != 0 && day > 28) ||
  500. hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
  501. ast_log(LOG_WARNING, "CEL variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
  502. continue;
  503. }
  504. if (year > 0 && year < 100) {
  505. year += 2000;
  506. }
  507. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  508. LENGTHEN_BUF2(26);
  509. ast_str_append(&sql2, 0, "%s{ ts '%04d-%02d-%02d %02d:%02d:%02d' }", first ? "" : ",", year, month, day, hour, minute, second);
  510. }
  511. break;
  512. case SQL_INTEGER:
  513. {
  514. int integer = 0;
  515. if (strcasecmp(entry->name, "eventtype") == 0) {
  516. integer = (int) record.event_type;
  517. } else if (ast_strlen_zero(colptr)) {
  518. continue;
  519. } else if (sscanf(colptr, "%30d", &integer) != 1) {
  520. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  521. continue;
  522. }
  523. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  524. LENGTHEN_BUF2(12);
  525. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  526. }
  527. break;
  528. case SQL_BIGINT:
  529. {
  530. long long integer = 0;
  531. if (strcasecmp(entry->name, "eventtype") == 0) {
  532. integer = (long long) record.event_type;
  533. } else if (ast_strlen_zero(colptr)) {
  534. continue;
  535. } else if (sscanf(colptr, "%30lld", &integer) != 1) {
  536. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  537. continue;
  538. }
  539. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  540. LENGTHEN_BUF2(24);
  541. ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", integer);
  542. }
  543. break;
  544. case SQL_SMALLINT:
  545. {
  546. short integer = 0;
  547. if (strcasecmp(entry->name, "eventtype") == 0) {
  548. integer = (short) record.event_type;
  549. } else if (ast_strlen_zero(colptr)) {
  550. continue;
  551. } else if (sscanf(colptr, "%30hd", &integer) != 1) {
  552. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  553. continue;
  554. }
  555. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  556. LENGTHEN_BUF2(6);
  557. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  558. }
  559. break;
  560. case SQL_TINYINT:
  561. {
  562. char integer = 0;
  563. if (strcasecmp(entry->name, "eventtype") == 0) {
  564. integer = (char) record.event_type;
  565. } else if (ast_strlen_zero(colptr)) {
  566. continue;
  567. } else if (sscanf(colptr, "%30hhd", &integer) != 1) {
  568. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  569. continue;
  570. }
  571. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  572. LENGTHEN_BUF2(4);
  573. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  574. }
  575. break;
  576. case SQL_BIT:
  577. {
  578. char integer = 0;
  579. if (strcasecmp(entry->name, "eventtype") == 0) {
  580. integer = (char) record.event_type;
  581. } else if (ast_strlen_zero(colptr)) {
  582. continue;
  583. } else if (sscanf(colptr, "%30hhd", &integer) != 1) {
  584. ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name);
  585. continue;
  586. }
  587. if (integer != 0)
  588. integer = 1;
  589. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  590. LENGTHEN_BUF2(2);
  591. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  592. }
  593. break;
  594. case SQL_NUMERIC:
  595. case SQL_DECIMAL:
  596. {
  597. double number = 0.0;
  598. if (strcasecmp(entry->name, "eventtype") == 0) {
  599. number = (double)record.event_type;
  600. } else if (ast_strlen_zero(colptr)) {
  601. continue;
  602. } else if (sscanf(colptr, "%30lf", &number) != 1) {
  603. ast_log(LOG_WARNING, "CEL variable %s is not an numeric type.\n", entry->name);
  604. continue;
  605. }
  606. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  607. LENGTHEN_BUF2(entry->decimals);
  608. ast_str_append(&sql2, 0, "%s%*.*lf", first ? "" : ",", entry->decimals, entry->radix, number);
  609. }
  610. break;
  611. case SQL_FLOAT:
  612. case SQL_REAL:
  613. case SQL_DOUBLE:
  614. {
  615. double number = 0.0;
  616. if (strcasecmp(entry->name, "eventtype") == 0) {
  617. number = (double) record.event_type;
  618. } else if (ast_strlen_zero(colptr)) {
  619. continue;
  620. } else if (sscanf(colptr, "%30lf", &number) != 1) {
  621. ast_log(LOG_WARNING, "CEL variable %s is not an numeric type.\n", entry->name);
  622. continue;
  623. }
  624. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  625. LENGTHEN_BUF2(entry->decimals);
  626. ast_str_append(&sql2, 0, "%s%lf", first ? "" : ",", number);
  627. }
  628. break;
  629. default:
  630. 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);
  631. continue;
  632. }
  633. first = 0;
  634. }
  635. }
  636. /* Concatenate the two constructed buffers */
  637. LENGTHEN_BUF1(ast_str_strlen(sql2));
  638. ast_str_append(&sql, 0, ")");
  639. ast_str_append(&sql2, 0, ")");
  640. ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2));
  641. ast_verb(11, "[%s]\n", ast_str_buffer(sql));
  642. stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, ast_str_buffer(sql));
  643. if (stmt) {
  644. SQLRowCount(stmt, &rows);
  645. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  646. }
  647. if (rows == 0) {
  648. ast_log(LOG_WARNING, "Insert failed on '%s:%s'. CEL failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
  649. }
  650. early_release:
  651. ast_odbc_release_obj(obj);
  652. }
  653. AST_RWLIST_UNLOCK(&odbc_tables);
  654. /* Next time, just allocate buffers that are that big to start with. */
  655. if (ast_str_strlen(sql) > maxsize) {
  656. maxsize = ast_str_strlen(sql);
  657. }
  658. if (ast_str_strlen(sql2) > maxsize2) {
  659. maxsize2 = ast_str_strlen(sql2);
  660. }
  661. ast_free(sql);
  662. ast_free(sql2);
  663. }
  664. static int unload_module(void)
  665. {
  666. if (event_sub) {
  667. event_sub = ast_event_unsubscribe(event_sub);
  668. }
  669. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  670. event_sub = ast_event_subscribe(AST_EVENT_CEL, odbc_log, "ODBC CEL backend", NULL, AST_EVENT_IE_END);
  671. if (!event_sub) {
  672. ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n");
  673. }
  674. ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
  675. return -1;
  676. }
  677. free_config();
  678. AST_RWLIST_UNLOCK(&odbc_tables);
  679. AST_RWLIST_HEAD_DESTROY(&odbc_tables);
  680. return 0;
  681. }
  682. static int load_module(void)
  683. {
  684. AST_RWLIST_HEAD_INIT(&odbc_tables);
  685. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  686. ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
  687. return 0;
  688. }
  689. load_config();
  690. AST_RWLIST_UNLOCK(&odbc_tables);
  691. event_sub = ast_event_subscribe(AST_EVENT_CEL, odbc_log, "ODBC CEL backend", NULL, AST_EVENT_IE_END);
  692. if (!event_sub) {
  693. ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n");
  694. }
  695. return AST_MODULE_LOAD_SUCCESS;
  696. }
  697. static int reload(void)
  698. {
  699. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  700. ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
  701. return -1;
  702. }
  703. free_config();
  704. load_config();
  705. AST_RWLIST_UNLOCK(&odbc_tables);
  706. return AST_MODULE_LOAD_SUCCESS;
  707. }
  708. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CEL backend",
  709. .load = load_module,
  710. .unload = unload_module,
  711. .reload = reload,
  712. .load_pri = AST_MODPRI_CDR_DRIVER,
  713. );