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