f6fecea4b32d31e1b4a090483ffffcac0a55e5fb
[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 #include <errno.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/file.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/options.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/module.h"
49 #include "asterisk/config.h"
50 #include "asterisk/res_odbc.h"
51 #include "asterisk/app.h"
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 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, const char *cmd, char *s, const char *value)
97 {
98         struct odbc_obj *obj = NULL;
99         struct acf_odbc_query *query;
100         char *t, buf[2048]="", varname[15];
101         int i, dsn;
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         SQLLEN 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         /* Parse our arguments */
125         t = value ? ast_strdupa(value) : "";
126
127         if (!s || !t) {
128                 ast_log(LOG_ERROR, "Out of memory\n");
129                 AST_LIST_UNLOCK(&queries);
130                 return -1;
131         }
132
133         AST_STANDARD_APP_ARGS(args, s);
134         for (i = 0; i < args.argc; i++) {
135                 snprintf(varname, sizeof(varname), "ARG%d", i + 1);
136                 pbx_builtin_pushvar_helper(chan, varname, args.field[i]);
137         }
138
139         /* Parse values, just like arguments */
140         /* Can't use the pipe, because app Set removes them */
141         AST_NONSTANDARD_APP_ARGS(values, t, ',');
142         for (i = 0; i < values.argc; i++) {
143                 snprintf(varname, sizeof(varname), "VAL%d", i + 1);
144                 pbx_builtin_pushvar_helper(chan, varname, values.field[i]);
145         }
146
147         /* Additionally set the value as a whole (but push an empty string if value is NULL) */
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         for (dsn = 0; dsn < 5; dsn++) {
167                 if (!ast_strlen_zero(query->writehandle[dsn])) {
168                         obj = ast_odbc_request_obj(query->writehandle[dsn], 0);
169                         if (obj)
170                                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, buf);
171                 }
172                 if (stmt)
173                         break;
174         }
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                 ast_odbc_release_obj(obj);
192
193         return 0;
194 }
195
196 static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, char *buf, size_t len)
197 {
198         struct odbc_obj *obj = NULL;
199         struct acf_odbc_query *query;
200         char sql[2048] = "", varname[15], colnames[2048] = "";
201         int res, x, buflen = 0, escapecommas, dsn;
202         AST_DECLARE_APP_ARGS(args,
203                 AST_APP_ARG(field)[100];
204         );
205         SQLHSTMT stmt;
206         SQLSMALLINT colcount=0;
207         SQLLEN indicator;
208         SQLSMALLINT collength;
209
210         AST_LIST_LOCK(&queries);
211         AST_LIST_TRAVERSE(&queries, query, list) {
212                 if (!strcmp(query->acf->name, cmd)) {
213                         break;
214                 }
215         }
216
217         if (!query) {
218                 ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
219                 AST_LIST_UNLOCK(&queries);
220                 return -1;
221         }
222
223         AST_STANDARD_APP_ARGS(args, s);
224         for (x = 0; x < args.argc; x++) {
225                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
226                 pbx_builtin_pushvar_helper(chan, varname, args.field[x]);
227         }
228
229         pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1);
230
231         /* Restore prior values */
232         for (x = 0; x < args.argc; x++) {
233                 snprintf(varname, sizeof(varname), "ARG%d", x + 1);
234                 pbx_builtin_setvar_helper(chan, varname, NULL);
235         }
236
237         /* Save this flag, so we can release the lock */
238         escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS);
239
240         AST_LIST_UNLOCK(&queries);
241
242         for (dsn = 0; dsn < 5; dsn++) {
243                 if (!ast_strlen_zero(query->writehandle[dsn])) {
244                         obj = ast_odbc_request_obj(query->writehandle[dsn], 0);
245                         if (obj)
246                                 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
247                 }
248                 if (stmt)
249                         break;
250         }
251
252         if (!stmt) {
253                 ast_log(LOG_ERROR, "Unable to execute query [%s]\n", sql);
254                 if (obj)
255                         ast_odbc_release_obj(obj);
256                 return -1;
257         }
258
259         res = SQLNumResultCols(stmt, &colcount);
260         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
261                 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
262                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
263                 ast_odbc_release_obj(obj);
264                 return -1;
265         }
266
267         *buf = '\0';
268
269         res = SQLFetch(stmt);
270         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
271                 int res1 = -1;
272                 if (res == SQL_NO_DATA) {
273                         if (option_verbose > 3) {
274                                 ast_verbose(VERBOSE_PREFIX_4 "Found no rows [%s]\n", sql);
275                         }
276                         res1 = 0;
277                 } else if (option_verbose > 3) {
278                         ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql);
279                 }
280                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
281                 ast_odbc_release_obj(obj);
282                 return res1;
283         }
284
285         for (x = 0; x < colcount; x++) {
286                 int i, namelen;
287                 char coldata[256], colname[256];
288
289                 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, NULL, NULL, NULL);
290                 if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) {
291                         snprintf(colname, sizeof(colname), "field%d", x);
292                 }
293
294                 if (!ast_strlen_zero(colnames))
295                         strncat(colnames, ",", sizeof(colnames) - 1);
296                 namelen = strlen(colnames);
297
298                 /* Copy data, encoding '\' and ',' for the argument parser */
299                 for (i = 0; i < sizeof(colname); i++) {
300                         if (escapecommas && (colname[i] == '\\' || colname[i] == ',')) {
301                                 colnames[namelen++] = '\\';
302                         }
303                         colnames[namelen++] = colname[i];
304
305                         if (namelen >= sizeof(colnames) - 2) {
306                                 colnames[namelen >= sizeof(colnames) ? sizeof(colnames) - 1 : namelen] = '\0';
307                                 break;
308                         }
309
310                         if (colname[i] == '\0')
311                                 break;
312                 }
313
314                 buflen = strlen(buf);
315                 res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator);
316                 if (indicator == SQL_NULL_DATA) {
317                         coldata[0] = '\0';
318                         res = SQL_SUCCESS;
319                 }
320
321                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
322                         ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
323                         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
324                         ast_odbc_release_obj(obj);
325                         return -1;
326                 }
327
328                 /* Copy data, encoding '\' and ',' for the argument parser */
329                 for (i = 0; i < sizeof(coldata); i++) {
330                         if (escapecommas && (coldata[i] == '\\' || coldata[i] == ',')) {
331                                 buf[buflen++] = '\\';
332                         }
333                         buf[buflen++] = coldata[i];
334
335                         if (buflen >= len - 2)
336                                 break;
337
338                         if (coldata[i] == '\0')
339                                 break;
340                 }
341
342                 buf[buflen - 1] = ',';
343                 buf[buflen] = '\0';
344         }
345         /* Trim trailing comma */
346         buf[buflen - 1] = '\0';
347
348         pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames);
349
350         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
351         ast_odbc_release_obj(obj);
352         return 0;
353 }
354
355 static int acf_escape(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
356 {
357         char *out = buf;
358
359         for (; *data && out - buf < len; data++) {
360                 if (*data == '\'') {
361                         *out = '\'';
362                         out++;
363                 }
364                 *out++ = *data;
365         }
366         *out = '\0';
367
368         return 0;
369 }
370
371 static struct ast_custom_function escape_function = {
372         .name = "SQL_ESC",
373         .synopsis = "Escapes single ticks for use in SQL statements",
374         .syntax = "SQL_ESC(<string>)",
375         .desc =
376 "Used in SQL templates to escape data which may contain single ticks (') which\n"
377 "are otherwise used to delimit data.  For example:\n"
378 "SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n",
379         .read = acf_escape,
380         .write = NULL,
381 };
382
383 static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query)
384 {
385         const char *tmp;
386         int i;
387
388         if (!cfg || !catg) {
389                 return EINVAL;
390         }
391
392         *query = ast_calloc(1, sizeof(struct acf_odbc_query));
393         if (! (*query))
394                 return ENOMEM;
395
396         if (((tmp = ast_variable_retrieve(cfg, catg, "writehandle"))) || ((tmp = ast_variable_retrieve(cfg, catg, "dsn")))) {
397                 char *tmp2 = ast_strdupa(tmp);
398                 AST_DECLARE_APP_ARGS(write,
399                         AST_APP_ARG(dsn)[5];
400                 );
401                 AST_NONSTANDARD_APP_ARGS(write, tmp2, ',');
402                 for (i = 0; i < 5; i++) {
403                         if (!ast_strlen_zero(write.dsn[i]))
404                                 ast_copy_string((*query)->writehandle[i], write.dsn[i], sizeof((*query)->writehandle[i]));
405                 }
406         }
407
408         if ((tmp = ast_variable_retrieve(cfg, catg, "readhandle"))) {
409                 char *tmp2 = ast_strdupa(tmp);
410                 AST_DECLARE_APP_ARGS(read,
411                         AST_APP_ARG(dsn)[5];
412                 );
413                 AST_NONSTANDARD_APP_ARGS(read, tmp2, ',');
414                 for (i = 0; i < 5; i++) {
415                         if (!ast_strlen_zero(read.dsn[i]))
416                                 ast_copy_string((*query)->readhandle[i], read.dsn[i], sizeof((*query)->readhandle[i]));
417                 }
418         } else {
419                 /* If no separate readhandle, then use the writehandle for reading */
420                 for (i = 0; i < 5; i++) {
421                         if (!ast_strlen_zero((*query)->writehandle[i]))
422                                 ast_copy_string((*query)->readhandle[i], (*query)->writehandle[i], sizeof((*query)->readhandle[i]));
423                 }
424         }
425
426         if ((tmp = ast_variable_retrieve(cfg, catg, "read"))) {
427                 ast_log(LOG_WARNING, "Parameter 'read' is deprecated for category %s.  Please use 'readsql' instead.\n", catg);
428                 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
429         } else if ((tmp = ast_variable_retrieve(cfg, catg, "readsql")))
430                 ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read));
431
432         if (!ast_strlen_zero((*query)->sql_read) && ast_strlen_zero((*query)->readhandle[0])) {
433                 free(*query);
434                 *query = NULL;
435                 ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for reading: %s\n", catg);
436                 return EINVAL;
437         }
438
439         if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) {
440                 ast_log(LOG_WARNING, "Parameter 'write' is deprecated for category %s.  Please use 'writesql' instead.\n", catg);
441                 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
442         } else if ((tmp = ast_variable_retrieve(cfg, catg, "writesql")))
443                 ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write));
444
445         if (!ast_strlen_zero((*query)->sql_write) && ast_strlen_zero((*query)->writehandle[0])) {
446                 free(*query);
447                 *query = NULL;
448                 ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for writing: %s\n", catg);
449                 return EINVAL;
450         }
451
452         /* Allow escaping of embedded commas in fields to be turned off */
453         ast_set_flag((*query), OPT_ESCAPECOMMAS);
454         if ((tmp = ast_variable_retrieve(cfg, catg, "escapecommas"))) {
455                 if (ast_false(tmp))
456                         ast_clear_flag((*query), OPT_ESCAPECOMMAS);
457         }
458
459         (*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function));
460         if (! (*query)->acf) {
461                 free(*query);
462                 *query = NULL;
463                 return ENOMEM;
464         }
465
466         if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
467                 asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg);
468         } else {
469                 asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg);
470         }
471
472         if (!((*query)->acf->name)) {
473                 free((*query)->acf);
474                 free(*query);
475                 *query = NULL;
476                 return ENOMEM;
477         }
478
479         asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name);
480
481         if (!((*query)->acf->syntax)) {
482                 free((char *)(*query)->acf->name);
483                 free((*query)->acf);
484                 free(*query);
485                 *query = NULL;
486                 return ENOMEM;
487         }
488
489         (*query)->acf->synopsis = "Runs the referenced query with the specified arguments";
490         if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) {
491                 asprintf((char **)&((*query)->acf->desc),
492                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
493                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
494                                         "${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n"
495                                         "either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
496                                         "\nRead:\n%s\n\nWrite:\n%s\n",
497                                         (*query)->sql_read,
498                                         (*query)->sql_write);
499         } else if (!ast_strlen_zero((*query)->sql_read)) {
500                 asprintf((char **)&((*query)->acf->desc),
501                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
502                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
503                                         "${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n",
504                                         (*query)->sql_read);
505         } else if (!ast_strlen_zero((*query)->sql_write)) {
506                 asprintf((char **)&((*query)->acf->desc),
507                                         "Runs the following query, as defined in func_odbc.conf, performing\n"
508                                         "substitution of the arguments into the query as specified by ${ARG1},\n"
509                                         "${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n"
510                                         "${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n"
511                                         "This function may only be set.\nSQL:\n%s\n",
512                                         (*query)->sql_write);
513         } else {
514                 free((char *)(*query)->acf->syntax);
515                 free((char *)(*query)->acf->name);
516                 free((*query)->acf);
517                 free(*query);
518                 ast_log(LOG_WARNING, "Section %s was found, but there was no SQL to execute.  Ignoring.\n", catg);
519                 return EINVAL;
520         }
521
522         if (! ((*query)->acf->desc)) {
523                 free((char *)(*query)->acf->syntax);
524                 free((char *)(*query)->acf->name);
525                 free((*query)->acf);
526                 free(*query);
527                 *query = NULL;
528                 return ENOMEM;
529         }
530
531         if (ast_strlen_zero((*query)->sql_read)) {
532                 (*query)->acf->read = NULL;
533         } else {
534                 (*query)->acf->read = acf_odbc_read;
535         }
536
537         if (ast_strlen_zero((*query)->sql_write)) {
538                 (*query)->acf->write = NULL;
539         } else {
540                 (*query)->acf->write = acf_odbc_write;
541         }
542
543         return 0;
544 }
545
546 static int free_acf_query(struct acf_odbc_query *query)
547 {
548         if (query) {
549                 if (query->acf) {
550                         if (query->acf->name)
551                                 free((char *)query->acf->name);
552                         if (query->acf->syntax)
553                                 free((char *)query->acf->syntax);
554                         if (query->acf->desc)
555                                 free((char *)query->acf->desc);
556                         free(query->acf);
557                 }
558                 free(query);
559         }
560         return 0;
561 }
562
563 static int load_module(void)
564 {
565         int res = 0;
566         struct ast_config *cfg;
567         char *catg;
568
569         AST_LIST_LOCK(&queries);
570
571         cfg = ast_config_load(config);
572         if (!cfg) {
573                 ast_log(LOG_NOTICE, "Unable to load config for func_odbc: %s\n", config);
574                 AST_LIST_UNLOCK(&queries);
575                 return AST_MODULE_LOAD_DECLINE;
576         }
577
578         for (catg = ast_category_browse(cfg, NULL);
579              catg;
580              catg = ast_category_browse(cfg, catg)) {
581                 struct acf_odbc_query *query = NULL;
582                 int err;
583
584                 if ((err = init_acf_query(cfg, catg, &query))) {
585                         if (err == ENOMEM)
586                                 ast_log(LOG_ERROR, "Out of memory\n");
587                         else if (err == EINVAL)
588                                 ast_log(LOG_ERROR, "Invalid parameters for category %s\n", catg);
589                         else
590                                 ast_log(LOG_ERROR, "%s (%d)\n", strerror(err), err);
591                 } else {
592                         AST_LIST_INSERT_HEAD(&queries, query, list);
593                         ast_custom_function_register(query->acf);
594                 }
595         }
596
597         ast_config_destroy(cfg);
598         res |= ast_custom_function_register(&escape_function);
599
600         AST_LIST_UNLOCK(&queries);
601         return res;
602 }
603
604 static int unload_module(void)
605 {
606         struct acf_odbc_query *query;
607         int res = 0;
608
609         AST_LIST_LOCK(&queries);
610         while (!AST_LIST_EMPTY(&queries)) {
611                 query = AST_LIST_REMOVE_HEAD(&queries, list);
612                 ast_custom_function_unregister(query->acf);
613                 free_acf_query(query);
614         }
615
616         res |= ast_custom_function_unregister(&escape_function);
617
618         /* Allow any threads waiting for this lock to pass (avoids a race) */
619         AST_LIST_UNLOCK(&queries);
620         usleep(1);
621         AST_LIST_LOCK(&queries);
622
623         AST_LIST_UNLOCK(&queries);
624         return 0;
625 }
626
627 static int reload(void)
628 {
629         int res = 0;
630         struct ast_config *cfg;
631         struct acf_odbc_query *oldquery;
632         char *catg;
633
634         AST_LIST_LOCK(&queries);
635
636         while (!AST_LIST_EMPTY(&queries)) {
637                 oldquery = AST_LIST_REMOVE_HEAD(&queries, list);
638                 ast_custom_function_unregister(oldquery->acf);
639                 free_acf_query(oldquery);
640         }
641
642         cfg = ast_config_load(config);
643         if (!cfg) {
644                 ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config);
645                 goto reload_out;
646         }
647
648         for (catg = ast_category_browse(cfg, NULL);
649              catg;
650              catg = ast_category_browse(cfg, catg)) {
651                 struct acf_odbc_query *query = NULL;
652
653                 if (init_acf_query(cfg, catg, &query)) {
654                         ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg);
655                 } else {
656                         AST_LIST_INSERT_HEAD(&queries, query, list);
657                         ast_custom_function_register(query->acf);
658                 }
659         }
660
661         ast_config_destroy(cfg);
662 reload_out:
663         AST_LIST_UNLOCK(&queries);
664         return res;
665 }
666
667 /* XXX need to revise usecount - set if query_lock is set */
668
669 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC lookups",
670                 .load = load_module,
671                 .unload = unload_module,
672                 .reload = reload,
673                );
674