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