summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/plugins/editheader/cmd-deleteheader.c
blob: a6964b780e39251f4531b3ab8ab6532f24560688 (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
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str-sanitize.h"
#include "mail-storage.h"

#include "rfc2822.h"
#include "edit-mail.h"

#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-message.h"
#include "sieve-comparators.h"
#include "sieve-match-types.h"

#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "sieve-match.h"

#include "ext-editheader-common.h"

/*
 * Deleteheader command
 *
 * Syntax:
 *   deleteheader [":index" <fieldno: number> [":last"]]
 *                [COMPARATOR] [MATCH-TYPE]
 *                <field-name: string> [<value-patterns: string-list>]
 */

static bool cmd_deleteheader_registered
	(struct sieve_validator *valdtr, const struct sieve_extension *ext,
		struct sieve_command_registration *cmd_reg);
static bool cmd_deleteheader_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd);
static bool cmd_deleteheader_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd);

const struct sieve_command_def deleteheader_command = {
	.identifier = "deleteheader",
	.type = SCT_COMMAND,
	.positional_args = -1, /* We check positional arguments ourselves */
	.subtests = 0,
	.block_allowed = FALSE,
	.block_required = FALSE,
	.registered = cmd_deleteheader_registered,
	.validate = cmd_deleteheader_validate,
	.generate = cmd_deleteheader_generate
};

/*
 * Deleteheader command tags
 */

/* Forward declarations */

static bool cmd_deleteheader_validate_index_tag
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);
static bool cmd_deleteheader_validate_last_tag
	(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
		struct sieve_command *cmd);

/* Argument objects */

static const struct sieve_argument_def deleteheader_index_tag = {
	.identifier = "index",
	.validate = cmd_deleteheader_validate_index_tag
};

static const struct sieve_argument_def deleteheader_last_tag = {
	.identifier = "last",
	.validate = cmd_deleteheader_validate_last_tag
};

/* Codes for optional arguments */

enum cmd_deleteheader_optional {
	OPT_INDEX = SIEVE_MATCH_OPT_LAST,
	OPT_LAST
};

/*
 * Deleteheader operation
 */

static bool cmd_deleteheader_operation_dump
	(const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int cmd_deleteheader_operation_execute
	(const struct sieve_runtime_env *renv, sieve_size_t *address);

const struct sieve_operation_def deleteheader_operation = {
	.mnemonic = "DELETEHEADER",
	.ext_def = &editheader_extension,
	.code = EXT_EDITHEADER_OPERATION_DELETEHEADER,
	.dump = cmd_deleteheader_operation_dump,
	.execute = cmd_deleteheader_operation_execute
};

/*
 * Command registration
 */

static bool cmd_deleteheader_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext ATTR_UNUSED,
	struct sieve_command_registration *cmd_reg)
{
	/* The order of these is not significant */
	sieve_comparators_link_tag(valdtr, cmd_reg, SIEVE_MATCH_OPT_COMPARATOR);
	sieve_match_types_link_tags(valdtr, cmd_reg, SIEVE_MATCH_OPT_MATCH_TYPE);

	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &deleteheader_index_tag, OPT_INDEX);
	sieve_validator_register_tag
		(valdtr, cmd_reg, ext, &deleteheader_last_tag, OPT_LAST);

	return TRUE;
}

/*
 * Command validation context
 */

struct cmd_deleteheader_context_data {
	struct sieve_ast_argument *arg_index;
	struct sieve_ast_argument *arg_last;
};

/*
 * Tag validation
 */

static struct cmd_deleteheader_context_data *
cmd_deleteheader_get_context
(struct sieve_command *cmd)
{
	struct cmd_deleteheader_context_data *ctx_data =
		(struct cmd_deleteheader_context_data *)cmd->data;

	if ( ctx_data != NULL ) return ctx_data;

	ctx_data = p_new
		(sieve_command_pool(cmd), struct cmd_deleteheader_context_data, 1);
	cmd->data = (void *)ctx_data;

	return ctx_data;
}

