Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Asterisk -- An open source telephony toolkit. |
3 |
* |
4 |
* Copyright (c) 2005 Tilghman Lesher |
5 |
* |
6 |
* Tilghman Lesher <func_odbc__200604@the-tilghman.com> |
7 |
* |
8 |
* See http://www.asterisk.org for more information about |
9 |
* the Asterisk project. Please do not directly contact |
10 |
* any of the maintainers of this project for assistance; |
11 |
* the project provides a web site, mailing lists and IRC |
12 |
* channels for your use. |
13 |
* |
14 |
* This program is free software, distributed under the terms of |
15 |
* the GNU General Public License Version 2. See the LICENSE file |
16 |
* at the top of the source tree. |
17 |
*/ |
18 |
|
19 |
/*! |
20 |
* \file |
21 |
* |
22 |
* \brief ODBC lookups |
23 |
* |
24 |
* \author Tilghman Lesher <func_odbc__200604@the-tilghman.com> |
25 |
*/ |
26 |
|
27 |
#include <sys/types.h> |
28 |
#include <stdio.h> |
29 |
#include <stdlib.h> |
30 |
#include <unistd.h> |
31 |
#include <string.h> |
32 |
#include <errno.h> |
33 |
|
34 |
#include "asterisk.h" |
35 |
|
36 |
ASTERISK_FILE_VERSION(__FILE__, "$Revision$") |
37 |
|
38 |
#include "asterisk/module.h" |
39 |
#include "asterisk/file.h" |
40 |
#include "asterisk/logger.h" |
41 |
#include "asterisk/options.h" |
42 |
#include "asterisk/channel.h" |
43 |
#include "asterisk/pbx.h" |
44 |
#include "asterisk/module.h" |
45 |
#include "asterisk/config.h" |
46 |
#include "asterisk/res_odbc.h" |
47 |
#include "asterisk/app.h" |
48 |
|
49 |
#include "separate.h" |
50 |
|
51 |
static char *tdesc = "ODBC lookups"; |
52 |
|
53 |
static char *config = "func_odbc.conf"; |
54 |
|
55 |
enum { |
56 |
OPT_ESCAPECOMMAS = (1 << 0), |
57 |
} odbc_option_flags; |
58 |
|
59 |
struct acf_odbc_query { |
60 |
AST_LIST_ENTRY(acf_odbc_query) list; |
61 |
char readhandle[5][30]; |
62 |
char writehandle[5][30]; |
63 |
char sql_read[2048]; |
64 |
char sql_write[2048]; |
65 |
unsigned int flags; |
66 |
struct ast_custom_function *acf; |
67 |
}; |
68 |
|
69 |
AST_LIST_HEAD_STATIC(queries, acf_odbc_query); |
70 |
|
71 |
#ifdef NEEDTRACE |
72 |
static void acf_odbc_error(SQLHSTMT stmt, int res) |
73 |
{ |
74 |
char state[10] = "", diagnostic[256] = ""; |
75 |
SQLINTEGER nativeerror = 0; |
76 |
SQLSMALLINT diagbytes = 0; |
77 |
SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes); |
78 |
ast_log(LOG_WARNING, "SQL return value %d: error %s: %s (len %d)\n", res, state, diagnostic, diagbytes); |
79 |
} |
80 |
#endif |
81 |
|
82 |
/* |
83 |
* Master control routine |
84 |
*/ |
85 |
#define INCREMENT_DSN if (++dsn < 5 && !ast_strlen_zero(query->writehandle[dsn])) goto retry_write |
86 |
static void acf_odbc_write(struct ast_channel *chan, char *cmd, char *data, const char *value) |
87 |
{ |
88 |
odbc_obj *obj; |
89 |
struct acf_odbc_query *query; |
90 |
char *s, *t, buf[2048]="", varname[15]; |
91 |
int res, i, dsn = 0, alloc, prepare, execute; |
92 |
SQLHSTMT stmt; |
93 |
SQLINTEGER nativeerror=0, numfields=0, rows=0; |
94 |
SQLSMALLINT diagbytes=0; |
95 |
unsigned char state[10], diagnostic[256]; |
96 |
AST_DECLARE_APP_ARGS(args, |
97 |
AST_APP_ARG(arg)[100]; |
98 |
); |
99 |
AST_DECLARE_APP_ARGS(values, |
100 |
AST_APP_ARG(val)[100]; |
101 |
); |
102 |
#ifdef NEEDTRACE |
103 |
SQLINTEGER enable = 1; |
104 |
char *tracefile = "/tmp/odbc.trace"; |
105 |
#endif |
106 |
|
107 |
AST_LIST_LOCK(&queries); |
108 |
AST_LIST_TRAVERSE(&queries, query, list) { |
109 |
if (!strcmp(query->acf->name, cmd)) { |
110 |
break; |
111 |
} |
112 |
} |
113 |
|
114 |
if (!query) { |
115 |
ast_log(LOG_ERROR, "No such function '%s'\n", cmd); |
116 |
AST_LIST_UNLOCK(&queries); |
117 |
return; |
118 |
} |
119 |
|
120 |
/* Parse our arguments */ |
121 |
s = ast_strdupa(data); |
122 |
if (value) { |
123 |
t = ast_strdupa(value); |
124 |
} else { |
125 |
t = ""; |
126 |
} |
127 |
|
128 |
if (!s || !t) { |
129 |
ast_log(LOG_ERROR, "Out of memory\n"); |
130 |
AST_LIST_UNLOCK(&queries); |
131 |
return; |
132 |
} |
133 |
|
134 |
TRUNK_STANDARD_APP_ARGS(args, s); |
135 |
for (i = 0; i < args.argc; i++) { |
136 |
snprintf(varname, sizeof(varname), "ARG%d", i + 1); |
137 |
pbx_builtin_pushvar_helper(chan, varname, args.arg[i]); |
138 |
} |
139 |
|
140 |
TRUNK_NONSTANDARD_APP_ARGS(values, t, ','); |
141 |
for (i = 0; i < values.argc; i++) { |
142 |
snprintf(varname, sizeof(varname), "VAL%d", i + 1); |
143 |
pbx_builtin_pushvar_helper(chan, varname, values.val[i]); |
144 |
} |
145 |
|
146 |
/* Additionally set the value as a whole */ |
147 |
/* Note that pbx_builtin_setvar_helper will quite happily take a NULL for the 3rd argument */ |
148 |
pbx_builtin_pushvar_helper(chan, "VALUE", value ? value : ""); |
149 |
|
150 |
pbx_substitute_variables_helper(chan, query->sql_write, buf, sizeof(buf) - 1); |
151 |
|
152 |
/* Restore prior values */ |
153 |
for (i = 0; i < args.argc; i++) { |
154 |
snprintf(varname, sizeof(varname), "ARG%d", i + 1); |
155 |
pbx_builtin_setvar_helper(chan, varname, NULL); |
156 |
} |
157 |
|
158 |
for (i = 0; i < values.argc; i++) { |
159 |
snprintf(varname, sizeof(varname), "VAL%d", i + 1); |
160 |
pbx_builtin_setvar_helper(chan, varname, NULL); |
161 |
} |
162 |
pbx_builtin_setvar_helper(chan, "VALUE", NULL); |
163 |
|
164 |
AST_LIST_UNLOCK(&queries); |
165 |
|
166 |
retry_write: |
167 |
obj = fetch_odbc_obj(query->writehandle[dsn], 0); |
168 |
|
169 |
if (!obj) { |
170 |
INCREMENT_DSN; |
171 |
ast_log(LOG_ERROR, "Unable to load ODBC write class (check res_odbc.conf)\n"); |
172 |
return; |
173 |
} |
174 |
|
175 |
#ifdef NEEDTRACE |
176 |
SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER); |
177 |
SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile)); |
178 |
#endif |
179 |
|
180 |
for (execute = 0; execute < 2; execute++) { |
181 |
for (prepare = 0; prepare < 2; prepare++) { |
182 |
for (alloc = 0; alloc < 2; alloc++) { |
183 |
res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); |
184 |
if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) |
185 |
break; |
186 |
else if (alloc == 0) |
187 |
odbc_sanity_check(obj); |
188 |
else { |
189 |
INCREMENT_DSN; |
190 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); |
191 |
pbx_builtin_setvar_helper(chan, "ODBCROWS", "-1"); |
192 |
return; |
193 |
} |
194 |
} |
195 |
|
196 |
res = SQLPrepare(stmt, (unsigned char *)buf, SQL_NTS); |
197 |
if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) |
198 |
break; |
199 |
else if (prepare == 0) |
200 |
odbc_sanity_check(obj); |
201 |
else { |
202 |
INCREMENT_DSN; |
203 |
ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", buf); |
204 |
SQLCloseCursor(stmt); |
205 |
SQLFreeHandle (SQL_HANDLE_STMT, stmt); |
206 |
pbx_builtin_setvar_helper(chan, "ODBCROWS", "-1"); |
207 |
return; |
208 |
} |
209 |
} |
210 |
|
211 |
res = SQLExecute(stmt); |
212 |
if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) { |
213 |
/* Rows affected */ |
214 |
SQLRowCount(stmt, &rows); |
215 |
break; |
216 |
} else if (execute == 0) |
217 |
odbc_sanity_check(obj); |
218 |
else { |
219 |
if (res == SQL_ERROR) { |
220 |
SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes); |
221 |
for (i = 0; i <= numfields; i++) { |
222 |
SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes); |
223 |
ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes); |
224 |
if (i > 10) { |
225 |
ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields); |
226 |
break; |
227 |
} |
228 |
} |
229 |
} |
230 |
INCREMENT_DSN; |
231 |
|
232 |
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { |
233 |
ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", buf); |
234 |
} |
235 |
rows = -1; |
236 |
} |
237 |
} |
238 |
|
239 |
/* Output the affected rows, for all cases. In the event of failure, we |
240 |
* flag this as -1 rows. Note that this is different from 0 affected rows |
241 |
* which would be the case if we succeeded in our query, but the values did |
242 |
* not change. */ |
243 |
snprintf(varname, sizeof(varname), "%d", (int)rows); |
244 |
pbx_builtin_setvar_helper(chan, "ODBCROWS", varname); |
245 |
|
246 |
SQLCloseCursor(stmt); |
247 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
248 |
} |
249 |
#undef INCREMENT_DSN |
250 |
|
251 |
#define INCREMENT_DSN if (++dsn < 5 && !ast_strlen_zero(query->readhandle[dsn])) goto restartread; |
252 |
static char *acf_odbc_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) |
253 |
{ |
254 |
odbc_obj *obj; |
255 |
struct acf_odbc_query *query; |
256 |
char *s, sql[2048] = "", varname[15], colnames[2048] = ""; |
257 |
int res, x, buflen = 0, dsn = 0, escapecommas, alloc, prepare, execute; |
258 |
AST_DECLARE_APP_ARGS(args, |
259 |
AST_APP_ARG(arg)[100]; |
260 |
); |
261 |
SQLHSTMT stmt; |
262 |
SQLSMALLINT colcount=0; |
263 |
SQLINTEGER indicator; |
264 |
SQLSMALLINT collength; |
265 |
#ifdef NEEDTRACE |
266 |
SQLINTEGER enable = 1; |
267 |
char *tracefile = "/tmp/odbc.trace"; |
268 |
#endif |
269 |
|
270 |
AST_LIST_LOCK(&queries); |
271 |
AST_LIST_TRAVERSE(&queries, query, list) { |
272 |
if (!strcmp(query->acf->name, cmd)) { |
273 |
break; |
274 |
} |
275 |
} |
276 |
|
277 |
if (!query) { |
278 |
ast_log(LOG_ERROR, "No such function '%s'\n", cmd); |
279 |
AST_LIST_UNLOCK(&queries); |
280 |
return ""; |
281 |
} |
282 |
|
283 |
/* Parse our arguments */ |
284 |
if (!(s = ast_strdupa(data))) { |
285 |
AST_LIST_UNLOCK(&queries); |
286 |
return ""; |
287 |
} |
288 |
|
289 |
TRUNK_STANDARD_APP_ARGS(args, s); |
290 |
for (x = 0; x < args.argc; x++) { |
291 |
snprintf(varname, sizeof(varname), "ARG%d", x + 1); |
292 |
pbx_builtin_pushvar_helper(chan, varname, args.arg[x]); |
293 |
} |
294 |
|
295 |
pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1); |
296 |
|
297 |
/* Restore prior values */ |
298 |
for (x = 0; x < args.argc; x++) { |
299 |
snprintf(varname, sizeof(varname), "ARG%d", x + 1); |
300 |
pbx_builtin_setvar_helper(chan, varname, NULL); |
301 |
} |
302 |
|
303 |
/* Save this flag, so we can release the lock */ |
304 |
escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS); |
305 |
|
306 |
AST_LIST_UNLOCK(&queries); |
307 |
|
308 |
restartread: |
309 |
obj = fetch_odbc_obj(query->readhandle[dsn], 0); |
310 |
|
311 |
if (!obj) { |
312 |
INCREMENT_DSN; |
313 |
ast_log(LOG_ERROR, "Unable to load ODBC read class (check res_odbc.conf)\n"); |
314 |
return ""; |
315 |
} |
316 |
|
317 |
#ifdef NEEDTRACE |
318 |
SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER); |
319 |
SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile)); |
320 |
#endif |
321 |
|
322 |
for (execute = 0; execute < 2; execute++) { |
323 |
for (prepare = 0; prepare < 2; prepare++) { |
324 |
for (alloc = 0; alloc < 2; alloc++) { |
325 |
res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); |
326 |
if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) |
327 |
break; |
328 |
else if (alloc == 0) |
329 |
odbc_sanity_check(obj); |
330 |
else { |
331 |
INCREMENT_DSN; |
332 |
ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); |
333 |
return ""; |
334 |
} |
335 |
} |
336 |
|
337 |
res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS); |
338 |
if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) |
339 |
break; |
340 |
else if (prepare == 0) |
341 |
odbc_sanity_check(obj); |
342 |
else { |
343 |
INCREMENT_DSN; |
344 |
SQLCloseCursor(stmt); |
345 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
346 |
ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql); |
347 |
return ""; |
348 |
} |
349 |
} |
350 |
|
351 |
res = odbc_smart_execute(obj, stmt); |
352 |
if ((res == SQL_SUCCESS) || (res == SQL_SUCCESS_WITH_INFO)) |
353 |
break; |
354 |
else if (execute == 0) |
355 |
odbc_sanity_check(obj); |
356 |
else { |
357 |
INCREMENT_DSN; |
358 |
SQLCloseCursor(stmt); |
359 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
360 |
ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql); |
361 |
return ""; |
362 |
} |
363 |
} |
364 |
|
365 |
res = SQLNumResultCols(stmt, &colcount); |
366 |
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { |
367 |
ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); |
368 |
SQLCloseCursor(stmt); |
369 |
SQLFreeHandle (SQL_HANDLE_STMT, stmt); |
370 |
return ""; |
371 |
} |
372 |
|
373 |
memset(buf, 0, len); |
374 |
|
375 |
res = SQLFetch(stmt); |
376 |
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { |
377 |
if (res == SQL_NO_DATA) { |
378 |
if (option_verbose > 3) { |
379 |
ast_verbose(VERBOSE_PREFIX_4 "Found no rows [%s]\n", sql); |
380 |
} |
381 |
} else if (option_verbose > 3) { |
382 |
ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql); |
383 |
} |
384 |
goto acf_out; |
385 |
} |
386 |
|
387 |
for (x = 0; x < colcount; x++) { |
388 |
int i, namelen; |
389 |
char coldata[256], colname[256]; |
390 |
|
391 |
res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, NULL, NULL, NULL); |
392 |
if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) { |
393 |
snprintf(colname, sizeof(colname), "field%d", x); |
394 |
} |
395 |
|
396 |
if (!ast_strlen_zero(colnames)) |
397 |
strncat(colnames, ",", sizeof(colnames) - 1); |
398 |
namelen = strlen(colnames); |
399 |
|
400 |
/* Copy data, encoding '\' and ',' for the argument parser */ |
401 |
for (i = 0; i < sizeof(colname); i++) { |
402 |
if (escapecommas && (colname[i] == '\\' || colname[i] == ',')) { |
403 |
colnames[namelen++] = '\\'; |
404 |
} |
405 |
colnames[namelen++] = colname[i]; |
406 |
|
407 |
if (namelen >= sizeof(colnames) - 2) { |
408 |
colnames[namelen >= sizeof(colnames) ? sizeof(colnames) - 1 : namelen] = '\0'; |
409 |
break; |
410 |
} |
411 |
|
412 |
if (colname[i] == '\0') |
413 |
break; |
414 |
} |
415 |
|
416 |
buflen = strlen(buf); |
417 |
res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator); |
418 |
if (indicator == SQL_NULL_DATA) { |
419 |
coldata[0] = '\0'; |
420 |
res = SQL_SUCCESS; |
421 |
} |
422 |
|
423 |
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { |
424 |
ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); |
425 |
SQLCloseCursor(stmt); |
426 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
427 |
return ""; |
428 |
} |
429 |
|
430 |
/* Copy data, encoding '\' and ',' for the argument parser */ |
431 |
for (i = 0; i < sizeof(coldata); i++) { |
432 |
if (escapecommas && (coldata[i] == '\\' || coldata[i] == ',')) { |
433 |
buf[buflen++] = '\\'; |
434 |
} |
435 |
buf[buflen++] = coldata[i]; |
436 |
|
437 |
if (buflen >= len - 2) { |
438 |
buf[buflen >= len ? len - 1 : buflen] = '\0'; |
439 |
break; |
440 |
} |
441 |
|
442 |
if (coldata[i] == '\0') |
443 |
break; |
444 |
} |
445 |
|
446 |
buf[buflen - 1] = ','; |
447 |
} |
448 |
/* Trim trailing comma */ |
449 |
buf[buflen - 1] = '\0'; |
450 |
|
451 |
pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames); |
452 |
|
453 |
acf_out: |
454 |
SQLCloseCursor(stmt); |
455 |
SQLFreeHandle(SQL_HANDLE_STMT, stmt); |
456 |
return buf; |
457 |
} |
458 |
#undef INCREMENT_DSN |
459 |
|
460 |
static char *acf_escape(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) |
461 |
{ |
462 |
char *in, *out = buf; |
463 |
for (in = data; *in && out - buf < len; in++) { |
464 |
if (*in == '\'') { |
465 |
*out = '\''; |
466 |
out++; |
467 |
} |
468 |
*out = *in; |
469 |
out++; |
470 |
} |
471 |
*out = '\0'; |
472 |
return buf; |
473 |
} |
474 |
|
475 |
static struct ast_custom_function escape_function = { |
476 |
.name = "SQL_ESC", |
477 |
.synopsis = "Escapes single ticks for use in SQL statements", |
478 |
.syntax = "SQL_ESC(<string>)", |
479 |
.desc = |
480 |
"Used in SQL templates to escape data which may contain single ticks (') which\n" |
481 |
"are otherwise used to delimit data. For example:\n" |
482 |
"SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n", |
483 |
.read = acf_escape, |
484 |
.write = NULL, |
485 |
}; |
486 |
|
487 |
|
488 |
static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query) |
489 |
{ |
490 |
const char *tmp; |
491 |
int i; |
492 |
|
493 |
if (!cfg || !catg) { |
494 |
return -1; |
495 |
} |
496 |
|
497 |
*query = calloc(1, sizeof(struct acf_odbc_query)); |
498 |
if (! (*query)) |
499 |
return ENOMEM; |
500 |
|
501 |
if (((tmp = ast_variable_retrieve(cfg, catg, "writehandle"))) || ((tmp = ast_variable_retrieve(cfg, catg, "dsn")))) { |
502 |
char *tmp2 = ast_strdupa(tmp); |
503 |
AST_DECLARE_APP_ARGS(write, |
504 |
AST_APP_ARG(dsn)[5]; |
505 |
); |
506 |
TRUNK_NONSTANDARD_APP_ARGS(write, tmp2, ','); |
507 |
for (i = 0; i < 5; i++) { |
508 |
if (!ast_strlen_zero(write.dsn[i])) |
509 |
ast_copy_string((*query)->writehandle[i], write.dsn[i], sizeof((*query)->writehandle[i])); |
510 |
} |
511 |
} |
512 |
|
513 |
if ((tmp = ast_variable_retrieve(cfg, catg, "readhandle"))) { |
514 |
char *tmp2 = ast_strdupa(tmp); |
515 |
AST_DECLARE_APP_ARGS(read, |
516 |
AST_APP_ARG(dsn)[5]; |
517 |
); |
518 |
TRUNK_NONSTANDARD_APP_ARGS(read, tmp2, ','); |
519 |
for (i = 0; i < 5; i++) { |
520 |
if (!ast_strlen_zero(read.dsn[i])) |
521 |
ast_copy_string((*query)->readhandle[i], read.dsn[i], sizeof((*query)->readhandle[i])); |
522 |
} |
523 |
} else { |
524 |
/* If no separate readhandle, then use the writehandle for reading */ |
525 |
for (i = 0; i < 5; i++) { |
526 |
if (!ast_strlen_zero((*query)->writehandle[i])) |
527 |
ast_copy_string((*query)->readhandle[i], (*query)->writehandle[i], sizeof((*query)->readhandle[i])); |
528 |
} |
529 |
} |
530 |
|
531 |
if ((tmp = ast_variable_retrieve(cfg, catg, "read"))) { |
532 |
ast_log(LOG_WARNING, "Parameter 'read' is deprecated for category %s. Please use 'readsql' instead.\n", catg); |
533 |
ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read)); |
534 |
} else if ((tmp = ast_variable_retrieve(cfg, catg, "readsql"))) |
535 |
ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read)); |
536 |
|
537 |
if (!ast_strlen_zero((*query)->sql_read) && ast_strlen_zero((*query)->readhandle[0])) { |
538 |
free(*query); |
539 |
*query = NULL; |
540 |
ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for reading: %s\n", catg); |
541 |
return EINVAL; |
542 |
} |
543 |
|
544 |
if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) { |
545 |
ast_log(LOG_WARNING, "Parameter 'write' is deprecated for category %s. Please use 'writesql' instead.\n", catg); |
546 |
ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write)); |
547 |
} else if ((tmp = ast_variable_retrieve(cfg, catg, "writesql"))) |
548 |
ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write)); |
549 |
|
550 |
if (!ast_strlen_zero((*query)->sql_write) && ast_strlen_zero((*query)->writehandle[0])) { |
551 |
free(*query); |
552 |
*query = NULL; |
553 |
ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for writing: %s\n", catg); |
554 |
return EINVAL; |
555 |
} |
556 |
|
557 |
/* Allow escaping of embedded commas in fields to be turned off */ |
558 |
ast_set_flag((*query), OPT_ESCAPECOMMAS); |
559 |
if ((tmp = ast_variable_retrieve(cfg, catg, "escapecommas"))) { |
560 |
if (ast_false(tmp)) |
561 |
ast_clear_flag((*query), OPT_ESCAPECOMMAS); |
562 |
} |
563 |
|
564 |
(*query)->acf = calloc(1, sizeof(struct ast_custom_function)); |
565 |
if (! (*query)->acf) { |
566 |
free(*query); |
567 |
*query = NULL; |
568 |
return ENOMEM; |
569 |
} |
570 |
|
571 |
if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) { |
572 |
asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg); |
573 |
} else { |
574 |
asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg); |
575 |
} |
576 |
|
577 |
if (!((*query)->acf->name)) { |
578 |
free((*query)->acf); |
579 |
free(*query); |
580 |
*query = NULL; |
581 |
return ENOMEM; |
582 |
} |
583 |
|
584 |
asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name); |
585 |
|
586 |
if (!((*query)->acf->syntax)) { |
587 |
free((char *)(*query)->acf->name); |
588 |
free((*query)->acf); |
589 |
free(*query); |
590 |
*query = NULL; |
591 |
return ENOMEM; |
592 |
} |
593 |
|
594 |
(*query)->acf->synopsis = "Runs the referenced query with the specified arguments"; |
595 |
if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) { |
596 |
asprintf((char **)&((*query)->acf->desc), |
597 |
"Runs the following query, as defined in func_odbc.conf, performing\n" |
598 |
"substitution of the arguments into the query as specified by ${ARG1},\n" |
599 |
"${ARG2}, ... ${ARGn}. When setting the function, the values are provided\n" |
600 |
"either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n" |
601 |
"\nRead:\n%s\n\nWrite:\n%s\n", |
602 |
(*query)->sql_read, |
603 |
(*query)->sql_write); |
604 |
} else if (!ast_strlen_zero((*query)->sql_read)) { |
605 |
asprintf((char **)&((*query)->acf->desc), |
606 |
"Runs the following query, as defined in func_odbc.conf, performing\n" |
607 |
"substitution of the arguments into the query as specified by ${ARG1},\n" |
608 |
"${ARG2}, ... ${ARGn}. This function may only be read, not set.\n\nSQL:\n%s\n", |
609 |
(*query)->sql_read); |
610 |
} else if (!ast_strlen_zero((*query)->sql_write)) { |
611 |
asprintf((char **)&((*query)->acf->desc), |
612 |
"Runs the following query, as defined in func_odbc.conf, performing\n" |
613 |
"substitution of the arguments into the query as specified by ${ARG1},\n" |
614 |
"${ARG2}, ... ${ARGn}. The values are provided either in whole as\n" |
615 |
"${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n" |
616 |
"This function may only be set.\nSQL:\n%s\n", |
617 |
(*query)->sql_write); |
618 |
} else { |
619 |
free((char *)(*query)->acf->syntax); |
620 |
free((char *)(*query)->acf->name); |
621 |
free((*query)->acf); |
622 |
free(*query); |
623 |
*query = NULL; |
624 |
ast_log(LOG_WARNING, "Section %s was found, but there was no SQL to execute. Ignoring.\n", catg); |
625 |
return EINVAL; |
626 |
} |
627 |
|
628 |
/* Could be out of memory, or could be we have neither sql_read nor sql_write */ |
629 |
if (! ((*query)->acf->desc)) { |
630 |
free((char *)(*query)->acf->syntax); |
631 |
free((char *)(*query)->acf->name); |
632 |
free((*query)->acf); |
633 |
free(*query); |
634 |
*query = NULL; |
635 |
return ENOMEM; |
636 |
} |
637 |
|
638 |
if (ast_strlen_zero((*query)->sql_read)) { |
639 |
(*query)->acf->read = NULL; |
640 |
} else { |
641 |
(*query)->acf->read = acf_odbc_read; |
642 |
} |
643 |
|
644 |
if (ast_strlen_zero((*query)->sql_write)) { |
645 |
(*query)->acf->write = NULL; |
646 |
} else { |
647 |
(*query)->acf->write = acf_odbc_write; |
648 |
} |
649 |
|
650 |
return 0; |
651 |
} |
652 |
|
653 |
static int free_acf_query(struct acf_odbc_query *query) |
654 |
{ |
655 |
if (query) { |
656 |
if (query->acf) { |
657 |
if (query->acf->name) |
658 |
free(query->acf->name); |
659 |
if (query->acf->syntax) |
660 |
free(query->acf->syntax); |
661 |
if (query->acf->desc) |
662 |
free(query->acf->desc); |
663 |
free(query->acf); |
664 |
} |
665 |
free(query); |
666 |
} |
667 |
return 0; |
668 |
} |
669 |
|
670 |
static int odbc_load_module(void) |
671 |
{ |
672 |
int res = 0; |
673 |
struct ast_config *cfg; |
674 |
char *catg; |
675 |
|
676 |
AST_LIST_LOCK(&queries); |
677 |
|
678 |
cfg = ast_config_load(config); |
679 |
if (!cfg) { |
680 |
ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config); |
681 |
AST_LIST_UNLOCK(&queries); |
682 |
return -1; |
683 |
} |
684 |
|
685 |
for (catg = ast_category_browse(cfg, NULL); |
686 |
catg; |
687 |
catg = ast_category_browse(cfg, catg)) { |
688 |
struct acf_odbc_query *query = NULL; |
689 |
int err; |
690 |
|
691 |
if ((err = init_acf_query(cfg, catg, &query))) { |
692 |
if (err == ENOMEM) |
693 |
ast_log(LOG_ERROR, "Out of memory\n"); |
694 |
else if (err == EINVAL) |
695 |
ast_log(LOG_ERROR, "Invalid parameters for category %s\n", catg); |
696 |
else |
697 |
ast_log(LOG_ERROR, "%s (%d)\n", strerror(err), err); |
698 |
} else { |
699 |
AST_LIST_INSERT_HEAD(&queries, query, list); |
700 |
ast_custom_function_register(query->acf); |
701 |
} |
702 |
} |
703 |
|
704 |
ast_config_destroy(cfg); |
705 |
ast_custom_function_register(&escape_function); |
706 |
|
707 |
AST_LIST_UNLOCK(&queries); |
708 |
return res; |
709 |
} |
710 |
|
711 |
static int odbc_unload_module(void) |
712 |
{ |
713 |
struct acf_odbc_query *query; |
714 |
|
715 |
AST_LIST_LOCK(&queries); |
716 |
while (!AST_LIST_EMPTY(&queries)) { |
717 |
query = AST_LIST_REMOVE_HEAD(&queries, list); |
718 |
ast_custom_function_unregister(query->acf); |
719 |
free_acf_query(query); |
720 |
} |
721 |
|
722 |
ast_custom_function_unregister(&escape_function); |
723 |
|
724 |
/* Allow any threads waiting for this lock to pass (avoids a race) */ |
725 |
AST_LIST_UNLOCK(&queries); |
726 |
AST_LIST_LOCK(&queries); |
727 |
|
728 |
AST_LIST_UNLOCK(&queries); |
729 |
return 0; |
730 |
} |
731 |
|
732 |
int reload(void) |
733 |
{ |
734 |
int res = 0; |
735 |
struct ast_config *cfg; |
736 |
struct acf_odbc_query *oldquery; |
737 |
char *catg; |
738 |
|
739 |
AST_LIST_LOCK(&queries); |
740 |
|
741 |
while (!AST_LIST_EMPTY(&queries)) { |
742 |
oldquery = AST_LIST_REMOVE_HEAD(&queries, list); |
743 |
ast_custom_function_unregister(oldquery->acf); |
744 |
free_acf_query(oldquery); |
745 |
} |
746 |
|
747 |
cfg = ast_config_load(config); |
748 |
if (!cfg) { |
749 |
ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config); |
750 |
goto reload_out; |
751 |
} |
752 |
|
753 |
for (catg = ast_category_browse(cfg, NULL); |
754 |
catg; |
755 |
catg = ast_category_browse(cfg, catg)) { |
756 |
struct acf_odbc_query *query = NULL; |
757 |
|
758 |
if (init_acf_query(cfg, catg, &query)) { |
759 |
ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg); |
760 |
} else { |
761 |
AST_LIST_INSERT_HEAD(&queries, query, list); |
762 |
ast_custom_function_register(query->acf); |
763 |
} |
764 |
} |
765 |
|
766 |
ast_config_destroy(cfg); |
767 |
reload_out: |
768 |
AST_LIST_UNLOCK(&queries); |
769 |
return res; |
770 |
} |
771 |
|
772 |
int unload_module(void) |
773 |
{ |
774 |
return odbc_unload_module(); |
775 |
} |
776 |
|
777 |
int load_module(void) |
778 |
{ |
779 |
return odbc_load_module(); |
780 |
} |
781 |
|
782 |
char *description(void) |
783 |
{ |
784 |
return tdesc; |
785 |
} |
786 |
|
787 |
int usecount(void) |
788 |
{ |
789 |
if (! ast_mutex_trylock(&(&queries)->lock)) { |
790 |
ast_mutex_unlock(&(&queries)->lock); |
791 |
return 0; |
792 |
} else { |
793 |
return 1; |
794 |
} |
795 |
} |
796 |
|
797 |
char *key() |
798 |
{ |
799 |
return ASTERISK_GPL_KEY; |
800 |
} |