Major changes to res_config to support centralized config, eliminate configuration...
[asterisk/asterisk.git] / res / res_odbc.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Copyright (C) 1999-2004, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * res_odbc.c <ODBC resource manager>
9  * Copyright (C) 2004 Anthony Minessale II <anthmct@yahoo.com>
10  */
11
12 #include <asterisk/file.h>
13 #include <asterisk/logger.h>
14 #include <asterisk/channel.h>
15 #include <asterisk/config.h>
16 #include <asterisk/options.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/cli.h>
20 #include <asterisk/lock.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <asterisk/res_odbc.h>
25 #define MAX_ODBC_HANDLES 25
26
27 struct odbc_list
28 {
29         char name[80];
30         odbc_obj *obj;
31         int used;
32 };
33
34 static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES];
35
36 static void odbc_destroy(void)
37 {
38         int x = 0;
39
40         for (x = 0; x < MAX_ODBC_HANDLES; x++) {
41                 if (ODBC_REGISTRY[x].obj) {
42                         destroy_obdc_obj(&ODBC_REGISTRY[x].obj);
43                         ODBC_REGISTRY[x].obj = NULL;
44                 }
45         }
46 }
47
48 static odbc_obj *odbc_read(struct odbc_list *registry, const char *name)
49 {
50         int x = 0;
51         for (x = 0; x < MAX_ODBC_HANDLES; x++) {
52                 if (registry[x].used && !strcmp(registry[x].name, name)) {
53                         return registry[x].obj;
54                 }
55         }
56         return NULL;
57 }
58
59 static int odbc_write(struct odbc_list *registry, char *name, odbc_obj * obj)
60 {
61         int x = 0;
62         for (x = 0; x < MAX_ODBC_HANDLES; x++) {
63                 if (!registry[x].used) {
64                         strncpy(registry[x].name, name, sizeof(registry[x].name) - 1);
65                         registry[x].obj = obj;
66                         registry[x].used = 1;
67                         return 1;
68                 }
69         }
70         return 0;
71 }
72
73 static void odbc_init(void)
74 {
75         int x = 0;
76         for (x = 0; x < MAX_ODBC_HANDLES; x++) {
77                 memset(&ODBC_REGISTRY[x], 0, sizeof(struct odbc_list));
78         }
79 }
80
81 static char *tdesc = "ODBC Resource";
82 /* internal stuff */
83
84 static int load_odbc_config(void)
85 {
86         static char *cfg = "res_odbc.conf";
87         struct ast_config *config;
88         struct ast_variable *v;
89         char *cat, *dsn, *username, *password;
90         int enabled;
91         int connect = 0;
92         char *env_var;
93
94         odbc_obj *obj;
95
96         config = ast_load(cfg);
97         if (config) {
98                 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
99                         if (!strcmp(cat, "ENV")) {
100                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
101                                         env_var = malloc(strlen(v->name) + strlen(v->value) + 2);
102                                         sprintf(env_var, "%s=%s", v->name, v->value);
103                                         ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
104                                         putenv(env_var);
105                                         free(env_var);
106                                 }
107
108                         cat = ast_category_browse(config, cat);
109                         }
110
111                         dsn = username = password = NULL;
112                         enabled = 1;
113                         connect = 0;
114                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
115                                 if (!strcmp(v->name, "enabled"))
116                                         enabled = ast_true(v->value);
117                                 if (!strcmp(v->name, "pre-connect"))
118                                         connect = ast_true(v->value);
119                                 if (!strcmp(v->name, "dsn"))
120                                         dsn = v->value;
121                                 if (!strcmp(v->name, "username"))
122                                         username = v->value;
123                                 if (!strcmp(v->name, "password"))
124                                         password = v->value;
125                         }
126
127                         if (enabled && dsn) {
128                                 obj = new_odbc_obj(cat, dsn, username, password);
129                                 if (obj) {
130                                         register_odbc_obj(cat, obj);
131                                         ast_log(LOG_NOTICE, "registered database handle '%s' dsn->[%s]\n", cat, obj->dsn);
132                                         if (connect) {
133                                                 odbc_obj_connect(obj);
134                                         }
135                                 } else {
136                                         ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat);
137                                 }
138
139                         }
140                 }
141                 ast_destroy(config);
142         }
143         return 0;
144 }
145
146 int odbc_dump_fd(int fd, odbc_obj * obj)
147 {
148         ast_cli(fd, "\n\nName: %s\nDSN: %s\nConnected: %s\n\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
149         return 0;
150 }
151
152 static int odbc_usage(int fd)
153 {
154         ast_cli(fd, "\n\nusage odbc <command> <arg1> .. <argn>\n\n");
155         return 0;
156 }
157
158 static int odbc_command(int fd, int argc, char **argv)
159 {
160         odbc_obj *obj;
161         int x = 0;
162         if (!argv[1])
163                 return odbc_usage(fd);
164
165         ast_cli(fd, "\n\n");
166
167         if (!strcmp(argv[1], "connect") || !strcmp(argv[1], "disconnect")) {
168                 if (!argv[2])
169                         return odbc_usage(fd);
170
171                 obj = odbc_read(ODBC_REGISTRY, argv[2]);
172                 if (obj) {
173                         if (!strcmp(argv[1], "connect"))
174                                 odbc_obj_connect(obj);
175
176                         if (!strcmp(argv[1], "disconnect"))
177                                 odbc_obj_disconnect(obj);
178                 }
179
180         } else if (!strcmp(argv[1], "show")) {
181                 if (!argv[2] || (argv[2] && !strcmp(argv[2], "all"))) {
182                         for (x = 0; x < MAX_ODBC_HANDLES; x++) {
183                                 if (!ODBC_REGISTRY[x].used)
184                                         break;
185                                 if (ODBC_REGISTRY[x].obj)
186                                         odbc_dump_fd(fd, ODBC_REGISTRY[x].obj);
187                         }
188                 } else {
189                         obj = odbc_read(ODBC_REGISTRY, argv[2]);
190                         if (obj)
191                                 odbc_dump_fd(fd, obj);
192                 }
193
194         } else {
195                 return odbc_usage(fd);
196         }
197         ast_cli(fd, "\n");
198         return 0;
199 }
200
201 static struct ast_cli_entry odbc_command_struct = {
202         {"odbc", NULL}, odbc_command,
203         "Execute ODBC Command", "obdc <command> <arg1> .. <argn>", NULL
204 };
205
206 /* api calls */
207
208 int register_odbc_obj(char *name, odbc_obj * obj)
209 {
210         if (obj != NULL)
211                 return odbc_write(ODBC_REGISTRY, name, obj);
212         return 0;
213 }
214
215 odbc_obj *fetch_odbc_obj(const char *name)
216 {
217         return (odbc_obj *) odbc_read(ODBC_REGISTRY, name);
218 }
219
220 odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
221 {
222         static odbc_obj *new;
223
224         new = malloc(sizeof(odbc_obj));
225         memset(new, 0, sizeof(odbc_obj));
226         new->env = SQL_NULL_HANDLE;
227
228         new->name = malloc(strlen(name) + 1);
229         if (new->name == NULL)
230                 return NULL;
231
232         new->dsn = malloc(strlen(dsn) + 1);
233         if (new->dsn == NULL)
234                 return NULL;
235
236         if (username) {
237                 new->username = malloc(strlen(username) + 1);
238                 if (new->username == NULL)
239                         return NULL;
240                 strcpy(new->username, username);
241         }
242
243         if (password) {
244                 new->password = malloc(strlen(password) + 1);
245                 if (new->password == NULL)
246                         return NULL;
247                 strcpy(new->password, password);
248         }
249
250         strcpy(new->name, name);
251         strcpy(new->dsn, dsn);
252         new->up = 0;
253         ast_mutex_init(&new->lock);
254         return new;
255 }
256
257 void destroy_obdc_obj(odbc_obj ** obj)
258 {
259         odbc_obj_disconnect(*obj);
260
261         ast_mutex_lock(&(*obj)->lock);
262         SQLFreeHandle(SQL_HANDLE_STMT, (*obj)->stmt);
263         SQLFreeHandle(SQL_HANDLE_DBC, (*obj)->con);
264         SQLFreeHandle(SQL_HANDLE_ENV, (*obj)->env);
265
266         free((*obj)->name);
267         free((*obj)->dsn);
268         if ((*obj)->username)
269                 free((*obj)->username);
270         if ((*obj)->password)
271                 free((*obj)->password);
272         ast_mutex_unlock(&(*obj)->lock);
273         free(*obj);
274 }
275
276 odbc_status odbc_obj_disconnect(odbc_obj * obj)
277 {
278         int res;
279         ast_mutex_lock(&obj->lock);
280
281         if (obj->up) {
282                 res = SQLDisconnect(obj->con);
283         } else {
284                 res = -1;
285         }
286
287         if (res == ODBC_SUCCESS) {
288                 ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
289                 obj->up = 0;
290         } else {
291                 ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
292                 obj->name, obj->dsn);
293         }
294         ast_mutex_unlock(&obj->lock);
295         return ODBC_SUCCESS;
296 }
297
298 odbc_status odbc_obj_connect(odbc_obj * obj)
299 {
300         int res;
301         long int err;
302         short int mlen;
303         char msg[200], stat[10];
304
305         ast_mutex_lock(&obj->lock);
306
307         if (obj->env == SQL_NULL_HANDLE) {
308                 res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env);
309
310                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
311                         if (option_verbose > 3)
312                                 ast_log(LOG_WARNING, "res_odbc: Error AllocHandle\n");
313                         ast_mutex_unlock(&obj->lock);
314                         return ODBC_FAIL;
315                 }
316
317                 res = SQLSetEnvAttr(obj->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
318
319                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
320                         if (option_verbose > 3)
321                                 ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
322                         SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
323                         ast_mutex_unlock(&obj->lock);
324                         return ODBC_FAIL;
325                 }
326
327                 res = SQLAllocHandle(SQL_HANDLE_DBC, obj->env, &obj->con);
328
329                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
330
331                         if (option_verbose > 3)
332                                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
333                         SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
334
335                         ast_mutex_unlock(&obj->lock);
336                         return ODBC_FAIL;
337                 }
338                 SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
339         }
340
341         ast_log(LOG_NOTICE, "Calling %p/%p\n", obj->username, obj->password);
342         res = SQLConnect(obj->con,
343                    (SQLCHAR *) obj->dsn, SQL_NTS,
344                    (SQLCHAR *) obj->username, SQL_NTS,
345                    (SQLCHAR *) obj->password, SQL_NTS);
346
347         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
348                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
349                 SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
350                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%ld %s\n", res, err, msg);
351                 return ODBC_FAIL;
352         } else {
353
354                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->name, obj->dsn);
355                 obj->up = 1;
356         }
357
358         ast_mutex_unlock(&obj->lock);
359         return ODBC_SUCCESS;
360 }
361
362 STANDARD_LOCAL_USER;
363
364 LOCAL_USER_DECL;
365
366 int unload_module(void)
367 {
368         STANDARD_HANGUP_LOCALUSERS;
369         odbc_destroy();
370         ast_cli_unregister(&odbc_command_struct);
371         ast_log(LOG_NOTICE, "res_odbc unloaded.\n");
372         return 0;
373 }
374
375 int load_module(void)
376 {
377         odbc_init();
378         load_odbc_config();
379         ast_cli_register(&odbc_command_struct);
380         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
381         return 0;
382 }
383
384 char *description(void)
385 {
386         return tdesc;
387 }
388
389 int usecount(void)
390 {
391         int res;
392         STANDARD_USECOUNT(res);
393         return res;
394 }
395
396 char *key()
397 {
398         return ASTERISK_GPL_KEY;
399 }