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