~ubuntu-dev/ubuntu/lucid/dovecot/lucid-201002101901

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
/* Copyright (c) 2002-2009 Dovecot Sieve authors, see the included COPYING file
 */

#include "lib.h"
#include "str-sanitize.h"
#include "message-date.h"

#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-comparators.h"
#include "sieve-match-types.h"
#include "sieve-address-parts.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "sieve-match.h"

#include "ext-date-common.h"

#include <time.h>

/*
 * Tests
 */

static bool tst_date_validate
	(struct sieve_validator *valdtr, struct sieve_command_context *tst);
static bool tst_date_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_command_context *ctx);
 
/* Address test
 *
 * Syntax:
 *    date [<":zone" <time-zone: string>> / ":originalzone"]
 *         [COMPARATOR] [MATCH-TYPE] <header-name: string>
 *         <date-part: string> <key-list: string-list>
 */

static bool tst_date_registered
	(struct sieve_validator *valdtr, struct sieve_command_registration *cmd_reg);

const struct sieve_command date_test = { 
	"date", 
	SCT_TEST, 
	3, 0, FALSE, FALSE,
	tst_date_registered,
	NULL, 
	tst_date_validate, 
	tst_date_generate, 
	NULL 
};

/* Currentdate test
 * 
 * Syntax:
 *    currentdate [":zone" <time-zone: string>]
 *                [COMPARATOR] [MATCH-TYPE]
 *                <date-part: string> <key-list: string-list>
 */

static bool tst_currentdate_registered
	(struct sieve_validator *valdtr, struct sieve_command_registration *cmd_reg);

const struct sieve_command currentdate_test = { 
	"currentdate", 
	SCT_TEST, 
	2, 0, FALSE, FALSE,
	tst_currentdate_registered,
	NULL, 
	tst_date_validate, 
	tst_date_generate, 
	NULL 
};

/* 
 * Tagged arguments 
 */

/* Forward declarations */

static bool tag_zone_validate
	(struct sieve_validator *validator, struct sieve_ast_argument **arg,
		struct sieve_command_context *cmd);
static bool tag_zone_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
		struct sieve_command_context *cmd);

/* Argument objects */

static const struct sieve_argument date_zone_tag = {
 	"zone",
	NULL, NULL,
	tag_zone_validate,
	NULL,
	tag_zone_generate
};

static const struct sieve_argument date_originalzone_tag = {
	"originalzone",
	NULL, NULL,
	tag_zone_validate,
	NULL,
	tag_zone_generate
};

/* 
 * Address operation 
 */

static bool tst_date_operation_dump
	(const struct sieve_operation *op, 
		const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int tst_date_operation_execute
	(const struct sieve_operation *op, 
		const struct sieve_runtime_env *renv, sieve_size_t *address);

const struct sieve_operation date_operation = { 
	"DATE",
	&date_extension,
	EXT_DATE_OPERATION_DATE,
	tst_date_operation_dump, 
	tst_date_operation_execute 
};

const struct sieve_operation currentdate_operation = { 
	"CURRENTDATE",
	&date_extension,
	EXT_DATE_OPERATION_CURRENTDATE,
	tst_date_operation_dump, 
	tst_date_operation_execute 
};

/*
 * Optional operands
 */

enum tst_date_optional {
	OPT_DATE_ZONE = SIEVE_MATCH_OPT_LAST,
	OPT_DATE_LAST
};

/*
 * Tag implementation
 */

static bool tag_zone_validate
(struct sieve_validator *validator, struct sieve_ast_argument **arg,
    struct sieve_command_context *cmd)
{
	struct sieve_ast_argument *tag = *arg;

	if ( (bool) cmd->data ) {
		if ( cmd->command == &date_test ) {
			sieve_argument_validate_error(validator, *arg,
				"multiple :zone or :originalzone arguments specified for "
				"the currentdate test");
		} else {
			sieve_argument_validate_error(validator, *arg,
				"multiple :zone arguments specified for the currentdate test");
		}
		return FALSE;
	}

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

	/* :content tag has a string-list argument */
	if ( tag->argument == &date_zone_tag ) {

		/* Check syntax:
		 *   :zone <time-zone: string>
		 */
		if ( !sieve_validate_tag_parameter
			(validator, cmd, tag, *arg, SAAT_STRING) ) {
			return FALSE;
		}

		/* Check it */
		if ( sieve_argument_is_string_literal(*arg) ) {
			const char *zone = sieve_ast_argument_strc(*arg);
	
			if ( !ext_date_parse_timezone(zone, NULL) ) {
				sieve_argument_validate_warning(validator, *arg,
					"specified :zone argument '%s' is not a valid timezone",
					str_sanitize(zone, 40));
			}		
		}
	
		/* Assign tag parameters */
		tag->parameters = *arg;
		*arg = sieve_ast_arguments_detach(*arg,1);
	} 

	cmd->data = (void *) TRUE;

	return TRUE;
}

/* 
 * Test registration 
 */

static bool tst_date_registered
(struct sieve_validator *valdtr, struct sieve_command_registration *cmd_reg) 
{
	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, &date_zone_tag, OPT_DATE_ZONE);
	sieve_validator_register_tag
		(valdtr, cmd_reg, &date_originalzone_tag, OPT_DATE_ZONE);

	return TRUE;
}

