summaryrefslogtreecommitdiffstats
path: root/src/lookups/ibase.c
blob: e52ca27a083f6e9e430385451d9e77d5de06f040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*************************************************
*     Exim - an Internet mail transport agent    *
*************************************************/

/* Copyright (c) University of Cambridge 1995 - 2018 */
/* See the file NOTICE for conditions of use and distribution. */

/* The code in this module was contributed by Ard Biesheuvel. */

#include "../exim.h"
#include "lf_functions.h"

#include <ibase.h>              /* The system header */

/* Structure and anchor for caching connections. */

typedef struct ibase_connection {
    struct ibase_connection *next;
    uschar *server;
    isc_db_handle dbh;
    isc_tr_handle transh;
} ibase_connection;

static ibase_connection *ibase_connections = NULL;



/*************************************************
*              Open entry point                  *
*************************************************/

/* See local README for interface description. */

static void *ibase_open(uschar * filename, uschar ** errmsg)
{
    return (void *) (1);        /* Just return something non-null */
}



/*************************************************
*               Tidy entry point                 *
*************************************************/

/* See local README for interface description. */

static void ibase_tidy(void)
{
    ibase_connection *cn;
    ISC_STATUS status[20];

    while ((cn = ibase_connections) != NULL) {
        ibase_connections = cn->next;
        DEBUG(D_lookup) debug_printf("close Interbase connection: %s\n",
                                     cn->server);
        isc_commit_transaction(status, &cn->transh);
        isc_detach_database(status, &cn->dbh);
    }
}

static int fetch_field(char *buffer, int buffer_size, XSQLVAR * var)
{
    if (buffer_size < var->sqllen)
        return 0;

    switch (var->sqltype & ~1) {
    case SQL_VARYING:
        strncpy(buffer, &var->sqldata[2], *(short *) var->sqldata);
        return *(short *) var->sqldata;
    case SQL_TEXT:
        strncpy(buffer, var->sqldata, var->sqllen);
        return var->sqllen;
    case SQL_SHORT:
        return sprintf(buffer, "%d", *(short *) var->sqldata);
    case SQL_LONG:
        return sprintf(buffer, "%ld", *(ISC_LONG *) var->sqldata);
#ifdef SQL_INT64
    case SQL_INT64:
        return sprintf(buffer, "%lld", *(ISC_INT64 *) var->sqldata);
#endif
    default:
        /* not implemented */
        return 0;
    }
}

/*************************************************
*        Internal search function                *
*************************************************/

/* This function is called from the find entry point to do the search for a
single server.

Arguments:
  query        the query string
  server       the server string
  resultptr    where to store the result
  errmsg       where to point an error message
  defer_break  TRUE if no more servers are to be tried after DEFER

The server string is of the form "host:dbname|user|password". The host can be
host:port. This string is in a nextinlist temporary buffer, so can be
overwritten.

Returns:       OK, FAIL, or DEFER
*/

