Import Upstream version 4.89
[hcoop/debian/exim4.git] / src / lookups / mysql.c
CommitLineData
420a0d19
CE
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
2813c06e 5/* Copyright (c) University of Cambridge 1995 - 2015 */
420a0d19
CE
6/* See the file NOTICE for conditions of use and distribution. */
7
8/* Thanks to Paul Kelly for contributing the original code for these
9functions. */
10
11
12#include "../exim.h"
13#include "lf_functions.h"
14
15#include <mysql.h> /* The system header */
16
17
18/* Structure and anchor for caching connections. */
19
20typedef struct mysql_connection {
21 struct mysql_connection *next;
22 uschar *server;
23 MYSQL *handle;
24} mysql_connection;
25
26static mysql_connection *mysql_connections = NULL;
27
28
29
30/*************************************************
31* Open entry point *
32*************************************************/
33
34/* See local README for interface description. */
35
36static void *
37mysql_open(uschar *filename, uschar **errmsg)
38{
39return (void *)(1); /* Just return something non-null */
40}
41
42
43
44/*************************************************
45* Tidy entry point *
46*************************************************/
47
48/* See local README for interface description. */
49
50static void
51mysql_tidy(void)
52{
53mysql_connection *cn;
54while ((cn = mysql_connections) != NULL)
55 {
56 mysql_connections = cn->next;
57 DEBUG(D_lookup) debug_printf("close MYSQL connection: %s\n", cn->server);
58 mysql_close(cn->handle);
59 }
60}
61
62
63
64/*************************************************
65* Internal search function *
66*************************************************/
67
68/* This function is called from the find entry point to do the search for a
69single server.
70
71Arguments:
72 query the query string
73 server the server string
74 resultptr where to store the result
75 errmsg where to point an error message
76 defer_break TRUE if no more servers are to be tried after DEFER
2813c06e 77 do_cache set zero if data is changed
420a0d19
CE
78
79The server string is of the form "host/dbname/user/password". The host can be
80host:port. This string is in a nextinlist temporary buffer, so can be
81overwritten.
82
83Returns: OK, FAIL, or DEFER
84*/
85
86static int
2813c06e
CE
87perform_mysql_search(const uschar *query, uschar *server, uschar **resultptr,
88 uschar **errmsg, BOOL *defer_break, uint *do_cache)
420a0d19
CE
89{
90MYSQL *mysql_handle = NULL; /* Keep compilers happy */
91MYSQL_RES *mysql_result = NULL;
92MYSQL_ROW mysql_row_data;
93MYSQL_FIELD *fields;
94
95int i;
96int ssize = 0;
97int offset = 0;
98int yield = DEFER;
99unsigned int num_fields;
100uschar *result = NULL;
101mysql_connection *cn;
102uschar *server_copy = NULL;
103uschar *sdata[4];
104
105/* Disaggregate the parameters from the server argument. The order is host,
106database, user, password. We can write to the string, since it is in a
107nextinlist temporary buffer. The copy of the string that is used for caching
108has the password removed. This copy is also used for debugging output. */
109
110for (i = 3; i > 0; i--)
111 {
112 uschar *pp = Ustrrchr(server, '/');
113 if (pp == NULL)
114 {
115 *errmsg = string_sprintf("incomplete MySQL server data: %s",
116 (i == 3)? server : server_copy);
117 *defer_break = TRUE;
118 return DEFER;
119 }
120 *pp++ = 0;
121 sdata[i] = pp;
122 if (i == 3) server_copy = string_copy(server); /* sans password */
123 }
124sdata[0] = server; /* What's left at the start */
125
126/* See if we have a cached connection to the server */
127
2813c06e 128for (cn = mysql_connections; cn; cn = cn->next)
420a0d19
CE
129 if (Ustrcmp(cn->server, server_copy) == 0)
130 {
131 mysql_handle = cn->handle;
132 break;
133 }
420a0d19
CE
134
135/* If no cached connection, we must set one up. Mysql allows for a host name
136and port to be specified. It also allows the name of a Unix socket to be used.
137Unfortunately, this contains slashes, but its use is expected to be rare, so
138the rather cumbersome syntax shouldn't inconvenience too many people. We use
2813c06e
CE
139this: host:port(socket)[group] where all the parts are optional.
140The "group" parameter specifies an option group from a MySQL option file. */
420a0d19 141
2813c06e 142if (!cn)
420a0d19
CE
143 {
144 uschar *p;
145 uschar *socket = NULL;
146 int port = 0;
2813c06e
CE
147 uschar *group = US"exim";
148
149 if ((p = Ustrchr(sdata[0], '[')))
150 {
151 *p++ = 0;
152 group = p;
153 while (*p && *p != ']') p++;
154 *p = 0;
155 }
420a0d19 156
2813c06e 157 if ((p = Ustrchr(sdata[0], '(')))
420a0d19
CE
158 {
159 *p++ = 0;
160 socket = p;
2813c06e 161 while (*p && *p != ')') p++;
420a0d19
CE
162 *p = 0;
163 }
164
2813c06e 165 if ((p = Ustrchr(sdata[0], ':')))
420a0d19
CE
166 {
167 *p++ = 0;
168 port = Uatoi(p);
169 }
170
2813c06e 171 if (Ustrchr(sdata[0], '/'))
420a0d19
CE
172 {
173 *errmsg = string_sprintf("unexpected slash in MySQL server hostname: %s",
174 sdata[0]);
175 *defer_break = TRUE;
176 return DEFER;
177 }
178
179 /* If the database is the empty string, set it NULL - the query must then
180 define it. */
181
182 if (sdata[1][0] == 0) sdata[1] = NULL;
183
184 DEBUG(D_lookup)
185 debug_printf("MYSQL new connection: host=%s port=%d socket=%s "
186 "database=%s user=%s\n", sdata[0], port, socket, sdata[1], sdata[2]);
187
188 /* Get store for a new handle, initialize it, and connect to the server */
189
190 mysql_handle = store_get(sizeof(MYSQL));
191 mysql_init(mysql_handle);
2813c06e 192 mysql_options(mysql_handle, MYSQL_READ_DEFAULT_GROUP, CS group);
420a0d19
CE
193 if (mysql_real_connect(mysql_handle,
194 /* host user passwd database */
195 CS sdata[0], CS sdata[2], CS sdata[3], CS sdata[1],
196 port, CS socket, CLIENT_MULTI_RESULTS) == NULL)
197 {
198 *errmsg = string_sprintf("MYSQL connection failed: %s",
199 mysql_error(mysql_handle));
200 *defer_break = FALSE;
201 goto MYSQL_EXIT;
202 }
203
204 /* Add the connection to the cache */
205
206 cn = store_get(sizeof(mysql_connection));
207 cn->server = server_copy;
208 cn->handle = mysql_handle;
209 cn->next = mysql_connections;
210 mysql_connections = cn;
211 }
212
213/* Else use a previously cached connection */
214
215else
216 {
217 DEBUG(D_lookup)
218 debug_printf("MYSQL using cached connection for %s\n", server_copy);
219 }
220
221/* Run the query */
222
223if (mysql_query(mysql_handle, CS query) != 0)
224 {
225 *errmsg = string_sprintf("MYSQL: query failed: %s\n",
226 mysql_error(mysql_handle));
227 *defer_break = FALSE;
228 goto MYSQL_EXIT;
229 }
230
231/* Pick up the result. If the query was not of the type that returns data,
232namely INSERT, UPDATE, or DELETE, an error occurs here. However, this situation
233can be detected by calling mysql_field_count(). If its result is zero, no data
234was expected (this is all explained clearly in the MySQL manual). In this case,
235we return the number of rows affected by the command. In this event, we do NOT
236want to cache the result; also the whole cache for the handle must be cleaned
2813c06e 237up. Setting do_cache zero requests this. */
420a0d19
CE
238
239if ((mysql_result = mysql_use_result(mysql_handle)) == NULL)
240 {
241 if ( mysql_field_count(mysql_handle) == 0 )
242 {
243 DEBUG(D_lookup) debug_printf("MYSQL: query was not one that returns data\n");
244 result = string_sprintf("%d", mysql_affected_rows(mysql_handle));
2813c06e 245 *do_cache = 0;
420a0d19
CE
246 goto MYSQL_EXIT;
247 }
248 *errmsg = string_sprintf("MYSQL: lookup result failed: %s\n",
249 mysql_error(mysql_handle));
250 *defer_break = FALSE;
251 goto MYSQL_EXIT;
252 }
253
254/* Find the number of fields returned. If this is one, we don't add field
255names to the data. Otherwise we do. */
256
257num_fields = mysql_num_fields(mysql_result);
258
259/* Get the fields and construct the result string. If there is more than one
260row, we insert '\n' between them. */
261
262fields = mysql_fetch_fields(mysql_result);
263
264while ((mysql_row_data = mysql_fetch_row(mysql_result)) != NULL)
265 {
266 unsigned long *lengths = mysql_fetch_lengths(mysql_result);
267
268 if (result != NULL)
2813c06e 269 result = string_catn(result, &ssize, &offset, US"\n", 1);
420a0d19
CE
270
271 if (num_fields == 1)
272 {
273 if (mysql_row_data[0] != NULL) /* NULL value yields nothing */
2813c06e 274 result = string_catn(result, &ssize, &offset, US mysql_row_data[0],
420a0d19
CE
275 lengths[0]);
276 }
277
278 else for (i = 0; i < num_fields; i++)
279 {
280 result = lf_quote(US fields[i].name, US mysql_row_data[i], lengths[i],
281 result, &ssize, &offset);
282 }
283 }
284
285/* more results? -1 = no, >0 = error, 0 = yes (keep looping)
286 This is needed because of the CLIENT_MULTI_RESULTS on mysql_real_connect(),
287 we don't expect any more results. */
288
289while((i = mysql_next_result(mysql_handle)) >= 0) {
290 if(i == 0) { /* Just ignore more results */
291 DEBUG(D_lookup) debug_printf("MYSQL: got unexpected more results\n");
292 continue;
293 }
294
295 *errmsg = string_sprintf("MYSQL: lookup result error when checking for more results: %s\n",
296 mysql_error(mysql_handle));
297 goto MYSQL_EXIT;
298}
299
300/* If result is NULL then no data has been found and so we return FAIL.
301Otherwise, we must terminate the string which has been built; string_cat()
302always leaves enough room for a terminating zero. */
303
304if (result == NULL)
305 {
306 yield = FAIL;
307 *errmsg = US"MYSQL: no data found";
308 }
309else
310 {
311 result[offset] = 0;
312 store_reset(result + offset + 1);
313 }
314
315/* Get here by goto from various error checks and from the case where no data
316was read (e.g. an update query). */
317
318MYSQL_EXIT:
319
320/* Free mysal store for any result that was got; don't close the connection, as
321it is cached. */
322
323if (mysql_result != NULL) mysql_free_result(mysql_result);
324
2813c06e 325/* Non-NULL result indicates a successful result */
420a0d19
CE
326
327if (result != NULL)
328 {
329 *resultptr = result;
330 return OK;
331 }
332else
333 {
334 DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
335 return yield; /* FAIL or DEFER */
336 }
337}
338
339
340
341
342/*************************************************
343* Find entry point *
344*************************************************/
345
346/* See local README for interface description. The handle and filename
347arguments are not used. The code to loop through a list of servers while the
348query is deferred with a retryable error is now in a separate function that is
349shared with other SQL lookups. */
350
351static int
2813c06e
CE
352mysql_find(void *handle, uschar *filename, const uschar *query, int length,
353 uschar **result, uschar **errmsg, uint *do_cache)
420a0d19
CE
354{
355return lf_sqlperform(US"MySQL", US"mysql_servers", mysql_servers, query,
356 result, errmsg, do_cache, perform_mysql_search);
357}
358
359
360
361/*************************************************
362* Quote entry point *
363*************************************************/
364
365/* The only characters that need to be quoted (with backslash) are newline,
366tab, carriage return, backspace, backslash itself, and the quote characters.
367Percent, and underscore and not escaped. They are only special in contexts
368where they can be wild cards, and this isn't usually the case for data inserted
369from messages, since that isn't likely to be treated as a pattern of any kind.
370Sadly, MySQL doesn't seem to behave like other programs. If you use something
371like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
372can't quote "on spec".
373
374Arguments:
375 s the string to be quoted
376 opt additional option text or NULL if none
377
378Returns: the processed string or NULL for a bad option
379*/
380
381static uschar *
382mysql_quote(uschar *s, uschar *opt)
383{
384register int c;
385int count = 0;
386uschar *t = s;
387uschar *quoted;
388
389if (opt != NULL) return NULL; /* No options recognized */
390
391while ((c = *t++) != 0)
392 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
393
394if (count == 0) return s;
395t = quoted = store_get(Ustrlen(s) + count + 1);
396
397while ((c = *s++) != 0)
398 {
399 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
400 {
401 *t++ = '\\';
402 switch(c)
403 {
404 case '\n': *t++ = 'n';
405 break;
406 case '\t': *t++ = 't';
407 break;
408 case '\r': *t++ = 'r';
409 break;
410 case '\b': *t++ = 'b';
411 break;
412 default: *t++ = c;
413 break;
414 }
415 }
416 else *t++ = c;
417 }
418
419*t = 0;
420return quoted;
421}
422
423
424/*************************************************
425* Version reporting entry point *
426*************************************************/
427
428/* See local README for interface description. */
429
430#include "../version.h"
431
432void
433mysql_version_report(FILE *f)
434{
435fprintf(f, "Library version: MySQL: Compile: %s [%s]\n"
436 " Runtime: %s\n",
437 MYSQL_SERVER_VERSION, MYSQL_COMPILATION_COMMENT,
438 mysql_get_client_info());
439#ifdef DYNLOOKUP
440fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
441#endif
442}
443
444/* These are the lookup_info blocks for this driver */
445
446static lookup_info mysql_lookup_info = {
447 US"mysql", /* lookup name */
448 lookup_querystyle, /* query-style lookup */
449 mysql_open, /* open function */
450 NULL, /* no check function */
451 mysql_find, /* find function */
452 NULL, /* no close function */
453 mysql_tidy, /* tidy function */
454 mysql_quote, /* quoting function */
455 mysql_version_report /* version reporting */
456};
457
458#ifdef DYNLOOKUP
459#define mysql_lookup_module_info _lookup_module_info
460#endif
461
462static lookup_info *_lookup_list[] = { &mysql_lookup_info };
463lookup_module_info mysql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
464
465/* End of lookups/mysql.c */