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