File: sieve-filter.c

package info (click to toggle)
dovecot 1%3A2.2.13-11
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 38,472 kB
  • sloc: ansic: 341,153; sh: 16,920; makefile: 5,385; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (567 lines) | stat: -rw-r--r-- 14,928 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "lib-signals.h"
#include "ioloop.h"
#include "env-util.h"
#include "str.h"
#include "str-sanitize.h"
#include "ostream.h"
#include "array.h"
#include "mail-namespace.h"
#include "mail-storage.h"
#include "mail-search-build.h"
#include "imap-utf7.h"

#include "sieve.h"
#include "sieve-extensions.h"
#include "sieve-binary.h"

#include "sieve-tool.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <sysexits.h>

/*
 * Print help
 */

static void print_help(void)
{
	printf(
"Usage: sieve-filter [-c <config-file>] [-C] [-D] [-e] [-m <default-mailbox>]\n"
"                    [-P <plugin>] [-q <output-mailbox>] [-Q <mail-command>]\n"
"                    [-s <script-file>] [-u <user>] [-v] [-W] [-x <extensions>]\n"
"                    <script-file> <source-mailbox> [<discard-action>]\n"
	);
}

enum sieve_filter_discard_action {
	SIEVE_FILTER_DACT_KEEP,        /* Keep discarded messages in source folder */
	SIEVE_FILTER_DACT_MOVE,        /* Move discarded messages to Trash folder */
	SIEVE_FILTER_DACT_DELETE,      /* Flag discarded messages as \DELETED */
	SIEVE_FILTER_DACT_EXPUNGE      /* Expunge discarded messages */
};

struct sieve_filter_data {
	enum sieve_filter_discard_action discard_action;
	struct mailbox *move_mailbox;

	struct sieve_script_env *senv;
	struct sieve_binary *main_sbin;
	struct sieve_error_handler *ehandler;

	unsigned int execute:1;
	unsigned int source_write:1;
};

struct sieve_filter_context {
	const struct sieve_filter_data *data;

	struct mailbox_transaction_context *move_trans;

	struct ostream *teststream;
};

