Convert func_odbc to use the prepare_and_execute callback, which helps with a databas...
[asterisk/asterisk.git] / funcs / func_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2005, 2006 Tilghman Lesher
5  *
6  * Tilghman Lesher <func_odbc__200508@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__200508@the-tilghman.com>
25  */
26
27 /*** MODULEINFO
28         <depend>unixodbc</depend>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40
41 #include "asterisk/module.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/options.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/config.h"
49 #include "asterisk/res_odbc.h"
50 #include "asterisk/app.h"
51
52 static char *tdesc = "ODBC lookups";
53
54 static char *config = "func_odbc.conf";
55
56 enum {
57         OPT_ESCAPECOMMAS =      (1 << 0),
58 } odbc_option_flags;
59
60 struct acf_odbc_query {
61         AST_LIST_ENTRY(acf_odbc_query) list;
62         char dsn[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 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
72 {
73         int res;
74         char *sql = data;
75         SQLHSTMT stmt;
76
77         res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
78         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
79                 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
80                 return NULL;
81         }
82
83         res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
84         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
85                 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
86                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
87                 return NULL;
88         }
89
90         return stmt;
91 }
92
93 /*
94  * Master control routine
95  */
96 static int acf_odbc_write(struct ast_channel *chan, char *cmd, char *s, const char *value)
97 {
98         struct odbc_obj *obj;
99         struct acf_odbc_query *query;
100         char *t, buf[2048]="", varname[15];
101         int i;
102         AST_DECLARE_APP_ARGS(values,
103                 AST_APP_ARG(field)[100];
104         );
105         AST_DECLARE_APP_ARGS(args,
106                 AST_APP_ARG(field)[100];
107         );
108         SQLHSTMT stmt;
109         SQLINTEGER rows=0;
110
111         AST_LIST_LOCK(&queries);
112         AST_LIST_TRAVERSE(&queries, query, list) {
113                 if (!strcmp(query->acf->name, cmd)) {
114                         break;
115                 }
116         }
117
118         if (!query) {
119                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
120                 AST_LIST_UNLOCK(&queries);
121                 return -1;
122         }
123
124         obj = odbc_request_obj(query->dsn, 0);
125
126         if (!obj) {
127                 ast_log(LOG_ERROR, "No database handle available with the name of '%s' (check res_odbc.conf)\n", query->dsn);
128                 AST_LIST_UNLOCK(&queries);
129                 return -1;
130         }
131
132         /* Parse our arguments */
133         t = value ? ast_strdupa(value) : "";
134
135         if (!s || !t) {
136                 ast_log(LOG_ERROR, "Out of memory\n");
137                 AST_LIST_UNLOCK(&queries);
138                 return -1;
139         }
140
141         AST_STANDARD_APP_ARGS(args, s);
142         for (i = 0; i < args.argc; i++) {
143                 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
144                 pbx_builtin_pushvar_helper(chan, varname, args.field[i]);
145         }
146
147         /* Parse values, just like arguments */
148         /* Can't use the pipe, because app Set removes them */
149         AST_NONSTANDARD_APP_ARGS(values, t, ',');
150         for (i = 0; i < values.argc; i++) {
151                 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
152                 pbx_builtin_pushvar_helper(chan, varname, values.field[i]);
153         }
154
155         /* Additionally set the value as a whole (but push an empty string if value is NULL) */
156         pbx_builtin_pushvar_helper(chan, "VALUE", value ? value : "");
157
158         pbx_substitute_variables_helper(chan, query->sql_write, buf, sizeof(buf) - 1);
159
160         /* Restore prior values */
161         for (i = 0; i < args.argc; i++) {
162                 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
163                 pbx_builtin_setvar_helper(chan, varname, NULL);
164         }
165
166         for (i = 0; i < values.argc; i++) {
167                 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
168                 pbx_builtin_setvar_helper(chan, varname, NULL);
169         }
170         pbx_builtin_setvar_helper(chan, "VALUE", NULL);
171
172         AST_LIST_UNLOCK(&queries);
173
174         stmt = odbc_prepare_and_execute(obj, generic_prepare, buf);
175
176         if (stmt) {
177                 /* Rows affected */
178                 SQLRowCount(stmt, &rows);
179         }
180
181         /* Output the affected rows, for all cases.  In the event of failure, we
182          * flag this as -1 rows.  Note that this is different from 0 affected rows
183          * which would be the case if we succeeded in our query, but the values did
184          * not change. */
185         snprintf(varname, sizeof(varname), "%d", (int)rows);
186         pbx_builtin_setvar_helper(chan, "ODBCROWS", varname);
187
188         if (stmt)
189                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
190         if (obj)
191                 odbc_release_obj(obj);
192
193         return 0;
194 }
195
196 static int acf_odbc_read(struct ast_channel *chan, char *cmd, char *s, char *buf, size_t len)
197 {
198         struct odbc_obj *obj;
199         struct acf_odbc_query *query;
200         char sql[2048] = "", varname[15];
201         int res, x, buflen = 0, escapecommas;
202         AST_DECLARE_APP_ARGS(args,
203                 AST_APP_ARG(field)[100];
204         );
205         SQLHSTMT stmt;
206         SQLSMALLINT colcount=0;
207         SQLINTEGER indicator;
208
209         AST_LIST_LOCK(&queries);
210         AST_LIST_TRAVERSE(&queries, query, list) {
211                 if (!strcmp(query->acf->name, cmd)) {
212                         break;
213                 }
214         }
215
216         if (!query) {
217                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
218                 AST_LIST_UNLOCK(&queries);
219                 return -1;
220         }
221
222         obj = odbc_request_obj(query->dsn, 0);
223
224         if (!obj) {
225                 ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
226                 AST_LIST_UNLOCK(&queries);
227                 return -1;
228         }
229
230         AST_STANDARD_APP_ARGS(args, s);
231         for (x = 0; x < args.argc; x++) {
232                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
233                 pbx_builtin_pushvar_helper(chan, varname, args.field[x]);
234         }
235
236         pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1);
237
238         /* Restore prior values */
239         for (x = 0; x < args.argc; x++) {
240                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
241                 pbx_builtin_setvar_helper(chan, varname, NULL);
242         }
243
244         /* Save this flag, so we can release the lock */
245         escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS);
246
247         AST_LIST_UNLOCK(&queries);
248
249         stmt = odbc_prepare_and_execute(obj, generic_prepare, sql);
250
251         if (!stmt) {
252                 odbc_release_obj(obj);
253                 return -1;
254         }
255
256         res = SQLNumResultCols(stmt, &colcount);
257         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
258                 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
259                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
260                 odbc_release_obj(obj);
261                 return -1;
262         }
263
264         *buf = '\0';
265
266         res = SQLFetch(stmt);
267         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
268                 int res1 = -1;
269                 if (res == SQL_NO_DATA) {
270                         if (option_verbose > 3) {
271                                 ast_verbose(VERBOSE_PREFIX_4 "Found no rows [%s]\n", sql);
272                         }
273                         res1 = 0;
274                 } else if (option_verbose > 3) {
275                         ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql);
276                 }
277                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
278                 odbc_release_obj(obj);
279                 return res1;
280         }
281
282         for (x = 0; x < colcount; x++) {
283                 int i;
284                 char coldata[256];
285
286                 buflen = strlen(buf);
287                 res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator);
288                 if (indicator == SQL_NULL_DATA) {
289                         coldata[0] = '\0';
290                         res = SQL_SUCCESS;
291                 }
292
293                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
294                         ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
295                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
296                         odbc_release_obj(obj);
297                         return -1;
298                 }
299
300                 /* Copy data, encoding '\' and ',' for the argument parser */
301                 for (i = 0; i < sizeof(coldata); i++) {
302                         if (escapecommas && (coldata[i] == '\\' || coldata[i] == ',')) {
303                                 buf[buflen++] = '\\';
304                         }
305                         buf[buflen++] = coldata[i];
306
307                         if (buflen >= len - 2)
308                                 break;
309
310                         if (coldata[i] == '\0')
311                                 break;
312                 }
313
314                 buf[buflen - 1] = ',';
315         }
316         /* Trim trailing comma */
317         buf[buflen - 1] = '\0';
318
319         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
320         odbc_release_obj(obj);
321         return 0;
322 }
323
324 static int acf_escape(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
325 {
326         char *out = buf;
327
328         for (; *data && out - buf < len; data++) {
329                 if (*data == '\'') {
330                         *out = '\'';
331                         out++;
332                 }
333                 *out++ = *data;
334         }
335         *out = '\0';
336
337         return 0;
338 }
339
340 static struct ast_custom_function escape_function = {
341         .name = "SQL_ESC",
342         .synopsis = "Escapes single ticks for use in SQL statements",
343         .syntax = "SQL_ESC(<string>)",
344         .desc =
345 "Used in SQL templates to escape data which may contain single ticks (') which\n"
346 "are otherwise used to delimit data.  For example:\n"
347 "SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n",
348         .read = acf_escape,
349         .write = NULL,
350 };
351
352 static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query)
353 {
354         char *tmp;
355
356         if (!cfg || !catg) {
357                 return -1;
358         }
359
360         *query = ast_calloc(1, sizeof(struct acf_odbc_query));
361         if (! (*query))
362                 return -1;
363
364         if ((tmp = ast_variable_retrieve(cfg, catg, "dsn"))) {
365                 ast_copy_string((*query)->dsn, tmp, sizeof((*query)->dsn));
366         } else {
367                 return -1;
368         }
369
370         if ((tmp = ast_variable_retrieve(cfg, catg, "read"))) {
371                 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
372         }
373
374         if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) {
375                 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
376         }
377
378         /* Allow escaping of embedded commas in fields to be turned off */
379         ast_set_flag((*query), OPT_ESCAPECOMMAS);
380         if ((tmp = ast_variable_retrieve(cfg, catg, "escapecommas"))) {
381                 if (ast_false(tmp))
382                         ast_clear_flag((*query), OPT_ESCAPECOMMAS);
383         }
384
385         (*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
386         if (! (*query)->acf) {
387                 free(*query);
388                 return -1;
389         }
390
391         if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
392                 asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg);
393         } else {
394                 asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg);
395         }
396
397         if (!((*query)->acf->name)) {
398                 free((*query)->acf);
399                 free(*query);
400                 return -1;
401         }
402
403         asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name);
404
405         if (!((*query)->acf->syntax)) {
406                 free((char *)(*query)->acf->name);
407                 free((*query)->acf);
408                 free(*query);
409                 return -1;
410         }
411
412         (*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
413         if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
414                 asprintf((char **)&((*query)->acf->desc),
415                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
416                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
417                                         "${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n"
418                                         "either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
419                                         "\nRead:\n%s\n\nWrite:\n%s\n",
420                                         (*query)->sql_read,
421                                         (*query)->sql_write);
422         } else if (!ast_strlen_zero((*query)->sql_read)) {
423                 asprintf((char **)&((*query)->acf->desc),
424                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
425                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
426                                         "${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n",
427                                         (*query)->sql_read);
428         } else if (!ast_strlen_zero((*query)->sql_write)) {
429                 asprintf((char **)&((*query)->acf->desc),
430                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
431                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
432                                         "${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n"
433                                         "${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
434                                         "This function may only be set.\nSQL:\n%s\n",
435                                         (*query)->sql_write);
436         }
437
438         /* Could be out of memory, or could be we have neither sql_read nor sql_write */
439         if (! ((*query)->acf->desc)) {
440                 free((char *)(*query)->acf->syntax);
441                 free((char *)(*query)->acf->name);
442                 free((*query)->acf);
443                 free(*query);
444                 return -1;
445         }
446
447         if (ast_strlen_zero((*query)->sql_read)) {
448                 (*query)->acf->read = NULL;
449         } else {
450                 (*query)->acf->read = acf_odbc_read;
451         }
452
453         if (ast_strlen_zero((*query)->sql_write)) {
454                 (*query)->acf->write = NULL;
455         } else {
456                 (*query)->acf->write = acf_odbc_write;
457         }
458
459         return 0;
460 }
461
462 static int free_acf_query(struct acf_odbc_query *query)
463 {
464         if (query) {
465                 if (query->acf) {
466                         if (query->acf->name)
467                                 free((char *)query->acf->name);
468                         if (query->acf->syntax)
469                                 free((char *)query->acf->syntax);
470                         if (query->acf->desc)
471                                 free((char *)query->acf->desc);
472                         free(query->acf);
473                 }
474                 free(query);
475         }
476         return 0;
477 }
478
479 static int odbc_load_module(void)
480 {
481         int res = 0;
482         struct ast_config *cfg;
483         char *catg;
484
485         AST_LIST_LOCK(&queries);
486
487         cfg = ast_config_load(config);
488         if (!cfg) {
489                 ast_log(LOG_NOTICE, "Unable to load config for func_odbc: %s\n", config);
490                 AST_LIST_UNLOCK(&queries);
491                 return 0;
492         }
493
494         for (catg = ast_category_browse(cfg, NULL);
495              catg;
496              catg = ast_category_browse(cfg, catg)) {
497                 struct acf_odbc_query *query = NULL;
498
499                 if (init_acf_query(cfg, catg, &query)) {
500                         ast_log(LOG_ERROR, "Out of memory\n");
501                         free_acf_query(query);
502                 } else {
503                         AST_LIST_INSERT_HEAD(&queries, query, list);
504                         ast_custom_function_register(query->acf);
505                 }
506         }
507
508         ast_config_destroy(cfg);
509         ast_custom_function_register(&escape_function);
510
511         AST_LIST_UNLOCK(&queries);
512         return res;
513 }
514
515 static int odbc_unload_module(void)
516 {
517         struct acf_odbc_query *query;
518
519         AST_LIST_LOCK(&queries);
520         while (!AST_LIST_EMPTY(&queries)) {
521                 query = AST_LIST_REMOVE_HEAD(&queries, list);
522                 ast_custom_function_unregister(query->acf);
523                 free_acf_query(query);
524         }
525
526         ast_custom_function_unregister(&escape_function);
527
528         /* Allow any threads waiting for this lock to pass (avoids a race) */
529         AST_LIST_UNLOCK(&queries);
530         AST_LIST_LOCK(&queries);
531
532         AST_LIST_UNLOCK(&queries);
533         return 0;
534 }
535
536 static int reload(void *mod)
537 {
538         int res = 0;
539         struct ast_config *cfg;
540         struct acf_odbc_query *oldquery;
541         char *catg;
542
543         AST_LIST_LOCK(&queries);
544
545         while (!AST_LIST_EMPTY(&queries)) {
546                 oldquery = AST_LIST_REMOVE_HEAD(&queries, list);
547                 ast_custom_function_unregister(oldquery->acf);
548                 free_acf_query(oldquery);
549         }
550
551         cfg = ast_config_load(config);
552         if (!cfg) {
553                 ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
554                 goto reload_out;
555         }
556
557         for (catg = ast_category_browse(cfg, NULL);
558              catg;
559              catg = ast_category_browse(cfg, catg)) {
560                 struct acf_odbc_query *query = NULL;
561
562                 if (init_acf_query(cfg, catg, &query)) {
563                         ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
564                 } else {
565                         AST_LIST_INSERT_HEAD(&queries, query, list);
566                         ast_custom_function_register(query->acf);
567                 }
568         }
569
570         ast_config_destroy(cfg);
571 reload_out:
572         AST_LIST_UNLOCK(&queries);
573         return res;
574 }
575
576 static int unload_module(void *mod)
577 {
578         return odbc_unload_module();
579 }
580
581 static int load_module(void *mod)
582 {
583         return odbc_load_module();
584 }
585
586 static const char *description(void)
587 {
588         return tdesc;
589 }
590
591 /* XXX need to revise usecount - set if query_lock is set */
592
593 static const char *key(void)
594 {
595         return ASTERISK_GPL_KEY;
596 }
597
598 STD_MOD(MOD_1, reload, NULL, NULL);
599