static int
perform_ibase_search(uschar * query, uschar * server, uschar ** resultptr,
                     uschar ** errmsg, BOOL * defer_break)
{
isc_stmt_handle stmth = NULL;
XSQLDA *out_sqlda;
XSQLVAR *var;

char buffer[256];
ISC_STATUS status[20], *statusp = status;

gstring * result;
int i;
int yield = DEFER;
ibase_connection *cn;
uschar *server_copy = NULL;
uschar *sdata[3];

/* Disaggregate the parameters from the server argument. The order is host,
database, user, password. We can write to the string, since it is in a
nextinlist temporary buffer. The copy of the string that is used for caching
has the password removed. This copy is also used for debugging output. */

for (i = 2; i > 0; i--)
  {
  uschar *pp = Ustrrchr(server, '|');

  if (pp == NULL)
    {
    *errmsg = string_sprintf("incomplete Interbase server data: %s",
		       (i == 3) ? server : server_copy);
    *defer_break = TRUE;
    return DEFER;
    }
  *pp++ = 0;
  sdata[i] = pp;
  if (i == 2)
      server_copy = string_copy(server);   /* sans password */
  }
sdata[0] = server;          /* What's left at the start */

/* See if we have a cached connection to the server */

for (cn = ibase_connections; cn != NULL; cn = cn->next)
  if (Ustrcmp(cn->server, server_copy) == 0)
    break;

/* Use a previously cached connection ? */

if (cn)
  {
  static char db_info_options[] = { isc_info_base_level };

  /* test if the connection is alive */
  if (isc_database_info(status, &cn->dbh, sizeof(db_info_options),
	db_info_options, sizeof(buffer), buffer))
    {
    /* error occurred: assume connection is down */
    DEBUG(D_lookup)
	debug_printf
	("Interbase cleaning up cached connection: %s\n",
	 cn->server);
    isc_detach_database(status, &cn->dbh);
    }
  else
    {
    DEBUG(D_lookup) debug_printf("Interbase using cached connection for %s\n",
		     server_copy);
    }
  }
else
  {
  cn = store_get(sizeof(ibase_connection));
  cn->server = server_copy;
  cn->dbh = NULL;
  cn->transh = NULL;
  cn->next = ibase_connections;
  ibase_connections = cn;
  }

/* If no cached connection, we must set one up. */

if (cn->dbh == NULL || cn->transh == NULL)
  {
  char *dpb, *p;
  short dpb_length;
  static char trans_options[] =
      { isc_tpb_version3, isc_tpb_read, isc_tpb_read_committed,
      isc_tpb_rec_version
  };

  /* Construct the database parameter buffer. */
  dpb = buffer;
  *dpb++ = isc_dpb_version1;
  *dpb++ = isc_dpb_user_name;
  *dpb++ = strlen(sdata[1]);
  for (p = sdata[1]; *p;)
      *dpb++ = *p++;
  *dpb++ = isc_dpb_password;
  *dpb++ = strlen(sdata[2]);
  for (p = sdata[2]; *p;)
      *dpb++ = *p++;
  dpb_length = dpb - buffer;

  DEBUG(D_lookup)
      debug_printf("new Interbase connection: database=%s user=%s\n",
		   sdata[0], sdata[1]);

  /* Connect to the database */
  if (isc_attach_database
      (status, 0, sdata[0], &cn->dbh, dpb_length, buffer))
    {
    isc_interprete(buffer, &statusp);
    *errmsg =
	string_sprintf("Interbase attach() failed: %s", buffer);
    *defer_break = FALSE;
    goto IBASE_EXIT;
    }

  /* Now start a read-only read-committed transaction */
  if (isc_start_transaction
      (status, &cn->transh, 1, &cn->dbh, sizeof(trans_options),
       trans_options))
    {
    isc_interprete(buffer, &statusp);
    isc_detach_database(status, &cn->dbh);
    *errmsg =
	string_sprintf("Interbase start_transaction() failed: %s",
		       buffer);
    *defer_break = FALSE;
    goto IBASE_EXIT;
    }
  }

/* Run the query */
if (isc_dsql_allocate_statement(status, &cn->dbh, &stmth))
  {
  isc_interprete(buffer, &statusp);
  *errmsg =
      string_sprintf("Interbase alloc_statement() failed: %s",
		     buffer);
  *defer_break = FALSE;
  goto IBASE_EXIT;
  }

out_sqlda = store_get(XSQLDA_LENGTH(1));
out_sqlda->version = SQLDA_VERSION1;
out_sqlda->sqln = 1;

if (isc_dsql_prepare
    (status, &cn->transh, &stmth, 0, query, 1, out_sqlda))
  {
  isc_interprete(buffer, &statusp);
  store_reset(out_sqlda);
  out_sqlda = NULL;
  *errmsg =
      string_sprintf("Interbase prepare_statement() failed: %s",
		     buffer);
  *defer_break = FALSE;
  goto IBASE_EXIT;
  }

/* re-allocate the output structure if there's more than one field */
if (out_sqlda->sqln < out_sqlda->sqld)
  {
  XSQLDA *new_sqlda = store_get(XSQLDA_LENGTH(out_sqlda->sqld));
  if (isc_dsql_describe
      (status, &stmth, out_sqlda->version, new_sqlda))
    {
    isc_interprete(buffer, &statusp);
    isc_dsql_free_statement(status, &stmth, DSQL_drop);
    store_reset(out_sqlda);
    out_sqlda = NULL;
    *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
		       buffer);
    *defer_break = FALSE;
    goto IBASE_EXIT;
    }
  out_sqlda = new_sqlda;
  }

