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 /* The code in this module was contributed by Ard Biesheuvel. */
13 #include "lf_functions.h"
15 #include <ibase.h> /* The system header */
17 /* Structure and anchor for caching connections. */
19 typedef struct ibase_connection {
20 struct ibase_connection *next;
26 static ibase_connection *ibase_connections = NULL;
30 /*************************************************
32 *************************************************/
34 /* See local README for interface description. */
36 static void *ibase_open(const uschar * filename, uschar ** errmsg)
38 return (void *) (1); /* Just return something non-null */
43 /*************************************************
45 *************************************************/
47 /* See local README for interface description. */
49 static void ibase_tidy(void)
52 ISC_STATUS status[20];
54 while ((cn = ibase_connections) != NULL) {
55 ibase_connections = cn->next;
56 DEBUG(D_lookup) debug_printf_indent("close Interbase connection: %s\n",
58 isc_commit_transaction(status, &cn->transh);
59 isc_detach_database(status, &cn->dbh);
63 static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
65 if (buffer_size < var->sqllen)
68 switch (var->sqltype & ~1) {
70 strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
71 return *(short *) var->sqldata;
73 strncpy(buffer, var->sqldata, var->sqllen);
76 return sprintf(buffer, "%d", *(short *) var->sqldata);
78 return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
81 return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
89 /*************************************************
90 * Internal search function *
91 *************************************************/
93 /* This function is called from the find entry point to do the search for a
97 query the query string
98 server the server string
99 resultptr where to store the result
100 errmsg where to point an error message
101 defer_break TRUE if no more servers are to be tried after DEFER
103 The server string is of the form "host:dbname|user|password". The host can be
104 host:port. This string is in a nextinlist temporary buffer, so can be
107 Returns: OK, FAIL, or DEFER
111 perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
112 uschar ** errmsg, BOOL * defer_break)
114 isc_stmt_handle stmth = NULL;
121 ISC_STATUS status[20], *statusp = status;
125 ibase_connection *cn;
126 uschar *server_copy = NULL;
129 /* Disaggregate the parameters from the server argument. The order is host,
130 database, user, password. We can write to the string, since it is in a
131 nextinlist temporary buffer. The copy of the string that is used for caching
132 has the password removed. This copy is also used for debugging output. */
134 for (int i = 2; i > 0; i--)
136 uschar *pp = Ustrrchr(server, '|');
140 *errmsg = string_sprintf("incomplete Interbase server data: %s",
141 (i == 3) ? server : server_copy);
148 server_copy = string_copy(server); /* sans password */
150 sdata[0] = server; /* What's left at the start */
152 /* See if we have a cached connection to the server */
154 for (cn = ibase_connections; cn != NULL; cn = cn->next)
155 if (Ustrcmp(cn->server, server_copy) == 0)
158 /* Use a previously cached connection ? */
162 static char db_info_options[] = { isc_info_base_level };
164 /* test if the connection is alive */
165 if (isc_database_info(status, &cn->dbh, sizeof(db_info_options),
166 db_info_options, sizeof(buffer), buffer))
168 /* error occurred: assume connection is down */
171 ("Interbase cleaning up cached connection: %s\n",
173 isc_detach_database(status, &cn->dbh);
176 DEBUG(D_lookup) debug_printf_indent("Interbase using cached connection for %s\n",
181 cn = store_get(sizeof(ibase_connection), GET_UNTAINTED);
182 cn->server = server_copy;
185 cn->next = ibase_connections;
186 ibase_connections = cn;
189 /* If no cached connection, we must set one up. */
191 if (cn->dbh == NULL || cn->transh == NULL)
195 static char trans_options[] =
196 { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed,
200 /* Construct the database parameter buffer. */
202 *dpb++ = isc_dpb_version1;
203 *dpb++ = isc_dpb_user_name;
204 *dpb++ = strlen(sdata[1]);
205 for (char * p = sdata[1]; *p;)
207 *dpb++ = isc_dpb_password;
208 *dpb++ = strlen(sdata[2]);
209 for (char * p = sdata[2]; *p;)
211 dpb_length = dpb - buffer;
214 debug_printf_indent("new Interbase connection: database=%s user=%s\n",
217 /* Connect to the database */
218 if (isc_attach_database
219 (status, 0, sdata[0], &cn->dbh, dpb_length, buffer))
221 isc_interprete(buffer, &statusp);
223 string_sprintf("Interbase attach() failed: %s", buffer);
224 *defer_break = FALSE;
228 /* Now start a read-only read-committed transaction */
229 if (isc_start_transaction
230 (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options),
233 isc_interprete(buffer, &statusp);
234 isc_detach_database(status, &cn->dbh);
236 string_sprintf("Interbase start_transaction() failed: %s",
238 *defer_break = FALSE;
244 if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth))
246 isc_interprete(buffer, &statusp);
248 string_sprintf("Interbase alloc_statement() failed: %s",
250 *defer_break = FALSE;
254 /* Lacking any information, assume that the data is untainted */
255 reset_point = store_mark();
256 out_sqlda = store_get(XSQLDA_LENGTH(1), GET_UNTAINTED);
257 out_sqlda->version = SQLDA_VERSION1;
261 (status, &cn->transh, &stmth, 0, query, 1, out_sqlda))
263 isc_interprete(buffer, &statusp);
264 reset_point = store_reset(reset_point);
267 string_sprintf("Interbase prepare_statement() failed: %s",
269 *defer_break = FALSE;
273 /* re-allocate the output structure if there's more than one field */
274 if (out_sqlda->sqln < out_sqlda->sqld)
276 XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld), GET_UNTAINTED);
277 if (isc_dsql_describe
278 (status, &stmth, out_sqlda->version, new_sqlda))
280 isc_interprete(buffer, &statusp);
281 isc_dsql_free_statement(status, &stmth, DSQL_drop);
282 reset_point = store_reset(reset_point);
284 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
286 *defer_break = FALSE;
289 out_sqlda = new_sqlda;
292 /* allocate storage for every returned field */
293 for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++)
295 switch (var->sqltype & ~1)
298 var->sqldata = CS store_get(sizeof(char) * var->sqllen + 2, GET_UNTAINTED);
301 var->sqldata = CS store_get(sizeof(char) * var->sqllen, GET_UNTAINTED);
304 var->sqldata = CS store_get(sizeof(short), GET_UNTAINTED);
307 var->sqldata = CS store_get(sizeof(ISC_LONG), GET_UNTAINTED);
311 var->sqldata = CS store_get(sizeof(ISC_INT64), GET_UNTAINTED);
315 var->sqldata = CS store_get(sizeof(float), GET_UNTAINTED);
318 var->sqldata = CS store_get(sizeof(double), GET_UNTAINTED);
322 var->sqldata = CS store_get(sizeof(ISC_QUAD), GET_UNTAINTED);
326 var->sqldata = CS store_get(sizeof(ISC_TIMESTAMP), GET_UNTAINTED);
329 var->sqldata = CS store_get(sizeof(ISC_DATE), GET_UNTAINTED);
332 var->sqldata = CS store_get(sizeof(ISC_TIME), GET_UNTAINTED);
336 if (var->sqltype & 1)
337 var->sqlind = (short *) store_get(sizeof(short), GET_UNTAINTED);
340 /* finally, we're ready to execute the statement */
342 (status, &cn->transh, &stmth, out_sqlda->version, NULL))
344 isc_interprete(buffer, &statusp);
345 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
347 isc_dsql_free_statement(status, &stmth, DSQL_drop);
348 *defer_break = FALSE;
352 while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) != 100L)
354 /* check if an error occurred */
355 if (status[0] & status[1])
357 isc_interprete(buffer, &statusp);
359 string_sprintf("Interbase fetch() failed: %s", buffer);
360 isc_dsql_free_statement(status, &stmth, DSQL_drop);
361 *defer_break = FALSE;
366 result = string_catn(result, US "\n", 1);
368 /* Find the number of fields returned. If this is one, we don't add field
369 names to the data. Otherwise we do. */
370 if (out_sqlda->sqld == 1)
372 if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1) /* NULL value yields nothing */
373 result = string_catn(result, US buffer,
374 fetch_field(buffer, sizeof(buffer),
375 &out_sqlda->sqlvar[0]));
379 for (int i = 0; i < out_sqlda->sqld; i++)
381 int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]);
383 result = string_catn(result, US out_sqlda->sqlvar[i].aliasname,
384 out_sqlda->sqlvar[i].aliasname_length);
385 result = string_catn(result, US "=", 1);
387 /* Quote the value if it contains spaces or is empty */
389 if (*out_sqlda->sqlvar[i].sqlind == -1) /* NULL value */
390 result = string_catn(result, US "\"\"", 2);
392 else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL)
394 result = string_catn(result, US "\"", 1);
395 for (int j = 0; j < len; j++)
397 if (buffer[j] == '\"' || buffer[j] == '\\')
398 result = string_cat(result, US "\\", 1);
399 result = string_cat(result, US buffer + j, 1);
401 result = string_catn(result, US "\"", 1);
404 result = string_catn(result, US buffer, len);
405 result = string_catn(result, US " ", 1);
409 /* If result is NULL then no data has been found and so we return FAIL.
410 Otherwise, we must terminate the string which has been built; string_cat()
411 always leaves enough room for a terminating zero. */
416 *errmsg = US "Interbase: no data found";
419 gstring_release_unused(result);
422 /* Get here by goto from various error checks. */
427 isc_dsql_free_statement(status, &stmth, DSQL_drop);
429 /* Non-NULL result indicates a successful result */
433 *resultptr = string_from_gstring(result);
438 DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
439 return yield; /* FAIL or DEFER */
446 /*************************************************
448 *************************************************/
450 /* See local README for interface description. The handle and filename
451 arguments are not used. Loop through a list of servers while the query is
452 deferred with a retryable error. */
455 ibase_find(void * handle, const uschar * filename, uschar * query, int length,
456 uschar ** result, uschar ** errmsg, uint * do_cache, const uschar * opts)
460 uschar *list = ibase_servers;
462 DEBUG(D_lookup) debug_printf_indent("Interbase query: %s\n", query);
464 while ((server = string_nextinlist(&list, &sep, NULL, 0)))
466 BOOL defer_break = FALSE;
467 int rc = perform_ibase_search(query, server, result, errmsg, &defer_break);
468 if (rc != DEFER || defer_break)
473 *errmsg = US "no Interbase servers defined (ibase_servers option)";
480 /*************************************************
481 * Quote entry point *
482 *************************************************/
484 /* The only characters that need to be quoted (with backslash) are newline,
485 tab, carriage return, backspace, backslash itself, and the quote characters.
486 Percent, and underscore and not escaped. They are only special in contexts
487 where they can be wild cards, and this isn't usually the case for data inserted
488 from messages, since that isn't likely to be treated as a pattern of any kind.
489 Sadly, MySQL doesn't seem to behave like other programs. If you use something
490 like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
491 can't quote "on spec".
494 s the string to be quoted
495 opt additional option text or NULL if none
496 idx lookup type index
498 Returns: the processed string or NULL for a bad option
502 ibase_quote(uschar * s, uschar * opt, unsigned idx)
506 uschar * t = s, * quoted;
509 return NULL; /* No options recognized */
512 if (c == '\'') count++;
514 t = quoted = store_get_quoted(Ustrlen(s) + count + 1, s, idx);
517 if (c == '\'') { *t++ = '\''; *t++ = '\''; }
525 /*************************************************
526 * Version reporting entry point *
527 *************************************************/
529 /* See local README for interface description. */
531 #include "../version.h"
534 ibase_version_report(gstring * g)
537 g = string_fmt_append(g, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR));
543 static lookup_info _lookup_info = {
544 .name = US"ibase", /* lookup name */
545 .type = lookup_querystyle, /* query-style lookup */
546 .open = ibase_open, /* open function */
547 .check NULL, /* no check function */
548 .find = ibase_find, /* find function */
549 .close = NULL, /* no close function */
550 .tidy = ibase_tidy, /* tidy function */
551 .quote = ibase_quote, /* quoting function */
552 .version_report = ibase_version_report /* version reporting */
556 #define ibase_lookup_module_info _lookup_module_info
559 static lookup_info *_lookup_list[] = { &_lookup_info };
560 lookup_module_info ibase_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
562 /* End of lookups/ibase.c */