1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
9 /* The code in this module was contributed by Ard Biesheuvel. */
12 #include "lf_functions.h"
14 #include <ibase.h> /* The system header */
16 /* Structure and anchor for caching connections. */
18 typedef struct ibase_connection {
19 struct ibase_connection *next;
25 static ibase_connection *ibase_connections = NULL;
29 /*************************************************
31 *************************************************/
33 /* See local README for interface description. */
35 static void *ibase_open(const uschar * filename, uschar ** errmsg)
37 return (void *) (1); /* Just return something non-null */
42 /*************************************************
44 *************************************************/
46 /* See local README for interface description. */
48 static void ibase_tidy(void)
51 ISC_STATUS status[20];
53 while ((cn = ibase_connections) != NULL) {
54 ibase_connections = cn->next;
55 DEBUG(D_lookup) debug_printf_indent("close Interbase connection: %s\n",
57 isc_commit_transaction(status, &cn->transh);
58 isc_detach_database(status, &cn->dbh);
62 static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
64 if (buffer_size < var->sqllen)
67 switch (var->sqltype & ~1) {
69 strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
70 return *(short *) var->sqldata;
72 strncpy(buffer, var->sqldata, var->sqllen);
75 return sprintf(buffer, "%d", *(short *) var->sqldata);
77 return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
80 return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
88 /*************************************************
89 * Internal search function *
90 *************************************************/
92 /* This function is called from the find entry point to do the search for a
96 query the query string
97 server the server string
98 resultptr where to store the result
99 errmsg where to point an error message
100 defer_break TRUE if no more servers are to be tried after DEFER
102 The server string is of the form "host:dbname|user|password". The host can be
103 host:port. This string is in a nextinlist temporary buffer, so can be
106 Returns: OK, FAIL, or DEFER
110 perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
111 uschar ** errmsg, BOOL * defer_break)
113 isc_stmt_handle stmth = NULL;
120 ISC_STATUS status[20], *statusp = status;
124 ibase_connection *cn;
125 uschar *server_copy = NULL;
128 /* Disaggregate the parameters from the server argument. The order is host,
129 database, user, password. We can write to the string, since it is in a
130 nextinlist temporary buffer. The copy of the string that is used for caching
131 has the password removed. This copy is also used for debugging output. */
133 for (int i = 2; i > 0; i--)
135 uschar *pp = Ustrrchr(server, '|');
139 *errmsg = string_sprintf("incomplete Interbase server data: %s",
140 (i == 3) ? server : server_copy);
147 server_copy = string_copy(server); /* sans password */
149 sdata[0] = server; /* What's left at the start */
151 /* See if we have a cached connection to the server */
153 for (cn = ibase_connections; cn != NULL; cn = cn->next)
154 if (Ustrcmp(cn->server, server_copy) == 0)
157 /* Use a previously cached connection ? */
161 static char db_info_options[] = { isc_info_base_level };
163 /* test if the connection is alive */
164 if (isc_database_info(status, &cn->dbh, sizeof(db_info_options),
165 db_info_options, sizeof(buffer), buffer))
167 /* error occurred: assume connection is down */
170 ("Interbase cleaning up cached connection: %s\n",
172 isc_detach_database(status, &cn->dbh);
175 DEBUG(D_lookup) debug_printf_indent("Interbase using cached connection for %s\n",
180 cn = store_get(sizeof(ibase_connection), FALSE);
181 cn->server = server_copy;
184 cn->next = ibase_connections;
185 ibase_connections = cn;
188 /* If no cached connection, we must set one up. */
190 if (cn->dbh == NULL || cn->transh == NULL)
194 static char trans_options[] =
195 { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed,
199 /* Construct the database parameter buffer. */
201 *dpb++ = isc_dpb_version1;
202 *dpb++ = isc_dpb_user_name;
203 *dpb++ = strlen(sdata[1]);
204 for (char * p = sdata[1]; *p;)
206 *dpb++ = isc_dpb_password;
207 *dpb++ = strlen(sdata[2]);
208 for (char * p = sdata[2]; *p;)
210 dpb_length = dpb - buffer;
213 debug_printf_indent("new Interbase connection: database=%s user=%s\n",
216 /* Connect to the database */
217 if (isc_attach_database
218 (status, 0, sdata[0], &cn->dbh, dpb_length, buffer))
220 isc_interprete(buffer, &statusp);
222 string_sprintf("Interbase attach() failed: %s", buffer);
223 *defer_break = FALSE;
227 /* Now start a read-only read-committed transaction */
228 if (isc_start_transaction
229 (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options),
232 isc_interprete(buffer, &statusp);
233 isc_detach_database(status, &cn->dbh);
235 string_sprintf("Interbase start_transaction() failed: %s",
237 *defer_break = FALSE;
243 if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth))
245 isc_interprete(buffer, &statusp);
247 string_sprintf("Interbase alloc_statement() failed: %s",
249 *defer_break = FALSE;
253 /* Lacking any information, assume that the data is untainted */
254 reset_point = store_mark();
255 out_sqlda = store_get(XSQLDA_LENGTH(1), FALSE);
256 out_sqlda->version = SQLDA_VERSION1;
260 (status, &cn->transh, &stmth, 0, query, 1, out_sqlda))
262 isc_interprete(buffer, &statusp);
263 reset_point = store_reset(reset_point);
266 string_sprintf("Interbase prepare_statement() failed: %s",
268 *defer_break = FALSE;
272 /* re-allocate the output structure if there's more than one field */
273 if (out_sqlda->sqln < out_sqlda->sqld)
275 XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld), FALSE);
276 if (isc_dsql_describe
277 (status, &stmth, out_sqlda->version, new_sqlda))
279 isc_interprete(buffer, &statusp);
280 isc_dsql_free_statement(status, &stmth, DSQL_drop);
281 reset_point = store_reset(reset_point);
283 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
285 *defer_break = FALSE;
288 out_sqlda = new_sqlda;
291 /* allocate storage for every returned field */
292 for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++)
294 switch (var->sqltype & ~1)
297 var->sqldata = CS store_get(sizeof(char) * var->sqllen + 2, FALSE);
300 var->sqldata = CS store_get(sizeof(char) * var->sqllen, FALSE);
303 var->sqldata = CS store_get(sizeof(short), FALSE);
306 var->sqldata = CS store_get(sizeof(ISC_LONG), FALSE);
310 var->sqldata = CS store_get(sizeof(ISC_INT64), FALSE);
314 var->sqldata = CS store_get(sizeof(float), FALSE);
317 var->sqldata = CS store_get(sizeof(double), FALSE);
321 var->sqldata = CS store_get(sizeof(ISC_QUAD), FALSE);
325 var->sqldata = CS store_get(sizeof(ISC_TIMESTAMP), FALSE);
328 var->sqldata = CS store_get(sizeof(ISC_DATE), FALSE);
331 var->sqldata = CS store_get(sizeof(ISC_TIME), FALSE);
335 if (var->sqltype & 1)
336 var->sqlind = (short *) store_get(sizeof(short), FALSE);
339 /* finally, we're ready to execute the statement */
341 (status, &cn->transh, &stmth, out_sqlda->version, NULL))
343 isc_interprete(buffer, &statusp);
344 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
346 isc_dsql_free_statement(status, &stmth, DSQL_drop);
347 *defer_break = FALSE;
351 while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) != 100L)
353 /* check if an error occurred */
354 if (status[0] & status[1])
356 isc_interprete(buffer, &statusp);
358 string_sprintf("Interbase fetch() failed: %s", buffer);
359 isc_dsql_free_statement(status, &stmth, DSQL_drop);
360 *defer_break = FALSE;
365 result = string_catn(result, US "\n", 1);
367 /* Find the number of fields returned. If this is one, we don't add field
368 names to the data. Otherwise we do. */
369 if (out_sqlda->sqld == 1)
371 if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1) /* NULL value yields nothing */
372 result = string_catn(result, US buffer,
373 fetch_field(buffer, sizeof(buffer),
374 &out_sqlda->sqlvar[0]));
378 for (int i = 0; i < out_sqlda->sqld; i++)
380 int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]);
382 result = string_catn(result, US out_sqlda->sqlvar[i].aliasname,
383 out_sqlda->sqlvar[i].aliasname_length);
384 result = string_catn(result, US "=", 1);
386 /* Quote the value if it contains spaces or is empty */
388 if (*out_sqlda->sqlvar[i].sqlind == -1) /* NULL value */
389 result = string_catn(result, US "\"\"", 2);
391 else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL)
393 result = string_catn(result, US "\"", 1);
394 for (int j = 0; j < len; j++)
396 if (buffer[j] == '\"' || buffer[j] == '\\')
397 result = string_cat(result, US "\\", 1);
398 result = string_cat(result, US buffer + j, 1);
400 result = string_catn(result, US "\"", 1);
403 result = string_catn(result, US buffer, len);
404 result = string_catn(result, US " ", 1);
408 /* If result is NULL then no data has been found and so we return FAIL.
409 Otherwise, we must terminate the string which has been built; string_cat()
410 always leaves enough room for a terminating zero. */
415 *errmsg = US "Interbase: no data found";
418 gstring_release_unused(result);
421 /* Get here by goto from various error checks. */
426 isc_dsql_free_statement(status, &stmth, DSQL_drop);
428 /* Non-NULL result indicates a successful result */
432 *resultptr = string_from_gstring(result);
437 DEBUG(D_lookup) debug_printf_indent("%s\n", *errmsg);
438 return yield; /* FAIL or DEFER */
445 /*************************************************
447 *************************************************/
449 /* See local README for interface description. The handle and filename
450 arguments are not used. Loop through a list of servers while the query is
451 deferred with a retryable error. */
454 ibase_find(void * handle, const uschar * filename, uschar * query, int length,
455 uschar ** result, uschar ** errmsg, uint * do_cache, const uschar * opts)
459 uschar *list = ibase_servers;
462 /* Keep picky compilers happy */
465 DEBUG(D_lookup) debug_printf_indent("Interbase query: %s\n", query);
467 while ((server = string_nextinlist(&list, &sep, buffer, sizeof(buffer))))
469 BOOL defer_break = FALSE;
470 int rc = perform_ibase_search(query, server, result, errmsg, &defer_break);
471 if (rc != DEFER || defer_break)
476 *errmsg = US "no Interbase servers defined (ibase_servers option)";
483 /*************************************************
484 * Quote entry point *
485 *************************************************/
487 /* The only characters that need to be quoted (with backslash) are newline,
488 tab, carriage return, backspace, backslash itself, and the quote characters.
489 Percent, and underscore and not escaped. They are only special in contexts
490 where they can be wild cards, and this isn't usually the case for data inserted
491 from messages, since that isn't likely to be treated as a pattern of any kind.
492 Sadly, MySQL doesn't seem to behave like other programs. If you use something
493 like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
494 can't quote "on spec".
497 s the string to be quoted
498 opt additional option text or NULL if none
500 Returns: the processed string or NULL for a bad option
503 static uschar *ibase_quote(uschar * s, uschar * opt)
511 return NULL; /* No options recognized */
513 while ((c = *t++) != 0)
514 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
519 t = quoted = store_get(Ustrlen(s) + count + 1, FALSE);
521 while ((c = *s++) != 0) {
522 if (Ustrchr("'", c) != NULL) {
527 case '\n': *t++ = 'n';
529 case '\t': *t++ = 't';
531 case '\r': *t++ = 'r';
533 case '\b': *t++ = 'b';
547 /*************************************************
548 * Version reporting entry point *
549 *************************************************/
551 /* See local README for interface description. */
553 #include "../version.h"
556 ibase_version_report(FILE *f)
559 fprintf(f, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR);
564 static lookup_info _lookup_info = {
565 .name = US"ibase", /* lookup name */
566 .type = lookup_querystyle, /* query-style lookup */
567 .open = ibase_open, /* open function */
568 .check NULL, /* no check function */
569 .find = ibase_find, /* find function */
570 .close = NULL, /* no close function */
571 .tidy = ibase_tidy, /* tidy function */
572 .quote = ibase_quote, /* quoting function */
573 .version_report = ibase_version_report /* version reporting */
577 #define ibase_lookup_module_info _lookup_module_info
580 static lookup_info *_lookup_list[] = { &_lookup_info };
581 lookup_module_info ibase_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
583 /* End of lookups/ibase.c */