1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* Thanks to Petr Cech for contributing the original code for these
9 functions. Thanks to Joachim Wieland for the initial patch for the Unix domain
13 #include "lf_functions.h"
15 #include <libpq-fe.h> /* The system header */
17 /* Structure and anchor for caching connections. */
19 typedef struct pgsql_connection {
20 struct pgsql_connection *next;
25 static pgsql_connection *pgsql_connections = NULL;
29 /*************************************************
31 *************************************************/
33 /* See local README for interface description. */
36 pgsql_open(const uschar * filename, uschar ** errmsg)
38 return (void *)(1); /* Just return something non-null */
43 /*************************************************
45 *************************************************/
47 /* See local README for interface description. */
53 while ((cn = pgsql_connections) != NULL)
55 pgsql_connections = cn->next;
56 DEBUG(D_lookup) debug_printf_indent("close PGSQL connection: %s\n", cn->server);
62 /*************************************************
63 * Notice processor function for pgsql *
64 *************************************************/
66 /* This function is passed to pgsql below, and called for any PostgreSQL
67 "notices". By default they are written to stderr, which is undesirable.
70 arg an opaque user cookie (not used)
77 notice_processor(void *arg, const char *message)
79 arg = arg; /* Keep compiler happy */
80 DEBUG(D_lookup) debug_printf_indent("PGSQL: %s\n", message);
85 /*************************************************
86 * Internal search function *
87 *************************************************/
89 /* This function is called from the find entry point to do the search for a
90 single server. The server string is of the form "server/dbname/user/password".
92 PostgreSQL supports connections through Unix domain sockets. This is usually
93 faster and costs less cpu time than a TCP/IP connection. However it can only be
94 used if the mail server runs on the same machine as the database server. A
95 configuration line for PostgreSQL via Unix domain sockets looks like this:
97 hide pgsql_servers = (/tmp/.s.PGSQL.5432)/db/user/password[:<nextserver>]
99 We enclose the path name in parentheses so that its slashes aren't visually
100 confused with the delimiters for the other pgsql_server settings.
102 For TCP/IP connections, the server is a host name and optional port (with a
106 1) All three '/' must be present.
107 2) If host is omitted the local unix socket is used.
110 query the query string
111 server the server string; this is in dynamic memory and can be updated
112 resultptr where to store the result
113 errmsg where to point an error message
114 defer_break set TRUE if no more servers are to be tried after DEFER
115 do_cache set FALSE if data is changed
118 Returns: OK, FAIL, or DEFER
122 perform_pgsql_search(const uschar *query, uschar *server, uschar **resultptr,
123 uschar **errmsg, BOOL *defer_break, uint *do_cache, const uschar * opts)
125 PGconn *pg_conn = NULL;
126 PGresult *pg_result = NULL;
128 gstring * result = NULL;
130 unsigned int num_fields, num_tuples;
131 pgsql_connection *cn;
132 rmark reset_point = store_mark();
133 uschar *server_copy = NULL;
136 /* Disaggregate the parameters from the server argument. The order is host or
137 path, database, user, password. We can write to the string, since it is in a
138 nextinlist temporary buffer. The copy of the string that is used for caching
139 has the password removed. This copy is also used for debugging output. */
141 for (int i = 2; i >= 0; i--)
143 uschar *pp = Ustrrchr(server, '/');
146 *errmsg = string_sprintf("incomplete pgSQL server data: %s",
147 (i == 2)? server : server_copy);
153 if (i == 2) server_copy = string_copy(server); /* sans password */
156 /* The total server string has now been truncated so that what is left at the
157 start is the identification of the server (host or path). See if we have a
158 cached connection to the server. */
160 for (cn = pgsql_connections; cn; cn = cn->next)
161 if (Ustrcmp(cn->server, server_copy) == 0)
163 pg_conn = cn->handle;
167 /* If there is no cached connection, we must set one up. */
173 /* For a Unix domain socket connection, the path is in parentheses */
177 uschar *last_slash, *last_dot, *p;
180 while (*p && *p != ')') p++;
183 last_slash = Ustrrchr(server, '/');
184 last_dot = Ustrrchr(server, '.');
186 DEBUG(D_lookup) debug_printf_indent("PGSQL new connection: socket=%s "
187 "database=%s user=%s\n", server, sdata[0], sdata[1]);
189 /* A valid socket name looks like this: /var/run/postgresql/.s.PGSQL.5432
190 We have to call PQsetdbLogin with '/var/run/postgresql' as the hostname
191 argument and put '5432' into the port variable. */
193 if (!last_slash || !last_dot)
195 *errmsg = string_sprintf("PGSQL invalid filename for socket: %s", server);
200 /* Terminate the path name and set up the port: we'll have something like
201 server = "/var/run/postgresql" and port = "5432". */
207 /* Host connection; sort out the port */
212 if ((p = Ustrchr(server, ':')))
218 if (Ustrchr(server, '/'))
220 *errmsg = string_sprintf("unexpected slash in pgSQL server hostname: %s",
226 DEBUG(D_lookup) debug_printf_indent("PGSQL new connection: host=%s port=%s "
227 "database=%s user=%s\n", server, port, sdata[0], sdata[1]);
230 /* If the database is the empty string, set it NULL - the query must then
233 if (sdata[0][0] == 0) sdata[0] = NULL;
235 /* Get store for a new handle, initialize it, and connect to the server */
237 pg_conn=PQsetdbLogin(
238 /* host port options tty database user passwd */
239 CS server, CS port, NULL, NULL, CS sdata[0], CS sdata[1], CS sdata[2]);
241 if(PQstatus(pg_conn) == CONNECTION_BAD)
243 reset_point = store_reset(reset_point);
244 *errmsg = string_sprintf("PGSQL connection failed: %s",
245 PQerrorMessage(pg_conn));
250 /* Set the client encoding to SQL_ASCII, which means that the server will
251 not try to interpret the query as being in any fancy encoding such as UTF-8
252 or other multibyte code that might cause problems with escaping. */
254 PQsetClientEncoding(pg_conn, "SQL_ASCII");
256 /* Set the notice processor to prevent notices from being written to stderr
257 (which is what the default does). Our function (above) just produces debug
260 PQsetNoticeProcessor(pg_conn, notice_processor, NULL);
262 /* Add the connection to the cache */
264 cn = store_get(sizeof(pgsql_connection), FALSE);
265 cn->server = server_copy;
266 cn->handle = pg_conn;
267 cn->next = pgsql_connections;
268 pgsql_connections = cn;
271 /* Else use a previously cached connection */
275 DEBUG(D_lookup) debug_printf_indent("PGSQL using cached connection for %s\n",
281 pg_result = PQexec(pg_conn, CS query);
282 switch(PQresultStatus(pg_result))
284 case PGRES_EMPTY_QUERY:
285 case PGRES_COMMAND_OK:
286 /* The command was successful but did not return any data since it was
287 not SELECT but either an INSERT, UPDATE or DELETE statement. Tell the
288 high level code to not cache this query, and clean the current cache for
289 this handle by setting *do_cache zero. */
291 result = string_cat(result, US PQcmdTuples(pg_result));
293 DEBUG(D_lookup) debug_printf_indent("PGSQL: command does not return any data "
294 "but was successful. Rows affected: %s\n", string_from_gstring(result));
297 case PGRES_TUPLES_OK:
301 /* This was the original code:
302 *errmsg = string_sprintf("PGSQL: query failed: %s\n",
303 PQresultErrorMessage(pg_result));
304 This was suggested by a user:
307 *errmsg = string_sprintf("PGSQL: query failed: %s (%s) (%s)\n",
308 PQresultErrorMessage(pg_result),
309 PQresStatus(PQresultStatus(pg_result)), query);
313 /* Result is in pg_result. Find the number of fields returned. If this is one,
314 we don't add field names to the data. Otherwise we do. If the query did not
315 return anything we skip the for loop; this also applies to the case
318 num_fields = PQnfields(pg_result);
319 num_tuples = PQntuples(pg_result);
321 /* Get the fields and construct the result string. If there is more than one
322 row, we insert '\n' between them. */
324 for (int i = 0; i < num_tuples; i++)
327 result = string_catn(result, US"\n", 1);
330 result = string_catn(result,
331 US PQgetvalue(pg_result, i, 0), PQgetlength(pg_result, i, 0));
333 for (int j = 0; j < num_fields; j++)
335 uschar *tmp = US PQgetvalue(pg_result, i, j);
336 result = lf_quote(US PQfname(pg_result, j), tmp, Ustrlen(tmp), result);
340 /* If result is NULL then no data has been found and so we return FAIL. */
345 *errmsg = US"PGSQL: no data found";
348 /* Get here by goto from various error checks. */
352 /* Free store for any result that was got; don't close the connection, as
355 if (pg_result) PQclear(pg_result);
357 /* Non-NULL result indicates a successful result */
361 gstring_release_unused(result);
362 *resultptr = string_from_gstring(result);
367 DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
368 return yield; /* FAIL or DEFER */
375 /*************************************************
377 *************************************************/
379 /* See local README for interface description. The handle and filename
380 arguments are not used. The code to loop through a list of servers while the
381 query is deferred with a retryable error is now in a separate function that is
382 shared with other SQL lookups. */
385 pgsql_find(void * handle, const uschar * filename, const uschar * query,
386 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
389 return lf_sqlperform(US"PostgreSQL", US"pgsql_servers", pgsql_servers, query,
390 result, errmsg, do_cache, opts, perform_pgsql_search);
395 /*************************************************
396 * Quote entry point *
397 *************************************************/
399 /* The characters that always need to be quoted (with backslash) are newline,
400 tab, carriage return, backspace, backslash itself, and the quote characters.
402 The original code quoted single quotes as \' which is documented as valid in
403 the O'Reilly book "Practical PostgreSQL" (first edition) as an alternative to
404 the SQL standard '' way of representing a single quote as data. However, in
405 June 2006 there was some security issue with using \' and so this has been
408 [Note: There is a function called PQescapeStringConn() that quotes strings.
409 This cannot be used because it needs a PGconn argument (the connection handle).
410 Why, I don't know. Seems odd for just string escaping...]
413 s the string to be quoted
414 opt additional option text or NULL if none
416 Returns: the processed string or NULL for a bad option
420 pgsql_quote(uschar *s, uschar *opt)
427 if (opt != NULL) return NULL; /* No options recognized */
429 while ((c = *t++) != 0)
430 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
432 if (count == 0) return s;
433 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
435 while ((c = *s++) != 0)
442 else if (Ustrchr("\n\t\r\b\"\\", c) != NULL)
447 case '\n': *t++ = 'n';
449 case '\t': *t++ = 't';
451 case '\r': *t++ = 'r';
453 case '\b': *t++ = 'b';
467 /*************************************************
468 * Version reporting entry point *
469 *************************************************/
471 /* See local README for interface description. */
473 #include "../version.h"
476 pgsql_version_report(FILE *f)
479 fprintf(f, "Library version: PostgreSQL: Exim version %s\n", EXIM_VERSION_STR);
482 /* Version reporting: there appears to be no available information about
483 the client library in libpq-fe.h; once you have a connection object, you
484 can access the server version and the chosen protocol version, but those
485 aren't really what we want. It might make sense to debug_printf those
486 when the connection is established though? */
490 static lookup_info _lookup_info = {
491 .name = US"pgsql", /* lookup name */
492 .type = lookup_querystyle, /* query-style lookup */
493 .open = pgsql_open, /* open function */
494 .check = NULL, /* no check function */
495 .find = pgsql_find, /* find function */
496 .close = NULL, /* no close function */
497 .tidy = pgsql_tidy, /* tidy function */
498 .quote = pgsql_quote, /* quoting function */
499 .version_report = pgsql_version_report /* version reporting */
503 #define pgsql_lookup_module_info _lookup_module_info
506 static lookup_info *_lookup_list[] = { &_lookup_info };
507 lookup_module_info pgsql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
509 /* End of lookups/pgsql.c */