ODBC fix for PPC (bug #895)
[asterisk/asterisk.git] / cdr / cdr_odbc.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * ODBC CDR Backend
5  * 
6  * Brian K. West <brian@bkw.org>
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License.
10  *
11  * Copyright (c) 2003 Digium, Inc.
12  *
13  */
14
15 #include <sys/types.h>
16 #include <asterisk/config.h>
17 #include <asterisk/options.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/module.h>
21 #include <asterisk/logger.h>
22 #include "../asterisk.h"
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <time.h>
30
31 #include <sql.h>
32 #include <sqlext.h>
33 #include <sqltypes.h>
34
35 #define DATE_FORMAT "%Y-%m-%d %T"
36
37 static char *desc = "ODBC CDR Backend";
38 static char *name = "ODBC";
39 static char *config = "cdr_odbc.conf";
40 static char *dsn = NULL, *username = NULL, *password = NULL, *loguniqueid = NULL;
41 static int dsn_alloc = 0, username_alloc = 0, password_alloc = 0, loguniqueid_alloc = 0;
42 static int connected = 0;
43
44 static ast_mutex_t odbc_lock = AST_MUTEX_INITIALIZER;
45
46 static int odbc_do_query(void);
47 static int odbc_init(void);
48
49 static SQLHENV  ODBC_env = SQL_NULL_HANDLE;     /* global ODBC Environment */
50 static SQLHDBC  ODBC_con;                       /* global ODBC Connection Handle */
51 static SQLHSTMT ODBC_stmt;                      /* global ODBC Statement Handle */
52
53 static int odbc_log(struct ast_cdr *cdr)
54 {
55         long int ODBC_err;
56         short int ODBC_mlen;
57         int ODBC_res;
58         char ODBC_msg[200], ODBC_stat[10];
59         char sqlcmd[2048], timestr[128];
60         int res = 0;
61         struct tm tm;
62         struct timeval tv;
63         time_t t;
64
65         gettimeofday(&tv,NULL);
66         t = tv.tv_sec;
67         localtime_r(&t,&tm);
68
69         ast_mutex_lock(&odbc_lock);
70         strftime(timestr,128,DATE_FORMAT,&tm);
71         memset(sqlcmd,0,2048);
72         if((loguniqueid != NULL) && ((strcmp(loguniqueid, "1") == 0) || (strcmp(loguniqueid, "yes") == 0)))
73         {
74                 sprintf(sqlcmd,"INSERT INTO cdr "
75                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
76                 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
77                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
78         }
79         else
80         {
81                 sprintf(sqlcmd,"INSERT INTO cdr "
82                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
83                 "duration,billsec,disposition,amaflags,accountcode) "
84                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
85         }
86
87         if(!connected)
88         {
89                 res =  odbc_init();
90                 if(res < 0)
91                 {
92                         connected = 0;
93                         ast_mutex_unlock(&odbc_lock);
94                         return 0;
95                 }                               
96                 
97         }
98
99         ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt);
100
101         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
102         {
103                 if(option_verbose > 3)
104                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
105                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
106                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);      
107                 connected = 0;
108                 ast_mutex_unlock(&odbc_lock);
109                 return 0;
110         }
111
112         /* We really should only have to do this once.  But for some
113            strange reason if I don't it blows holes in memory like
114            like a shotgun.  So we just do this so its safe. */
115
116         ODBC_res = SQLPrepare(ODBC_stmt, sqlcmd, SQL_NTS);
117         
118         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
119         {
120                 if(option_verbose > 3)
121                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
122                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
123                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
124                 connected = 0;
125                 ast_mutex_unlock(&odbc_lock);
126                 return 0;
127         }
128
129         SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, &timestr, 0, NULL);
130         SQLBindParameter(ODBC_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->clid, 0, NULL);
131         SQLBindParameter(ODBC_stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->src, 0, NULL);
132         SQLBindParameter(ODBC_stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->dst, 0, NULL);
133         SQLBindParameter(ODBC_stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->dcontext, 0, NULL);
134         SQLBindParameter(ODBC_stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->channel, 0, NULL);
135         SQLBindParameter(ODBC_stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->dstchannel, 0, NULL);
136         SQLBindParameter(ODBC_stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->lastapp, 0, NULL);
137         SQLBindParameter(ODBC_stmt, 9, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->lastdata, 0, NULL);
138         SQLBindParameter(ODBC_stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
139         SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
140         SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
141         SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
142         SQLBindParameter(ODBC_stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->accountcode, 0, NULL);
143
144         if((loguniqueid != NULL) && ((strcmp(loguniqueid, "1") == 0) || (strcmp(loguniqueid, "yes") == 0)))
145         {
146                 SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->uniqueid, 0, NULL);
147                 SQLBindParameter(ODBC_stmt, 16, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, cdr->userfield, 0, NULL);
148         }
149
150         if(connected)
151         {
152                 res = odbc_do_query();
153                 if(res < 0)
154                 {
155                         if(option_verbose > 3)          
156                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
157                         res = odbc_init();
158                         if(option_verbose > 3)
159                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
160                         if(res < 0)
161                         {
162                                 if(option_verbose > 3)
163                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
164                                 connected = 0;
165                         }
166                         else
167                         {
168                                 if(option_verbose > 3)
169                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
170                                 res = odbc_do_query();
171                                 if(res < 0)
172                                 {
173                                         if(option_verbose > 3)
174                                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
175                                 }
176                         }
177                 }
178         }
179         else
180         {
181                 if(option_verbose > 3)
182                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
183         }
184         ast_mutex_unlock(&odbc_lock);
185         return 0;
186 }
187
188 char *description(void)
189 {
190         return desc;
191 }
192
193 static int odbc_unload_module(void)
194 {
195         ast_mutex_lock(&odbc_lock);
196         if (connected)
197         {
198                 if(option_verbose > 3)
199                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Disconnecting from %s\n", dsn);
200                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
201                 SQLDisconnect(ODBC_con);
202                 SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
203                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
204                 connected = 0;
205         }
206         if (dsn && dsn_alloc)
207         {
208                 if(option_verbose > 3)
209                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
210                 free(dsn);
211                 dsn = NULL;
212                 dsn_alloc = 0;
213         }
214         if (username && username_alloc)
215         {
216                 if(option_verbose > 3)
217                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
218                 free(username);
219                 username = NULL;
220                 username_alloc = 0;
221         }
222         if (password && password_alloc)
223         {
224                 if(option_verbose > 3)
225                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
226                 free(password);
227                 password = NULL;
228                 password_alloc = 0;
229         }
230         if (loguniqueid && loguniqueid_alloc)
231         {
232                 if(option_verbose > 3)
233                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free loguniqueid\n");
234                 free(loguniqueid);
235                 loguniqueid = NULL;
236                 loguniqueid_alloc = 0;
237         }
238         ast_cdr_unregister(name);
239         ast_mutex_unlock(&odbc_lock);
240         return 0;
241 }
242
243 static int odbc_load_module(void)
244 {
245         int res = 0;
246         struct ast_config *cfg;
247         struct ast_variable *var;
248         char *tmp;
249
250         ast_mutex_lock(&odbc_lock);
251
252         cfg = ast_load(config);
253         if (!cfg)
254         {
255                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
256                 goto out;
257         }
258         
259         var = ast_variable_browse(cfg, "global");
260         if (!var) {
261                 /* nothing configured */
262                 goto out;
263         }
264
265         tmp = ast_variable_retrieve(cfg,"global","dsn");
266         if (tmp)
267         {
268                 dsn = malloc(strlen(tmp) + 1);
269                 if (dsn != NULL)
270                 {
271                         dsn_alloc = 1;
272                         strcpy(dsn,tmp);
273                 }
274                 else
275                 {
276                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
277                         return -1;
278                 }
279         }
280         else
281         {
282                 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming asteriskdb\n");
283                 dsn = "asteriskdb";
284         }
285
286         tmp = ast_variable_retrieve(cfg,"global","username");
287         if (tmp)
288         {
289                 username = malloc(strlen(tmp) + 1);
290                 if (username != NULL)
291                 {
292                         username_alloc = 1;
293                         strcpy(username,tmp);
294                 }
295                 else
296                 {
297                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
298                         return -1;
299                 }
300         }
301         else
302         {
303                 ast_log(LOG_WARNING,"cdr_odbc: username not specified.  Assuming root\n");
304                 username = "root";
305         }
306
307         tmp = ast_variable_retrieve(cfg,"global","password");
308         if (tmp)
309         {
310                 password = malloc(strlen(tmp) + 1);
311                 if (password != NULL)
312                 {
313                         password_alloc = 1;
314                         strcpy(password,tmp);
315                 }
316                 else
317                 {
318                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
319                         return -1;
320                 }
321         }
322         else
323         {
324                 ast_log(LOG_WARNING,"cdr_odbc: database password not specified.  Assuming blank\n");
325                 password = "";
326         }
327
328         tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
329         if (tmp)
330         {
331                 loguniqueid = malloc(strlen(tmp) + 1);
332                 if (loguniqueid != NULL)
333                 {
334                         strcpy(loguniqueid,tmp);
335                         loguniqueid_alloc = 1;
336                         ast_log(LOG_NOTICE,"cdr_odbc: Logging uniqueid\n");
337                 }
338                 else
339                 {
340                         ast_log(LOG_ERROR,"cdr_odbc: Not logging uniqueid\n");
341                         loguniqueid_alloc = 1;
342                         loguniqueid = NULL;
343                 }
344         }
345         else
346         {
347                 ast_log(LOG_WARNING,"cdr_odbc: Not logging uniqueid\n");
348                 loguniqueid = NULL;
349         }
350
351         ast_destroy(cfg);
352         if(option_verbose > 3)
353         {
354                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: dsn is %s\n",dsn);
355                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: username is %s\n",username);
356                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: password is [secret]\n");
357
358         }
359         
360         res = odbc_init();
361         if(res < 0)
362         {
363                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
364                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
365         }
366
367         res = ast_cdr_register(name, desc, odbc_log);
368         if (res)
369         {
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         long int 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         {
388                 if(option_verbose > 3)
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         }
395         else
396         {
397                 if(option_verbose > 3)
398                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
399                 connected = 1;
400         }
401         return 0;
402 }
403
404 static int odbc_init(void)
405 {
406         long int ODBC_err;
407         short int ODBC_mlen;
408         int ODBC_res;
409         char ODBC_msg[200], ODBC_stat[10];
410
411         if ( ODBC_env == SQL_NULL_HANDLE || connected == 0 )
412         {
413                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
414
415                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
416                 {
417                         if(option_verbose > 3)
418                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
419                         connected = 0;
420                         return -1;
421                 }
422
423                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
424
425                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
426                 {
427                         if(option_verbose > 3)
428                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
429                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
430                         connected = 0;
431                         return -1;
432                 }
433
434                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
435
436                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
437                 {
438                         if(option_verbose > 3)
439                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
440                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
441                         connected = 0;
442                         return -1;
443                 }
444
445                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
446         }
447
448         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
449
450         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
451         {
452                 if(option_verbose > 3)
453                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
454                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
455                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
456                 connected = 0;
457                 return -1;
458         }
459         else
460         {
461                 if(option_verbose > 3)
462                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
463                 connected = 1;
464         }
465         return 0;
466 }
467
468 int load_module(void)
469 {
470         return odbc_load_module();
471 }
472
473 int unload_module(void)
474 {
475         return odbc_unload_module();
476 }
477
478 int reload(void)
479 {
480         odbc_unload_module();
481         return odbc_load_module();
482 }
483
484 int usecount(void)
485 {
486         return connected;
487 }
488
489 char *key()
490 {
491         return ASTERISK_GPL_KEY;
492 }