Index: res/res_config_odbc.c =================================================================== --- res/res_config_odbc.c (revision 272145) +++ res/res_config_odbc.c (working copy) @@ -1,7 +1,7 @@ /* * Asterisk -- An open source telephony toolkit. * - * Copyright (C) 1999 - 2005, Digium, Inc. + * Copyright (C) 1999 - 2010, Digium, Inc. * * Mark Spencer * @@ -53,18 +53,33 @@ #include "asterisk/options.h" #include "asterisk/res_odbc.h" #include "asterisk/utils.h" +#include "asterisk/stringfields.h" struct custom_prepare_struct { const char *sql; const char *extra; + AST_DECLARE_STRING_FIELDS( + AST_STRING_FIELD(encoding)[256]; + ); va_list ap; }; +static void decode_chunk(char *chunk) +{ + for (; *chunk; chunk++) { + if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) { + sscanf(chunk + 1, "%02hhd", chunk); + memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1); + } + } +} + static SQLHSTMT custom_prepare(struct odbc_obj *obj, void *data) { int res, x = 1; struct custom_prepare_struct *cps = data; const char *newparam, *newval; + char encodebuf[1024]; SQLHSTMT stmt; va_list ap; @@ -85,6 +100,26 @@ while ((newparam = va_arg(ap, const char *))) { newval = va_arg(ap, const char *); + if (strchr(newval, ';')) { + char *eptr = encodebuf; + const char *vptr = newval; + for (; *vptr && eptr < encodebuf + sizeof(encodebuf); vptr++) { + if (strchr("%;", *vptr)) { + snprintf(eptr, encodebuf + sizeof(encodebuf) - eptr, "^%02hhX", *vptr); + eptr += 3; + vptr++; + } else { + *eptr++ = *vptr++; + } + } + if (eptr < encodebuf + sizeof(encodebuf)) { + *eptr = '\0'; + } else { + encodebuf[sizeof(encodebuf) - 1] = '\0'; + } + ast_string_field_set(cps, encoding[x], encodebuf); + newval = cps->encoding[x]; + } SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL); } va_end(ap); @@ -118,22 +153,29 @@ va_list aq; struct custom_prepare_struct cps = { .sql = sql }; + if (ast_string_field_init(&cps, 256)) { + return NULL; + } va_copy(cps.ap, ap); va_copy(aq, ap); - if (!table) + if (!table) { + ast_string_field_free_memory(&cps); return NULL; + } obj = ast_odbc_request_obj(database, 0); if (!obj) { ast_log(LOG_ERROR, "No database handle available with the name of '%s' (check res_odbc.conf)\n", database); + ast_string_field_free_memory(&cps); return NULL; } newparam = va_arg(aq, const char *); if (!newparam) { ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } newval = va_arg(aq, const char *); @@ -152,6 +194,7 @@ if (!stmt) { ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } @@ -160,6 +203,7 @@ ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } @@ -167,12 +211,14 @@ if (res == SQL_NO_DATA) { SQLFreeHandle (SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql); SQLFreeHandle (SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } for (x = 0; x < colcount; x++) { @@ -185,6 +231,7 @@ if (var) ast_variables_destroy(var); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } @@ -201,15 +248,20 @@ return NULL; } stringp = rowdata; - while(stringp) { + while (stringp) { chunk = strsep(&stringp, ";"); if (!ast_strlen_zero(ast_strip(chunk))) { + if (strchr(chunk, '^')) { + decode_chunk(chunk); + } if (prev) { prev->next = ast_variable_new(coltitle, chunk); - if (prev->next) + if (prev->next) { prev = prev->next; - } else + } + } else { prev = var = ast_variable_new(coltitle, chunk); + } } } } @@ -217,6 +269,7 @@ SQLFreeHandle(SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return var; } @@ -248,20 +301,24 @@ struct custom_prepare_struct cps = { .sql = sql }; va_list aq; + if (!table || ast_string_field_init(&cps, 256)) { + return NULL; + } va_copy(cps.ap, ap); va_copy(aq, ap); - if (!table) - return NULL; memset(&ra, 0, sizeof(ra)); obj = ast_odbc_request_obj(database, 0); - if (!obj) + if (!obj) { + ast_string_field_free_memory(&cps); return NULL; + } newparam = va_arg(aq, const char *); if (!newparam) { ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } initfield = ast_strdupa(newparam); @@ -285,6 +342,7 @@ if (!stmt) { ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } @@ -293,6 +351,7 @@ ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); SQLFreeHandle(SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } @@ -301,6 +360,7 @@ ast_log(LOG_WARNING, "Out of memory!\n"); SQLFreeHandle(SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return NULL; } @@ -337,11 +397,15 @@ continue; } stringp = rowdata; - while(stringp) { + while (stringp) { chunk = strsep(&stringp, ";"); if (!ast_strlen_zero(ast_strip(chunk))) { - if (initfield && !strcmp(initfield, coltitle)) + if (strchr(chunk, '^')) { + decode_chunk(chunk); + } + if (initfield && !strcmp(initfield, coltitle)) { ast_category_rename(cat, chunk); + } var = ast_variable_new(coltitle, chunk); ast_variable_append(cat, var); } @@ -352,6 +416,7 @@ SQLFreeHandle(SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return cfg; } @@ -366,19 +431,21 @@ va_list aq; struct custom_prepare_struct cps = { .sql = sql, .extra = lookup }; + if (!table || ast_string_field_init(&cps, 256)) { + return -1; + } va_copy(cps.ap, ap); va_copy(aq, ap); - - if (!table) - return -1; - obj = ast_odbc_request_obj(database, 0); - if (!obj) + if (!(obj = ast_odbc_request_obj(database, 0))) { + ast_string_field_free_memory(&cps); return -1; + } newparam = va_arg(aq, const char *); if (!newparam) { ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return -1; } newval = va_arg(aq, const char *); @@ -394,20 +461,23 @@ if (!stmt) { ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); return -1; } res = SQLRowCount(stmt, &rowcount); SQLFreeHandle (SQL_HANDLE_STMT, stmt); ast_odbc_release_obj(obj); + ast_string_field_free_memory(&cps); if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql); return -1; } - if (rowcount >= 0) - return (int)rowcount; + if (rowcount >= 0) { + return (int) rowcount; + } return -1; } Index: res/res_config_pgsql.c =================================================================== --- res/res_config_pgsql.c (revision 272145) +++ res/res_config_pgsql.c (working copy) @@ -1,8 +1,8 @@ /* * Asterisk -- A telephony toolkit for Linux. * - * Copyright (C) 1999-2005, Digium, Inc. - * + * Copyright (C) 1999-2010, Digium, Inc. + * * Manuel Guesdon - Postgresql RealTime Driver Author/Adaptor * Mark Spencer - Asterisk Author * Matthew Boehm - MySQL RealTime Driver Author @@ -76,11 +76,42 @@ cli_realtime_pgsql_status_usage }, }; +static char *encode_chunk(const char *chunk, char *buf, size_t len) +{ + char *cptr = buf; + for (; *chunk && cptr < buf + len; chunk++) { + if (strchr(";^", *chunk)) { + snprintf(cptr, buf + len - cptr, "^%02hhX", *chunk); + cptr += 3; + } else { + *cptr++ = *chunk; + } + } + if (cptr < buf + len) { + *cptr = '\0'; + } else { + buf[len - 1] = '\0'; + } + return buf; +} + +static char *decode_chunk(char *chunk) +{ + char *orig = chunk; + for (; *chunk; chunk++) { + if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) { + sscanf(chunk + 1, "%02hhd", chunk); + memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1); + } + } + return orig; +} + static struct ast_variable *realtime_pgsql(const char *database, const char *table, va_list ap) { PGresult *result = NULL; int num_rows = 0, pgerror; - char sql[256], escapebuf[513]; + char sql[256], escapebuf[2049], semibuf[1024]; char *stringp; char *chunk; char *op; @@ -109,7 +140,7 @@ If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */ op = strchr(newparam, ' ') ? "" : " ="; - PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror); + PQescapeStringConn(pgsqlConn, escapebuf, encode_chunk(newval, semibuf, sizeof(semibuf)), (sizeof(escapebuf) - 1) / 2, &pgerror); if (pgerror) { ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval); va_end(ap); @@ -125,7 +156,7 @@ else op = ""; - PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror); + PQescapeStringConn(pgsqlConn, escapebuf, encode_chunk(newval, semibuf, sizeof(semibuf)), (sizeof(escapebuf) - 1) / 2, &pgerror); if (pgerror) { ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval); va_end(ap); @@ -167,7 +198,7 @@ } } - ast_log(LOG_DEBUG, "1Postgresql RealTime: Result=%p Query: %s\n", result, sql); + ast_log(LOG_DEBUG, "Postgresql RealTime: Result=%p Query: %s\n", result, sql); if ((num_rows = PQntuples(result)) > 0) { int i = 0; @@ -189,7 +220,7 @@ stringp = PQgetvalue(result, rowIndex, i); while (stringp) { chunk = strsep(&stringp, ";"); - if (chunk && !ast_strlen_zero(ast_strip(chunk))) { + if (chunk && !ast_strlen_zero(decode_chunk(ast_strip(chunk)))) { if (prev) { prev->next = ast_variable_new(fieldnames[i], chunk); if (prev->next) { @@ -217,7 +248,7 @@ { PGresult *result = NULL; int num_rows = 0, pgerror; - char sql[256], escapebuf[513]; + char sql[256], escapebuf[2049], semibuf[1024]; const char *initfield = NULL; char *stringp; char *chunk; @@ -264,7 +295,7 @@ else op = ""; - PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror); + PQescapeStringConn(pgsqlConn, escapebuf, encode_chunk(newval, semibuf, sizeof(semibuf)), (sizeof(escapebuf) - 1) / 2, &pgerror); if (pgerror) { ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval); va_end(ap); @@ -280,7 +311,7 @@ else op = ""; - PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror); + PQescapeStringConn(pgsqlConn, escapebuf, encode_chunk(newval, semibuf, sizeof(semibuf)), (sizeof(escapebuf) - 1) / 2, &pgerror); if (pgerror) { ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval); va_end(ap); @@ -353,7 +384,7 @@ stringp = PQgetvalue(result, rowIndex, i); while (stringp) { chunk = strsep(&stringp, ";"); - if (chunk && !ast_strlen_zero(ast_strip(chunk))) { + if (chunk && !ast_strlen_zero(decode_chunk(ast_strip(chunk)))) { if (initfield && !strcmp(initfield, fieldnames[i])) { ast_category_rename(cat, chunk); } @@ -381,7 +412,7 @@ { PGresult *result = NULL; int numrows = 0, pgerror; - char sql[256], escapebuf[513]; + char sql[256], escapebuf[2049], semibuf[1024]; const char *newparam, *newval; if (!table) { @@ -405,7 +436,7 @@ /* Create the first part of the query using the first parameter/value pairs we just extracted If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */ - PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror); + PQescapeStringConn(pgsqlConn, escapebuf, encode_chunk(newval, semibuf, sizeof(semibuf)), (sizeof(escapebuf) - 1) / 2, &pgerror); if (pgerror) { ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval); va_end(ap); @@ -416,7 +447,7 @@ while ((newparam = va_arg(ap, const char *))) { newval = va_arg(ap, const char *); - PQescapeStringConn(pgsqlConn, escapebuf, newval, (sizeof(escapebuf) - 1) / 2, &pgerror); + PQescapeStringConn(pgsqlConn, escapebuf, encode_chunk(newval, semibuf, sizeof(semibuf)), (sizeof(escapebuf) - 1) / 2, &pgerror); if (pgerror) { ast_log(LOG_ERROR, "Postgres detected invalid input: '%s'\n", newval); va_end(ap);