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