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