00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "asterisk.h"
00032
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00034
00035 #include <sys/types.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <string.h>
00040 #include <errno.h>
00041
00042 #include "asterisk/module.h"
00043 #include "asterisk/file.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/options.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/config.h"
00050 #include "asterisk/res_odbc.h"
00051 #include "asterisk/app.h"
00052
00053 static char *config = "func_odbc.conf";
00054
00055 enum {
00056 OPT_ESCAPECOMMAS = (1 << 0),
00057 } odbc_option_flags;
00058
00059 struct acf_odbc_query {
00060 AST_LIST_ENTRY(acf_odbc_query) list;
00061 char dsn[30];
00062 char sql_read[2048];
00063 char sql_write[2048];
00064 unsigned int flags;
00065 struct ast_custom_function *acf;
00066 };
00067
00068 AST_LIST_HEAD_STATIC(queries, acf_odbc_query);
00069
00070 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
00071 {
00072 int res;
00073 char *sql = data;
00074 SQLHSTMT stmt;
00075
00076 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
00077 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00078 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
00079 return NULL;
00080 }
00081
00082 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
00083 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00084 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
00085 SQLCloseCursor(stmt);
00086 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
00087 return NULL;
00088 }
00089
00090 return stmt;
00091 }
00092
00093
00094
00095
00096 static int acf_odbc_write(struct ast_channel *chan, char *cmd, char *s, const char *value)
00097 {
00098 struct odbc_obj *obj;
00099 struct acf_odbc_query *query;
00100 char *t, buf[2048]="", varname[15];
00101 int i, bogus_chan = 0;
00102 AST_DECLARE_APP_ARGS(values,
00103 AST_APP_ARG(field)[100];
00104 );
00105 AST_DECLARE_APP_ARGS(args,
00106 AST_APP_ARG(field)[100];
00107 );
00108 SQLHSTMT stmt;
00109 SQLLEN rows=0;
00110
00111 AST_LIST_LOCK(&queries);
00112 AST_LIST_TRAVERSE(&queries, query, list) {
00113 if (!strcmp(query->acf->name, cmd)) {
00114 break;
00115 }
00116 }
00117
00118 if (!query) {
00119 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
00120 AST_LIST_UNLOCK(&queries);
00121 return -1;
00122 }
00123
00124 obj = ast_odbc_request_obj(query->dsn, 0);
00125
00126 if (!obj) {
00127 ast_log(LOG_ERROR, "No database handle available with the name of '%s' (check res_odbc.conf)\n", query->dsn);
00128 AST_LIST_UNLOCK(&queries);
00129 return -1;
00130 }
00131
00132 if (!chan) {
00133 if ((chan = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/func_odbc")))
00134 bogus_chan = 1;
00135 }
00136
00137 if (chan)
00138 ast_autoservice_start(chan);
00139
00140
00141 t = value ? ast_strdupa(value) : "";
00142
00143 if (!s || !t) {
00144 ast_log(LOG_ERROR, "Out of memory\n");
00145 AST_LIST_UNLOCK(&queries);
00146 if (chan)
00147 ast_autoservice_stop(chan);
00148 if (bogus_chan)
00149 ast_channel_free(chan);
00150 return -1;
00151 }
00152
00153 AST_STANDARD_APP_ARGS(args, s);
00154 for (i = 0; i < args.argc; i++) {
00155 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
00156 pbx_builtin_pushvar_helper(chan, varname, args.field[i]);
00157 }
00158
00159
00160
00161 AST_NONSTANDARD_APP_ARGS(values, t, ',');
00162 for (i = 0; i < values.argc; i++) {
00163 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
00164 pbx_builtin_pushvar_helper(chan, varname, values.field[i]);
00165 }
00166
00167
00168 pbx_builtin_pushvar_helper(chan, "VALUE", value ? value : "");
00169
00170 pbx_substitute_variables_helper(chan, query->sql_write, buf, sizeof(buf) - 1);
00171
00172
00173 for (i = 0; i < args.argc; i++) {
00174 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
00175 pbx_builtin_setvar_helper(chan, varname, NULL);
00176 }
00177
00178 for (i = 0; i < values.argc; i++) {
00179 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
00180 pbx_builtin_setvar_helper(chan, varname, NULL);
00181 }
00182 pbx_builtin_setvar_helper(chan, "VALUE", NULL);
00183
00184 AST_LIST_UNLOCK(&queries);
00185
00186 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, buf);
00187
00188 if (stmt) {
00189
00190 SQLRowCount(stmt, &rows);
00191 }
00192
00193
00194
00195
00196
00197 snprintf(varname, sizeof(varname), "%d", (int)rows);
00198 pbx_builtin_setvar_helper(chan, "ODBCROWS", varname);
00199
00200 if (stmt) {
00201 SQLCloseCursor(stmt);
00202 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00203 }
00204 if (obj)
00205 ast_odbc_release_obj(obj);
00206
00207 if (chan)
00208 ast_autoservice_stop(chan);
00209 if (bogus_chan)
00210 ast_channel_free(chan);
00211
00212 return 0;
00213 }
00214
00215 static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf, size_t len)
00216 {
00217 struct odbc_obj *obj;
00218 struct acf_odbc_query *query;
00219 char sql[2048] = "", varname[15];
00220 int res, x, buflen = 1, escapecommas, bogus_chan = 0;
00221 AST_DECLARE_APP_ARGS(args,
00222 AST_APP_ARG(field)[100];
00223 );
00224 SQLHSTMT stmt;
00225 SQLSMALLINT colcount=0;
00226 SQLLEN indicator;
00227
00228 AST_LIST_LOCK(&queries);
00229 AST_LIST_TRAVERSE(&queries, query, list) {
00230 if (!strcmp(query->acf->name, cmd)) {
00231 break;
00232 }
00233 }
00234
00235 if (!query) {
00236 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
00237 AST_LIST_UNLOCK(&queries);
00238 return -1;
00239 }
00240
00241 obj = ast_odbc_request_obj(query->dsn, 0);
00242
00243 if (!obj) {
00244 ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
00245 AST_LIST_UNLOCK(&queries);
00246 return -1;
00247 }
00248
00249 if (!chan) {
00250 if ((chan = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/func_odbc")))
00251 bogus_chan = 1;
00252 }
00253
00254 if (chan)
00255 ast_autoservice_start(chan);
00256
00257 AST_STANDARD_APP_ARGS(args, s);
00258 for (x = 0; x < args.argc; x++) {
00259 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
00260 pbx_builtin_pushvar_helper(chan, varname, args.field[x]);
00261 }
00262
00263 pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1);
00264
00265
00266 for (x = 0; x < args.argc; x++) {
00267 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
00268 pbx_builtin_setvar_helper(chan, varname, NULL);
00269 }
00270
00271
00272 escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS);
00273
00274 AST_LIST_UNLOCK(&queries);
00275
00276 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
00277
00278 if (!stmt) {
00279 ast_odbc_release_obj(obj);
00280 if (chan)
00281 ast_autoservice_stop(chan);
00282 if (bogus_chan)
00283 ast_channel_free(chan);
00284 return -1;
00285 }
00286
00287 res = SQLNumResultCols(stmt, &colcount);
00288 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00289 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
00290 SQLCloseCursor(stmt);
00291 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
00292 ast_odbc_release_obj(obj);
00293 if (chan)
00294 ast_autoservice_stop(chan);
00295 if (bogus_chan)
00296 ast_channel_free(chan);
00297 return -1;
00298 }
00299
00300 *buf = '\0';
00301
00302 res = SQLFetch(stmt);
00303 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00304 int res1 = -1;
00305 if (res == SQL_NO_DATA) {
00306 if (option_verbose > 3) {
00307 ast_verbose(VERBOSE_PREFIX_4 "Found no rows [%s]\n", sql);
00308 }
00309 res1 = 0;
00310 } else if (option_verbose > 3) {
00311 ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql);
00312 }
00313 SQLCloseCursor(stmt);
00314 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00315 ast_odbc_release_obj(obj);
00316 if (chan)
00317 ast_autoservice_stop(chan);
00318 if (bogus_chan)
00319 ast_channel_free(chan);
00320 return res1;
00321 }
00322
00323 for (x = 0; x < colcount; x++) {
00324 int i;
00325 char coldata[256];
00326
00327 buflen = strlen(buf);
00328 res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator);
00329 if (indicator == SQL_NULL_DATA) {
00330 coldata[0] = '\0';
00331 res = SQL_SUCCESS;
00332 }
00333
00334 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00335 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
00336 SQLCloseCursor(stmt);
00337 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00338 ast_odbc_release_obj(obj);
00339 if (chan)
00340 ast_autoservice_stop(chan);
00341 if (bogus_chan)
00342 ast_channel_free(chan);
00343 return -1;
00344 }
00345
00346
00347 for (i = 0; i < sizeof(coldata); i++) {
00348 if (escapecommas && (coldata[i] == '\\' || coldata[i] == ',')) {
00349 buf[buflen++] = '\\';
00350 }
00351 buf[buflen++] = coldata[i];
00352
00353 if (buflen >= len - 2)
00354 break;
00355
00356 if (coldata[i] == '\0')
00357 break;
00358 }
00359
00360 buf[buflen - 1] = ',';
00361 buf[buflen] = '\0';
00362 }
00363
00364 buf[buflen - 1] = '\0';
00365
00366 SQLCloseCursor(stmt);
00367 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00368 ast_odbc_release_obj(obj);
00369 if (chan)
00370 ast_autoservice_stop(chan);
00371 if (bogus_chan)
00372 ast_channel_free(chan);
00373 return 0;
00374 }
00375
00376 static int acf_escape(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00377 {
00378 char *out = buf;
00379
00380 for (; *data && out - buf < len; data++) {
00381 if (*data == '\'') {
00382 *out = '\'';
00383 out++;
00384 }
00385 *out++ = *data;
00386 }
00387 *out = '\0';
00388
00389 return 0;
00390 }
00391
00392 static struct ast_custom_function escape_function = {
00393 .name = "SQL_ESC",
00394 .synopsis = "Escapes single ticks for use in SQL statements",
00395 .syntax = "SQL_ESC(<string>)",
00396 .desc =
00397 "Used in SQL templates to escape data which may contain single ticks (') which\n"
00398 "are otherwise used to delimit data. For example:\n"
00399 "SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n",
00400 .read = acf_escape,
00401 .write = NULL,
00402 };
00403
00404 static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query)
00405 {
00406 const char *tmp;
00407 int res;
00408
00409 if (!cfg || !catg) {
00410 return -1;
00411 }
00412
00413 *query = ast_calloc(1, sizeof(struct acf_odbc_query));
00414 if (! (*query))
00415 return -1;
00416
00417 if ((tmp = ast_variable_retrieve(cfg, catg, "dsn"))) {
00418 ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn));
00419 } else if ((tmp = ast_variable_retrieve(cfg, catg, "writehandle")) || (tmp = ast_variable_retrieve(cfg, catg, "readhandle"))) {
00420 ast_log(LOG_WARNING, "Separate read and write handles are not supported in this version of func_odbc.so\n");
00421 ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn));
00422 } else {
00423 free(*query);
00424 *query = NULL;
00425 ast_log(LOG_ERROR, "No database handle was specified for func_odbc class '%s'\n", catg);
00426 return -1;
00427 }
00428
00429 if ((tmp = ast_variable_retrieve(cfg, catg, "read")) || (tmp = ast_variable_retrieve(cfg, catg, "readsql"))) {
00430 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
00431 }
00432
00433 if ((tmp = ast_variable_retrieve(cfg, catg, "write")) || (tmp = ast_variable_retrieve(cfg, catg, "writesql"))) {
00434 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
00435 }
00436
00437
00438 ast_set_flag((*query), OPT_ESCAPECOMMAS);
00439 if ((tmp = ast_variable_retrieve(cfg, catg, "escapecommas"))) {
00440 if (ast_false(tmp))
00441 ast_clear_flag((*query), OPT_ESCAPECOMMAS);
00442 }
00443
00444 (*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
00445 if (! (*query)->acf) {
00446 free(*query);
00447 *query = NULL;
00448 return -1;
00449 }
00450
00451 if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
00452 if (asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg) < 0) {
00453 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00454 }
00455 } else {
00456 if (asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg) < 0) {
00457 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00458 }
00459 }
00460
00461 if (!((*query)->acf->name)) {
00462 free((*query)->acf);
00463 free(*query);
00464 *query = NULL;
00465 return -1;
00466 }
00467
00468 if (asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name) < 0) {
00469 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00470 (*query)->acf->syntax = NULL;
00471 }
00472
00473 if (!((*query)->acf->syntax)) {
00474 free((char *)(*query)->acf->name);
00475 free((*query)->acf);
00476 free(*query);
00477 *query = NULL;
00478 return -1;
00479 }
00480
00481 res = 0;
00482 (*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
00483 if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
00484 res = asprintf((char **)&((*query)->acf->desc),
00485 "Runs the following query, as defined in func_odbc.conf, performing\n"
00486 "substitution of the arguments into the query as specified by ${ARG1},\n"
00487 "${ARG2}, ... ${ARGn}. When setting the function, the values are provided\n"
00488 "either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
00489 "\nRead:\n%s\n\nWrite:\n%s\n",
00490 (*query)->sql_read,
00491 (*query)->sql_write);
00492 } else if (!ast_strlen_zero((*query)->sql_read)) {
00493 res = asprintf((char **)&((*query)->acf->desc),
00494 "Runs the following query, as defined in func_odbc.conf, performing\n"
00495 "substitution of the arguments into the query as specified by ${ARG1},\n"
00496 "${ARG2}, ... ${ARGn}. This function may only be read, not set.\n\nSQL:\n%s\n",
00497 (*query)->sql_read);
00498 } else if (!ast_strlen_zero((*query)->sql_write)) {
00499 res = asprintf((char **)&((*query)->acf->desc),
00500 "Runs the following query, as defined in func_odbc.conf, performing\n"
00501 "substitution of the arguments into the query as specified by ${ARG1},\n"
00502 "${ARG2}, ... ${ARGn}. The values are provided either in whole as\n"
00503 "${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
00504 "This function may only be set.\nSQL:\n%s\n",
00505 (*query)->sql_write);
00506 } else {
00507 ast_log(LOG_ERROR, "No SQL was found for func_odbc class '%s'\n", catg);
00508 }
00509
00510 if (res < 0) {
00511 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00512 (*query)->acf->desc = NULL;
00513 }
00514
00515
00516 if (!((*query)->acf->desc)) {
00517 free((char *)(*query)->acf->syntax);
00518 free((char *)(*query)->acf->name);
00519 free((*query)->acf);
00520 free(*query);
00521 *query = NULL;
00522 return -1;
00523 }
00524
00525 if (ast_strlen_zero((*query)->sql_read)) {
00526 (*query)->acf->read = NULL;
00527 } else {
00528 (*query)->acf->read = acf_odbc_read;
00529 }
00530
00531 if (ast_strlen_zero((*query)->sql_write)) {
00532 (*query)->acf->write = NULL;
00533 } else {
00534 (*query)->acf->write = acf_odbc_write;
00535 }
00536
00537 return 0;
00538 }
00539
00540 static int free_acf_query(struct acf_odbc_query *query)
00541 {
00542 if (query) {
00543 if (query->acf) {
00544 if (query->acf->name)
00545 free((char *)query->acf->name);
00546 if (query->acf->syntax)
00547 free((char *)query->acf->syntax);
00548 if (query->acf->desc)
00549 free((char *)query->acf->desc);
00550 free(query->acf);
00551 }
00552 free(query);
00553 }
00554 return 0;
00555 }
00556
00557 static int odbc_load_module(void)
00558 {
00559 int res = 0;
00560 struct ast_config *cfg;
00561 char *catg;
00562
00563 AST_LIST_LOCK(&queries);
00564
00565 cfg = ast_config_load(config);
00566 if (!cfg) {
00567 ast_log(LOG_NOTICE, "Unable to load config for func_odbc: %s\n", config);
00568 AST_LIST_UNLOCK(&queries);
00569 return AST_MODULE_LOAD_DECLINE;
00570 }
00571
00572 for (catg = ast_category_browse(cfg, NULL);
00573 catg;
00574 catg = ast_category_browse(cfg, catg)) {
00575 struct acf_odbc_query *query = NULL;
00576
00577 if (init_acf_query(cfg, catg, &query)) {
00578 free_acf_query(query);
00579 } else {
00580 AST_LIST_INSERT_HEAD(&queries, query, list);
00581 ast_custom_function_register(query->acf);
00582 }
00583 }
00584
00585 ast_config_destroy(cfg);
00586 ast_custom_function_register(&escape_function);
00587
00588 AST_LIST_UNLOCK(&queries);
00589 return res;
00590 }
00591
00592 static int odbc_unload_module(void)
00593 {
00594 struct acf_odbc_query *query;
00595
00596 AST_LIST_LOCK(&queries);
00597 while (!AST_LIST_EMPTY(&queries)) {
00598 query = AST_LIST_REMOVE_HEAD(&queries, list);
00599 ast_custom_function_unregister(query->acf);
00600 free_acf_query(query);
00601 }
00602
00603 ast_custom_function_unregister(&escape_function);
00604
00605
00606 AST_LIST_UNLOCK(&queries);
00607 AST_LIST_LOCK(&queries);
00608
00609 AST_LIST_UNLOCK(&queries);
00610 return 0;
00611 }
00612
00613 static int reload(void)
00614 {
00615 int res = 0;
00616 struct ast_config *cfg;
00617 struct acf_odbc_query *oldquery;
00618 char *catg;
00619
00620 AST_LIST_LOCK(&queries);
00621
00622 while (!AST_LIST_EMPTY(&queries)) {
00623 oldquery = AST_LIST_REMOVE_HEAD(&queries, list);
00624 ast_custom_function_unregister(oldquery->acf);
00625 free_acf_query(oldquery);
00626 }
00627
00628 cfg = ast_config_load(config);
00629 if (!cfg) {
00630 ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
00631 goto reload_out;
00632 }
00633
00634 for (catg = ast_category_browse(cfg, NULL);
00635 catg;
00636 catg = ast_category_browse(cfg, catg)) {
00637 struct acf_odbc_query *query = NULL;
00638
00639 if (init_acf_query(cfg, catg, &query)) {
00640 ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
00641 } else {
00642 AST_LIST_INSERT_HEAD(&queries, query, list);
00643 ast_custom_function_register(query->acf);
00644 }
00645 }
00646
00647 ast_config_destroy(cfg);
00648 reload_out:
00649 AST_LIST_UNLOCK(&queries);
00650 return res;
00651 }
00652
00653 static int unload_module(void)
00654 {
00655 return odbc_unload_module();
00656 }
00657
00658 static int load_module(void)
00659 {
00660 return odbc_load_module();
00661 }
00662
00663
00664
00665 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC lookups",
00666 .load = load_module,
00667 .unload = unload_module,
00668 .reload = reload,
00669 );
00670