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