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