include "logger.h" and errno.h from asterisk.h - usage shows that they
[asterisk/asterisk.git] / res / res_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * res_odbc.c <ODBC resource manager>
9  * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief ODBC resource manager
25  * 
26  * \author Mark Spencer <markster@digium.com>
27  * \author Anthony Minessale II <anthmct@yahoo.com>
28  *
29  * \arg See also: \ref cdr_odbc
30  */
31
32 /*** MODULEINFO
33         <depend>unixodbc</depend>
34         <depend>ltdl</depend>
35  ***/
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include "asterisk/file.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/config.h"
44 #include "asterisk/options.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/cli.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/res_odbc.h"
50
51 struct odbc_class
52 {
53         AST_LIST_ENTRY(odbc_class) list;
54         char name[80];
55         char dsn[80];
56         char username[80];
57         char password[80];
58         char sanitysql[256];
59         SQLHENV env;
60         unsigned int haspool:1;         /* Boolean - TDS databases need this */
61         unsigned int limit:10;          /* Gives a limit of 1023 maximum */
62         unsigned int count:10;          /* Running count of pooled connections */
63         unsigned int delme:1;                   /* Purge the class */
64         AST_LIST_HEAD(, odbc_obj) odbc_obj;
65 };
66
67 AST_LIST_HEAD_STATIC(odbc_list, odbc_class);
68
69 static odbc_status odbc_obj_connect(struct odbc_obj *obj);
70 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
71 static int odbc_register_class(struct odbc_class *class, int connect);
72
73
74 SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data)
75 {
76         int attempt;
77         SQLHSTMT stmt;
78
79         for (attempt = 0; attempt < 2; attempt++) {
80                 stmt = exec_cb(obj, data);
81
82                 if (stmt) {
83                         break;
84                 } else {
85                         obj->up = 0;
86                         ast_log(LOG_WARNING, "SQL Exec Direct failed.  Attempting a reconnect...\n");
87
88                         odbc_obj_disconnect(obj);
89                         odbc_obj_connect(obj);
90                 }
91         }
92
93         return stmt;
94 }
95
96 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
97 {
98         int res = 0, i, attempt;
99         SQLINTEGER nativeerror=0, numfields=0;
100         SQLSMALLINT diagbytes=0;
101         unsigned char state[10], diagnostic[256];
102         SQLHSTMT stmt;
103
104         for (attempt = 0; attempt < 2; attempt++) {
105                 /* This prepare callback may do more than just prepare -- it may also
106                  * bind parameters, bind results, etc.  The real key, here, is that
107                  * when we disconnect, all handles become invalid for most databases.
108                  * We must therefore redo everything when we establish a new
109                  * connection. */
110                 stmt = prepare_cb(obj, data);
111
112                 if (stmt) {
113                         res = SQLExecute(stmt);
114                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
115                                 if (res == SQL_ERROR) {
116                                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
117                                         for (i = 0; i < numfields; i++) {
118                                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
119                                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
120                                                 if (i > 10) {
121                                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
122                                                         break;
123                                                 }
124                                         }
125                                 }
126
127                                 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
128                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
129                                 stmt = NULL;
130
131                                 obj->up = 0;
132                                 /*
133                                  * While this isn't the best way to try to correct an error, this won't automatically
134                                  * fail when the statement handle invalidates.
135                                  */
136                                 ast_odbc_sanity_check(obj);
137                                 continue;
138                         }
139                         break;
140                 } else if (attempt == 0)
141                         ast_odbc_sanity_check(obj);
142         }
143
144         return stmt;
145 }
146
147 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) 
148 {
149         int res = 0, i;
150         SQLINTEGER nativeerror=0, numfields=0;
151         SQLSMALLINT diagbytes=0;
152         unsigned char state[10], diagnostic[256];
153
154         res = SQLExecute(stmt);
155         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
156                 if (res == SQL_ERROR) {
157                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
158                         for (i = 0; i < numfields; i++) {
159                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
160                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
161                                 if (i > 10) {
162                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
163                                         break;
164                                 }
165                         }
166                 }
167 #if 0
168                 /* This is a really bad method of trying to correct a dead connection.  It
169                  * only ever really worked with MySQL.  It will not work with any other
170                  * database, since most databases prepare their statements on the server,
171                  * and if you disconnect, you invalidate the statement handle.  Hence, if
172                  * you disconnect, you're going to fail anyway, whether you try to execute
173                  * a second time or not.
174                  */
175                 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
176                 ast_mutex_lock(&obj->lock);
177                 obj->up = 0;
178                 ast_mutex_unlock(&obj->lock);
179                 odbc_obj_disconnect(obj);
180                 odbc_obj_connect(obj);
181                 res = SQLExecute(stmt);
182 #endif
183         }
184         
185         return res;
186 }
187
188
189 int ast_odbc_sanity_check(struct odbc_obj *obj) 
190 {
191         char *test_sql = "select 1";
192         SQLHSTMT stmt;
193         int res = 0;
194
195         if (!ast_strlen_zero(obj->parent->sanitysql))
196                 test_sql = obj->parent->sanitysql;
197
198         if (obj->up) {
199                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
200                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
201                         obj->up = 0;
202                 } else {
203                         res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
204                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
205                                 obj->up = 0;
206                         } else {
207                                 res = SQLExecute(stmt);
208                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
209                                         obj->up = 0;
210                                 }
211                         }
212                 }
213                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
214         }
215
216         if (!obj->up) { /* Try to reconnect! */
217                 ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
218                 odbc_obj_disconnect(obj);
219                 odbc_obj_connect(obj);
220         }
221         return obj->up;
222 }
223
224 static int load_odbc_config(void)
225 {
226         static char *cfg = "res_odbc.conf";
227         struct ast_config *config;
228         struct ast_variable *v;
229         char *cat;
230         const char *dsn, *username, *password, *sanitysql;
231         int enabled, pooling, limit;
232         int connect = 0, res = 0;
233         struct ast_flags config_flags = { 0 };
234
235         struct odbc_class *new;
236
237         config = ast_config_load(cfg, config_flags);
238         if (!config) {
239                 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
240                 return -1;
241         }
242         for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
243                 if (!strcasecmp(cat, "ENV")) {
244                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
245                                 setenv(v->name, v->value, 1);
246                                 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
247                         }
248                 } else {
249                         /* Reset all to defaults for each class of odbc connections */
250                         dsn = username = password = sanitysql = NULL;
251                         enabled = 1;
252                         connect = 0;
253                         pooling = 0;
254                         limit = 0;
255                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
256                                 if (!strcasecmp(v->name, "pooling")) {
257                                         if (ast_true(v->value))
258                                                 pooling = 1;
259                                 } else if (!strcasecmp(v->name, "limit")) {
260                                         sscanf(v->value, "%d", &limit);
261                                         if (ast_true(v->value) && !limit) {
262                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
263                                                 limit = 1023;
264                                         } else if (ast_false(v->value)) {
265                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
266                                                 enabled = 0;
267                                                 break;
268                                         }
269                                 } else if (!strcasecmp(v->name, "enabled")) {
270                                         enabled = ast_true(v->value);
271                                 } else if (!strcasecmp(v->name, "pre-connect")) {
272                                         connect = ast_true(v->value);
273                                 } else if (!strcasecmp(v->name, "dsn")) {
274                                         dsn = v->value;
275                                 } else if (!strcasecmp(v->name, "username")) {
276                                         username = v->value;
277                                 } else if (!strcasecmp(v->name, "password")) {
278                                         password = v->value;
279                                 } else if (!strcasecmp(v->name, "sanitysql")) {
280                                         sanitysql = v->value;
281                                 }
282                         }
283
284                         if (enabled && !ast_strlen_zero(dsn)) {
285                                 new = ast_calloc(1, sizeof(*new));
286
287                                 if (!new) {
288                                         res = -1;
289                                         break;
290                                 }
291
292                                 if (cat)
293                                         ast_copy_string(new->name, cat, sizeof(new->name));
294                                 if (dsn)
295                                         ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
296                                 if (username)
297                                         ast_copy_string(new->username, username, sizeof(new->username));
298                                 if (password)
299                                         ast_copy_string(new->password, password, sizeof(new->password));
300                                 if (sanitysql)
301                                         ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
302
303                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
304                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
305
306                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
307                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
308                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
309                                         return res;
310                                 }
311
312                                 if (pooling) {
313                                         new->haspool = pooling;
314                                         if (limit) {
315                                                 new->limit = limit;
316                                         } else {
317                                                 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
318                                                 new->limit = 5;
319                                         }
320                                 }
321
322                                 odbc_register_class(new, connect);
323                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
324                         }
325                 }
326         }
327         ast_config_destroy(config);
328         return res;
329 }
330
331 static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
332 {
333         struct odbc_class *class;
334         struct odbc_obj *current;
335         int length = 0;
336         int which = 0;
337         char *ret = NULL;
338
339         switch (cmd) {
340         case CLI_INIT:
341                 e->command = "odbc show";
342                 e->usage =
343                                 "Usage: odbc show [class]\n"
344                                 "       List settings of a particular ODBC class or,\n"
345                                 "       if not specified, all classes.\n";
346                 return NULL;
347         case CLI_GENERATE:
348                 if (a->pos != 2)
349                         return NULL;
350                 length = strlen(a->word);
351                 AST_LIST_LOCK(&odbc_list);
352                 AST_LIST_TRAVERSE(&odbc_list, class, list) {
353                         if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
354                                 ret = ast_strdup(class->name);
355                                 break;
356                         }
357                 }
358                 if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
359                         ret = ast_strdup("all");
360                 }
361                 AST_LIST_UNLOCK(&odbc_list);
362                 return ret;
363         }
364
365         ast_cli(a->fd, "\nODBC DSN Settings\n");
366         ast_cli(a->fd, "-----------------\n\n");
367         AST_LIST_LOCK(&odbc_list);
368         AST_LIST_TRAVERSE(&odbc_list, class, list) {
369                 if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) {
370                         int count = 0;
371                         ast_cli(a->fd, "  Name:   %s\n  DSN:    %s\n", class->name, class->dsn);
372
373                         if (class->haspool) {
374                                 ast_cli(a->fd, "  Pooled: Yes\n  Limit:  %d\n  Connections in use: %d\n", class->limit, class->count);
375
376                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
377                                         ast_cli(a->fd, "    - Connection %d: %s\n", ++count, current->used ? "in use" : current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
378                                 }
379                         } else {
380                                 /* Should only ever be one of these */
381                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
382                                         ast_cli(a->fd, "  Pooled: No\n  Connected: %s\n", current->up && ast_odbc_sanity_check(current) ? "Yes" : "No");
383                                 }
384                         }
385                         ast_cli(a->fd, "\n");
386                 }
387         }
388         AST_LIST_UNLOCK(&odbc_list);
389
390         return CLI_SUCCESS;
391 }
392
393 static struct ast_cli_entry cli_odbc[] = {
394         AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)")
395 };
396
397 static int odbc_register_class(struct odbc_class *class, int connect)
398 {
399         struct odbc_obj *obj;
400         if (class) {
401                 AST_LIST_LOCK(&odbc_list);
402                 AST_LIST_INSERT_HEAD(&odbc_list, class, list);
403                 AST_LIST_UNLOCK(&odbc_list);
404
405                 if (connect) {
406                         /* Request and release builds a connection */
407                         obj = ast_odbc_request_obj(class->name, 0);
408                         if (obj)
409                                 ast_odbc_release_obj(obj);
410                 }
411
412                 return 0;
413         } else {
414                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
415                 return -1;
416         }
417 }
418
419 void ast_odbc_release_obj(struct odbc_obj *obj)
420 {
421         /* For pooled connections, this frees the connection to be
422          * reused.  For non-pooled connections, it does nothing. */
423         obj->used = 0;
424 }
425
426 struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
427 {
428         struct odbc_obj *obj = NULL;
429         struct odbc_class *class;
430
431         AST_LIST_LOCK(&odbc_list);
432         AST_LIST_TRAVERSE(&odbc_list, class, list) {
433                 if (!strcmp(class->name, name))
434                         break;
435         }
436         AST_LIST_UNLOCK(&odbc_list);
437
438         if (!class)
439                 return NULL;
440
441         AST_LIST_LOCK(&class->odbc_obj);
442         if (class->haspool) {
443                 /* Recycle connections before building another */
444                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
445                         if (! obj->used) {
446                                 obj->used = 1;
447                                 break;
448                         }
449                 }
450
451                 if (!obj && (class->count < class->limit)) {
452                         class->count++;
453                         obj = ast_calloc(1, sizeof(*obj));
454                         if (!obj) {
455                                 AST_LIST_UNLOCK(&class->odbc_obj);
456                                 return NULL;
457                         }
458                         ast_mutex_init(&obj->lock);
459                         obj->parent = class;
460                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
461                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
462                                 ast_mutex_destroy(&obj->lock);
463                                 free(obj);
464                                 obj = NULL;
465                                 class->count--;
466                         } else {
467                                 obj->used = 1;
468                                 AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
469                         }
470                 }
471         } else {
472                 /* Non-pooled connection: multiple modules can use the same connection. */
473                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
474                         /* Non-pooled connection: if there is an entry, return it */
475                         break;
476                 }
477
478                 if (!obj) {
479                         /* No entry: build one */
480                         obj = ast_calloc(1, sizeof(*obj));
481                         if (!obj) {
482                                 AST_LIST_UNLOCK(&class->odbc_obj);
483                                 return NULL;
484                         }
485                         ast_mutex_init(&obj->lock);
486                         obj->parent = class;
487                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
488                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
489                                 ast_mutex_destroy(&obj->lock);
490                                 ast_free(obj);
491                                 obj = NULL;
492                         } else {
493                                 AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list);
494                         }
495                 }
496         }
497         AST_LIST_UNLOCK(&class->odbc_obj);
498
499         if (obj && check) {
500                 ast_odbc_sanity_check(obj);
501         }
502         return obj;
503 }
504
505 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
506 {
507         int res;
508         ast_mutex_lock(&obj->lock);
509
510         res = SQLDisconnect(obj->con);
511
512         if (res == ODBC_SUCCESS) {
513                 ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
514         } else {
515                 ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
516                 obj->parent->name, obj->parent->dsn);
517         }
518         obj->up = 0;
519         ast_mutex_unlock(&obj->lock);
520         return ODBC_SUCCESS;
521 }
522
523 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
524 {
525         int res;
526         SQLINTEGER err;
527         short int mlen;
528         unsigned char msg[200], stat[10];
529 #ifdef NEEDTRACE
530         SQLINTEGER enable = 1;
531         char *tracefile = "/tmp/odbc.trace";
532 #endif
533         ast_mutex_lock(&obj->lock);
534
535         res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
536
537         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
538                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
539                 ast_mutex_unlock(&obj->lock);
540                 return ODBC_FAIL;
541         }
542         SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
543 #ifdef NEEDTRACE
544         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
545         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
546 #endif
547
548         if (obj->up) {
549                 odbc_obj_disconnect(obj);
550                 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
551         } else {
552                 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
553         }
554
555         res = SQLConnect(obj->con,
556                    (SQLCHAR *) obj->parent->dsn, SQL_NTS,
557                    (SQLCHAR *) obj->parent->username, SQL_NTS,
558                    (SQLCHAR *) obj->parent->password, SQL_NTS);
559
560         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
561                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
562                 ast_mutex_unlock(&obj->lock);
563                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
564                 return ODBC_FAIL;
565         } else {
566                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
567                 obj->up = 1;
568         }
569
570         ast_mutex_unlock(&obj->lock);
571         return ODBC_SUCCESS;
572 }
573
574 static int reload(void)
575 {
576         static char *cfg = "res_odbc.conf";
577         struct ast_config *config;
578         struct ast_variable *v;
579         char *cat;
580         const char *dsn, *username, *password, *sanitysql;
581         int enabled, pooling, limit;
582         int connect = 0, res = 0;
583         struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
584
585         struct odbc_class *new, *class;
586         struct odbc_obj *current;
587
588         /* First, mark all to be purged */
589         AST_LIST_LOCK(&odbc_list);
590         AST_LIST_TRAVERSE(&odbc_list, class, list) {
591                 class->delme = 1;
592         }
593
594         config = ast_config_load(cfg, config_flags);
595         if (config != NULL && config != CONFIG_STATUS_FILEUNCHANGED) {
596                 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
597                         if (!strcasecmp(cat, "ENV")) {
598                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
599                                         setenv(v->name, v->value, 1);
600                                         ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
601                                 }
602                         } else {
603                                 /* Reset all to defaults for each class of odbc connections */
604                                 dsn = username = password = sanitysql = NULL;
605                                 enabled = 1;
606                                 connect = 0;
607                                 pooling = 0;
608                                 limit = 0;
609                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
610                                         if (!strcasecmp(v->name, "pooling")) {
611                                                 pooling = 1;
612                                         } else if (!strcasecmp(v->name, "limit")) {
613                                                 sscanf(v->value, "%d", &limit);
614                                                 if (ast_true(v->value) && !limit) {
615                                                         ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
616                                                         limit = 1023;
617                                                 } else if (ast_false(v->value)) {
618                                                         ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
619                                                         enabled = 0;
620                                                         break;
621                                                 }
622                                         } else if (!strcasecmp(v->name, "enabled")) {
623                                                 enabled = ast_true(v->value);
624                                         } else if (!strcasecmp(v->name, "pre-connect")) {
625                                                 connect = ast_true(v->value);
626                                         } else if (!strcasecmp(v->name, "dsn")) {
627                                                 dsn = v->value;
628                                         } else if (!strcasecmp(v->name, "username")) {
629                                                 username = v->value;
630                                         } else if (!strcasecmp(v->name, "password")) {
631                                                 password = v->value;
632                                         } else if (!strcasecmp(v->name, "sanitysql")) {
633                                                 sanitysql = v->value;
634                                         }
635                                 }
636
637                                 if (enabled && !ast_strlen_zero(dsn)) {
638                                         /* First, check the list to see if it already exists */
639                                         AST_LIST_TRAVERSE(&odbc_list, class, list) {
640                                                 if (!strcmp(class->name, cat)) {
641                                                         class->delme = 0;
642                                                         break;
643                                                 }
644                                         }
645
646                                         if (class) {
647                                                 new = class;
648                                         } else {
649                                                 new = ast_calloc(1, sizeof(*new));
650                                         }
651
652                                         if (!new) {
653                                                 res = -1;
654                                                 break;
655                                         }
656
657                                         if (cat)
658                                                 ast_copy_string(new->name, cat, sizeof(new->name));
659                                         if (dsn)
660                                                 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
661                                         if (username)
662                                                 ast_copy_string(new->username, username, sizeof(new->username));
663                                         if (password)
664                                                 ast_copy_string(new->password, password, sizeof(new->password));
665                                         if (sanitysql)
666                                                 ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
667
668                                         if (!class) {
669                                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
670                                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
671
672                                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
673                                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
674                                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
675                                                         AST_LIST_UNLOCK(&odbc_list);
676                                                         return res;
677                                                 }
678                                         }
679
680                                         if (pooling) {
681                                                 new->haspool = pooling;
682                                                 if (limit) {
683                                                         new->limit = limit;
684                                                 } else {
685                                                         ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
686                                                         new->limit = 5;
687                                                 }
688                                         }
689
690                                         if (class) {
691                                                 ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
692                                         } else {
693                                                 odbc_register_class(new, connect);
694                                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
695                                         }
696                                 }
697                         }
698                 }
699                 ast_config_destroy(config);
700         }
701
702         /* Purge classes that we know can go away (pooled with 0, only) */
703         AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
704                 if (class->delme && class->haspool && class->count == 0) {
705                         while ((current = AST_LIST_REMOVE_HEAD(&class->odbc_obj, list))) {
706                                 odbc_obj_disconnect(current);
707                                 ast_mutex_destroy(&current->lock);
708                                 ast_free(current);
709                         }
710
711                         AST_LIST_REMOVE_CURRENT(list);
712                         ast_free(class);
713                 }
714         }
715         AST_LIST_TRAVERSE_SAFE_END;
716         AST_LIST_UNLOCK(&odbc_list);
717
718         return 0;
719 }
720
721 static int unload_module(void)
722 {
723         /* Prohibit unloading */
724         return -1;
725 }
726
727 static int load_module(void)
728 {
729         if(load_odbc_config() == -1)
730                 return AST_MODULE_LOAD_DECLINE;
731         ast_cli_register_multiple(cli_odbc, sizeof(cli_odbc) / sizeof(struct ast_cli_entry));
732         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
733         return 0;
734 }
735
736 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ODBC resource",
737                 .load = load_module,
738                 .unload = unload_module,
739                 .reload = reload,
740                );