Merged revisions 74515 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 /*** MODULEINFO
32         <depend>unixodbc</depend>
33         <depend>ltdl</depend>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <time.h>
47
48 #ifndef __CYGWIN__
49 #include <sql.h>
50 #include <sqlext.h>
51 #include <sqltypes.h>
52 #else
53 #include <windows.h>
54 #include <w32api/sql.h>
55 #include <w32api/sqlext.h>
56 #include <w32api/sqltypes.h>
57 #endif
58
59 #include "asterisk/config.h"
60 #include "asterisk/options.h"
61 #include "asterisk/channel.h"
62 #include "asterisk/cdr.h"
63 #include "asterisk/module.h"
64 #include "asterisk/logger.h"
65
66 #define DATE_FORMAT "%Y-%m-%d %T"
67
68 static char *name = "ODBC";
69 static char *config = "cdr_odbc.conf";
70 static char *dsn = NULL, *username = NULL, *password = NULL, *table = NULL;
71 static int loguniqueid = 0;
72 static int usegmtime = 0;
73 static int dispositionstring = 0;
74 static int connected = 0;
75
76 AST_MUTEX_DEFINE_STATIC(odbc_lock);
77
78 static int odbc_do_query(void);
79 static int odbc_init(void);
80
81 static SQLHENV  ODBC_env = SQL_NULL_HANDLE;     /* global ODBC Environment */
82 static SQLHDBC  ODBC_con;                       /* global ODBC Connection Handle */
83 static SQLHSTMT ODBC_stmt;                      /* global ODBC Statement Handle */
84
85 static void odbc_disconnect(void)
86 {
87         SQLDisconnect(ODBC_con);
88         SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
89         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
90         connected = 0;
91 }
92
93 static int odbc_log(struct ast_cdr *cdr)
94 {
95         int ODBC_res;
96         char sqlcmd[2048] = "", timestr[128];
97         int res = 0;
98         struct tm tm;
99
100         if (usegmtime) 
101                 gmtime_r(&cdr->start.tv_sec,&tm);
102         else
103                 ast_localtime(&cdr->start.tv_sec, &tm, NULL);
104
105         ast_mutex_lock(&odbc_lock);
106         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
107         memset(sqlcmd,0,2048);
108         if (loguniqueid) {
109                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
110                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
111                 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
112                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
113         } else {
114                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
115                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
116                 "duration,billsec,disposition,amaflags,accountcode) "
117                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
118         }
119
120         if (!connected) {
121                 res = odbc_init();
122                 if (res < 0) {
123                         odbc_disconnect();
124                         ast_mutex_unlock(&odbc_lock);
125                         return 0;
126                 }                               
127         }
128
129         ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt);
130
131         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
132                 if (option_verbose > 10)
133                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
134                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
135                 odbc_disconnect();
136                 ast_mutex_unlock(&odbc_lock);
137                 return 0;
138         }
139
140         /* We really should only have to do this once.  But for some
141            strange reason if I don't it blows holes in memory like
142            like a shotgun.  So we just do this so its safe. */
143
144         ODBC_res = SQLPrepare(ODBC_stmt, (unsigned char *)sqlcmd, SQL_NTS);
145         
146         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
147                 if (option_verbose > 10)
148                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
149                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
150                 odbc_disconnect();
151                 ast_mutex_unlock(&odbc_lock);
152                 return 0;
153         }
154
155         SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(timestr), 0, &timestr, 0, NULL);
156         SQLBindParameter(ODBC_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
157         SQLBindParameter(ODBC_stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
158         SQLBindParameter(ODBC_stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
159         SQLBindParameter(ODBC_stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
160         SQLBindParameter(ODBC_stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
161         SQLBindParameter(ODBC_stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
162         SQLBindParameter(ODBC_stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
163         SQLBindParameter(ODBC_stmt, 9, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
164         SQLBindParameter(ODBC_stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
165         SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
166         if (dispositionstring)
167                 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);
168         else
169                 SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
170         SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
171         SQLBindParameter(ODBC_stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
172
173         if (loguniqueid) {
174                 SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
175                 SQLBindParameter(ODBC_stmt, 16, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
176         }
177
178         if (connected) {
179                 res = odbc_do_query();
180                 if (res < 0) {
181                         if (option_verbose > 10)                
182                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
183                         if (option_verbose > 10)
184                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
185                         SQLDisconnect(ODBC_con);
186                         res = odbc_init();
187                         if (res < 0) {
188                                 if (option_verbose > 10)
189                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
190                                 odbc_disconnect();
191                         } else {
192                                 if (option_verbose > 10)
193                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
194                                 res = odbc_do_query();
195                                 if (res < 0) {
196                                         if (option_verbose > 10)
197                                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
198                                 }
199                         }
200                 }
201         } else {
202                 if (option_verbose > 10)
203                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
204         }
205         SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
206         ast_mutex_unlock(&odbc_lock);
207         return 0;
208 }
209
210 static int odbc_unload_module(void)
211 {
212         ast_mutex_lock(&odbc_lock);
213         if (connected) {
214                 if (option_verbose > 10)
215                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Disconnecting from %s\n", dsn);
216                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
217                 odbc_disconnect();
218         }
219         if (dsn) {
220                 if (option_verbose > 10)
221                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
222                 ast_free(dsn);
223         }
224         if (username) {
225                 if (option_verbose > 10)
226                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
227                 ast_free(username);
228         }
229         if (password) {
230                 if (option_verbose > 10)
231                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
232                 ast_free(password);
233         }
234         if (table) {
235                 if (option_verbose > 10)
236                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free table\n");
237                 ast_free(table);
238         }
239
240         ast_cdr_unregister(name);
241         ast_mutex_unlock(&odbc_lock);
242         return 0;
243 }
244
245 static int odbc_load_module(void)
246 {
247         int res = 0;
248         struct ast_config *cfg;
249         struct ast_variable *var;
250         const char *tmp;
251
252         ast_mutex_lock(&odbc_lock);
253
254         cfg = ast_config_load(config);
255         if (!cfg) {
256                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
257                 res = AST_MODULE_LOAD_DECLINE;
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_debug(1,"cdr_odbc: Logging uniqueid\n");
311                 } else {
312                         ast_debug(1,"cdr_odbc: Not logging uniqueid\n");
313                 }
314         } else {
315                 ast_debug(1,"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_debug(1,"cdr_odbc: Logging in GMT\n");
324                 } else {
325                         ast_debug(1,"cdr_odbc: Not logging in GMT\n");
326                 }
327         } else {
328                 ast_debug(1,"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         if (option_verbose > 2) {
345                 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn);
346                 if (username)
347                 {
348                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: username is %s\n",username);
349                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: password is [secret]\n");
350                 }
351                 else
352                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: retreiving username and password from odbc config\n");
353                 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: table is %s\n",table);
354         }
355         
356         res = odbc_init();
357         if (res < 0) {
358                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
359                 if (option_verbose > 2) {
360                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
361                 }
362         }
363         res = ast_cdr_register(name, ast_module_info->description, odbc_log);
364         if (res) {
365                 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
366         }
367 out:
368         if (cfg)
369                 ast_config_destroy(cfg);
370         ast_mutex_unlock(&odbc_lock);
371         return res;
372 }
373
374 static int odbc_do_query(void)
375 {
376         int ODBC_res;
377         
378         ODBC_res = SQLExecute(ODBC_stmt);
379         
380         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
381                 if (option_verbose > 10)
382                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
383                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
384                 odbc_disconnect();
385                 return -1;
386         } else {
387                 if (option_verbose > 10)
388                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
389                 connected = 1;
390         }
391         return 0;
392 }
393
394 static int odbc_init(void)
395 {
396         int ODBC_res;
397
398         if (ODBC_env == SQL_NULL_HANDLE || connected == 0) {
399                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
400                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
401                         if (option_verbose > 10)
402                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
403                         connected = 0;
404                         return -1;
405                 }
406
407                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
408
409                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
410                         if (option_verbose > 10)
411                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
412                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
413                         connected = 0;
414                         return -1;
415                 }
416
417                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
418
419                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
420                         if (option_verbose > 10)
421                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
422                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
423                         connected = 0;
424                         return -1;
425                 }
426                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
427         }
428
429         /* Note that the username and password could be NULL here, but that is allowed in ODBC.
430            In this case, the default username and password will be used from odbc.conf */
431         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
432
433         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
434                 if (option_verbose > 10)
435                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
436                 SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
437                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
438                 connected = 0;
439                 return -1;
440         } else {
441                 if (option_verbose > 10)
442                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
443                 connected = 1;
444         }
445         return 0;
446 }
447
448 static int load_module(void)
449 {
450         return odbc_load_module();
451 }
452
453 static int unload_module(void)
454 {
455         return odbc_unload_module();
456 }
457
458 static int reload(void)
459 {
460         odbc_unload_module();
461         return odbc_load_module();
462 }
463
464 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC CDR Backend",
465                 .load = load_module,
466                 .unload = unload_module,
467                 .reload = reload,
468                );