/src/pigeonhole/src/lib-sieve/plugins/editheader/cmd-addheader.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str-sanitize.h" |
5 | | #include "mail-storage.h" |
6 | | |
7 | | #include "rfc2822.h" |
8 | | #include "edit-mail.h" |
9 | | |
10 | | #include "sieve-extensions.h" |
11 | | #include "sieve-commands.h" |
12 | | #include "sieve-code.h" |
13 | | #include "sieve-message.h" |
14 | | |
15 | | #include "sieve-validator.h" |
16 | | #include "sieve-generator.h" |
17 | | #include "sieve-binary.h" |
18 | | #include "sieve-interpreter.h" |
19 | | #include "sieve-dump.h" |
20 | | |
21 | | #include "ext-editheader-common.h" |
22 | | |
23 | | /* |
24 | | * Addheader command |
25 | | * |
26 | | * Syntax |
27 | | * "addheader" [":last"] <field-name: string> <value: string> |
28 | | */ |
29 | | |
30 | | static bool |
31 | | cmd_addheader_registered(struct sieve_validator *valdtr, |
32 | | const struct sieve_extension *ext, |
33 | | struct sieve_command_registration *cmd_reg); |
34 | | static bool |
35 | | cmd_addheader_validate(struct sieve_validator *valdtr, |
36 | | struct sieve_command *tst); |
37 | | static bool |
38 | | cmd_addheader_generate(const struct sieve_codegen_env *cgenv, |
39 | | struct sieve_command *ctx); |
40 | | |
41 | | const struct sieve_command_def addheader_command = { |
42 | | .identifier = "addheader", |
43 | | .type = SCT_COMMAND, |
44 | | .positional_args = 2, |
45 | | .subtests = 0, |
46 | | .block_allowed = FALSE, |
47 | | .block_required = FALSE, |
48 | | .registered = cmd_addheader_registered, |
49 | | .validate = cmd_addheader_validate, |
50 | | .generate = cmd_addheader_generate |
51 | | }; |
52 | | |
53 | | /* |
54 | | * Addheader command tags |
55 | | */ |
56 | | |
57 | | /* Argument objects */ |
58 | | |
59 | | static const struct sieve_argument_def addheader_last_tag = { |
60 | | .identifier = "last", |
61 | | }; |
62 | | |
63 | | /* Codes for optional arguments */ |
64 | | |
65 | | enum cmd_addheader_optional { |
66 | | OPT_END, |
67 | | OPT_LAST, |
68 | | }; |
69 | | |
70 | | /* |
71 | | * Addheader operation |
72 | | */ |
73 | | |
74 | | static bool |
75 | | cmd_addheader_operation_dump(const struct sieve_dumptime_env *denv, |
76 | | sieve_size_t *address); |
77 | | static int |
78 | | cmd_addheader_operation_execute(const struct sieve_runtime_env *renv, |
79 | | sieve_size_t *address); |
80 | | |
81 | | const struct sieve_operation_def addheader_operation = { |
82 | | .mnemonic = "ADDHEADER", |
83 | | .ext_def = &editheader_extension, |
84 | | .code = EXT_EDITHEADER_OPERATION_ADDHEADER, |
85 | | .dump = cmd_addheader_operation_dump, |
86 | | .execute = cmd_addheader_operation_execute, |
87 | | }; |
88 | | |
89 | | /* |
90 | | * Utility |
91 | | */ |
92 | | |
93 | | static bool _str_contains_nul(const string_t *str) |
94 | 0 | { |
95 | 0 | const unsigned char *p, *pend; |
96 | |
|
97 | 0 | p = str_data(str); |
98 | 0 | pend = p + str_len(str); |
99 | 0 | while (p < pend) { |
100 | 0 | if (*p == '\0') |
101 | 0 | return TRUE; |
102 | 0 | p++; |
103 | 0 | } |
104 | 0 | return FALSE; |
105 | 0 | } |
106 | | |
107 | | /* |
108 | | * Validation |
109 | | */ |
110 | | |
111 | | static bool |
112 | | cmd_addheader_validate(struct sieve_validator *valdtr, |
113 | | struct sieve_command *cmd) |
114 | 0 | { |
115 | 0 | struct sieve_ast_argument *arg = cmd->first_positional; |
116 | | |
117 | | /* Check field-name syntax */ |
118 | |
|
119 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "field-name", |
120 | 0 | 1, SAAT_STRING)) |
121 | 0 | return FALSE; |
122 | 0 | if (!sieve_validator_argument_activate(valdtr, cmd, arg, FALSE)) |
123 | 0 | return FALSE; |
124 | | |
125 | 0 | if (sieve_argument_is_string_literal(arg)) { |
126 | 0 | string_t *fname = sieve_ast_argument_str(arg); |
127 | |
|
128 | 0 | if (!rfc2822_header_field_name_verify(str_c(fname), |
129 | 0 | str_len(fname))) { |
130 | 0 | sieve_argument_validate_error( |
131 | 0 | valdtr, arg, "addheader command: " |
132 | 0 | "specified field name '%s' is invalid", |
133 | 0 | str_sanitize(str_c(fname), 80)); |
134 | 0 | return FALSE; |
135 | 0 | } |
136 | | |
137 | 0 | if (!ext_editheader_header_allow_add(cmd->ext, str_c(fname))) { |
138 | 0 | sieve_argument_validate_warning( |
139 | 0 | valdtr, arg, "addheader command: " |
140 | 0 | "adding specified header field '%s' is forbidden; " |
141 | 0 | "modification will be denied", |
142 | 0 | str_sanitize(str_c(fname), 80)); |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* Check value syntax */ |
147 | | |
148 | 0 | arg = sieve_ast_argument_next(arg); |
149 | |
|
150 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "value", 2, |
151 | 0 | SAAT_STRING)) |
152 | 0 | return FALSE; |
153 | 0 | if (!sieve_validator_argument_activate(valdtr, cmd, arg, FALSE)) |
154 | 0 | return FALSE; |
155 | | |
156 | 0 | if (sieve_argument_is_string_literal(arg)) { |
157 | 0 | string_t *fvalue = sieve_ast_argument_str(arg); |
158 | |
|
159 | 0 | if (_str_contains_nul(fvalue)) { |
160 | 0 | sieve_argument_validate_error( |
161 | 0 | valdtr, arg, |
162 | 0 | "addheader command: specified value '%s' is invalid " |
163 | 0 | "(contains NUL character)", |
164 | 0 | str_sanitize(str_c(fvalue), 80)); |
165 | 0 | return FALSE; |
166 | 0 | } |
167 | | |
168 | 0 | if (!rfc2822_header_field_body_verify(str_c( |
169 | 0 | fvalue), str_len(fvalue), TRUE, TRUE)) { |
170 | 0 | sieve_argument_validate_error( |
171 | 0 | valdtr, arg, |
172 | 0 | "addheader command: specified value '%s' is invalid", |
173 | 0 | str_sanitize(str_c(fvalue), 80)); |
174 | 0 | return SIEVE_EXEC_FAILURE; |
175 | 0 | } |
176 | | |
177 | 0 | if (ext_editheader_header_too_large( |
178 | 0 | cmd->ext, str_len(fvalue))) { |
179 | 0 | sieve_argument_validate_error( |
180 | 0 | valdtr, arg, "addheader command: " |
181 | 0 | "specified header value '%s' is too large (%zu bytes)", |
182 | 0 | str_sanitize(str_c(fvalue), 80), |
183 | 0 | str_len(fvalue)); |
184 | 0 | return SIEVE_EXEC_FAILURE; |
185 | 0 | } |
186 | 0 | } |
187 | 0 | return TRUE; |
188 | 0 | } |
189 | | |
190 | | /* |
191 | | * Command registration |
192 | | */ |
193 | | |
194 | | static bool |
195 | | cmd_addheader_registered(struct sieve_validator *valdtr, |
196 | | const struct sieve_extension *ext, |
197 | | struct sieve_command_registration *cmd_reg) |
198 | 0 | { |
199 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
200 | 0 | &addheader_last_tag, OPT_LAST); |
201 | 0 | return TRUE; |
202 | 0 | } |
203 | | |
204 | | /* |
205 | | * Code generation |
206 | | */ |
207 | | |
208 | | static bool |
209 | | cmd_addheader_generate(const struct sieve_codegen_env *cgenv, |
210 | | struct sieve_command *cmd) |
211 | 0 | { |
212 | 0 | (void)sieve_operation_emit(cgenv->sblock, cmd->ext, |
213 | 0 | &addheader_operation); |
214 | | |
215 | | /* Generate arguments */ |
216 | 0 | return sieve_generate_arguments(cgenv, cmd, NULL); |
217 | 0 | } |
218 | | |
219 | | /* |
220 | | * Code dump |
221 | | */ |
222 | | |
223 | | static bool |
224 | | cmd_addheader_operation_dump(const struct sieve_dumptime_env *denv, |
225 | | sieve_size_t *address) |
226 | 0 | { |
227 | 0 | int opt_code = 0; |
228 | |
|
229 | 0 | sieve_code_dumpf(denv, "ADDHEADER"); |
230 | 0 | sieve_code_descend(denv); |
231 | | |
232 | | /* Dump optional operands */ |
233 | |
|
234 | 0 | for (;;) { |
235 | 0 | int opt; |
236 | |
|
237 | 0 | opt = sieve_opr_optional_dump(denv, address, &opt_code); |
238 | 0 | if (opt < 0) |
239 | 0 | return FALSE; |
240 | 0 | if (opt == 0) |
241 | 0 | break; |
242 | | |
243 | 0 | if (opt_code == OPT_LAST) |
244 | 0 | sieve_code_dumpf(denv, "last"); |
245 | 0 | else |
246 | 0 | return FALSE; |
247 | 0 | } |
248 | | |
249 | 0 | return (sieve_opr_string_dump(denv, address, "field-name") && |
250 | 0 | sieve_opr_string_dump(denv, address, "value")); |
251 | 0 | } |
252 | | |
253 | | /* |
254 | | * Interpretation |
255 | | */ |
256 | | |
257 | | static int |
258 | | cmd_addheader_operation_execute(const struct sieve_runtime_env *renv, |
259 | | sieve_size_t *address) |
260 | 0 | { |
261 | 0 | const struct sieve_extension *this_ext = renv->oprtn->ext; |
262 | 0 | string_t *field_name; |
263 | 0 | string_t *value; |
264 | 0 | struct edit_mail *edmail; |
265 | 0 | bool last = FALSE; |
266 | 0 | int opt_code = 0; |
267 | 0 | int ret; |
268 | | |
269 | | /* |
270 | | * Read operands |
271 | | */ |
272 | | |
273 | | /* Optional operands */ |
274 | |
|
275 | 0 | for (;;) { |
276 | 0 | int opt; |
277 | |
|
278 | 0 | opt = sieve_opr_optional_read(renv, address, &opt_code); |
279 | 0 | if (opt < 0) |
280 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
281 | 0 | if (opt == 0) |
282 | 0 | break; |
283 | | |
284 | 0 | switch (opt_code) { |
285 | 0 | case OPT_LAST: |
286 | 0 | last = TRUE; |
287 | 0 | break; |
288 | 0 | default: |
289 | 0 | sieve_runtime_trace_error( |
290 | 0 | renv, "unknown optional operand"); |
291 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | | /* Read message */ |
296 | | |
297 | 0 | ret = sieve_opr_string_read(renv, address, "field-name", &field_name); |
298 | 0 | if (ret <= 0) |
299 | 0 | return ret; |
300 | 0 | ret = sieve_opr_string_read(renv, address, "value", &value); |
301 | 0 | if (ret <= 0) |
302 | 0 | return ret; |
303 | | |
304 | | /* |
305 | | * Verify arguments |
306 | | */ |
307 | | |
308 | 0 | if (!rfc2822_header_field_name_verify(str_c(field_name), |
309 | 0 | str_len(field_name))) { |
310 | 0 | sieve_runtime_error( |
311 | 0 | renv, NULL, "addheader action: " |
312 | 0 | "specified field name '%s' is invalid", |
313 | 0 | str_sanitize(str_c(field_name), 80)); |
314 | 0 | return SIEVE_EXEC_FAILURE; |
315 | 0 | } |
316 | | |
317 | 0 | if (!ext_editheader_header_allow_add(this_ext, str_c(field_name))) { |
318 | 0 | sieve_runtime_warning( |
319 | 0 | renv, NULL, "addheader action: " |
320 | 0 | "adding specified header field '%s' is forbidden; " |
321 | 0 | "modification denied", |
322 | 0 | str_sanitize(str_c(field_name), 80)); |
323 | 0 | return SIEVE_EXEC_OK; |
324 | 0 | } |
325 | | |
326 | 0 | if (_str_contains_nul(value)) { |
327 | 0 | sieve_runtime_error( |
328 | 0 | renv, NULL, "addheader action: " |
329 | 0 | "specified value '%s' is invalid (contains NUL character)", |
330 | 0 | str_sanitize(str_c(value), 80)); |
331 | 0 | return SIEVE_EXEC_FAILURE; |
332 | 0 | } |
333 | | |
334 | 0 | if (ext_editheader_header_too_large(this_ext, str_len(value))) { |
335 | 0 | sieve_runtime_error( |
336 | 0 | renv, NULL, "addheader action: " |
337 | 0 | "specified header value '%s' is too large (%zu bytes)", |
338 | 0 | str_sanitize(str_c(value), 80), str_len(value)); |
339 | 0 | return SIEVE_EXEC_FAILURE; |
340 | 0 | } |
341 | | |
342 | | /* |
343 | | * Perform operation |
344 | | */ |
345 | | |
346 | 0 | sieve_runtime_trace( |
347 | 0 | renv, SIEVE_TRLVL_COMMANDS, "addheader \"%s: %s\"", |
348 | 0 | str_sanitize(str_c(field_name), 80), |
349 | 0 | str_sanitize(str_c(value), 80)); |
350 | |
|
351 | 0 | edmail = sieve_message_edit(renv->msgctx); |
352 | 0 | edit_mail_header_add( |
353 | 0 | edmail, rfc2822_header_field_name_sanitize(str_c(field_name)), |
354 | 0 | str_c(value), last); |
355 | 0 | return SIEVE_EXEC_OK; |
356 | 0 | } |