summaryrefslogtreecommitdiffabout
path: root/src/bi_db.m4
blob: f366008f93c43e09a3e4fa393ea176feab78b9a6 (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
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2006, 2007, 2008 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 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(mf_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)
{
	dbmap_lookup(env, dbname, key, NULL,
		     LOOKUP_TEST_ONLY | 
                        (MF_OPTVAL(null) ? LOOKUP_NULL_BYTE : 0));
}
END

MF_DEFUN(dbget, STRING, STRING dbname, STRING key, OPTIONAL,
	 STRING defval, NUMBER null)
{
	dbmap_lookup(env, dbname, key, 
                     MF_OPTVAL(defval), 
                     (MF_OPTVAL(null) ? LOOKUP_NULL_BYTE : 0));
}
END

MF_DEFUN(dbput, VOID, STRING dbname, STRING keystr, STRING value,
	 OPTIONAL, NUMBER null)
{
	int rc;
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	
	if (mu_dbm_open(dbname, &db, MU_STREAM_RDWR, 0640, NULL))
		MF_THROW(mf_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))
		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,
		  mf_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)
{
	DBM_FILE db;
	DBM_DATUM key;
	int rc;	
	if (mu_dbm_open(dbname, &db, MU_STREAM_RDWR, 0640, NULL))
		MF_THROW(mf_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))
		MU_DATUM_SIZE(key)++;
	rc = mu_dbm_delete(&db, key);
	mu_dbm_close(&db);
	MF_ASSERT(rc == 0,
                  mf_dbfailure,
                  _("Failed to delete data `%s' from `%s': %s"),
		  keystr,
                  dbname,
		  mu_dbm_strerror());
}
END


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);

/* 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)
{
	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, mf_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,
			 mf_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);
			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);

	MF_RETURN(rc);
}
END

MF_DEFUN(db_name, STRING, STRING fmtid)
{
	struct db_format *fmt = db_format_lookup(fmtid);
	MF_ASSERT(fmt != NULL,
		  mf_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,
		  mf_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,
		  mf_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,
		  mf_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.