Fix reconnecting to pgsql database after connection loss.
[asterisk/asterisk.git] / res / res_config_pgsql.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Copyright (C) 1999-2010, Digium, Inc.
5  *
6  * Manuel Guesdon <mguesdon@oxymium.net> - PostgreSQL RealTime Driver Author/Adaptor
7  * Mark Spencer <markster@digium.com>  - Asterisk Author
8  * Matthew Boehm <mboehm@cytelcom.com> - MySQL RealTime Driver Author
9  *
10  * res_config_pgsql.c <PostgreSQL plugin for RealTime configuration engine>
11  *
12  * v1.0   - (07-11-05) - Initial version based on res_config_mysql v2.0
13  */
14
15 /*! \file
16  *
17  * \brief PostgreSQL plugin for Asterisk RealTime Architecture
18  *
19  * \author Mark Spencer <markster@digium.com>
20  * \author Manuel Guesdon <mguesdon@oxymium.net> - PostgreSQL RealTime Driver Author/Adaptor
21  *
22  * \extref PostgreSQL http://www.postgresql.org
23  */
24
25 /*** MODULEINFO
26         <depend>pgsql</depend>
27         <support_level>extended</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <libpq-fe.h>                   /* PostgreSQL */
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/config.h"
40 #include "asterisk/module.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/cli.h"
44
45 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
46 AST_THREADSTORAGE(sql_buf);
47 AST_THREADSTORAGE(findtable_buf);
48 AST_THREADSTORAGE(where_buf);
49 AST_THREADSTORAGE(escapebuf_buf);
50 AST_THREADSTORAGE(semibuf_buf);
51
52 #define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
53
54 static PGconn *pgsqlConn = NULL;
55 static int version;
56 #define has_schema_support      (version > 70300 ? 1 : 0)
57
58 #define MAX_DB_OPTION_SIZE 64
59
60 struct columns {
61         char *name;
62         char *type;
63         int len;
64         unsigned int notnull:1;
65         unsigned int hasdefault:1;
66         AST_LIST_ENTRY(columns) list;
67 };
68
69 struct tables {
70         ast_rwlock_t lock;
71         AST_LIST_HEAD_NOLOCK(psql_columns, columns) columns;
72         AST_LIST_ENTRY(tables) list;
73         char name[0];
74 };
75
76 static AST_LIST_HEAD_STATIC(psql_tables, tables);
77
78 static char dbhost[MAX_DB_OPTION_SIZE] = "";
79 static char dbuser[MAX_DB_OPTION_SIZE] = "";
80 static char dbpass[MAX_DB_OPTION_SIZE] = "";
81 static char dbname[MAX_DB_OPTION_SIZE] = "";
82 static char dbsock[MAX_DB_OPTION_SIZE] = "";
83 static int dbport = 5432;
84 static time_t connect_time = 0;
85
86 static int parse_config(int reload);
87 static int pgsql_reconnect(const char *database);
88 static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
89 static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
90
91 static enum { RQ_WARN, RQ_CREATECLOSE, RQ_CREATECHAR } requirements;
92
93 static struct ast_cli_entry cli_realtime[] = {
94         AST_CLI_DEFINE(handle_cli_realtime_pgsql_status, "Shows connection information for the PostgreSQL RealTime driver"),
95         AST_CLI_DEFINE(handle_cli_realtime_pgsql_cache, "Shows cached tables within the PostgreSQL realtime driver"),
96 };
97
98 #define ESCAPE_STRING(buffer, stringname) \
99         do { \
100                 int len = strlen(stringname); \
101                 struct ast_str *semi = ast_str_thread_get(&semibuf_buf, len * 3 + 1); \
102                 const char *chunk = stringname; \
103                 ast_str_reset(semi); \
104                 for (; *chunk; chunk++) { \
105                         if (strchr(";^", *chunk)) { \
106                                 ast_str_append(&semi, 0, "^%02hhX", *chunk); \
107                         } else { \
108                                 ast_str_append(&semi, 0, "%c", *chunk); \
109                         } \
110                 } \
111                 if (ast_str_strlen(semi) > (ast_str_size(buffer) - 1) / 2) { \
112                         ast_str_make_space(&buffer, ast_str_strlen(semi) * 2 + 1); \
113                 } \
114                 PQescapeStringConn(pgsqlConn, ast_str_buffer(buffer), ast_str_buffer(semi), ast_str_size(buffer), &pgresult); \
115         } while (0)
116
117 static void destroy_table(struct tables *table)
118 {
119         struct columns *column;
120         ast_rwlock_wrlock(&table->lock);
121         while ((column = AST_LIST_REMOVE_HEAD(&table->columns, list))) {
122                 ast_free(column);
123         }
124         ast_rwlock_unlock(&table->lock);
125         ast_rwlock_destroy(&table->lock);
126         ast_free(table);
127 }
128
129 /*! \brief Helper function for pgsql_exec.  For running querys, use pgsql_exec()
130  *
131  *  Connect if not currently connected.  Run the given query.
132  *
133  *  \param database   database name we are connected to (used for error logging)
134  *  \param tablename  table  name we are connected to (used for error logging)
135  *  \param sql        sql query string to execute
136  *  \param result     pointer for where to store the result handle
137  *
138  *  \return -1 on fatal query error
139  *  \return -2 on query failure that resulted in disconnection
140  *  \return 0 on success
141  *
142  *  \example see pgsql_exec for full example
143  */
144 static int _pgsql_exec(const char *database, const char *tablename, const char *sql, PGresult **result)
145 {
146         ExecStatusType result_status;
147
148         if (!pgsqlConn) {
149                 ast_debug(1, "PostgreSQL connection not defined, connecting\n");
150
151                 if (pgsql_reconnect(database) != 1) {
152                         ast_log(LOG_NOTICE, "reconnect failed\n");
153                         *result = NULL;
154                         return -1;
155                 }
156
157                 ast_debug(1, "PostgreSQL connection successful\n");
158         }
159
160         *result = PQexec(pgsqlConn, sql);
161         result_status = PQresultStatus(*result);
162         if (result_status != PGRES_COMMAND_OK
163                 && result_status != PGRES_TUPLES_OK
164                 && result_status != PGRES_NONFATAL_ERROR) {
165
166                 ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to query '%s@%s'.\n", tablename, database);
167                 ast_log(LOG_ERROR, "PostgreSQL RealTime: Query Failed: %s\n", sql);
168                 ast_log(LOG_ERROR, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
169                         PQresultErrorMessage(*result),
170                         PQresStatus(result_status));
171
172                 /* we may have tried to run a command on a disconnected/disconnecting handle */
173                 /* are we no longer connected to the database... if not try again */
174                 if (PQstatus(pgsqlConn) != CONNECTION_OK) {
175                         PQfinish(pgsqlConn);
176                         pgsqlConn = NULL;
177                         return -2;
178                 }
179
180                 /* connection still okay, which means the query is just plain bad */
181                 return -1;
182         }
183
184         ast_debug(1, "PostgreSQL query successful: %s\n", sql);
185         return 0;
186 }
187
188 /*! \brief Do a postgres query, with reconnection support
189  *
190  *  Connect if not currently connected.  Run the given query
191  *  and if we're disconnected afterwards, reconnect and query again.
192  *
193  *  \param database   database name we are connected to (used for error logging)
194  *  \param tablename  table  name we are connected to (used for error logging)
195  *  \param sql        sql query string to execute
196  *  \param result     pointer for where to store the result handle
197  *
198  *  \return -1 on query failure
199  *  \return 0 on success
200  *
201  *  \example
202  *      int i, rows;
203  *      PGresult *result;
204  *      char *field_name, *field_type, *field_len, *field_notnull, *field_default;
205  *
206  *      pgsql_exec("db", "table", "SELECT 1", &result)
207  *
208  *      rows = PQntuples(result);
209  *      for (i = 0; i < rows; i++) {
210  *              field_name    = PQgetvalue(result, i, 0);
211  *              field_type    = PQgetvalue(result, i, 1);
212  *              field_len     = PQgetvalue(result, i, 2);
213  *              field_notnull = PQgetvalue(result, i, 3);
214  *              field_default = PQgetvalue(result, i, 4);
215  *      }
216  *
217  */
218 static int pgsql_exec(const char *database, const char *tablename, const char *sql, PGresult **result)
219 {
220         int attempts = 0;
221         int res;
222
223         /* Try the query, note failure if any */
224         /* On first failure, reconnect and try again (_pgsql_exec handles reconnect) */
225         /* On second failure, treat as fatal query error */
226
227         while (attempts++ < 2) {
228                 ast_debug(1, "PostgreSQL query attempt %d\n", attempts);
229                 res = _pgsql_exec(database, tablename, sql, result);
230
231                 if (res == 0) {
232                         if (attempts > 1) {
233                                 ast_log(LOG_NOTICE, "PostgreSQL RealTime: Query finally succeeded: %s\n", sql);
234                         }
235
236                         return 0;
237                 }
238
239                 if (res == -1) {
240                         return -1; /* Still connected to db, but could not process query (fatal error) */
241                 }
242
243                 /* res == -2 (query on a disconnected handle) */
244                 ast_debug(1, "PostgreSQL query attempt %d failed, trying again\n", attempts);
245         }
246
247         return -1;
248 }
249
250 static struct tables *find_table(const char *database, const char *orig_tablename)
251 {
252         struct columns *column;
253         struct tables *table;
254         struct ast_str *sql = ast_str_thread_get(&findtable_buf, 330);
255         PGresult *result;
256         int exec_result;
257         char *fname, *ftype, *flen, *fnotnull, *fdef;
258         int i, rows;
259
260         AST_LIST_LOCK(&psql_tables);
261         AST_LIST_TRAVERSE(&psql_tables, table, list) {
262                 if (!strcasecmp(table->name, orig_tablename)) {
263                         ast_debug(1, "Found table in cache; now locking\n");
264                         ast_rwlock_rdlock(&table->lock);
265                         ast_debug(1, "Lock cached table; now returning\n");
266                         AST_LIST_UNLOCK(&psql_tables);
267                         return table;
268                 }
269         }
270
271         if (database == NULL) {
272                 return NULL;
273         }
274
275         ast_debug(1, "Table '%s' not found in cache, querying now\n", orig_tablename);
276
277         /* Not found, scan the table */
278         if (has_schema_support) {
279                 char *schemaname, *tablename;
280                 if (strchr(orig_tablename, '.')) {
281                         schemaname = ast_strdupa(orig_tablename);
282                         tablename = strchr(schemaname, '.');
283                         *tablename++ = '\0';
284                 } else {
285                         schemaname = "";
286                         tablename = ast_strdupa(orig_tablename);
287                 }
288
289                 /* Escape special characters in schemaname */
290                 if (strchr(schemaname, '\\') || strchr(schemaname, '\'')) {
291                         char *tmp = schemaname, *ptr;
292
293                         ptr = schemaname = alloca(strlen(tmp) * 2 + 1);
294                         for (; *tmp; tmp++) {
295                                 if (strchr("\\'", *tmp)) {
296                                         *ptr++ = *tmp;
297                                 }
298                                 *ptr++ = *tmp;
299                         }
300                         *ptr = '\0';
301                 }
302                 /* Escape special characters in tablename */
303                 if (strchr(tablename, '\\') || strchr(tablename, '\'')) {
304                         char *tmp = tablename, *ptr;
305
306                         ptr = tablename = alloca(strlen(tmp) * 2 + 1);
307                         for (; *tmp; tmp++) {
308                                 if (strchr("\\'", *tmp)) {
309                                         *ptr++ = *tmp;
310                                 }
311                                 *ptr++ = *tmp;
312                         }
313                         *ptr = '\0';
314                 }
315
316                 ast_str_set(&sql, 0, "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace AND c.relname = '%s' AND n.nspname = %s%s%s) INNER JOIN pg_catalog.pg_attribute a ON (NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum ORDER BY n.nspname, c.relname, attnum",
317                         tablename,
318                         ast_strlen_zero(schemaname) ? "" : "'", ast_strlen_zero(schemaname) ? "current_schema()" : schemaname, ast_strlen_zero(schemaname) ? "" : "'");
319         } else {
320                 /* Escape special characters in tablename */
321                 if (strchr(orig_tablename, '\\') || strchr(orig_tablename, '\'')) {
322                         const char *tmp = orig_tablename;
323                         char *ptr;
324
325                         orig_tablename = ptr = alloca(strlen(tmp) * 2 + 1);
326                         for (; *tmp; tmp++) {
327                                 if (strchr("\\'", *tmp)) {
328                                         *ptr++ = *tmp;
329                                 }
330                                 *ptr++ = *tmp;
331                         }
332                         *ptr = '\0';
333                 }
334
335                 ast_str_set(&sql, 0, "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM pg_class c, pg_type t, pg_attribute a LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum", orig_tablename);
336         }
337
338         exec_result = pgsql_exec(database, orig_tablename, ast_str_buffer(sql), &result);
339         ast_debug(1, "Query of table structure complete.  Now retrieving results.\n");
340         if (exec_result != 0) {
341                 ast_log(LOG_ERROR, "Failed to query database columns for table %s\n", orig_tablename);
342                 PQclear(result);
343                 AST_LIST_UNLOCK(&psql_tables);
344                 return NULL;
345         }
346
347         if (!(table = ast_calloc(1, sizeof(*table) + strlen(orig_tablename) + 1))) {
348                 ast_log(LOG_ERROR, "Unable to allocate memory for new table structure\n");
349                 PQclear(result);
350                 AST_LIST_UNLOCK(&psql_tables);
351                 return NULL;
352         }
353         strcpy(table->name, orig_tablename); /* SAFE */
354         ast_rwlock_init(&table->lock);
355         AST_LIST_HEAD_INIT_NOLOCK(&table->columns);
356
357         rows = PQntuples(result);
358         for (i = 0; i < rows; i++) {
359                 fname = PQgetvalue(result, i, 0);
360                 ftype = PQgetvalue(result, i, 1);
361                 flen = PQgetvalue(result, i, 2);
362                 fnotnull = PQgetvalue(result, i, 3);
363                 fdef = PQgetvalue(result, i, 4);
364                 ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
365
366                 if (!(column = ast_calloc(1, sizeof(*column) + strlen(fname) + strlen(ftype) + 2))) {
367                         ast_log(LOG_ERROR, "Unable to allocate column element for %s, %s\n", orig_tablename, fname);
368                         PQclear(result);
369                         destroy_table(table);
370                         AST_LIST_UNLOCK(&psql_tables);
371                         return NULL;
372                 }
373
374                 if (strcmp(flen, "-1") == 0) {
375                         /* Some types, like chars, have the length stored in a different field */
376                         flen = PQgetvalue(result, i, 5);
377                         sscanf(flen, "%30d", &column->len);
378                         column->len -= 4;
379                 } else {
380                         sscanf(flen, "%30d", &column->len);
381                 }
382                 column->name = (char *)column + sizeof(*column);
383                 column->type = (char *)column + sizeof(*column) + strlen(fname) + 1;
384                 strcpy(column->name, fname);
385                 strcpy(column->type, ftype);
386                 if (*fnotnull == 't') {
387                         column->notnull = 1;
388                 } else {
389                         column->notnull = 0;
390                 }
391                 if (!ast_strlen_zero(fdef)) {
392                         column->hasdefault = 1;
393                 } else {
394                         column->hasdefault = 0;
395                 }
396                 AST_LIST_INSERT_TAIL(&table->columns, column, list);
397         }
398         PQclear(result);
399
400         AST_LIST_INSERT_TAIL(&psql_tables, table, list);
401         ast_rwlock_rdlock(&table->lock);
402         AST_LIST_UNLOCK(&psql_tables);
403         return table;
404 }
405
406 #define release_table(table) ast_rwlock_unlock(&(table)->lock);
407
408 static struct columns *find_column(struct tables *t, const char *colname)
409 {
410         struct columns *column;
411
412         /* Check that the column exists in the table */
413         AST_LIST_TRAVERSE(&t->columns, column, list) {
414                 if (strcmp(column->name, colname) == 0) {
415                         return column;
416                 }
417         }
418         return NULL;
419 }
420
421 static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, va_list ap)
422 {
423         PGresult *result = NULL;
424         int num_rows = 0, pgresult;
425         struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
426         struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
427         char *stringp;
428         char *chunk;
429         char *op;
430         const char *newparam, *newval;
431         struct ast_variable *var = NULL, *prev = NULL;
432
433         /*
434          * Ignore database from the extconfig.conf since it was
435          * configured by res_pgsql.conf.
436          */
437         database = dbname;
438
439         if (!tablename) {
440                 ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
441                 return NULL;
442         }
443
444         /* Get the first parameter and first value in our list of passed paramater/value pairs */
445         newparam = va_arg(ap, const char *);
446         newval = va_arg(ap, const char *);
447         if (!newparam || !newval) {
448                 ast_log(LOG_WARNING,
449                                 "PostgreSQL RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
450                 if (pgsqlConn) {
451                         PQfinish(pgsqlConn);
452                         pgsqlConn = NULL;
453                 }
454                 return NULL;
455         }
456
457         /* Create the first part of the query using the first parameter/value pairs we just extracted
458            If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
459         op = strchr(newparam, ' ') ? "" : " =";
460
461         ESCAPE_STRING(escapebuf, newval);
462         if (pgresult) {
463                 ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
464                 va_end(ap);
465                 return NULL;
466         }
467
468         ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'", tablename, newparam, op, ast_str_buffer(escapebuf));
469         while ((newparam = va_arg(ap, const char *))) {
470                 newval = va_arg(ap, const char *);
471                 if (!strchr(newparam, ' '))
472                         op = " =";
473                 else
474                         op = "";
475
476                 ESCAPE_STRING(escapebuf, newval);
477                 if (pgresult) {
478                         ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
479                         va_end(ap);
480                         return NULL;
481                 }
482
483                 ast_str_append(&sql, 0, " AND %s%s '%s'", newparam, op, ast_str_buffer(escapebuf));
484         }
485         va_end(ap);
486
487         /* We now have our complete statement; Lets connect to the server and execute it. */
488         ast_mutex_lock(&pgsql_lock);
489
490         if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
491                 PQclear(result);
492                 ast_mutex_unlock(&pgsql_lock);
493                 return NULL;
494         }
495
496         ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, ast_str_buffer(sql));
497
498         if ((num_rows = PQntuples(result)) > 0) {
499                 int i = 0;
500                 int rowIndex = 0;
501                 int numFields = PQnfields(result);
502                 char **fieldnames = NULL;
503
504                 ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
505
506                 if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
507                         PQclear(result);
508                         ast_mutex_unlock(&pgsql_lock);
509                         return NULL;
510                 }
511                 for (i = 0; i < numFields; i++)
512                         fieldnames[i] = PQfname(result, i);
513                 for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
514                         for (i = 0; i < numFields; i++) {
515                                 stringp = PQgetvalue(result, rowIndex, i);
516                                 while (stringp) {
517                                         chunk = strsep(&stringp, ";");
518                                         if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
519                                                 if (prev) {
520                                                         prev->next = ast_variable_new(fieldnames[i], chunk, "");
521                                                         if (prev->next) {
522                                                                 prev = prev->next;
523                                                         }
524                                                 } else {
525                                                         prev = var = ast_variable_new(fieldnames[i], chunk, "");
526                                                 }
527                                         }
528                                 }
529                         }
530                 }
531                 ast_free(fieldnames);
532         } else {
533                 ast_debug(1, "Postgresql RealTime: Could not find any rows in table %s@%s.\n", tablename, database);
534         }
535
536         PQclear(result);
537         ast_mutex_unlock(&pgsql_lock);
538
539         return var;
540 }
541
542 static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, va_list ap)
543 {
544         PGresult *result = NULL;
545         int num_rows = 0, pgresult;
546         struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
547         struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
548         const char *initfield = NULL;
549         char *stringp;
550         char *chunk;
551         char *op;
552         const char *newparam, *newval;
553         struct ast_variable *var = NULL;
554         struct ast_config *cfg = NULL;
555         struct ast_category *cat = NULL;
556
557         /*
558          * Ignore database from the extconfig.conf since it was
559          * configured by res_pgsql.conf.
560          */
561         database = dbname;
562
563         if (!table) {
564                 ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
565                 return NULL;
566         }
567
568         if (!(cfg = ast_config_new()))
569                 return NULL;
570
571         /* Get the first parameter and first value in our list of passed paramater/value pairs */
572         newparam = va_arg(ap, const char *);
573         newval = va_arg(ap, const char *);
574         if (!newparam || !newval) {
575                 ast_log(LOG_WARNING,
576                                 "PostgreSQL RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
577                 if (pgsqlConn) {
578                         PQfinish(pgsqlConn);
579                         pgsqlConn = NULL;
580                 }
581                 ast_config_destroy(cfg);
582                 return NULL;
583         }
584
585         initfield = ast_strdupa(newparam);
586         if ((op = strchr(initfield, ' '))) {
587                 *op = '\0';
588         }
589
590         /* Create the first part of the query using the first parameter/value pairs we just extracted
591            If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
592
593         if (!strchr(newparam, ' '))
594                 op = " =";
595         else
596                 op = "";
597
598         ESCAPE_STRING(escapebuf, newval);
599         if (pgresult) {
600                 ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
601                 va_end(ap);
602                 ast_config_destroy(cfg);
603                 return NULL;
604         }
605
606         ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op, ast_str_buffer(escapebuf));
607         while ((newparam = va_arg(ap, const char *))) {
608                 newval = va_arg(ap, const char *);
609                 if (!strchr(newparam, ' '))
610                         op = " =";
611                 else
612                         op = "";
613
614                 ESCAPE_STRING(escapebuf, newval);
615                 if (pgresult) {
616                         ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
617                         va_end(ap);
618                         ast_config_destroy(cfg);
619                         return NULL;
620                 }
621
622                 ast_str_append(&sql, 0, " AND %s%s '%s'", newparam, op, ast_str_buffer(escapebuf));
623         }
624
625         if (initfield) {
626                 ast_str_append(&sql, 0, " ORDER BY %s", initfield);
627         }
628
629         va_end(ap);
630
631         /* We now have our complete statement; Lets connect to the server and execute it. */
632         ast_mutex_lock(&pgsql_lock);
633
634         if (pgsql_exec(database, table, ast_str_buffer(sql), &result) != 0) {
635                 ast_mutex_unlock(&pgsql_lock);
636                 ast_config_destroy(cfg);
637                 return NULL;
638         } else {
639                 ExecStatusType result_status = PQresultStatus(result);
640                 if (result_status != PGRES_COMMAND_OK
641                         && result_status != PGRES_TUPLES_OK
642                         && result_status != PGRES_NONFATAL_ERROR) {
643                         ast_log(LOG_WARNING,
644                                         "PostgreSQL RealTime: Failed to query %s@%s. Check debug for more info.\n", table, database);
645                         ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
646                         ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
647                                                 PQresultErrorMessage(result), PQresStatus(result_status));
648                         PQclear(result);
649                         ast_mutex_unlock(&pgsql_lock);
650                         ast_config_destroy(cfg);
651                         return NULL;
652                 }
653         }
654
655         ast_debug(1, "PostgreSQL RealTime: Result=%p Query: %s\n", result, ast_str_buffer(sql));
656
657         if ((num_rows = PQntuples(result)) > 0) {
658                 int numFields = PQnfields(result);
659                 int i = 0;
660                 int rowIndex = 0;
661                 char **fieldnames = NULL;
662
663                 ast_debug(1, "PostgreSQL RealTime: Found %d rows.\n", num_rows);
664
665                 if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
666                         PQclear(result);
667                         ast_mutex_unlock(&pgsql_lock);
668                         ast_config_destroy(cfg);
669                         return NULL;
670                 }
671                 for (i = 0; i < numFields; i++)
672                         fieldnames[i] = PQfname(result, i);
673
674                 for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
675                         var = NULL;
676                         if (!(cat = ast_category_new("","",99999)))
677                                 continue;
678                         for (i = 0; i < numFields; i++) {
679                                 stringp = PQgetvalue(result, rowIndex, i);
680                                 while (stringp) {
681                                         chunk = strsep(&stringp, ";");
682                                         if (chunk && !ast_strlen_zero(ast_realtime_decode_chunk(ast_strip(chunk)))) {
683                                                 if (initfield && !strcmp(initfield, fieldnames[i])) {
684                                                         ast_category_rename(cat, chunk);
685                                                 }
686                                                 var = ast_variable_new(fieldnames[i], chunk, "");
687                                                 ast_variable_append(cat, var);
688                                         }
689                                 }
690                         }
691                         ast_category_append(cfg, cat);
692                 }
693                 ast_free(fieldnames);
694         } else {
695                 ast_debug(1, "PostgreSQL RealTime: Could not find any rows in table %s.\n", table);
696         }
697
698         PQclear(result);
699         ast_mutex_unlock(&pgsql_lock);
700
701         return cfg;
702 }
703
704 static int update_pgsql(const char *database, const char *tablename, const char *keyfield,
705                                                 const char *lookup, va_list ap)
706 {
707         PGresult *result = NULL;
708         int numrows = 0, pgresult;
709         const char *newparam, *newval;
710         struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
711         struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 100);
712         struct tables *table;
713         struct columns *column = NULL;
714
715         /*
716          * Ignore database from the extconfig.conf since it was
717          * configured by res_pgsql.conf.
718          */
719         database = dbname;
720
721         if (!tablename) {
722                 ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
723                 return -1;
724         }
725
726         if (!(table = find_table(database, tablename))) {
727                 ast_log(LOG_ERROR, "Table '%s' does not exist!!\n", tablename);
728                 return -1;
729         }
730
731         /* Get the first parameter and first value in our list of passed paramater/value pairs */
732         newparam = va_arg(ap, const char *);
733         newval = va_arg(ap, const char *);
734         if (!newparam || !newval) {
735                 ast_log(LOG_WARNING,
736                                 "PostgreSQL RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
737                 if (pgsqlConn) {
738                         PQfinish(pgsqlConn);
739                         pgsqlConn = NULL;
740                 }
741                 release_table(table);
742                 return -1;
743         }
744
745         /* Check that the column exists in the table */
746         AST_LIST_TRAVERSE(&table->columns, column, list) {
747                 if (strcmp(column->name, newparam) == 0) {
748                         break;
749                 }
750         }
751
752         if (!column) {
753                 ast_log(LOG_ERROR, "PostgreSQL RealTime: Updating on column '%s', but that column does not exist within the table '%s'!\n", newparam, tablename);
754                 release_table(table);
755                 return -1;
756         }
757
758         /* Create the first part of the query using the first parameter/value pairs we just extracted
759            If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
760
761         ESCAPE_STRING(escapebuf, newval);
762         if (pgresult) {
763                 ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
764                 va_end(ap);
765                 release_table(table);
766                 return -1;
767         }
768         ast_str_set(&sql, 0, "UPDATE %s SET %s = '%s'", tablename, newparam, ast_str_buffer(escapebuf));
769
770         while ((newparam = va_arg(ap, const char *))) {
771                 newval = va_arg(ap, const char *);
772
773                 if (!find_column(table, newparam)) {
774                         ast_log(LOG_NOTICE, "Attempted to update column '%s' in table '%s', but column does not exist!\n", newparam, tablename);
775                         continue;
776                 }
777
778                 ESCAPE_STRING(escapebuf, newval);
779                 if (pgresult) {
780                         ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
781                         va_end(ap);
782                         release_table(table);
783                         return -1;
784                 }
785
786                 ast_str_append(&sql, 0, ", %s = '%s'", newparam, ast_str_buffer(escapebuf));
787         }
788         va_end(ap);
789         release_table(table);
790
791         ESCAPE_STRING(escapebuf, lookup);
792         if (pgresult) {
793                 ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", lookup);
794                 va_end(ap);
795                 return -1;
796         }
797
798         ast_str_append(&sql, 0, " WHERE %s = '%s'", keyfield, ast_str_buffer(escapebuf));
799
800         ast_debug(1, "PostgreSQL RealTime: Update SQL: %s\n", ast_str_buffer(sql));
801
802         /* We now have our complete statement; Lets connect to the server and execute it. */
803         ast_mutex_lock(&pgsql_lock);
804
805         if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
806                 ast_mutex_unlock(&pgsql_lock);
807                 return -1;
808         } else {
809                 ExecStatusType result_status = PQresultStatus(result);
810                 if (result_status != PGRES_COMMAND_OK
811                         && result_status != PGRES_TUPLES_OK
812                         && result_status != PGRES_NONFATAL_ERROR) {
813                         ast_log(LOG_WARNING,
814                                         "PostgreSQL RealTime: Failed to query database. Check debug for more info.\n");
815                         ast_debug(1, "PostgreSQL RealTime: Query: %s\n", ast_str_buffer(sql));
816                         ast_debug(1, "PostgreSQL RealTime: Query Failed because: %s (%s)\n",
817                                                 PQresultErrorMessage(result), PQresStatus(result_status));
818                         PQclear(result);
819                         ast_mutex_unlock(&pgsql_lock);
820                         return -1;
821                 }
822         }
823
824         numrows = atoi(PQcmdTuples(result));
825         ast_mutex_unlock(&pgsql_lock);
826
827         ast_debug(1, "PostgreSQL RealTime: Updated %d rows on table: %s\n", numrows, tablename);
828
829         /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
830          * An integer greater than zero indicates the number of rows affected
831          * Zero indicates that no records were updated
832          * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
833          */
834
835         if (numrows >= 0)
836                 return (int) numrows;
837
838         return -1;
839 }
840
841 static int update2_pgsql(const char *database, const char *tablename, va_list ap)
842 {
843         PGresult *result = NULL;
844         int numrows = 0, pgresult, first = 1;
845         struct ast_str *escapebuf = ast_str_thread_get(&escapebuf_buf, 16);
846         const char *newparam, *newval;
847         struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
848         struct ast_str *where = ast_str_thread_get(&where_buf, 100);
849         struct tables *table;
850
851         /*
852          * Ignore database from the extconfig.conf since it was
853          * configured by res_pgsql.conf.
854          */
855         database = dbname;
856
857         if (!tablename) {
858                 ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
859                 return -1;
860         }
861
862         if (!escapebuf || !sql || !where) {
863                 /* Memory error, already handled */
864                 return -1;
865         }
866
867         if (!(table = find_table(database, tablename))) {
868                 ast_log(LOG_ERROR, "Table '%s' does not exist!!\n", tablename);
869                 return -1;
870         }
871
872         ast_str_set(&sql, 0, "UPDATE %s SET ", tablename);
873         ast_str_set(&where, 0, "WHERE");
874
875         while ((newparam = va_arg(ap, const char *))) {
876                 if (!find_column(table, newparam)) {
877                         ast_log(LOG_ERROR, "Attempted to update based on criteria column '%s' (%s@%s), but that column does not exist!\n", newparam, tablename, database);
878                         release_table(table);
879                         return -1;
880                 }
881
882                 newval = va_arg(ap, const char *);
883                 ESCAPE_STRING(escapebuf, newval);
884                 if (pgresult) {
885                         ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
886                         release_table(table);
887                         ast_free(sql);
888                         return -1;
889                 }
890                 ast_str_append(&where, 0, "%s %s='%s'", first ? "" : " AND", newparam, ast_str_buffer(escapebuf));
891                 first = 0;
892         }
893
894         if (first) {
895                 ast_log(LOG_WARNING,
896                                 "PostgreSQL RealTime: Realtime update requires at least 1 parameter and 1 value to search on.\n");
897                 if (pgsqlConn) {
898                         PQfinish(pgsqlConn);
899                         pgsqlConn = NULL;
900                 }
901                 release_table(table);
902                 return -1;
903         }
904
905         /* Now retrieve the columns to update */
906         first = 1;
907         while ((newparam = va_arg(ap, const char *))) {
908                 newval = va_arg(ap, const char *);
909
910                 /* If the column is not within the table, then skip it */
911                 if (!find_column(table, newparam)) {
912                         ast_log(LOG_NOTICE, "Attempted to update column '%s' in table '%s@%s', but column does not exist!\n", newparam, tablename, database);
913                         continue;
914                 }
915
916                 ESCAPE_STRING(escapebuf, newval);
917                 if (pgresult) {
918                         ast_log(LOG_ERROR, "PostgreSQL RealTime: detected invalid input: '%s'\n", newval);
919                         release_table(table);
920                         ast_free(sql);
921                         return -1;
922                 }
923
924                 ast_str_append(&sql, 0, "%s %s='%s'", first ? "" : ",", newparam, ast_str_buffer(escapebuf));
925         }
926         release_table(table);
927
928         ast_str_append(&sql, 0, " %s", ast_str_buffer(where));
929
930         ast_debug(1, "PostgreSQL RealTime: Update SQL: %s\n", ast_str_buffer(sql));
931
932         /* We now have our complete statement; connect to the server and execute it. */
933         if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
934                 ast_mutex_unlock(&pgsql_lock);
935                 return -1;
936         }
937
938         numrows = atoi(PQcmdTuples(result));
939         ast_mutex_unlock(&pgsql_lock);
940
941         ast_debug(1, "PostgreSQL RealTime: Updated %d rows on table: %s\n", numrows, tablename);
942
943         /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
944          * An integer greater than zero indicates the number of rows affected
945          * Zero indicates that no records were updated
946          * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
947          */
948
949         if (numrows >= 0) {
950                 return (int) numrows;
951         }
952
953         return -1;
954 }
955
956 static int store_pgsql(const char *database, const char *table, va_list ap)
957 {
958         PGresult *result = NULL;
959         Oid insertid;
960         struct ast_str *buf = ast_str_thread_get(&escapebuf_buf, 256);
961         struct ast_str *sql1 = ast_str_thread_get(&sql_buf, 256);
962         struct ast_str *sql2 = ast_str_thread_get(&where_buf, 256);
963         int pgresult;
964         const char *newparam, *newval;
965
966         /*
967          * Ignore database from the extconfig.conf since it was
968          * configured by res_pgsql.conf.
969          */
970         database = dbname;
971
972         if (!table) {
973                 ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
974                 return -1;
975         }
976
977         /* Get the first parameter and first value in our list of passed paramater/value pairs */
978         newparam = va_arg(ap, const char *);
979         newval = va_arg(ap, const char *);
980         if (!newparam || !newval) {
981                 ast_log(LOG_WARNING,
982                                 "PostgreSQL RealTime: Realtime storage requires at least 1 parameter and 1 value to store.\n");
983                 if (pgsqlConn) {
984                         PQfinish(pgsqlConn);
985                         pgsqlConn = NULL;
986                 }
987                 return -1;
988         }
989
990         /* Must connect to the server before anything else, as the escape function requires the connection handle.. */
991         ast_mutex_lock(&pgsql_lock);
992         if (!pgsql_reconnect(database)) {
993                 ast_mutex_unlock(&pgsql_lock);
994                 return -1;
995         }
996
997         /* Create the first part of the query using the first parameter/value pairs we just extracted
998            If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
999         ESCAPE_STRING(buf, newparam);
1000         ast_str_set(&sql1, 0, "INSERT INTO %s (%s", table, ast_str_buffer(buf));
1001         ESCAPE_STRING(buf, newval);
1002         ast_str_set(&sql2, 0, ") VALUES ('%s'", ast_str_buffer(buf));
1003         while ((newparam = va_arg(ap, const char *))) {
1004                 newval = va_arg(ap, const char *);
1005                 ESCAPE_STRING(buf, newparam);
1006                 ast_str_append(&sql1, 0, ", %s", ast_str_buffer(buf));
1007                 ESCAPE_STRING(buf, newval);
1008                 ast_str_append(&sql2, 0, ", '%s'", ast_str_buffer(buf));
1009         }
1010         va_end(ap);
1011         ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
1012
1013         ast_debug(1, "PostgreSQL RealTime: Insert SQL: %s\n", ast_str_buffer(sql1));
1014
1015         if (pgsql_exec(database, table, ast_str_buffer(sql1), &result) != 0) {
1016                 ast_mutex_unlock(&pgsql_lock);
1017                 return -1;
1018         }
1019
1020         insertid = PQoidValue(result);
1021         PQclear(result);
1022         ast_mutex_unlock(&pgsql_lock);
1023
1024         ast_debug(1, "PostgreSQL RealTime: row inserted on table: %s, id: %u\n", table, insertid);
1025
1026         /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
1027          * An integer greater than zero indicates the number of rows affected
1028          * Zero indicates that no records were updated
1029          * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
1030          */
1031
1032         if (insertid >= 0)
1033                 return (int) insertid;
1034
1035         return -1;
1036 }
1037
1038 static int destroy_pgsql(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
1039 {
1040         PGresult *result = NULL;
1041         int numrows = 0;
1042         int pgresult;
1043         struct ast_str *sql = ast_str_thread_get(&sql_buf, 256);
1044         struct ast_str *buf1 = ast_str_thread_get(&where_buf, 60), *buf2 = ast_str_thread_get(&escapebuf_buf, 60);
1045         const char *newparam, *newval;
1046
1047         /*
1048          * Ignore database from the extconfig.conf since it was
1049          * configured by res_pgsql.conf.
1050          */
1051         database = dbname;
1052
1053         if (!table) {
1054                 ast_log(LOG_WARNING, "PostgreSQL RealTime: No table specified.\n");
1055                 return -1;
1056         }
1057
1058         /* Get the first parameter and first value in our list of passed paramater/value pairs */
1059         /*newparam = va_arg(ap, const char *);
1060         newval = va_arg(ap, const char *);
1061         if (!newparam || !newval) {*/
1062         if (ast_strlen_zero(keyfield) || ast_strlen_zero(lookup))  {
1063                 ast_log(LOG_WARNING,
1064                                 "PostgreSQL RealTime: Realtime destroy requires at least 1 parameter and 1 value to search on.\n");
1065                 if (pgsqlConn) {
1066                         PQfinish(pgsqlConn);
1067                         pgsqlConn = NULL;
1068                 };
1069                 return -1;
1070         }
1071
1072         /* Must connect to the server before anything else, as the escape function requires the connection handle.. */
1073         ast_mutex_lock(&pgsql_lock);
1074         if (!pgsql_reconnect(database)) {
1075                 ast_mutex_unlock(&pgsql_lock);
1076                 return -1;
1077         }
1078
1079
1080         /* Create the first part of the query using the first parameter/value pairs we just extracted
1081            If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
1082
1083         ESCAPE_STRING(buf1, keyfield);
1084         ESCAPE_STRING(buf2, lookup);
1085         ast_str_set(&sql, 0, "DELETE FROM %s WHERE %s = '%s'", table, ast_str_buffer(buf1), ast_str_buffer(buf2));
1086         while ((newparam = va_arg(ap, const char *))) {
1087                 newval = va_arg(ap, const char *);
1088                 ESCAPE_STRING(buf1, newparam);
1089                 ESCAPE_STRING(buf2, newval);
1090                 ast_str_append(&sql, 0, " AND %s = '%s'", ast_str_buffer(buf1), ast_str_buffer(buf2));
1091         }
1092         va_end(ap);
1093
1094         ast_debug(1, "PostgreSQL RealTime: Delete SQL: %s\n", ast_str_buffer(sql));
1095
1096         if (pgsql_exec(database, table, ast_str_buffer(sql), &result) != 0) {
1097                 ast_mutex_unlock(&pgsql_lock);
1098                 return -1;
1099         }
1100
1101         numrows = atoi(PQcmdTuples(result));
1102         ast_mutex_unlock(&pgsql_lock);
1103
1104         ast_debug(1, "PostgreSQL RealTime: Deleted %d rows on table: %s\n", numrows, table);
1105
1106         /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
1107          * An integer greater than zero indicates the number of rows affected
1108          * Zero indicates that no records were updated
1109          * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
1110          */
1111
1112         if (numrows >= 0)
1113                 return (int) numrows;
1114
1115         return -1;
1116 }
1117
1118
1119 static struct ast_config *config_pgsql(const char *database, const char *table,
1120                                                                            const char *file, struct ast_config *cfg,
1121                                                                            struct ast_flags flags, const char *suggested_incl, const char *who_asked)
1122 {
1123         PGresult *result = NULL;
1124         long num_rows;
1125         struct ast_variable *new_v;
1126         struct ast_category *cur_cat = NULL;
1127         struct ast_str *sql = ast_str_thread_get(&sql_buf, 100);
1128         char last[80];
1129         int last_cat_metric = 0;
1130
1131         last[0] = '\0';
1132
1133         /*
1134          * Ignore database from the extconfig.conf since it is
1135          * configured by res_pgsql.conf.
1136          */
1137         database = dbname;
1138
1139         if (!file || !strcmp(file, RES_CONFIG_PGSQL_CONF)) {
1140                 ast_log(LOG_WARNING, "PostgreSQL RealTime: Cannot configure myself.\n");
1141                 return NULL;
1142         }
1143
1144         ast_str_set(&sql, 0, "SELECT category, var_name, var_val, cat_metric FROM %s "
1145                         "WHERE filename='%s' and commented=0 "
1146                         "ORDER BY cat_metric DESC, var_metric ASC, category, var_name ", table, file);
1147
1148         ast_debug(1, "PostgreSQL RealTime: Static SQL: %s\n", ast_str_buffer(sql));
1149
1150         ast_mutex_lock(&pgsql_lock);
1151
1152         /* We now have our complete statement; Lets connect to the server and execute it. */
1153         if (pgsql_exec(database, table, ast_str_buffer(sql), &result) != 0) {
1154                 ast_mutex_unlock(&pgsql_lock);
1155                 return NULL;
1156         }
1157
1158         if ((num_rows = PQntuples(result)) > 0) {
1159                 int rowIndex = 0;
1160
1161                 ast_debug(1, "PostgreSQL RealTime: Found %ld rows.\n", num_rows);
1162
1163                 for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
1164                         char *field_category = PQgetvalue(result, rowIndex, 0);
1165                         char *field_var_name = PQgetvalue(result, rowIndex, 1);
1166                         char *field_var_val = PQgetvalue(result, rowIndex, 2);
1167                         char *field_cat_metric = PQgetvalue(result, rowIndex, 3);
1168                         if (!strcmp(field_var_name, "#include")) {
1169                                 if (!ast_config_internal_load(field_var_val, cfg, flags, "", who_asked)) {
1170                                         PQclear(result);
1171                                         ast_mutex_unlock(&pgsql_lock);
1172                                         return NULL;
1173                                 }
1174                                 continue;
1175                         }
1176
1177                         if (strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
1178                                 cur_cat = ast_category_new(field_category, "", 99999);
1179                                 if (!cur_cat)
1180                                         break;
1181                                 ast_copy_string(last, field_category, sizeof(last));
1182                                 last_cat_metric = atoi(field_cat_metric);
1183                                 ast_category_append(cfg, cur_cat);
1184                         }
1185                         new_v = ast_variable_new(field_var_name, field_var_val, "");
1186                         ast_variable_append(cur_cat, new_v);
1187                 }
1188         } else {
1189                 ast_log(LOG_WARNING,
1190                                 "PostgreSQL RealTime: Could not find config '%s' in database.\n", file);
1191         }
1192
1193         PQclear(result);
1194         ast_mutex_unlock(&pgsql_lock);
1195
1196         return cfg;
1197 }
1198
1199 static int require_pgsql(const char *database, const char *tablename, va_list ap)
1200 {
1201         struct columns *column;
1202         struct tables *table;
1203         char *elm;
1204         int type, size, res = 0;
1205
1206         /*
1207          * Ignore database from the extconfig.conf since it was
1208          * configured by res_pgsql.conf.
1209          */
1210         database = dbname;
1211
1212         table = find_table(database, tablename);
1213         if (!table) {
1214                 ast_log(LOG_WARNING, "Table %s not found in database.  This table should exist if you're using realtime.\n", tablename);
1215                 return -1;
1216         }
1217
1218         while ((elm = va_arg(ap, char *))) {
1219                 type = va_arg(ap, require_type);
1220                 size = va_arg(ap, int);
1221                 AST_LIST_TRAVERSE(&table->columns, column, list) {
1222                         if (strcmp(column->name, elm) == 0) {
1223                                 /* Char can hold anything, as long as it is large enough */
1224                                 if ((strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0 || strcmp(column->type, "bpchar") == 0)) {
1225                                         if ((size > column->len) && column->len != -1) {
1226                                                 ast_log(LOG_WARNING, "Column '%s' should be at least %d long, but is only %d long.\n", column->name, size, column->len);
1227                                                 res = -1;
1228                                         }
1229                                 } else if (strncmp(column->type, "int", 3) == 0) {
1230                                         int typesize = atoi(column->type + 3);
1231                                         /* Integers can hold only other integers */
1232                                         if ((type == RQ_INTEGER8 || type == RQ_UINTEGER8 ||
1233                                                 type == RQ_INTEGER4 || type == RQ_UINTEGER4 ||
1234                                                 type == RQ_INTEGER3 || type == RQ_UINTEGER3 ||
1235                                                 type == RQ_UINTEGER2) && typesize == 2) {
1236                                                 ast_log(LOG_WARNING, "Column '%s' may not be large enough for the required data length: %d\n", column->name, size);
1237                                                 res = -1;
1238                                         } else if ((type == RQ_INTEGER8 || type == RQ_UINTEGER8 ||
1239                                                 type == RQ_UINTEGER4) && typesize == 4) {
1240                                                 ast_log(LOG_WARNING, "Column '%s' may not be large enough for the required data length: %d\n", column->name, size);
1241                                                 res = -1;
1242                                         } else if (type == RQ_CHAR || type == RQ_DATETIME || type == RQ_FLOAT || type == RQ_DATE) {
1243                                                 ast_log(LOG_WARNING, "Column '%s' is of the incorrect type: (need %s(%d) but saw %s)\n",
1244                                                         column->name,
1245                                                                 type == RQ_CHAR ? "char" :
1246                                                                 type == RQ_DATETIME ? "datetime" :
1247                                                                 type == RQ_DATE ? "date" :
1248                                                                 type == RQ_FLOAT ? "float" :
1249                                                                 "a rather stiff drink ",
1250                                                         size, column->type);
1251                                                 res = -1;
1252                                         }
1253                                 } else if (strncmp(column->type, "float", 5) == 0) {
1254                                         if (!ast_rq_is_int(type) && type != RQ_FLOAT) {
1255                                                 ast_log(LOG_WARNING, "Column %s cannot be a %s\n", column->name, column->type);
1256                                                 res = -1;
1257                                         }
1258                                 } else if (strncmp(column->type, "timestamp", 9) == 0) {
1259                                         if (type != RQ_DATETIME && type != RQ_DATE) {
1260                                                 ast_log(LOG_WARNING, "Column %s cannot be a %s\n", column->name, column->type);
1261                                                 res = -1;
1262                                         }
1263                                 } else { /* There are other types that no module implements yet */
1264                                         ast_log(LOG_WARNING, "Possibly unsupported column type '%s' on column '%s'\n", column->type, column->name);
1265                                         res = -1;
1266                                 }
1267                                 break;
1268                         }
1269                 }
1270
1271                 if (!column) {
1272                         if (requirements == RQ_WARN) {
1273                                 ast_log(LOG_WARNING, "Table %s requires a column '%s' of size '%d', but no such column exists.\n", tablename, elm, size);
1274                         } else {
1275                                 struct ast_str *sql = ast_str_create(100);
1276                                 char fieldtype[15];
1277                                 PGresult *result;
1278
1279                                 if (requirements == RQ_CREATECHAR || type == RQ_CHAR) {
1280                                         /* Size is minimum length; make it at least 50% greater,
1281                                          * just to be sure, because PostgreSQL doesn't support
1282                                          * resizing columns. */
1283                                         snprintf(fieldtype, sizeof(fieldtype), "CHAR(%d)",
1284                                                 size < 15 ? size * 2 :
1285                                                 (size * 3 / 2 > 255) ? 255 : size * 3 / 2);
1286                                 } else if (type == RQ_INTEGER1 || type == RQ_UINTEGER1 || type == RQ_INTEGER2) {
1287                                         snprintf(fieldtype, sizeof(fieldtype), "INT2");
1288                                 } else if (type == RQ_UINTEGER2 || type == RQ_INTEGER3 || type == RQ_UINTEGER3 || type == RQ_INTEGER4) {
1289                                         snprintf(fieldtype, sizeof(fieldtype), "INT4");
1290                                 } else if (type == RQ_UINTEGER4 || type == RQ_INTEGER8) {
1291                                         snprintf(fieldtype, sizeof(fieldtype), "INT8");
1292                                 } else if (type == RQ_UINTEGER8) {
1293                                         /* No such type on PostgreSQL */
1294                                         snprintf(fieldtype, sizeof(fieldtype), "CHAR(20)");
1295                                 } else if (type == RQ_FLOAT) {
1296                                         snprintf(fieldtype, sizeof(fieldtype), "FLOAT8");
1297                                 } else if (type == RQ_DATE) {
1298                                         snprintf(fieldtype, sizeof(fieldtype), "DATE");
1299                                 } else if (type == RQ_DATETIME) {
1300                                         snprintf(fieldtype, sizeof(fieldtype), "TIMESTAMP");
1301                                 } else {
1302                                         ast_log(LOG_ERROR, "Unrecognized request type %d\n", type);
1303                                         ast_free(sql);
1304                                         continue;
1305                                 }
1306                                 ast_str_set(&sql, 0, "ALTER TABLE %s ADD COLUMN %s %s", tablename, elm, fieldtype);
1307                                 ast_debug(1, "About to lock pgsql_lock (running alter on table '%s' to add column '%s')\n", tablename, elm);
1308
1309                                 ast_mutex_lock(&pgsql_lock);
1310                                 ast_debug(1, "About to run ALTER query on table '%s' to add column '%s'\n", tablename, elm);
1311
1312                                 if (pgsql_exec(database, tablename, ast_str_buffer(sql), &result) != 0) {
1313                                         ast_mutex_unlock(&pgsql_lock);
1314                                         return -1;
1315                                 }
1316
1317                                 ast_debug(1, "Finished running ALTER query on table '%s'\n", tablename);
1318                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
1319                                         ast_log(LOG_ERROR, "Unable to add column: %s\n", ast_str_buffer(sql));
1320                                 }
1321                                 PQclear(result);
1322                                 ast_mutex_unlock(&pgsql_lock);
1323
1324                                 ast_free(sql);
1325                         }
1326                 }
1327         }
1328         release_table(table);
1329         return res;
1330 }
1331
1332 static int unload_pgsql(const char *database, const char *tablename)
1333 {
1334         struct tables *cur;
1335
1336         /*
1337          * Ignore database from the extconfig.conf since it was
1338          * configured by res_pgsql.conf.
1339          */
1340         database = dbname;
1341
1342         ast_debug(2, "About to lock table cache list\n");
1343         AST_LIST_LOCK(&psql_tables);
1344         ast_debug(2, "About to traverse table cache list\n");
1345         AST_LIST_TRAVERSE_SAFE_BEGIN(&psql_tables, cur, list) {
1346                 if (strcmp(cur->name, tablename) == 0) {
1347                         ast_debug(2, "About to remove matching cache entry\n");
1348                         AST_LIST_REMOVE_CURRENT(list);
1349                         ast_debug(2, "About to destroy matching cache entry\n");
1350                         destroy_table(cur);
1351                         ast_debug(1, "Cache entry '%s@%s' destroyed\n", tablename, database);
1352                         break;
1353                 }
1354         }
1355         AST_LIST_TRAVERSE_SAFE_END
1356         AST_LIST_UNLOCK(&psql_tables);
1357         ast_debug(2, "About to return\n");
1358         return cur ? 0 : -1;
1359 }
1360
1361 static struct ast_config_engine pgsql_engine = {
1362         .name = "pgsql",
1363         .load_func = config_pgsql,
1364         .realtime_func = realtime_pgsql,
1365         .realtime_multi_func = realtime_multi_pgsql,
1366         .store_func = store_pgsql,
1367         .destroy_func = destroy_pgsql,
1368         .update_func = update_pgsql,
1369         .update2_func = update2_pgsql,
1370         .require_func = require_pgsql,
1371         .unload_func = unload_pgsql,
1372 };
1373
1374 static int load_module(void)
1375 {
1376         if(!parse_config(0))
1377                 return AST_MODULE_LOAD_DECLINE;
1378
1379         ast_config_engine_register(&pgsql_engine);
1380         ast_verb(1, "PostgreSQL RealTime driver loaded.\n");
1381         ast_cli_register_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
1382
1383         return 0;
1384 }
1385
1386 static int unload_module(void)
1387 {
1388         struct tables *table;
1389         /* Acquire control before doing anything to the module itself. */
1390         ast_mutex_lock(&pgsql_lock);
1391
1392         if (pgsqlConn) {
1393                 PQfinish(pgsqlConn);
1394                 pgsqlConn = NULL;
1395         }
1396         ast_cli_unregister_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
1397         ast_config_engine_deregister(&pgsql_engine);
1398         ast_verb(1, "PostgreSQL RealTime unloaded.\n");
1399
1400         /* Destroy cached table info */
1401         AST_LIST_LOCK(&psql_tables);
1402         while ((table = AST_LIST_REMOVE_HEAD(&psql_tables, list))) {
1403                 destroy_table(table);
1404         }
1405         AST_LIST_UNLOCK(&psql_tables);
1406
1407         /* Unlock so something else can destroy the lock. */
1408         ast_mutex_unlock(&pgsql_lock);
1409
1410         return 0;
1411 }
1412
1413 static int reload(void)
1414 {
1415         parse_config(1);
1416
1417         return 0;
1418 }
1419
1420 static int parse_config(int is_reload)
1421 {
1422         struct ast_config *config;
1423         const char *s;
1424         struct ast_flags config_flags = { is_reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1425
1426         config = ast_config_load(RES_CONFIG_PGSQL_CONF, config_flags);
1427         if (config == CONFIG_STATUS_FILEUNCHANGED) {
1428                 return 0;
1429         }
1430
1431         if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
1432                 ast_log(LOG_WARNING, "Unable to load config %s\n", RES_CONFIG_PGSQL_CONF);
1433                 return 0;
1434         }
1435
1436         ast_mutex_lock(&pgsql_lock);
1437
1438         if (pgsqlConn) {
1439                 PQfinish(pgsqlConn);
1440                 pgsqlConn = NULL;
1441         }
1442
1443         if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
1444                 ast_log(LOG_WARNING,
1445                                 "PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");
1446                 strcpy(dbuser, "asterisk");
1447         } else {
1448                 ast_copy_string(dbuser, s, sizeof(dbuser));
1449         }
1450
1451         if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
1452                 ast_log(LOG_WARNING,
1453                                 "PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");
1454                 strcpy(dbpass, "asterisk");
1455         } else {
1456                 ast_copy_string(dbpass, s, sizeof(dbpass));
1457         }
1458
1459         if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
1460                 ast_log(LOG_WARNING,
1461                                 "PostgreSQL RealTime: No database host found, using localhost via socket.\n");
1462                 dbhost[0] = '\0';
1463         } else {
1464                 ast_copy_string(dbhost, s, sizeof(dbhost));
1465         }
1466
1467         if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
1468                 ast_log(LOG_WARNING,
1469                                 "PostgreSQL RealTime: No database name found, using 'asterisk' as default.\n");
1470                 strcpy(dbname, "asterisk");
1471         } else {
1472                 ast_copy_string(dbname, s, sizeof(dbname));
1473         }
1474
1475         if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
1476                 ast_log(LOG_WARNING,
1477                                 "PostgreSQL RealTime: No database port found, using 5432 as default.\n");
1478                 dbport = 5432;
1479         } else {
1480                 dbport = atoi(s);
1481         }
1482
1483         if (!ast_strlen_zero(dbhost)) {
1484                 /* No socket needed */
1485         } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {
1486                 ast_log(LOG_WARNING,
1487                                 "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);
1488                 strcpy(dbsock, "/tmp");
1489         } else {
1490                 ast_copy_string(dbsock, s, sizeof(dbsock));
1491         }
1492
1493         if (!(s = ast_variable_retrieve(config, "general", "requirements"))) {
1494                 ast_log(LOG_WARNING,
1495                                 "PostgreSQL RealTime: no requirements setting found, using 'warn' as default.\n");
1496                 requirements = RQ_WARN;
1497         } else if (!strcasecmp(s, "createclose")) {
1498                 requirements = RQ_CREATECLOSE;
1499         } else if (!strcasecmp(s, "createchar")) {
1500                 requirements = RQ_CREATECHAR;
1501         }
1502
1503         ast_config_destroy(config);
1504
1505         if (option_debug) {
1506                 if (!ast_strlen_zero(dbhost)) {
1507                         ast_debug(1, "PostgreSQL RealTime Host: %s\n", dbhost);
1508                         ast_debug(1, "PostgreSQL RealTime Port: %i\n", dbport);
1509                 } else {
1510                         ast_debug(1, "PostgreSQL RealTime Socket: %s\n", dbsock);
1511                 }
1512                 ast_debug(1, "PostgreSQL RealTime User: %s\n", dbuser);
1513                 ast_debug(1, "PostgreSQL RealTime Password: %s\n", dbpass);
1514                 ast_debug(1, "PostgreSQL RealTime DBName: %s\n", dbname);
1515         }
1516
1517         if (!pgsql_reconnect(NULL)) {
1518                 ast_log(LOG_WARNING,
1519                                 "PostgreSQL RealTime: Couldn't establish connection. Check debug.\n");
1520                 ast_debug(1, "PostgreSQL RealTime: Cannot Connect: %s\n", PQerrorMessage(pgsqlConn));
1521         }
1522
1523         ast_verb(2, "PostgreSQL RealTime reloaded.\n");
1524
1525         /* Done reloading. Release lock so others can now use driver. */
1526         ast_mutex_unlock(&pgsql_lock);
1527
1528         return 1;
1529 }
1530
1531 static int pgsql_reconnect(const char *database)
1532 {
1533         char my_database[50];
1534
1535         ast_copy_string(my_database, S_OR(database, dbname), sizeof(my_database));
1536
1537         /* mutex lock should have been locked before calling this function. */
1538
1539         if (pgsqlConn && PQstatus(pgsqlConn) != CONNECTION_OK) {
1540                 PQfinish(pgsqlConn);
1541                 pgsqlConn = NULL;
1542         }
1543
1544         /* DB password can legitimately be 0-length */
1545         if ((!pgsqlConn) && (!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) {
1546                 struct ast_str *connInfo = ast_str_create(128);
1547
1548                 ast_str_set(&connInfo, 0, "host=%s port=%d dbname=%s user=%s",
1549                         S_OR(dbhost, dbsock), dbport, my_database, dbuser);
1550                 if (!ast_strlen_zero(dbpass))
1551                         ast_str_append(&connInfo, 0, " password=%s", dbpass);
1552
1553                 ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo));
1554                 pgsqlConn = PQconnectdb(ast_str_buffer(connInfo));
1555                 ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo));
1556                 ast_free(connInfo);
1557                 connInfo = NULL;
1558
1559                 ast_debug(1, "pgsqlConn=%p\n", pgsqlConn);
1560                 if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
1561                         ast_debug(1, "PostgreSQL RealTime: Successfully connected to database.\n");
1562                         connect_time = time(NULL);
1563                         version = PQserverVersion(pgsqlConn);
1564                         return 1;
1565                 } else {
1566                         ast_log(LOG_ERROR,
1567                                         "PostgreSQL RealTime: Failed to connect database %s on %s: %s\n",
1568                                         my_database, dbhost, PQresultErrorMessage(NULL));
1569                         return 0;
1570                 }
1571         } else {
1572                 ast_debug(1, "PostgreSQL RealTime: One or more of the parameters in the config does not pass our validity checks.\n");
1573                 return 1;
1574         }
1575 }
1576
1577 static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1578 {
1579         struct tables *cur;
1580         int l, which;
1581         char *ret = NULL;
1582
1583         switch (cmd) {
1584         case CLI_INIT:
1585                 e->command = "realtime show pgsql cache";
1586                 e->usage =
1587                         "Usage: realtime show pgsql cache [<table>]\n"
1588                         "       Shows table cache for the PostgreSQL RealTime driver\n";
1589                 return NULL;
1590         case CLI_GENERATE:
1591                 if (a->argc != 4) {
1592                         return NULL;
1593                 }
1594                 l = strlen(a->word);
1595                 which = 0;
1596                 AST_LIST_LOCK(&psql_tables);
1597                 AST_LIST_TRAVERSE(&psql_tables, cur, list) {
1598                         if (!strncasecmp(a->word, cur->name, l) && ++which > a->n) {
1599                                 ret = ast_strdup(cur->name);
1600                                 break;
1601                         }
1602                 }
1603                 AST_LIST_UNLOCK(&psql_tables);
1604                 return ret;
1605         }
1606
1607         if (a->argc == 4) {
1608                 /* List of tables */
1609                 AST_LIST_LOCK(&psql_tables);
1610                 AST_LIST_TRAVERSE(&psql_tables, cur, list) {
1611                         ast_cli(a->fd, "%s\n", cur->name);
1612                 }
1613                 AST_LIST_UNLOCK(&psql_tables);
1614         } else if (a->argc == 5) {
1615                 /* List of columns */
1616                 if ((cur = find_table(NULL, a->argv[4]))) {
1617                         struct columns *col;
1618                         ast_cli(a->fd, "Columns for Table Cache '%s':\n", a->argv[4]);
1619                         ast_cli(a->fd, "%-20.20s %-20.20s %-3.3s %-8.8s\n", "Name", "Type", "Len", "Nullable");
1620                         AST_LIST_TRAVERSE(&cur->columns, col, list) {
1621                                 ast_cli(a->fd, "%-20.20s %-20.20s %3d %-8.8s\n", col->name, col->type, col->len, col->notnull ? "NOT NULL" : "");
1622                         }
1623                         release_table(cur);
1624                 } else {
1625                         ast_cli(a->fd, "No such table '%s'\n", a->argv[4]);
1626                 }
1627         }
1628         return 0;
1629 }
1630
1631 static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1632 {
1633         char status[256], credentials[100] = "";
1634         int ctimesec = time(NULL) - connect_time;
1635
1636         switch (cmd) {
1637         case CLI_INIT:
1638                 e->command = "realtime show pgsql status";
1639                 e->usage =
1640                         "Usage: realtime show pgsql status\n"
1641                         "       Shows connection information for the PostgreSQL RealTime driver\n";
1642                 return NULL;
1643         case CLI_GENERATE:
1644                 return NULL;
1645         }
1646
1647         if (a->argc != 4)
1648                 return CLI_SHOWUSAGE;
1649
1650         if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
1651                 if (!ast_strlen_zero(dbhost))
1652                         snprintf(status, sizeof(status), "Connected to %s@%s, port %d", dbname, dbhost, dbport);
1653                 else if (!ast_strlen_zero(dbsock))
1654                         snprintf(status, sizeof(status), "Connected to %s on socket file %s", dbname, dbsock);
1655                 else
1656                         snprintf(status, sizeof(status), "Connected to %s@%s", dbname, dbhost);
1657
1658                 if (!ast_strlen_zero(dbuser))
1659                         snprintf(credentials, sizeof(credentials), " with username %s", dbuser);
1660
1661                 if (ctimesec > 31536000)
1662                         ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
1663                                         status, credentials, ctimesec / 31536000, (ctimesec % 31536000) / 86400,
1664                                         (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60, ctimesec % 60);
1665                 else if (ctimesec > 86400)
1666                         ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status,
1667                                         credentials, ctimesec / 86400, (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60,
1668                                         ctimesec % 60);
1669                 else if (ctimesec > 3600)
1670                         ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, credentials,
1671                                         ctimesec / 3600, (ctimesec % 3600) / 60, ctimesec % 60);
1672                 else if (ctimesec > 60)
1673                         ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials, ctimesec / 60,
1674                                         ctimesec % 60);
1675                 else
1676                         ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec);
1677
1678                 return CLI_SUCCESS;
1679         } else {
1680                 return CLI_FAILURE;
1681         }
1682 }
1683
1684 /* needs usecount semantics defined */
1685 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL RealTime Configuration Driver",
1686                 .load = load_module,
1687                 .unload = unload_module,
1688                 .reload = reload,
1689                 .load_pri = AST_MODPRI_REALTIME_DRIVER,
1690                );