static int filter_message
(struct sieve_filter_context *sfctx, struct mail *mail)
{
	struct sieve_error_handler *ehandler = sfctx->data->ehandler;
	struct sieve_script_env *senv = sfctx->data->senv;
	struct sieve_exec_status estatus;
	struct sieve_binary *sbin;
	struct sieve_message_data msgdata;
	const char *recipient = NULL, *sender = NULL;
	bool execute = sfctx->data->execute;
	bool source_write = sfctx->data->source_write;
	const char *subject, *date;
	uoff_t size = 0;
	int ret;

	sieve_tool_get_envelope_data(mail, &recipient, &sender);

	/* Initialize execution status */
	memset(&estatus, 0, sizeof(estatus));
	senv->exec_status = &estatus;

	/* Collect necessary message data */
	memset(&msgdata, 0, sizeof(msgdata));
	msgdata.mail = mail;
	msgdata.return_path = sender;
	msgdata.orig_envelope_to = recipient;
	msgdata.final_envelope_to = recipient;
	msgdata.auth_user = senv->user->username;
	(void)mail_get_first_header(mail, "Message-ID", &msgdata.id);

	if ( mail_get_virtual_size(mail, &size) < 0 ) {
		if ( mail->expunged )
			return 1;

		sieve_error(ehandler, NULL, "failed to obtain message size; "
			"skipping this message (id=%s)",
			( msgdata.id == NULL ? "none" : msgdata.id ));
		return 0;
	}

	if ( mail_get_first_header(mail, "date", &date) <= 0 )
		date = "";
	if ( mail_get_first_header(mail, "subject", &subject) <= 0 )
		subject = "";

	/* Single script */
	sbin = sfctx->data->main_sbin;

	/* Execute script */
	if ( execute ) {
		sieve_info(ehandler, NULL,
			"filtering: [%s; %"PRIuUOFF_T" bytes] `%s'", date, size,
			str_sanitize(subject, 40));

		ret = sieve_execute(sbin, &msgdata, senv, ehandler, 0, NULL);
	} else {
		(void)o_stream_send_str(sfctx->teststream,
			t_strdup_printf(">> Filtering message:\n\n"
				"  ID:      %s\n"
			  "  Date:    %s\n"
        "  Size:    %"PRIuUOFF_T" bytes\n"
				"  Subject: %s\n", ( msgdata.id == NULL ? "none" : msgdata.id ),
				date, size, str_sanitize(subject, 40)));

		ret = sieve_test
			(sbin, &msgdata, senv, ehandler, sfctx->teststream, 0, NULL);
	}

	/* Handle message in source folder */
	if ( ret > 0 ) {
		struct mailbox *move_box = sfctx->data->move_mailbox;
		enum sieve_filter_discard_action discard_action =
			sfctx->data->discard_action;

		if ( !source_write ) {
			/* READ-ONLY; Do nothing */

		} else if ( estatus.keep_original  ) {
			/* Explicitly `stored' in source box; just keep it there */
			sieve_info(ehandler, NULL, "message kept in source mailbox");

		} else if ( estatus.message_saved ) {
			sieve_info(ehandler, NULL,
				"message expunged from source mailbox upon successful move");

			if ( execute )
				mail_expunge(mail);

		} else {

			switch ( discard_action ) {
			/* Leave it there */
			case SIEVE_FILTER_DACT_KEEP:
				sieve_info(ehandler, NULL, "message left in source mailbox");
				break;
			/* Move message to indicated folder */
			case SIEVE_FILTER_DACT_MOVE:
				sieve_info(ehandler, NULL,
					"message in source mailbox moved to mailbox '%s'",
					mailbox_get_name(move_box));

				if ( execute && move_box != NULL ) {
					struct mailbox_transaction_context *t = sfctx->move_trans;
					struct mail_save_context *save_ctx;

					save_ctx = mailbox_save_alloc(t);

					if ( mailbox_copy(&save_ctx, mail) < 0 ) {
						enum mail_error error;
						const char *errstr;

						errstr = mail_storage_get_last_error
							(mailbox_get_storage(move_box), &error);

						sieve_error(ehandler, NULL,
							"failed to move message to mailbox %s: %s",
							mailbox_get_name(move_box), errstr);
						return -1;
				    }

					mail_expunge(mail);
				}
				break;
			/* Flag message as \DELETED */
			case SIEVE_FILTER_DACT_DELETE:
				sieve_info(ehandler, NULL, "message flagged as deleted in source mailbox");
				if ( execute )
					mail_update_flags(mail, MODIFY_ADD, MAIL_DELETED);
				break;
			/* Expunge the message immediately */
			case SIEVE_FILTER_DACT_EXPUNGE:
				sieve_info(ehandler, NULL, "message expunged from source mailbox");
				if ( execute )
					mail_expunge(mail);
				break;
			/* Unknown */
			default:
				i_unreached();
				break;
			}
		}
	}

	switch ( ret ) {
	case SIEVE_EXEC_OK:
		break;
	case SIEVE_EXEC_BIN_CORRUPT:
		sieve_error(ehandler, NULL, "sieve script binary is corrupt");
		return -1;
	case SIEVE_EXEC_FAILURE:
	case SIEVE_EXEC_TEMP_FAILURE:
	case SIEVE_EXEC_KEEP_FAILED:
		sieve_error(ehandler, NULL,
			"sieve script execution failed for this message; "
			"message left in source mailbox");
		return 0;
	}

	return 1;
}

/* FIXME: introduce this into Dovecot */
static void mail_search_build_add_flags
(struct mail_search_args *args, enum mail_flags flags, bool not)
{
	struct mail_search_arg *arg;

	arg = p_new(args->pool, struct mail_search_arg, 1);
	arg->type = SEARCH_FLAGS;
	arg->value.flags = flags;
	arg->match_not = not;

	arg->next = args->args;
	args->args = arg;
}

