summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/sieve-tools/sieve-test.c
blob: e7345be886393d84ac0e139c4ca2a569e86523f6 (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
/* Copyright (c) 2002-2018 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 "ostream.h"
#include "array.h"
#include "mail-namespace.h"
#include "mail-storage.h"
#include "master-service.h"
#include "master-service-settings.h"
#include "mail-storage-service.h"

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

#include "sieve-tool.h"

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


/*
 * Configuration
 */

#define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail"

/*
 * Print help
 */

static void print_help(void)
{
	printf(
"Usage: sieve-test [-a <orig-recipient-address] [-c <config-file>]\n"
"                  [-C] [-D] [-d <dump-filename>] [-e]\n"
"                  [-f <envelope-sender>] [-l <mail-location>]\n"
"                  [-m <default-mailbox>] [-P <plugin>]\n"
"                  [-r <recipient-address>] [-s <script-file>]\n"
"                  [-t <trace-file>] [-T <trace-option>] [-x <extensions>]\n"
"                  <script-file> <mail-file>\n"
	);
}

/*
 * Dummy SMTP session
 */

static void *
sieve_smtp_start(const struct sieve_script_env *senv ATTR_UNUSED,
		 const struct smtp_address *mail_from)
{
	struct ostream *output;

	i_info("sending message from <%s>:", smtp_address_encode(mail_from));

	output = o_stream_create_fd(STDOUT_FILENO, (size_t)-1);
	o_stream_set_no_error_handling(output, TRUE);
	return (void*)output;
}

static void
sieve_smtp_add_rcpt(const struct sieve_script_env *senv ATTR_UNUSED,
		    void *handle ATTR_UNUSED,
		    const struct smtp_address *rcpt_to)
{
	printf("\nRECIPIENT: %s\n", smtp_address_encode(rcpt_to));
}

static struct ostream *
sieve_smtp_send(const struct sieve_script_env *senv ATTR_UNUSED, void *handle)
{
	printf("START MESSAGE:\n");

	return (struct ostream *)handle;
}

static void
sieve_smtp_abort(const struct sieve_script_env *senv ATTR_UNUSED, void *handle)
{
	struct ostream *output = (struct ostream *)handle;

	printf("#### ABORT MESSAGE ####\n\n");
	o_stream_unref(&output);
}

static int
sieve_smtp_finish(const struct sieve_script_env *senv ATTR_UNUSED, void *handle,
		  const char **error_r ATTR_UNUSED)
{
	struct ostream *output = (struct ostream *)handle;

	printf("END MESSAGE\n\n");
	o_stream_unref(&output);
	return 1;
}

/*
 * Dummy duplicate check implementation
 */

static void *
duplicate_transaction_begin(const struct sieve_script_env *senv ATTR_UNUSED)
{
	return NULL;
}

static void duplicate_transaction_commit(void **_dup_trans ATTR_UNUSED)
{
}

static void duplicate_transaction_rollback(void **_dup_trans ATTR_UNUSED)
{
}

static int
duplicate_check(void *_dup_trans ATTR_UNUSED,
		const struct sieve_script_env *senv,
		const void *id ATTR_UNUSED, size_t id_size ATTR_UNUSED)
{
	i_info("checked duplicate for user %s.\n", senv->user->username);
	return 0;
}

static void
duplicate_mark(void *_dup_trans ATTR_UNUSED,
	       const struct sieve_script_env *senv,
	       const void *id ATTR_UNUSED, size_t id_size ATTR_UNUSED,
	       time_t time ATTR_UNUSED)
{
	i_info("marked duplicate for user %s.\n", senv->user->username);
}

/*
 * Result logging
 */

static const char *
result_amend_log_message(const struct sieve_script_env *senv,
			 enum log_type log_type, const char *message)
{
	const struct sieve_message_data *msgdata = senv->script_context;
	string_t *str;

	if (log_type == LOG_TYPE_DEBUG)
		return message;

	str = t_str_new(256);
	str_printfa(str, "msgid=%s", (msgdata->id == NULL ?
				      "unspecified" : msgdata->id));
	str_append(str, ": ");
	str_append(str, message);

	return str_c(str);
}

/*
 * Tool implementation
 */

int main(int argc, char **argv)
{
	struct sieve_instance *svinst;
	ARRAY_TYPE (const_string) scriptfiles;
	const char *scriptfile, *mailbox, *dumpfile, *tracefile, *mailfile,
		*mailloc, *errstr;
	struct smtp_address *rcpt_to, *final_rcpt_to, *mail_from;
	struct sieve_trace_config trace_config;
	struct mail *mail;
	struct sieve_binary *main_sbin, *sbin = NULL;
	struct sieve_message_data msgdata;
	struct sieve_script_env scriptenv;
	struct sieve_exec_status estatus;
	enum sieve_execute_flags exflags = SIEVE_EXECUTE_FLAG_LOG_RESULT;
	struct sieve_error_handler *ehandler;
	struct ostream *teststream = NULL;
	struct sieve_trace_log *trace_log = NULL;
	bool force_compile = FALSE, execute = FALSE;
	int exit_status = EXIT_SUCCESS;
	int ret, c;

	sieve_tool = sieve_tool_init("sieve-test", &argc, &argv,
				     "r:a:f:m:d:l:s:eCt:T:DP:x:u:", FALSE);

	ehandler = NULL;
	t_array_init(&scriptfiles, 16);

	/* Parse arguments */
	mailbox = dumpfile = tracefile = mailloc = NULL;
	mail_from = final_rcpt_to = rcpt_to = NULL;
	i_zero(&trace_config);
	trace_config.level = SIEVE_TRLVL_ACTIONS;
	while ((c = sieve_tool_getopt(sieve_tool)) > 0) {
		switch (c) {
		case 'r':
			/* final recipient address */
			if (smtp_address_parse_mailbox(
				pool_datastack_create(), optarg,
				SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
				&final_rcpt_to, &errstr) < 0)
				i_fatal("Invalid -r parameter: %s", errstr);
			break;
		case 'a':
			/* original recipient address */
			if (smtp_address_parse_mailbox(
				pool_datastack_create(), optarg,
				SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
				&rcpt_to, &errstr) < 0)
				i_fatal("Invalid -a parameter: %s", errstr);
			break;
		case 'f':
			/* envelope sender address */
			if (smtp_address_parse_mailbox(
				pool_datastack_create(), optarg,
				0, &mail_from, &errstr) < 0)
				i_fatal("Invalid -f parameter: %s", errstr);
			break;
		case 'm':
			/* default mailbox (keep box) */
			mailbox = optarg;
			break;
		case 'l':
			/* mail location */
			mailloc = optarg;
			break;
		case 't':
			/* trace file */
			tracefile = optarg;
			break;
			/* trace options */
		case 'T':
			sieve_tool_parse_trace_option(&trace_config, optarg);
			break;
		case 'd':
			/* dump file */
			dumpfile = optarg;
			break;
		case 's':
			/* scriptfile executed before main script */
			{
				const char *file;

				file = t_strdup(optarg);
				array_append(&scriptfiles, &file, 1);
			}
			break;
			/* execution mode */
		case 'e':
			execute = TRUE;
			break;
			/* force script compile */
		case 'C':
			force_compile = TRUE;
			break;
		default:
			/* unrecognized option */
			print_help();
			i_fatal_status(EX_USAGE, "Unknown argument: %c", c);
			break;
		}
	}

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

	if (optind < argc)
		mailfile = argv[optind++];
	else {
		print_help();
		i_fatal_status(EX_USAGE, "Missing <mail-file> argument");
	}

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

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

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

	/* Create error handler */
	ehandler = sieve_stderr_ehandler_create(svinst, 0);
	sieve_error_handler_accept_infolog(ehandler, TRUE);
	sieve_error_handler_accept_debuglog(ehandler, svinst->debug);

	/* 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);
	}

	if (main_sbin == NULL) {
		exit_status = EXIT_FAILURE;
	} else {
		/* Dump script */
		sieve_tool_dump_binary_to(main_sbin, dumpfile, FALSE);

		/* Obtain mail namespaces from -l argument */
		if (mailloc != NULL)
			sieve_tool_init_mail_user(sieve_tool, mailloc);

		/* Initialize raw mail object */
		mail = sieve_tool_open_file_as_mail(sieve_tool, mailfile);

		if (mailbox == NULL)
			mailbox = "INBOX";

		/* Collect necessary message data */
		i_zero(&msgdata);
		msgdata.mail = mail;
		msgdata.auth_user = sieve_tool_get_username(sieve_tool);
		(void)mail_get_message_id(mail, &msgdata.id);

		sieve_tool_get_envelope_data(&msgdata, mail,
					     mail_from, rcpt_to, final_rcpt_to);

		/* Create streams for test and trace output */

		if (!execute) {
			teststream = o_stream_create_fd(1, 0);
			o_stream_set_no_error_handling(teststream, TRUE);
		}

		if (tracefile != NULL) {
			(void)sieve_trace_log_create(
				svinst, (strcmp(tracefile, "-") == 0 ?
					 NULL : tracefile), &trace_log);
		}

		/* Compose script environment */
		if (sieve_script_env_init(
			&scriptenv, sieve_tool_get_mail_user(sieve_tool),
			&errstr) < 0) {
			i_fatal("Failed to initialize script execution: %s",
				errstr);
		}

		scriptenv.default_mailbox = mailbox;
		scriptenv.smtp_start = sieve_smtp_start;
		scriptenv.smtp_add_rcpt = sieve_smtp_add_rcpt;
		scriptenv.smtp_send = sieve_smtp_send;
		scriptenv.smtp_abort = sieve_smtp_abort;
		scriptenv.smtp_finish = sieve_smtp_finish;
		scriptenv.duplicate_transaction_begin =
			duplicate_transaction_begin;
		scriptenv.duplicate_transaction_commit =
			duplicate_transaction_commit;
		scriptenv.duplicate_transaction_rollback =
			duplicate_transaction_rollback;
		scriptenv.duplicate_mark = duplicate_mark;
		scriptenv.duplicate_check = duplicate_check;
		scriptenv.result_amend_log_message = result_amend_log_message;
		scriptenv.trace_log = trace_log;
		scriptenv.trace_config = trace_config;
		scriptenv.script_context = &msgdata;

		i_zero(&estatus);
		scriptenv.exec_status = &estatus;

		/* Run the test */
		ret = 1;
		if (array_count(&scriptfiles) == 0) {
			/* Single script */
			sbin = main_sbin;
			main_sbin = NULL;

			/* Execute/Test script */
			if (execute) {
				ret = sieve_execute(sbin, &msgdata, &scriptenv,
						    ehandler, ehandler,
						    exflags);
			} else {
				ret = sieve_test(sbin, &msgdata, &scriptenv,
						 ehandler, teststream, exflags);
			}
		} else {
			/* Multiple scripts */
			const char *const *sfiles;
			unsigned int i, count;
			struct sieve_multiscript *mscript;
			bool more = TRUE;

			if (execute)
				mscript = sieve_multiscript_start_execute(
					svinst, &msgdata, &scriptenv);
			else
				mscript = sieve_multiscript_start_test(
					svinst, &msgdata, &scriptenv,
					teststream);

			/* Execute scripts sequentially */
			sfiles = array_get(&scriptfiles, &count);
			for (i = 0; i < count && more; i++) {
				if (teststream != NULL) {
					o_stream_nsend_str(
						teststream,
						t_strdup_printf("\n## Executing script: %s\n",
								sfiles[i]));
				}

				/* Close previous script */
				if (sbin != NULL)
					sieve_close(&sbin);

				/* Compile sieve script */
				if (force_compile) {
					sbin = sieve_tool_script_compile(
						svinst, sfiles[i], sfiles[i]);
					if (sbin != NULL)
						(void)sieve_save(sbin, FALSE, NULL);
				} else {
					sbin = sieve_tool_script_open(svinst, sfiles[i]);
				}

				if (sbin == NULL) {
					ret = SIEVE_EXEC_FAILURE;
					break;
				}

				/* Execute/Test script */
				more = sieve_multiscript_run(
					mscript, sbin, ehandler, ehandler,
					exflags);
			}

			/* Execute/Test main script */
			if (more && ret > 0) {
				if (teststream != NULL) {
					o_stream_nsend_str(
						teststream,
						t_strdup_printf("## Executing script: %s\n",
								scriptfile));
				}

				/* Close previous script */
				if (sbin != NULL)
					sieve_close(&sbin);

				sbin = main_sbin;
				main_sbin = NULL;

				(void)sieve_multiscript_run(
					mscript, sbin, ehandler, ehandler,
					exflags);
			}

			ret = sieve_multiscript_finish(&mscript, ehandler,
						       exflags, ret);
		}

		/* Run */
		switch (ret) {
		case SIEVE_EXEC_OK:
			i_info("final result: success");
			break;
		case SIEVE_EXEC_RESOURCE_LIMIT:
			i_info("resource limit exceeded");
			exit_status = EXIT_FAILURE;
			break;
		case SIEVE_EXEC_BIN_CORRUPT:
			i_info("corrupt binary deleted.");
			i_unlink_if_exists(sieve_binary_path(sbin));
			/* fall through */
		case SIEVE_EXEC_FAILURE:
			i_info("final result: failed; "
			       "resolved with successful implicit keep");
			exit_status = EXIT_FAILURE;
			break;
		case SIEVE_EXEC_TEMP_FAILURE:
			i_info("final result: temporary failure");
			exit_status = EXIT_FAILURE;
			break;
		case SIEVE_EXEC_KEEP_FAILED:
			i_info("final result: utter failure");
			exit_status = EXIT_FAILURE;
			break;
		}

		if (teststream != NULL)
			o_stream_destroy(&teststream);
		if (trace_log != NULL)
			sieve_trace_log_free(&trace_log);

		/* Cleanup remaining binaries */
		if (sbin != NULL)
			sieve_close(&sbin);
		if (main_sbin != NULL)
			sieve_close(&main_sbin);
	}

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

	sieve_tool_deinit(&sieve_tool);

	return exit_status;
}