e94e9a71d6fe6bfaabef7f967ddfdafde45f691b
[exim.git] / src / src / lookups / pgsql.c
1 /* $Cambridge: exim/src/src/lookups/pgsql.c,v 1.11 2009/11/16 19:50:38 nm4 Exp $ */
2
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10 /* Thanks to Petr Cech for contributing the original code for these
11 functions. Thanks to Joachim Wieland for the initial patch for the Unix domain
12 socket extension. */
13
14 #include "../exim.h"
15 #include "lf_functions.h"
16
17 #include <libpq-fe.h> /* The system header */
18
19 /* Structure and anchor for caching connections. */
20
21 typedef struct pgsql_connection {
22 struct pgsql_connection *next;
23 uschar *server;
24 PGconn *handle;
25 } pgsql_connection;
26
27 static pgsql_connection *pgsql_connections = NULL;
28
29
30
31 /*************************************************
32 * Open entry point *
33 *************************************************/
34
35 /* See local README for interface description. */
36
37 static void *
38 pgsql_open(uschar *filename, uschar **errmsg)
39 {
40 return (void *)(1); /* Just return something non-null */
41 }
42
43
44
45 /*************************************************
46 * Tidy entry point *
47 *************************************************/
48
49 /* See local README for interface description. */
50
51 static void
52 pgsql_tidy(void)
53 {
54 pgsql_connection *cn;
55 while ((cn = pgsql_connections) != NULL)
56 {
57 pgsql_connections = cn->next;
58 DEBUG(D_lookup) debug_printf("close PGSQL connection: %s\n", cn->server);
59 PQfinish(cn->handle);
60 }
61 }
62
63
64 /*************************************************
65 * Notice processor function for pgsql *
66 *************************************************/
67
68 /* This function is passed to pgsql below, and called for any PostgreSQL
69 "notices". By default they are written to stderr, which is undesirable.
70
71 Arguments:
72 arg an opaque user cookie (not used)
73 message the notice
74
75 Returns: nothing
76 */
77
78 static void
79 notice_processor(void *arg, const char *message)
80 {
81 arg = arg; /* Keep compiler happy */
82 DEBUG(D_lookup) debug_printf("PGSQL: %s\n", message);
83 }
84
85
86
87 /*************************************************
88 * Internal search function *
89 *************************************************/
90
91 /* This function is called from the find entry point to do the search for a
92 single server. The server string is of the form "server/dbname/user/password".
93
94 PostgreSQL supports connections through Unix domain sockets. This is usually
95 faster and costs less cpu time than a TCP/IP connection. However it can only be
96 used if the mail server runs on the same machine as the database server. A
97 configuration line for PostgreSQL via Unix domain sockets looks like this:
98
99 hide pgsql_servers = (/tmp/.s.PGSQL.5432)/db/user/password[:<nextserver>]
100
101 We enclose the path name in parentheses so that its slashes aren't visually
102 confused with the delimeters for the other pgsql_server settings.
103
104 For TCP/IP connections, the server is a host name and optional port (with a
105 colon separator).
106
107 NOTE:
108 1) All three '/' must be present.
109 2) If host is omitted the local unix socket is used.
110
111 Arguments:
112 query the query string
113 server the server string; this is in dynamic memory and can be updated
114 resultptr where to store the result
115 errmsg where to point an error message
116 defer_break set TRUE if no more servers are to be tried after DEFER
117 do_cache set FALSE if data is changed
118
119 Returns: OK, FAIL, or DEFER
120 */
121
122 static int
123 perform_pgsql_search(uschar *query, uschar *server, uschar **resultptr,
124 uschar **errmsg, BOOL *defer_break, BOOL *do_cache)
125 {
126 PGconn *pg_conn = NULL;
127 PGresult *pg_result = NULL;
128
129 int i;
130 int ssize = 0;
131 int offset = 0;
132 int yield = DEFER;
133 unsigned int num_fields, num_tuples;
134 uschar *result = NULL;
135 pgsql_connection *cn;
136 uschar *server_copy = NULL;
137 uschar *sdata[3];
138
139 /* Disaggregate the parameters from the server argument. The order is host or
140 path, database, user, password. We can write to the string, since it is in a
141 nextinlist temporary buffer. The copy of the string that is used for caching
142 has the password removed. This copy is also used for debugging output. */
143
144 for (i = 2; i >= 0; i--)
145 {
146 uschar *pp = Ustrrchr(server, '/');
147 if (pp == NULL)
148 {
149 *errmsg = string_sprintf("incomplete pgSQL server data: %s",
150 (i == 2)? server : server_copy);
151 *defer_break = TRUE;
152 return DEFER;
153 }
154 *pp++ = 0;
155 sdata[i] = pp;
156 if (i == 2) server_copy = string_copy(server); /* sans password */
157 }
158
159 /* The total server string has now been truncated so that what is left at the
160 start is the identification of the server (host or path). See if we have a
161 cached connection to the server. */
162
163 for (cn = pgsql_connections; cn != NULL; cn = cn->next)
164 {
165 if (Ustrcmp(cn->server, server_copy) == 0)
166 {
167 pg_conn = cn->handle;
168 break;
169 }
170 }
171
172 /* If there is no cached connection, we must set one up. */
173
174 if (cn == NULL)
175 {
176 uschar *port = US"";
177
178 /* For a Unix domain socket connection, the path is in parentheses */
179
180 if (*server == '(')
181 {
182 uschar *last_slash, *last_dot, *p;
183
184 p = ++server;
185 while (*p != 0 && *p != ')') p++;
186 *p = 0;
187
188 last_slash = Ustrrchr(server, '/');
189 last_dot = Ustrrchr(server, '.');
190
191 DEBUG(D_lookup) debug_printf("PGSQL new connection: socket=%s "
192 "database=%s user=%s\n", server, sdata[0], sdata[1]);
193
194 /* A valid socket name looks like this: /var/run/postgresql/.s.PGSQL.5432
195 We have to call PQsetdbLogin with '/var/run/postgresql' as the hostname
196 argument and put '5432' into the port variable. */
197
198 if (last_slash == NULL || last_dot == NULL)
199 {
200 *errmsg = string_sprintf("PGSQL invalid filename for socket: %s",
201 server);
202 *defer_break = TRUE;
203 return DEFER;
204 }
205
206 /* Terminate the path name and set up the port: we'll have something like
207 server = "/var/run/postgresql" and port = "5432". */
208
209 *last_slash = 0;
210 port = last_dot + 1;
211 }
212
213 /* Host connection; sort out the port */
214
215 else
216 {
217 uschar *p;
218 if ((p = Ustrchr(server, ':')) != NULL)
219 {
220 *p++ = 0;
221 port = p;
222 }
223
224 if (Ustrchr(server, '/') != NULL)
225 {
226 *errmsg = string_sprintf("unexpected slash in pgSQL server hostname: %s",
227 server);
228 *defer_break = TRUE;
229 return DEFER;
230 }
231
232 DEBUG(D_lookup) debug_printf("PGSQL new connection: host=%s port=%s "
233 "database=%s user=%s\n", server, port, sdata[0], sdata[1]);
234 }
235
236 /* If the database is the empty string, set it NULL - the query must then
237 define it. */
238
239 if (sdata[0][0] == 0) sdata[0] = NULL;
240
241 /* Get store for a new handle, initialize it, and connect to the server */
242
243 pg_conn=PQsetdbLogin(
244 /* host port options tty database user passwd */
245 CS server, CS port, NULL, NULL, CS sdata[0], CS sdata[1], CS sdata[2]);
246
247 if(PQstatus(pg_conn) == CONNECTION_BAD)
248 {
249 store_reset(server_copy);
250 *errmsg = string_sprintf("PGSQL connection failed: %s",
251 PQerrorMessage(pg_conn));
252 PQfinish(pg_conn);
253 goto PGSQL_EXIT;
254 }
255
256 /* Set the client encoding to SQL_ASCII, which means that the server will
257 not try to interpret the query as being in any fancy encoding such as UTF-8
258 or other multibyte code that might cause problems with escaping. */
259
260 PQsetClientEncoding(pg_conn, "SQL_ASCII");
261
262 /* Set the notice processor to prevent notices from being written to stderr
263 (which is what the default does). Our function (above) just produces debug
264 output. */
265
266 PQsetNoticeProcessor(pg_conn, notice_processor, NULL);
267
268 /* Add the connection to the cache */
269
270 cn = store_get(sizeof(pgsql_connection));
271 cn->server = server_copy;
272 cn->handle = pg_conn;
273 cn->next = pgsql_connections;
274 pgsql_connections = cn;
275 }
276
277 /* Else use a previously cached connection */
278
279 else
280 {
281 DEBUG(D_lookup) debug_printf("PGSQL using cached connection for %s\n",
282 server_copy);
283 }
284
285 /* Run the query */
286
287 pg_result = PQexec(pg_conn, CS query);
288 switch(PQresultStatus(pg_result))
289 {
290 case PGRES_EMPTY_QUERY:
291 case PGRES_COMMAND_OK:
292 /* The command was successful but did not return any data since it was
293 * not SELECT but either an INSERT, UPDATE or DELETE statement. Tell the
294 * high level code to not cache this query, and clean the current cache for
295 * this handle by setting *do_cache FALSE. */
296 result = string_copy(US PQcmdTuples(pg_result));
297 offset = Ustrlen(result);
298 *do_cache = FALSE;
299 DEBUG(D_lookup) debug_printf("PGSQL: command does not return any data "
300 "but was successful. Rows affected: %s\n", result);
301
302 case PGRES_TUPLES_OK:
303 break;
304
305 default:
306 /* This was the original code:
307 *errmsg = string_sprintf("PGSQL: query failed: %s\n",
308 PQresultErrorMessage(pg_result));
309 This was suggested by a user:
310 */
311
312 *errmsg = string_sprintf("PGSQL: query failed: %s (%s) (%s)\n",
313 PQresultErrorMessage(pg_result),
314 PQresStatus(PQresultStatus(pg_result)), query);
315 goto PGSQL_EXIT;
316 }
317
318 /* Result is in pg_result. Find the number of fields returned. If this is one,
319 we don't add field names to the data. Otherwise we do. If the query did not
320 return anything we skip the for loop; this also applies to the case
321 PGRES_COMMAND_OK. */
322
323 num_fields = PQnfields(pg_result);
324 num_tuples = PQntuples(pg_result);
325
326 /* Get the fields and construct the result string. If there is more than one
327 row, we insert '\n' between them. */
328
329 for (i = 0; i < num_tuples; i++)
330 {
331 if (result != NULL)
332 result = string_cat(result, &ssize, &offset, US"\n", 1);
333
334 if (num_fields == 1)
335 {
336 result = string_cat(result, &ssize, &offset,
337 US PQgetvalue(pg_result, i, 0), PQgetlength(pg_result, i, 0));
338 }
339
340 else
341 {
342 int j;
343 for (j = 0; j < num_fields; j++)
344 {
345 uschar *tmp = US PQgetvalue(pg_result, i, j);
346 result = lf_quote(US PQfname(pg_result, j), tmp, Ustrlen(tmp), result,
347 &ssize, &offset);
348 }
349 }
350 }
351
352 /* If result is NULL then no data has been found and so we return FAIL.
353 Otherwise, we must terminate the string which has been built; string_cat()
354 always leaves enough room for a terminating zero. */
355
356 if (result == NULL)
357 {
358 yield = FAIL;
359 *errmsg = US"PGSQL: no data found";
360 }
361 else
362 {
363 result[offset] = 0;
364 store_reset(result + offset + 1);
365 }
366
367 /* Get here by goto from various error checks. */
368
369 PGSQL_EXIT:
370
371 /* Free store for any result that was got; don't close the connection, as
372 it is cached. */
373
374 if (pg_result != NULL) PQclear(pg_result);
375
376 /* Non-NULL result indicates a sucessful result */
377
378 if (result != NULL)
379 {
380 *resultptr = result;
381 return OK;
382 }
383 else
384 {
385 DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
386 return yield; /* FAIL or DEFER */
387 }
388 }
389
390
391
392
393 /*************************************************
394 * Find entry point *
395 *************************************************/
396
397 /* See local README for interface description. The handle and filename
398 arguments are not used. The code to loop through a list of servers while the
399 query is deferred with a retryable error is now in a separate function that is
400 shared with other SQL lookups. */
401
402 static int
403 pgsql_find(void *handle, uschar *filename, uschar *query, int length,
404 uschar **result, uschar **errmsg, BOOL *do_cache)
405 {
406 return lf_sqlperform(US"PostgreSQL", US"pgsql_servers", pgsql_servers, query,
407 result, errmsg, do_cache, perform_pgsql_search);
408 }
409
410
411
412 /*************************************************
413 * Quote entry point *
414 *************************************************/
415
416 /* The characters that always need to be quoted (with backslash) are newline,
417 tab, carriage return, backspace, backslash itself, and the quote characters.
418 Percent and underscore are only special in contexts where they can be wild
419 cards, and this isn't usually the case for data inserted from messages, since
420 that isn't likely to be treated as a pattern of any kind. However, pgsql seems
421 to allow escaping "on spec". If you use something like "where id="ab\%cd" it
422 does treat the string as "ab%cd". So we can safely quote percent and
423 underscore. [This is different to MySQL, where you can't do this.]
424
425 The original code quoted single quotes as \' which is documented as valid in
426 the O'Reilly book "Practical PostgreSQL" (first edition) as an alternative to
427 the SQL standard '' way of representing a single quote as data. However, in
428 June 2006 there was some security issue with using \' and so this has been
429 changed.
430
431 [Note: There is a function called PQescapeStringConn() that quotes strings.
432 This cannot be used because it needs a PGconn argument (the connection handle).
433 Why, I don't know. Seems odd for just string escaping...]
434
435 Arguments:
436 s the string to be quoted
437 opt additional option text or NULL if none
438
439 Returns: the processed string or NULL for a bad option
440 */
441
442 static uschar *
443 pgsql_quote(uschar *s, uschar *opt)
444 {
445 register int c;
446 int count = 0;
447 uschar *t = s;
448 uschar *quoted;
449
450 if (opt != NULL) return NULL; /* No options recognized */
451
452 while ((c = *t++) != 0)
453 if (Ustrchr("\n\t\r\b\'\"\\%_", c) != NULL) count++;
454
455 if (count == 0) return s;
456 t = quoted = store_get(Ustrlen(s) + count + 1);
457
458 while ((c = *s++) != 0)
459 {
460 if (c == '\'')
461 {
462 *t++ = '\'';
463 *t++ = '\'';
464 }
465 else if (Ustrchr("\n\t\r\b\"\\%_", c) != NULL)
466 {
467 *t++ = '\\';
468 switch(c)
469 {
470 case '\n': *t++ = 'n';
471 break;
472 case '\t': *t++ = 't';
473 break;
474 case '\r': *t++ = 'r';
475 break;
476 case '\b': *t++ = 'b';
477 break;
478 default: *t++ = c;
479 break;
480 }
481 }
482 else *t++ = c;
483 }
484
485 *t = 0;
486 return quoted;
487 }
488
489
490 /*************************************************
491 * Version reporting entry point *
492 *************************************************/
493
494 /* See local README for interface description. */
495
496 #include "../version.h"
497
498 void
499 pgsql_version_report(FILE *f)
500 {
501 #ifdef DYNLOOKUP
502 fprintf(f, "Library version: PostgreSQL: Exim version %s\n", EXIM_VERSION_STR);
503 #endif
504
505 /* Version reporting: there appears to be no available information about
506 the client library in libpq-fe.h; once you have a connection object, you
507 can access the server version and the chosen protocol version, but those
508 aren't really what we want. It might make sense to debug_printf those
509 when the connection is established though? */
510 }
511
512
513 static lookup_info _lookup_info = {
514 US"pgsql", /* lookup name */
515 lookup_querystyle, /* query-style lookup */
516 pgsql_open, /* open function */
517 NULL, /* no check function */
518 pgsql_find, /* find function */
519 NULL, /* no close function */
520 pgsql_tidy, /* tidy function */
521 pgsql_quote, /* quoting function */
522 pgsql_version_report /* version reporting */
523 };
524
525 #ifdef DYNLOOKUP
526 #define pgsql_lookup_module_info _lookup_module_info
527 #endif
528
529 static lookup_info *_lookup_list[] = { &_lookup_info };
530 lookup_module_info pgsql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
531
532 /* End of lookups/pgsql.c */