static bool cmd_deleteheader_validate_index_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct sieve_ast_argument *tag = *arg;
	struct cmd_deleteheader_context_data *ctx_data;
	sieve_number_t index;

	/* Detach the tag itself */
	*arg = sieve_ast_arguments_detach(*arg,1);

	/* Check syntax:
	 *   :index number
	 */
	if ( !sieve_validate_tag_parameter
		(valdtr, cmd, tag, *arg, NULL, 0, SAAT_NUMBER, FALSE) ) {
		return FALSE;
	}

	index = sieve_ast_argument_number(*arg);
	if ( index > INT_MAX ) {
		sieve_argument_validate_warning(valdtr, *arg,
			"the :%s tag for the %s %s has a parameter value '%llu' "
			"exceeding the maximum (%d)",
			sieve_argument_identifier(tag), sieve_command_identifier(cmd),
			sieve_command_type_name(cmd), (unsigned long long) index,
			INT_MAX);
		return FALSE;
	}

	ctx_data = cmd_deleteheader_get_context(cmd);
	ctx_data->arg_index = *arg;

	/* Skip parameter */
	*arg = sieve_ast_argument_next(*arg);

	return TRUE;
}

static bool cmd_deleteheader_validate_last_tag
(struct sieve_validator *valdtr ATTR_UNUSED, struct sieve_ast_argument **arg,
	struct sieve_command *cmd)
{
	struct cmd_deleteheader_context_data *ctx_data;

	ctx_data = cmd_deleteheader_get_context(cmd);
	ctx_data->arg_last = *arg;

	/* Skip parameter */
	*arg = sieve_ast_argument_next(*arg);

	return TRUE;
}

/*
 * Validation
 */

static bool cmd_deleteheader_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd)
{
	struct sieve_ast_argument *arg = cmd->first_positional;
	struct cmd_deleteheader_context_data *ctx_data =
		(struct cmd_deleteheader_context_data *)cmd->data;
	struct sieve_comparator cmp_default =
		SIEVE_COMPARATOR_DEFAULT(i_ascii_casemap_comparator);
	struct sieve_match_type mcht_default =
		SIEVE_MATCH_TYPE_DEFAULT(is_match_type);

	if ( ctx_data != NULL ) {
		if ( ctx_data->arg_last != NULL && ctx_data->arg_index == NULL ) {
			sieve_argument_validate_error(valdtr, ctx_data->arg_last,
				"the :last tag for the %s %s cannot be specified "
				"without the :index tag",
				sieve_command_identifier(cmd), sieve_command_type_name(cmd));
		}
	}

	/* Field name argument */

	if ( arg == NULL ) {
		sieve_command_validate_error(valdtr, cmd,
			"the %s %s expects at least one positional argument, but none was found",
			sieve_command_identifier(cmd), sieve_command_type_name(cmd));
		return FALSE;
	}

	if ( !sieve_validate_positional_argument
		(valdtr, cmd, arg, "field name", 1, SAAT_STRING_LIST) ) {
		return FALSE;
	}

	if ( !sieve_validator_argument_activate(valdtr, cmd, arg, FALSE) )
		return FALSE;

	if ( sieve_argument_is_string_literal(arg) ) {
		string_t *fname = sieve_ast_argument_str(arg);

		if ( !rfc2822_header_field_name_verify(str_c(fname), str_len(fname)) ) {
			sieve_argument_validate_error(valdtr, arg, "deleteheader command:"
				"specified field name `%s' is invalid",
				str_sanitize(str_c(fname), 80));
			return FALSE;
		}

		if ( !ext_editheader_header_allow_delete
			(cmd->ext, str_c(fname)) ) {
			sieve_argument_validate_warning
				(valdtr, arg, "deleteheader command: "
					"deleting specified header field `%s' is forbidden; "
					"modification will be denied",
					str_sanitize(str_c(fname), 80));
		}
	}

	/* Value patterns argument */

	arg = sieve_ast_argument_next(arg);
	if ( arg == NULL ) {
		/* There is none; let's not generate code for useless match arguments */
		sieve_match_type_arguments_remove(valdtr, cmd);

		return TRUE;
	}

	if ( !sieve_validate_positional_argument
		(valdtr, cmd, arg, "value patterns", 2, SAAT_STRING_LIST) ) {
		return FALSE;
	}

	if ( !sieve_validator_argument_activate(valdtr, cmd, arg, FALSE) )
		return FALSE;

	/* Validate the value patterns to a specified match type */
	return sieve_match_type_validate
		(valdtr, cmd, arg, &mcht_default, &cmp_default);
}