static int filter_mailbox
(const struct sieve_filter_data *sfdata, struct mailbox *src_box)
{
	struct sieve_filter_context sfctx;
	struct mailbox *move_box = sfdata->move_mailbox;
	struct sieve_error_handler *ehandler = sfdata->ehandler;
	struct mail_search_args *search_args;
	struct mailbox_transaction_context *t;
	struct mail_search_context *search_ctx;
	struct mail *mail;
	int ret = 1;

	/* Sync source mailbox */

	if ( mailbox_sync(src_box, MAILBOX_SYNC_FLAG_FULL_READ) < 0 ) {
		sieve_error(ehandler, NULL, "failed to sync source mailbox");
		return -1;
	}

	/* Initialize */

	memset(&sfctx, 0, sizeof(sfctx));
	sfctx.data = sfdata;

	/* Create test stream */
	if ( !sfdata->execute )
		sfctx.teststream = o_stream_create_fd(1, 0, FALSE);

	/* Start move mailbox transaction */

	if ( move_box != NULL ) {
		sfctx.move_trans = mailbox_transaction_begin
			(move_box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
	}

	/* Search non-deleted messages in the source folder */

	search_args = mail_search_build_init();
	mail_search_build_add_flags(search_args, MAIL_DELETED, TRUE);

	t = mailbox_transaction_begin(src_box, 0);
	search_ctx = mailbox_search_init(t, search_args, NULL, 0, NULL);
	mail_search_args_unref(&search_args);

	/* Iterate through all requested messages */

	while ( ret >= 0 && mailbox_search_next(search_ctx, &mail) > 0 ) {
		ret = filter_message(&sfctx, mail);
	}

	/* Cleanup */

	if ( mailbox_search_deinit(&search_ctx) < 0 ) {
		ret = -1;
	}

	if ( sfctx.move_trans != NULL ) {
		if ( mailbox_transaction_commit(&sfctx.move_trans) < 0 ) {
			ret = -1;
		}
	}

	if ( mailbox_transaction_commit(&t) < 0 ) {
		ret = -1;
	}

	if ( sfctx.teststream != NULL )
		o_stream_destroy(&sfctx.teststream);

	if ( ret < 0 ) return ret;

	/* Sync mailbox */

	if ( sfdata->execute ) {
		if ( mailbox_sync(src_box, MAILBOX_SYNC_FLAG_FULL_WRITE) < 0 ) {
			sieve_error(ehandler, NULL, "failed to sync source mailbox");
			return -1;
		}
	}

	return ret;
}

static const char *mailbox_name_to_mutf7(const char *mailbox_utf8)
{
	string_t *str = t_str_new(128);

	if (imap_utf8_to_utf7(mailbox_utf8, str) < 0)
		return mailbox_utf8;
	else
		return str_c(str);
}

/*
 * Tool implementation
 */

int main(int argc, char **argv)
{
	struct sieve_instance *svinst;
	ARRAY_TYPE (const_string) scriptfiles;
	const char *scriptfile,	*src_mailbox, *dst_mailbox, *move_mailbox;
	struct sieve_filter_data sfdata;
	enum sieve_filter_discard_action discard_action = SIEVE_FILTER_DACT_KEEP;
	struct mail_user *mail_user;
	struct sieve_binary *main_sbin;
	struct sieve_script_env scriptenv;
	struct sieve_error_handler *ehandler;
	bool force_compile, execute, source_write, verbose;
	struct mail_namespace *ns;
	struct mailbox *src_box = NULL, *move_box = NULL;
	enum mailbox_flags open_flags = MAILBOX_FLAG_IGNORE_ACLS;
	enum mail_error error;
	int c;

	sieve_tool = sieve_tool_init("sieve-filter", &argc, &argv,
		"m:s:x:P:u:q:Q:DCevW", FALSE);

	t_array_init(&scriptfiles, 16);

	/* Parse arguments */
	dst_mailbox = move_mailbox = NULL;
	force_compile = execute = source_write = verbose = FALSE;
	while ((c = sieve_tool_getopt(sieve_tool)) > 0) {
		switch (c) {
		case 'm':
			/* default mailbox (keep box) */
			dst_mailbox = optarg;
			break;
		case 's':
			/* scriptfile executed before main script */
			{
				const char *file;

				file = t_strdup(optarg);
				array_append(&scriptfiles, &file, 1);

				/* FIXME: */
				i_fatal_status(EX_USAGE,
					"The -s argument is currently NOT IMPLEMENTED");
			}
			break;
		case 'q':
			i_fatal_status(EX_USAGE,
				"The -q argument is currently NOT IMPLEMENTED");
			break;
		case 'Q':
			i_fatal_status(EX_USAGE,
				"The -Q argument is currently NOT IMPLEMENTED");
			break;
		case 'e':
			/* execution mode */
			execute = TRUE;
			break;
		case 'C':
			/* force script compile */
			force_compile = TRUE;
			break;
		case 'W':
			/* enable source mailbox write */
			source_write = TRUE;
			break;
		case 'v':
			/* enable verbose output */
			verbose = TRUE;
			break;
		default:
			/* unrecognized option */
			print_help();
			i_fatal_status(EX_USAGE, "Unknown argument: %c", c);
			break;
		}
	}

	/* Script file argument */
	if ( optind < argc ) {
		scriptfile = t_strdup(argv[optind++]);
	} else {
		print_help();
		i_fatal_status(EX_USAGE, "Missing <script-file> argument");
	}

	/* Source mailbox argument */
	if ( optind < argc ) {
		src_mailbox = t_strdup(argv[optind++]);
	} else {
		print_help();
		i_fatal_status(EX_USAGE, "Missing <source-mailbox> argument");
	}

	/* Source action argument */
	if ( optind < argc ) {
		const char *srcact = argv[optind++];

		if ( strcmp(srcact, "keep") == 0 ) {
			discard_action = SIEVE_FILTER_DACT_KEEP;
		} else if ( strcmp(srcact, "move") == 0 ) {
			discard_action = SIEVE_FILTER_DACT_MOVE;
			if ( optind < argc ) {
				move_mailbox = t_strdup(argv[optind++]);
			} else {
				print_help();
				i_fatal_status(EX_USAGE,
					"Invalid <discard-action> argument: "
					"the `move' action requires mailbox argument");
			}
		} else if ( strcmp(srcact, "delete") == 0 ) {
			discard_action = SIEVE_FILTER_DACT_DELETE;
		} else if ( strcmp(srcact, "expunge") == 0 ) {
			discard_action = SIEVE_FILTER_DACT_EXPUNGE;
		} else {
			print_help();
			i_fatal_status(EX_USAGE, "Invalid <discard-action> argument");
		}
	}

	if ( optind != argc ) {
		print_help();
		i_fatal_status(EX_USAGE, "Unknown argument: %s", argv[optind]);
	}

	if ( dst_mailbox == NULL ) {
		dst_mailbox = src_mailbox;
	}

	/* Finish tool initialization */
	svinst = sieve_tool_init_finish(sieve_tool, TRUE, FALSE);

	/* Enable debug extension */
	sieve_enable_debug_extension(svinst);

	/* Create error handler */
	ehandler = sieve_stderr_ehandler_create(svinst, 0);
	sieve_system_ehandler_set(ehandler);
	sieve_error_handler_accept_infolog(ehandler, verbose);

	/* Compile main sieve script */
	if ( force_compile ) {
		main_sbin = sieve_tool_script_compile(svinst, scriptfile, NULL);
		if ( main_sbin != NULL )
			(void) sieve_save(main_sbin, TRUE, NULL);
	} else {
		main_sbin = sieve_tool_script_open(svinst, scriptfile);
	}

	/* Initialize mail user */
	mail_user = sieve_tool_get_mail_user(sieve_tool);

	/* Open the source mailbox */

	src_mailbox = mailbox_name_to_mutf7(src_mailbox);
	ns = mail_namespace_find(mail_user->namespaces, src_mailbox);
	if ( ns == NULL )
		i_fatal("Unknown namespace for source mailbox '%s'", src_mailbox);

	if ( !source_write || !execute )
		open_flags |= MAILBOX_FLAG_READONLY;

	src_box = mailbox_alloc(ns->list, src_mailbox, open_flags);
	if ( mailbox_open(src_box) < 0 ) {
		i_fatal("Couldn't open source mailbox '%s': %s",
			src_mailbox, mailbox_get_last_error(src_box, &error));
	}

	/* Open move box if necessary */

	if ( execute && discard_action == SIEVE_FILTER_DACT_MOVE &&
		move_mailbox != NULL ) {
		move_mailbox = mailbox_name_to_mutf7(move_mailbox);
		ns = mail_namespace_find(mail_user->namespaces, move_mailbox);
		if ( ns == NULL )
			i_fatal("Unknown namespace for mailbox '%s'", move_mailbox);

		move_box = mailbox_alloc(ns->list, move_mailbox, open_flags);
		if ( mailbox_open(move_box) < 0 ) {
			i_fatal("Couldn't open mailbox '%s': %s",
				move_mailbox, mailbox_get_last_error(move_box, &error));
		}

		if ( mailbox_backends_equal(src_box, move_box) ) {
			i_fatal("Source mailbox and mailbox for move action are identical.");
		}
	}

	/* Compose script environment */
	memset(&scriptenv, 0, sizeof(scriptenv));
	scriptenv.mailbox_autocreate = FALSE;
	scriptenv.default_mailbox = dst_mailbox;
	scriptenv.user = mail_user;
	scriptenv.postmaster_address = "postmaster@example.com";
	scriptenv.smtp_open = NULL;
	scriptenv.smtp_close = NULL;

	/* Compose filter context */
	memset(&sfdata, 0, sizeof(sfdata));
	sfdata.senv = &scriptenv;
	sfdata.discard_action = discard_action;
	sfdata.move_mailbox = move_box;
	sfdata.main_sbin = main_sbin;
	sfdata.ehandler = ehandler;
	sfdata.execute = execute;
	sfdata.source_write = source_write;

	/* Apply Sieve filter to all messages found */
	(void) filter_mailbox(&sfdata, src_box);

	/* Close the source mailbox */
	if ( src_box != NULL )
		mailbox_free(&src_box);

	/* Close the move mailbox */
	if ( move_box != NULL )
		mailbox_free(&move_box);

	/* Cleanup error handler */
	sieve_error_handler_unref(&ehandler);

	sieve_tool_deinit(&sieve_tool);

	return 0;
}