static bool tst_currentdate_registered
(struct sieve_validator *valdtr, struct sieve_command_registration *cmd_reg) 
{
	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, &date_zone_tag, OPT_DATE_ZONE);

	return TRUE;
}

/* 
 * Validation 
 */
 
static bool tst_date_validate
	(struct sieve_validator *valdtr, struct sieve_command_context *tst) 
{
	struct sieve_ast_argument *arg = tst->first_positional;
	unsigned int arg_offset = 0 ;
		
	/* Check header name */

	if ( tst->command == &date_test ) {
		arg_offset = 1;

		if ( !sieve_validate_positional_argument
			(valdtr, tst, arg, "header name", 1, SAAT_STRING) ) {
			return FALSE;
		}
	
		if ( !sieve_validator_argument_activate(valdtr, tst, arg, FALSE) )
			return FALSE;

		if ( !sieve_command_verify_headers_argument(valdtr, arg) )
    	    return FALSE;

		arg = sieve_ast_argument_next(arg);
	}

	/* Check date part */

	if ( !sieve_validate_positional_argument
		(valdtr, tst, arg, "date part", arg_offset + 1, SAAT_STRING) ) {
		return FALSE;
	}
	
	if ( !sieve_validator_argument_activate(valdtr, tst, arg, FALSE) )
		return FALSE;

	arg = sieve_ast_argument_next(arg);

	/* Check key list */
		
	if ( !sieve_validate_positional_argument
		(valdtr, tst, arg, "key list", arg_offset + 2, SAAT_STRING_LIST) ) {
		return FALSE;
	}

	if ( !sieve_validator_argument_activate(valdtr, tst, arg, FALSE) )
		return FALSE;
	
	/* Validate the key argument to a specified match type */
	return sieve_match_type_validate
		(valdtr, tst, arg, &is_match_type, &i_ascii_casemap_comparator); 
}

/* 
 * Code generation 
 */

static bool tst_date_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command_context *tst) 
{
	if ( tst->command == &date_test )
		sieve_operation_emit_code(cgenv->sbin, &date_operation);
	else if ( tst->command == &currentdate_test )
		sieve_operation_emit_code(cgenv->sbin, &currentdate_operation);
	else
		i_unreached();

	/* Generate arguments */  	
	return sieve_generate_arguments(cgenv, tst, NULL);
}

static bool tag_zone_generate
(const struct sieve_codegen_env *cgenv, struct sieve_ast_argument *arg,
    struct sieve_command_context *cmd)
{
	struct sieve_ast_argument *param = arg->parameters;

	if ( param == NULL ) {
		sieve_opr_omitted_emit(cgenv->sbin);
		return TRUE;
	}

	if ( param->argument != NULL && param->argument->generate != NULL )
		return param->argument->generate(cgenv, param, cmd);

	return FALSE;	
}

/* 
 * Code dump 
 */

static bool tst_date_operation_dump
(const struct sieve_operation *op,	
	const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	int opt_code = 0;
	const struct sieve_operand *operand;

	sieve_code_dumpf(denv, "%s", op->mnemonic);
	sieve_code_descend(denv);
	
	/* Handle any optional arguments */
  do {
		if ( !sieve_match_dump_optional_operands(denv, address, &opt_code) )
			return FALSE;

		switch ( opt_code ) {
		case SIEVE_MATCH_OPT_END:
			break;
		case OPT_DATE_ZONE:
			operand = sieve_operand_read(denv->sbin, address);
			if ( operand == NULL ) {
				sieve_code_dumpf(denv, "ERROR: INVALID OPERAND");
				return FALSE;
			}				

			if ( sieve_operand_is_omitted(operand) ) {
				sieve_code_dumpf(denv, "zone: ORIGINAL");
			} else {
				if ( !sieve_opr_string_dump_data
					(denv, operand, address, "zone") )
					return FALSE;
			}
			break;
    default:
			return FALSE;
		}
	} while ( opt_code != SIEVE_MATCH_OPT_END );

	if ( op == &date_operation &&
		!sieve_opr_string_dump(denv, address, "header name") )
		return FALSE;

	return
		sieve_opr_string_dump(denv, address, "date part") && 
		sieve_opr_stringlist_dump(denv, address, "key list");
}

/* 
 * Code execution 
 */

