1 /* $Cambridge: exim/src/src/lookups/mysql.c,v 1.7 2010/03/05 15:59:29 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
10 /* Thanks to Paul Kelly for contributing the original code for these
15 #include "lf_functions.h"
17 #include <mysql.h> /* The system header */
20 /* Structure and anchor for caching connections. */
22 typedef struct mysql_connection {
23 struct mysql_connection *next;
28 static mysql_connection *mysql_connections = NULL;
32 /*************************************************
34 *************************************************/
36 /* See local README for interface description. */
39 mysql_open(uschar *filename, uschar **errmsg)
41 return (void *)(1); /* Just return something non-null */
46 /*************************************************
48 *************************************************/
50 /* See local README for interface description. */
56 while ((cn = mysql_connections) != NULL)
58 mysql_connections = cn->next;
59 DEBUG(D_lookup) debug_printf("close MYSQL connection: %s\n", cn->server);
60 mysql_close(cn->handle);
66 /*************************************************
67 * Internal search function *
68 *************************************************/
70 /* This function is called from the find entry point to do the search for a
74 query the query string
75 server the server string
76 resultptr where to store the result
77 errmsg where to point an error message
78 defer_break TRUE if no more servers are to be tried after DEFER
79 do_cache set false if data is changed
81 The server string is of the form "host/dbname/user/password". The host can be
82 host:port. This string is in a nextinlist temporary buffer, so can be
85 Returns: OK, FAIL, or DEFER
89 perform_mysql_search(uschar *query, uschar *server, uschar **resultptr,
90 uschar **errmsg, BOOL *defer_break, BOOL *do_cache)
92 MYSQL *mysql_handle = NULL; /* Keep compilers happy */
93 MYSQL_RES *mysql_result = NULL;
94 MYSQL_ROW mysql_row_data;
101 unsigned int num_fields;
102 uschar *result = NULL;
103 mysql_connection *cn;
104 uschar *server_copy = NULL;
107 /* Disaggregate the parameters from the server argument. The order is host,
108 database, user, password. We can write to the string, since it is in a
109 nextinlist temporary buffer. The copy of the string that is used for caching
110 has the password removed. This copy is also used for debugging output. */
112 for (i = 3; i > 0; i--)
114 uschar *pp = Ustrrchr(server, '/');
117 *errmsg = string_sprintf("incomplete MySQL server data: %s",
118 (i == 3)? server : server_copy);
124 if (i == 3) server_copy = string_copy(server); /* sans password */
126 sdata[0] = server; /* What's left at the start */
128 /* See if we have a cached connection to the server */
130 for (cn = mysql_connections; cn != NULL; cn = cn->next)
132 if (Ustrcmp(cn->server, server_copy) == 0)
134 mysql_handle = cn->handle;
139 /* If no cached connection, we must set one up. Mysql allows for a host name
140 and port to be specified. It also allows the name of a Unix socket to be used.
141 Unfortunately, this contains slashes, but its use is expected to be rare, so
142 the rather cumbersome syntax shouldn't inconvenience too many people. We use
143 this: host:port(socket) where all the parts are optional. */
148 uschar *socket = NULL;
151 if ((p = Ustrchr(sdata[0], '(')) != NULL)
155 while (*p != 0 && *p != ')') p++;
159 if ((p = Ustrchr(sdata[0], ':')) != NULL)
165 if (Ustrchr(sdata[0], '/') != NULL)
167 *errmsg = string_sprintf("unexpected slash in MySQL server hostname: %s",
173 /* If the database is the empty string, set it NULL - the query must then
176 if (sdata[1][0] == 0) sdata[1] = NULL;
179 debug_printf("MYSQL new connection: host=%s port=%d socket=%s "
180 "database=%s user=%s\n", sdata[0], port, socket, sdata[1], sdata[2]);
182 /* Get store for a new handle, initialize it, and connect to the server */
184 mysql_handle = store_get(sizeof(MYSQL));
185 mysql_init(mysql_handle);
186 if (mysql_real_connect(mysql_handle,
187 /* host user passwd database */
188 CS sdata[0], CS sdata[2], CS sdata[3], CS sdata[1],
189 port, CS socket, CLIENT_MULTI_RESULTS) == NULL)
191 *errmsg = string_sprintf("MYSQL connection failed: %s",
192 mysql_error(mysql_handle));
193 *defer_break = FALSE;
197 /* Add the connection to the cache */
199 cn = store_get(sizeof(mysql_connection));
200 cn->server = server_copy;
201 cn->handle = mysql_handle;
202 cn->next = mysql_connections;
203 mysql_connections = cn;
206 /* Else use a previously cached connection */
211 debug_printf("MYSQL using cached connection for %s\n", server_copy);
216 if (mysql_query(mysql_handle, CS query) != 0)
218 *errmsg = string_sprintf("MYSQL: query failed: %s\n",
219 mysql_error(mysql_handle));
220 *defer_break = FALSE;
224 /* Pick up the result. If the query was not of the type that returns data,
225 namely INSERT, UPDATE, or DELETE, an error occurs here. However, this situation
226 can be detected by calling mysql_field_count(). If its result is zero, no data
227 was expected (this is all explained clearly in the MySQL manual). In this case,
228 we return the number of rows affected by the command. In this event, we do NOT
229 want to cache the result; also the whole cache for the handle must be cleaned
230 up. Setting do_cache FALSE requests this. */
232 if ((mysql_result = mysql_use_result(mysql_handle)) == NULL)
234 if ( mysql_field_count(mysql_handle) == 0 )
236 DEBUG(D_lookup) debug_printf("MYSQL: query was not one that returns data\n");
237 result = string_sprintf("%d", mysql_affected_rows(mysql_handle));
241 *errmsg = string_sprintf("MYSQL: lookup result failed: %s\n",
242 mysql_error(mysql_handle));
243 *defer_break = FALSE;
247 /* Find the number of fields returned. If this is one, we don't add field
248 names to the data. Otherwise we do. */
250 num_fields = mysql_num_fields(mysql_result);
252 /* Get the fields and construct the result string. If there is more than one
253 row, we insert '\n' between them. */
255 fields = mysql_fetch_fields(mysql_result);
257 while ((mysql_row_data = mysql_fetch_row(mysql_result)) != NULL)
259 unsigned long *lengths = mysql_fetch_lengths(mysql_result);
262 result = string_cat(result, &ssize, &offset, US"\n", 1);
266 if (mysql_row_data[0] != NULL) /* NULL value yields nothing */
267 result = string_cat(result, &ssize, &offset, US mysql_row_data[0],
271 else for (i = 0; i < num_fields; i++)
273 result = lf_quote(US fields[i].name, US mysql_row_data[i], lengths[i],
274 result, &ssize, &offset);
278 /* more results? -1 = no, >0 = error, 0 = yes (keep looping)
279 This is needed because of the CLIENT_MULTI_RESULTS on mysql_real_connect(),
280 we don't expect any more results. */
282 while((i = mysql_next_result(mysql_handle)) >= 0) {
283 if(i == 0) { /* Just ignore more results */
284 DEBUG(D_lookup) debug_printf("MYSQL: got unexpected more results\n");
288 *errmsg = string_sprintf("MYSQL: lookup result error when checking for more results: %s\n",
289 mysql_error(mysql_handle));
293 /* If result is NULL then no data has been found and so we return FAIL.
294 Otherwise, we must terminate the string which has been built; string_cat()
295 always leaves enough room for a terminating zero. */
300 *errmsg = US"MYSQL: no data found";
305 store_reset(result + offset + 1);
308 /* Get here by goto from various error checks and from the case where no data
309 was read (e.g. an update query). */
313 /* Free mysal store for any result that was got; don't close the connection, as
316 if (mysql_result != NULL) mysql_free_result(mysql_result);
318 /* Non-NULL result indicates a sucessful result */
327 DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
328 return yield; /* FAIL or DEFER */
335 /*************************************************
337 *************************************************/
339 /* See local README for interface description. The handle and filename
340 arguments are not used. The code to loop through a list of servers while the
341 query is deferred with a retryable error is now in a separate function that is
342 shared with other SQL lookups. */
345 mysql_find(void *handle, uschar *filename, uschar *query, int length,
346 uschar **result, uschar **errmsg, BOOL *do_cache)
348 return lf_sqlperform(US"MySQL", US"mysql_servers", mysql_servers, query,
349 result, errmsg, do_cache, perform_mysql_search);
354 /*************************************************
355 * Quote entry point *
356 *************************************************/
358 /* The only characters that need to be quoted (with backslash) are newline,
359 tab, carriage return, backspace, backslash itself, and the quote characters.
360 Percent, and underscore and not escaped. They are only special in contexts
361 where they can be wild cards, and this isn't usually the case for data inserted
362 from messages, since that isn't likely to be treated as a pattern of any kind.
363 Sadly, MySQL doesn't seem to behave like other programs. If you use something
364 like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
365 can't quote "on spec".
368 s the string to be quoted
369 opt additional option text or NULL if none
371 Returns: the processed string or NULL for a bad option
375 mysql_quote(uschar *s, uschar *opt)
382 if (opt != NULL) return NULL; /* No options recognized */
384 while ((c = *t++) != 0)
385 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL) count++;
387 if (count == 0) return s;
388 t = quoted = store_get(Ustrlen(s) + count + 1);
390 while ((c = *s++) != 0)
392 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
397 case '\n': *t++ = 'n';
399 case '\t': *t++ = 't';
401 case '\r': *t++ = 'r';
403 case '\b': *t++ = 'b';
417 /*************************************************
418 * Version reporting entry point *
419 *************************************************/
421 /* See local README for interface description. */
423 #include "../version.h"
426 mysql_version_report(FILE *f)
428 fprintf(f, "Library version: MySQL: Compile: %s [%s]\n"
430 MYSQL_SERVER_VERSION, MYSQL_COMPILATION_COMMENT,
431 mysql_get_client_info());
433 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
437 /* These are the lookup_info blocks for this driver */
439 static lookup_info mysql_lookup_info = {
440 US"mysql", /* lookup name */
441 lookup_querystyle, /* query-style lookup */
442 mysql_open, /* open function */
443 NULL, /* no check function */
444 mysql_find, /* find function */
445 NULL, /* no close function */
446 mysql_tidy, /* tidy function */
447 mysql_quote, /* quoting function */
448 mysql_version_report /* version reporting */
452 #define mysql_lookup_module_info _lookup_module_info
455 static lookup_info *_lookup_list[] = { &mysql_lookup_info };
456 lookup_module_info mysql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
458 /* End of lookups/mysql.c */