File: cmd-filter-sieve.c

package info (click to toggle)
dovecot 1:2.3.4.1-5+deb10u1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 46,880 kB
  • sloc: ansic: 499,137; makefile: 7,352; sh: 5,592; cpp: 1,555; perl: 303; python: 73; xml: 44; pascal: 27
file content (405 lines) | stat: -rw-r--r-- 10,510 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
/* Copyright (c) 2017-2018 Pigeonhole authors, see the included COPYING file */

#include "imap-common.h"
#include "str.h"
#include "istream.h"
#include "istream-seekable.h"
#include "ostream.h"
#include "imap-commands.h"

#include "imap-filter.h"
#include "imap-filter-sieve.h"

#define FILTER_MAX_INMEM_SIZE (1024*128)

static int
cmd_filter_sieve_compile_script(struct imap_filter_context *ctx)
{
	struct client_command_context *cmd = ctx->cmd;
	struct imap_filter_sieve_context *sctx = ctx->sieve;
	struct client *client = cmd->client;
	string_t *errors = NULL;
	bool have_warnings = FALSE;
	int ret = 0;

	ret = imap_filter_sieve_compile(sctx, &errors, &have_warnings);
	if (ret >= 0 && !have_warnings)
		return 0;
		
	o_stream_nsend_str(client->output,
		t_strdup_printf("* FILTER (TAG %s) "
				"%s {%"PRIuSIZE_T"}\r\n",
				(ret < 0 ? "ERRORS" : "WARNINGS"),
				cmd->tag, str_len(errors)));
	o_stream_nsend(client->output,
		       str_data(errors), str_len(errors));
	o_stream_nsend_str(client->output, "\r\n");
	
	if (ret < 0) {
		ctx->compile_failure = TRUE;
		ctx->failed = TRUE;
		return -1;
	}
	return 0;
}

static bool
cmd_filter_sieve_delivery(struct client_command_context *cmd)
{
	struct imap_filter_context *ctx = cmd->context;
	struct client *client = cmd->client;
	struct imap_filter_sieve_context *sctx = ctx->sieve;
	enum mail_error error;
	const char *error_string;
	int ret;

	if (cmd->cancel) {
		imap_filter_deinit(ctx);
		return TRUE;
	}

	i_assert(sctx->filter_type == IMAP_FILTER_SIEVE_TYPE_DELIVERY);
	ret = imap_filter_sieve_open_personal(sctx, NULL,
					      &error, &error_string);
	if (ret < 0) {
		client_send_tagline(cmd,
			imap_get_error_string(cmd, error_string, error));
		imap_filter_deinit(ctx);
		return TRUE;
	}
	if (cmd_filter_sieve_compile_script(ctx) < 0) {
		client_send_tagline(cmd, "NO Failed to compile Sieve script");
		client->input_skip_line = TRUE;
		imap_filter_deinit(ctx);
		return TRUE;
	}

	imap_parser_reset(ctx->parser);
	cmd->func = imap_filter_search;
	return imap_filter_search(cmd);
}

static int
cmd_filter_sieve_script_parse_name_arg(struct imap_filter_context *ctx)
{
	struct client_command_context *cmd = ctx->cmd;
	const struct imap_arg *args;
	const char *error;
	enum imap_parser_error parse_error;
	int ret;

	ret = imap_parser_read_args(ctx->parser, 1, 0, &args);
	if (ret < 0) {
		if (ret == -2)
			return 0;
		error = imap_parser_get_error(ctx->parser, &parse_error);
		switch (parse_error) {
		case IMAP_PARSE_ERROR_NONE:
			i_unreached();
		case IMAP_PARSE_ERROR_LITERAL_TOO_BIG:
			client_disconnect_with_error(ctx->cmd->client, error);
			break;
		default:
			client_send_command_error(ctx->cmd, error);
			break;
		}
		return -1;
	}

	switch (args[0].type) {
	case IMAP_ARG_EOL:
		client_send_command_error(ctx->cmd, "Script name missing");
		return -1;
	case IMAP_ARG_LIST:
		client_send_command_error(ctx->cmd,
			  "Script name must be a string");
		return -1;
	case IMAP_ARG_NIL:
	case IMAP_ARG_ATOM:
	case IMAP_ARG_STRING:
		/* we have the value already */
		if (ctx->failed)
			return 1;
		ctx->script_name = p_strdup(cmd->pool,
					    imap_arg_as_nstring(&args[0]));
		break;
	case IMAP_ARG_LITERAL:
	case IMAP_ARG_LITERAL_SIZE:
	case IMAP_ARG_LITERAL_SIZE_NONSYNC:
		i_unreached();
	}
	return 1;
}

