prevent deadlock if no config file
[asterisk/asterisk.git] / cdr / cdr_odbc.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * ODBC CDR Backend
5  * 
6  * Brian K. West <brian@bkw.org>
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License.
10  *
11  * Copyright (c) 2003 Digium, Inc.
12  *
13  */
14
15 #include <sys/types.h>
16 #include <asterisk/config.h>
17 #include <asterisk/options.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/module.h>
21 #include <asterisk/logger.h>
22 #include "../asterisk.h"
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <time.h>
30
31 #include <sql.h>
32 #include <sqlext.h>
33 #include <sqltypes.h>
34
35 #define DATE_FORMAT "%Y-%m-%d %T"
36
37 static char *desc = "ODBC CDR Backend";
38 static char *name = "ODBC";
39 static char *config = "cdr_odbc.conf";
40 static char *dsn = NULL, *username = NULL, *password = NULL, *loguniqueid = NULL;
41 static int dsn_alloc = 0, username_alloc = 0, password_alloc = 0, loguniqueid_alloc = 0;
42 static int connected = 0;
43
44 static ast_mutex_t odbc_lock = AST_MUTEX_INITIALIZER;
45
46 static int odbc_do_query(void);
47 static int odbc_init(void);
48
49 static SQLHENV  ODBC_env = SQL_NULL_HANDLE;     /* global ODBC Environment */
50 static SQLHDBC  ODBC_con;                       /* global ODBC Connection Handle */
51 static SQLHSTMT ODBC_stmt;                      /* global ODBC Statement Handle */
52
53 static int odbc_log(struct ast_cdr *cdr)
54 {
55         long int ODBC_err;
56         short int ODBC_mlen;
57         int ODBC_res;
58         char ODBC_msg[200], ODBC_stat[10];
59         char sqlcmd[2048], timestr[128];
60         int res = 0;
61         struct tm tm;
62         struct timeval tv;
63         time_t t;
64
65         gettimeofday(&tv,NULL);
66         t = tv.tv_sec;
67         localtime_r(&t,&tm);
68
69         ast_mutex_lock(&odbc_lock);
70         strftime(timestr,128,DATE_FORMAT,&tm);
71         memset(sqlcmd,0,2048);
72         if((loguniqueid != NULL) && ((strcmp(loguniqueid, "1") == 0) || (strcmp(loguniqueid, "yes") == 0)))
73         {
74                 sprintf(sqlcmd,"INSERT INTO cdr "
75                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
76                 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) "
77                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
78         }
79         else
80         {
81                 sprintf(sqlcmd,"INSERT INTO cdr "
82                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
83                 "duration,billsec,disposition,amaflags,accountcode) "
84                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
85         }
86
87         if(!connected)
88         {
89                 res =  odbc_init();
90                 if(res < 0)
91                 {
92                         connected = 0;
93                         ast_mutex_unlock(&odbc_lock);
94                         return 0;
95                 }                               
96                 
97         }
98
99         ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt);
100
101         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
102         {
103                 if(option_verbose > 3)
104                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
105                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
106                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);      
107                 connected = 0;
108                 ast_mutex_unlock(&odbc_lock);
109                 return 0;
110         }
111
112         /* We really should only have to do this once.  But for some
113            strange reason if I don't it blows holes in memory like
114            like a shotgun.  So we just do this so its safe. */
115
116         ODBC_res = SQLPrepare(ODBC_stmt, sqlcmd, SQL_NTS);
117         
118         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
119         {
120                 if(option_verbose > 3)
121                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
122                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
123                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
124                 connected = 0;
125                 ast_mutex_unlock(&odbc_lock);
126                 return 0;
127         }
128
129         SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, &timestr, 0, NULL);
130         SQLBindParameter(ODBC_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->clid, 0, NULL);
131         SQLBindParameter(ODBC_stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->src, 0, NULL);
132         SQLBindParameter(ODBC_stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->dst, 0, NULL);
133         SQLBindParameter(ODBC_stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->dcontext, 0, NULL);
134         SQLBindParameter(ODBC_stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->channel, 0, NULL);
135         SQLBindParameter(ODBC_stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->dstchannel, 0, NULL);
136         SQLBindParameter(ODBC_stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->lastapp, 0, NULL);
137         SQLBindParameter(ODBC_stmt, 9, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->lastdata, 0, NULL);
138         SQLBindParameter(ODBC_stmt, 10, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
139         SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
140         SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
141         SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
142         SQLBindParameter(ODBC_stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->accountcode, 0, NULL);
143
144         if((loguniqueid != NULL) && ((strcmp(loguniqueid, "1") == 0) || (strcmp(loguniqueid, "yes") == 0)))
145         {
146                 SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->uniqueid, 0, NULL);
147         }
148
149         if(connected)
150         {
151                 res = odbc_do_query();
152                 if(res < 0)
153                 {
154                         if(option_verbose > 3)          
155                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
156                         res = odbc_init();
157                         if(option_verbose > 3)
158                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
159                         if(res < 0)
160                         {
161                                 if(option_verbose > 3)
162                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
163                                 connected = 0;
164                         }
165                         else
166                         {
167                                 if(option_verbose > 3)
168                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
169                                 res = odbc_do_query();
170                                 if(res < 0)
171                                 {
172                                         if(option_verbose > 3)
173                                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
174                                 }
175                         }
176                 }
177         }
178         else
179         {
180                 if(option_verbose > 3)
181                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
182         }
183         ast_mutex_unlock(&odbc_lock);
184         return 0;
185 }
186
187 char *description(void)
188 {
189         return desc;
190 }
191
192 static int odbc_unload_module(void)
193 {
194         ast_mutex_lock(&odbc_lock);
195         if (connected)
196         {
197                 if(option_verbose > 3)
198                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Disconnecting from %s\n", dsn);
199                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
200                 SQLDisconnect(ODBC_con);
201                 SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
202                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
203                 connected = 0;
204         }
205         if (dsn && dsn_alloc)
206         {
207                 if(option_verbose > 3)
208                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
209                 free(dsn);
210                 dsn = NULL;
211                 dsn_alloc = 0;
212         }
213         if (username && username_alloc)
214         {
215                 if(option_verbose > 3)
216                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
217                 free(username);
218                 username = NULL;
219                 username_alloc = 0;
220         }
221         if (password && password_alloc)
222         {
223                 if(option_verbose > 3)
224                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
225                 free(password);
226                 password = NULL;
227                 password_alloc = 0;
228         }
229         if (loguniqueid && loguniqueid_alloc)
230         {
231                 if(option_verbose > 3)
232                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free loguniqueid\n");
233                 free(loguniqueid);
234                 loguniqueid = NULL;
235                 loguniqueid_alloc = 0;
236         }
237         ast_cdr_unregister(name);
238         ast_mutex_unlock(&odbc_lock);
239         return 0;
240 }
241
242 static int odbc_load_module(void)
243 {
244         int res = 0;
245         struct ast_config *cfg;
246         struct ast_variable *var;
247         char *tmp;
248
249         ast_mutex_lock(&odbc_lock);
250
251         cfg = ast_load(config);
252         if (!cfg)
253         {
254                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
255                 goto out;
256         }
257         
258         var = ast_variable_browse(cfg, "global");
259         if (!var) {
260                 /* nothing configured */
261                 goto out;
262         }
263
264         tmp = ast_variable_retrieve(cfg,"global","dsn");
265         if (tmp)
266         {
267                 dsn = malloc(strlen(tmp) + 1);
268                 if (dsn != NULL)
269                 {
270                         dsn_alloc = 1;
271                         strcpy(dsn,tmp);
272                 }
273                 else
274                 {
275                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
276                         return -1;
277                 }
278         }
279         else
280         {
281                 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming asteriskdb\n");
282                 dsn = "asteriskdb";
283         }
284
285         tmp = ast_variable_retrieve(cfg,"global","username");
286         if (tmp)
287         {
288                 username = malloc(strlen(tmp) + 1);
289                 if (username != NULL)
290                 {
291                         username_alloc = 1;
292                         strcpy(username,tmp);
293                 }
294                 else
295                 {
296                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
297                         return -1;
298                 }
299         }
300         else
301         {
302                 ast_log(LOG_WARNING,"cdr_odbc: username not specified.  Assuming root\n");
303                 username = "root";
304         }
305
306         tmp = ast_variable_retrieve(cfg,"global","password");
307         if (tmp)
308         {
309                 password = malloc(strlen(tmp) + 1);
310                 if (password != NULL)
311                 {
312                         password_alloc = 1;
313                         strcpy(password,tmp);
314                 }
315                 else
316                 {
317                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
318                         return -1;
319                 }
320         }
321         else
322         {
323                 ast_log(LOG_WARNING,"cdr_odbc: database password not specified.  Assuming blank\n");
324                 password = "";
325         }
326
327         tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
328         if (tmp)
329         {
330                 loguniqueid = malloc(strlen(tmp) + 1);
331                 if (loguniqueid != NULL)
332                 {
333                         strcpy(loguniqueid,tmp);
334                         loguniqueid_alloc = 1;
335                         ast_log(LOG_WARNING,"cdr_odbc: Logging uniqueid\n");
336                 }
337                 else
338                 {
339                         ast_log(LOG_ERROR,"cdr_odbc: Not logging uniqueid\n");
340                         loguniqueid_alloc = 1;
341                         loguniqueid = NULL;
342                 }
343         }
344         else
345         {
346                 ast_log(LOG_WARNING,"cdr_odbc: Not logging uniqueid\n");
347                 loguniqueid = NULL;
348         }
349
350         ast_destroy(cfg);
351         if(option_verbose > 3)
352         {
353                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: dsn is %s\n",dsn);
354                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: username is %s\n",username);
355                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: password is [secret]\n");
356
357         }
358         
359         res = odbc_init();
360         if(res < 0)
361         {
362                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
363                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
364         }
365
366         res = ast_cdr_register(name, desc, odbc_log);
367         if (res)
368         {
369                 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
370         }
371 out:
372         ast_mutex_unlock(&odbc_lock);
373         return res;
374 }
375
376 static int odbc_do_query(void)
377 {
378         long int ODBC_err;
379         int ODBC_res;
380         short int ODBC_mlen;
381         char ODBC_msg[200], ODBC_stat[10];
382
383         ODBC_res = SQLExecute(ODBC_stmt);
384
385         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
386         {
387                 if(option_verbose > 3)
388                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
389                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
390                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
391                 connected = 0;
392                 return -1;
393         }
394         else
395         {
396                 if(option_verbose > 3)
397                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
398                 connected = 1;
399         }
400         return 0;
401 }
402
403 static int odbc_init(void)
404 {
405         long int ODBC_err;
406         short int ODBC_mlen;
407         int ODBC_res;
408         char ODBC_msg[200], ODBC_stat[10];
409
410         if ( ODBC_env == SQL_NULL_HANDLE || connected == 0 )
411         {
412                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
413
414                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
415                 {
416                         if(option_verbose > 3)
417                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
418                         connected = 0;
419                         return -1;
420                 }
421
422                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
423
424                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
425                 {
426                         if(option_verbose > 3)
427                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
428                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
429                         connected = 0;
430                         return -1;
431                 }
432
433                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
434
435                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
436                 {
437                         if(option_verbose > 3)
438                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
439                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
440                         connected = 0;
441                         return -1;
442                 }
443
444                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
445         }
446
447         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
448
449         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
450         {
451                 if(option_verbose > 3)
452                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
453                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
454                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
455                 connected = 0;
456                 return -1;
457         }
458         else
459         {
460                 if(option_verbose > 3)
461                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
462                 connected = 1;
463         }
464         return 0;
465 }
466
467 int load_module(void)
468 {
469         return odbc_load_module();
470 }
471
472 int unload_module(void)
473 {
474         return odbc_unload_module();
475 }
476
477 int reload(void)
478 {
479         odbc_unload_module();
480         return odbc_load_module();
481 }
482
483 int usecount(void)
484 {
485         return connected;
486 }
487
488 char *key()
489 {
490         return ASTERISK_GPL_KEY;
491 }