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