-
Notifications
You must be signed in to change notification settings - Fork 29
/
imap-filter-sieve.c
1007 lines (843 loc) · 26.1 KB
/
imap-filter-sieve.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2017-2018 Pigeonhole authors, see the included COPYING file */
#include "imap-common.h"
#include "str.h"
#include "ioloop.h"
#include "time-util.h"
#include "module-context.h"
#include "message-address.h"
#include "mail-user.h"
#include "mail-duplicate.h"
#include "mail-storage-private.h"
#include "iostream-ssl.h"
#include "smtp-submit.h"
#include "sieve.h"
#include "sieve-storage.h"
#include "sieve-script.h"
#include "imap-filter-sieve.h"
#define DUPLICATE_DB_NAME "lda-dupes"
#define IMAP_FILTER_SIEVE_USER_CONTEXT(obj) \
MODULE_CONTEXT(obj, imap_filter_sieve_user_module)
#define IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(obj) \
MODULE_CONTEXT_REQUIRE(obj, imap_filter_sieve_user_module)
struct imap_filter_sieve_script {
struct sieve_script *script;
struct sieve_binary *binary;
/* Compile failed once with this error;
don't try again for this transaction */
enum sieve_error compile_error;
/* Binary corrupt after recompile; don't recompile again */
bool binary_corrupt:1;
};
struct imap_filter_sieve_user {
union mail_user_module_context module_ctx;
struct client *client;
struct sieve_instance *svinst;
struct sieve_storage *storage;
struct sieve_storage *global_storage;
struct mail_duplicate_db *dup_db;
struct sieve_error_handler *master_ehandler;
};
static MODULE_CONTEXT_DEFINE_INIT(imap_filter_sieve_user_module,
&mail_user_module_register);
/*
*
*/
static const char *
imap_filter_sieve_get_setting(void *context, const char *identifier)
{
struct imap_filter_sieve_user *ifsuser = context;
struct mail_user *user = ifsuser->client->user;
return mail_user_plugin_getenv(user, identifier);
}
static const struct sieve_callbacks imap_filter_sieve_callbacks = {
NULL,
imap_filter_sieve_get_setting
};
static struct sieve_instance *
imap_filter_sieve_get_svinst(struct imap_filter_sieve_context *sctx)
{
struct mail_user *user = sctx->user;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
struct sieve_environment svenv;
const struct mail_storage_settings *mail_set;
bool debug = user->mail_debug;
if (ifsuser->svinst != NULL)
return ifsuser->svinst;
mail_set = mail_user_set_get_storage_set(user);
ifsuser->dup_db = mail_duplicate_db_init(user, DUPLICATE_DB_NAME);
i_zero(&svenv);
svenv.username = user->username;
(void)mail_user_get_home(user, &svenv.home_dir);
svenv.hostname = mail_set->hostname;
svenv.base_dir = user->set->base_dir;
svenv.flags = SIEVE_FLAG_HOME_RELATIVE;
svenv.location = SIEVE_ENV_LOCATION_MS;
svenv.delivery_phase = SIEVE_DELIVERY_PHASE_POST;
ifsuser->svinst = sieve_init(&svenv, &imap_filter_sieve_callbacks,
ifsuser, debug);
ifsuser->master_ehandler = sieve_master_ehandler_create(
ifsuser->svinst, NULL, 0); // FIXME: prefix?
sieve_system_ehandler_set(ifsuser->master_ehandler);
sieve_error_handler_accept_infolog(ifsuser->master_ehandler, TRUE);
sieve_error_handler_accept_debuglog(ifsuser->master_ehandler, debug);
return ifsuser->svinst;
}
static int
imap_filter_sieve_get_personal_storage(struct imap_filter_sieve_context *sctx,
struct sieve_storage **storage_r,
enum mail_error *error_code_r,
const char **error_r)
{
struct mail_user *user = sctx->user;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
enum sieve_storage_flags storage_flags = 0;
struct sieve_instance *svinst;
enum sieve_error error;
*error_code_r = MAIL_ERROR_NONE;
*error_r = NULL;
if (ifsuser->storage != NULL) {
*storage_r = ifsuser->storage;
return 0;
}
// FIXME: limit interval between retries
svinst = imap_filter_sieve_get_svinst(sctx);
ifsuser->storage = sieve_storage_create_main(svinst, user,
storage_flags, &error);
if (ifsuser->storage != NULL) {
*storage_r = ifsuser->storage;
return 0;
}
switch (error) {
case SIEVE_ERROR_NOT_POSSIBLE:
*error_r = "Sieve processing is disabled for this user";
*error_code_r = MAIL_ERROR_NOTPOSSIBLE;
break;
case SIEVE_ERROR_NOT_FOUND:
*error_r = "Sieve script storage not accessible";
*error_code_r = MAIL_ERROR_NOTFOUND;
break;
default:
*error_r = t_strflocaltime(MAIL_ERRSTR_CRITICAL_MSG_STAMP,
ioloop_time);
*error_code_r = MAIL_ERROR_TEMP;
break;
}
return -1;
}
static int
imap_filter_sieve_get_global_storage(struct imap_filter_sieve_context *sctx,
struct sieve_storage **storage_r,
enum mail_error *error_code_r,
const char **error_r)
{
struct mail_user *user = sctx->user;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
struct sieve_instance *svinst;
const char *location;
enum sieve_error error;
*error_code_r = MAIL_ERROR_NONE;
*error_r = NULL;
if (ifsuser->global_storage != NULL) {
*storage_r = ifsuser->global_storage;
return 0;
}
svinst = imap_filter_sieve_get_svinst(sctx);
location = mail_user_plugin_getenv(user, "sieve_global");
if (location == NULL) {
sieve_sys_info(svinst, "include: sieve_global is unconfigured; "
"include of `:global' script is therefore not possible");
*error_code_r = MAIL_ERROR_NOTFOUND;
*error_r = "No global Sieve scripts available";
return -1;
}
ifsuser->global_storage =
sieve_storage_create(svinst, location, 0, &error);
if (ifsuser->global_storage != NULL) {
*storage_r = ifsuser->global_storage;
return 0;
}
switch (error) {
case SIEVE_ERROR_NOT_POSSIBLE:
case SIEVE_ERROR_NOT_FOUND:
*error_r = "No global Sieve scripts available";
*error_code_r = MAIL_ERROR_NOTFOUND;
break;
default:
*error_r = t_strflocaltime(MAIL_ERRSTR_CRITICAL_MSG_STAMP,
ioloop_time);
*error_code_r = MAIL_ERROR_TEMP;
break;
}
return -1;
}
/*
*
*/
struct imap_filter_sieve_context *
imap_filter_sieve_context_create(struct imap_filter_context *ctx,
enum imap_filter_sieve_type type)
{
struct client_command_context *cmd = ctx->cmd;
struct imap_filter_sieve_context *sctx;
sctx = p_new(cmd->pool, struct imap_filter_sieve_context, 1);
sctx->pool = cmd->pool;
sctx->filter_type = type;
sctx->user = ctx->cmd->client->user;
return sctx;
}
void imap_filter_sieve_context_free(struct imap_filter_sieve_context **_sctx)
{
struct imap_filter_sieve_context *sctx = *_sctx;
struct imap_filter_sieve_script *scripts;
unsigned int i;
*_sctx = NULL;
if (sctx == NULL)
return;
scripts = sctx->scripts;
for (i = 0; i < sctx->scripts_count; i++) {
if (scripts[i].binary != NULL)
sieve_close(&scripts[i].binary);
if (scripts[i].script != NULL)
sieve_script_unref(&scripts[i].script);
}
str_free(&sctx->errors);
}
/*
* Error handling
*/
static struct sieve_error_handler *
imap_filter_sieve_create_error_handler(struct imap_filter_sieve_context *sctx)
{
struct sieve_instance *svinst = imap_filter_sieve_get_svinst(sctx);
/* Prepare error handler */
if (sctx->errors == NULL)
sctx->errors = str_new(default_pool, 1024);
else
str_truncate(sctx->errors, 0);
return sieve_strbuf_ehandler_create(svinst, sctx->errors, TRUE,
10 /* client->set->_max_compile_errors */);
}
/*
*
*/
static struct sieve_binary *
imap_sieve_filter_open_script(struct imap_filter_sieve_context *sctx,
struct sieve_script *script,
enum sieve_compile_flags cpflags,
struct sieve_error_handler *user_ehandler,
bool recompile, enum sieve_error *error_r)
{
struct mail_user *user = sctx->user;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
struct sieve_instance *svinst = imap_filter_sieve_get_svinst(sctx);
struct sieve_error_handler *ehandler;
struct sieve_binary *sbin;
const char *compile_name = "compile";
bool debug = user->mail_debug;
if (recompile) {
/* Warn */
sieve_sys_warning(svinst,
"Encountered corrupt binary: re-compiling script %s",
sieve_script_location(script));
compile_name = "re-compile";
} else if (debug) {
sieve_sys_debug(svinst,
"Loading script %s", sieve_script_location(script));
}
if (script == sctx->user_script)
ehandler = user_ehandler;
else
ehandler = ifsuser->master_ehandler;
sieve_error_handler_reset(ehandler);
/* Load or compile the sieve script */
if (recompile) {
sbin = sieve_compile_script(script, ehandler, cpflags, error_r);
} else {
sbin = sieve_open_script(script, ehandler, cpflags, error_r);
}
/* Handle error */
if (sbin == NULL) {
switch (*error_r) {
/* Script not found */
case SIEVE_ERROR_NOT_FOUND:
if (debug) {
sieve_sys_debug(svinst, "Script `%s' is missing for %s",
sieve_script_location(script), compile_name);
}
break;
/* Temporary failure */
case SIEVE_ERROR_TEMP_FAILURE:
sieve_sys_error(svinst,
"Failed to open script `%s' for %s (temporary failure)",
sieve_script_location(script), compile_name);
break;
/* Compile failed */
case SIEVE_ERROR_NOT_VALID:
if (script == sctx->user_script)
break;
sieve_sys_error(svinst, "Failed to %s script `%s'",
compile_name, sieve_script_location(script));
break;
/* Something else */
default:
sieve_sys_error(svinst, "Failed to open script `%s' for %s",
sieve_script_location(script), compile_name);
break;
}
return NULL;
}
if (!recompile)
(void)sieve_save(sbin, FALSE, NULL);
return sbin;
}
int imap_filter_sieve_compile(struct imap_filter_sieve_context *sctx,
string_t **errors_r, bool *have_warnings_r)
{
struct imap_filter_sieve_script *scripts = sctx->scripts;
unsigned int count = sctx->scripts_count, i;
struct sieve_error_handler *ehandler;
enum sieve_error error;
int ret = 0;
*errors_r = NULL;
*have_warnings_r = FALSE;
/* Prepare error handler */
ehandler = imap_filter_sieve_create_error_handler(sctx);
for (i = 0; i < count; i++) {
struct sieve_script *script = scripts[i].script;
i_assert(script != NULL);
scripts[i].binary =
imap_sieve_filter_open_script(sctx, script, 0, ehandler,
FALSE, &error);
if (scripts[i].binary == NULL) {
if (error != SIEVE_ERROR_NOT_VALID) {
const char *errormsg =
sieve_script_get_last_error(script, &error);
if (error != SIEVE_ERROR_NONE) {
str_truncate(sctx->errors, 0);
str_append(sctx->errors, errormsg);
}
}
ret = -1;
break;
}
}
*have_warnings_r = (sieve_get_warnings(ehandler) > 0);
*errors_r = sctx->errors;
sieve_error_handler_unref(&ehandler);
return ret;
}
void imap_filter_sieve_open_input(struct imap_filter_sieve_context *sctx,
struct istream *input)
{
struct sieve_instance *svinst;
struct sieve_script *script;
svinst = imap_filter_sieve_get_svinst(sctx);
script = sieve_data_script_create_from_input(svinst, "script", input);
sctx->user_script = script;
sctx->scripts = p_new(sctx->pool, struct imap_filter_sieve_script, 1);
sctx->scripts_count = 1;
sctx->scripts[0].script = script;
}
int imap_filter_sieve_open_personal(struct imap_filter_sieve_context *sctx,
const char *name,
enum mail_error *error_code_r,
const char **error_r)
{
struct sieve_storage *storage;
struct sieve_script *script;
enum sieve_error error;
if (imap_filter_sieve_get_personal_storage(sctx, &storage,
error_code_r, error_r) < 0)
return -1;
if (name == NULL)
script = sieve_storage_active_script_open(storage, NULL);
else
script = sieve_storage_open_script(storage, name, NULL);
if (script == NULL) {
*error_r = sieve_storage_get_last_error(storage, &error);
switch (error) {
case SIEVE_ERROR_NOT_FOUND:
*error_code_r = MAIL_ERROR_NOTFOUND;
break;
case SIEVE_ERROR_NOT_POSSIBLE:
*error_code_r = MAIL_ERROR_NOTPOSSIBLE;
break;
default:
*error_code_r = MAIL_ERROR_TEMP;
}
return -1;
}
sctx->user_script = script;
sctx->scripts = p_new(sctx->pool, struct imap_filter_sieve_script, 1);
sctx->scripts_count = 1;
sctx->scripts[0].script = script;
return 0;
}
int imap_filter_sieve_open_global(struct imap_filter_sieve_context *sctx,
const char *name,
enum mail_error *error_code_r,
const char **error_r)
{
struct sieve_storage *storage;
struct sieve_script *script;
enum sieve_error error;
if (imap_filter_sieve_get_global_storage(sctx, &storage,
error_code_r, error_r) < 0)
return -1;
script = sieve_storage_open_script(storage, name, NULL);
if (script == NULL) {
*error_r = sieve_storage_get_last_error(storage, &error);
switch (error) {
case SIEVE_ERROR_NOT_FOUND:
*error_code_r = MAIL_ERROR_NOTFOUND;
break;
case SIEVE_ERROR_NOT_POSSIBLE:
*error_code_r = MAIL_ERROR_NOTPOSSIBLE;
break;
default:
*error_code_r = MAIL_ERROR_TEMP;
}
return -1;
}
sctx->user_script = script;
sctx->scripts = p_new(sctx->pool, struct imap_filter_sieve_script, 1);
sctx->scripts_count = 1;
sctx->scripts[0].script = script;
return 0;
}
/*
* Mail transmission
*/
static void *
imap_filter_sieve_smtp_start(const struct sieve_script_env *senv,
const struct smtp_address *mail_from)
{
struct imap_filter_sieve_context *sctx = senv->script_context;
struct mail_user *user = sctx->user;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
const struct smtp_submit_settings *smtp_set = ifsuser->client->smtp_set;
struct ssl_iostream_settings ssl_set;
i_zero(&ssl_set);
mail_user_init_ssl_client_settings(user, &ssl_set);
return (void *)smtp_submit_init_simple(smtp_set, &ssl_set, mail_from);
}
static void
imap_filter_sieve_smtp_add_rcpt(const struct sieve_script_env *senv ATTR_UNUSED,
void *handle,
const struct smtp_address *rcpt_to)
{
struct smtp_submit *smtp_submit = handle;
smtp_submit_add_rcpt(smtp_submit, rcpt_to);
}
static struct ostream *
imap_filter_sieve_smtp_send(const struct sieve_script_env *senv ATTR_UNUSED,
void *handle)
{
struct smtp_submit *smtp_submit = handle;
return smtp_submit_send(smtp_submit);
}
static void
imap_filter_sieve_smtp_abort(const struct sieve_script_env *senv ATTR_UNUSED,
void *handle)
{
struct smtp_submit *smtp_submit = handle;
smtp_submit_deinit(&smtp_submit);
}
static int
imap_filter_sieve_smtp_finish(const struct sieve_script_env *senv ATTR_UNUSED,
void *handle, const char **error_r)
{
struct smtp_submit *smtp_submit = handle;
int ret;
ret = smtp_submit_run(smtp_submit, error_r);
smtp_submit_deinit(&smtp_submit);
return ret;
}
/*
* Duplicate checking
*/
static bool
imap_filter_sieve_duplicate_check(const struct sieve_script_env *senv,
const void *id, size_t id_size)
{
struct imap_filter_sieve_context *sctx = senv->script_context;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(sctx->user);
return mail_duplicate_check(ifsuser->dup_db,
id, id_size, senv->user->username);
}
static void
imap_filter_sieve_duplicate_mark(const struct sieve_script_env *senv,
const void *id, size_t id_size, time_t time)
{
struct imap_filter_sieve_context *sctx = senv->script_context;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(sctx->user);
mail_duplicate_mark(ifsuser->dup_db,
id, id_size, senv->user->username, time);
}
static void
imap_filter_sieve_duplicate_flush(const struct sieve_script_env *senv)
{
struct imap_filter_sieve_context *sctx = senv->script_context;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(sctx->user);
mail_duplicate_db_flush(ifsuser->dup_db);
}
/*
*
*/
static int
imap_sieve_filter_handle_exec_status(struct imap_filter_sieve_context *sctx,
struct sieve_script *script, int status,
bool keep,
struct sieve_exec_status *estatus)
{
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(sctx->user);
struct sieve_instance *svinst = ifsuser->svinst;
sieve_sys_error_func_t error_func, user_error_func;
enum mail_error mail_error = MAIL_ERROR_NONE;
int ret = -1;
error_func = user_error_func = sieve_sys_error;
if (estatus != NULL && estatus->last_storage != NULL &&
estatus->store_failed) {
(void)mail_storage_get_last_error(estatus->last_storage,
&mail_error);
/* Don't bother administrator too much with benign errors */
if (mail_error == MAIL_ERROR_NOQUOTA) {
error_func = sieve_sys_info;
user_error_func = sieve_sys_info;
}
}
switch ( status ) {
case SIEVE_EXEC_FAILURE:
user_error_func(svinst,
"Execution of script %s failed",
sieve_script_location(script));
ret = -1;
break;
case SIEVE_EXEC_TEMP_FAILURE:
error_func(svinst,
"Execution of script %s was aborted "
"due to temporary failure",
sieve_script_location(script));
ret = -1;
break;
case SIEVE_EXEC_BIN_CORRUPT:
sieve_sys_error(svinst,
"!!BUG!!: Binary compiled from %s is still corrupt; "
"bailing out and reverting to default action",
sieve_script_location(script));
ret = -1;
break;
case SIEVE_EXEC_KEEP_FAILED:
error_func(svinst,
"Execution of script %s failed "
"with unsuccessful implicit keep",
sieve_script_location(script));
ret = -1;
break;
case SIEVE_EXEC_OK:
ret = (keep ? 0 : 1);
break;
}
return ret;
}
static int
imap_sieve_filter_run_scripts(struct imap_filter_sieve_context *sctx,
struct sieve_error_handler *user_ehandler,
const struct sieve_message_data *msgdata,
const struct sieve_script_env *scriptenv)
{
struct mail_user *user = sctx->user;
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
struct sieve_instance *svinst = ifsuser->svinst;
struct imap_filter_sieve_script *scripts = sctx->scripts;
unsigned int count = sctx->scripts_count;
struct sieve_multiscript *mscript;
struct sieve_error_handler *ehandler;
struct sieve_script *last_script = NULL;
bool user_script = FALSE, more = TRUE;
bool debug = user->mail_debug, keep = TRUE;
enum sieve_compile_flags cpflags;
enum sieve_execute_flags exflags;
enum sieve_error compile_error = SIEVE_ERROR_NONE;
unsigned int i;
int ret;
/* Start execution */
mscript = sieve_multiscript_start_execute(svinst, msgdata, scriptenv);
/* Execute scripts */
for (i = 0; i < count && more; i++) {
struct sieve_script *script = scripts[i].script;
struct sieve_binary *sbin = scripts[i].binary;
if (sbin == NULL) {
if (debug) {
sieve_sys_debug(svinst,
"Skipping script from `%s'",
sieve_script_location(script));
}
continue;
}
cpflags = 0;
exflags = SIEVE_EXECUTE_FLAG_SKIP_RESPONSES;
user_script = ( script == sctx->user_script );
last_script = script;
if (user_script) {
cpflags |= SIEVE_COMPILE_FLAG_NOGLOBAL;
exflags |= SIEVE_EXECUTE_FLAG_NOGLOBAL;
ehandler = user_ehandler;
} else {
ehandler = ifsuser->master_ehandler;
}
/* Execute */
if (debug) {
sieve_sys_debug(svinst,
"Executing script from `%s'",
sieve_get_source(sbin));
}
more = sieve_multiscript_run(mscript,
sbin, ehandler, ehandler, exflags);
if (!more) {
if (!scripts[i].binary_corrupt &&
sieve_multiscript_status(mscript)
== SIEVE_EXEC_BIN_CORRUPT &&
sieve_is_loaded(sbin)) {
/* Close corrupt script */
sieve_close(&sbin);
/* Recompile */
scripts[i].binary = sbin =
imap_sieve_filter_open_script(sctx,
script, cpflags, user_ehandler,
FALSE, &compile_error);
if ( sbin == NULL ) {
scripts[i].compile_error = compile_error;
break;
}
/* Execute again */
more = sieve_multiscript_run(mscript, sbin,
ehandler, ehandler, exflags);
/* Save new version */
if (sieve_multiscript_status(mscript)
== SIEVE_EXEC_BIN_CORRUPT)
scripts[i].binary_corrupt = TRUE;
else if (more)
(void)sieve_save(sbin, FALSE, NULL);
}
}
}
/* Finish execution */
exflags = SIEVE_EXECUTE_FLAG_SKIP_RESPONSES;
ehandler = (user_ehandler != NULL ?
user_ehandler : ifsuser->master_ehandler);
if (compile_error == SIEVE_ERROR_TEMP_FAILURE) {
ret = sieve_multiscript_tempfail(&mscript, ehandler, exflags);
} else {
ret = sieve_multiscript_finish(&mscript, ehandler, exflags,
&keep);
}
/* Don't log additional messages about compile failure */
if (compile_error != SIEVE_ERROR_NONE &&
ret == SIEVE_EXEC_FAILURE) {
sieve_sys_info(svinst,
"Aborted script execution sequence "
"with successful implicit keep");
return 1;
}
i_assert(last_script != NULL); /* at least one script is executed */
return imap_sieve_filter_handle_exec_status(sctx,
last_script, ret, keep, scriptenv->exec_status);
}
static int
parse_address(const char *address, const struct smtp_address **addr_r)
{
struct message_address *msg_addr;
struct smtp_address *smtp_addr;
if (message_address_parse_path(pool_datastack_create(),
(const unsigned char *)address,
strlen(address), &msg_addr) < 0) {
*addr_r = NULL;
return -1;
}
if (smtp_address_create_from_msg_temp(msg_addr, &smtp_addr) < 0) {
*addr_r = NULL;
return -1;
}
*addr_r = smtp_addr;
return 1;
}
static void
imap_sieve_filter_get_msgdata(struct imap_filter_sieve_context *sctx,
struct mail *mail,
struct sieve_message_data *msgdata_r)
{
struct sieve_instance *svinst = imap_filter_sieve_get_svinst(sctx);
struct mail_user *user = sctx->user;
const char *address, *error;
const struct smtp_address *mail_from, *rcpt_to;
struct smtp_address *user_addr;
int ret;
mail_from = NULL;
if ((ret=mail_get_special(mail, MAIL_FETCH_FROM_ENVELOPE,
&address)) > 0 &&
(ret=parse_address(address, &mail_from)) < 0) {
sieve_sys_warning(svinst,
"Failed to parse message FROM_ENVELOPE");
}
if (ret <= 0 &&
mail_get_first_header(mail, "Return-Path",
&address) > 0 &&
parse_address(address, &mail_from) < 0) {
sieve_sys_info(svinst,
"Failed to parse Return-Path header");
}
rcpt_to = NULL;
if (mail_get_first_header(mail, "Delivered-To",
&address) > 0 &&
parse_address(address, &rcpt_to) < 0) {
sieve_sys_info(svinst,
"Failed to parse Delivered-To header");
}
if (rcpt_to == NULL) {
if (svinst->user_email != NULL)
rcpt_to = svinst->user_email;
else if (smtp_address_parse_username(sctx->pool, user->username,
&user_addr, &error) < 0) {
sieve_sys_warning(svinst,
"Cannot obtain SMTP address from username `%s': %s",
user->username, error);
} else {
if (user_addr->domain == NULL)
user_addr->domain = svinst->domainname;
rcpt_to = user_addr;
}
}
// FIXME: maybe parse top Received header.
i_zero(msgdata_r);
msgdata_r->mail = mail;
msgdata_r->envelope.mail_from = mail_from;
msgdata_r->envelope.rcpt_to = rcpt_to;
msgdata_r->auth_user = user->username;
(void)mail_get_first_header(mail, "Message-ID", &msgdata_r->id);
}
int imap_sieve_filter_run_mail(struct imap_filter_sieve_context *sctx,
struct mail *mail, string_t **errors_r,
bool *have_warnings_r)
{
struct sieve_instance *svinst = imap_filter_sieve_get_svinst(sctx);
struct mail_user *user = sctx->user;
struct sieve_error_handler *user_ehandler;
struct sieve_message_data msgdata;
struct sieve_script_env scriptenv;
struct sieve_exec_status estatus;
struct sieve_trace_config trace_config;
struct sieve_trace_log *trace_log;
const char *error;
int ret;
*errors_r = NULL;
*have_warnings_r = FALSE;
/* Prepare error handler */
user_ehandler = imap_filter_sieve_create_error_handler(sctx);
/* Initialize trace logging */
trace_log = NULL;
if (sieve_trace_config_get(svinst, &trace_config) >= 0) {
const char *tr_label = t_strdup_printf
("%s.%s.%u", user->username,
mailbox_get_vname(mail->box), mail->uid);
if (sieve_trace_log_open(svinst, tr_label, &trace_log) < 0)
i_zero(&trace_config);
}
T_BEGIN {
/* Collect necessary message data */
imap_sieve_filter_get_msgdata(sctx, mail, &msgdata);
/* Compose script execution environment */
if (sieve_script_env_init(&scriptenv, user, &error) < 0) {
sieve_sys_error(svinst,
"Failed to initialize script execution: %s",
error);
ret = -1;
} else {
scriptenv.default_mailbox = mailbox_get_vname(mail->box);
scriptenv.smtp_start = imap_filter_sieve_smtp_start;
scriptenv.smtp_add_rcpt = imap_filter_sieve_smtp_add_rcpt;
scriptenv.smtp_send = imap_filter_sieve_smtp_send;
scriptenv.smtp_abort = imap_filter_sieve_smtp_abort;
scriptenv.smtp_finish = imap_filter_sieve_smtp_finish;
scriptenv.duplicate_mark = imap_filter_sieve_duplicate_mark;
scriptenv.duplicate_check = imap_filter_sieve_duplicate_check;
scriptenv.duplicate_flush = imap_filter_sieve_duplicate_flush;
scriptenv.trace_log = trace_log;
scriptenv.trace_config = trace_config;
scriptenv.script_context = sctx;
i_zero(&estatus);
scriptenv.exec_status = &estatus;
/* Execute script(s) */
ret = imap_sieve_filter_run_scripts(sctx, user_ehandler,
&msgdata, &scriptenv);
}
} T_END;
if ( trace_log != NULL )
sieve_trace_log_free(&trace_log);
*have_warnings_r = (sieve_get_warnings(user_ehandler) > 0);
*errors_r = sctx->errors;
sieve_error_handler_unref(&user_ehandler);
return ret;
}
/*
* User
*/
static void imap_filter_sieve_user_deinit(struct mail_user *user)
{
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(user);
sieve_error_handler_unref(&ifsuser->master_ehandler);
if (ifsuser->storage != NULL)
sieve_storage_unref(&ifsuser->storage);
if (ifsuser->svinst != NULL)
sieve_deinit(&ifsuser->svinst);
if (ifsuser->dup_db != NULL)
mail_duplicate_db_deinit(&ifsuser->dup_db);
ifsuser->module_ctx.super.deinit(user);
}
static void imap_filter_sieve_user_created(struct mail_user *user)
{
struct imap_filter_sieve_user *ifsuser;
struct mail_user_vfuncs *v = user->vlast;
ifsuser = p_new(user->pool, struct imap_filter_sieve_user, 1);
ifsuser->module_ctx.super = *v;
user->vlast = &ifsuser->module_ctx.super;
v->deinit = imap_filter_sieve_user_deinit;
MODULE_CONTEXT_SET(user, imap_filter_sieve_user_module, ifsuser);
}
/*
* Hooks
*/
static struct mail_storage_hooks imap_filter_sieve_mail_storage_hooks = {
.mail_user_created = imap_filter_sieve_user_created,
};
/*
* Client
*/
void imap_filter_sieve_client_created(struct client *client)
{
struct imap_filter_sieve_user *ifsuser =
IMAP_FILTER_SIEVE_USER_CONTEXT_REQUIRE(client->user);
ifsuser->client = client;
}
/*
*
*/
void imap_filter_sieve_init(struct module *module)
{
mail_storage_hooks_add(module, &imap_filter_sieve_mail_storage_hooks);
}