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