Brian's ODBC patches
[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;
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) "
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_SSHORT, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
139         SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
140         SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
141         SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SSHORT, 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         }
148
149         if(connected)
150         {
151                 res = odbc_do_query();
152                 if(res < 0)
153                 {
154                         if(option_verbose > 3)          
155                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
156                         res = odbc_init();
157                         if(option_verbose > 3)
158                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
159                         if(res < 0)
160                         {
161                                 if(option_verbose > 3)
162                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
163                                 connected = 0;
164                         }
165                         else
166                         {
167                                 if(option_verbose > 3)
168                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
169                                 res = odbc_do_query();
170                                 if(res < 0)
171                                 {
172                                         if(option_verbose > 3)
173                                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
174                                 }
175                         }
176                 }
177         }
178         else
179         {
180                 if(option_verbose > 3)
181                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
182         }
183         ast_mutex_unlock(&odbc_lock);
184         return 0;
185 }
186
187 char *description(void)
188 {
189         return desc;
190 }
191
192 static int odbc_unload_module(void)
193 {
194         ast_mutex_lock(&odbc_lock);
195         if (connected)
196         {
197                 if(option_verbose > 3)
198                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Disconnecting from %s\n", dsn);
199                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
200                 SQLDisconnect(ODBC_con);
201                 SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
202                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
203                 connected = 0;
204         }
205         if (dsn && dsn_alloc)
206         {
207                 if(option_verbose > 3)
208                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
209                 free(dsn);
210                 dsn = NULL;
211                 dsn_alloc = 0;
212         }
213         if (username && username_alloc)
214         {
215                 if(option_verbose > 3)
216                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
217                 free(username);
218                 username = NULL;
219                 username_alloc = 0;
220         }
221         if (password && password_alloc)
222         {
223                 if(option_verbose > 3)
224                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
225                 free(password);
226                 password = NULL;
227                 password_alloc = 0;
228         }
229         ast_cdr_unregister(name);
230         ast_mutex_unlock(&odbc_lock);
231         return 0;
232 }
233
234 static int odbc_load_module(void)
235 {
236         int res;
237         struct ast_config *cfg;
238         struct ast_variable *var;
239         char *tmp;
240
241         ast_mutex_lock(&odbc_lock);
242
243         cfg = ast_load(config);
244         if (!cfg)
245         {
246                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
247                 return 0;
248         }
249         
250         var = ast_variable_browse(cfg, "global");
251         if (!var) {
252                 /* nothing configured */
253                 return 0;
254         }
255
256         tmp = ast_variable_retrieve(cfg,"global","dsn");
257         if (tmp)
258         {
259                 dsn = malloc(strlen(tmp) + 1);
260                 if (dsn != NULL)
261                 {
262                         dsn_alloc = 1;
263                         strcpy(dsn,tmp);
264                 }
265                 else
266                 {
267                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
268                         return -1;
269                 }
270         }
271         else
272         {
273                 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming asteriskdb\n");
274                 dsn = "asteriskdb";
275         }
276
277         tmp = ast_variable_retrieve(cfg,"global","username");
278         if (tmp)
279         {
280                 username = malloc(strlen(tmp) + 1);
281                 if (username != NULL)
282                 {
283                         username_alloc = 1;
284                         strcpy(username,tmp);
285                 }
286                 else
287                 {
288                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
289                         return -1;
290                 }
291         }
292         else
293         {
294                 ast_log(LOG_WARNING,"cdr_odbc: username not specified.  Assuming root\n");
295                 username = "root";
296         }
297
298         tmp = ast_variable_retrieve(cfg,"global","password");
299         if (tmp)
300         {
301                 password = malloc(strlen(tmp) + 1);
302                 if (password != NULL)
303                 {
304                         password_alloc = 1;
305                         strcpy(password,tmp);
306                 }
307                 else
308                 {
309                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
310                         return -1;
311                 }
312         }
313         else
314         {
315                 ast_log(LOG_WARNING,"cdr_odbc: database password not specified.  Assuming blank\n");
316                 password = "";
317         }
318
319         tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
320         if (tmp)
321         {
322                 loguniqueid = malloc(strlen(tmp) + 1);
323                 if (loguniqueid != NULL)
324                 {
325                         strcpy(loguniqueid,tmp);
326                         ast_log(LOG_WARNING,"cdr_odbc: Logging uniqueid\n");
327                 }
328                 else
329                 {
330                         ast_log(LOG_ERROR,"cdr_odbc: Not logging uniqueid\n");
331                         loguniqueid = NULL;
332                 }
333         }
334         else
335         {
336                 ast_log(LOG_WARNING,"cdr_odbc: Not logging uniqueid\n");
337                 loguniqueid = NULL;
338         }
339
340         ast_destroy(cfg);
341         if(option_verbose > 3)
342         {
343                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: dsn is %s\n",dsn);
344                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: username is %s\n",username);
345                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: password is [secret]\n");
346
347         }
348         
349         res = odbc_init();
350         if(res < 0)
351         {
352                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
353                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
354         }
355
356         res = ast_cdr_register(name, desc, odbc_log);
357         if (res)
358         {
359                 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
360         }
361         ast_mutex_unlock(&odbc_lock);
362         return res;
363 }
364
365 static int odbc_do_query(void)
366 {
367         long int ODBC_err;
368         int ODBC_res;
369         short int ODBC_mlen;
370         char ODBC_msg[200], ODBC_stat[10];
371
372         ODBC_res = SQLExecute(ODBC_stmt);
373
374         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
375         {
376                 if(option_verbose > 3)
377                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
378                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
379                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
380                 connected = 0;
381                 return -1;
382         }
383         else
384         {
385                 if(option_verbose > 3)
386                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
387                 connected = 1;
388         }
389         return 0;
390 }
391
392 static int odbc_init(void)
393 {
394         long int ODBC_err;
395         short int ODBC_mlen;
396         int ODBC_res;
397         char ODBC_msg[200], ODBC_stat[10];
398
399         if ( ODBC_env == SQL_NULL_HANDLE || connected == 0 )
400         {
401                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
402
403                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
404                 {
405                         if(option_verbose > 3)
406                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
407                         connected = 0;
408                         return -1;
409                 }
410
411                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
412
413                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
414                 {
415                         if(option_verbose > 3)
416                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
417                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
418                         connected = 0;
419                         return -1;
420                 }
421
422                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
423
424                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
425                 {
426                         if(option_verbose > 3)
427                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
428                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
429                         connected = 0;
430                         return -1;
431                 }
432
433                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
434         }
435
436         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
437
438         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
439         {
440                 if(option_verbose > 3)
441                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
442                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
443                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
444                 connected = 0;
445                 return -1;
446         }
447         else
448         {
449                 if(option_verbose > 3)
450                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
451                 connected = 1;
452         }
453         return 0;
454 }
455
456 int load_module(void)
457 {
458         return odbc_load_module();
459 }
460
461 int unload_module(void)
462 {
463         return odbc_unload_module();
464 }
465
466 int reload(void)
467 {
468         odbc_unload_module();
469         return odbc_load_module();
470 }
471
472 int usecount(void)
473 {
474         return connected;
475 }
476
477 char *key()
478 {
479         return ASTERISK_GPL_KEY;
480 }