(closes issue #10684)
[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 (!ast_strlen_zero(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         struct ast_flags config_flags = { 0 };
217
218         struct odbc_class *new;
219
220         config = ast_config_load(cfg, config_flags);
221         if (!config) {
222                 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
223                 return -1;
224         }
225         for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
226                 if (!strcasecmp(cat, "ENV")) {
227                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
228                                 setenv(v->name, v->value, 1);
229                                 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
230                         }
231                 } else {
232                         /* Reset all to defaults for each class of odbc connections */
233                         dsn = username = password = sanitysql = NULL;
234                         enabled = 1;
235                         connect = 0;
236                         pooling = 0;
237                         limit = 0;
238                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
239                                 if (!strcasecmp(v->name, "pooling")) {
240                                         if (ast_true(v->value))
241                                                 pooling = 1;
242                                 } else if (!strcasecmp(v->name, "limit")) {
243                                         sscanf(v->value, "%d", &limit);
244                                         if (ast_true(v->value) && !limit) {
245                                                 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);
246                                                 limit = 1023;
247                                         } else if (ast_false(v->value)) {
248                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
249                                                 enabled = 0;
250                                                 break;
251                                         }
252                                 } else if (!strcasecmp(v->name, "enabled")) {
253                                         enabled = ast_true(v->value);
254                                 } else if (!strcasecmp(v->name, "pre-connect")) {
255                                         connect = ast_true(v->value);
256                                 } else if (!strcasecmp(v->name, "dsn")) {
257                                         dsn = v->value;
258                                 } else if (!strcasecmp(v->name, "username")) {
259                                         username = v->value;
260                                 } else if (!strcasecmp(v->name, "password")) {
261                                         password = v->value;
262                                 } else if (!strcasecmp(v->name, "sanitysql")) {
263                                         sanitysql = v->value;
264                                 }
265                         }
266
267                         if (enabled && !ast_strlen_zero(dsn)) {
268                                 new = ast_calloc(1, sizeof(*new));
269
270                                 if (!new) {
271                                         res = -1;
272                                         break;
273                                 }
274
275                                 if (cat)
276                                         ast_copy_string(new->name, cat, sizeof(new->name));
277                                 if (dsn)
278                                         ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
279                                 if (username)
280                                         ast_copy_string(new->username, username, sizeof(new->username));
281                                 if (password)
282                                         ast_copy_string(new->password, password, sizeof(new->password));
283                                 if (sanitysql)
284                                         ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
285
286                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
287                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
288
289                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
290                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
291                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
292                                         return res;
293                                 }
294
295                                 if (pooling) {
296                                         new->haspool = pooling;
297                                         if (limit) {
298                                                 new->limit = limit;
299                                         } else {
300                                                 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
301                                                 new->limit = 5;
302                                         }
303                                 }
304
305                                 odbc_register_class(new, connect);
306                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
307                         }
308                 }
309         }
310         ast_config_destroy(config);
311         return res;
312 }
313
314 static int odbc_show_command(int fd, int argc, char **argv)
315 {
316         struct odbc_class *class;
317         struct odbc_obj *current;
318
319         AST_LIST_LOCK(&odbc_list);
320         AST_LIST_TRAVERSE(&odbc_list, class, list) {
321                 if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) {
322                         int count = 0;
323                         ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
324
325                         if (class->haspool) {
326                                 ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections in use: %d\n", class->limit, class->count);
327
328                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
329                                         ast_cli(fd, "  Connection %d: %s\n", ++count, current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
330                                 }
331                         } else {
332                                 /* Should only ever be one of these */
333                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
334                                         ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && ast_odbc_sanity_check(current) ? "yes" : "no");
335                                 }
336                         }
337
338                                 ast_cli(fd, "\n");
339                 }
340         }
341         AST_LIST_UNLOCK(&odbc_list);
342
343         return 0;
344 }
345
346 static const char show_usage[] =
347 "Usage: odbc show [<class>]\n"
348 "       List settings of a particular ODBC class.\n"
349 "       or, if not specified, all classes.\n";
350
351 static struct ast_cli_entry cli_odbc[] = {
352         { { "odbc", "show", NULL },
353         odbc_show_command, "List ODBC DSN(s)",
354         show_usage },
355 };
356
357 static int odbc_register_class(struct odbc_class *class, int connect)
358 {
359         struct odbc_obj *obj;
360         if (class) {
361                 AST_LIST_LOCK(&odbc_list);
362                 AST_LIST_INSERT_HEAD(&odbc_list, class, list);
363                 AST_LIST_UNLOCK(&odbc_list);
364
365                 if (connect) {
366                         /* Request and release builds a connection */
367                         obj = ast_odbc_request_obj(class->name, 0);
368                         if (obj)
369                                 ast_odbc_release_obj(obj);
370                 }
371
372                 return 0;
373         } else {
374                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
375                 return -1;
376         }
377 }
378
379 void ast_odbc_release_obj(struct odbc_obj *obj)
380 {
381         /* For pooled connections, this frees the connection to be
382          * reused.  For non-pooled connections, it does nothing. */
383         obj->used = 0;
384 }
385
386 struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
387 {
388         struct odbc_obj *obj = NULL;
389         struct odbc_class *class;
390
391         AST_LIST_LOCK(&odbc_list);
392         AST_LIST_TRAVERSE(&odbc_list, class, list) {
393                 if (!strcmp(class->name, name))
394                         break;
395         }
396         AST_LIST_UNLOCK(&odbc_list);
397
398         if (!class)
399                 return NULL;
400
401         AST_LIST_LOCK(&class->odbc_obj);
402         if (class->haspool) {
403                 /* Recycle connections before building another */
404                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
405                         if (! obj->used) {
406                                 obj->used = 1;
407                                 break;
408                         }
409                 }
410
411                 if (!obj && (class->count < class->limit)) {
412                         class->count++;
413                         obj = ast_calloc(1, sizeof(*obj));
414                         if (!obj) {
415                                 AST_LIST_UNLOCK(&class->odbc_obj);
416                                 return NULL;
417                         }
418                         ast_mutex_init(&obj->lock);
419                         obj->parent = class;
420                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
421                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
422                                 ast_mutex_destroy(&obj->lock);
423                                 free(obj);
424                                 obj = NULL;
425                                 class->count--;
426                         } else {
427                                 obj->used = 1;
428                                 AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
429                         }
430                 }
431         } else {
432                 /* Non-pooled connection: multiple modules can use the same connection. */
433                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
434                         /* Non-pooled connection: if there is an entry, return it */
435                         break;
436                 }
437
438                 if (!obj) {
439                         /* No entry: build one */
440                         obj = ast_calloc(1, sizeof(*obj));
441                         if (!obj) {
442                                 AST_LIST_UNLOCK(&class->odbc_obj);
443                                 return NULL;
444                         }
445                         ast_mutex_init(&obj->lock);
446                         obj->parent = class;
447                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
448                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
449                                 ast_mutex_destroy(&obj->lock);
450                                 ast_free(obj);
451                                 obj = NULL;
452                         } else {
453                                 AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list);
454                         }
455                 }
456         }
457         AST_LIST_UNLOCK(&class->odbc_obj);
458
459         if (obj && check) {
460                 ast_odbc_sanity_check(obj);
461         }
462         return obj;
463 }
464
465 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
466 {
467         int res;
468         ast_mutex_lock(&obj->lock);
469
470         res = SQLDisconnect(obj->con);
471
472         if (res == ODBC_SUCCESS) {
473                 ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
474         } else {
475                 ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
476                 obj->parent->name, obj->parent->dsn);
477         }
478         obj->up = 0;
479         ast_mutex_unlock(&obj->lock);
480         return ODBC_SUCCESS;
481 }
482
483 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
484 {
485         int res;
486         SQLINTEGER err;
487         short int mlen;
488         unsigned char msg[200], stat[10];
489 #ifdef NEEDTRACE
490         SQLINTEGER enable = 1;
491         char *tracefile = "/tmp/odbc.trace";
492 #endif
493         ast_mutex_lock(&obj->lock);
494
495         res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
496
497         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
498                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
499                 ast_mutex_unlock(&obj->lock);
500                 return ODBC_FAIL;
501         }
502         SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
503 #ifdef NEEDTRACE
504         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
505         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
506 #endif
507
508         if (obj->up) {
509                 odbc_obj_disconnect(obj);
510                 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
511         } else {
512                 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
513         }
514
515         res = SQLConnect(obj->con,
516                    (SQLCHAR *) obj->parent->dsn, SQL_NTS,
517                    (SQLCHAR *) obj->parent->username, SQL_NTS,
518                    (SQLCHAR *) obj->parent->password, SQL_NTS);
519
520         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
521                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
522                 ast_mutex_unlock(&obj->lock);
523                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
524                 return ODBC_FAIL;
525         } else {
526                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
527                 obj->up = 1;
528         }
529
530         ast_mutex_unlock(&obj->lock);
531         return ODBC_SUCCESS;
532 }
533
534 static int reload(void)
535 {
536         static char *cfg = "res_odbc.conf";
537         struct ast_config *config;
538         struct ast_variable *v;
539         char *cat, *dsn, *username, *password, *sanitysql;
540         int enabled, pooling, limit;
541         int connect = 0, res = 0;
542         struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
543
544         struct odbc_class *new, *class;
545         struct odbc_obj *current;
546
547         /* First, mark all to be purged */
548         AST_LIST_LOCK(&odbc_list);
549         AST_LIST_TRAVERSE(&odbc_list, class, list) {
550                 class->delme = 1;
551         }
552
553         config = ast_config_load(cfg, config_flags);
554         if (config != NULL && config != CONFIG_STATUS_FILEUNCHANGED) {
555                 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
556                         if (!strcasecmp(cat, "ENV")) {
557                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
558                                         setenv(v->name, v->value, 1);
559                                         ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
560                                 }
561                         } else {
562                                 /* Reset all to defaults for each class of odbc connections */
563                                 dsn = username = password = sanitysql = NULL;
564                                 enabled = 1;
565                                 connect = 0;
566                                 pooling = 0;
567                                 limit = 0;
568                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
569                                         if (!strcasecmp(v->name, "pooling")) {
570                                                 pooling = 1;
571                                         } else if (!strcasecmp(v->name, "limit")) {
572                                                 sscanf(v->value, "%d", &limit);
573                                                 if (ast_true(v->value) && !limit) {
574                                                         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);
575                                                         limit = 1023;
576                                                 } else if (ast_false(v->value)) {
577                                                         ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
578                                                         enabled = 0;
579                                                         break;
580                                                 }
581                                         } else if (!strcasecmp(v->name, "enabled")) {
582                                                 enabled = ast_true(v->value);
583                                         } else if (!strcasecmp(v->name, "pre-connect")) {
584                                                 connect = ast_true(v->value);
585                                         } else if (!strcasecmp(v->name, "dsn")) {
586                                                 dsn = v->value;
587                                         } else if (!strcasecmp(v->name, "username")) {
588                                                 username = v->value;
589                                         } else if (!strcasecmp(v->name, "password")) {
590                                                 password = v->value;
591                                         } else if (!strcasecmp(v->name, "sanitysql")) {
592                                                 sanitysql = v->value;
593                                         }
594                                 }
595
596                                 if (enabled && !ast_strlen_zero(dsn)) {
597                                         /* First, check the list to see if it already exists */
598                                         AST_LIST_TRAVERSE(&odbc_list, class, list) {
599                                                 if (!strcmp(class->name, cat)) {
600                                                         class->delme = 0;
601                                                         break;
602                                                 }
603                                         }
604
605                                         if (class) {
606                                                 new = class;
607                                         } else {
608                                                 new = ast_calloc(1, sizeof(*new));
609                                         }
610
611                                         if (!new) {
612                                                 res = -1;
613                                                 break;
614                                         }
615
616                                         if (cat)
617                                                 ast_copy_string(new->name, cat, sizeof(new->name));
618                                         if (dsn)
619                                                 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
620                                         if (username)
621                                                 ast_copy_string(new->username, username, sizeof(new->username));
622                                         if (password)
623                                                 ast_copy_string(new->password, password, sizeof(new->password));
624                                         if (sanitysql)
625                                                 ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
626
627                                         if (!class) {
628                                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
629                                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
630
631                                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
632                                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
633                                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
634                                                         AST_LIST_UNLOCK(&odbc_list);
635                                                         return res;
636                                                 }
637                                         }
638
639                                         if (pooling) {
640                                                 new->haspool = pooling;
641                                                 if (limit) {
642                                                         new->limit = limit;
643                                                 } else {
644                                                         ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
645                                                         new->limit = 5;
646                                                 }
647                                         }
648
649                                         if (class) {
650                                                 ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
651                                         } else {
652                                                 odbc_register_class(new, connect);
653                                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
654                                         }
655                                 }
656                         }
657                 }
658                 ast_config_destroy(config);
659         }
660
661         /* Purge classes that we know can go away (pooled with 0, only) */
662         AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
663                 if (class->delme && class->haspool && class->count == 0) {
664                         AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) {
665                                 AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list);
666                                 odbc_obj_disconnect(current);
667                                 ast_mutex_destroy(&current->lock);
668                                 ast_free(current);
669                         }
670                         AST_LIST_TRAVERSE_SAFE_END;
671
672                         AST_LIST_REMOVE_CURRENT(&odbc_list, list);
673                         ast_free(class);
674                 }
675         }
676         AST_LIST_TRAVERSE_SAFE_END;
677         AST_LIST_UNLOCK(&odbc_list);
678
679         return 0;
680 }
681
682 static int unload_module(void)
683 {
684         /* Prohibit unloading */
685         return -1;
686 }
687
688 static int load_module(void)
689 {
690         if(load_odbc_config() == -1)
691                 return AST_MODULE_LOAD_DECLINE;
692         ast_cli_register_multiple(cli_odbc, sizeof(cli_odbc) / sizeof(struct ast_cli_entry));
693         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
694         return 0;
695 }
696
697 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ODBC resource",
698                 .load = load_module,
699                 .unload = unload_module,
700                 .reload = reload,
701                );