c8efb062014988b1f567f860d3f1ad3a49bd1ebc
[asterisk/asterisk.git] / res / res_odbc.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Copyright (C) 1999, Mark Spencer
5  *
6  * Mark Spencer <markster@linux-support.net>
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, 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 && username && password) {
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(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         new->username = malloc(strlen(username) + 1);
237         if (new->username == NULL)
238                 return NULL;
239
240         new->password = malloc(strlen(password) + 1);
241         if (new->password == NULL)
242                 return NULL;
243
244         strcpy(new->name, name);
245         strcpy(new->dsn, dsn);
246         strcpy(new->username, username);
247         strcpy(new->password, password);
248         new->up = 0;
249         ast_mutex_init(&new->lock);
250         return new;
251 }
252
253 void destroy_obdc_obj(odbc_obj ** obj)
254 {
255         odbc_obj_disconnect(*obj);
256
257         ast_mutex_lock(&(*obj)->lock);
258         SQLFreeHandle(SQL_HANDLE_STMT, (*obj)->stmt);
259         SQLFreeHandle(SQL_HANDLE_DBC, (*obj)->con);
260         SQLFreeHandle(SQL_HANDLE_ENV, (*obj)->env);
261
262         free((*obj)->name);
263         free((*obj)->dsn);
264         free((*obj)->username);
265         free((*obj)->password);
266         ast_mutex_unlock(&(*obj)->lock);
267         free(*obj);
268 }
269
270 odbc_status odbc_obj_disconnect(odbc_obj * obj)
271 {
272         int res;
273         ast_mutex_lock(&obj->lock);
274
275         if (obj->up) {
276                 res = SQLDisconnect(obj->con);
277         } else {
278                 res = -1;
279         }
280
281         if (res == ODBC_SUCCESS) {
282                 ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
283                 obj->up = 0;
284         } else {
285                 ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
286                 obj->name, obj->dsn);
287         }
288         ast_mutex_unlock(&obj->lock);
289         return ODBC_SUCCESS;
290 }
291
292 odbc_status odbc_obj_connect(odbc_obj * obj)
293 {
294         int res;
295         long int err;
296         short int mlen;
297         char msg[200], stat[10];
298
299         ast_mutex_lock(&obj->lock);
300
301         if (obj->env == SQL_NULL_HANDLE) {
302                 res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env);
303
304                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
305                         if (option_verbose > 3)
306                                 ast_log(LOG_WARNING, "res_odbc: Error AllocHandle\n");
307                         ast_mutex_unlock(&obj->lock);
308                         return ODBC_FAIL;
309                 }
310
311                 res = SQLSetEnvAttr(obj->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
312
313                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
314                         if (option_verbose > 3)
315                                 ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
316                         SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
317                         ast_mutex_unlock(&obj->lock);
318                         return ODBC_FAIL;
319                 }
320
321                 res = SQLAllocHandle(SQL_HANDLE_DBC, obj->env, &obj->con);
322
323                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
324
325                         if (option_verbose > 3)
326                                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
327                         SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
328
329                         ast_mutex_unlock(&obj->lock);
330                         return ODBC_FAIL;
331                 }
332                 SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
333         }
334
335         res = SQLConnect(obj->con,
336                    (SQLCHAR *) obj->dsn, SQL_NTS,
337                    (SQLCHAR *) obj->username, SQL_NTS,
338                    (SQLCHAR *) obj->password, SQL_NTS);
339
340         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
341                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
342                 SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
343                 if (option_verbose > 3)
344                         ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%ld %s\n", res, err, msg);
345                 return ODBC_FAIL;
346         } else {
347
348                 if (option_verbose > 3)
349                         ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->name, obj->dsn);
350                 obj->up = 1;
351         }
352
353         ast_mutex_unlock(&obj->lock);
354         return ODBC_SUCCESS;
355 }
356
357 STANDARD_LOCAL_USER;
358
359 LOCAL_USER_DECL;
360
361 int unload_module(void)
362 {
363         STANDARD_HANGUP_LOCALUSERS;
364         odbc_destroy();
365         ast_cli_unregister(&odbc_command_struct);
366         ast_log(LOG_NOTICE, "res_odbc unloaded.\n");
367         return 0;
368 }
369
370 int load_module(void)
371 {
372         odbc_init();
373         load_odbc_config();
374         ast_cli_register(&odbc_command_struct);
375         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
376         return 0;
377 }
378
379 char *description(void)
380 {
381         return tdesc;
382 }
383
384 int usecount(void)
385 {
386         int res;
387         STANDARD_USECOUNT(res);
388         return res;
389 }
390
391 char *key()
392 {
393         return ASTERISK_GPL_KEY;
394 }