Merge remaining audit patch (save dlfcn.c)
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * PostgreSQL CDR logger 
5  *
6  * Matthew D. Hardeman <mhardemn@papersoft.com> 
7  * Adapted from the MySQL CDR logger originally by James Sharp 
8  *
9  * Modified September 2003
10  * Matthew D. Hardeman <mhardemn@papersoft.com>
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License.
14  *
15  */
16
17 #include <sys/types.h>
18 #include <asterisk/config.h>
19 #include <asterisk/options.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/cdr.h>
22 #include <asterisk/module.h>
23 #include <asterisk/logger.h>
24 #include "../asterisk.h"
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <time.h>
32
33 #include <libpq-fe.h>
34
35 #define DATE_FORMAT "%Y-%m-%d %T"
36
37 static char *desc = "PostgreSQL CDR Backend";
38 static char *name = "pgsql";
39 static char *config = "cdr_pgsql.conf";
40 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL;
41 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0;
42 static int connected = 0;
43
44 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
45
46 PGconn          *conn;
47 PGresult        *result;
48
49 static int pgsql_log(struct ast_cdr *cdr)
50 {
51         struct tm tm;
52         char sqlcmd[2048] = "", timestr[128];
53         char *pgerror;
54
55         ast_mutex_lock(&pgsql_lock);
56
57         localtime_r(&cdr->start.tv_sec,&tm);
58         strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
59
60         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
61                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
62                 if (PQstatus(conn) != CONNECTION_BAD) {
63                         connected = 1;
64                 } else {
65                         pgerror = PQerrorMessage(conn);
66                         ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
67                         ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
68                 }
69         }
70
71         if (connected) {
72                 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
73                 char *uniqueid=NULL, *userfield=NULL;
74
75                 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
76                 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
77                         PQescapeString(clid, cdr->clid, strlen(cdr->clid));
78                 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
79                         PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
80                 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
81                         PQescapeString(channel, cdr->channel, strlen(cdr->channel));
82                 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
83                         PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
84                 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
85                         PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
86                 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
87                         PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
88                 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
89                         PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
90                 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
91                         PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
92
93                 /* Check for all alloca failures above at once */
94                 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
95                         ast_log(LOG_ERROR, "cdr_pgsql:  Out of memory error (insert fails)\n");
96                         ast_mutex_unlock(&pgsql_lock);
97                         return -1;
98                 }
99
100                 ast_log(LOG_DEBUG,"cdr_pgsql: inserting a CDR record.\n");
101
102                 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO cdr (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')",timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
103                 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command executed:  %s\n",sqlcmd);
104         
105                 /* Test to be sure we're still connected... */
106                 /* If we're connected, and connection is working, good. */
107                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
108                 if (PQstatus(conn) == CONNECTION_OK) {
109                         connected = 1;
110                 } else {
111                         ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
112                         PQreset(conn);
113                         if (PQstatus(conn) == CONNECTION_OK) {
114                                 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
115                                 connected = 1;
116                         } else {
117                                 pgerror = PQerrorMessage(conn);
118                                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
119                                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
120                                 connected = 0;
121                                 ast_mutex_unlock(&pgsql_lock);
122                                 return -1;
123                         }
124                 }
125                 result = PQexec(conn, sqlcmd);
126                 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
127                         pgerror = PQresultErrorMessage(result);
128                         ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
129                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
130                         ast_mutex_unlock(&pgsql_lock);
131                         return -1;
132                 }
133         }
134         ast_mutex_unlock(&pgsql_lock);
135         return 0;
136 }
137
138 char *description(void)
139 {
140         return desc;
141 }
142
143 static int my_unload_module(void)
144
145         PQfinish(conn);
146         connected = 0;
147         if (pghostname && hostname_alloc) {
148                 free(pghostname);
149                 pghostname = NULL;
150                 hostname_alloc = 0;
151         }
152         if (pgdbname && dbname_alloc) {
153                 free(pgdbname);
154                 pgdbname = NULL;
155                 dbname_alloc = 0;
156         }
157         if (pgdbuser && dbuser_alloc) {
158                 free(pgdbuser);
159                 pgdbuser = NULL;
160                 dbuser_alloc = 0;
161         }
162         if (pgdbsock && dbsock_alloc) {
163                 free(pgdbsock);
164                 pgdbsock = NULL;
165                 dbsock_alloc = 0;
166         }
167         if (pgpassword && password_alloc) {
168                 free(pgpassword);
169                 pgpassword = NULL;
170                 password_alloc = 0;
171         }
172         if (pgdbport && dbport_alloc) {
173                 free(pgdbport);
174                 pgdbport = NULL;
175                 dbport_alloc = 0;
176         }
177         ast_cdr_unregister(name);
178         return 0;
179 }
180
181 static int my_load_module(void)
182 {
183         int res;
184         struct ast_config *cfg;
185         struct ast_variable *var;
186         char *pgerror;
187         char *tmp;
188
189         cfg = ast_load(config);
190         if (!cfg) {
191                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
192                 return 0;
193         }
194         
195         var = ast_variable_browse(cfg, "global");
196         if (!var) {
197                 /* nothing configured */
198                 return 0;
199         }
200
201         tmp = ast_variable_retrieve(cfg,"global","hostname");
202         if (tmp) {
203                 pghostname = malloc(strlen(tmp) + 1);
204                 if (pghostname != NULL) {
205                         memset(pghostname, 0, strlen(tmp) + 1);
206                         hostname_alloc = 1;
207                         strncpy(pghostname, tmp, strlen(tmp));
208                 } else {
209                         ast_log(LOG_ERROR,"Out of memory error.\n");
210                         return -1;
211                 }
212         } else {
213                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming localhost\n");
214                 pghostname = "localhost";
215         }
216
217         tmp = ast_variable_retrieve(cfg,"global","dbname");
218         if (tmp) {
219                 pgdbname = malloc(strlen(tmp) + 1);
220                 if (pgdbname != NULL) {
221                         memset(pgdbname, 0, strlen(tmp) + 1);
222                         dbname_alloc = 1;
223                         strncpy(pgdbname, tmp, strlen(tmp));
224                 } else {
225                         ast_log(LOG_ERROR,"Out of memory error.\n");
226                         return -1;
227                 }
228         } else {
229                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
230                 pgdbname = "asteriskcdrdb";
231         }
232
233         tmp = ast_variable_retrieve(cfg,"global","user");
234         if (tmp) {
235                 pgdbuser = malloc(strlen(tmp) + 1);
236                 if (pgdbuser != NULL) {
237                         memset(pgdbuser, 0, strlen(tmp) + 1);
238                         dbuser_alloc = 1;
239                         strncpy(pgdbuser, tmp, strlen(tmp));
240                 } else {
241                         ast_log(LOG_ERROR,"Out of memory error.\n");
242                         return -1;
243                 }
244         } else {
245                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming root\n");
246                 pgdbuser = "root";
247         }
248
249         tmp = ast_variable_retrieve(cfg,"global","password");
250         if (tmp) {
251                 pgpassword = malloc(strlen(tmp) + 1);
252                 if (pgpassword != NULL) {
253                         memset(pgpassword, 0, strlen(tmp) + 1);
254                         password_alloc = 1;
255                         strncpy(pgpassword, tmp, strlen(tmp));
256                 } else {
257                         ast_log(LOG_ERROR,"Out of memory error.\n");
258                         return -1;
259                 }
260         } else {
261                 ast_log(LOG_WARNING,"PostgreSQL database password not specified.  Assuming blank\n");
262                 pgpassword = "";
263         }
264
265         tmp = ast_variable_retrieve(cfg,"global","port");
266         if (tmp) {
267                 pgdbport = malloc(strlen(tmp) + 1);
268                 if (pgdbport != NULL) {
269                         memset(pgdbport, 0, strlen(tmp) + 1);
270                         dbport_alloc = 1;
271                         strncpy(pgdbport, tmp, strlen(tmp));
272                 } else {
273                         ast_log(LOG_ERROR,"Out of memory error.\n");
274                         return -1;
275                 }
276         } else {
277                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
278                 pgdbport = "5432";
279         }
280
281         ast_destroy(cfg);
282
283         ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
284         ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
285         if (pgdbsock)
286                 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
287         ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
288         ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
289         ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
290
291         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
292         if (PQstatus(conn) != CONNECTION_BAD) {
293                 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
294                 connected = 1;
295         } else {
296                 pgerror = PQerrorMessage(conn);
297                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
298                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
299                 connected = 0;
300         }
301
302         res = ast_cdr_register(name, desc, pgsql_log);
303         if (res) {
304                 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
305         }
306         return res;
307 }
308
309 int load_module(void)
310 {
311         return my_load_module();
312 }
313
314 int unload_module(void)
315 {
316         return my_unload_module();
317 }
318
319 int reload(void)
320 {
321         my_unload_module();
322         return my_load_module();
323 }
324
325 int usecount(void)
326 {
327         return connected;
328 }
329
330 char *key()
331 {
332         return ASTERISK_GPL_KEY;
333 }