1 /* $Cambridge: exim/src/src/lookups/ibase.c,v 1.4 2007/01/08 10:50:19 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2007 */
8 /* See the file NOTICE for conditions of use and distribution. */
10 /* The code in this module was contributed by Ard Biesheuvel. */
13 #include "lf_functions.h"
17 static void dummy(int x)
22 #include <ibase.h> /* The system header */
24 /* Structure and anchor for caching connections. */
26 typedef struct ibase_connection {
27 struct ibase_connection *next;
33 static ibase_connection *ibase_connections = NULL;
37 /*************************************************
39 *************************************************/
41 /* See local README for interface description. */
43 void *ibase_open(uschar * filename, uschar ** errmsg)
45 return (void *) (1); /* Just return something non-null */
50 /*************************************************
52 *************************************************/
54 /* See local README for interface description. */
59 ISC_STATUS status[20];
61 while ((cn = ibase_connections) != NULL) {
62 ibase_connections = cn->next;
63 DEBUG(D_lookup) debug_printf("close Interbase connection: %s\n",
65 isc_commit_transaction(status, &cn->transh);
66 isc_detach_database(status, &cn->dbh);
70 static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
72 if (buffer_size < var->sqllen)
75 switch (var->sqltype & ~1) {
77 strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
78 return *(short *) var->sqldata;
80 strncpy(buffer, var->sqldata, var->sqllen);
83 return sprintf(buffer, "%d", *(short *) var->sqldata);
85 return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
88 return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
96 /*************************************************
97 * Internal search function *
98 *************************************************/
100 /* This function is called from the find entry point to do the search for a
104 query the query string
105 server the server string
106 resultptr where to store the result
107 errmsg where to point an error message
108 defer_break TRUE if no more servers are to be tried after DEFER
110 The server string is of the form "host:dbname|user|password". The host can be
111 host:port. This string is in a nextinlist temporary buffer, so can be
114 Returns: OK, FAIL, or DEFER
118 perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
119 uschar ** errmsg, BOOL * defer_break)
121 isc_stmt_handle stmth = NULL;
126 ISC_STATUS status[20], *statusp = status;
132 uschar *result = NULL;
133 ibase_connection *cn;
134 uschar *server_copy = NULL;
137 /* Disaggregate the parameters from the server argument. The order is host,
138 database, user, password. We can write to the string, since it is in a
139 nextinlist temporary buffer. The copy of the string that is used for caching
140 has the password removed. This copy is also used for debugging output. */
142 for (i = 2; i > 0; i--) {
143 uschar *pp = Ustrrchr(server, '|');
146 string_sprintf("incomplete Interbase server data: %s",
147 (i == 3) ? server : server_copy);
154 server_copy = string_copy(server); /* sans password */
156 sdata[0] = server; /* What's left at the start */
158 /* See if we have a cached connection to the server */
160 for (cn = ibase_connections; cn != NULL; cn = cn->next) {
161 if (Ustrcmp(cn->server, server_copy) == 0) {
166 /* Use a previously cached connection ? */
169 static char db_info_options[] = { isc_info_base_level };
171 /* test if the connection is alive */
172 if (isc_database_info
173 (status, &cn->dbh, sizeof(db_info_options), db_info_options,
174 sizeof(buffer), buffer)) {
175 /* error occurred: assume connection is down */
178 ("Interbase cleaning up cached connection: %s\n",
180 isc_detach_database(status, &cn->dbh);
183 debug_printf("Interbase using cached connection for %s\n",
187 cn = store_get(sizeof(ibase_connection));
188 cn->server = server_copy;
191 cn->next = ibase_connections;
192 ibase_connections = cn;
195 /* If no cached connection, we must set one up. */
197 if (cn->dbh == NULL || cn->transh == NULL) {
201 static char trans_options[] =
202 { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed,
206 /* Construct the database parameter buffer. */
208 *dpb++ = isc_dpb_version1;
209 *dpb++ = isc_dpb_user_name;
210 *dpb++ = strlen(sdata[1]);
211 for (p = sdata[1]; *p;)
213 *dpb++ = isc_dpb_password;
214 *dpb++ = strlen(sdata[2]);
215 for (p = sdata[2]; *p;)
217 dpb_length = dpb - buffer;
220 debug_printf("new Interbase connection: database=%s user=%s\n",
223 /* Connect to the database */
224 if (isc_attach_database
225 (status, 0, sdata[0], &cn->dbh, dpb_length, buffer)) {
226 isc_interprete(buffer, &statusp);
228 string_sprintf("Interbase attach() failed: %s", buffer);
229 *defer_break = FALSE;
233 /* Now start a read-only read-committed transaction */
234 if (isc_start_transaction
235 (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options),
237 isc_interprete(buffer, &statusp);
238 isc_detach_database(status, &cn->dbh);
240 string_sprintf("Interbase start_transaction() failed: %s",
242 *defer_break = FALSE;
248 if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth)) {
249 isc_interprete(buffer, &statusp);
251 string_sprintf("Interbase alloc_statement() failed: %s",
253 *defer_break = FALSE;
257 out_sqlda = store_get(XSQLDA_LENGTH(1));
258 out_sqlda->version = SQLDA_VERSION1;
262 (status, &cn->transh, &stmth, 0, query, 1, out_sqlda)) {
263 isc_interprete(buffer, &statusp);
264 store_reset(out_sqlda);
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) {
275 XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld));
276 if (isc_dsql_describe
277 (status, &stmth, out_sqlda->version, new_sqlda)) {
278 isc_interprete(buffer, &statusp);
279 isc_dsql_free_statement(status, &stmth, DSQL_drop);
280 store_reset(out_sqlda);
283 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++) {
293 switch (var->sqltype & ~1) {
296 (char *) store_get(sizeof(char) * var->sqllen + 2);
300 (char *) store_get(sizeof(char) * var->sqllen);
303 var->sqldata = (char *) store_get(sizeof(short));
306 var->sqldata = (char *) store_get(sizeof(ISC_LONG));
310 var->sqldata = (char *) store_get(sizeof(ISC_INT64));
314 var->sqldata = (char *) store_get(sizeof(float));
317 var->sqldata = (char *) store_get(sizeof(double));
321 var->sqldata = (char *) store_get(sizeof(ISC_QUAD));
325 var->sqldata = (char *) store_get(sizeof(ISC_TIMESTAMP));
328 var->sqldata = (char *) store_get(sizeof(ISC_DATE));
331 var->sqldata = (char *) store_get(sizeof(ISC_TIME));
335 if (var->sqltype & 1) {
336 var->sqlind = (short *) store_get(sizeof(short));
340 /* finally, we're ready to execute the statement */
342 (status, &cn->transh, &stmth, out_sqlda->version, NULL)) {
343 isc_interprete(buffer, &statusp);
345 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) !=
354 /* check if an error occurred */
355 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_cat(result, &ssize, &offset, 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) {
370 if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1) /* NULL value yields nothing */
372 string_cat(result, &ssize, &offset, US buffer,
373 fetch_field(buffer, sizeof(buffer),
374 &out_sqlda->sqlvar[0]));
378 for (i = 0; i < out_sqlda->sqld; i++) {
379 int len = fetch_field(buffer, sizeof(buffer),
380 &out_sqlda->sqlvar[i]);
383 string_cat(result, &ssize, &offset,
384 US out_sqlda->sqlvar[i].aliasname,
385 out_sqlda->sqlvar[i].aliasname_length);
386 result = string_cat(result, &ssize, &offset, US "=", 1);
388 /* Quote the value if it contains spaces or is empty */
390 if (*out_sqlda->sqlvar[i].sqlind == -1) { /* NULL value */
392 string_cat(result, &ssize, &offset, US "\"\"", 2);
395 else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL) {
398 string_cat(result, &ssize, &offset, US "\"", 1);
399 for (j = 0; j < len; j++) {
400 if (buffer[j] == '\"' || buffer[j] == '\\')
402 string_cat(result, &ssize, &offset,
405 string_cat(result, &ssize, &offset,
409 string_cat(result, &ssize, &offset, US "\"", 1);
412 string_cat(result, &ssize, &offset, US buffer,
415 result = string_cat(result, &ssize, &offset, US " ", 1);
419 /* If result is NULL then no data has been found and so we return FAIL.
420 Otherwise, we must terminate the string which has been built; string_cat()
421 always leaves enough room for a terminating zero. */
423 if (result == NULL) {
425 *errmsg = US "Interbase: no data found";
428 store_reset(result + offset + 1);
432 /* Get here by goto from various error checks. */
437 isc_dsql_free_statement(status, &stmth, DSQL_drop);
439 /* Non-NULL result indicates a sucessful result */
441 if (result != NULL) {
445 DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
446 return yield; /* FAIL or DEFER */
453 /*************************************************
455 *************************************************/
457 /* See local README for interface description. The handle and filename
458 arguments are not used. Loop through a list of servers while the query is
459 deferred with a retryable error. */
462 ibase_find(void *handle, uschar * filename, uschar * query, int length,
463 uschar ** result, uschar ** errmsg, BOOL *do_cache)
467 uschar *list = ibase_servers;
470 /* Keep picky compilers happy */
473 DEBUG(D_lookup) debug_printf("Interbase query: %s\n", query);
476 string_nextinlist(&list, &sep, buffer,
477 sizeof(buffer))) != NULL) {
478 BOOL defer_break = FALSE;
479 int rc = perform_ibase_search(query, server, result, errmsg,
481 if (rc != DEFER || defer_break)
485 if (ibase_servers == NULL)
486 *errmsg = US "no Interbase servers defined (ibase_servers option)";
493 /*************************************************
494 * Quote entry point *
495 *************************************************/
497 /* The only characters that need to be quoted (with backslash) are newline,
498 tab, carriage return, backspace, backslash itself, and the quote characters.
499 Percent, and underscore and not escaped. They are only special in contexts
500 where they can be wild cards, and this isn't usually the case for data inserted
501 from messages, since that isn't likely to be treated as a pattern of any kind.
502 Sadly, MySQL doesn't seem to behave like other programs. If you use something
503 like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
504 can't quote "on spec".
507 s the string to be quoted
508 opt additional option text or NULL if none
510 Returns: the processed string or NULL for a bad option
513 uschar *ibase_quote(uschar * s, uschar * opt)
521 return NULL; /* No options recognized */
523 while ((c = *t++) != 0)
524 if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
529 t = quoted = store_get(Ustrlen(s) + count + 1);
531 while ((c = *s++) != 0) {
532 if (Ustrchr("'", c) != NULL) {
537 case '\n': *t++ = 'n';
539 case '\t': *t++ = 't';
541 case '\r': *t++ = 'r';
543 case '\b': *t++ = 'b';
558 /* End of lookups/ibase.c */