/* allocate storage for every returned field */
for (i = 0, var = out_sqlda->sqlvar; i < out_sqlda->sqld; i++, var++)
  {
  switch (var->sqltype & ~1)
    {
    case SQL_VARYING:
	var->sqldata = CS store_get(sizeof(char) * var->sqllen + 2);
	break;
    case SQL_TEXT:
	var->sqldata = CS store_get(sizeof(char) * var->sqllen);
	break;
    case SQL_SHORT:
	var->sqldata = CS  store_get(sizeof(short));
	break;
    case SQL_LONG:
	var->sqldata = CS  store_get(sizeof(ISC_LONG));
	break;
#ifdef SQL_INT64
    case SQL_INT64:
	var->sqldata = CS  store_get(sizeof(ISC_INT64));
	break;
#endif
    case SQL_FLOAT:
	var->sqldata = CS  store_get(sizeof(float));
	break;
    case SQL_DOUBLE:
	var->sqldata = CS  store_get(sizeof(double));
	break;
#ifdef SQL_TIMESTAMP
    case SQL_DATE:
	var->sqldata = CS  store_get(sizeof(ISC_QUAD));
	break;
#else
    case SQL_TIMESTAMP:
	var->sqldata = CS  store_get(sizeof(ISC_TIMESTAMP));
	break;
    case SQL_TYPE_DATE:
	var->sqldata = CS  store_get(sizeof(ISC_DATE));
	break;
    case SQL_TYPE_TIME:
	var->sqldata = CS  store_get(sizeof(ISC_TIME));
	break;
  #endif
    }
  if (var->sqltype & 1)
    var->sqlind = (short *) store_get(sizeof(short));
  }

/* finally, we're ready to execute the statement */
if (isc_dsql_execute
    (status, &cn->transh, &stmth, out_sqlda->version, NULL))
  {
  isc_interprete(buffer, &statusp);
  *errmsg = string_sprintf("Interbase describe_statement() failed: %s",
		     buffer);
  isc_dsql_free_statement(status, &stmth, DSQL_drop);
  *defer_break = FALSE;
  goto IBASE_EXIT;
  }

while (isc_dsql_fetch(status, &stmth, out_sqlda->version, out_sqlda) != 100L)
  {
  /* check if an error occurred */
  if (status[0] & status[1])
    {
    isc_interprete(buffer, &statusp);
    *errmsg =
	string_sprintf("Interbase fetch() failed: %s", buffer);
    isc_dsql_free_statement(status, &stmth, DSQL_drop);
    *defer_break = FALSE;
    goto IBASE_EXIT;
    }

  if (result)
    result = string_catn(result, US "\n", 1);

  /* Find the number of fields returned. If this is one, we don't add field
     names to the data. Otherwise we do. */
  if (out_sqlda->sqld == 1)
    {
    if (out_sqlda->sqlvar[0].sqlind == NULL || *out_sqlda->sqlvar[0].sqlind != -1)     /* NULL value yields nothing */
      result = string_catn(result, US buffer,
		       fetch_field(buffer, sizeof(buffer),
				   &out_sqlda->sqlvar[0]));
    }

  else
    for (i = 0; i < out_sqlda->sqld; i++)
      {
      int len = fetch_field(buffer, sizeof(buffer), &out_sqlda->sqlvar[i]);

      result = string_catn(result, US out_sqlda->sqlvar[i].aliasname,
		     out_sqlda->sqlvar[i].aliasname_length);
      result = string_catn(result, US "=", 1);

      /* Quote the value if it contains spaces or is empty */

      if (*out_sqlda->sqlvar[i].sqlind == -1)       /* NULL value */
	result = string_catn(result, US "\"\"", 2);

      else if (buffer[0] == 0 || Ustrchr(buffer, ' ') != NULL)
	{
	int j;

	result = string_catn(result, US "\"", 1);
	for (j = 0; j < len; j++)
	  {
	  if (buffer[j] == '\"' || buffer[j] == '\\')
	      result = string_cat(result, US "\\", 1);
	  result = string_cat(result, US buffer + j, 1);
	  }
	result = string_catn(result, US "\"", 1);
	}
      else
	result = string_catn(result, US buffer, len);
      result = string_catn(result, US " ", 1);
      }
  }

/* If result is NULL then no data has been found and so we return FAIL.
Otherwise, we must terminate the string which has been built; string_cat()
always leaves enough room for a terminating zero. */

