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