/src/pigeonhole/src/lib-sieve/plugins/vacation/cmd-vacation.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str.h" |
5 | | #include "strfuncs.h" |
6 | | #include "md5.h" |
7 | | #include "hostpid.h" |
8 | | #include "str-sanitize.h" |
9 | | #include "ostream.h" |
10 | | #include "message-address.h" |
11 | | #include "message-date.h" |
12 | | #include "var-expand.h" |
13 | | #include "ioloop.h" |
14 | | #include "mail-storage.h" |
15 | | |
16 | | #include "rfc2822.h" |
17 | | |
18 | | #include "sieve-common.h" |
19 | | #include "sieve-limits.h" |
20 | | #include "sieve-stringlist.h" |
21 | | #include "sieve-code.h" |
22 | | #include "sieve-address.h" |
23 | | #include "sieve-extensions.h" |
24 | | #include "sieve-commands.h" |
25 | | #include "sieve-actions.h" |
26 | | #include "sieve-validator.h" |
27 | | #include "sieve-generator.h" |
28 | | #include "sieve-interpreter.h" |
29 | | #include "sieve-dump.h" |
30 | | #include "sieve-result.h" |
31 | | #include "sieve-message.h" |
32 | | #include "sieve-smtp.h" |
33 | | |
34 | | #include "ext-vacation-common.h" |
35 | | |
36 | | #include <stdio.h> |
37 | | |
38 | | /* |
39 | | * Forward declarations |
40 | | */ |
41 | | |
42 | | static const struct sieve_argument_def vacation_days_tag; |
43 | | static const struct sieve_argument_def vacation_subject_tag; |
44 | | static const struct sieve_argument_def vacation_from_tag; |
45 | | static const struct sieve_argument_def vacation_addresses_tag; |
46 | | static const struct sieve_argument_def vacation_mime_tag; |
47 | | static const struct sieve_argument_def vacation_handle_tag; |
48 | | |
49 | | /* |
50 | | * Vacation command |
51 | | * |
52 | | * Syntax: |
53 | | * vacation [":days" number] [":subject" string] |
54 | | * [":from" string] [":addresses" string-list] |
55 | | * [":mime"] [":handle" string] <reason: string> |
56 | | */ |
57 | | |
58 | | static bool |
59 | | cmd_vacation_registered(struct sieve_validator *valdtr, |
60 | | const struct sieve_extension *ext, |
61 | | struct sieve_command_registration *cmd_reg); |
62 | | static bool |
63 | | cmd_vacation_pre_validate(struct sieve_validator *valdtr, |
64 | | struct sieve_command *cmd); |
65 | | static bool |
66 | | cmd_vacation_validate(struct sieve_validator *valdtr, |
67 | | struct sieve_command *cmd); |
68 | | static bool |
69 | | cmd_vacation_generate(const struct sieve_codegen_env *cgenv, |
70 | | struct sieve_command *cmd); |
71 | | |
72 | | const struct sieve_command_def vacation_command = { |
73 | | .identifier = "vacation", |
74 | | .type = SCT_COMMAND, |
75 | | .positional_args = 1, |
76 | | .subtests = 0, |
77 | | .block_allowed = FALSE, |
78 | | .block_required = FALSE, |
79 | | .registered = cmd_vacation_registered, |
80 | | .pre_validate = cmd_vacation_pre_validate, |
81 | | .validate = cmd_vacation_validate, |
82 | | .generate = cmd_vacation_generate, |
83 | | }; |
84 | | |
85 | | /* |
86 | | * Vacation command tags |
87 | | */ |
88 | | |
89 | | /* Forward declarations */ |
90 | | |
91 | | static bool |
92 | | cmd_vacation_validate_number_tag(struct sieve_validator *valdtr, |
93 | | struct sieve_ast_argument **arg, |
94 | | struct sieve_command *cmd); |
95 | | static bool |
96 | | cmd_vacation_validate_string_tag(struct sieve_validator *valdtr, |
97 | | struct sieve_ast_argument **arg, |
98 | | struct sieve_command *cmd); |
99 | | static bool |
100 | | cmd_vacation_validate_stringlist_tag(struct sieve_validator *valdtr, |
101 | | struct sieve_ast_argument **arg, |
102 | | struct sieve_command *cmd); |
103 | | static bool |
104 | | cmd_vacation_validate_mime_tag(struct sieve_validator *valdtr, |
105 | | struct sieve_ast_argument **arg, |
106 | | struct sieve_command *cmd); |
107 | | |
108 | | /* Argument objects */ |
109 | | |
110 | | static const struct sieve_argument_def vacation_days_tag = { |
111 | | .identifier = "days", |
112 | | .validate = cmd_vacation_validate_number_tag, |
113 | | }; |
114 | | |
115 | | static const struct sieve_argument_def vacation_seconds_tag = { |
116 | | .identifier = "seconds", |
117 | | .validate = cmd_vacation_validate_number_tag, |
118 | | }; |
119 | | |
120 | | static const struct sieve_argument_def vacation_subject_tag = { |
121 | | .identifier = "subject", |
122 | | .validate = cmd_vacation_validate_string_tag, |
123 | | }; |
124 | | |
125 | | static const struct sieve_argument_def vacation_from_tag = { |
126 | | .identifier = "from", |
127 | | .validate = cmd_vacation_validate_string_tag, |
128 | | }; |
129 | | |
130 | | static const struct sieve_argument_def vacation_addresses_tag = { |
131 | | .identifier = "addresses", |
132 | | .validate = cmd_vacation_validate_stringlist_tag, |
133 | | }; |
134 | | |
135 | | static const struct sieve_argument_def vacation_mime_tag = { |
136 | | .identifier = "mime", |
137 | | .validate = cmd_vacation_validate_mime_tag, |
138 | | }; |
139 | | |
140 | | static const struct sieve_argument_def vacation_handle_tag = { |
141 | | .identifier = "handle", |
142 | | .validate = cmd_vacation_validate_string_tag, |
143 | | }; |
144 | | |
145 | | /* Codes for optional arguments */ |
146 | | |
147 | | enum cmd_vacation_optional { |
148 | | OPT_END, |
149 | | OPT_SECONDS, |
150 | | OPT_SUBJECT, |
151 | | OPT_FROM, |
152 | | OPT_ADDRESSES, |
153 | | OPT_MIME, |
154 | | }; |
155 | | |
156 | | /* |
157 | | * Vacation operation |
158 | | */ |
159 | | |
160 | | static bool |
161 | | ext_vacation_operation_dump(const struct sieve_dumptime_env *denv, |
162 | | sieve_size_t *address); |
163 | | static int |
164 | | ext_vacation_operation_execute(const struct sieve_runtime_env *renv, |
165 | | sieve_size_t *address); |
166 | | |
167 | | const struct sieve_operation_def vacation_operation = { |
168 | | .mnemonic = "VACATION", |
169 | | .ext_def = &vacation_extension, |
170 | | .dump = ext_vacation_operation_dump, |
171 | | .execute = ext_vacation_operation_execute, |
172 | | }; |
173 | | |
174 | | /* |
175 | | * Vacation action |
176 | | */ |
177 | | |
178 | | /* Forward declarations */ |
179 | | |
180 | | static int |
181 | | act_vacation_check_duplicate(const struct sieve_runtime_env *renv, |
182 | | const struct sieve_action *act, |
183 | | const struct sieve_action *act_other); |
184 | | int act_vacation_check_conflict(const struct sieve_runtime_env *renv, |
185 | | const struct sieve_action *act, |
186 | | const struct sieve_action *act_other); |
187 | | static void |
188 | | act_vacation_print(const struct sieve_action *action, |
189 | | const struct sieve_result_print_env *rpenv, bool *keep); |
190 | | static int |
191 | | act_vacation_commit(const struct sieve_action_exec_env *aenv, void *tr_context); |
192 | | |
193 | | /* Action object */ |
194 | | |
195 | | const struct sieve_action_def act_vacation = { |
196 | | .name = "vacation", |
197 | | .flags = SIEVE_ACTFLAG_SENDS_RESPONSE, |
198 | | .check_duplicate = act_vacation_check_duplicate, |
199 | | .check_conflict = act_vacation_check_conflict, |
200 | | .print = act_vacation_print, |
201 | | .commit = act_vacation_commit, |
202 | | }; |
203 | | |
204 | | /* Action context information */ |
205 | | |
206 | | struct act_vacation_context { |
207 | | const char *reason; |
208 | | |
209 | | sieve_number_t seconds; |
210 | | const char *subject; |
211 | | const char *handle; |
212 | | bool mime; |
213 | | const char *from; |
214 | | const struct smtp_address *from_address; |
215 | | const struct smtp_address *const *addresses; |
216 | | }; |
217 | | |
218 | | /* |
219 | | * Command validation context |
220 | | */ |
221 | | |
222 | | struct cmd_vacation_context_data { |
223 | | string_t *from; |
224 | | string_t *subject; |
225 | | |
226 | | bool mime; |
227 | | |
228 | | struct sieve_ast_argument *handle_arg; |
229 | | }; |
230 | | |
231 | | /* |
232 | | * Tag validation |
233 | | */ |
234 | | |
235 | | static bool |
236 | | cmd_vacation_validate_number_tag(struct sieve_validator *valdtr, |
237 | | struct sieve_ast_argument **arg, |
238 | | struct sieve_command *cmd) |
239 | 0 | { |
240 | 0 | const struct sieve_extension *ext = sieve_argument_ext(*arg); |
241 | 0 | const struct ext_vacation_context *extctx = ext->context; |
242 | 0 | struct sieve_ast_argument *tag = *arg; |
243 | 0 | sieve_number_t period, seconds; |
244 | | |
245 | | /* Detach the tag itself */ |
246 | 0 | *arg = sieve_ast_arguments_detach(*arg,1); |
247 | | |
248 | | /* Check syntax: |
249 | | * :days number |
250 | | */ |
251 | 0 | if (!sieve_validate_tag_parameter(valdtr, cmd, tag, *arg, NULL, 0, |
252 | 0 | SAAT_NUMBER, FALSE)) |
253 | 0 | return FALSE; |
254 | | |
255 | 0 | period = sieve_ast_argument_number(*arg); |
256 | 0 | if (sieve_argument_is(tag, vacation_days_tag)) |
257 | 0 | seconds = period * (24*60*60); |
258 | 0 | else if (sieve_argument_is(tag, vacation_seconds_tag)) |
259 | 0 | seconds = period; |
260 | 0 | else |
261 | 0 | i_unreached(); |
262 | | |
263 | 0 | i_assert(extctx->set->max_period > 0); |
264 | | |
265 | | /* Enforce :seconds >= min_period */ |
266 | 0 | if (seconds < extctx->set->min_period) { |
267 | 0 | seconds = extctx->set->min_period; |
268 | |
|
269 | 0 | sieve_argument_validate_warning( |
270 | 0 | valdtr, *arg, |
271 | 0 | "specified :%s value '%llu' is under the minimum", |
272 | 0 | sieve_argument_identifier(tag), |
273 | 0 | (unsigned long long)period); |
274 | | /* Enforce :days <= max_period */ |
275 | 0 | } else if (seconds > extctx->set->max_period) { |
276 | 0 | seconds = extctx->set->max_period; |
277 | |
|
278 | 0 | sieve_argument_validate_warning( |
279 | 0 | valdtr, *arg, |
280 | 0 | "specified :%s value '%llu' is over the maximum", |
281 | 0 | sieve_argument_identifier(tag), |
282 | 0 | (unsigned long long)period); |
283 | 0 | } |
284 | |
|
285 | 0 | sieve_ast_argument_number_set(*arg, seconds); |
286 | | |
287 | | /* Skip parameter */ |
288 | 0 | *arg = sieve_ast_argument_next(*arg); |
289 | |
|
290 | 0 | return TRUE; |
291 | 0 | } |
292 | | |
293 | | static bool |
294 | | cmd_vacation_validate_string_tag(struct sieve_validator *valdtr, |
295 | | struct sieve_ast_argument **arg, |
296 | | struct sieve_command *cmd) |
297 | 0 | { |
298 | 0 | struct sieve_ast_argument *tag = *arg; |
299 | 0 | struct cmd_vacation_context_data *ctx_data = |
300 | 0 | (struct cmd_vacation_context_data *)cmd->data; |
301 | | |
302 | | /* Detach the tag itself */ |
303 | 0 | *arg = sieve_ast_arguments_detach(*arg,1); |
304 | | |
305 | | /* Check syntax: |
306 | | * :subject string |
307 | | * :from string |
308 | | * :handle string |
309 | | */ |
310 | 0 | if (!sieve_validate_tag_parameter(valdtr, cmd, tag, *arg, NULL, 0, |
311 | 0 | SAAT_STRING, FALSE)) |
312 | 0 | return FALSE; |
313 | | |
314 | 0 | if (sieve_argument_is(tag, vacation_from_tag)) { |
315 | 0 | if (sieve_argument_is_string_literal(*arg)) { |
316 | 0 | string_t *address = sieve_ast_argument_str(*arg); |
317 | 0 | const char *error; |
318 | 0 | bool result; |
319 | |
|
320 | 0 | T_BEGIN { |
321 | 0 | result = sieve_address_validate_str(address, |
322 | 0 | &error); |
323 | |
|
324 | 0 | if (!result) { |
325 | 0 | sieve_argument_validate_error( |
326 | 0 | valdtr, *arg, |
327 | 0 | "specified :from address '%s' is invalid for vacation action: %s", |
328 | 0 | str_sanitize(str_c(address), 128), |
329 | 0 | error); |
330 | 0 | } |
331 | 0 | } T_END; |
332 | | |
333 | 0 | if (!result) |
334 | 0 | return FALSE; |
335 | 0 | } |
336 | | |
337 | 0 | ctx_data->from = sieve_ast_argument_str(*arg); |
338 | | |
339 | | /* Skip parameter */ |
340 | 0 | *arg = sieve_ast_argument_next(*arg); |
341 | 0 | } else if (sieve_argument_is(tag, vacation_subject_tag)) { |
342 | 0 | ctx_data->subject = sieve_ast_argument_str(*arg); |
343 | | |
344 | | /* Skip parameter */ |
345 | 0 | *arg = sieve_ast_argument_next(*arg); |
346 | 0 | } else if (sieve_argument_is(tag, vacation_handle_tag)) { |
347 | 0 | ctx_data->handle_arg = *arg; |
348 | | |
349 | | /* Detach optional argument (emitted as mandatory) */ |
350 | 0 | *arg = sieve_ast_arguments_detach(*arg, 1); |
351 | 0 | } |
352 | 0 | return TRUE; |
353 | 0 | } |
354 | | |
355 | | static bool |
356 | | cmd_vacation_validate_stringlist_tag(struct sieve_validator *valdtr, |
357 | | struct sieve_ast_argument **arg, |
358 | | struct sieve_command *cmd) |
359 | 0 | { |
360 | 0 | struct sieve_ast_argument *tag = *arg; |
361 | | |
362 | | /* Detach the tag itself */ |
363 | 0 | *arg = sieve_ast_arguments_detach(*arg,1); |
364 | | |
365 | | /* Check syntax: |
366 | | * :addresses string-list |
367 | | */ |
368 | 0 | if (!sieve_validate_tag_parameter(valdtr, cmd, tag, *arg, NULL, 0, |
369 | 0 | SAAT_STRING_LIST, FALSE)) |
370 | 0 | return FALSE; |
371 | | |
372 | | /* Skip parameter */ |
373 | 0 | *arg = sieve_ast_argument_next(*arg); |
374 | |
|
375 | 0 | return TRUE; |
376 | 0 | } |
377 | | |
378 | | static bool |
379 | | cmd_vacation_validate_mime_tag(struct sieve_validator *valdtr ATTR_UNUSED, |
380 | | struct sieve_ast_argument **arg, |
381 | | struct sieve_command *cmd) |
382 | 0 | { |
383 | 0 | struct cmd_vacation_context_data *ctx_data = |
384 | 0 | (struct cmd_vacation_context_data *)cmd->data; |
385 | |
|
386 | 0 | ctx_data->mime = TRUE; |
387 | | |
388 | | /* Skip tag */ |
389 | 0 | *arg = sieve_ast_argument_next(*arg); |
390 | |
|
391 | 0 | return TRUE; |
392 | 0 | } |
393 | | |
394 | | /* |
395 | | * Command registration |
396 | | */ |
397 | | |
398 | | static bool |
399 | | cmd_vacation_registered(struct sieve_validator *valdtr, |
400 | | const struct sieve_extension *ext, |
401 | | struct sieve_command_registration *cmd_reg) |
402 | 0 | { |
403 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
404 | 0 | &vacation_days_tag, OPT_SECONDS); |
405 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
406 | 0 | &vacation_subject_tag, OPT_SUBJECT); |
407 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
408 | 0 | &vacation_from_tag, OPT_FROM); |
409 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
410 | 0 | &vacation_addresses_tag, OPT_ADDRESSES); |
411 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
412 | 0 | &vacation_mime_tag, OPT_MIME); |
413 | 0 | sieve_validator_register_tag(valdtr, cmd_reg, ext, |
414 | 0 | &vacation_handle_tag, 0); |
415 | 0 | return TRUE; |
416 | 0 | } |
417 | | |
418 | | bool ext_vacation_register_seconds_tag( |
419 | | struct sieve_validator *valdtr, |
420 | | const struct sieve_extension *vacation_ext) |
421 | 0 | { |
422 | 0 | sieve_validator_register_external_tag( |
423 | 0 | valdtr, vacation_command.identifier, vacation_ext, |
424 | 0 | &vacation_seconds_tag, OPT_SECONDS); |
425 | |
|
426 | 0 | return TRUE; |
427 | 0 | } |
428 | | |
429 | | /* |
430 | | * Command validation |
431 | | */ |
432 | | |
433 | | static bool |
434 | | cmd_vacation_pre_validate(struct sieve_validator *valdtr ATTR_UNUSED, |
435 | | struct sieve_command *cmd) |
436 | 0 | { |
437 | 0 | struct cmd_vacation_context_data *ctx_data; |
438 | | |
439 | | /* Assign context */ |
440 | 0 | ctx_data = p_new(sieve_command_pool(cmd), |
441 | 0 | struct cmd_vacation_context_data, 1); |
442 | 0 | cmd->data = ctx_data; |
443 | |
|
444 | 0 | return TRUE; |
445 | 0 | } |
446 | | |
447 | | static const char _handle_empty_subject[] = "<default-subject>"; |
448 | | static const char _handle_empty_from[] = "<default-from>"; |
449 | | static const char _handle_mime_enabled[] = "<MIME>"; |
450 | | static const char _handle_mime_disabled[] = "<NO-MIME>"; |
451 | | |
452 | | static bool |
453 | | cmd_vacation_validate(struct sieve_validator *valdtr, |
454 | | struct sieve_command *cmd) |
455 | 0 | { |
456 | 0 | struct sieve_ast_argument *arg = cmd->first_positional; |
457 | 0 | struct cmd_vacation_context_data *ctx_data = |
458 | 0 | (struct cmd_vacation_context_data *)cmd->data; |
459 | |
|
460 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "reason", 1, |
461 | 0 | SAAT_STRING)) |
462 | 0 | return FALSE; |
463 | | |
464 | 0 | if (!sieve_validator_argument_activate(valdtr, cmd, arg, FALSE)) |
465 | 0 | return FALSE; |
466 | | |
467 | | /* Construct handle if not set explicitly */ |
468 | 0 | if (ctx_data->handle_arg == NULL) { |
469 | 0 | T_BEGIN { |
470 | 0 | string_t *handle; |
471 | 0 | string_t *reason = sieve_ast_argument_str(arg); |
472 | 0 | unsigned int size = str_len(reason); |
473 | | |
474 | | /* Precalculate the size of it all */ |
475 | 0 | size += (ctx_data->subject == NULL ? |
476 | 0 | sizeof(_handle_empty_subject) - 1 : |
477 | 0 | str_len(ctx_data->subject)); |
478 | 0 | size += (ctx_data->from == NULL ? |
479 | 0 | sizeof(_handle_empty_from) - 1 : |
480 | 0 | str_len(ctx_data->from)); |
481 | 0 | size += (ctx_data->mime ? |
482 | 0 | sizeof(_handle_mime_enabled) - 1 : |
483 | 0 | sizeof(_handle_mime_disabled) - 1); |
484 | | |
485 | | /* Construct the string */ |
486 | 0 | handle = t_str_new(size); |
487 | 0 | str_append_str(handle, reason); |
488 | |
|
489 | 0 | if (ctx_data->subject != NULL) |
490 | 0 | str_append_str(handle, ctx_data->subject); |
491 | 0 | else |
492 | 0 | str_append(handle, _handle_empty_subject); |
493 | |
|
494 | 0 | if (ctx_data->from != NULL) |
495 | 0 | str_append_str(handle, ctx_data->from); |
496 | 0 | else |
497 | 0 | str_append(handle, _handle_empty_from); |
498 | |
|
499 | 0 | str_append(handle, (ctx_data->mime ? |
500 | 0 | _handle_mime_enabled : |
501 | 0 | _handle_mime_disabled)); |
502 | | |
503 | | /* Create positional handle argument */ |
504 | 0 | ctx_data->handle_arg = |
505 | 0 | sieve_ast_argument_string_create( |
506 | 0 | cmd->ast_node, handle, |
507 | 0 | sieve_ast_node_line(cmd->ast_node)); |
508 | 0 | } T_END; |
509 | | |
510 | 0 | if (!sieve_validator_argument_activate( |
511 | 0 | valdtr, cmd, ctx_data->handle_arg, TRUE)) |
512 | 0 | return FALSE; |
513 | 0 | } else { |
514 | | /* Attach explicit handle argument as positional */ |
515 | 0 | (void)sieve_ast_argument_attach(cmd->ast_node, |
516 | 0 | ctx_data->handle_arg); |
517 | 0 | } |
518 | | |
519 | 0 | return TRUE; |
520 | 0 | } |
521 | | |
522 | | /* |
523 | | * Code generation |
524 | | */ |
525 | | |
526 | | static bool |
527 | | cmd_vacation_generate(const struct sieve_codegen_env *cgenv, |
528 | | struct sieve_command *cmd) |
529 | 0 | { |
530 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, &vacation_operation); |
531 | | |
532 | | /* Generate arguments */ |
533 | 0 | if (!sieve_generate_arguments(cgenv, cmd, NULL)) |
534 | 0 | return FALSE; |
535 | 0 | return TRUE; |
536 | 0 | } |
537 | | |
538 | | /* |
539 | | * Code dump |
540 | | */ |
541 | | |
542 | | static bool |
543 | | ext_vacation_operation_dump(const struct sieve_dumptime_env *denv, |
544 | | sieve_size_t *address) |
545 | 0 | { |
546 | 0 | int opt_code = 0; |
547 | |
|
548 | 0 | sieve_code_dumpf(denv, "VACATION"); |
549 | 0 | sieve_code_descend(denv); |
550 | | |
551 | | /* Dump optional operands */ |
552 | |
|
553 | 0 | for (;;) { |
554 | 0 | int opt; |
555 | 0 | bool opok = TRUE; |
556 | |
|
557 | 0 | if ((opt = sieve_opr_optional_dump(denv, address, |
558 | 0 | &opt_code)) < 0) |
559 | 0 | return FALSE; |
560 | | |
561 | 0 | if (opt == 0) |
562 | 0 | break; |
563 | | |
564 | 0 | switch (opt_code) { |
565 | 0 | case OPT_SECONDS: |
566 | 0 | opok = sieve_opr_number_dump(denv, address, "seconds"); |
567 | 0 | break; |
568 | 0 | case OPT_SUBJECT: |
569 | 0 | opok = sieve_opr_string_dump(denv, address, "subject"); |
570 | 0 | break; |
571 | 0 | case OPT_FROM: |
572 | 0 | opok = sieve_opr_string_dump(denv, address, "from"); |
573 | 0 | break; |
574 | 0 | case OPT_ADDRESSES: |
575 | 0 | opok = sieve_opr_stringlist_dump(denv, address, |
576 | 0 | "addresses"); |
577 | 0 | break; |
578 | 0 | case OPT_MIME: |
579 | 0 | sieve_code_dumpf(denv, "mime"); |
580 | 0 | break; |
581 | 0 | default: |
582 | 0 | return FALSE; |
583 | 0 | } |
584 | | |
585 | 0 | if (!opok) |
586 | 0 | return FALSE; |
587 | 0 | } |
588 | | |
589 | | /* Dump reason and handle operands */ |
590 | 0 | return (sieve_opr_string_dump(denv, address, "reason") && |
591 | 0 | sieve_opr_string_dump(denv, address, "handle")); |
592 | 0 | } |
593 | | |
594 | | /* |
595 | | * Code execution |
596 | | */ |
597 | | |
598 | | static int |
599 | | ext_vacation_operation_execute(const struct sieve_runtime_env *renv, |
600 | | sieve_size_t *address) |
601 | 0 | { |
602 | 0 | const struct sieve_extension *this_ext = renv->oprtn->ext; |
603 | 0 | const struct ext_vacation_context *extctx = this_ext->context; |
604 | 0 | struct sieve_side_effects_list *slist = NULL; |
605 | 0 | struct act_vacation_context *act; |
606 | 0 | pool_t pool; |
607 | 0 | int opt_code = 0; |
608 | 0 | sieve_number_t seconds = extctx->set->default_period; |
609 | 0 | bool mime = FALSE; |
610 | 0 | struct sieve_stringlist *addresses = NULL; |
611 | 0 | string_t *reason, *subject = NULL, *from = NULL, *handle = NULL; |
612 | 0 | const struct smtp_address *from_address = NULL; |
613 | 0 | int ret; |
614 | | |
615 | | /* |
616 | | * Read code |
617 | | */ |
618 | | |
619 | | /* Optional operands */ |
620 | |
|
621 | 0 | for (;;) { |
622 | 0 | int opt; |
623 | |
|
624 | 0 | if ((opt = sieve_opr_optional_read(renv, address, |
625 | 0 | &opt_code)) < 0) |
626 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
627 | | |
628 | 0 | if (opt == 0) |
629 | 0 | break; |
630 | | |
631 | 0 | switch (opt_code) { |
632 | 0 | case OPT_SECONDS: |
633 | 0 | ret = sieve_opr_number_read(renv, address, "seconds", |
634 | 0 | &seconds); |
635 | 0 | break; |
636 | 0 | case OPT_SUBJECT: |
637 | 0 | ret = sieve_opr_string_read(renv, address, "subject", |
638 | 0 | &subject); |
639 | 0 | break; |
640 | 0 | case OPT_FROM: |
641 | 0 | ret = sieve_opr_string_read(renv, address, "from", |
642 | 0 | &from); |
643 | 0 | break; |
644 | 0 | case OPT_ADDRESSES: |
645 | 0 | ret = sieve_opr_stringlist_read(renv, address, |
646 | 0 | "addresses", |
647 | 0 | &addresses); |
648 | 0 | break; |
649 | 0 | case OPT_MIME: |
650 | 0 | mime = TRUE; |
651 | 0 | ret = SIEVE_EXEC_OK; |
652 | 0 | break; |
653 | 0 | default: |
654 | 0 | sieve_runtime_trace_error( |
655 | 0 | renv, "unknown optional operand"); |
656 | 0 | ret = SIEVE_EXEC_BIN_CORRUPT; |
657 | 0 | } |
658 | | |
659 | 0 | if (ret <= 0) |
660 | 0 | return ret; |
661 | 0 | } |
662 | | |
663 | | /* Fixed operands */ |
664 | | |
665 | 0 | ret = sieve_opr_string_read(renv, address, "reason", &reason); |
666 | 0 | if (ret <= 0) |
667 | 0 | return ret; |
668 | 0 | ret = sieve_opr_string_read(renv, address, "handle", &handle); |
669 | 0 | if (ret <= 0) |
670 | 0 | return ret; |
671 | | |
672 | | /* |
673 | | * Perform operation |
674 | | */ |
675 | | |
676 | | /* Trace */ |
677 | | |
678 | 0 | if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_ACTIONS)) { |
679 | 0 | sieve_runtime_trace(renv, 0, "vacation action"); |
680 | 0 | sieve_runtime_trace_descend(renv); |
681 | 0 | sieve_runtime_trace(renv, 0, "auto-reply with message '%s'", |
682 | 0 | str_sanitize(str_c(reason), 80)); |
683 | 0 | } |
684 | | |
685 | | /* Parse :from address */ |
686 | 0 | if (from != NULL) { |
687 | 0 | const char *error; |
688 | |
|
689 | 0 | from_address = sieve_address_parse_str(from, &error); |
690 | 0 | if (from_address == NULL) { |
691 | 0 | sieve_runtime_error( |
692 | 0 | renv, NULL, |
693 | 0 | "specified :from address '%s' is invalid for vacation action: %s", |
694 | 0 | str_sanitize(str_c(from), 128), error); |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | /* Add vacation action to the result */ |
699 | |
|
700 | 0 | pool = sieve_result_pool(renv->result); |
701 | 0 | act = p_new(pool, struct act_vacation_context, 1); |
702 | 0 | act->reason = p_strdup(pool, str_c(reason)); |
703 | 0 | act->handle = p_strdup(pool, str_c(handle)); |
704 | 0 | act->seconds = seconds; |
705 | 0 | act->mime = mime; |
706 | 0 | if (subject != NULL) |
707 | 0 | act->subject = p_strdup(pool, str_c(subject)); |
708 | 0 | if (from != NULL) { |
709 | 0 | act->from = p_strdup(pool, str_c(from)); |
710 | 0 | act->from_address = smtp_address_clone(pool, from_address); |
711 | 0 | } |
712 | | |
713 | | /* Normalize all addresses */ |
714 | 0 | if (addresses != NULL) { |
715 | 0 | ARRAY_TYPE(smtp_address_const) addrs; |
716 | 0 | string_t *raw_address; |
717 | 0 | int ret; |
718 | |
|
719 | 0 | sieve_stringlist_reset(addresses); |
720 | |
|
721 | 0 | p_array_init(&addrs, pool, 4); |
722 | |
|
723 | 0 | raw_address = NULL; |
724 | 0 | while ((ret = sieve_stringlist_next_item(addresses, |
725 | 0 | &raw_address)) > 0) { |
726 | 0 | const struct smtp_address *addr; |
727 | 0 | const char *error; |
728 | |
|
729 | 0 | addr = sieve_address_parse_str(raw_address, &error); |
730 | 0 | if (addr != NULL) { |
731 | 0 | addr = smtp_address_clone(pool, addr); |
732 | 0 | array_append(&addrs, &addr, 1); |
733 | 0 | } else { |
734 | 0 | sieve_runtime_error( |
735 | 0 | renv, NULL, |
736 | 0 | "specified :addresses item '%s' is invalid: " |
737 | 0 | "%s for vacation action (ignored)", |
738 | 0 | str_sanitize(str_c(raw_address),128), |
739 | 0 | error); |
740 | 0 | } |
741 | 0 | } |
742 | |
|
743 | 0 | if (ret < 0) { |
744 | 0 | sieve_runtime_trace_error( |
745 | 0 | renv, "invalid addresses stringlist"); |
746 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
747 | 0 | } |
748 | | |
749 | 0 | (void)array_append_space(&addrs); |
750 | 0 | act->addresses = array_idx(&addrs, 0); |
751 | 0 | } |
752 | | |
753 | 0 | if (sieve_result_add_action(renv, this_ext, "vacation", &act_vacation, |
754 | 0 | slist, act, 0, FALSE) < 0) |
755 | 0 | return SIEVE_EXEC_FAILURE; |
756 | | |
757 | 0 | return SIEVE_EXEC_OK; |
758 | 0 | } |
759 | | |
760 | | /* |
761 | | * Action |
762 | | */ |
763 | | |
764 | | /* Runtime verification */ |
765 | | |
766 | | static int |
767 | | act_vacation_check_duplicate(const struct sieve_runtime_env *renv ATTR_UNUSED, |
768 | | const struct sieve_action *act, |
769 | | const struct sieve_action *act_other) |
770 | 0 | { |
771 | 0 | if (!sieve_action_is_executed(act_other, renv->result)) { |
772 | 0 | sieve_runtime_error( |
773 | 0 | renv, act->location, |
774 | 0 | "duplicate vacation action not allowed " |
775 | 0 | "(previously triggered one was here: %s)", |
776 | 0 | act_other->location); |
777 | 0 | return -1; |
778 | 0 | } |
779 | | |
780 | | /* Not an error if executed in preceeding script */ |
781 | 0 | return 1; |
782 | 0 | } |
783 | | |
784 | | int act_vacation_check_conflict(const struct sieve_runtime_env *renv, |
785 | | const struct sieve_action *act, |
786 | | const struct sieve_action *act_other) |
787 | 0 | { |
788 | 0 | if ((act_other->def->flags & SIEVE_ACTFLAG_SENDS_RESPONSE) > 0) { |
789 | 0 | if (!sieve_action_is_executed(act_other, renv->result)) { |
790 | 0 | sieve_runtime_error( |
791 | 0 | renv, act->location, |
792 | 0 | "vacation action conflicts with other action: " |
793 | 0 | "the %s action (%s) also sends a response back to the sender", |
794 | 0 | act_other->def->name, act_other->location); |
795 | 0 | return -1; |
796 | 0 | } else { |
797 | | /* Not an error if executed in preceeding script */ |
798 | 0 | return 1; |
799 | 0 | } |
800 | 0 | } |
801 | | |
802 | 0 | return 0; |
803 | 0 | } |
804 | | |
805 | | /* Result printing */ |
806 | | |
807 | | static void act_vacation_print(const struct sieve_action *action ATTR_UNUSED, |
808 | | const struct sieve_result_print_env *rpenv, |
809 | | bool *keep ATTR_UNUSED) |
810 | 0 | { |
811 | 0 | struct act_vacation_context *ctx = |
812 | 0 | (struct act_vacation_context *)action->context; |
813 | |
|
814 | 0 | sieve_result_action_printf(rpenv, "send vacation message:"); |
815 | 0 | sieve_result_printf(rpenv, " => seconds : %llu\n", |
816 | 0 | (unsigned long long)ctx->seconds); |
817 | 0 | if (ctx->subject != NULL) { |
818 | 0 | sieve_result_printf(rpenv, " => subject : %s\n", |
819 | 0 | ctx->subject); |
820 | 0 | } |
821 | 0 | if (ctx->from != NULL) { |
822 | 0 | sieve_result_printf(rpenv, " => from : %s\n", |
823 | 0 | ctx->from); |
824 | 0 | } |
825 | 0 | if (ctx->handle != NULL) { |
826 | 0 | sieve_result_printf(rpenv, " => handle : %s\n", |
827 | 0 | ctx->handle); |
828 | 0 | } |
829 | 0 | sieve_result_printf(rpenv, "\nSTART MESSAGE\n%s\nEND MESSAGE\n", |
830 | 0 | ctx->reason); |
831 | 0 | } |
832 | | |
833 | | /* Result execution */ |
834 | | |
835 | | /* Headers known to be associated with mailing lists |
836 | | */ |
837 | | static const char *const _list_headers[] = { |
838 | | "list-id", |
839 | | "list-owner", |
840 | | "list-subscribe", |
841 | | "list-post", |
842 | | "list-unsubscribe", |
843 | | "list-help", |
844 | | "list-archive", |
845 | | NULL |
846 | | }; |
847 | | |
848 | | /* Headers that should be searched for the user's own mail address(es) |
849 | | */ |
850 | | |
851 | | static const char *const _my_address_headers[] = { |
852 | | "to", |
853 | | "cc", |
854 | | "bcc", |
855 | | "resent-to", |
856 | | "resent-cc", |
857 | | "resent-bcc", |
858 | | NULL |
859 | | }; |
860 | | |
861 | | /* Headers that should be searched for the full sender address |
862 | | */ |
863 | | |
864 | | static const char *const _sender_headers[] = { |
865 | | "sender", |
866 | | "resent-from", |
867 | | "from", |
868 | | NULL |
869 | | }; |
870 | | |
871 | | static inline bool _is_system_address(const struct smtp_address *address) |
872 | 0 | { |
873 | 0 | if (strcasecmp(address->localpart, "MAILER-DAEMON") == 0) |
874 | 0 | return TRUE; |
875 | 0 | if (strcasecmp(address->localpart, "LISTSERV") == 0) |
876 | 0 | return TRUE; |
877 | 0 | if (strcasecmp(address->localpart, "majordomo") == 0) |
878 | 0 | return TRUE; |
879 | 0 | if (strstr(address->localpart, "-request") != NULL) |
880 | 0 | return TRUE; |
881 | 0 | if (str_begins_with(address->localpart, "owner-")) |
882 | 0 | return TRUE; |
883 | 0 | return FALSE; |
884 | 0 | } |
885 | | |
886 | | static bool |
887 | | _msg_address_equals(const struct message_address *addr1, |
888 | | const struct smtp_address *addr2) |
889 | 0 | { |
890 | 0 | struct smtp_address saddr; |
891 | |
|
892 | 0 | i_assert(addr1->mailbox != NULL); |
893 | 0 | return (smtp_address_init_from_msg(&saddr, addr1) >= 0 && |
894 | 0 | smtp_address_equals_icase(addr2, &saddr)); |
895 | 0 | } |
896 | | |
897 | | static inline bool |
898 | | _header_contains_my_address(const char *header_val, |
899 | | const struct smtp_address *my_address) |
900 | 0 | { |
901 | 0 | const struct message_address *msg_addr; |
902 | |
|
903 | 0 | msg_addr = message_address_parse(pool_datastack_create(), |
904 | 0 | (const unsigned char *)header_val, |
905 | 0 | strlen(header_val), 256, 0); |
906 | 0 | while (msg_addr != NULL) { |
907 | 0 | if (msg_addr->domain != NULL) { |
908 | 0 | if (_msg_address_equals(msg_addr, my_address)) |
909 | 0 | return TRUE; |
910 | 0 | } |
911 | | |
912 | 0 | msg_addr = msg_addr->next; |
913 | 0 | } |
914 | | |
915 | 0 | return FALSE; |
916 | 0 | } |
917 | | |
918 | | static inline bool |
919 | | _contains_my_address(const char *const *headers, |
920 | | const struct smtp_address *my_address) |
921 | 0 | { |
922 | 0 | const char *const *hdsp = headers; |
923 | |
|
924 | 0 | while (*hdsp != NULL) { |
925 | 0 | bool result; |
926 | |
|
927 | 0 | T_BEGIN { |
928 | 0 | result = _header_contains_my_address(*hdsp, my_address); |
929 | 0 | } T_END; |
930 | | |
931 | 0 | if (result) |
932 | 0 | return TRUE; |
933 | | |
934 | 0 | hdsp++; |
935 | 0 | } |
936 | | |
937 | 0 | return FALSE; |
938 | 0 | } |
939 | | |
940 | | static bool _contains_8bit(const char *text) |
941 | 0 | { |
942 | 0 | const unsigned char *p = (const unsigned char *)text; |
943 | |
|
944 | 0 | for (; *p != '\0'; p++) { |
945 | 0 | if ((*p & 0x80) != 0) |
946 | 0 | return TRUE; |
947 | 0 | } |
948 | 0 | return FALSE; |
949 | 0 | } |
950 | | |
951 | | static bool |
952 | | _header_get_full_reply_recipient(const struct ext_vacation_context *extctx, |
953 | | const struct smtp_address *smtp_to, |
954 | | const char *header, |
955 | | struct message_address *reply_to_r) |
956 | 0 | { |
957 | 0 | const struct message_address *addr; |
958 | |
|
959 | 0 | addr = message_address_parse( |
960 | 0 | pool_datastack_create(), |
961 | 0 | (const unsigned char *)header, |
962 | 0 | strlen(header), 256, 0); |
963 | |
|
964 | 0 | for (; addr != NULL; addr = addr->next) { |
965 | 0 | bool matched = extctx->set->to_header_ignore_envelope; |
966 | |
|
967 | 0 | if (addr->domain == NULL || addr->invalid_syntax) |
968 | 0 | continue; |
969 | | |
970 | 0 | if (!matched) |
971 | 0 | matched = _msg_address_equals(addr, smtp_to); |
972 | |
|
973 | 0 | if (matched) { |
974 | 0 | *reply_to_r = *addr; |
975 | 0 | return TRUE; |
976 | 0 | } |
977 | 0 | } |
978 | 0 | return FALSE; |
979 | 0 | } |
980 | | |
981 | | static int |
982 | | _get_full_reply_recipient(const struct sieve_action_exec_env *aenv, |
983 | | const struct ext_vacation_context *extctx, |
984 | | const struct smtp_address *smtp_to, |
985 | | struct message_address *reply_to_r) |
986 | 0 | { |
987 | 0 | const struct sieve_execute_env *eenv = aenv->exec_env; |
988 | 0 | const struct sieve_message_data *msgdata = eenv->msgdata; |
989 | 0 | const char *const *hdsp; |
990 | 0 | int ret; |
991 | |
|
992 | 0 | hdsp = _sender_headers; |
993 | 0 | for (; *hdsp != NULL; hdsp++) { |
994 | 0 | const char *header; |
995 | |
|
996 | 0 | ret = mail_get_first_header(msgdata->mail, *hdsp, &header); |
997 | 0 | if (ret < 0) { |
998 | 0 | return sieve_result_mail_error( |
999 | 0 | aenv, msgdata->mail, |
1000 | 0 | "failed to read header field '%s'", *hdsp); |
1001 | 0 | } |
1002 | 0 | if (ret == 0 || header == NULL) |
1003 | 0 | continue; |
1004 | | |
1005 | 0 | if (_header_get_full_reply_recipient(extctx, smtp_to, |
1006 | 0 | header, reply_to_r)) |
1007 | 0 | return SIEVE_EXEC_OK; |
1008 | 0 | } |
1009 | | |
1010 | 0 | reply_to_r->mailbox = smtp_to->localpart; |
1011 | 0 | reply_to_r->domain = smtp_to->domain; |
1012 | 0 | return SIEVE_EXEC_OK; |
1013 | 0 | } |
1014 | | |
1015 | | static const struct var_expand_table * |
1016 | | _get_var_expand_table(const struct sieve_action_exec_env *aenv ATTR_UNUSED, |
1017 | | const char *subject) |
1018 | 0 | { |
1019 | 0 | const struct var_expand_table stack_tab[] = { |
1020 | 0 | { .key = "subject", .value = subject }, |
1021 | 0 | VAR_EXPAND_TABLE_END |
1022 | 0 | }; |
1023 | |
|
1024 | 0 | return p_memdup(unsafe_data_stack_pool, stack_tab, sizeof(stack_tab)); |
1025 | 0 | } |
1026 | | |
1027 | | static int |
1028 | | act_vacation_get_default_subject(const struct sieve_action_exec_env *aenv, |
1029 | | const struct ext_vacation_context *extctx, |
1030 | | const char **subject_r) |
1031 | 0 | { |
1032 | 0 | const struct sieve_execute_env *eenv = aenv->exec_env; |
1033 | 0 | const struct sieve_message_data *msgdata = eenv->msgdata; |
1034 | 0 | const char *header, *error; |
1035 | 0 | string_t *str; |
1036 | 0 | int ret; |
1037 | |
|
1038 | 0 | *subject_r = (*extctx->set->default_subject == '\0' ? |
1039 | 0 | "Automated reply" : extctx->set->default_subject); |
1040 | 0 | ret = mail_get_first_header_utf8(msgdata->mail, "subject", &header); |
1041 | 0 | if (ret < 0) { |
1042 | 0 | return sieve_result_mail_error( |
1043 | 0 | aenv, msgdata->mail, |
1044 | 0 | "failed to read header field 'subject'"); |
1045 | 0 | } |
1046 | 0 | if (ret == 0) |
1047 | 0 | return SIEVE_EXEC_OK; |
1048 | 0 | if (*extctx->set->default_subject_template == '\0') { |
1049 | 0 | *subject_r = t_strconcat("Auto: ", header, NULL); |
1050 | 0 | return SIEVE_EXEC_OK; |
1051 | 0 | } |
1052 | | |
1053 | 0 | str = t_str_new(256); |
1054 | 0 | const struct var_expand_params params = { |
1055 | 0 | .table = _get_var_expand_table(aenv, header), |
1056 | 0 | }; |
1057 | 0 | if (var_expand(str, extctx->set->default_subject_template, ¶ms, |
1058 | 0 | &error) < 0) { |
1059 | 0 | e_error(aenv->event, |
1060 | 0 | "Failed to expand deliver_log_format=%s: %s", |
1061 | 0 | extctx->set->default_subject_template, error); |
1062 | 0 | *subject_r = t_strconcat("Auto: ", header, NULL); |
1063 | 0 | return SIEVE_EXEC_OK; |
1064 | 0 | } |
1065 | | |
1066 | 0 | *subject_r = str_c(str); |
1067 | 0 | return SIEVE_EXEC_OK; |
1068 | 0 | } |
1069 | | |
1070 | | static int |
1071 | | act_vacation_send(const struct sieve_action_exec_env *aenv, |
1072 | | const struct ext_vacation_context *extctx, |
1073 | | struct act_vacation_context *actx, |
1074 | | const struct smtp_address *smtp_to, |
1075 | | const struct smtp_address *smtp_from, |
1076 | | const struct message_address *reply_from) |
1077 | 0 | { |
1078 | 0 | const struct sieve_execute_env *eenv = aenv->exec_env; |
1079 | 0 | const struct sieve_message_data *msgdata = eenv->msgdata; |
1080 | 0 | const struct sieve_script_env *senv = eenv->scriptenv; |
1081 | 0 | struct sieve_smtp_context *sctx; |
1082 | 0 | struct ostream *output; |
1083 | 0 | string_t *msg; |
1084 | 0 | struct message_address reply_to; |
1085 | 0 | const char *header, *outmsgid, *subject, *error; |
1086 | 0 | int ret; |
1087 | | |
1088 | | /* Check smpt functions just to be sure */ |
1089 | |
|
1090 | 0 | if (!sieve_smtp_available(senv)) { |
1091 | 0 | sieve_result_global_warning( |
1092 | 0 | aenv, "vacation action has no means to send mail"); |
1093 | 0 | return SIEVE_EXEC_OK; |
1094 | 0 | } |
1095 | | |
1096 | | /* Make sure we have a subject for our reply */ |
1097 | | |
1098 | 0 | if (actx->subject == NULL || *(actx->subject) == '\0') { |
1099 | 0 | ret = act_vacation_get_default_subject(aenv, extctx, &subject); |
1100 | 0 | if (ret <= 0) |
1101 | 0 | return ret; |
1102 | 0 | } else { |
1103 | 0 | subject = actx->subject; |
1104 | 0 | } |
1105 | | |
1106 | 0 | subject = str_sanitize_utf8( |
1107 | 0 | subject, SIEVE_MAX_SUBJECT_HEADER_CODEPOINTS); |
1108 | | |
1109 | | /* Obtain full To address for reply */ |
1110 | |
|
1111 | 0 | i_zero(&reply_to); |
1112 | 0 | reply_to.mailbox = smtp_to->localpart; |
1113 | 0 | reply_to.domain = smtp_to->domain; |
1114 | 0 | ret = _get_full_reply_recipient(aenv, extctx, smtp_to, &reply_to); |
1115 | 0 | if (ret <= 0) |
1116 | 0 | return ret; |
1117 | | |
1118 | | /* Open smtp session */ |
1119 | | |
1120 | 0 | sctx = sieve_smtp_start_single(senv, smtp_to, smtp_from, &output); |
1121 | |
|
1122 | 0 | outmsgid = sieve_message_get_new_id(eenv->svinst); |
1123 | | |
1124 | | /* Produce a proper reply */ |
1125 | |
|
1126 | 0 | msg = t_str_new(512); |
1127 | 0 | rfc2822_header_write(msg, "X-Sieve", SIEVE_IMPLEMENTATION); |
1128 | 0 | rfc2822_header_write(msg, "Message-ID", outmsgid); |
1129 | 0 | rfc2822_header_write(msg, "Date", message_date_create(ioloop_time)); |
1130 | |
|
1131 | 0 | if (actx->from != NULL && *(actx->from) != '\0') { |
1132 | 0 | rfc2822_header_write_address(msg, "From", actx->from); |
1133 | 0 | } else { |
1134 | 0 | if (reply_from == NULL || reply_from->mailbox == NULL || |
1135 | 0 | *reply_from->mailbox == '\0') |
1136 | 0 | reply_from = sieve_get_postmaster(senv); |
1137 | 0 | rfc2822_header_write( |
1138 | 0 | msg, "From", |
1139 | 0 | message_address_first_to_string(reply_from)); |
1140 | 0 | } |
1141 | |
|
1142 | 0 | rfc2822_header_write(msg, "To", |
1143 | 0 | message_address_first_to_string(&reply_to)); |
1144 | |
|
1145 | 0 | if (_contains_8bit(subject)) |
1146 | 0 | rfc2822_header_utf8_printf(msg, "Subject", "%s", subject); |
1147 | 0 | else |
1148 | 0 | rfc2822_header_printf(msg, "Subject", "%s", subject); |
1149 | | |
1150 | | /* Compose proper in-reply-to and references headers */ |
1151 | |
|
1152 | 0 | ret = mail_get_first_header(msgdata->mail, "references", &header); |
1153 | 0 | if (ret < 0) { |
1154 | 0 | sieve_smtp_abort(sctx); |
1155 | 0 | return sieve_result_mail_error( |
1156 | 0 | aenv, msgdata->mail, |
1157 | 0 | "failed to read header field 'references'"); |
1158 | 0 | } |
1159 | | |
1160 | 0 | if (msgdata->id != NULL) { |
1161 | 0 | rfc2822_header_write(msg, "In-Reply-To", msgdata->id); |
1162 | |
|
1163 | 0 | if (ret > 0 && header != NULL) { |
1164 | 0 | rfc2822_header_write( |
1165 | 0 | msg, "References", |
1166 | 0 | t_strconcat(header, " ", msgdata->id, NULL)); |
1167 | 0 | } else { |
1168 | 0 | rfc2822_header_write(msg, "References", msgdata->id); |
1169 | 0 | } |
1170 | 0 | } else if (ret > 0 && header != NULL) { |
1171 | 0 | rfc2822_header_write(msg, "References", header); |
1172 | 0 | } |
1173 | |
|
1174 | 0 | rfc2822_header_write(msg, "Auto-Submitted", "auto-replied (vacation)"); |
1175 | 0 | rfc2822_header_write(msg, "Precedence", "bulk"); |
1176 | | |
1177 | | /* Prevent older Microsoft products from replying to this message */ |
1178 | 0 | rfc2822_header_write(msg, "X-Auto-Response-Suppress", "All"); |
1179 | |
|
1180 | 0 | rfc2822_header_write(msg, "MIME-Version", "1.0"); |
1181 | |
|
1182 | 0 | if (!actx->mime) { |
1183 | 0 | rfc2822_header_write(msg, "Content-Type", |
1184 | 0 | "text/plain; charset=utf-8"); |
1185 | 0 | rfc2822_header_write(msg, "Content-Transfer-Encoding", "8bit"); |
1186 | 0 | str_append(msg, "\r\n"); |
1187 | 0 | } |
1188 | |
|
1189 | 0 | str_printfa(msg, "%s\r\n", actx->reason); |
1190 | 0 | o_stream_nsend(output, str_data(msg), str_len(msg)); |
1191 | | |
1192 | | /* Close smtp session */ |
1193 | 0 | ret = sieve_smtp_finish(sctx, &error); |
1194 | 0 | if (ret <= 0) { |
1195 | 0 | if (ret < 0) { |
1196 | 0 | sieve_result_global_error( |
1197 | 0 | aenv, "failed to send vacation response to %s: " |
1198 | 0 | "<%s> (temporary error)", |
1199 | 0 | smtp_address_encode(smtp_to), |
1200 | 0 | str_sanitize(error, 512)); |
1201 | 0 | } else { |
1202 | 0 | sieve_result_global_log_error( |
1203 | 0 | aenv, "failed to send vacation response to %s: " |
1204 | 0 | "<%s> (permanent error)", |
1205 | 0 | smtp_address_encode(smtp_to), |
1206 | 0 | str_sanitize(error, 512)); |
1207 | 0 | } |
1208 | | /* This error will be ignored in the end */ |
1209 | 0 | return SIEVE_EXEC_FAILURE; |
1210 | 0 | } |
1211 | | |
1212 | 0 | eenv->exec_status->significant_action_executed = TRUE; |
1213 | 0 | return SIEVE_EXEC_OK; |
1214 | 0 | } |
1215 | | |
1216 | | static void |
1217 | | act_vacation_hash(struct act_vacation_context *vctx, const char *sender, |
1218 | | unsigned char hash_r[]) |
1219 | 0 | { |
1220 | 0 | const char *rpath = t_str_lcase(sender); |
1221 | 0 | struct md5_context ctx; |
1222 | |
|
1223 | 0 | md5_init(&ctx); |
1224 | 0 | md5_update(&ctx, rpath, strlen(rpath)); |
1225 | |
|
1226 | 0 | md5_update(&ctx, vctx->handle, strlen(vctx->handle)); |
1227 | |
|
1228 | 0 | md5_final(&ctx, hash_r); |
1229 | 0 | } |
1230 | | |
1231 | | static int |
1232 | | act_vacation_commit(const struct sieve_action_exec_env *aenv, |
1233 | | void *tr_context ATTR_UNUSED) |
1234 | 0 | { |
1235 | 0 | const struct sieve_action *action = aenv->action; |
1236 | 0 | const struct sieve_extension *ext = action->ext; |
1237 | 0 | const struct sieve_execute_env *eenv = aenv->exec_env; |
1238 | 0 | struct sieve_instance *svinst = eenv->svinst; |
1239 | 0 | const struct ext_vacation_context *extctx = ext->context; |
1240 | 0 | struct act_vacation_context *actx = action->context; |
1241 | 0 | unsigned char dupl_hash[MD5_RESULTLEN]; |
1242 | 0 | struct mail *mail = sieve_message_get_mail(aenv->msgctx); |
1243 | 0 | const struct smtp_address *sender, *recipient; |
1244 | 0 | const struct smtp_address *orig_recipient, *user_email; |
1245 | 0 | const struct smtp_address *smtp_from; |
1246 | 0 | struct message_address reply_from; |
1247 | 0 | const char *const *hdsp, *const *headers; |
1248 | 0 | int ret; |
1249 | |
|
1250 | 0 | if ((eenv->flags & SIEVE_EXECUTE_FLAG_SKIP_RESPONSES) != 0) { |
1251 | 0 | sieve_result_global_log( |
1252 | 0 | aenv, "not sending vacation reply (skipped)"); |
1253 | 0 | return SIEVE_EXEC_OK; |
1254 | 0 | } |
1255 | | |
1256 | 0 | sender = sieve_message_get_sender(aenv->msgctx); |
1257 | 0 | recipient = sieve_message_get_final_recipient(aenv->msgctx); |
1258 | |
|
1259 | 0 | i_zero(&reply_from); |
1260 | 0 | smtp_from = orig_recipient = user_email = NULL; |
1261 | | |
1262 | | /* Is the recipient unset? |
1263 | | */ |
1264 | 0 | if (smtp_address_isnull(recipient)) { |
1265 | 0 | sieve_result_global_warning( |
1266 | 0 | aenv, "vacation action aborted: " |
1267 | 0 | "envelope recipient is <>"); |
1268 | 0 | return SIEVE_EXEC_OK; |
1269 | 0 | } |
1270 | | |
1271 | | /* Is the return path unset ? |
1272 | | */ |
1273 | 0 | if (smtp_address_isnull(sender)) { |
1274 | 0 | sieve_result_global_log(aenv, "discarded vacation reply to <>"); |
1275 | 0 | return SIEVE_EXEC_OK; |
1276 | 0 | } |
1277 | | |
1278 | | /* Are we perhaps trying to respond to ourselves ? |
1279 | | */ |
1280 | 0 | if (smtp_address_equals_icase(sender, recipient)) { |
1281 | 0 | sieve_result_global_log( |
1282 | 0 | aenv, "discarded vacation reply to own address <%s>", |
1283 | 0 | smtp_address_encode(sender)); |
1284 | 0 | return SIEVE_EXEC_OK; |
1285 | 0 | } |
1286 | | |
1287 | | /* Are we perhaps trying to respond to one of our alternative :addresses? |
1288 | | */ |
1289 | 0 | if (actx->addresses != NULL) { |
1290 | 0 | const struct smtp_address *const *alt_address; |
1291 | |
|
1292 | 0 | alt_address = actx->addresses; |
1293 | 0 | while (*alt_address != NULL) { |
1294 | 0 | if (smtp_address_equals_icase(sender, *alt_address)) { |
1295 | 0 | sieve_result_global_log( |
1296 | 0 | aenv, |
1297 | 0 | "discarded vacation reply to own address <%s> " |
1298 | 0 | "(as specified using :addresses argument)", |
1299 | 0 | smtp_address_encode(sender)); |
1300 | 0 | return SIEVE_EXEC_OK; |
1301 | 0 | } |
1302 | 0 | alt_address++; |
1303 | 0 | } |
1304 | 0 | } |
1305 | | |
1306 | | /* Did whe respond to this user before? */ |
1307 | 0 | if (sieve_action_duplicate_check_available(aenv)) { |
1308 | 0 | bool duplicate; |
1309 | |
|
1310 | 0 | act_vacation_hash(actx, smtp_address_encode(sender), dupl_hash); |
1311 | |
|
1312 | 0 | ret = sieve_action_duplicate_check(aenv, dupl_hash, |
1313 | 0 | sizeof(dupl_hash), |
1314 | 0 | &duplicate); |
1315 | 0 | if (ret < SIEVE_EXEC_OK) { |
1316 | 0 | sieve_result_critical( |
1317 | 0 | aenv, "failed to check for duplicate vacation response", |
1318 | 0 | "failed to check for duplicate vacation response%s", |
1319 | 0 | (ret == SIEVE_EXEC_TEMP_FAILURE ? |
1320 | 0 | " (temporaty failure)" : "")); |
1321 | 0 | return ret; |
1322 | 0 | } |
1323 | 0 | if (duplicate) { |
1324 | 0 | sieve_result_global_log( |
1325 | 0 | aenv, |
1326 | 0 | "discarded duplicate vacation response to <%s>", |
1327 | 0 | smtp_address_encode(sender)); |
1328 | 0 | return SIEVE_EXEC_OK; |
1329 | 0 | } |
1330 | 0 | } |
1331 | | |
1332 | | /* Are we trying to respond to a mailing list ? */ |
1333 | 0 | hdsp = _list_headers; |
1334 | 0 | while (*hdsp != NULL) { |
1335 | 0 | ret = mail_get_headers(mail, *hdsp, &headers); |
1336 | 0 | if (ret < 0) { |
1337 | 0 | return sieve_result_mail_error( |
1338 | 0 | aenv, mail, |
1339 | 0 | "failed to read header field '%s'", *hdsp); |
1340 | 0 | } |
1341 | | |
1342 | 0 | if (ret > 0 && headers[0] != NULL) { |
1343 | | /* Yes, bail out */ |
1344 | 0 | sieve_result_global_log( |
1345 | 0 | aenv, "discarding vacation response " |
1346 | 0 | "to mailinglist recipient <%s>", |
1347 | 0 | smtp_address_encode(sender)); |
1348 | 0 | return SIEVE_EXEC_OK; |
1349 | 0 | } |
1350 | 0 | hdsp++; |
1351 | 0 | } |
1352 | | |
1353 | | /* Is the message that we are replying to an automatic reply ? */ |
1354 | 0 | ret = mail_get_headers(mail, "auto-submitted", &headers); |
1355 | 0 | if (ret < 0) { |
1356 | 0 | return sieve_result_mail_error( |
1357 | 0 | aenv, mail, |
1358 | 0 | "failed to read header field 'auto-submitted'"); |
1359 | 0 | } |
1360 | | /* Theoretically multiple headers could exist, so lets make sure */ |
1361 | 0 | if (ret > 0) { |
1362 | 0 | hdsp = headers; |
1363 | 0 | while (*hdsp != NULL) { |
1364 | 0 | if (strcasecmp(*hdsp, "no") != 0) { |
1365 | 0 | sieve_result_global_log( |
1366 | 0 | aenv, "discarding vacation response " |
1367 | 0 | "to auto-submitted message from <%s>", |
1368 | 0 | smtp_address_encode(sender)); |
1369 | 0 | return SIEVE_EXEC_OK; |
1370 | 0 | } |
1371 | 0 | hdsp++; |
1372 | 0 | } |
1373 | 0 | } |
1374 | | |
1375 | | /* Check for the (non-standard) precedence header */ |
1376 | 0 | ret = mail_get_headers(mail, "precedence", &headers); |
1377 | 0 | if (ret < 0) { |
1378 | 0 | return sieve_result_mail_error( |
1379 | 0 | aenv, mail, "failed to read header field 'precedence'"); |
1380 | 0 | } |
1381 | | /* Theoretically multiple headers could exist, so lets make sure */ |
1382 | 0 | if (ret > 0) { |
1383 | 0 | hdsp = headers; |
1384 | 0 | while (*hdsp != NULL) { |
1385 | 0 | if (strcasecmp(*hdsp, "junk") == 0 || |
1386 | 0 | strcasecmp(*hdsp, "bulk") == 0 || |
1387 | 0 | strcasecmp(*hdsp, "list") == 0) { |
1388 | 0 | sieve_result_global_log( |
1389 | 0 | aenv, "discarding vacation response " |
1390 | 0 | "to precedence=%s message from <%s>", |
1391 | 0 | *hdsp, smtp_address_encode(sender)); |
1392 | 0 | return SIEVE_EXEC_OK; |
1393 | 0 | } |
1394 | 0 | hdsp++; |
1395 | 0 | } |
1396 | 0 | } |
1397 | | |
1398 | | /* Check for the (non-standard) Microsoft X-Auto-Response-Suppress header */ |
1399 | 0 | ret = mail_get_headers(mail, "x-auto-response-suppress", &headers); |
1400 | 0 | if (ret < 0) { |
1401 | 0 | return sieve_result_mail_error( |
1402 | 0 | aenv, mail, |
1403 | 0 | "failed to read header field 'x-auto-response-suppress'"); |
1404 | 0 | } |
1405 | | /* Theoretically multiple headers could exist, so lets make sure */ |
1406 | 0 | if (ret > 0) { |
1407 | 0 | hdsp = headers; |
1408 | 0 | while (*hdsp != NULL) { |
1409 | 0 | const char *const *flags = t_strsplit(*hdsp, ","); |
1410 | |
|
1411 | 0 | while (*flags != NULL) { |
1412 | 0 | const char *flag = t_str_trim(*flags, " \t"); |
1413 | |
|
1414 | 0 | if (strcasecmp(flag, "All") == 0 || |
1415 | 0 | strcasecmp(flag, "OOF") == 0) { |
1416 | 0 | sieve_result_global_log( |
1417 | 0 | aenv, "discarding vacation response to message from <%s> " |
1418 | 0 | "('%s' flag found in x-auto-response-suppress header)", |
1419 | 0 | smtp_address_encode(sender), flag); |
1420 | 0 | return SIEVE_EXEC_OK; |
1421 | 0 | } |
1422 | 0 | flags++; |
1423 | 0 | } |
1424 | 0 | hdsp++; |
1425 | 0 | } |
1426 | 0 | } |
1427 | | |
1428 | | /* Do not reply to system addresses */ |
1429 | 0 | if (_is_system_address(sender)) { |
1430 | 0 | sieve_result_global_log( |
1431 | 0 | aenv, "not sending vacation response to system address <%s>", |
1432 | 0 | smtp_address_encode(sender)); |
1433 | 0 | return SIEVE_EXEC_OK; |
1434 | 0 | } |
1435 | | |
1436 | | /* Fetch original recipient if necessary */ |
1437 | 0 | if (extctx->set->use_original_recipient) |
1438 | 0 | orig_recipient = sieve_message_get_orig_recipient(aenv->msgctx); |
1439 | | /* Fetch explicitly configured user email address */ |
1440 | 0 | if (svinst->set->parsed.user_email != NULL) |
1441 | 0 | user_email = svinst->set->parsed.user_email; |
1442 | | |
1443 | | /* Is the original message directly addressed to the user or the addresses |
1444 | | * specified using the :addresses tag? |
1445 | | */ |
1446 | 0 | hdsp = _my_address_headers; |
1447 | 0 | while (*hdsp != NULL) { |
1448 | 0 | ret = mail_get_headers(mail, *hdsp, &headers); |
1449 | 0 | if (ret < 0) { |
1450 | 0 | return sieve_result_mail_error( |
1451 | 0 | aenv, mail, "failed to read header field '%s'", |
1452 | 0 | *hdsp); |
1453 | 0 | } |
1454 | 0 | if (ret > 0 && headers[0] != NULL) { |
1455 | | /* Final recipient directly listed in headers? */ |
1456 | 0 | if (_contains_my_address(headers, recipient)) { |
1457 | 0 | smtp_from = recipient; |
1458 | 0 | message_address_init_from_smtp( |
1459 | 0 | &reply_from, NULL, recipient); |
1460 | 0 | break; |
1461 | 0 | } |
1462 | | |
1463 | | /* Original recipient directly listed in headers? */ |
1464 | 0 | if (!smtp_address_isnull(orig_recipient) && |
1465 | 0 | _contains_my_address(headers, orig_recipient)) { |
1466 | 0 | smtp_from = orig_recipient; |
1467 | 0 | message_address_init_from_smtp( |
1468 | 0 | &reply_from, NULL, orig_recipient); |
1469 | 0 | break; |
1470 | 0 | } |
1471 | | |
1472 | | /* User-provided :addresses listed in headers? */ |
1473 | 0 | if (actx->addresses != NULL) { |
1474 | 0 | bool found = FALSE; |
1475 | 0 | const struct smtp_address *const *my_address; |
1476 | |
|
1477 | 0 | my_address = actx->addresses; |
1478 | 0 | while (!found && *my_address != NULL) { |
1479 | 0 | if ((found = _contains_my_address(headers, *my_address))) { |
1480 | | /* Avoid letting user determine SMTP sender directly */ |
1481 | 0 | smtp_from = (orig_recipient == NULL ? |
1482 | 0 | recipient : orig_recipient); |
1483 | 0 | message_address_init_from_smtp( |
1484 | 0 | &reply_from, NULL, *my_address); |
1485 | 0 | } |
1486 | 0 | my_address++; |
1487 | 0 | } |
1488 | |
|
1489 | 0 | if (found) break; |
1490 | 0 | } |
1491 | | |
1492 | | /* Explicitly-configured user email address directly listed in |
1493 | | headers? */ |
1494 | 0 | if (user_email != NULL && |
1495 | 0 | _contains_my_address(headers, user_email)) { |
1496 | 0 | smtp_from = user_email; |
1497 | 0 | message_address_init_from_smtp( |
1498 | 0 | &reply_from, NULL, smtp_from); |
1499 | 0 | break; |
1500 | 0 | } |
1501 | 0 | } |
1502 | 0 | hdsp++; |
1503 | 0 | } |
1504 | | |
1505 | | /* My address not found in the headers; we got an implicit delivery */ |
1506 | 0 | if (*hdsp == NULL) { |
1507 | 0 | if (!extctx->set->check_recipient) { |
1508 | | /* Send reply from envelope recipient address */ |
1509 | 0 | smtp_from = (orig_recipient == NULL ? |
1510 | 0 | recipient : orig_recipient); |
1511 | 0 | if (user_email == NULL) |
1512 | 0 | user_email = sieve_get_user_email(svinst); |
1513 | 0 | message_address_init_from_smtp(&reply_from, |
1514 | 0 | NULL, user_email); |
1515 | 0 | } else { |
1516 | 0 | const char *orig_rcpt_str = "", *user_email_str = ""; |
1517 | | |
1518 | | /* Bail out */ |
1519 | 0 | if (extctx->set->use_original_recipient) { |
1520 | 0 | orig_rcpt_str = |
1521 | 0 | t_strdup_printf("original-recipient=<%s>, ", |
1522 | 0 | (orig_recipient == NULL ? "UNAVAILABLE" : |
1523 | 0 | smtp_address_encode(orig_recipient))); |
1524 | 0 | } |
1525 | |
|
1526 | 0 | if (user_email != NULL) { |
1527 | 0 | user_email_str = t_strdup_printf( |
1528 | 0 | "user-email=<%s>, ", |
1529 | 0 | smtp_address_encode(user_email)); |
1530 | 0 | } |
1531 | |
|
1532 | 0 | sieve_result_global_log( |
1533 | 0 | aenv, "discarding vacation response for implicitly delivered message; " |
1534 | 0 | "no known (envelope) recipient address found in message headers " |
1535 | 0 | "(recipient=<%s>, %s%sand%s additional ':addresses' are specified)", |
1536 | 0 | smtp_address_encode(recipient), |
1537 | 0 | orig_rcpt_str, user_email_str, |
1538 | 0 | (actx->addresses == NULL || *actx->addresses == NULL ? |
1539 | 0 | " no" : "")); |
1540 | 0 | return SIEVE_EXEC_OK; |
1541 | 0 | } |
1542 | 0 | } |
1543 | | |
1544 | | /* Send the message */ |
1545 | | |
1546 | 0 | T_BEGIN { |
1547 | 0 | ret = act_vacation_send( |
1548 | 0 | aenv, extctx, actx, sender, |
1549 | 0 | (extctx->set->send_from_recipient ? smtp_from : NULL), |
1550 | 0 | &reply_from); |
1551 | 0 | } T_END; |
1552 | | |
1553 | 0 | if (ret == SIEVE_EXEC_OK) { |
1554 | 0 | sieve_number_t seconds; |
1555 | |
|
1556 | 0 | eenv->exec_status->significant_action_executed = TRUE; |
1557 | |
|
1558 | 0 | struct event_passthrough *e = |
1559 | 0 | sieve_action_create_finish_event(aenv); |
1560 | |
|
1561 | 0 | sieve_result_event_log(aenv, e->event(), |
1562 | 0 | "sent vacation response to <%s>", |
1563 | 0 | smtp_address_encode(sender)); |
1564 | | |
1565 | | /* Check period limits once more */ |
1566 | 0 | seconds = actx->seconds; |
1567 | 0 | if (seconds < extctx->set->min_period) |
1568 | 0 | seconds = extctx->set->min_period; |
1569 | 0 | else if (extctx->set->max_period > 0 && |
1570 | 0 | seconds > extctx->set->max_period) |
1571 | 0 | seconds = extctx->set->max_period; |
1572 | | |
1573 | | /* Mark as replied */ |
1574 | 0 | if (seconds > 0) { |
1575 | 0 | sieve_action_duplicate_mark(aenv, dupl_hash, |
1576 | 0 | sizeof(dupl_hash), |
1577 | 0 | ioloop_time + seconds); |
1578 | 0 | } |
1579 | 0 | } |
1580 | |
|
1581 | 0 | if (ret == SIEVE_EXEC_TEMP_FAILURE) |
1582 | 0 | return SIEVE_EXEC_TEMP_FAILURE; |
1583 | | |
1584 | | /* Ignore all other errors */ |
1585 | 0 | return SIEVE_EXEC_OK; |
1586 | 0 | } |