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