1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
10 /* Thanks to Paul Kelly for contributing the original code for these
15 #include "lf_functions.h"
17 #include <mysql.h> /* The system header */
19 /* We define symbols for *_VERSION_ID (numeric), *_VERSION_STR (char*)
20 and *_BASE_STR (char*). It's a bit of guesswork. Especially for mariadb
21 with versions before 10.2, as they do not define there there specific symbols.
24 /* Newer (>= 10.2) MariaDB */
25 #if defined MARIADB_VERSION_ID
26 #define EXIM_MxSQL_VERSION_ID MARIADB_VERSION_ID
28 /* MySQL defines MYSQL_VERSION_ID, and MariaDB does so */
29 /* https://dev.mysql.com/doc/refman/5.7/en/c-api-server-client-versions.html */
30 #elif defined LIBMYSQL_VERSION_ID
31 #define EXIM_MxSQL_VERSION_ID LIBMYSQL_VERSION_ID
32 #elif defined MYSQL_VERSION_ID
33 #define EXIM_MxSQL_VERSION_ID MYSQL_VERSION_ID
36 #define EXIM_MYSQL_VERSION_ID 0
39 /* Newer (>= 10.2) MariaDB */
40 #ifdef MARIADB_CLIENT_VERSION_STR
41 #define EXIM_MxSQL_VERSION_STR MARIADB_CLIENT_VERSION_STR
43 /* Mysql uses MYSQL_SERVER_VERSION */
44 #elif defined LIBMYSQL_VERSION
45 #define EXIM_MxSQL_VERSION_STR LIBMYSQL_VERSION
46 #elif defined MYSQL_SERVER_VERSION
47 #define EXIM_MxSQL_VERSION_STR MYSQL_SERVER_VERSION
50 #define EXIM_MxSQL_VERSION_STR "unknown"
53 #if defined MARIADB_BASE_VERSION
54 #define EXIM_MxSQL_BASE_STR MARIADB_BASE_VERSION
56 #elif defined MARIADB_PACKAGE_VERSION
57 #define EXIM_MxSQL_BASE_STR "mariadb"
59 #elif defined MYSQL_BASE_VERSION
60 #define EXIM_MxSQL_BASE_STR MYSQL_BASE_VERSION
63 #define EXIM_MxSQL_BASE_STR "n.A."
67 /* Structure and anchor for caching connections. */
69 typedef struct mysql_connection {
70 struct mysql_connection *next;
75 static mysql_connection *mysql_connections = NULL;
79 /*************************************************
81 *************************************************/
83 /* See local README for interface description. */
86 mysql_open(const uschar * filename, uschar ** errmsg)
88 return (void *)(1); /* Just return something non-null */
93 /*************************************************
95 *************************************************/
97 /* See local README for interface description. */
102 mysql_connection *cn;
103 while ((cn = mysql_connections) != NULL)
105 mysql_connections = cn->next;
106 DEBUG(D_lookup) debug_printf_indent("close MYSQL connection: %s\n", cn->server);
107 mysql_close(cn->handle);
113 /*************************************************
114 * Internal search function *
115 *************************************************/
117 /* This function is called from the find entry point to do the search for a
121 query the query string
122 server the server string
123 resultptr where to store the result
124 errmsg where to point an error message
125 defer_break TRUE if no more servers are to be tried after DEFER
126 do_cache set zero if data is changed
129 The server string is of the form "host/dbname/user/password". The host can be
130 host:port. This string is in a nextinlist temporary buffer, so can be
133 Returns: OK, FAIL, or DEFER
137 perform_mysql_search(const uschar *query, uschar *server, uschar **resultptr,
138 uschar **errmsg, BOOL *defer_break, uint *do_cache, const uschar * opts)
140 MYSQL *mysql_handle = NULL; /* Keep compilers happy */
141 MYSQL_RES *mysql_result = NULL;
142 MYSQL_ROW mysql_row_data;
147 unsigned int num_fields;
148 gstring * result = NULL;
149 mysql_connection *cn;
150 uschar *server_copy = NULL;
153 /* Disaggregate the parameters from the server argument. The order is host,
154 database, user, password. We can write to the string, since it is in a
155 nextinlist temporary buffer. The copy of the string that is used for caching
156 has the password removed. This copy is also used for debugging output. */
158 for (int i = 3; i > 0; i--)
160 uschar *pp = Ustrrchr(server, '/');
163 *errmsg = string_sprintf("incomplete MySQL server data: %s",
164 (i == 3)? server : server_copy);
170 if (i == 3) server_copy = string_copy(server); /* sans password */
172 sdata[0] = server; /* What's left at the start */
174 /* See if we have a cached connection to the server */
176 for (cn = mysql_connections; cn; cn = cn->next)
177 if (Ustrcmp(cn->server, server_copy) == 0)
178 { mysql_handle = cn->handle; break; }
180 /* If no cached connection, we must set one up. Mysql allows for a host name
181 and port to be specified. It also allows the name of a Unix socket to be used.
182 Unfortunately, this contains slashes, but its use is expected to be rare, so
183 the rather cumbersome syntax shouldn't inconvenience too many people. We use
184 this: host:port(socket)[group] where all the parts are optional.
185 The "group" parameter specifies an option group from a MySQL option file. */
190 uschar *socket = NULL;
192 uschar *group = US"exim";
194 if ((p = Ustrchr(sdata[0], '[')))
198 while (*p && *p != ']') p++;
202 if ((p = Ustrchr(sdata[0], '(')))
206 while (*p && *p != ')') p++;
210 /* If there is a colon (":") it could be a port number after an ipv4 or
211 hostname, or could be an ipv6. The latter must have at least two colons,
212 and we look instead for a period ("."). */
214 if ((p = Ustrrchr(sdata[0], ':')))
215 if ( Ustrchr(sdata[0], ':') == p /* only one colon */
216 || (p = Ustrrchr(sdata[0], '.')) /* >1 colon, and a period */
223 if (Ustrchr(sdata[0], '/'))
225 *errmsg = string_sprintf("unexpected slash in MySQL server hostname: %s",
231 /* If the database is the empty string, set it NULL - the query must then
234 if (sdata[1][0] == 0) sdata[1] = NULL;
237 debug_printf_indent("MYSQL new connection: host=%s port=%d socket=%s "
238 "database=%s user=%s\n", sdata[0], port, socket, sdata[1], sdata[2]);
240 /* Get store for a new handle, initialize it, and connect to the server */
242 mysql_handle = store_get(sizeof(MYSQL), GET_UNTAINTED);
243 mysql_init(mysql_handle);
244 mysql_options(mysql_handle, MYSQL_READ_DEFAULT_GROUP, CS group);
245 if (mysql_real_connect(mysql_handle,
246 /* host user passwd database */
247 CS sdata[0], CS sdata[2], CS sdata[3], CS sdata[1],
248 port, CS socket, CLIENT_MULTI_RESULTS) == NULL)
250 *errmsg = string_sprintf("MYSQL connection failed: %s",
251 mysql_error(mysql_handle));
252 *defer_break = FALSE;
256 /* Add the connection to the cache */
258 cn = store_get(sizeof(mysql_connection), GET_UNTAINTED);
259 cn->server = server_copy;
260 cn->handle = mysql_handle;
261 cn->next = mysql_connections;
262 mysql_connections = cn;
265 /* Else use a previously cached connection */
270 debug_printf_indent("MYSQL using cached connection for %s\n", server_copy);
275 if (mysql_query(mysql_handle, CS query) != 0)
277 *errmsg = string_sprintf("MYSQL: query failed: %s\n",
278 mysql_error(mysql_handle));
279 *defer_break = FALSE;
283 /* Pick up the result. If the query was not of the type that returns data,
284 namely INSERT, UPDATE, or DELETE, an error occurs here. However, this situation
285 can be detected by calling mysql_field_count(). If its result is zero, no data
286 was expected (this is all explained clearly in the MySQL manual). In this case,
287 we return the number of rows affected by the command. In this event, we do NOT
288 want to cache the result; also the whole cache for the handle must be cleaned
289 up. Setting do_cache zero requests this. */
291 if (!(mysql_result = mysql_use_result(mysql_handle)))
293 if (mysql_field_count(mysql_handle) == 0)
295 DEBUG(D_lookup) debug_printf_indent("MYSQL: query was not one that returns data\n");
296 result = string_cat(result,
297 string_sprintf("%lld", mysql_affected_rows(mysql_handle)));
301 *errmsg = string_sprintf("MYSQL: lookup result failed: %s\n",
302 mysql_error(mysql_handle));
303 *defer_break = FALSE;
307 /* Find the number of fields returned. If this is one, we don't add field
308 names to the data. Otherwise we do. */
310 num_fields = mysql_num_fields(mysql_result);
312 /* Get the fields and construct the result string. If there is more than one
313 row, we insert '\n' between them. */
315 fields = mysql_fetch_fields(mysql_result);
317 while ((mysql_row_data = mysql_fetch_row(mysql_result)))
319 unsigned long * lengths = mysql_fetch_lengths(mysql_result);
322 result = string_catn(result, US"\n", 1);
325 for (int i = 0; i < num_fields; i++)
326 result = lf_quote(US fields[i].name, US mysql_row_data[i], lengths[i],
329 else if (mysql_row_data[0] != NULL) /* NULL value yields nothing */
330 result = lengths[0] == 0 && !result
331 ? string_get(1) /* for 0-len string result ensure non-null gstring */
332 : string_catn(result, US mysql_row_data[0], lengths[0]);
335 /* more results? -1 = no, >0 = error, 0 = yes (keep looping)
336 This is needed because of the CLIENT_MULTI_RESULTS on mysql_real_connect(),
337 we don't expect any more results. */
339 while((i = mysql_next_result(mysql_handle)) >= 0)
342 *errmsg = string_sprintf(
343 "MYSQL: lookup result error when checking for more results: %s\n",
344 mysql_error(mysql_handle));
347 else /* just ignore more results */
348 DEBUG(D_lookup) debug_printf_indent("MYSQL: got unexpected more results\n");
350 /* If result is NULL then no data has been found and so we return FAIL.
351 Otherwise, we must terminate the string which has been built; string_cat()
352 always leaves enough room for a terminating zero. */
357 *errmsg = US"MYSQL: no data found";
360 /* Get here by goto from various error checks and from the case where no data
361 was read (e.g. an update query). */
365 /* Free mysal store for any result that was got; don't close the connection, as
368 if (mysql_result) mysql_free_result(mysql_result);
370 /* Non-NULL result indicates a successful result */
374 *resultptr = string_from_gstring(result);
375 gstring_release_unused(result);
380 DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
381 return yield; /* FAIL or DEFER */
388 /*************************************************
390 *************************************************/
392 /* See local README for interface description. The handle and filename
393 arguments are not used. The code to loop through a list of servers while the
394 query is deferred with a retryable error is now in a separate function that is
395 shared with other SQL lookups. */
398 mysql_find(void * handle, const uschar * filename, const uschar * query,
399 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
402 return lf_sqlperform(US"MySQL", US"mysql_servers", mysql_servers, query,
403 result, errmsg, do_cache, opts, perform_mysql_search);
408 /*************************************************
409 * Quote entry point *
410 *************************************************/
412 /* The only characters that need to be quoted (with backslash) are newline,
413 tab, carriage return, backspace, backslash itself, and the quote characters.
414 Percent, and underscore and not escaped. They are only special in contexts
415 where they can be wild cards, and this isn't usually the case for data inserted
416 from messages, since that isn't likely to be treated as a pattern of any kind.
417 Sadly, MySQL doesn't seem to behave like other programs. If you use something
418 like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
419 can't quote "on spec".
422 s the string to be quoted
423 opt additional option text or NULL if none
424 idx lookup type index
426 Returns: the processed string or NULL for a bad option
430 mysql_quote(uschar * s, uschar * opt, unsigned idx)
433 uschar * t = s, * quoted;
435 if (opt) return NULL; /* No options recognized */
438 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
440 /* Old code: if (count == 0) return s;
441 Now always allocate and copy, to track the quoted status. */
443 t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx, US"mysql");
447 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
452 case '\n': *t++ = 'n'; break;
453 case '\t': *t++ = 't'; break;
454 case '\r': *t++ = 'r'; break;
455 case '\b': *t++ = 'b'; break;
456 default: *t++ = c; break;
467 /*************************************************
468 * Version reporting entry point *
469 *************************************************/
471 /* See local README for interface description. */
473 #include "../version.h"
476 mysql_version_report(gstring * g)
478 g = string_fmt_append(g,
479 "Library version: MySQL: Compile: %lu %s [%s]\n"
480 " Runtime: %lu %s\n",
481 (long)EXIM_MxSQL_VERSION_ID, EXIM_MxSQL_VERSION_STR, EXIM_MxSQL_BASE_STR,
482 mysql_get_client_version(), mysql_get_client_info());
484 g = string_fmt_append(g,
485 " Exim version %s\n", EXIM_VERSION_STR);
490 /* These are the lookup_info blocks for this driver */
492 static lookup_info mysql_lookup_info = {
493 .name = US"mysql", /* lookup name */
494 .type = lookup_querystyle, /* query-style lookup */
495 .open = mysql_open, /* open function */
496 .check = NULL, /* no check function */
497 .find = mysql_find, /* find function */
498 .close = NULL, /* no close function */
499 .tidy = mysql_tidy, /* tidy function */
500 .quote = mysql_quote, /* quoting function */
501 .version_report = mysql_version_report /* version reporting */
505 #define mysql_lookup_module_info _lookup_module_info
508 static lookup_info *_lookup_list[] = { &mysql_lookup_info };
509 lookup_module_info mysql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
511 /* End of lookups/mysql.c */