static int tst_date_operation_execute
(const struct sieve_operation *op, 
	const struct sieve_runtime_env *renv, sieve_size_t *address)
{	
	bool result = TRUE, zone_specified = FALSE, got_date = FALSE, matched = FALSE;
	int opt_code = 0;
	const struct sieve_message_data *msgdata = renv->msgdata;
	const struct sieve_comparator *cmp = &i_ascii_casemap_comparator;
	const struct sieve_match_type *mtch = &is_match_type;
	const struct sieve_operand *operand;
	struct sieve_match_context *mctx;
	string_t *header_name = NULL, *date_part = NULL, *zone = NULL;
	struct sieve_coded_stringlist *key_list;
	time_t date_value, local_time;
	struct tm *date_tm;
	const char *part_value;
	int local_zone = 0, original_zone = 0, wanted_zone = 0;
	int ret;
	
	/* Read optional operands */
	do {
		if ( (ret=sieve_match_read_optional_operands
			(renv, address, &opt_code, &cmp, &mtch)) <= 0 )
			return ret;

		switch ( opt_code ) {
		case SIEVE_MATCH_OPT_END:
			break;
		case OPT_DATE_ZONE:
			operand = sieve_operand_read(renv->sbin, address);
			if ( operand == NULL ) {
				sieve_runtime_trace_error(renv, "invalid operand");
				return SIEVE_EXEC_BIN_CORRUPT;
			}

			if ( !sieve_operand_is_omitted(operand) ) {
				if ( !sieve_opr_string_read_data
					(renv, operand, address, &zone) ) {
					sieve_runtime_trace_error(renv, "invalid zone operand");
					return SIEVE_EXEC_BIN_CORRUPT;
				}
			}

			zone_specified = TRUE;
			break;
		default:
			sieve_runtime_trace_error(renv, "unknown optional operand");
			return SIEVE_EXEC_BIN_CORRUPT;
		}
	} while ( opt_code != SIEVE_MATCH_OPT_END );


	if ( op == &date_operation ) {
		/* Read header name */
		if ( !sieve_opr_string_read(renv, address, &header_name) ) {
			sieve_runtime_trace_error(renv, "invalid header-name operand");
			return SIEVE_EXEC_BIN_CORRUPT;
		}
	}

	/* Read date part */
	if ( !sieve_opr_string_read(renv, address, &date_part) ) {
		sieve_runtime_trace_error(renv, "invalid date-part operand");
		return SIEVE_EXEC_BIN_CORRUPT;
	}
		
	/* Read key-list */
	if ( (key_list=sieve_opr_stringlist_read(renv, address)) == NULL ) {
		sieve_runtime_trace_error(renv, "invalid key-list operand");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	/* Perform test */

	sieve_runtime_trace(renv, "%s test", op->mnemonic);

	/* Get the date value */

	local_time = ext_date_get_current_date(renv, &local_zone);

	if ( op == 	&date_operation ) {
		const char *header_value;
		const char *date_string;

		/* Get date from the message */

		/* Read first header
		 *   NOTE: need something for index extension to hook into some time. 
		 */
		if ( (ret=mail_get_first_header
			(msgdata->mail, str_c(header_name), &header_value)) > 0 ) {

			/* Extract the date string value */
			date_string = strrchr(header_value, ';');
			if ( date_string == NULL )
				/* Direct header value */
				date_string = header_value;
			else {
				/* Delimited by ';', e.g. a Received: header */
				date_string++; 
			}

			/* Parse the date value */
			if ( message_date_parse((const unsigned char *) date_string,
				strlen(date_string), &date_value, &original_zone) ) {
				got_date = TRUE;
			}
		}
	} else if ( op == &currentdate_operation ) {
		/* Use time stamp recorded at the time the script first started */

		date_value = local_time;
		original_zone = local_zone;
		got_date = TRUE;

	} else {
		i_unreached();
	}

	if ( got_date ) {
		/* Apply wanted timezone */

		if ( !zone_specified )
			wanted_zone = local_zone;
		else if ( zone == NULL 
			|| !ext_date_parse_timezone(str_c(zone), &wanted_zone) ) {

			/* FIXME: warn about parse failures */
			wanted_zone = original_zone;
		}

		date_value += wanted_zone * 60;

		/* Convert timestamp to struct tm */

		if ( (date_tm=gmtime(&date_value)) == NULL ) {
			got_date = FALSE;
		} else {
			/* Extract the date part */
			part_value = ext_date_part_extract
				(str_c(date_part), date_tm, wanted_zone);
		}
	}

	/* Initialize match */
	mctx = sieve_match_begin(renv->interp, mtch, cmp, NULL, key_list); 	
	
	if ( got_date && part_value != NULL ) {		
		/* Match value */
		if ( (ret=sieve_match_value(mctx, part_value, strlen(part_value))) < 0 )
			result = FALSE;
		else
			matched = ret > 0;
	}

	/* Finish match */
	if ( (ret=sieve_match_end(&mctx)) < 0 ) 
		result = FALSE;
	else
		matched = ( ret > 0 || matched );
	
	/* Set test result for subsequent conditional jump */
	if ( result ) {
		sieve_interpreter_set_test_result(renv->interp, matched);
		return SIEVE_EXEC_OK;
	}	

	sieve_runtime_trace_error(renv, "invalid string-list item");
	return SIEVE_EXEC_BIN_CORRUPT;
}