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