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