static bool
cmd_filter_sieve_script_parse_name(struct client_command_context *cmd)
{
	struct imap_filter_context *ctx = cmd->context;
	struct client *client = cmd->client;
	struct imap_filter_sieve_context *sctx = ctx->sieve;
	enum mail_error error;
	const char *error_string;
	int ret;

	if (cmd->cancel) {
		imap_filter_deinit(ctx);
		return TRUE;
	}

	if ((ret=cmd_filter_sieve_script_parse_name_arg(ctx)) == 0)
		return FALSE;
	if (ret < 0) {
		/* already sent the error to client */ ;
		imap_filter_deinit(ctx);
		return TRUE;
	}

	switch (sctx->filter_type) {
	case IMAP_FILTER_SIEVE_TYPE_PERSONAL:
		ret = imap_filter_sieve_open_personal(sctx, ctx->script_name,
						      &error, &error_string);
		break;
	case IMAP_FILTER_SIEVE_TYPE_GLOBAL:
		ret = imap_filter_sieve_open_global(sctx, ctx->script_name,
						    &error, &error_string);
		break;
	case IMAP_FILTER_SIEVE_TYPE_DELIVERY:
	case IMAP_FILTER_SIEVE_TYPE_SCRIPT:
		i_unreached();
	}
	if (ret < 0) {
		client_send_tagline(cmd,
			imap_get_error_string(cmd, error_string, error));
		imap_filter_deinit(ctx);
		return TRUE;
	}
	if (cmd_filter_sieve_compile_script(ctx) < 0) {
		client_send_tagline(cmd, "NO Failed to compile Sieve script");
		client->input_skip_line = TRUE;
		imap_filter_deinit(ctx);
		return TRUE;
	}

	imap_parser_reset(ctx->parser);
	cmd->func = imap_filter_search;
	return imap_filter_search(cmd);
}

static void
cmd_filter_sieve_compile_input(struct imap_filter_context *ctx,
				struct istream *input)
{
	struct imap_filter_sieve_context *sctx = ctx->sieve;

	imap_filter_sieve_open_input(sctx, input);
	(void)cmd_filter_sieve_compile_script(ctx);
}

static int
cmd_filter_sieve_script_read_stream(struct imap_filter_context *ctx)
{
	struct istream *input = ctx->script_input;
	const unsigned char *data;
	size_t size;
	int ret;

	while ((ret = i_stream_read_more(input, &data, &size)) > 0)
		i_stream_skip(input, size);
	if (input->v_offset == ctx->script_len) {
		/* finished reading the value */
		i_stream_seek(input, 0);

		if (ctx->failed) {
			i_stream_unref(&ctx->script_input);
			return 1;
		}

		cmd_filter_sieve_compile_input(ctx, ctx->script_input);
		i_stream_unref(&ctx->script_input);
		return 1;
	}
	if (input->eof) {
		/* client disconnected */
		return -1;
	}
	return 0;
}

static int
cmd_filter_sieve_script_parse_value_arg(struct imap_filter_context *ctx)
{
	const struct imap_arg *args;
	const char *value, *error;
	enum imap_parser_error parse_error;
	struct istream *input, *inputs[2];
	string_t *path;
	int ret;

	ret = imap_parser_read_args(ctx->parser, 1,
				    IMAP_PARSE_FLAG_LITERAL_SIZE |
				    IMAP_PARSE_FLAG_LITERAL8, &args);
	if (ret < 0) {
		if (ret == -2)
			return 0;
		error = imap_parser_get_error(ctx->parser, &parse_error);
		switch (parse_error) {
		case IMAP_PARSE_ERROR_NONE:
			i_unreached();
		case IMAP_PARSE_ERROR_LITERAL_TOO_BIG:
			client_disconnect_with_error(ctx->cmd->client, error);
			break;
		default:
			client_send_command_error(ctx->cmd, error);
			break;
		}
		return -1;
	}

	switch (args[0].type) {
	case IMAP_ARG_EOL:
		client_send_command_error(ctx->cmd, "Script value missing");
		return -1;
	case IMAP_ARG_NIL:
	case IMAP_ARG_ATOM:
	case IMAP_ARG_LIST:
		client_send_command_error(ctx->cmd,
			  "Script value must be a string");
		return -1;
	case IMAP_ARG_STRING:
		/* we have the value already */
		if (ctx->failed)
			return 1;
		value = imap_arg_as_nstring(&args[0]);
		input = i_stream_create_from_data(value, strlen(value));
		cmd_filter_sieve_compile_input(ctx, input);
		i_stream_unref(&input);
		return 1;
	case IMAP_ARG_LITERAL_SIZE:
		o_stream_nsend(ctx->cmd->client->output, "+ OK\r\n", 6);
		o_stream_uncork(ctx->cmd->client->output);
		o_stream_cork(ctx->cmd->client->output);
		/* fall through */
	case IMAP_ARG_LITERAL_SIZE_NONSYNC:
		ctx->script_len = imap_arg_as_literal_size(&args[0]);

		inputs[0] = i_stream_create_limit(ctx->cmd->client->input,
						  ctx->script_len);
		inputs[1] = NULL;

		path = t_str_new(128);
		mail_user_set_get_temp_prefix(path,
					      ctx->cmd->client->user->set);
		ctx->script_input = i_stream_create_seekable_path(
			inputs, FILTER_MAX_INMEM_SIZE, str_c(path));
		i_stream_set_name(ctx->script_input,
				  i_stream_get_name(inputs[0]));
		i_stream_unref(&inputs[0]);
		break;
	case IMAP_ARG_LITERAL:
		i_unreached();
	}
	return cmd_filter_sieve_script_read_stream(ctx);
}

