update dependency information to match new names for dependencies
[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  ***/
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include <sys/types.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <time.h>
46
47 #ifndef __CYGWIN__
48 #include <sql.h>
49 #include <sqlext.h>
50 #include <sqltypes.h>
51 #else
52 #include <windows.h>
53 #include <w32api/sql.h>
54 #include <w32api/sqlext.h>
55 #include <w32api/sqltypes.h>
56 #endif
57
58 #include "asterisk/config.h"
59 #include "asterisk/options.h"
60 #include "asterisk/channel.h"
61 #include "asterisk/cdr.h"
62 #include "asterisk/module.h"
63 #include "asterisk/logger.h"
64
65 #define DATE_FORMAT "%Y-%m-%d %T"
66
67 static char *desc = "ODBC CDR Backend";
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                 localtime_r(&cdr->start.tv_sec,&tm);
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 const char *description(void)
211 {
212         return desc;
213 }
214
215 static int odbc_unload_module(void)
216 {
217         ast_mutex_lock(&odbc_lock);
218         if (connected) {
219                 if (option_verbose > 10)
220                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Disconnecting from %s\n", dsn);
221                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
222                 odbc_disconnect();
223         }
224         if (dsn) {
225                 if (option_verbose > 10)
226                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
227                 free(dsn);
228         }
229         if (username) {
230                 if (option_verbose > 10)
231                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
232                 free(username);
233         }
234         if (password) {
235                 if (option_verbose > 10)
236                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
237                 free(password);
238         }
239         if (table) {
240                 if (option_verbose > 10)
241                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free table\n");
242                 free(table);
243         }
244
245         ast_cdr_unregister(name);
246         ast_mutex_unlock(&odbc_lock);
247         return 0;
248 }
249
250 static int odbc_load_module(void)
251 {
252         int res = 0;
253         struct ast_config *cfg;
254         struct ast_variable *var;
255         char *tmp;
256
257         ast_mutex_lock(&odbc_lock);
258
259         cfg = ast_config_load(config);
260         if (!cfg) {
261                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
262                 goto out;
263         }
264         
265         var = ast_variable_browse(cfg, "global");
266         if (!var) {
267                 /* nothing configured */
268                 goto out;
269         }
270
271         tmp = ast_variable_retrieve(cfg,"global","dsn");
272         if (tmp == NULL) {
273                 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming asteriskdb\n");
274                 tmp = "asteriskdb";
275         }
276         dsn = strdup(tmp);
277         if (dsn == NULL) {
278                 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
279                 res = -1;
280                 goto out;
281         }
282
283         tmp = ast_variable_retrieve(cfg,"global","dispositionstring");
284         if (tmp) {
285                 dispositionstring = ast_true(tmp);
286         } else {
287                 dispositionstring = 0;
288         }
289                 
290         tmp = ast_variable_retrieve(cfg,"global","username");
291         if (tmp) {
292                 username = strdup(tmp);
293                 if (username == NULL) {
294                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
295                         res = -1;
296                         goto out;
297                 }
298         }
299
300         tmp = ast_variable_retrieve(cfg,"global","password");
301         if (tmp) {
302                 password = strdup(tmp);
303                 if (password == NULL) {
304                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
305                         res = -1;
306                         goto out;
307                 }
308         }
309
310         tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
311         if (tmp) {
312                 loguniqueid = ast_true(tmp);
313                 if (loguniqueid) {
314                         ast_log(LOG_DEBUG,"cdr_odbc: Logging uniqueid\n");
315                 } else {
316                         ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n");
317                 }
318         } else {
319                 ast_log(LOG_DEBUG,"cdr_odbc: Not logging uniqueid\n");
320                 loguniqueid = 0;
321         }
322
323         tmp = ast_variable_retrieve(cfg,"global","usegmtime");
324         if (tmp) {
325                 usegmtime = ast_true(tmp);
326                 if (usegmtime) {
327                         ast_log(LOG_DEBUG,"cdr_odbc: Logging in GMT\n");
328                 } else {
329                         ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n");
330                 }
331         } else {
332                 ast_log(LOG_DEBUG,"cdr_odbc: Not logging in GMT\n");
333                 usegmtime = 0;
334         }
335
336         tmp = ast_variable_retrieve(cfg,"global","table");
337         if (tmp == NULL) {
338                 ast_log(LOG_WARNING,"cdr_odbc: table not specified.  Assuming cdr\n");
339                 tmp = "cdr";
340         }
341         table = strdup(tmp);
342         if (table == NULL) {
343                 ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
344                 res = -1;
345                 goto out;
346         }
347
348         ast_config_destroy(cfg);
349         if (option_verbose > 2) {
350                 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: dsn is %s\n",dsn);
351                 if (username)
352                 {
353                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: username is %s\n",username);
354                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: password is [secret]\n");
355                 }
356                 else
357                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: retreiving username and password from odbc config\n");
358                 ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: table is %s\n",table);
359         }
360         
361         res = odbc_init();
362         if (res < 0) {
363                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
364                 if (option_verbose > 2) {
365                         ast_verbose( VERBOSE_PREFIX_3 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
366                 }
367         }
368         res = ast_cdr_register(name, desc, odbc_log);
369         if (res) {
370                 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
371         }
372 out:
373         ast_mutex_unlock(&odbc_lock);
374         return res;
375 }
376
377 static int odbc_do_query(void)
378 {
379         SQLINTEGER ODBC_err;
380         int ODBC_res;
381         short int ODBC_mlen;
382         char ODBC_msg[200], ODBC_stat[10];
383         
384         ODBC_res = SQLExecute(ODBC_stmt);
385         
386         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
387                 if (option_verbose > 10)
388                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
389                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, (unsigned char *)ODBC_stat, &ODBC_err, (unsigned char *)ODBC_msg, 100, &ODBC_mlen);
390                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
391                 odbc_disconnect();
392                 return -1;
393         } else {
394                 if (option_verbose > 10)
395                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
396                 connected = 1;
397         }
398         return 0;
399 }
400
401 static int odbc_init(void)
402 {
403         int ODBC_res;
404
405         if (ODBC_env == SQL_NULL_HANDLE || connected == 0) {
406                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
407                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
408                         if (option_verbose > 10)
409                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
410                         connected = 0;
411                         return -1;
412                 }
413
414                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
415
416                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
417                         if (option_verbose > 10)
418                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
419                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
420                         connected = 0;
421                         return -1;
422                 }
423
424                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
425
426                 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
427                         if (option_verbose > 10)
428                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
429                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
430                         connected = 0;
431                         return -1;
432                 }
433                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
434         }
435
436         /* Note that the username and password could be NULL here, but that is allowed in ODBC.
437            In this case, the default username and password will be used from odbc.conf */
438         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
439
440         if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
441                 if (option_verbose > 10)
442                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
443                 SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
444                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
445                 connected = 0;
446                 return -1;
447         } else {
448                 if (option_verbose > 10)
449                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
450                 connected = 1;
451         }
452         return 0;
453 }
454
455 static int load_module(void *mod)
456 {
457         return odbc_load_module();
458 }
459
460 static int unload_module(void *mod)
461 {
462         return odbc_unload_module();
463 }
464
465 static int reload(void *mod)
466 {
467         odbc_unload_module();
468         return odbc_load_module();
469 }
470
471 static const char *key(void)
472 {
473         return ASTERISK_GPL_KEY;
474 }
475
476 STD_MOD(MOD_0, reload, NULL, NULL);