Expansions: acl expansion error detail
[exim.git] / src / src / lookups / ibase.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/* The code in this module was contributed by Ard Biesheuvel. */
9
10#include "../exim.h"
11#include "lf_functions.h"
0756eb3c 12
0756eb3c
PH
13#include <ibase.h> /* The system header */
14
15/* Structure and anchor for caching connections. */
16
17typedef struct ibase_connection {
18 struct ibase_connection *next;
19 uschar *server;
20 isc_db_handle dbh;
21 isc_tr_handle transh;
22} ibase_connection;
23
24static ibase_connection *ibase_connections = NULL;
25
26
27
28/*************************************************
29* Open entry point *
30*************************************************/
31
32/* See local README for interface description. */
33
e6d225ae 34static void *ibase_open(uschar * filename, uschar ** errmsg)
0756eb3c
PH
35{
36 return (void *) (1); /* Just return something non-null */
37}
38
39
40
41/*************************************************
42* Tidy entry point *
43*************************************************/
44
45/* See local README for interface description. */
46
e6d225ae 47static void ibase_tidy(void)
0756eb3c
PH
48{
49 ibase_connection *cn;
50 ISC_STATUS status[20];
51
52 while ((cn = ibase_connections) != NULL) {
53 ibase_connections = cn->next;
54 DEBUG(D_lookup) debug_printf("close Interbase connection: %s\n",
55 cn->server);
56 isc_commit_transaction(status, &cn->transh);
57 isc_detach_database(status, &cn->dbh);
58 }
59}
60
61static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
62{
63 if (buffer_size < var->sqllen)
64 return 0;
65
66 switch (var->sqltype & ~1) {
67 case SQL_VARYING:
68 strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
69 return *(short *) var->sqldata;
70 case SQL_TEXT:
71 strncpy(buffer, var->sqldata, var->sqllen);
72 return var->sqllen;
73 case SQL_SHORT:
74 return sprintf(buffer, "%d", *(short *) var->sqldata);
75 case SQL_LONG:
76 return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
77#ifdef SQL_INT64
78 case SQL_INT64:
79 return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
80#endif
81 default:
82 /* not implemented */
83 return 0;
84 }
85}
86
87/*************************************************
88* Internal search function *
89*************************************************/
90
91/* This function is called from the find entry point to do the search for a
92single server.
93
94Arguments:
95 query the query string
96 server the server string
97 resultptr where to store the result
98 errmsg where to point an error message
99 defer_break TRUE if no more servers are to be tried after DEFER
100
101The server string is of the form "host:dbname|user|password". The host can be
102host:port. This string is in a nextinlist temporary buffer, so can be
103overwritten.
104
105Returns: OK, FAIL, or DEFER
106*/
107
108static int
109perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
110 uschar ** errmsg, BOOL * defer_break)
111{
acec9514
JH
112isc_stmt_handle stmth = NULL;
113XSQLDA *out_sqlda;
114XSQLVAR *var;
d7978c0f 115int i;
acec9514
JH
116
117char buffer[256];
118ISC_STATUS status[20], *statusp = status;
119
120gstring * result;
acec9514
JH
121int yield = DEFER;
122ibase_connection *cn;
123uschar *server_copy = NULL;
124uschar *sdata[3];
0756eb3c
PH
125
126/* Disaggregate the parameters from the server argument. The order is host,
127database, user, password. We can write to the string, since it is in a
128nextinlist temporary buffer. The copy of the string that is used for caching
129has the password removed. This copy is also used for debugging output. */
130
d7978c0f 131for (int i = 2; i > 0; i--)
acec9514
JH
132 {
133 uschar *pp = Ustrrchr(server, '|');
134
135 if (pp == NULL)
136 {
137 *errmsg = string_sprintf("incomplete Interbase server data: %s",
138 (i == 3) ? server : server_copy);
139 *defer_break = TRUE;
140 return DEFER;
0756eb3c 141 }
acec9514
JH
142 *pp++ = 0;
143 sdata[i] = pp;
144 if (i == 2)
145 server_copy = string_copy(server); /* sans password */
146 }
147sdata[0] = server; /* What's left at the start */
0756eb3c
PH
148
149/* See if we have a cached connection to the server */
150
acec9514
JH
151for (cn = ibase_connections; cn != NULL; cn = cn->next)
152 if (Ustrcmp(cn->server, server_copy) == 0)
153 break;
0756eb3c
PH
154
155/* Use a previously cached connection ? */
156
acec9514
JH
157if (cn)
158 {
159 static char db_info_options[] = { isc_info_base_level };
160
161 /* test if the connection is alive */
162 if (isc_database_info(status, &cn->dbh, sizeof(db_info_options),
163 db_info_options, sizeof(buffer), buffer))
164 {
165 /* error occurred: assume connection is down */
166 DEBUG(D_lookup)
167 debug_printf
168 ("Interbase cleaning up cached connection: %s\n",
169 cn->server);
170 isc_detach_database(status, &cn->dbh);
0756eb3c 171 }
acec9514
JH
172 else
173 {
174 DEBUG(D_lookup) debug_printf("Interbase using cached connection for %s\n",
175 server_copy);
176 }
177 }
178else
179 {
180 cn = store_get(sizeof(ibase_connection));
181 cn->server = server_copy;
182 cn->dbh = NULL;
183 cn->transh = NULL;
184 cn->next = ibase_connections;
185 ibase_connections = cn;
186 }
0756eb3c
PH
187
188/* If no cached connection, we must set one up. */
189
acec9514
JH
190if (cn->dbh == NULL || cn->transh == NULL)
191 {
d7978c0f 192 char *dpb;
acec9514
JH
193 short dpb_length;
194 static char trans_options[] =
195 { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed,
196 isc_tpb_rec_version
197 };
198
199 /* Construct the database parameter buffer. */
200 dpb = buffer;
201 *dpb++ = isc_dpb_version1;
202 *dpb++ = isc_dpb_user_name;
203 *dpb++ = strlen(sdata[1]);
d7978c0f 204 for (char * p = sdata[1]; *p;)
acec9514
JH
205 *dpb++ = *p++;
206 *dpb++ = isc_dpb_password;
207 *dpb++ = strlen(sdata[2]);
d7978c0f 208 for (char * p = sdata[2]; *p;)
acec9514
JH
209 *dpb++ = *p++;
210 dpb_length = dpb - buffer;
211
212 DEBUG(D_lookup)
213 debug_printf("new Interbase connection: database=%s user=%s\n",
214 sdata[0], sdata[1]);
215
216 /* Connect to the database */
217 if (isc_attach_database
218 (status, 0, sdata[0], &cn->dbh, dpb_length, buffer))
219 {
220 isc_interprete(buffer, &statusp);
221 *errmsg =
222 string_sprintf("Interbase attach() failed: %s", buffer);
223 *defer_break = FALSE;
224 goto IBASE_EXIT;
0756eb3c
PH
225 }
226
acec9514
JH
227 /* Now start a read-only read-committed transaction */
228 if (isc_start_transaction
229 (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options),
230 trans_options))
231 {
232 isc_interprete(buffer, &statusp);
233 isc_detach_database(status, &cn->dbh);
234 *errmsg =
235 string_sprintf("Interbase start_transaction() failed: %s",
236 buffer);
237 *defer_break = FALSE;
238 goto IBASE_EXIT;
0756eb3c 239 }
acec9514 240 }
0756eb3c 241
acec9514
JH
242/* Run the query */
243if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth))
244 {
245 isc_interprete(buffer, &statusp);
246 *errmsg =
247 string_sprintf("Interbase alloc_statement() failed: %s",
248 buffer);
249 *defer_break = FALSE;
250 goto IBASE_EXIT;
251 }
252
253out_sqlda = store_get(XSQLDA_LENGTH(1));
254out_sqlda->version = SQLDA_VERSION1;
255out_sqlda->sqln = 1;
256
257if (isc_dsql_prepare
258 (status, &cn->transh, &stmth, 0, query, 1, out_sqlda))
259 {
260 isc_interprete(buffer, &statusp);
261 store_reset(out_sqlda);
262 out_sqlda = NULL;
263 *errmsg =
264 string_sprintf("Interbase prepare_statement() failed: %s",
265 buffer);
266 *defer_break = FALSE;
267 goto IBASE_EXIT;
268 }
0756eb3c
PH
269
270/* re-allocate the output structure if there's more than one field */
acec9514
JH
271if (out_sqlda->sqln < out_sqlda->sqld)
272 {
273 XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld));
274 if (isc_dsql_describe
275 (status, &stmth, out_sqlda->version, new_sqlda))
276 {
277 isc_interprete(buffer, &statusp);
278 isc_dsql_free_statement(status, &stmth, DSQL_drop);
279 store_reset(out_sqlda);
280 out_sqlda = NULL;
281 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
282 buffer);
283 *defer_break = FALSE;
284 goto IBASE_EXIT;
0756eb3c 285 }
acec9514
JH
286 out_sqlda = new_sqlda;
287 }
0756eb3c
PH
288
289/* allocate storage for every returned field */
acec9514
JH
290for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++)
291 {
292 switch (var->sqltype & ~1)
293 {
294 case SQL_VARYING:
295 var->sqldata = CS store_get(sizeof(char) * var->sqllen + 2);
296 break;
297 case SQL_TEXT:
298 var->sqldata = CS store_get(sizeof(char) * var->sqllen);
299 break;
300 case SQL_SHORT:
301 var->sqldata = CS store_get(sizeof(short));
302 break;
303 case SQL_LONG:
304 var->sqldata = CS store_get(sizeof(ISC_LONG));
305 break;
0756eb3c 306#ifdef SQL_INT64
acec9514
JH
307 case SQL_INT64:
308 var->sqldata = CS store_get(sizeof(ISC_INT64));
309 break;
0756eb3c 310#endif
acec9514
JH
311 case SQL_FLOAT:
312 var->sqldata = CS store_get(sizeof(float));
313 break;
314 case SQL_DOUBLE:
315 var->sqldata = CS store_get(sizeof(double));
316 break;
0756eb3c 317#ifdef SQL_TIMESTAMP
acec9514
JH
318 case SQL_DATE:
319 var->sqldata = CS store_get(sizeof(ISC_QUAD));
320 break;
0756eb3c 321#else
acec9514
JH
322 case SQL_TIMESTAMP:
323 var->sqldata = CS store_get(sizeof(ISC_TIMESTAMP));
324 break;
325 case SQL_TYPE_DATE:
326 var->sqldata = CS store_get(sizeof(ISC_DATE));
327 break;
328 case SQL_TYPE_TIME:
329 var->sqldata = CS store_get(sizeof(ISC_TIME));
330 break;
331 #endif
0756eb3c 332 }
acec9514
JH
333 if (var->sqltype & 1)
334 var->sqlind = (short *) store_get(sizeof(short));
335 }
336
337/* finally, we're ready to execute the statement */
338if (isc_dsql_execute
339 (status, &cn->transh, &stmth, out_sqlda->version, NULL))
340 {
341 isc_interprete(buffer, &statusp);
342 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
343 buffer);
344 isc_dsql_free_statement(status, &stmth, DSQL_drop);
345 *defer_break = FALSE;
346 goto IBASE_EXIT;
347 }
348
349while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) != 100L)
350 {
351 /* check if an error occurred */
352 if (status[0] & status[1])
353 {
354 isc_interprete(buffer, &statusp);
355 *errmsg =
356 string_sprintf("Interbase fetch() failed: %s", buffer);
357 isc_dsql_free_statement(status, &stmth, DSQL_drop);
358 *defer_break = FALSE;
359 goto IBASE_EXIT;
0756eb3c
PH
360 }
361
acec9514
JH
362 if (result)
363 result = string_catn(result, US "\n", 1);
364
365 /* Find the number of fields returned. If this is one, we don't add field
366 names to the data. Otherwise we do. */
367 if (out_sqlda->sqld == 1)
368 {
369 if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1) /* NULL value yields nothing */
370 result = string_catn(result, US buffer,
371 fetch_field(buffer, sizeof(buffer),
372 &out_sqlda->sqlvar[0]));
0756eb3c
PH
373 }
374
acec9514 375 else
d7978c0f 376 for (int i = 0; i < out_sqlda->sqld; i++)
acec9514
JH
377 {
378 int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]);
379
380 result = string_catn(result, US out_sqlda->sqlvar[i].aliasname,
381 out_sqlda->sqlvar[i].aliasname_length);
382 result = string_catn(result, US "=", 1);
383
384 /* Quote the value if it contains spaces or is empty */
385
386 if (*out_sqlda->sqlvar[i].sqlind == -1) /* NULL value */
387 result = string_catn(result, US "\"\"", 2);
388
389 else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL)
390 {
acec9514 391 result = string_catn(result, US "\"", 1);
d7978c0f 392 for (int j = 0; j < len; j++)
acec9514
JH
393 {
394 if (buffer[j] == '\"' || buffer[j] == '\\')
395 result = string_cat(result, US "\\", 1);
396 result = string_cat(result, US buffer + j, 1);
397 }
398 result = string_catn(result, US "\"", 1);
399 }
400 else
401 result = string_catn(result, US buffer, len);
402 result = string_catn(result, US " ", 1);
403 }
404 }
405
0756eb3c
PH
406/* If result is NULL then no data has been found and so we return FAIL.
407Otherwise, we must terminate the string which has been built; string_cat()
408always leaves enough room for a terminating zero. */
409
acec9514
JH
410if (!result)
411 {
412 yield = FAIL;
413 *errmsg = US "Interbase: no data found";
414 }
415else
416 store_reset(result->s + result->ptr + 1);
0756eb3c
PH
417
418
419/* Get here by goto from various error checks. */
420
acec9514 421IBASE_EXIT:
0756eb3c 422
acec9514
JH
423if (stmth)
424 isc_dsql_free_statement(status, &stmth, DSQL_drop);
0756eb3c 425
4c04137d 426/* Non-NULL result indicates a successful result */
0756eb3c 427
acec9514
JH
428if (result)
429 {
430 *resultptr = string_from_gstring(result);
431 return OK;
432 }
433else
434 {
435 DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
436 return yield; /* FAIL or DEFER */
437 }
0756eb3c
PH
438}
439
440
441
442
443/*************************************************
444* Find entry point *
445*************************************************/
446
447/* See local README for interface description. The handle and filename
448arguments are not used. Loop through a list of servers while the query is
449deferred with a retryable error. */
450
e6d225ae 451static int
0756eb3c 452ibase_find(void *handle, uschar * filename, uschar * query, int length,
14b3c5bc 453 uschar ** result, uschar ** errmsg, uint *do_cache)
0756eb3c
PH
454{
455 int sep = 0;
456 uschar *server;
457 uschar *list = ibase_servers;
458 uschar buffer[512];
459
460 /* Keep picky compilers happy */
461 do_cache = do_cache;
462
463 DEBUG(D_lookup) debug_printf("Interbase query: %s\n", query);
464
465 while ((server =
466 string_nextinlist(&list, &sep, buffer,
467 sizeof(buffer))) != NULL) {
468 BOOL defer_break = FALSE;
469 int rc = perform_ibase_search(query, server, result, errmsg,
470 &defer_break);
471 if (rc != DEFER || defer_break)
472 return rc;
473 }
474
475 if (ibase_servers == NULL)
476 *errmsg = US "no Interbase servers defined (ibase_servers option)";
477
478 return DEFER;
479}
480
481
482
483/*************************************************
484* Quote entry point *
485*************************************************/
486
487/* The only characters that need to be quoted (with backslash) are newline,
488tab, carriage return, backspace, backslash itself, and the quote characters.
489Percent, and underscore and not escaped. They are only special in contexts
490where they can be wild cards, and this isn't usually the case for data inserted
491from messages, since that isn't likely to be treated as a pattern of any kind.
492Sadly, MySQL doesn't seem to behave like other programs. If you use something
493like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
494can't quote "on spec".
495
496Arguments:
497 s the string to be quoted
498 opt additional option text or NULL if none
499
500Returns: the processed string or NULL for a bad option
501*/
502
e6d225ae 503static uschar *ibase_quote(uschar * s, uschar * opt)
0756eb3c
PH
504{
505 register int c;
506 int count = 0;
507 uschar *t = s;
508 uschar *quoted;
509
510 if (opt != NULL)
511 return NULL; /* No options recognized */
512
513 while ((c = *t++) != 0)
514 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
515 count++;
516
517 if (count == 0)
518 return s;
519 t = quoted = store_get(Ustrlen(s) + count + 1);
520
521 while ((c = *s++) != 0) {
522 if (Ustrchr("'", c) != NULL) {
523 *t++ = '\'';
524 *t++ = '\'';
525/* switch(c)
526 {
527 case '\n': *t++ = 'n';
528 break;
529 case '\t': *t++ = 't';
530 break;
531 case '\r': *t++ = 'r';
532 break;
533 case '\b': *t++ = 'b';
534 break;
535 default: *t++ = c;
536 break;
537 }*/
538 } else
539 *t++ = c;
540 }
541
542 *t = 0;
543 return quoted;
544}
545
6545de78
PP
546
547/*************************************************
548* Version reporting entry point *
549*************************************************/
550
551/* See local README for interface description. */
552
553#include "../version.h"
554
555void
556ibase_version_report(FILE *f)
557{
558#ifdef DYNLOOKUP
559fprintf(f, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR);
560#endif
561}
562
563
e6d225ae
DW
564static lookup_info _lookup_info = {
565 US"ibase", /* lookup name */
566 lookup_querystyle, /* query-style lookup */
567 ibase_open, /* open function */
568 NULL, /* no check function */
569 ibase_find, /* find function */
570 NULL, /* no close function */
571 ibase_tidy, /* tidy function */
6545de78
PP
572 ibase_quote, /* quoting function */
573 ibase_version_report /* version reporting */
e6d225ae
DW
574};
575
576#ifdef DYNLOOKUP
577#define ibase_lookup_module_info _lookup_module_info
0756eb3c 578#endif
94431adb 579
e6d225ae
DW
580static lookup_info *_lookup_list[] = { &_lookup_info };
581lookup_module_info ibase_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
0756eb3c
PH
582
583/* End of lookups/ibase.c */