Fix missing ast_destroy's in PGSQL (bug #2944)
[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         if (conn)
146                 PQfinish(conn);
147         conn = NULL;
148         connected = 0;
149         if (pghostname && hostname_alloc) {
150                 free(pghostname);
151                 pghostname = NULL;
152                 hostname_alloc = 0;
153         }
154         if (pgdbname && dbname_alloc) {
155                 free(pgdbname);
156                 pgdbname = NULL;
157                 dbname_alloc = 0;
158         }
159         if (pgdbuser && dbuser_alloc) {
160                 free(pgdbuser);
161                 pgdbuser = NULL;
162                 dbuser_alloc = 0;
163         }
164         if (pgdbsock && dbsock_alloc) {
165                 free(pgdbsock);
166                 pgdbsock = NULL;
167                 dbsock_alloc = 0;
168         }
169         if (pgpassword && password_alloc) {
170                 free(pgpassword);
171                 pgpassword = NULL;
172                 password_alloc = 0;
173         }
174         if (pgdbport && dbport_alloc) {
175                 free(pgdbport);
176                 pgdbport = NULL;
177                 dbport_alloc = 0;
178         }
179         ast_cdr_unregister(name);
180         return 0;
181 }
182
183 static int process_my_load_module(struct ast_config *cfg)
184 {
185         int res;
186         struct ast_variable *var;
187         char *pgerror;
188         char *tmp;
189
190         var = ast_variable_browse(cfg, "global");
191         if (!var) {
192                 /* nothing configured */
193                 return 0;
194         }
195
196         tmp = ast_variable_retrieve(cfg,"global","hostname");
197         if (tmp) {
198                 pghostname = malloc(strlen(tmp) + 1);
199                 if (pghostname != NULL) {
200                         memset(pghostname, 0, strlen(tmp) + 1);
201                         hostname_alloc = 1;
202                         strncpy(pghostname, tmp, strlen(tmp));
203                 } else {
204                         ast_log(LOG_ERROR,"Out of memory error.\n");
205                         return -1;
206                 }
207         } else {
208                 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming localhost\n");
209                 pghostname = "localhost";
210         }
211
212         tmp = ast_variable_retrieve(cfg,"global","dbname");
213         if (tmp) {
214                 pgdbname = malloc(strlen(tmp) + 1);
215                 if (pgdbname != NULL) {
216                         memset(pgdbname, 0, strlen(tmp) + 1);
217                         dbname_alloc = 1;
218                         strncpy(pgdbname, tmp, strlen(tmp));
219                 } else {
220                         ast_log(LOG_ERROR,"Out of memory error.\n");
221                         return -1;
222                 }
223         } else {
224                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
225                 pgdbname = "asteriskcdrdb";
226         }
227
228         tmp = ast_variable_retrieve(cfg,"global","user");
229         if (tmp) {
230                 pgdbuser = malloc(strlen(tmp) + 1);
231                 if (pgdbuser != NULL) {
232                         memset(pgdbuser, 0, strlen(tmp) + 1);
233                         dbuser_alloc = 1;
234                         strncpy(pgdbuser, tmp, strlen(tmp));
235                 } else {
236                         ast_log(LOG_ERROR,"Out of memory error.\n");
237                         return -1;
238                 }
239         } else {
240                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming root\n");
241                 pgdbuser = "root";
242         }
243
244         tmp = ast_variable_retrieve(cfg,"global","password");
245         if (tmp) {
246                 pgpassword = malloc(strlen(tmp) + 1);
247                 if (pgpassword != NULL) {
248                         memset(pgpassword, 0, strlen(tmp) + 1);
249                         password_alloc = 1;
250                         strncpy(pgpassword, tmp, strlen(tmp));
251                 } else {
252                         ast_log(LOG_ERROR,"Out of memory error.\n");
253                         return -1;
254                 }
255         } else {
256                 ast_log(LOG_WARNING,"PostgreSQL database password not specified.  Assuming blank\n");
257                 pgpassword = "";
258         }
259
260         tmp = ast_variable_retrieve(cfg,"global","port");
261         if (tmp) {
262                 pgdbport = malloc(strlen(tmp) + 1);
263                 if (pgdbport != NULL) {
264                         memset(pgdbport, 0, strlen(tmp) + 1);
265                         dbport_alloc = 1;
266                         strncpy(pgdbport, tmp, strlen(tmp));
267                 } else {
268                         ast_log(LOG_ERROR,"Out of memory error.\n");
269                         return -1;
270                 }
271         } else {
272                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
273                 pgdbport = "5432";
274         }
275
276         ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
277         ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
278         if (pgdbsock)
279                 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
280         ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
281         ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
282         ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
283
284         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
285         if (PQstatus(conn) != CONNECTION_BAD) {
286                 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
287                 connected = 1;
288         } else {
289                 pgerror = PQerrorMessage(conn);
290                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
291                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
292                 connected = 0;
293         }
294
295         res = ast_cdr_register(name, desc, pgsql_log);
296         if (res) {
297                 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
298         }
299         return res;
300 }
301
302 static int my_load_module(void)
303 {
304         struct ast_config *cfg;
305         int res;
306         cfg = ast_load(config);
307         if (!cfg) {
308                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
309                 return 0;
310         }
311         res = process_my_load_module(cfg);
312         ast_destroy(cfg);
313         return res;
314 }
315
316 int load_module(void)
317 {
318         return my_load_module();
319 }
320
321 int unload_module(void)
322 {
323         return my_unload_module();
324 }
325
326 int reload(void)
327 {
328         my_unload_module();
329         return my_load_module();
330 }
331
332 int usecount(void)
333 {
334         return connected;
335 }
336
337 char *key()
338 {
339         return ASTERISK_GPL_KEY;
340 }