if (!result)
  {
  yield = FAIL;
  *errmsg = US "Interbase: no data found";
  }
else
  store_reset(result->s + result->ptr + 1);


/* Get here by goto from various error checks. */

IBASE_EXIT:

if (stmth)
  isc_dsql_free_statement(status, &stmth, DSQL_drop);

/* Non-NULL result indicates a successful result */

if (result)
  {
  *resultptr = string_from_gstring(result);
  return OK;
  }
else
  {
  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
  return yield;           /* FAIL or DEFER */
  }
}




/*************************************************
*               Find entry point                 *
*************************************************/

/* See local README for interface description. The handle and filename
arguments are not used. Loop through a list of servers while the query is
deferred with a retryable error. */

static int
ibase_find(void *handle, uschar * filename, uschar * query, int length,
           uschar ** result, uschar ** errmsg, uint *do_cache)
{
    int sep = 0;
    uschar *server;
    uschar *list = ibase_servers;
    uschar buffer[512];

    /* Keep picky compilers happy */
    do_cache = do_cache;

    DEBUG(D_lookup) debug_printf("Interbase query: %s\n", query);

    while ((server =
            string_nextinlist(&list, &sep, buffer,
                              sizeof(buffer))) != NULL) {
        BOOL defer_break = FALSE;
        int rc = perform_ibase_search(query, server, result, errmsg,
                                      &defer_break);
        if (rc != DEFER || defer_break)
            return rc;
    }

    if (ibase_servers == NULL)
        *errmsg = US "no Interbase servers defined (ibase_servers option)";

    return DEFER;
}



/*************************************************
*               Quote entry point                *
*************************************************/

/* The only characters that need to be quoted (with backslash) are newline,
tab, carriage return, backspace, backslash itself, and the quote characters.
Percent, and underscore and not escaped. They are only special in contexts
where they can be wild cards, and this isn't usually the case for data inserted
from messages, since that isn't likely to be treated as a pattern of any kind.
Sadly, MySQL doesn't seem to behave like other programs. If you use something
like "where id="ab\%cd" it does not treat the string as "ab%cd". So you really
can't quote "on spec".

Arguments:
  s          the string to be quoted
  opt        additional option text or NULL if none

Returns:     the processed string or NULL for a bad option
*/

static uschar *ibase_quote(uschar * s, uschar * opt)
{
    register int c;
    int count = 0;
    uschar *t = s;
    uschar *quoted;

    if (opt != NULL)
        return NULL;            /* No options recognized */

    while ((c = *t++) != 0)
        if (Ustrchr("\n\t\r\b\'\"\\", c) != NULL)
            count++;

    if (count == 0)
        return s;
    t = quoted = store_get(Ustrlen(s) + count + 1);

    while ((c = *s++) != 0) {
        if (Ustrchr("'", c) != NULL) {
            *t++ = '\'';
            *t++ = '\'';
/*    switch(c)
      {
      case '\n': *t++ = 'n';
      break;
      case '\t': *t++ = 't';
      break;
      case '\r': *t++ = 'r';
      break;
      case '\b': *t++ = 'b';
      break;
      default:   *t++ = c;
      break;
      }*/
        } else
            *t++ = c;
    }

    *t = 0;
    return quoted;
}


/*************************************************
*         Version reporting entry point          *
*************************************************/

/* See local README for interface description. */

#include "../version.h"

void
ibase_version_report(FILE *f)
{
#ifdef DYNLOOKUP
fprintf(f, "Library version: ibase: Exim version %s\n", EXIM_VERSION_STR);
#endif
}


static lookup_info _lookup_info = {
  US"ibase",                     /* lookup name */
  lookup_querystyle,             /* query-style lookup */
  ibase_open,                    /* open function */
  NULL,                          /* no check function */
  ibase_find,                    /* find function */
  NULL,                          /* no close function */
  ibase_tidy,                    /* tidy function */
  ibase_quote,                   /* quoting function */
  ibase_version_report           /* version reporting */
};

#ifdef DYNLOOKUP
#define ibase_lookup_module_info _lookup_module_info
#endif

static lookup_info *_lookup_list[] = { &_lookup_info };
lookup_module_info ibase_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };

/* End of lookups/ibase.c */