-
Notifications
You must be signed in to change notification settings - Fork 29
/
tst-duplicate.c
416 lines (350 loc) · 10.5 KB
/
tst-duplicate.c
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
/* Copyright (c) 2002-2017 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "mail-storage.h"
#include "sieve-common.h"
#include "sieve-code.h"
#include "sieve-extensions.h"
#include "sieve-commands.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "ext-duplicate-common.h"
/* Duplicate test
*
* Syntax:
* Usage: "duplicate" [":handle" <handle: string>]
* [":header" <header-name: string> /
* ":uniqueid" <value: string>]
* [":seconds" <timeout: number>] [":last"]
*/
static bool tst_duplicate_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
struct sieve_command_registration *cmd_reg);
static bool tst_duplicate_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *ctx);
const struct sieve_command_def tst_duplicate = {
.identifier = "duplicate",
.type = SCT_TEST,
.positional_args = 0,
.subtests = 0,
.block_allowed = FALSE,
.block_required = FALSE,
.registered = tst_duplicate_registered,
.generate = tst_duplicate_generate
};
/*
* Duplicate test tags
*/
static bool tst_duplicate_validate_number_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
struct sieve_command *cmd);
static bool tst_duplicate_validate_string_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
struct sieve_command *cmd);
static const struct sieve_argument_def duplicate_seconds_tag = {
.identifier = "seconds",
.validate = tst_duplicate_validate_number_tag
};
static const struct sieve_argument_def duplicate_header_tag = {
.identifier = "header",
.validate = tst_duplicate_validate_string_tag
};
static const struct sieve_argument_def duplicate_uniqueid_tag = {
.identifier = "uniqueid",
.validate = tst_duplicate_validate_string_tag
};
static const struct sieve_argument_def duplicate_value_tag = {
.identifier = "value", /* vnd.dovecot.duplicate (deprecated) */
.validate = tst_duplicate_validate_string_tag
};
static const struct sieve_argument_def duplicate_handle_tag = {
.identifier = "handle",
.validate = tst_duplicate_validate_string_tag
};
static const struct sieve_argument_def duplicate_last_tag = {
.identifier = "last"
};
/* Codes for optional arguments */
enum tst_duplicate_optional {
OPT_END,
OPT_SECONDS,
OPT_HEADER,
OPT_UNIQUEID,
OPT_LAST,
OPT_HANDLE
};
/*
* Duplicate operation
*/
static bool tst_duplicate_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int tst_duplicate_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address);
const struct sieve_operation_def tst_duplicate_operation = {
.mnemonic = "DUPLICATE",
.ext_def = &duplicate_extension,
.dump = tst_duplicate_operation_dump,
.execute = tst_duplicate_operation_execute
};
/*
* Tag validation
*/
static bool tst_duplicate_validate_number_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
struct sieve_command *cmd)
{
const struct sieve_extension *ext = sieve_argument_ext(*arg);
const struct ext_duplicate_config *config =
(const struct ext_duplicate_config *) ext->context;
struct sieve_ast_argument *tag = *arg;
sieve_number_t seconds;
/* Detach the tag itself */
*arg = sieve_ast_arguments_detach(*arg,1);
/* Check syntax:
* :seconds number
*/
if ( !sieve_validate_tag_parameter
(valdtr, cmd, tag, *arg, NULL, 0, SAAT_NUMBER, FALSE) ) {
return FALSE;
}
seconds = sieve_ast_argument_number(*arg);
/* Enforce :days <= max_period */
if ( config->max_period > 0 && seconds > config->max_period ) {
seconds = config->max_period;
sieve_argument_validate_warning(valdtr, *arg,
"specified :seconds value '%llu' is over the maximum",
(unsigned long long)seconds);
}
sieve_ast_argument_number_set(*arg, seconds);
/* Skip parameter */
*arg = sieve_ast_argument_next(*arg);
return TRUE;
}
static bool tst_duplicate_validate_string_tag
(struct sieve_validator *valdtr, struct sieve_ast_argument **arg,
struct sieve_command *cmd)
{
const struct sieve_extension *ext = cmd->ext;
struct sieve_ast_argument *tag = *arg;
/* Detach the tag itself */
*arg = sieve_ast_arguments_detach(*arg,1);
/* Check syntax:
* :header <header-name: string>
* :value <value: string>
* :handle <handle: string>
*/
if ( !sieve_validate_tag_parameter
(valdtr, cmd, tag, *arg, NULL, 0, SAAT_STRING, FALSE) ) {
return FALSE;
}
if ( !sieve_argument_is(tag, duplicate_handle_tag) && (bool)cmd->data ) {
sieve_argument_validate_error(valdtr, *arg,
"conflicting :header and %s arguments specified "
"for the duplicate test",
(sieve_extension_is(ext, duplicate_extension) ? ":uniqueid" : ":value"));
return FALSE;
}
/* :header <header-name: string> */
if ( sieve_argument_is(tag, duplicate_header_tag) ) {
if ( !sieve_command_verify_headers_argument(valdtr, *arg) )
return FALSE;
cmd->data = (void *)TRUE;
/* :handle <handle: string> */
} else if ( sieve_argument_is(tag, duplicate_handle_tag) ) {
/* nothing to be done */
} else if ( sieve_argument_is(tag, duplicate_uniqueid_tag) ) {
i_assert(sieve_extension_is(ext, duplicate_extension));
cmd->data = (void *)TRUE;
/* :value <value: string> (vnd.dovecot.duplicate) */
} else if ( sieve_argument_is(tag, duplicate_value_tag) ) {
i_assert(sieve_extension_is(ext, vnd_duplicate_extension));
cmd->data = (void *)TRUE;
} else {
i_unreached();
}
/* Skip parameter */
*arg = sieve_ast_argument_next(*arg);
return TRUE;
}
/*
* Command registration
*/
static bool tst_duplicate_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
struct sieve_command_registration *cmd_reg)
{
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &duplicate_seconds_tag, OPT_SECONDS);
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &duplicate_last_tag, OPT_LAST);
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &duplicate_header_tag, OPT_HEADER);
if ( sieve_extension_is(ext, duplicate_extension) ) {
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &duplicate_uniqueid_tag, OPT_UNIQUEID);
} else {
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &duplicate_value_tag, OPT_UNIQUEID);
}
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &duplicate_handle_tag, OPT_HANDLE);
return TRUE;
}
/*
* Code generation
*/
static bool tst_duplicate_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
sieve_operation_emit(cgenv->sblock, cmd->ext, &tst_duplicate_operation);
if ( !sieve_generate_arguments(cgenv, cmd, NULL) )
return FALSE;
return TRUE;
}
/*
* Code dump
*/
static bool tst_duplicate_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
const struct sieve_extension *ext = denv->oprtn->ext;
int opt_code = 0;
sieve_code_dumpf(denv, "DUPLICATE");
sieve_code_descend(denv);
/* Dump optional operands */
for (;;) {
int opt;
bool opok = TRUE;
if ( (opt=sieve_opr_optional_dump(denv, address, &opt_code)) < 0 )
return FALSE;
if ( opt == 0 ) break;
switch ( opt_code ) {
case OPT_SECONDS:
opok = sieve_opr_number_dump(denv, address, "seconds");
break;
case OPT_LAST:
sieve_code_dumpf(denv, "last");
break;
case OPT_HEADER:
opok = sieve_opr_string_dump(denv, address, "header");
break;
case OPT_UNIQUEID:
if ( sieve_extension_is(ext, duplicate_extension) )
opok = sieve_opr_string_dump(denv, address, "uniqueid");
else
opok = sieve_opr_string_dump(denv, address, "value");
break;
case OPT_HANDLE:
opok = sieve_opr_string_dump(denv, address, "handle");
break;
default:
return FALSE;
}
if ( !opok ) return FALSE;
}
return TRUE;
}
/*
* Code execution
*/
static int tst_duplicate_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address ATTR_UNUSED)
{
const struct sieve_extension *ext = renv->oprtn->ext;
const struct ext_duplicate_config *config =
(const struct ext_duplicate_config *) ext->context;
struct mail *mail = renv->msgdata->mail;
int opt_code = 0;
string_t *handle = NULL, *header = NULL, *uniqueid = NULL;
const char *val = NULL;
size_t val_len = 0;
sieve_number_t seconds = config->default_period;
bool last = FALSE, duplicate = FALSE;
int ret;
/*
* Read operands
*/
/* Optional operands */
for (;;) {
int opt;
if ( (opt=sieve_opr_optional_read(renv, address, &opt_code)) < 0 )
return SIEVE_EXEC_BIN_CORRUPT;
if ( opt == 0 ) break;
switch ( opt_code ) {
case OPT_SECONDS:
ret = sieve_opr_number_read(renv, address, "seconds", &seconds);
break;
case OPT_LAST:
last = TRUE;
ret = SIEVE_EXEC_OK;
break;
case OPT_HEADER:
ret = sieve_opr_string_read(renv, address, "header", &header);
break;
case OPT_UNIQUEID:
if ( sieve_extension_is(ext, duplicate_extension) )
ret = sieve_opr_string_read(renv, address, "uniqueid", &uniqueid);
else
ret = sieve_opr_string_read(renv, address, "value", &uniqueid);
break;
case OPT_HANDLE:
ret = sieve_opr_string_read(renv, address, "handle", &handle);
break;
default:
sieve_runtime_trace_error(renv, "unknown optional operand");
ret = SIEVE_EXEC_BIN_CORRUPT;
}
if ( ret <= 0 ) return ret;
}
/*
* Perform operation
*/
/* Trace */
sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, "duplicate test");
sieve_runtime_trace_descend(renv);
/* Get value */
if ( uniqueid != NULL ) {
val = str_c(uniqueid);
val_len = str_len(uniqueid);
} else {
if ( header == NULL ) {
ret = mail_get_first_header_utf8(mail, "Message-ID", &val);
if ( ret < 0 ) {
return sieve_runtime_mail_error (renv, mail,
"duplicate test: "
"failed to read header field `message-id'");
}
} else {
ret = mail_get_first_header_utf8(mail, str_c(header), &val);
if ( ret < 0 ) {
return sieve_runtime_mail_error (renv, mail,
"duplicate test: "
"failed to read header field `%s'", str_c(header));
}
}
if ( ret > 0 )
val_len = strlen(val);
}
/* Check duplicate */
if (val == NULL) {
duplicate = FALSE;
} else {
if ((ret=ext_duplicate_check
(renv, handle, val, val_len, seconds, last)) < 0)
return SIEVE_EXEC_FAILURE;
duplicate = ( ret > 0 );
}
/* Trace */
if (duplicate) {
sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
"message is a duplicate");
} else {
sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS,
"message is not a duplicate");
}
/* Set test result for subsequent conditional jump */
sieve_interpreter_set_test_result(renv->interp, duplicate);
return SIEVE_EXEC_OK;
}