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