1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* The code in this module was contributed by Ard Biesheuvel. */
11 #include "lf_functions.h"
13 #include <ibase.h> /* The system header */
15 /* Structure and anchor for caching connections. */
17 typedef struct ibase_connection {
18 struct ibase_connection *next;
24 static ibase_connection *ibase_connections = NULL;
28 /*************************************************
30 *************************************************/
32 /* See local README for interface description. */
34 static void *ibase_open(uschar * filename, uschar ** errmsg)
36 return (void *) (1); /* Just return something non-null */
41 /*************************************************
43 *************************************************/
45 /* See local README for interface description. */
47 static void ibase_tidy(void)
50 ISC_STATUS status[20];
52 while ((cn = ibase_connections) != NULL) {
53 ibase_connections = cn->next;
54 DEBUG(D_lookup) debug_printf("close Interbase connection: %s\n",
56 isc_commit_transaction(status, &cn->transh);
57 isc_detach_database(status, &cn->dbh);
61 static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
63 if (buffer_size < var->sqllen)
66 switch (var->sqltype & ~1) {
68 strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
69 return *(short *) var->sqldata;
71 strncpy(buffer, var->sqldata, var->sqllen);
74 return sprintf(buffer, "%d", *(short *) var->sqldata);
76 return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
79 return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
87 /*************************************************
88 * Internal search function *
89 *************************************************/
91 /* This function is called from the find entry point to do the search for a
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
101 The server string is of the form "host:dbname|user|password". The host can be
102 host:port. This string is in a nextinlist temporary buffer, so can be
105 Returns: OK, FAIL, or DEFER
109 perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
110 uschar ** errmsg, BOOL * defer_break)
112 isc_stmt_handle stmth = NULL;
117 ISC_STATUS status[20], *statusp = status;
122 ibase_connection *cn;
123 uschar *server_copy = NULL;
126 /* Disaggregate the parameters from the server argument. The order is host,
127 database, user, password. We can write to the string, since it is in a
128 nextinlist temporary buffer. The copy of the string that is used for caching
129 has the password removed. This copy is also used for debugging output. */
131 for (i = 2; i > 0; i--)
133 uschar *pp = Ustrrchr(server, '|');
137 *errmsg = string_sprintf("incomplete Interbase server data: %s",
138 (i == 3) ? server : server_copy);
145 server_copy = string_copy(server); /* sans password */
147 sdata[0] = server; /* What's left at the start */
149 /* See if we have a cached connection to the server */
151 for (cn = ibase_connections; cn != NULL; cn = cn->next)
152 if (Ustrcmp(cn->server, server_copy) == 0)
155 /* Use a previously cached connection ? */
159 static char db_info_options[] = { isc_info_base_level };
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))
165 /* error occurred: assume connection is down */
168 ("Interbase cleaning up cached connection: %s\n",
170 isc_detach_database(status, &cn->dbh);
174 DEBUG(D_lookup) debug_printf("Interbase using cached connection for %s\n",
180 cn = store_get(sizeof(ibase_connection));
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 (p = sdata[1]; *p;)
206 *dpb++ = isc_dpb_password;
207 *dpb++ = strlen(sdata[2]);
208 for (p = sdata[2]; *p;)
210 dpb_length = dpb - buffer;
213 debug_printf("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 out_sqlda = store_get(XSQLDA_LENGTH(1));
254 out_sqlda->version = SQLDA_VERSION1;
258 (status, &cn->transh, &stmth, 0, query, 1, out_sqlda))
260 isc_interprete(buffer, &statusp);
261 store_reset(out_sqlda);
264 string_sprintf("Interbase prepare_statement() failed: %s",
266 *defer_break = FALSE;
270 /* re-allocate the output structure if there's more than one field */
271 if (out_sqlda->sqln < out_sqlda->sqld)
273 XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld));
274 if (isc_dsql_describe
275 (status, &stmth, out_sqlda->version, new_sqlda))
277 isc_interprete(buffer, &statusp);
278 isc_dsql_free_statement(status, &stmth, DSQL_drop);
279 store_reset(out_sqlda);
281 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
283 *defer_break = FALSE;
286 out_sqlda = new_sqlda;
289 /* allocate storage for every returned field */
290 for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++)
292 switch (var->sqltype & ~1)
295 var->sqldata = CS store_get(sizeof(char) * var->sqllen + 2);
298 var->sqldata = CS store_get(sizeof(char) * var->sqllen);
301 var->sqldata = CS store_get(sizeof(short));
304 var->sqldata = CS store_get(sizeof(ISC_LONG));
308 var->sqldata = CS store_get(sizeof(ISC_INT64));
312 var->sqldata = CS store_get(sizeof(float));
315 var->sqldata = CS store_get(sizeof(double));
319 var->sqldata = CS store_get(sizeof(ISC_QUAD));
323 var->sqldata = CS store_get(sizeof(ISC_TIMESTAMP));
326 var->sqldata = CS store_get(sizeof(ISC_DATE));
329 var->sqldata = CS store_get(sizeof(ISC_TIME));
333 if (var->sqltype & 1)
334 var->sqlind = (short *) store_get(sizeof(short));
337 /* finally, we're ready to execute the statement */
339 (status, &cn->transh, &stmth, out_sqlda->version, NULL))
341 isc_interprete(buffer, &statusp);
342 *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
344 isc_dsql_free_statement(status, &stmth, DSQL_drop);
345 *defer_break = FALSE;
349 while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) != 100L)
351 /* check if an error occurred */
352 if (status[0] & status[1])
354 isc_interprete(buffer, &statusp);
356 string_sprintf("Interbase fetch() failed: %s", buffer);
357 isc_dsql_free_statement(status, &stmth, DSQL_drop);
358 *defer_break = FALSE;
363 result = string_catn(result, US "\n", 1);
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)
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]));
376 for (i = 0; i < out_sqlda->sqld; i++)
378 int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]);
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);
384 /* Quote the value if it contains spaces or is empty */
386 if (*out_sqlda->sqlvar[i].sqlind == -1) /* NULL value */
387 result = string_catn(result, US "\"\"", 2);
389 else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL)
393 result = string_catn(result, US "\"", 1);
394 for (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 store_reset(result->s + result->ptr + 1);
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("%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, uschar * filename, uschar * query, int length,
455 uschar ** result, uschar ** errmsg, uint *do_cache)
459 uschar *list = ibase_servers;
462 /* Keep picky compilers happy */
465 DEBUG(D_lookup) debug_printf("Interbase query: %s\n", query);
468 string_nextinlist(&list, &sep, buffer,
469 sizeof(buffer))) != NULL) {
470 BOOL defer_break = FALSE;
471 int rc = perform_ibase_search(query, server, result, errmsg,
473 if (rc != DEFER || defer_break)
477 if (ibase_servers == NULL)
478 *errmsg = US "no Interbase servers defined (ibase_servers option)";
485 /*************************************************
486 * Quote entry point *
487 *************************************************/
489 /* The only characters that need to be quoted (with backslash) are newline,
490 tab, carriage return, backspace, backslash itself, and the quote characters.
491 Percent, and underscore and not escaped. They are only special in contexts
492 where they can be wild cards, and this isn't usually the case for data inserted
493 from messages, since that isn't likely to be treated as a pattern of any kind.
494 Sadly, MySQL doesn't seem to behave like other programs. If you use something
495 like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
496 can't quote "on spec".
499 s the string to be quoted
500 opt additional option text or NULL if none
502 Returns: the processed string or NULL for a bad option
505 static uschar *ibase_quote(uschar * s, uschar * opt)
513 return NULL; /* No options recognized */
515 while ((c = *t++) != 0)
516 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
521 t = quoted = store_get(Ustrlen(s) + count + 1);
523 while ((c = *s++) != 0) {
524 if (Ustrchr("'", c) != NULL) {
529 case '\n': *t++ = 'n';
531 case '\t': *t++ = 't';
533 case '\r': *t++ = 'r';
535 case '\b': *t++ = 'b';
549 /*************************************************
550 * Version reporting entry point *
551 *************************************************/
553 /* See local README for interface description. */
555 #include "../version.h"
558 ibase_version_report(FILE *f)
561 fprintf(f, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR);
566 static lookup_info _lookup_info = {
567 US"ibase", /* lookup name */
568 lookup_querystyle, /* query-style lookup */
569 ibase_open, /* open function */
570 NULL, /* no check function */
571 ibase_find, /* find function */
572 NULL, /* no close function */
573 ibase_tidy, /* tidy function */
574 ibase_quote, /* quoting function */
575 ibase_version_report /* version reporting */
579 #define ibase_lookup_module_info _lookup_module_info
582 static lookup_info *_lookup_list[] = { &_lookup_info };
583 lookup_module_info ibase_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
585 /* End of lookups/ibase.c */