Merge remaining audit patch (save dlfcn.c)
[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 AST_MUTEX_DEFINE_STATIC(odbc_lock);
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
63         localtime_r(&cdr->start.tv_sec,&tm);
64
65         ast_mutex_lock(&odbc_lock);
66         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
67         memset(sqlcmd,0,2048);
68         if((loguniqueid != NULL) && ((strcmp(loguniqueid, "1") == 0) || (strcmp(loguniqueid, "yes") == 0)))
69         {
70                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO cdr "
71                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
72                 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
73                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
74         }
75         else
76         {
77                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO cdr "
78                 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
79                 "duration,billsec,disposition,amaflags,accountcode) "
80                 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
81         }
82
83         if(!connected)
84         {
85                 res =  odbc_init();
86                 if(res < 0)
87                 {
88                         connected = 0;
89                         ast_mutex_unlock(&odbc_lock);
90                         return 0;
91                 }                               
92                 
93         }
94
95         ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt);
96
97         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
98         {
99                 if(option_verbose > 10)
100                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
101                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
102                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);      
103                 connected = 0;
104                 ast_mutex_unlock(&odbc_lock);
105                 return 0;
106         }
107
108         /* We really should only have to do this once.  But for some
109            strange reason if I don't it blows holes in memory like
110            like a shotgun.  So we just do this so its safe. */
111
112         ODBC_res = SQLPrepare(ODBC_stmt, sqlcmd, SQL_NTS);
113         
114         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
115         {
116                 if(option_verbose > 10)
117                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
118                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
119                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
120                 connected = 0;
121                 ast_mutex_unlock(&odbc_lock);
122                 return 0;
123         }
124
125         SQLBindParameter(ODBC_stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(timestr), 0, &timestr, 0, NULL);
126         SQLBindParameter(ODBC_stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
127         SQLBindParameter(ODBC_stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
128         SQLBindParameter(ODBC_stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
129         SQLBindParameter(ODBC_stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
130         SQLBindParameter(ODBC_stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
131         SQLBindParameter(ODBC_stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
132         SQLBindParameter(ODBC_stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
133         SQLBindParameter(ODBC_stmt, 9, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
134         SQLBindParameter(ODBC_stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
135         SQLBindParameter(ODBC_stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
136         SQLBindParameter(ODBC_stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
137         SQLBindParameter(ODBC_stmt, 13, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
138         SQLBindParameter(ODBC_stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
139
140         if((loguniqueid != NULL) && ((strcmp(loguniqueid, "1") == 0) || (strcmp(loguniqueid, "yes") == 0)))
141         {
142                 SQLBindParameter(ODBC_stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
143                 SQLBindParameter(ODBC_stmt, 16, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
144         }
145
146         if(connected)
147         {
148                 res = odbc_do_query();
149                 if(res < 0)
150                 {
151                         if(option_verbose > 10)         
152                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
153                         res = odbc_init();
154                         if(option_verbose > 10)
155                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
156                         if(res < 0)
157                         {
158                                 if(option_verbose > 10)
159                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
160                                 connected = 0;
161                         }
162                         else
163                         {
164                                 if(option_verbose > 10)
165                                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
166                                 res = odbc_do_query();
167                                 if(res < 0)
168                                 {
169                                         if(option_verbose > 10)
170                                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
171                                 }
172                         }
173                 }
174         }
175         else
176         {
177                 if(option_verbose > 10)
178                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
179         }
180         ast_mutex_unlock(&odbc_lock);
181         return 0;
182 }
183
184 char *description(void)
185 {
186         return desc;
187 }
188
189 static int odbc_unload_module(void)
190 {
191         ast_mutex_lock(&odbc_lock);
192         if (connected)
193         {
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                 connected = 0;
201         }
202         if (dsn && dsn_alloc)
203         {
204                 if(option_verbose > 10)
205                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
206                 free(dsn);
207                 dsn = NULL;
208                 dsn_alloc = 0;
209         }
210         if (username && username_alloc)
211         {
212                 if(option_verbose > 10)
213                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free username\n");
214                 free(username);
215                 username = NULL;
216                 username_alloc = 0;
217         }
218         if (password && password_alloc)
219         {
220                 if(option_verbose > 10)
221                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free password\n");
222                 free(password);
223                 password = NULL;
224                 password_alloc = 0;
225         }
226         if (loguniqueid && loguniqueid_alloc)
227         {
228                 if(option_verbose > 10)
229                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free loguniqueid\n");
230                 free(loguniqueid);
231                 loguniqueid = NULL;
232                 loguniqueid_alloc = 0;
233         }
234         ast_cdr_unregister(name);
235         ast_mutex_unlock(&odbc_lock);
236         return 0;
237 }
238
239 static int odbc_load_module(void)
240 {
241         int res = 0;
242         struct ast_config *cfg;
243         struct ast_variable *var;
244         char *tmp;
245
246         ast_mutex_lock(&odbc_lock);
247
248         cfg = ast_load(config);
249         if (!cfg)
250         {
251                 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config);
252                 goto out;
253         }
254         
255         var = ast_variable_browse(cfg, "global");
256         if (!var) {
257                 /* nothing configured */
258                 goto out;
259         }
260
261         tmp = ast_variable_retrieve(cfg,"global","dsn");
262         if (tmp)
263         {
264                 dsn = malloc(strlen(tmp) + 1);
265                 if (dsn != NULL)
266                 {
267                         memset(dsn, 0, strlen(tmp) + 1);
268                         dsn_alloc = 1;
269                         strncpy(dsn, tmp, strlen(tmp));
270                 }
271                 else
272                 {
273                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
274                         return -1;
275                 }
276         }
277         else
278         {
279                 ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming asteriskdb\n");
280                 dsn = "asteriskdb";
281         }
282
283         tmp = ast_variable_retrieve(cfg,"global","username");
284         if (tmp)
285         {
286                 username = malloc(strlen(tmp) + 1);
287                 if (username != NULL)
288                 {
289                         memset(username, 0, strlen(tmp) + 1);
290                         username_alloc = 1;
291                         strncpy(username, tmp, strlen(tmp));
292                 }
293                 else
294                 {
295                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
296                         return -1;
297                 }
298         }
299         else
300         {
301                 ast_log(LOG_WARNING,"cdr_odbc: username not specified.  Assuming root\n");
302                 username = "root";
303         }
304
305         tmp = ast_variable_retrieve(cfg,"global","password");
306         if (tmp)
307         {
308                 password = malloc(strlen(tmp) + 1);
309                 if (password != NULL)
310                 {
311                         memset(password, 0, strlen(tmp) + 1);
312                         password_alloc = 1;
313                         strncpy(password, tmp, strlen(tmp));
314                 }
315                 else
316                 {
317                         ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
318                         return -1;
319                 }
320         }
321         else
322         {
323                 ast_log(LOG_WARNING,"cdr_odbc: database password not specified.  Assuming blank\n");
324                 password = "";
325         }
326
327         tmp = ast_variable_retrieve(cfg,"global","loguniqueid");
328         if (tmp)
329         {
330                 loguniqueid = malloc(strlen(tmp) + 1);
331                 if (loguniqueid != NULL)
332                 {
333                         strcpy(loguniqueid,tmp);
334                         loguniqueid_alloc = 1;
335                         ast_log(LOG_NOTICE,"cdr_odbc: Logging uniqueid\n");
336                 }
337                 else
338                 {
339                         ast_log(LOG_ERROR,"cdr_odbc: Not logging uniqueid\n");
340                         loguniqueid_alloc = 1;
341                         loguniqueid = NULL;
342                 }
343         }
344         else
345         {
346                 ast_log(LOG_WARNING,"cdr_odbc: Not logging uniqueid\n");
347                 loguniqueid = NULL;
348         }
349
350         ast_destroy(cfg);
351         if(option_verbose > 3)
352         {
353                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: dsn is %s\n",dsn);
354                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: username is %s\n",username);
355                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: password is [secret]\n");
356
357         }
358         
359         res = odbc_init();
360         if(res < 0)
361         {
362                 ast_log(LOG_ERROR, "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
363                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Unable to connect to datasource: %s\n", dsn);
364         }
365
366         res = ast_cdr_register(name, desc, odbc_log);
367         if (res)
368         {
369                 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
370         }
371 out:
372         ast_mutex_unlock(&odbc_lock);
373         return res;
374 }
375
376 static int odbc_do_query(void)
377 {
378         long int ODBC_err;
379         int ODBC_res;
380         short int ODBC_mlen;
381         char ODBC_msg[200], ODBC_stat[10];
382
383         ODBC_res = SQLExecute(ODBC_stmt);
384
385         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
386         {
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, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
390                 SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
391                 connected = 0;
392                 return -1;
393         }
394         else
395         {
396                 if(option_verbose > 10)
397                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query Successful!\n");
398                 connected = 1;
399         }
400         return 0;
401 }
402
403 static int odbc_init(void)
404 {
405         long int ODBC_err;
406         short int ODBC_mlen;
407         int ODBC_res;
408         char ODBC_msg[200], ODBC_stat[10];
409
410         if ( ODBC_env == SQL_NULL_HANDLE || connected == 0 )
411         {
412                 ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
413
414                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
415                 {
416                         if(option_verbose > 10)
417                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
418                         connected = 0;
419                         return -1;
420                 }
421
422                 ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
423
424                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
425                 {
426                         if(option_verbose > 10)
427                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
428                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
429                         connected = 0;
430                         return -1;
431                 }
432
433                 ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
434
435                 if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
436                 {
437                         if(option_verbose > 10)
438                                 ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
439                         SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
440                         connected = 0;
441                         return -1;
442                 }
443
444                 SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0);    
445         }
446
447         ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
448
449         if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO))
450         {
451                 if(option_verbose > 10)
452                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
453                 SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen);
454                 SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
455                 connected = 0;
456                 return -1;
457         }
458         else
459         {
460                 if(option_verbose > 10)
461                         ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Connected to %s\n", dsn);
462                 connected = 1;
463         }
464         return 0;
465 }
466
467 int load_module(void)
468 {
469         return odbc_load_module();
470 }
471
472 int unload_module(void)
473 {
474         return odbc_unload_module();
475 }
476
477 int reload(void)
478 {
479         odbc_unload_module();
480         return odbc_load_module();
481 }
482
483 int usecount(void)
484 {
485         return connected;
486 }
487
488 char *key()
489 {
490         return ASTERISK_GPL_KEY;
491 }