/*
 * Code generation
 */

static bool cmd_deleteheader_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
	sieve_operation_emit(cgenv->sblock, cmd->ext, &deleteheader_operation);

 	/* Generate arguments */
	if ( !sieve_generate_arguments(cgenv, cmd, NULL) )
		return FALSE;

	/* Emit a placeholder when the value-patterns argument is missing */
	if ( sieve_ast_argument_next(cmd->first_positional) == NULL )
		sieve_opr_omitted_emit(cgenv->sblock);

	return TRUE;
}

/*
 * Code dump
 */

static bool cmd_deleteheader_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	int opt_code = 0;

	sieve_code_dumpf(denv, "DELETEHEADER");
	sieve_code_descend(denv);

	/* Optional operands */
	for (;;) {
		int opt;

		if ( (opt=sieve_match_opr_optional_dump(denv, address, &opt_code)) < 0 )
			return FALSE;

		if ( opt == 0 ) break;

		switch ( opt_code ) {
		case OPT_INDEX:
			if ( !sieve_opr_number_dump(denv, address, "index") )
				return FALSE;
			break;
		case OPT_LAST:
			sieve_code_dumpf(denv, "last");
			break;
		default:
			return FALSE;
		}
	};

	if ( !sieve_opr_string_dump(denv, address, "field name") )
		return FALSE;

	return sieve_opr_stringlist_dump_ex(denv, address, "value patterns", "");
}

/*
 * Code execution
 */

