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