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