static bool
cmd_filter_sieve_script_parse_value(struct client_command_context *cmd)
{
	struct imap_filter_context *ctx = cmd->context;
	struct client *client = cmd->client;
	int ret;

	if (cmd->cancel) {
		imap_filter_deinit(ctx);
		return TRUE;
	}

	if (ctx->script_input != NULL) {
		if ((ret=cmd_filter_sieve_script_read_stream(ctx)) == 0)
			return FALSE;
	} else {
		if ((ret=cmd_filter_sieve_script_parse_value_arg(ctx)) == 0)
			return FALSE;
	}

	if (ret < 0) {
		/* already sent the error to client */ ;
		imap_filter_deinit(ctx);
		return TRUE;
	} else if (ctx->compile_failure) {
		client_send_tagline(cmd, "NO Failed to compile Sieve script");
		client->input_skip_line = TRUE;
		imap_filter_deinit(ctx);
		return TRUE;
	}

	imap_parser_reset(ctx->parser);
	cmd->func = imap_filter_search;
	return imap_filter_search(cmd);
}

bool cmd_filter_sieve(struct client_command_context *cmd)
{
	struct imap_filter_context *ctx = cmd->context;
	struct client *client = cmd->client;
	enum imap_filter_sieve_type type;
	const struct imap_arg *args;
	const char *sieve_type;

	if (!client_read_args(cmd, 2, 0, &args))
		return FALSE;
	args++;

	/* sieve-type */
	if (IMAP_ARG_IS_EOL(args)) {
		client_send_command_error(cmd,
			"Missing SIEVE filter sub-type.");
		return TRUE;
	}
	if (!imap_arg_get_atom(args, &sieve_type)) {
		client_send_command_error(cmd,
			"SIEVE filter sub-type is not an atom.");
		return TRUE;
	}
	if (strcasecmp(sieve_type, "DELIVERY") == 0) {
		type = IMAP_FILTER_SIEVE_TYPE_DELIVERY;
	} else if (strcasecmp(sieve_type, "PERSONAL") == 0) {
		type = IMAP_FILTER_SIEVE_TYPE_PERSONAL;
	} else if (strcasecmp(sieve_type, "GLOBAL") == 0) {
		type = IMAP_FILTER_SIEVE_TYPE_GLOBAL;
	} else if (strcasecmp(sieve_type, "SCRIPT") == 0) {
		type = IMAP_FILTER_SIEVE_TYPE_SCRIPT;
	} else {
		client_send_command_error(cmd, t_strdup_printf(
			"Unknown SIEVE filter sub-type `%s'",
			sieve_type));
		return TRUE;
	}

	ctx->sieve = imap_filter_sieve_context_create(ctx, type);

	/* we support large scripts, so read the values from client
	   asynchronously the same way as APPEND does. */
	client->input_lock = cmd;
	ctx->parser = imap_parser_create(client->input, client->output,
					 client->set->imap_max_line_length);
	if (client->set->imap_literal_minus)
		imap_parser_enable_literal_minus(ctx->parser);
	o_stream_unset_flush_callback(client->output);

	switch (type) {
	case IMAP_FILTER_SIEVE_TYPE_DELIVERY:
		cmd->func = cmd_filter_sieve_delivery;
		break;
	case IMAP_FILTER_SIEVE_TYPE_PERSONAL:
		cmd->func = cmd_filter_sieve_script_parse_name;
		break;
	case IMAP_FILTER_SIEVE_TYPE_GLOBAL:
		cmd->func = cmd_filter_sieve_script_parse_name;
		break;
	case IMAP_FILTER_SIEVE_TYPE_SCRIPT:
		cmd->func = cmd_filter_sieve_script_parse_value;
		break;
	}
	cmd->context = ctx;
	return cmd->func(cmd);
}