-
Notifications
You must be signed in to change notification settings - Fork 29
/
cmd-addheader.c
337 lines (268 loc) · 7.74 KB
/
cmd-addheader.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
/* 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-extensions.h"
#include "sieve-commands.h"
#include "sieve-code.h"
#include "sieve-message.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-binary.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "ext-editheader-common.h"
/*
* Addheader command
*
* Syntax
* "addheader" [":last"] <field-name: string> <value: string>
*/
static bool cmd_addheader_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
struct sieve_command_registration *cmd_reg);
static bool cmd_addheader_validate
(struct sieve_validator *valdtr, struct sieve_command *tst);
static bool cmd_addheader_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *ctx);
const struct sieve_command_def addheader_command = {
.identifier = "addheader",
.type = SCT_COMMAND,
.positional_args = 2,
.subtests = 0,
.block_allowed = FALSE,
.block_required = FALSE,
.registered = cmd_addheader_registered,
.validate = cmd_addheader_validate,
.generate = cmd_addheader_generate
};
/*
* Addheader command tags
*/
/* Argument objects */
static const struct sieve_argument_def addheader_last_tag = {
.identifier = "last"
};
/* Codes for optional arguments */
enum cmd_addheader_optional {
OPT_END,
OPT_LAST
};
/*
* Addheader operation
*/
static bool cmd_addheader_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int cmd_addheader_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address);
const struct sieve_operation_def addheader_operation = {
.mnemonic = "ADDHEADER",
.ext_def = &editheader_extension,
.code = EXT_EDITHEADER_OPERATION_ADDHEADER,
.dump = cmd_addheader_operation_dump,
.execute = cmd_addheader_operation_execute
};
/*
* Utility
*/
static bool _str_contains_nul(const string_t *str)
{
const unsigned char *p, *pend;
p = str_data(str);
pend = p + str_len(str);
while (p < pend) {
if (*p == '\0')
return TRUE;
p++;
}
return FALSE;
}
/*
* Validation
*/
static bool cmd_addheader_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd)
{
struct sieve_ast_argument *arg = cmd->first_positional;
/* Check field-name syntax */
if ( !sieve_validate_positional_argument
(valdtr, cmd, arg, "field-name", 1, SAAT_STRING) ) {
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, "addheader command: specified field name `%s' is invalid",
str_sanitize(str_c(fname), 80));
return FALSE;
}
if ( !ext_editheader_header_allow_add
(cmd->ext, str_c(fname)) ) {
sieve_argument_validate_warning
(valdtr, arg, "addheader command: "
"adding specified header field `%s' is forbidden; "
"modification will be denied",
str_sanitize(str_c(fname), 80));
}
}
/* Check value syntax */
arg = sieve_ast_argument_next(arg);
if ( !sieve_validate_positional_argument
(valdtr, cmd, arg, "value", 2, SAAT_STRING) ) {
return FALSE;
}
if ( !sieve_validator_argument_activate(valdtr, cmd, arg, FALSE) )
return FALSE;
if ( sieve_argument_is_string_literal(arg) ) {
string_t *fvalue = sieve_ast_argument_str(arg);
if ( _str_contains_nul(fvalue) ) {
sieve_argument_validate_error(valdtr, arg,
"addheader command: specified value `%s' is invalid "
"(contains NUL character)", str_sanitize(str_c(fvalue), 80));
return FALSE;
}
if ( !rfc2822_header_field_body_verify
(str_c(fvalue), str_len(fvalue), TRUE, TRUE) ) {
sieve_argument_validate_warning(valdtr, arg,
"addheader command: specified value `%s' is invalid",
str_sanitize(str_c(fvalue), 80));
}
if ( ext_editheader_header_too_large(cmd->ext, str_len(fvalue)) ) {
sieve_argument_validate_error(valdtr, arg, "addheader command: "
"specified header value `%s' is too large (%"PRIuSIZE_T" bytes)",
str_sanitize(str_c(fvalue), 80), str_len(fvalue));
return SIEVE_EXEC_FAILURE;
}
}
return TRUE;
}
/*
* Command registration
*/
static bool cmd_addheader_registered
(struct sieve_validator *valdtr, const struct sieve_extension *ext,
struct sieve_command_registration *cmd_reg)
{
sieve_validator_register_tag
(valdtr, cmd_reg, ext, &addheader_last_tag, OPT_LAST);
return TRUE;
}
/*
* Code generation
*/
static bool cmd_addheader_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
(void)sieve_operation_emit(cgenv->sblock, cmd->ext, &addheader_operation);
/* Generate arguments */
return sieve_generate_arguments(cgenv, cmd, NULL);
}
/*
* Code dump
*/
static bool cmd_addheader_operation_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
int opt_code = 0;
sieve_code_dumpf(denv, "addheader");
sieve_code_descend(denv);
/* Dump optional operands */
for (;;) {
int opt;
if ( (opt=sieve_opr_optional_dump(denv, address, &opt_code)) < 0 )
return FALSE;
if ( opt == 0 ) break;
if ( opt_code == OPT_LAST ) {
sieve_code_dumpf(denv, "last");
} else {
return FALSE;
}
}
return
sieve_opr_string_dump(denv, address, "field-name") &&
sieve_opr_string_dump(denv, address, "value");
}
/*
* Interpretation
*/
static int cmd_addheader_operation_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address)
{
const struct sieve_extension *this_ext = renv->oprtn->ext;
string_t *field_name;
string_t *value;
struct edit_mail *edmail;
bool last = FALSE;
int opt_code = 0;
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_LAST:
last = TRUE;
break;
default:
sieve_runtime_trace_error(renv, "unknown optional operand");
return SIEVE_EXEC_BIN_CORRUPT;
}
}
/* Read message */
if ( (ret=sieve_opr_string_read
(renv, address, "field-name", &field_name)) <= 0 )
return ret;
if ( (ret=sieve_opr_string_read
(renv, address, "value", &value)) <= 0 )
return ret;
/*
* Verify arguments
*/
if ( !rfc2822_header_field_name_verify
(str_c(field_name), str_len(field_name)) ) {
sieve_runtime_error(renv, NULL, "addheader action: "
"specified field name `%s' is invalid",
str_sanitize(str_c(field_name), 80));
return SIEVE_EXEC_FAILURE;
}
if ( !ext_editheader_header_allow_add
(this_ext, str_c(field_name)) ) {
sieve_runtime_warning(renv, NULL, "addheader action: "
"adding specified header field `%s' is forbidden; "
"modification denied", str_sanitize(str_c(field_name), 80));
return SIEVE_EXEC_OK;
}
if ( _str_contains_nul(value) ) {
sieve_runtime_error(renv, NULL, "addheader action: "
"specified value `%s' is invalid (contains NUL character)",
str_sanitize(str_c(value), 80));
return SIEVE_EXEC_FAILURE;
}
if ( ext_editheader_header_too_large(this_ext, str_len(value)) ) {
sieve_runtime_error(renv, NULL, "addheader action: "
"specified header value `%s' is too large (%"PRIuSIZE_T" bytes)",
str_sanitize(str_c(value), 80), str_len(value));
return SIEVE_EXEC_FAILURE;
}
/*
* Perform operation
*/
sieve_runtime_trace(renv, SIEVE_TRLVL_COMMANDS, "addheader \"%s: %s\"",
str_sanitize(str_c(field_name), 80), str_sanitize(str_c(value), 80));
edmail = sieve_message_edit(renv->msgctx);
edit_mail_header_add(edmail,
rfc2822_header_field_name_sanitize(str_c(field_name)),
str_c(value), last);
return SIEVE_EXEC_OK;
}