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