| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include <sys/types.h> |
| 38 |
#include <time.h> |
| 39 |
#include <math.h> |
| 40 |
|
| 41 |
#include <sql.h> |
| 42 |
#include <sqlext.h> |
| 43 |
#include <sqltypes.h> |
| 44 |
|
| 45 |
#include "asterisk/config.h" |
| 46 |
#include "asterisk/channel.h" |
| 47 |
#include "asterisk/lock.h" |
| 48 |
#include "asterisk/linkedlists.h" |
| 49 |
#include "asterisk/res_odbc.h" |
| 50 |
#include "asterisk/cel.h" |
| 51 |
#include "asterisk/module.h" |
| 52 |
|
| 53 |
#define CONFIG "cel_odbc.conf" |
| 54 |
|
| 55 |
#define ODBC_BACKEND_NAME "ODBC CEL backend" |
| 56 |
|
| 57 |
|
| 58 |
#define CEL_SHOW_USERDEF_DEFAULT 0 |
| 59 |
|
| 60 |
|
| 61 |
static unsigned char cel_show_user_def; |
| 62 |
|
| 63 |
|
| 64 |
static int maxsize = 512, maxsize2 = 512; |
| 65 |
|
| 66 |
struct columns { |
| 67 |
char *name; |
| 68 |
char *celname; |
| 69 |
char *filtervalue; |
| 70 |
char *staticvalue; |
| 71 |
SQLSMALLINT type; |
| 72 |
SQLINTEGER size; |
| 73 |
SQLSMALLINT decimals; |
| 74 |
SQLSMALLINT radix; |
| 75 |
SQLSMALLINT nullable; |
| 76 |
SQLINTEGER octetlen; |
| 77 |
AST_LIST_ENTRY(columns) list; |
| 78 |
}; |
| 79 |
|
| 80 |
struct tables { |
| 81 |
char *connection; |
| 82 |
char *table; |
| 83 |
unsigned int usegmtime:1; |
| 84 |
unsigned int allowleapsec:1; |
| 85 |
AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns; |
| 86 |
AST_RWLIST_ENTRY(tables) list; |
| 87 |
}; |
| 88 |
|
| 89 |
static AST_RWLIST_HEAD_STATIC(odbc_tables, tables); |
| 90 |
|
| 91 |
static int load_config(void) |
| 92 |
{ |
| 93 |
struct ast_config *cfg; |
| 94 |
struct ast_variable *var; |
| 95 |
const char *tmp, *catg; |
| 96 |
struct tables *tableptr; |
| 97 |
struct columns *entry; |
| 98 |
struct odbc_obj *obj; |
| 99 |
char columnname[80]; |
| 100 |
char connection[40]; |
| 101 |
char table[40]; |
| 102 |
int lenconnection, lentable; |
| 103 |
SQLLEN sqlptr; |
| 104 |
int res = 0; |
| 105 |
SQLHSTMT stmt = NULL; |
| 106 |
struct ast_flags config_flags = { 0 }; |
| 107 |
|
| 108 |
cfg = ast_config_load(CONFIG, config_flags); |
| 109 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 110 |
ast_log(LOG_WARNING, "Unable to load " CONFIG ". No ODBC CEL records!\n"); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT; |
| 116 |
for (var = ast_variable_browse(cfg, "general"); var; var = var->next) { |
| 117 |
if (!strcasecmp(var->name, "show_user_defined")) { |
| 118 |
cel_show_user_def = ast_true(var->value) ? 1 : 0; |
| 119 |
} else { |
| 120 |
|
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) { |
| 125 |
if (!strcasecmp(catg, "general")) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
var = ast_variable_browse(cfg, catg); |
| 129 |
if (!var) |
| 130 |
continue; |
| 131 |
|
| 132 |
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) { |
| 133 |
ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg); |
| 134 |
continue; |
| 135 |
} |
| 136 |
ast_copy_string(connection, tmp, sizeof(connection)); |
| 137 |
lenconnection = strlen(connection); |
| 138 |
|
| 139 |
|
| 140 |
obj = ast_odbc_request_obj(connection, 1); |
| 141 |
if (!obj) { |
| 142 |
ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg); |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) { |
| 147 |
ast_log(LOG_NOTICE, "No table name found. Assuming 'cel'.\n"); |
| 148 |
tmp = "cel"; |
| 149 |
} |
| 150 |
ast_copy_string(table, tmp, sizeof(table)); |
| 151 |
lentable = strlen(table); |
| 152 |
|
| 153 |
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
| 154 |
if (!SQL_SUCCEEDED(res)) { |
| 155 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection); |
| 156 |
ast_odbc_release_obj(obj); |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS); |
| 161 |
if (!SQL_SUCCEEDED(res)) { |
| 162 |
ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection); |
| 163 |
ast_odbc_release_obj(obj); |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1); |
| 168 |
if (!tableptr) { |
| 169 |
ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection); |
| 170 |
ast_odbc_release_obj(obj); |
| 171 |
res = -1; |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
tableptr->connection = (char *)tableptr + sizeof(*tableptr); |
| 176 |
tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1; |
| 177 |
ast_copy_string(tableptr->connection, connection, lenconnection + 1); |
| 178 |
ast_copy_string(tableptr->table, table, lentable + 1); |
| 179 |
|
| 180 |
tableptr->usegmtime = 0; |
| 181 |
if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) { |
| 182 |
tableptr->usegmtime = ast_true(tmp); |
| 183 |
} |
| 184 |
|
| 185 |
tableptr->allowleapsec = 1; |
| 186 |
if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "allowleapsecond"))) { |
| 187 |
tableptr->allowleapsec = ast_true(tmp); |
| 188 |
} |
| 189 |
|
| 190 |
ast_verb(3, "Found CEL table %s@%s.\n", tableptr->table, tableptr->connection); |
| 191 |
|
| 192 |
|
| 193 |
for (var = ast_variable_browse(cfg, catg); var; var = var->next) { |
| 194 |
if (strncmp(var->name, "filter", 6) == 0) { |
| 195 |
char *celvar = ast_strdupa(var->name + 6); |
| 196 |
celvar = ast_strip(celvar); |
| 197 |
ast_verb(3, "Found filter %s for cel variable %s in %s@%s\n", var->value, celvar, tableptr->table, tableptr->connection); |
| 198 |
|
| 199 |
entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(celvar) + 1 + strlen(var->value) + 1); |
| 200 |
if (!entry) { |
| 201 |
ast_log(LOG_ERROR, "Out of memory creating filter entry for CEL variable '%s' in table '%s' on connection '%s'\n", celvar, table, connection); |
| 202 |
res = -1; |
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
entry->name = NULL; |
| 208 |
entry->celname = (char *)entry + sizeof(*entry); |
| 209 |
entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(celvar) + 1; |
| 210 |
strcpy(entry->celname, celvar); |
| 211 |
strcpy(entry->filtervalue, var->value); |
| 212 |
|
| 213 |
AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) { |
| 218 |
char *celvar = "", *staticvalue = ""; |
| 219 |
|
| 220 |
SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
for (var = ast_variable_browse(cfg, catg); var; var = var->next) { |
| 229 |
if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) { |
| 230 |
char *alias = ast_strdupa(var->name + 5); |
| 231 |
celvar = ast_strip(alias); |
| 232 |
ast_verb(3, "Found alias %s for column %s in %s@%s\n", celvar, columnname, tableptr->table, tableptr->connection); |
| 233 |
break; |
| 234 |
} else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) { |
| 235 |
char *item = ast_strdupa(var->name + 6); |
| 236 |
item = ast_strip(item); |
| 237 |
if (item[0] == '"' && item[strlen(item) - 1] == '"') { |
| 238 |
|
| 239 |
item[strlen(item) - 1] = '\0'; |
| 240 |
item++; |
| 241 |
} |
| 242 |
staticvalue = item; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(celvar) + 1 + strlen(staticvalue) + 1); |
| 247 |
if (!entry) { |
| 248 |
ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection); |
| 249 |
res = -1; |
| 250 |
break; |
| 251 |
} |
| 252 |
entry->name = (char *)entry + sizeof(*entry); |
| 253 |
strcpy(entry->name, columnname); |
| 254 |
|
| 255 |
if (!ast_strlen_zero(celvar)) { |
| 256 |
entry->celname = entry->name + strlen(columnname) + 1; |
| 257 |
strcpy(entry->celname, celvar); |
| 258 |
} else { |
| 259 |
entry->celname = (char *)entry + sizeof(*entry); |
| 260 |
} |
| 261 |
|
| 262 |
if (!ast_strlen_zero(staticvalue)) { |
| 263 |
entry->staticvalue = entry->celname + strlen(entry->celname) + 1; |
| 264 |
strcpy(entry->staticvalue, staticvalue); |
| 265 |
} |
| 266 |
|
| 267 |
SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL); |
| 268 |
SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL); |
| 269 |
SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL); |
| 270 |
SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL); |
| 271 |
SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL); |
| 272 |
SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL); |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
if (entry->octetlen == 0) |
| 278 |
entry->octetlen = entry->size; |
| 279 |
|
| 280 |
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); |
| 281 |
|
| 282 |
AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list); |
| 283 |
res = 0; |
| 284 |
} |
| 285 |
|
| 286 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 287 |
ast_odbc_release_obj(obj); |
| 288 |
|
| 289 |
if (AST_LIST_FIRST(&(tableptr->columns))) |
| 290 |
AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list); |
| 291 |
else |
| 292 |
ast_free(tableptr); |
| 293 |
} |
| 294 |
ast_config_destroy(cfg); |
| 295 |
return res; |
| 296 |
} |
| 297 |
|
| 298 |
static int free_config(void) |
| 299 |
{ |
| 300 |
struct tables *table; |
| 301 |
struct columns *entry; |
| 302 |
while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) { |
| 303 |
while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) { |
| 304 |
ast_free(entry); |
| 305 |
} |
| 306 |
ast_free(table); |
| 307 |
} |
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
#define LENGTHEN_BUF(size, var_sql) \ |
| 312 |
do { \ |
| 313 |
\ |
| 314 |
if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \ |
| 315 |
if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 1) / 512 + 1) * 512) != 0) { \ |
| 316 |
ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", tableptr->connection, tableptr->table); \ |
| 317 |
ast_free(sql); \ |
| 318 |
ast_free(sql2); \ |
| 319 |
AST_RWLIST_UNLOCK(&odbc_tables); \ |
| 320 |
return; \ |
| 321 |
} \ |
| 322 |
} \ |
| 323 |
} while (0) |
| 324 |
|
| 325 |
#define LENGTHEN_BUF1(size) \ |
| 326 |
LENGTHEN_BUF(size, sql); |
| 327 |
|
| 328 |
#define LENGTHEN_BUF2(size) \ |
| 329 |
LENGTHEN_BUF(size, sql2); |
| 330 |
|
| 331 |
static void odbc_log(struct ast_event *event) |
| 332 |
{ |
| 333 |
struct tables *tableptr; |
| 334 |
struct columns *entry; |
| 335 |
struct odbc_obj *obj; |
| 336 |
struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2); |
| 337 |
char *tmp; |
| 338 |
char colbuf[1024], *colptr; |
| 339 |
SQLHSTMT stmt = NULL; |
| 340 |
int res; |
| 341 |
struct ast_cel_event_record record = { |
| 342 |
.version = AST_CEL_EVENT_RECORD_VERSION, |
| 343 |
}; |
| 344 |
|
| 345 |
if (ast_cel_fill_record(event, &record)) { |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
if (!sql || !sql2) { |
| 350 |
if (sql) |
| 351 |
ast_free(sql); |
| 352 |
if (sql2) |
| 353 |
ast_free(sql2); |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
if (AST_RWLIST_RDLOCK(&odbc_tables)) { |
| 358 |
ast_log(LOG_ERROR, "Unable to lock table list. Insert CEL(s) failed.\n"); |
| 359 |
ast_free(sql); |
| 360 |
ast_free(sql2); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) { |
| 365 |
char *separator = ""; |
| 366 |
ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table); |
| 367 |
ast_str_set(&sql2, 0, " VALUES ("); |
| 368 |
|
| 369 |
|
| 370 |
if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) { |
| 371 |
ast_log(LOG_WARNING, "Unable to retrieve database handle for '%s:%s'. CEL failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql)); |
| 372 |
continue; |
| 373 |
} |
| 374 |
|
| 375 |
AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) { |
| 376 |
int datefield = 0; |
| 377 |
int unknown = 0; |
| 378 |
if (strcasecmp(entry->celname, "eventtime") == 0) { |
| 379 |
datefield = 1; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
if (entry->staticvalue) { |
| 384 |
colptr = ast_strdupa(entry->staticvalue); |
| 385 |
} else if (datefield) { |
| 386 |
struct timeval date_tv = record.event_time; |
| 387 |
struct ast_tm tm = { 0, }; |
| 388 |
ast_localtime(&date_tv, &tm, tableptr->usegmtime ? "UTC" : NULL); |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S.%6q", &tm); |
| 400 |
colptr = colbuf; |
| 401 |
} else { |
| 402 |
if (strcmp(entry->celname, "userdeftype") == 0) { |
| 403 |
ast_copy_string(colbuf, record.user_defined_name, sizeof(colbuf)); |
| 404 |
} else if (strcmp(entry->celname, "cid_name") == 0) { |
| 405 |
ast_copy_string(colbuf, record.caller_id_name, sizeof(colbuf)); |
| 406 |
} else if (strcmp(entry->celname, "cid_num") == 0) { |
| 407 |
ast_copy_string(colbuf, record.caller_id_num, sizeof(colbuf)); |
| 408 |
} else if (strcmp(entry->celname, "cid_ani") == 0) { |
| 409 |
ast_copy_string(colbuf, record.caller_id_ani, sizeof(colbuf)); |
| 410 |
} else if (strcmp(entry->celname, "cid_rdnis") == 0) { |
| 411 |
ast_copy_string(colbuf, record.caller_id_rdnis, sizeof(colbuf)); |
| 412 |
} else if (strcmp(entry->celname, "cid_dnid") == 0) { |
| 413 |
ast_copy_string(colbuf, record.caller_id_dnid, sizeof(colbuf)); |
| 414 |
} else if (strcmp(entry->celname, "exten") == 0) { |
| 415 |
ast_copy_string(colbuf, record.extension, sizeof(colbuf)); |
| 416 |
} else if (strcmp(entry->celname, "context") == 0) { |
| 417 |
ast_copy_string(colbuf, record.context, sizeof(colbuf)); |
| 418 |
} else if (strcmp(entry->celname, "channame") == 0) { |
| 419 |
ast_copy_string(colbuf, record.channel_name, sizeof(colbuf)); |
| 420 |
} else if (strcmp(entry->celname, "appname") == 0) { |
| 421 |
ast_copy_string(colbuf, record.application_name, sizeof(colbuf)); |
| 422 |
} else if (strcmp(entry->celname, "appdata") == 0) { |
| 423 |
ast_copy_string(colbuf, record.application_data, sizeof(colbuf)); |
| 424 |
} else if (strcmp(entry->celname, "accountcode") == 0) { |
| 425 |
ast_copy_string(colbuf, record.account_code, sizeof(colbuf)); |
| 426 |
} else if (strcmp(entry->celname, "peeraccount") == 0) { |
| 427 |
ast_copy_string(colbuf, record.peer_account, sizeof(colbuf)); |
| 428 |
} else if (strcmp(entry->celname, "uniqueid") == 0) { |
| 429 |
ast_copy_string(colbuf, record.unique_id, sizeof(colbuf)); |
| 430 |
} else if (strcmp(entry->celname, "linkedid") == 0) { |
| 431 |
ast_copy_string(colbuf, record.linked_id, sizeof(colbuf)); |
| 432 |
} else if (strcmp(entry->celname, "userfield") == 0) { |
| 433 |
ast_copy_string(colbuf, record.user_field, sizeof(colbuf)); |
| 434 |
} else if (strcmp(entry->celname, "peer") == 0) { |
| 435 |
ast_copy_string(colbuf, record.peer, sizeof(colbuf)); |
| 436 |
} else if (strcmp(entry->celname, "amaflags") == 0) { |
| 437 |
snprintf(colbuf, sizeof(colbuf), "%u", record.amaflag); |
| 438 |
} else if (strcmp(entry->celname, "extra") == 0) { |
| 439 |
ast_copy_string(colbuf, record.extra, sizeof(colbuf)); |
| 440 |
} else if (strcmp(entry->celname, "eventtype") == 0) { |
| 441 |
snprintf(colbuf, sizeof(colbuf), "%u", record.event_type); |
| 442 |
} else { |
| 443 |
colbuf[0] = 0; |
| 444 |
unknown = 1; |
| 445 |
} |
| 446 |
colptr = colbuf; |
| 447 |
} |
| 448 |
|
| 449 |
if (colptr && !unknown) { |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
if (entry->filtervalue && strcasecmp(colptr, entry->filtervalue) != 0) { |
| 455 |
ast_verb(4, "CEL column '%s' with value '%s' does not match filter of" |
| 456 |
" '%s'. Cancelling this CEL.\n", |
| 457 |
entry->celname, colptr, entry->filtervalue); |
| 458 |
goto early_release; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
if (ast_strlen_zero(entry->name)) |
| 463 |
continue; |
| 464 |
|
| 465 |
LENGTHEN_BUF1(strlen(entry->name)); |
| 466 |
|
| 467 |
switch (entry->type) { |
| 468 |
case SQL_CHAR: |
| 469 |
case SQL_VARCHAR: |
| 470 |
case SQL_LONGVARCHAR: |
| 471 |
#ifdef HAVE_ODBC_WCHAR |
| 472 |
case SQL_WCHAR: |
| 473 |
case SQL_WVARCHAR: |
| 474 |
case SQL_WLONGVARCHAR: |
| 475 |
#endif |
| 476 |
case SQL_BINARY: |
| 477 |
case SQL_VARBINARY: |
| 478 |
case SQL_LONGVARBINARY: |
| 479 |
case SQL_GUID: |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
if (strcasecmp(entry->name, "eventtype") == 0) { |
| 484 |
const char *event_name; |
| 485 |
|
| 486 |
event_name = (!cel_show_user_def |
| 487 |
&& record.event_type == AST_CEL_USER_DEFINED) |
| 488 |
? record.user_defined_name : record.event_name; |
| 489 |
snprintf(colbuf, sizeof(colbuf), "%s", event_name); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
if (entry->type != SQL_GUID) { |
| 494 |
if (strlen(colptr) > entry->octetlen) { |
| 495 |
colptr[entry->octetlen] = '\0'; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 500 |
LENGTHEN_BUF2(strlen(colptr)); |
| 501 |
|
| 502 |
|
| 503 |
ast_str_append(&sql2, 0, "%s'", separator); |
| 504 |
for (tmp = colptr; *tmp; tmp++) { |
| 505 |
if (*tmp == '\'') { |
| 506 |
ast_str_append(&sql2, 0, "''"); |
| 507 |
} else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) { |
| 508 |
ast_str_append(&sql2, 0, "\\\\"); |
| 509 |
} else { |
| 510 |
ast_str_append(&sql2, 0, "%c", *tmp); |
| 511 |
} |
| 512 |
} |
| 513 |
ast_str_append(&sql2, 0, "'"); |
| 514 |
break; |
| 515 |
case SQL_TYPE_DATE: |
| 516 |
if (ast_strlen_zero(colptr)) { |
| 517 |
continue; |
| 518 |
} else { |
| 519 |
int year = 0, month = 0, day = 0; |
| 520 |
if (strcasecmp(entry->name, "eventdate") == 0) { |
| 521 |
struct ast_tm tm; |
| 522 |
ast_localtime(&record.event_time, &tm, tableptr->usegmtime ? "UTC" : NULL); |
| 523 |
year = tm.tm_year + 1900; |
| 524 |
month = tm.tm_mon + 1; |
| 525 |
day = tm.tm_mday; |
| 526 |
} else { |
| 527 |
if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 || |
| 528 |
month <= 0 || month > 12 || day < 0 || day > 31 || |
| 529 |
((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) || |
| 530 |
(month == 2 && year % 400 == 0 && day > 29) || |
| 531 |
(month == 2 && year % 100 == 0 && day > 28) || |
| 532 |
(month == 2 && year % 4 == 0 && day > 29) || |
| 533 |
(month == 2 && year % 4 != 0 && day > 28)) { |
| 534 |
ast_log(LOG_WARNING, "CEL variable %s is not a valid date ('%s').\n", entry->name, colptr); |
| 535 |
continue; |
| 536 |
} |
| 537 |
|
| 538 |
if (year > 0 && year < 100) { |
| 539 |
year += 2000; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 544 |
LENGTHEN_BUF2(17); |
| 545 |
ast_str_append(&sql2, 0, "%s{d '%04d-%02d-%02d'}", separator, year, month, day); |
| 546 |
} |
| 547 |
break; |
| 548 |
case SQL_TYPE_TIME: |
| 549 |
if (ast_strlen_zero(colptr)) { |
| 550 |
continue; |
| 551 |
} else { |
| 552 |
int hour = 0, minute = 0, second = 0; |
| 553 |
if (strcasecmp(entry->name, "eventdate") == 0) { |
| 554 |
struct ast_tm tm; |
| 555 |
ast_localtime(&record.event_time, &tm, tableptr->usegmtime ? "UTC" : NULL); |
| 556 |
hour = tm.tm_hour; |
| 557 |
minute = tm.tm_min; |
| 558 |
second = (tableptr->allowleapsec || tm.tm_sec < 60) ? tm.tm_sec : 59; |
| 559 |
} else { |
| 560 |
int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second); |
| 561 |
|
| 562 |
if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > (tableptr->allowleapsec ? 60 : 59)) { |
| 563 |
ast_log(LOG_WARNING, "CEL variable %s is not a valid time ('%s').\n", entry->name, colptr); |
| 564 |
continue; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 569 |
LENGTHEN_BUF2(15); |
| 570 |
ast_str_append(&sql2, 0, "%s{t '%02d:%02d:%02d'}", separator, hour, minute, second); |
| 571 |
} |
| 572 |
break; |
| 573 |
case SQL_TYPE_TIMESTAMP: |
| 574 |
case SQL_TIMESTAMP: |
| 575 |
case SQL_DATETIME: |
| 576 |
if (ast_strlen_zero(colptr)) { |
| 577 |
continue; |
| 578 |
} else { |
| 579 |
if (datefield) { |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 585 |
LENGTHEN_BUF2(27); |
| 586 |
ast_str_append(&sql2, 0, "%s{ts '%s'}", separator, colptr); |
| 587 |
} else { |
| 588 |
int year = 0, month = 0, day = 0, hour = 0, minute = 0; |
| 589 |
|
| 590 |
double second = 0.0; |
| 591 |
if (strcasecmp(entry->name, "eventdate") == 0) { |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
struct ast_tm tm; |
| 598 |
ast_localtime(&record.event_time, &tm, tableptr->usegmtime ? "UTC" : NULL); |
| 599 |
year = tm.tm_year + 1900; |
| 600 |
month = tm.tm_mon + 1; |
| 601 |
day = tm.tm_mday; |
| 602 |
hour = tm.tm_hour; |
| 603 |
minute = tm.tm_min; |
| 604 |
second = (tableptr->allowleapsec || tm.tm_sec < 60) ? tm.tm_sec : 59; |
| 605 |
second += (tm.tm_usec / 1000000.0); |
| 606 |
} else { |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%lf", &year, &month, &day, &hour, &minute, &second); |
| 612 |
|
| 613 |
if ((count != 3 && count != 5 && count != 6) || year <= 0 || |
| 614 |
month <= 0 || month > 12 || day < 0 || day > 31 || |
| 615 |
((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) || |
| 616 |
(month == 2 && year % 400 == 0 && day > 29) || |
| 617 |
(month == 2 && year % 100 == 0 && day > 28) || |
| 618 |
(month == 2 && year % 4 == 0 && day > 29) || |
| 619 |
(month == 2 && year % 4 != 0 && day > 28) || |
| 620 |
hour > 23 || minute > 59 || ((int)floor(second)) > (tableptr->allowleapsec ? 60 : 59) || |
| 621 |
hour < 0 || minute < 0 || ((int)floor(second)) < 0) { |
| 622 |
ast_log(LOG_WARNING, "CEL variable %s is not a valid timestamp ('%s').\n", entry->name, colptr); |
| 623 |
continue; |
| 624 |
} |
| 625 |
|
| 626 |
if (year > 0 && year < 100) { |
| 627 |
year += 2000; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 632 |
LENGTHEN_BUF2(27); |
| 633 |
ast_str_append(&sql2, 0, "%s{ts '%04d-%02d-%02d %02d:%02d:%09.6lf'}", separator, year, month, day, hour, minute, second); |
| 634 |
} |
| 635 |
} |
| 636 |
break; |
| 637 |
case SQL_INTEGER: |
| 638 |
{ |
| 639 |
int integer = 0; |
| 640 |
if (sscanf(colptr, "%30d", &integer) != 1) { |
| 641 |
ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name); |
| 642 |
continue; |
| 643 |
} |
| 644 |
|
| 645 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 646 |
LENGTHEN_BUF2(12); |
| 647 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 648 |
} |
| 649 |
break; |
| 650 |
case SQL_BIGINT: |
| 651 |
{ |
| 652 |
long long integer = 0; |
| 653 |
int ret; |
| 654 |
if ((ret = sscanf(colptr, "%30lld", &integer)) != 1) { |
| 655 |
ast_log(LOG_WARNING, "CEL variable %s is not an integer. (%d - '%s')\n", entry->name, ret, colptr); |
| 656 |
continue; |
| 657 |
} |
| 658 |
|
| 659 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 660 |
LENGTHEN_BUF2(24); |
| 661 |
ast_str_append(&sql2, 0, "%s%lld", separator, integer); |
| 662 |
} |
| 663 |
break; |
| 664 |
case SQL_SMALLINT: |
| 665 |
{ |
| 666 |
short integer = 0; |
| 667 |
if (sscanf(colptr, "%30hd", &integer) != 1) { |
| 668 |
ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name); |
| 669 |
continue; |
| 670 |
} |
| 671 |
|
| 672 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 673 |
LENGTHEN_BUF2(7); |
| 674 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 675 |
} |
| 676 |
break; |
| 677 |
case SQL_TINYINT: |
| 678 |
{ |
| 679 |
signed char integer = 0; |
| 680 |
if (sscanf(colptr, "%30hhd", &integer) != 1) { |
| 681 |
ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name); |
| 682 |
continue; |
| 683 |
} |
| 684 |
|
| 685 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 686 |
LENGTHEN_BUF2(4); |
| 687 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 688 |
} |
| 689 |
break; |
| 690 |
case SQL_BIT: |
| 691 |
{ |
| 692 |
signed char integer = 0; |
| 693 |
if (sscanf(colptr, "%30hhd", &integer) != 1) { |
| 694 |
ast_log(LOG_WARNING, "CEL variable %s is not an integer.\n", entry->name); |
| 695 |
continue; |
| 696 |
} |
| 697 |
if (integer != 0) |
| 698 |
integer = 1; |
| 699 |
|
| 700 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 701 |
LENGTHEN_BUF2(2); |
| 702 |
ast_str_append(&sql2, 0, "%s%d", separator, integer); |
| 703 |
} |
| 704 |
break; |
| 705 |
case SQL_NUMERIC: |
| 706 |
case SQL_DECIMAL: |
| 707 |
{ |
| 708 |
double number = 0.0; |
| 709 |
if (sscanf(colptr, "%30lf", &number) != 1) { |
| 710 |
ast_log(LOG_WARNING, "CEL variable %s is not an numeric type.\n", entry->name); |
| 711 |
continue; |
| 712 |
} |
| 713 |
|
| 714 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 715 |
LENGTHEN_BUF2(entry->decimals + 2); |
| 716 |
ast_str_append(&sql2, 0, "%s%*.*lf", separator, entry->decimals, entry->radix, number); |
| 717 |
} |
| 718 |
break; |
| 719 |
case SQL_FLOAT: |
| 720 |
case SQL_REAL: |
| 721 |
case SQL_DOUBLE: |
| 722 |
{ |
| 723 |
double number = 0.0; |
| 724 |
if (sscanf(colptr, "%30lf", &number) != 1) { |
| 725 |
ast_log(LOG_WARNING, "CEL variable %s is not an numeric type.\n", entry->name); |
| 726 |
continue; |
| 727 |
} |
| 728 |
|
| 729 |
ast_str_append(&sql, 0, "%s%s", separator, entry->name); |
| 730 |
LENGTHEN_BUF2(entry->decimals); |
| 731 |
ast_str_append(&sql2, 0, "%s%lf", separator, number); |
| 732 |
} |
| 733 |
break; |
| 734 |
default: |
| 735 |
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); |
| 736 |
continue; |
| 737 |
} |
| 738 |
separator = ", "; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
LENGTHEN_BUF1(ast_str_strlen(sql2)); |
| 744 |
ast_str_append(&sql, 0, ")"); |
| 745 |
ast_str_append(&sql2, 0, ")"); |
| 746 |
ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2)); |
| 747 |
|
| 748 |
ast_debug(3, "Executing SQL statement: [%s]\n", ast_str_buffer(sql)); |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
| 753 |
if (!SQL_SUCCEEDED(res)) { |
| 754 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", |
| 755 |
tableptr->connection); |
| 756 |
goto early_release; |
| 757 |
} |
| 758 |
|
| 759 |
res = ast_odbc_execute_sql(obj, stmt, ast_str_buffer(sql)); |
| 760 |
if (!SQL_SUCCEEDED(res)) { |
| 761 |
ast_log(LOG_WARNING, "Insert failed on '%s:%s'. CEL failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql)); |
| 762 |
} |
| 763 |
|
| 764 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
| 765 |
|
| 766 |
early_release: |
| 767 |
ast_odbc_release_obj(obj); |
| 768 |
} |
| 769 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 770 |
|
| 771 |
|
| 772 |
if (ast_str_strlen(sql) > maxsize) { |
| 773 |
maxsize = ast_str_strlen(sql); |
| 774 |
} |
| 775 |
if (ast_str_strlen(sql2) > maxsize2) { |
| 776 |
maxsize2 = ast_str_strlen(sql2); |
| 777 |
} |
| 778 |
|
| 779 |
ast_free(sql); |
| 780 |
ast_free(sql2); |
| 781 |
} |
| 782 |
|
| 783 |
static int unload_module(void) |
| 784 |
{ |
| 785 |
if (AST_RWLIST_WRLOCK(&odbc_tables)) { |
| 786 |
ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n"); |
| 787 |
return -1; |
| 788 |
} |
| 789 |
|
| 790 |
ast_cel_backend_unregister(ODBC_BACKEND_NAME); |
| 791 |
free_config(); |
| 792 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 793 |
AST_RWLIST_HEAD_DESTROY(&odbc_tables); |
| 794 |
|
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
static int load_module(void) |
| 799 |
{ |
| 800 |
AST_RWLIST_HEAD_INIT(&odbc_tables); |
| 801 |
|
| 802 |
if (AST_RWLIST_WRLOCK(&odbc_tables)) { |
| 803 |
ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n"); |
| 804 |
return AST_MODULE_LOAD_DECLINE; |
| 805 |
} |
| 806 |
load_config(); |
| 807 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 808 |
if (ast_cel_backend_register(ODBC_BACKEND_NAME, odbc_log)) { |
| 809 |
ast_log(LOG_ERROR, "Unable to subscribe to CEL events\n"); |
| 810 |
free_config(); |
| 811 |
return AST_MODULE_LOAD_DECLINE; |
| 812 |
} |
| 813 |
return AST_MODULE_LOAD_SUCCESS; |
| 814 |
} |
| 815 |
|
| 816 |
static int reload(void) |
| 817 |
{ |
| 818 |
if (AST_RWLIST_WRLOCK(&odbc_tables)) { |
| 819 |
ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n"); |
| 820 |
return AST_MODULE_LOAD_DECLINE; |
| 821 |
} |
| 822 |
|
| 823 |
free_config(); |
| 824 |
load_config(); |
| 825 |
AST_RWLIST_UNLOCK(&odbc_tables); |
| 826 |
return AST_MODULE_LOAD_SUCCESS; |
| 827 |
} |
| 828 |
|
| 829 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CEL backend", |
| 830 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 831 |
.load = load_module, |
| 832 |
.unload = unload_module, |
| 833 |
.reload = reload, |
| 834 |
.load_pri = AST_MODPRI_CDR_DRIVER, |
| 835 |
.requires = "cel,res_odbc", |
| 836 |
); |