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