aboutsummaryrefslogtreecommitdiff
path: root/mfd/bi_db.m4
blob: f97fa8b715a4aae374d7e19e4b381af0e47022e9 (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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2006, 2007, 2008, 2009 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#define DEFAULT_DB_MODE 0640
#include <fnmatch.h>

struct db_prop {                /* Database properties */
	char *pat;              /* Database name pattern */
	mode_t mode;            /* File mode */
	int null;               /* Null byte */
};

static mu_list_t db_prop_list;

static int
db_prop_compare(const void *item, const void *data)
{
	const struct db_prop *prop = item;
	const char *name = data;
	return strcmp(prop->pat, name);
}

static int
strtomode(char *str, mode_t *pmode)
{
	mode_t mode = 0;
	struct { char c; unsigned mask; } modetab[] = {
		{ 'r', S_IRUSR },
		{ 'w', S_IWUSR },
		{ 'x', S_IXUSR },
		{ 'r', S_IRGRP },
		{ 'w', S_IWGRP },
		{ 'x', S_IXGRP },
		{ 'r', S_IROTH },
		{ 'w', S_IWOTH },
		{ 'x', S_IXOTH },
		{ 0 }
	};
	int i;

	for (i = 0; modetab[i].c; i++) {
		if (!str[i])
			return i + 1;
		if (str[i] == modetab[i].c)
			mode |= modetab[i].mask;
		else if (str[i] != '-')
			return i + 1;
	}
	if (str[i])
		return i + 1;
	*pmode = mode;
	return 0;
}

/* #pragma dbprop <name> [null] [mode] */
MF_PRAGMA(dbprop, 3, 4)
{
	int null = 0;
	mode_t mode = DEFAULT_DB_MODE;
	struct db_prop *prop;
	int rc;
	char *pat;

	--argc;
	pat = *++argv;
	
	while (--argc) {
		char *p = *++argv;

		if (strcmp (p, "null") == 0)
			null = 1;
		else if (c_isdigit(*p)) {
			unsigned long n = strtoul(p, &p, 8);
			if (*p || (mode = n) != n) {
				parse_error(_("bad numeric file mode"));
				return;
			}
		} else if (rc = strtomode(p, &mode)) {
			parse_error(_("bad symbolic file mode (near %s)"),
				    p + rc - 1);
			return;
		}
	}

	if (!db_prop_list) {
		rc = mu_list_create(&db_prop_list);
		if (rc) {
			parse_error(_("Cannot create list: %s"),
				    mu_strerror(rc));
			return;
		}
		mu_list_set_comparator(db_prop_list, db_prop_compare);
	}

	if (mu_list_locate(db_prop_list, pat, (void**) &prop)) {
		prop = xmalloc(sizeof(*prop));
		prop->pat = xstrdup(pat);
		rc = mu_list_append(db_prop_list, prop);
		if (rc) {
			parse_error(_("Cannot create list: %s"),
				    mu_strerror(rc));
			return;
		}
	}
	prop->mode = mode;
	prop->null = null;
}

const struct db_prop *
db_prop_lookup(const char *name)
{
	mu_iterator_t itr;
	const struct db_prop *found = NULL;
	
	if (db_prop_list && mu_list_get_iterator(db_prop_list, &itr)) {
		for (mu_iterator_first(itr);
		     !mu_iterator_is_done(itr);
		     mu_iterator_next(itr)) {
			const struct db_prop *p;
			mu_iterator_current(itr, (void**)&p);
			if (strcmp(p->pat, name) == 0) {
				found = p;
				break;
			} else if (fnmatch(p->pat, name, 0) == 0)
				found = p;
		}
		mu_iterator_destroy(&itr);
	}
	return found;	
}


#define LOOKUP_NULL_BYTE 0x1
#define LOOKUP_TEST_ONLY 0x2

static int
dbmap_lookup(eval_environ_t env, char *dbname, const char *keystr,
	     const char *defval, int flags)
{
	int rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;

	if (!defval)
		defval = "";
	if (mu_dbm_open(dbname, &db, MU_STREAM_READ, 0, NULL))
		MF_THROW(mfe_dbfailure,
			 _("mu_dbm_open(%s) failed: %s"),
			 dbname,
			 mu_dbm_strerror());

	memset(&key, 0, sizeof key);
	memset(&contents, 0, sizeof contents);
	MU_DATUM_PTR(key) = (void*) keystr;
	MU_DATUM_SIZE(key) = strlen(keystr);
	if (flags & LOOKUP_NULL_BYTE)
		MU_DATUM_SIZE(key)++;
	rc = mu_dbm_fetch(&db, key, &contents) == 0;
	debug2(10, "Looking up %s: %s", keystr, rc ? "true" : "false");
	if (flags & LOOKUP_TEST_ONLY) 
		push(env, (STKVAL)rc);
	else {
		if (!rc) {
			if (defval)
				pushs(env, (STKVAL)defval);
			else
				push(env, 0);
		} else if (((char*)MU_DATUM_PTR(contents))[MU_DATUM_SIZE(contents)-1]) {
			size_t off;
			size_t len = MU_DATUM_SIZE(contents);
			char *s = MF_ALLOC_HEAP(off, len + 1);
			memcpy(s, MU_DATUM_PTR(contents), len);
			s[len] = 0;
			push(env, (STKVAL) off);
		} else
			pushs(env, MU_DATUM_PTR(contents));
	}
	mu_dbm_datum_free(&contents);
	mu_dbm_close(&db);
	return rc;
}

MF_DEFUN(dbmap, NUMBER, STRING dbname, STRING key, OPTIONAL, NUMBER null)
{
	const struct db_prop *prop = db_prop_lookup(dbname);
	dbmap_lookup(env, dbname, key, NULL,
		     LOOKUP_TEST_ONLY | 
		         (MF_OPTVAL(null, prop && prop->null)
			   ? LOOKUP_NULL_BYTE : 0));
}
END

MF_DEFUN(dbget, STRING, STRING dbname, STRING key, OPTIONAL,
	 STRING defval, NUMBER null)
{
	const struct db_prop *prop = db_prop_lookup(dbname);
	dbmap_lookup(env, dbname, key, 
                     MF_OPTVAL(defval), 
		     MF_OPTVAL(null, prop && prop->null)
		      ? LOOKUP_NULL_BYTE : 0);
}
END

MF_DEFUN(dbput, VOID, STRING dbname, STRING keystr, STRING value,
	 OPTIONAL, NUMBER null, NUMBER mode)
{
	int rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	const struct db_prop *prop = db_prop_lookup(dbname);
	
	if (mu_dbm_open(dbname, &db, MU_STREAM_RDWR,
			MF_OPTVAL(mode, (prop ? prop->mode : DEFAULT_DB_MODE)),
			NULL))
		MF_THROW(mfe_dbfailure,
			 _("mu_dbm_open(%s) failed: %s"),
			 dbname,
			 mu_dbm_strerror());
	memset(&key, 0, sizeof key);
	MU_DATUM_PTR(key) = keystr;
	MU_DATUM_SIZE(key) = strlen(keystr);
	if (MF_OPTVAL(null, prop && prop->null))
		MU_DATUM_SIZE(key)++;

	memset(&contents, 0, sizeof contents);
	MU_DATUM_PTR(contents) = value;
	MU_DATUM_SIZE(contents) = strlen(value) + 1;
	
	rc = mu_dbm_insert(&db, key, contents, 1);
	mu_dbm_close(&db);
	MF_ASSERT(rc == 0,
		  mfe_dbfailure,
		  _("Failed to insert data to %s: %s %s: %s"),
		  dbname,
		  keystr,
		  value,
		  mu_dbm_strerror());
}
END

MF_DEFUN(dbdel, VOID, STRING dbname, STRING keystr, OPTIONAL, NUMBER null,
	 NUMBER mode)
{
	DBM_FILE db;
	DBM_DATUM key;
	int rc;
	const struct db_prop *prop = db_prop_lookup(dbname);
	
	if (mu_dbm_open(dbname, &db, MU_STREAM_RDWR,
			MF_OPTVAL(mode, (prop ? prop->mode : DEFAULT_DB_MODE)),
			NULL))
		MF_THROW(mfe_dbfailure,
			 _("mu_dbm_open(%s) failed: %s"),
			 dbname,
			 mu_dbm_strerror());
	memset(&key, 0, sizeof key);
	MU_DATUM_PTR(key) = keystr;
	MU_DATUM_SIZE(key) = strlen(keystr);
	if (MF_OPTVAL(null, prop && prop->null))
		MU_DATUM_SIZE(key)++;
	rc = mu_dbm_delete(&db, key);
	mu_dbm_close(&db);
	MF_ASSERT(rc == 0,
                  mfe_dbfailure,
                  _("Failed to delete data `%s' from `%s': %s"),
		  keystr,
                  dbname,
		  mu_dbm_strerror());
}
END


#define NUMDB 128

struct db_tab {
	int used;
	DBM_FILE db;
	DBM_DATUM key;
};

static void *
alloc_db_tab()
{
	return xcalloc(NUMDB, sizeof(struct db_tab));
}

static void
close_db_tab(struct db_tab *dbt)
{
	if (dbt->used) {
		mu_dbm_datum_free(&dbt->key);
		mu_dbm_close(&dbt->db);
		dbt->used = 0;
	}
}

static void
destroy_db_tab(void *data)
{
	int i;
	struct db_tab *db = data;
	for (i = 0; i < NUMDB; i++) 
		close_db_tab(db + i);
	free(db);
}

MF_DECLARE_DATA(DBTAB, alloc_db_tab, destroy_db_tab);

static int
new_db_tab(struct db_tab *dbt)
{
	int i;
	for (i = 0; i < NUMDB; i++)
		if (!dbt[i].used) {
			dbt[i].used = 1;
			return i;
		}
	return -1;
}
			


MF_DEFUN(dbfirst, NUMBER, STRING dbname)
{
	int rc;
	int n;
	struct db_tab *dbt = MF_GET_DATA;
	DBM_FILE db;
	DBM_DATUM key;
	
	if (mu_dbm_open(dbname, &db, MU_STREAM_READ, 0, NULL))
		MF_THROW(mfe_dbfailure,
			 _("mu_dbm_open(%s) failed: %s"),
			 dbname,
			 mu_dbm_strerror());
	rc = mu_dbm_firstkey(&db, &key);
	MF_ASSERT(rc == 0, mfe_dbfailure,
		  _("mu_dbm_firstkey failed: %s"),
		  mu_dbm_strerror());
	n = new_db_tab(dbt);
	MF_ASSERT(n >= 0,
		  mfe_failure,
		 _("No more database entries available"));
	dbt += n;
	dbt->db = db;
	dbt->key = key;
	MF_RETURN(n);
}
END

MF_DEFUN(dbnext, NUMBER, NUMBER dn)
{
	struct db_tab *dbt = MF_GET_DATA + dn;
	DBM_DATUM nextkey;
	int rc;
	
	MF_ASSERT(dn >= 0 && dn < NUMDB && dbt->used,
		  mfe_range,
		  _("Invalid database descriptor"));

	rc = mu_dbm_nextkey(&dbt->db, dbt->key, &nextkey);
	if (rc) {
		close_db_tab(dbt);
		MF_RETURN(0);
	}
	mu_dbm_datum_free(&dbt->key);
	dbt->key = nextkey;
	MF_RETURN(1);
}
END

MF_DEFUN(dbkey, STRING, NUMBER dn)
{
	size_t off, len;
	char *s;
	struct db_tab *dbt = MF_GET_DATA + dn;
	
	MF_ASSERT(dn >= 0 && dn < NUMDB && dbt->used,
		  mfe_range,
		  _("Invalid database descriptor"));
	
	len = MU_DATUM_SIZE(dbt->key);
	s = MF_ALLOC_HEAP(off, len + 1);
	memcpy(s, MU_DATUM_PTR(dbt->key), len);
	s[len] = 0;
	MF_RETURN(off);
}
END

MF_DEFUN(dbvalue, STRING, NUMBER dn)
{
	int rc;
	size_t off, len;
	char *s;
	struct db_tab *dbt = MF_GET_DATA + dn;
	DBM_DATUM contents;
	
	MF_ASSERT(dn >= 0 && dn < NUMDB && dbt->used,
		  mfe_range,
		  _("Invalid database descriptor"));

	memset(&contents, 0, sizeof contents);
	rc = mu_dbm_fetch(&dbt->db, dbt->key, &contents);
	MF_ASSERT(rc == 0,
		  mfe_dbfailure,
		  _("Key not found: %s"),
		  mu_dbm_strerror());
	
	len = MU_DATUM_SIZE(contents);
	s = MF_ALLOC_HEAP(off, len + 1);
	memcpy(s, MU_DATUM_PTR(contents), len);
	s[len] = 0;
	mu_dbm_datum_free(&contents);
	MF_RETURN(off);
}
END


enum greylist_semantics
{
	greylist_traditional,
	greylist_ct
};

static enum greylist_semantics greylist_semantics = greylist_traditional;

/* #pragma greylist {traditional|gray|ct|con-tassios}*/
MF_PRAGMA(greylist, 2, 2)
{
	if (strcmp(argv[1], "traditional") == 0
	    || strcmp(argv[1], "gray") == 0)
		greylist_semantics = greylist_traditional;
	else if (strcmp(argv[1], "ct") == 0
		 || strcmp(argv[1], "con-tassios") == 0)
		greylist_semantics = greylist_ct;
	else
		/* TRANSLATORS: Do not translate keywords:
		   traditional, gray, ct, con-tassios */
		parse_error(_("unknown semantics; allowed values are: "
			      "traditional (or gray) and "
			      "ct (or con-tassios)"));
}

static void
greylist_print_item(const char *key, size_t size, const void *content)
{
	time_t timestamp = *(time_t*) content;
	size--; /* Size includes the trailing nul */
	printf("%*.*s ", size, size, key);
	format_time_str(stdout, timestamp);
	putchar('\n');
}

static int
greylist_expire_item(const void *content)
{
	time_t timestamp = *(time_t*) content;
	return greylist_format->expire_interval &&
		time(NULL) - timestamp > greylist_format->expire_interval;
}


static struct db_format greylist_format_struct = {
	"greylist",
	DEFAULT_GREYLIST_DATABASE,
	1,
	DEFAULT_EXPIRE_INTERVAL,
	greylist_print_item,
	greylist_expire_item
};

struct db_format *greylist_format = &greylist_format_struct;

MF_VAR(greylist_seconds_left, NUMBER);

/* The traditional (aka gray's) greylist implementation: the greylist
   database keeps the time the greylisting was activated.
 */
static int
do_greylist_traditional(eval_environ_t env, char *email, long interval)
{
	int rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	int readonly;
	time_t now;

	rc = mu_dbm_open(greylist_format->dbname, &db, MU_STREAM_RDWR, 0600,
			 &readonly);
        MF_ASSERT(rc == 0, mfe_dbfailure, _("mu_dbm_open(%s) failed: %s"),
			      greylist_format->dbname, mu_dbm_strerror());
	
	memset(&key, 0, sizeof key);
	memset(&contents, 0, sizeof contents);
	MU_DATUM_PTR(key) = email;
	MU_DATUM_SIZE(key) = strlen(email)+1;

	time(&now);
	if (mu_dbm_fetch(&db, key, &contents) == 0) {
		time_t timestamp, diff;

		MF_ASSERT(MU_DATUM_SIZE(contents) == sizeof timestamp,
			 mfe_dbfailure,
		         _("Greylist database %s has wrong data size"),
			 greylist_format->dbname);
		
		timestamp = *(time_t*) MU_DATUM_PTR(contents);
		diff = now - timestamp;

		__DBG(20) {
			char timebuf[32];
			debug_log("%s entered greylist database on %s, "
				  "%ld seconds ago",
				  email,
				  mailfromd_timestr(timestamp, timebuf,
						    sizeof timebuf),
				  (long) diff);
		}

		if (diff < interval) {
			diff = interval - diff;

			MF_VAR_REF(greylist_seconds_left, diff);
			
			debug2(20, "%s still greylisted (for %lu sec.)",
			       email,
			       (unsigned long) diff);
			rc = 1;
		} else if (diff > greylist_format->expire_interval) {
			debug1(20, "greylist record for %s expired",
			       email);
			MF_VAR_REF(greylist_seconds_left, interval);
			if (!readonly) {
				memcpy(MU_DATUM_PTR(contents),
				       &now, sizeof now);
				if (mu_dbm_insert(&db, key, contents, 1))
					mu_error(_("Cannot insert datum `%-.*s' in "
						   "greylist database %s: %s"),
			                         MU_DATUM_SIZE(key),
						 (char*)MU_DATUM_PTR(key),
						 greylist_format->dbname,
	                                         mu_dbm_strerror());
			} else
				debug(20, "database opened in readonly mode: "
				      "not updating");
			rc = 1;
		} else {
			debug1(20, "%s finished greylisting period",
			       email);
			rc = 0;
		}
		mu_dbm_datum_free(&contents);
	} else if (!readonly) {
		debug1(20, "greylisting %s", email);
		MF_VAR_REF(greylist_seconds_left, interval);
		MU_DATUM_PTR(contents) = (void*)&now;
		MU_DATUM_SIZE(contents) = sizeof now;
		if (mu_dbm_insert(&db, key, contents, 1))
			mu_error(_("Cannot insert datum `%-.*s' in greylist "
				   "database %s: %s"),
		                 MU_DATUM_SIZE(key), (char*)MU_DATUM_PTR(key),
				 greylist_format->dbname,
	                         mu_dbm_strerror());
		rc = 1;
	} else
		rc = 0;

	mu_dbm_close(&db);

	return rc;
}

/* Implementation of the is_greylisted predicate has no sense for 
   traditional greylist databases, because greylisting interval is
   not known beforehand.
   FIXME: keep the reference below up to date.
 */
static int
is_greylisted_traditional(eval_environ_t env, char *email)
{
	MF_THROW(mfe_failure,
		 _("is_greylisted is not implemented for traditional greylist databases; "
		   "see documentation, chapter %s %s for more info"),
		 "4.12.1.23", "Greylisting functions");
	return 0;
}

/* New greylist implementation (by Con Tassios): the database keeps
   the time the greylisting period is set to expire (`interval' seconds
   from now)
*/
static int
do_greylist_ct(eval_environ_t env, char *email, long interval)
{
	int rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	int readonly;
	time_t now;

	rc = mu_dbm_open(greylist_format->dbname, &db, MU_STREAM_RDWR, 0600,
			 &readonly);
        MF_ASSERT(rc == 0, mfe_dbfailure, _("mu_dbm_open(%s) failed: %s"),
			      greylist_format->dbname, mu_dbm_strerror());
	
	memset(&key, 0, sizeof key);
	memset(&contents, 0, sizeof contents);
	MU_DATUM_PTR(key) = email;
	MU_DATUM_SIZE(key) = strlen(email)+1;

	time(&now);
	if (mu_dbm_fetch(&db, key, &contents) == 0) {
		time_t timestamp;

		MF_ASSERT(MU_DATUM_SIZE(contents) == sizeof timestamp,
			 mfe_dbfailure,
		         _("Greylist database %s has wrong data size"),
			 greylist_format->dbname);
		
		timestamp = *(time_t*) MU_DATUM_PTR(contents);

		if (now < timestamp) {
			time_t diff = timestamp - now;
			MF_VAR_REF(greylist_seconds_left, diff);
			
			debug2(20, "%s still greylisted (for %lu sec.)",
			       email,
			       (unsigned long) diff);
			rc = 1;
		} else if (now - timestamp >
			   greylist_format->expire_interval) {
			debug1(20, "greylist record for %s expired", email);
			MF_VAR_REF(greylist_seconds_left, interval);
			if (!readonly) {
				now += interval;
				memcpy(MU_DATUM_PTR(contents),
				       &now, sizeof now);
				if (mu_dbm_insert(&db, key, contents, 1))
					mu_error(_("Cannot insert datum "
						   "`%-.*s' in greylist "
						   "database %s: %s"),
			                         MU_DATUM_SIZE(key),
						 (char*)MU_DATUM_PTR(key),
						 greylist_format->dbname,
	                                         mu_dbm_strerror());
			} else
				debug(20, "database opened in readonly mode: "
				      "not updating");
			rc = 1;
		} else {
			debug1(20, "%s finished greylisting period", email);
			rc = 0;
		}
		mu_dbm_datum_free(&contents);
	} else if (!readonly) {
		debug1(20, "greylisting %s", email);
		MF_VAR_REF(greylist_seconds_left, interval);
		now += interval;
		MU_DATUM_PTR(contents) = (void*)&now;
		MU_DATUM_SIZE(contents) = sizeof now;
		if (mu_dbm_insert(&db, key, contents, 1))
			mu_error(_("Cannot insert datum `%-.*s' in greylist "
				   "database %s: %s"),
		                 MU_DATUM_SIZE(key), (char*)MU_DATUM_PTR(key),
				 greylist_format->dbname,
	                         mu_dbm_strerror());
		rc = 1;
	} else
		rc = 0;
	
	mu_dbm_close(&db);
	
	return rc;
}

/* The `is_greylisted' predicate for new databases */
static int
is_greylisted_ct(eval_environ_t env, char *email)
{
	int rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	int readonly;
	time_t now;

	rc = mu_dbm_open(greylist_format->dbname, &db, MU_STREAM_RDWR, 0600,
			 &readonly);
	MF_ASSERT(rc == 0, mfe_dbfailure, _("mu_dbm_open(%s) failed: %s"),
		  greylist_format->dbname, mu_dbm_strerror());

	memset(&key, 0, sizeof key);
	memset(&contents, 0, sizeof contents);
	MU_DATUM_PTR(key) = email;
	MU_DATUM_SIZE(key) = strlen(email) + 1;

	time(&now);
	if (mu_dbm_fetch(&db, key, &contents) == 0) {
		time_t timestamp;

		MF_ASSERT(MU_DATUM_SIZE(contents) == sizeof timestamp,
			mfe_dbfailure,
			_("Greylist database %s has wrong data size"),
			greylist_format->dbname);
                
		timestamp = *(time_t*) MU_DATUM_PTR(contents);
                
		rc = timestamp > now;
                
		mu_dbm_datum_free(&contents);
	} else  
		rc = 0;

	mu_dbm_close(&db);

	return rc;
}

struct greylist_class {
	int (*gl_fun)(eval_environ_t, char *, long);
	int (*gl_pred)(eval_environ_t, char *);
};

struct greylist_class greylist_class[] = {
	{ do_greylist_traditional, is_greylisted_traditional },
	{ do_greylist_ct, is_greylisted_ct }
};

/* greylist(key, interval)

   Returns true if the key is greylisted, false if it's OK to
   deliver mail.
 */
MF_DEFUN(greylist, NUMBER, STRING email, NUMBER interval)
{
	MF_RETURN(greylist_class[greylist_semantics].gl_fun(env, email,
							    interval));
}
END

/* is_greylisted(key)
   
   Returns true if the key is greylisted, otherwise false
   
*/
MF_DEFUN(is_greylisted, NUMBER, STRING email)
{
	MF_RETURN(greylist_class[greylist_semantics].gl_pred(env, email));
}
END

MF_DEFUN(db_name, STRING, STRING fmtid)
{
	struct db_format *fmt = db_format_lookup(fmtid);
	MF_ASSERT(fmt != NULL,
		  mfe_not_found,
		  _("No such db format: %s"), fmtid);
	MF_RETURN_STRING(fmt->dbname);
}
END

MF_DEFUN(db_get_active, NUMBER, STRING fmtid)
{
	struct db_format *fmt = db_format_lookup(fmtid);
	MF_ASSERT(fmt != NULL,
		  mfe_not_found,
		  _("No such db format: %s"), fmtid);
	MF_RETURN(fmt->enabled);
}
END

MF_DEFUN(db_set_active, VOID, STRING fmtid, NUMBER active)
{
	struct db_format *fmt = db_format_lookup(fmtid);
	MF_ASSERT(fmt != NULL,
		  mfe_not_found,
		  _("No such db format: %s"), fmtid);
	fmt->enabled = active;
}
END

MF_DEFUN(db_expire_interval, NUMBER, STRING fmtid)
{
	struct db_format *fmt = db_format_lookup(fmtid);
	MF_ASSERT(fmt != NULL,
		  mfe_not_found,
		  _("No such db format: %s"), fmtid);
	MF_RETURN(fmt->expire_interval);
}
END

MF_INIT

Return to:

Send suggestions and report system problems to the System administrator.