static int cmd_deleteheader_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address)
{
	const struct sieve_extension *this_ext = renv->oprtn->ext;
	int opt_code = 0;
	struct sieve_comparator cmp =
		SIEVE_COMPARATOR_DEFAULT(i_ascii_casemap_comparator);
	struct sieve_match_type mcht =
		SIEVE_MATCH_TYPE_DEFAULT(is_match_type);
	string_t *field_name;
	struct sieve_stringlist *vpattern_list = NULL;
	struct edit_mail *edmail;
	sieve_number_t index_offset = 0;
	bool index_last = FALSE;
	bool trace = FALSE;
	int ret;

	/*
	 * Read operands
	 */

	for (;;) {
		int opt;

		if ( (opt=sieve_match_opr_optional_read
			(renv, address, &opt_code, &ret, &cmp, &mcht)) < 0 )
			return ret;

		if ( opt == 0 ) break;

		switch ( opt_code ) {
		case OPT_INDEX:
			if ( (ret=sieve_opr_number_read(renv, address, "index", &index_offset))
				<= 0 )
				return ret;

			if ( index_offset > INT_MAX ) {
				sieve_runtime_trace_error(renv, "index is > %d", INT_MAX);
				return SIEVE_EXEC_BIN_CORRUPT;
			}

			break;
		case OPT_LAST:
			index_last = TRUE;
			break;
		default:
			sieve_runtime_trace_error(renv, "unknown optional operand");
			return SIEVE_EXEC_BIN_CORRUPT;
		}
	}

	/* Read field-name */
	if ( (ret=sieve_opr_string_read(renv, address, "field-name", &field_name))
		<= 0 )
		return ret;

	/* Read value-patterns */
	if ( (ret=sieve_opr_stringlist_read_ex
		(renv, address, "value-patterns", TRUE, &vpattern_list)) <= 0 )
		return ret;

	/*
	 * Verify arguments
	 */

	if ( !rfc2822_header_field_name_verify
		(str_c(field_name), str_len(field_name)) ) {
		sieve_runtime_error(renv, NULL, "deleteheader action: "
			"specified field name `%s' is invalid",
			str_sanitize(str_c(field_name), 80));
		return SIEVE_EXEC_FAILURE;
	}

	if ( !ext_editheader_header_allow_delete
		(this_ext, str_c(field_name)) ) {
		sieve_runtime_warning(renv, NULL, "deleteheader action: "
			"deleting specified header field `%s' is forbidden; "
			"modification denied",
			str_sanitize(str_c(field_name), 80));
		return SIEVE_EXEC_OK;
	}

	/*
	 * Execute command
	 */

	sieve_runtime_trace(renv, SIEVE_TRLVL_COMMANDS, "deleteheader command");

	/* Start editing the mail */
	edmail = sieve_message_edit(renv->msgctx);

	trace = sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS);

	/* Either do string matching or just kill all/indexed notify action(s) */
	if ( vpattern_list != NULL ) {
		struct edit_mail_header_iter *edhiter;
		struct sieve_match_context *mctx;

		if ( trace ) {
			sieve_runtime_trace_descend(renv);
			if ( index_offset != 0 ) {
				sieve_runtime_trace(renv, 0,
					"deleting matching occurrences of header `%s' at index %llu%s",
					str_c(field_name), (unsigned long long)index_offset,
					( index_last ? " from last": ""));
			} else {
				sieve_runtime_trace(renv, 0,
					"deleting matching occurrences of header `%s'", str_c(field_name));
			}
		}

		/* Iterate through all headers and delete those that match */
		if ( (ret=edit_mail_headers_iterate_init
			(edmail, str_c(field_name), index_last, &edhiter)) > 0 )
		{
			int mret = 0;
			sieve_number_t pos = 0;

			/* Initialize match */
			mctx = sieve_match_begin(renv, &mcht, &cmp);

			/* Match */
			for (;;) {
				pos++;

				/* Check index if any */
				if ( index_offset == 0 || pos == index_offset ) {
					const char *value;
					int match;

					/* Match value against all value patterns */
					edit_mail_headers_iterate_get(edhiter, &value);
					if ( (match=sieve_match_value
						(mctx, value, strlen(value), vpattern_list)) < 0 )
						break;

					if ( match > 0 ) {
						/* Remove it and iterate to next */
						sieve_runtime_trace(renv, 0, "deleting header with value `%s'",
							value);

						if ( !edit_mail_headers_iterate_remove(edhiter) ) break;
						continue;
					}
				}

				if ( !edit_mail_headers_iterate_next(edhiter) )
					break;
			}

			/* Finish match */
			mret = sieve_match_end(&mctx, &ret);

			edit_mail_headers_iterate_deinit(&edhiter);

			if ( mret < 0 )
				return ret;
		}

		if ( ret == 0 ) {
			sieve_runtime_trace(renv, 0, "header `%s' not found", str_c(field_name));
		} else if ( ret < 0 ) {
			sieve_runtime_warning(renv, NULL, "deleteheader action: "
				"failed to delete occurrences of header `%s' (this should not happen!)",
				str_c(field_name));
		}

	} else {
		int index = ( index_last ? -((int)index_offset) : ((int)index_offset) );

		if ( trace ) {
			sieve_runtime_trace_descend(renv);
			if ( index_offset != 0 ) {
				sieve_runtime_trace(renv, 0, "deleting header `%s' at index %llu%s",
					str_c(field_name), (unsigned long long)index_offset,
					( index_last ? " from last": ""));
			} else {
				sieve_runtime_trace(renv, 0, "deleting header `%s'", str_c(field_name));
			}
		}

		/* Delete all occurrences of header */
		ret = edit_mail_header_delete(edmail, str_c(field_name), index);

		if ( ret < 0 ) {
			sieve_runtime_warning(renv, NULL, "deleteheader action: "
				"failed to delete occurrences of header `%s' (this should not happen!)",
				str_c(field_name));
		} else if ( trace ) {
			sieve_runtime_trace(renv, 0, "deleted %d occurrences of header `%s'",
				ret, str_c(field_name));
		}

	}

	return SIEVE_EXEC_OK;
}