fix a small memory leak, from brian
[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;
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                 return 0;
256         }
257         
258         var = ast_variable_browse(cfg, "global");
259         if (!var) {
260                 /* nothing configured */
261                 return 0;
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         ast_mutex_unlock(&odbc_lock);
372         return res;
373 }
374
375 static int odbc_do_query(void)
376 {
377         long int ODBC_err;
378         int ODBC_res;
379         short int ODBC_mlen;
380         char ODBC_msg[200], ODBC_stat[10];
381
382         ODBC_res = SQLExecute(ODBC_stmt);
383
384         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
385         {
386                 if(option_verbose > 3)
387                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
388                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
389                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
390                 connected = 0;
391                 return -1;
392         }
393         else
394         {
395                 if(option_verbose > 3)
396                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
397                 connected = 1;
398         }
399         return 0;
400 }
401
402 static int odbc_init(void)
403 {
404         long int ODBC_err;
405         short int ODBC_mlen;
406         int ODBC_res;
407         char ODBC_msg[200], ODBC_stat[10];
408
409         if ( ODBC_env == SQL_NULL_HANDLE || connected == 0 )
410         {
411                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
412
413                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
414                 {
415                         if(option_verbose > 3)
416                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
417                         connected = 0;
418                         return -1;
419                 }
420
421                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
422
423                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
424                 {
425                         if(option_verbose > 3)
426                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
427                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
428                         connected = 0;
429                         return -1;
430                 }
431
432                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
433
434                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
435                 {
436                         if(option_verbose > 3)
437                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
438                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
439                         connected = 0;
440                         return -1;
441                 }
442
443                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
444         }
445
446         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
447
448         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
449         {
450                 if(option_verbose > 3)
451                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
452                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
453                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
454                 connected = 0;
455                 return -1;
456         }
457         else
458         {
459                 if(option_verbose > 3)
460                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
461                 connected = 1;
462         }
463         return 0;
464 }
465
466 int load_module(void)
467 {
468         return odbc_load_module();
469 }
470
471 int unload_module(void)
472 {
473         return odbc_unload_module();
474 }
475
476 int reload(void)
477 {
478         odbc_unload_module();
479         return odbc_load_module();
480 }
481
482 int usecount(void)
483 {
484         return connected;
485 }
486
487 char *key()
488 {
489         return ASTERISK_GPL_KEY;
490 }