-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathmail-storage.c
3553 lines (3076 loc) · 95.7 KB
/
mail-storage.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) 2002-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "llist.h"
#include "mail-storage.h"
#include "str.h"
#include "str-parse.h"
#include "sha1.h"
#include "unichar.h"
#include "hex-binary.h"
#include "fs-api.h"
#include "iostream-ssl.h"
#include "file-dotlock.h"
#include "file-create-locked.h"
#include "istream.h"
#include "eacces-error.h"
#include "mkdir-parents.h"
#include "time-util.h"
#include "var-expand.h"
#include "dsasl-client.h"
#include "imap-date.h"
#include "mail-index-private.h"
#include "mail-index-alloc-cache.h"
#include "mailbox-tree.h"
#include "mailbox-list-private.h"
#include "mail-storage-private.h"
#include "mail-storage-settings.h"
#include "mail-namespace.h"
#include "mail-search.h"
#include "mail-search-register.h"
#include "mail-search-mime-register.h"
#include "mailbox-search-result-private.h"
#include "mailbox-guid-cache.h"
#include "mail-cache.h"
#include "utc-mktime.h"
#include <ctype.h>
#define MAILBOX_DELETE_RETRY_SECS 30
#define MAILBOX_MAX_HIERARCHY_NAME_LENGTH 255
extern struct mail_search_register *mail_search_register_imap;
extern struct mail_search_register *mail_search_register_human;
struct event_category event_category_storage = {
.name = "storage",
};
struct event_category event_category_mailbox = {
.parent = &event_category_storage,
.name = "mailbox",
};
struct event_category event_category_mail = {
.parent = &event_category_mailbox,
.name = "mail",
};
struct mail_storage_module_register mail_storage_module_register = { 0 };
struct mail_module_register mail_module_register = { 0 };
struct mail_storage_mail_index_module mail_storage_mail_index_module =
MODULE_CONTEXT_INIT(&mail_index_module_register);
ARRAY_TYPE(mail_storage) mail_storage_classes;
static int mail_storage_init_refcount = 0;
void mail_storage_init(void)
{
if (mail_storage_init_refcount++ > 0)
return;
dsasl_clients_init();
mailbox_attributes_init();
mailbox_lists_init();
mail_storage_hooks_init();
i_array_init(&mail_storage_classes, 8);
mail_storage_register_all();
mailbox_list_register_all();
}
void mail_storage_deinit(void)
{
i_assert(mail_storage_init_refcount > 0);
if (--mail_storage_init_refcount > 0)
return;
if (mail_search_register_human != NULL)
mail_search_register_deinit(&mail_search_register_human);
if (mail_search_register_imap != NULL)
mail_search_register_deinit(&mail_search_register_imap);
mail_search_mime_register_deinit();
if (array_is_created(&mail_storage_classes))
array_free(&mail_storage_classes);
mail_storage_hooks_deinit();
mailbox_lists_deinit();
mailbox_attributes_deinit();
dsasl_clients_deinit();
}
void mail_storage_class_register(struct mail_storage *storage_class)
{
i_assert(mail_storage_find_class(storage_class->name) == NULL);
/* append it after the list, so the autodetection order is correct */
array_push_back(&mail_storage_classes, &storage_class);
}
void mail_storage_class_unregister(struct mail_storage *storage_class)
{
struct mail_storage *const *classes;
unsigned int i, count;
classes = array_get(&mail_storage_classes, &count);
for (i = 0; i < count; i++) {
if (classes[i] == storage_class) {
array_delete(&mail_storage_classes, i, 1);
break;
}
}
}
struct mail_storage *mail_storage_find_class(const char *name)
{
struct mail_storage *const *classes;
unsigned int i, count;
i_assert(name != NULL);
classes = array_get(&mail_storage_classes, &count);
for (i = 0; i < count; i++) {
if (strcasecmp(classes[i]->name, name) == 0)
return classes[i];
}
return NULL;
}
static struct mail_storage *
mail_storage_autodetect(const struct mail_namespace *ns,
struct mailbox_list_settings *set)
{
struct mail_storage *const *classes;
unsigned int i, count;
classes = array_get(&mail_storage_classes, &count);
for (i = 0; i < count; i++) {
if (classes[i]->v.autodetect != NULL) {
if (classes[i]->v.autodetect(ns, set))
return classes[i];
}
}
return NULL;
}
static void
mail_storage_set_autodetection(const char **data, const char **driver)
{
const char *p;
/* check if data is in driver:data format (eg. mbox:~/mail) */
p = *data;
while (i_isalnum(*p) || *p == '_') p++;
if (*p == ':' && p != *data) {
/* no autodetection if the storage driver is given. */
*driver = t_strdup_until(*data, p);
*data = p + 1;
}
}
static struct mail_storage *
mail_storage_get_class(struct mail_namespace *ns, const char *driver,
struct mailbox_list_settings *list_set,
enum mail_storage_flags flags, const char **error_r)
{
struct mail_storage *storage_class = NULL;
const char *home;
if (driver == NULL) {
/* no mail_location, autodetect */
} else if (strcmp(driver, "auto") == 0) {
/* explicit autodetection with "auto" driver. */
if (list_set->root_dir != NULL &&
*list_set->root_dir == '\0') {
/* handle the same as with driver=NULL */
list_set->root_dir = NULL;
}
} else {
storage_class = mail_user_get_storage_class(ns->user, driver);
if (storage_class == NULL) {
*error_r = t_strdup_printf(
"Unknown mail storage driver %s", driver);
return NULL;
}
}
if (list_set->root_dir == NULL || *list_set->root_dir == '\0') {
/* no root directory given. is this allowed? */
const struct mailbox_list *list;
list = list_set->layout == NULL ? NULL :
mailbox_list_find_class(list_set->layout);
if (storage_class == NULL &&
(flags & MAIL_STORAGE_FLAG_NO_AUTODETECTION) == 0) {
/* autodetection should take care of this */
} else if (storage_class != NULL &&
(storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_ROOT) != 0) {
/* root not required for this storage */
} else if (list != NULL &&
(list->props & MAILBOX_LIST_PROP_NO_ROOT) != 0) {
/* root not required for this layout */
} else {
*error_r = "Root mail directory not given";
return NULL;
}
}
if (storage_class != NULL) {
storage_class->v.get_list_settings(ns, list_set);
return storage_class;
}
storage_class = mail_storage_autodetect(ns, list_set);
if (storage_class != NULL)
return storage_class;
(void)mail_user_get_home(ns->user, &home);
if (home == NULL || *home == '\0') home = "(not set)";
if (ns->set->location == NULL || *ns->set->location == '\0') {
*error_r = t_strdup_printf(
"Mail storage autodetection failed with home=%s", home);
} else if (str_begins_with(ns->set->location, "auto:")) {
*error_r = t_strdup_printf(
"Autodetection failed for %s (home=%s)",
ns->set->location, home);
} else {
*error_r = t_strdup_printf(
"Ambiguous mail location setting, "
"don't know what to do with it: %s "
"(try prefixing it with mbox: or maildir:)",
ns->set->location);
}
return NULL;
}
static int
mail_storage_verify_root(const char *root_dir, const char *dir_type,
const char **error_r)
{
struct stat st;
if (stat(root_dir, &st) == 0) {
/* exists */
if (S_ISDIR(st.st_mode))
return 0;
*error_r = t_strdup_printf(
"Root mail directory is a file: %s", root_dir);
return -1;
} else if (ENOACCESS(errno)) {
*error_r = mail_error_eacces_msg("stat", root_dir);
return -1;
} else if (errno != ENOENT) {
*error_r = t_strdup_printf("stat(%s) failed: %m", root_dir);
return -1;
} else {
*error_r = t_strdup_printf(
"Root %s directory doesn't exist: %s", dir_type, root_dir);
return -1;
}
}
static int
mail_storage_create_root(struct mailbox_list *list,
enum mail_storage_flags flags, const char **error_r)
{
const char *root_dir, *type_name, *error;
enum mailbox_list_path_type type;
if (list->set.iter_from_index_dir) {
type = MAILBOX_LIST_PATH_TYPE_INDEX;
type_name = "index";
} else {
type = MAILBOX_LIST_PATH_TYPE_MAILBOX;
type_name = "mail";
}
if (!mailbox_list_get_root_path(list, type, &root_dir)) {
/* storage doesn't use directories (e.g. shared root) */
return 0;
}
if ((flags & MAIL_STORAGE_FLAG_NO_AUTOVERIFY) != 0) {
if (!event_want_debug_log(list->ns->user->event))
return 0;
/* we don't need to verify, but since debugging is
enabled, check and log if the root doesn't exist */
if (mail_storage_verify_root(root_dir, type_name, &error) < 0) {
e_debug(list->ns->user->event,
"Namespace %s: Creating storage despite: %s",
list->ns->prefix, error);
}
return 0;
}
if ((flags & MAIL_STORAGE_FLAG_NO_AUTOCREATE) == 0) {
/* If the directories don't exist, we'll just autocreate them
later. */
return 0;
}
return mail_storage_verify_root(root_dir, type_name, error_r);
}
static bool
mail_storage_match_class(struct mail_storage *storage,
const struct mail_storage *storage_class,
const struct mailbox_list_settings *set)
{
if (strcmp(storage->name, storage_class->name) != 0)
return FALSE;
if ((storage->class_flags & MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT) != 0 &&
strcmp(storage->unique_root_dir,
(set->root_dir != NULL ? set->root_dir : "")) != 0)
return FALSE;
if (strcmp(storage->name, "shared") == 0) {
/* allow multiple independent shared namespaces */
return FALSE;
}
return TRUE;
}
static struct mail_storage *
mail_storage_find(struct mail_user *user,
const struct mail_storage *storage_class,
const struct mailbox_list_settings *set)
{
struct mail_storage *storage = user->storages;
for (; storage != NULL; storage = storage->next) {
if (mail_storage_match_class(storage, storage_class, set))
return storage;
}
return NULL;
}
static int
mail_storage_create_full_real(struct mail_namespace *ns, const char *driver,
const char *data, enum mail_storage_flags flags,
struct mail_storage **storage_r,
const char **error_r)
{
struct mail_storage *storage_class, *storage = NULL;
struct mailbox_list *list;
struct mailbox_list_settings list_set;
enum mailbox_list_flags list_flags = 0;
const char *p;
if ((flags & MAIL_STORAGE_FLAG_KEEP_HEADER_MD5) == 0 &&
ns->mail_set->pop3_uidl_format != NULL) {
/* if pop3_uidl_format contains %m, we want to keep the
header MD5 sums stored even if we're not running POP3
right now. */
p = ns->mail_set->pop3_uidl_format;
while ((p = strchr(p, '%')) != NULL) {
if (p[1] == '%')
p += 2;
else if (var_get_key(++p) == 'm') {
flags |= MAIL_STORAGE_FLAG_KEEP_HEADER_MD5;
break;
}
}
}
mailbox_list_settings_init_defaults(&list_set);
if (data == NULL) {
/* autodetect */
} else if (driver != NULL && strcmp(driver, "shared") == 0) {
/* internal shared namespace */
list_set.root_dir = ns->user->set->base_dir;
} else {
if (driver == NULL)
mail_storage_set_autodetection(&data, &driver);
if (mailbox_list_settings_parse(ns->user, data, &list_set,
error_r) < 0)
return -1;
}
storage_class = mail_storage_get_class(ns, driver, &list_set, flags,
error_r);
if (storage_class == NULL)
return -1;
i_assert(list_set.layout != NULL);
if (ns->list == NULL) {
/* first storage for namespace */
if (mail_storage_is_mailbox_file(storage_class))
list_flags |= MAILBOX_LIST_FLAG_MAILBOX_FILES;
if ((storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_ROOT) != 0)
list_flags |= MAILBOX_LIST_FLAG_NO_MAIL_FILES;
if ((storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_LIST_DELETES) != 0)
list_flags |= MAILBOX_LIST_FLAG_NO_DELETES;
if (mailbox_list_create(list_set.layout, ns, &list_set,
list_flags, &list, error_r) < 0) {
*error_r = t_strdup_printf("Mailbox list driver %s: %s",
list_set.layout, *error_r);
return -1;
}
if ((storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_ROOT) == 0) {
if (mail_storage_create_root(ns->list, flags, error_r) < 0)
return -1;
}
}
storage = mail_storage_find(ns->user, storage_class, &list_set);
if (storage != NULL) {
/* using an existing storage */
storage->refcount++;
mail_namespace_add_storage(ns, storage);
*storage_r = storage;
return 0;
}
storage = storage_class->v.alloc();
if (storage->lost_mailbox_prefix == NULL)
storage->lost_mailbox_prefix = MAIL_STORAGE_LOST_MAILBOX_PREFIX;
storage->refcount = 1;
storage->storage_class = storage_class;
storage->user = ns->user;
storage->set = ns->mail_set;
storage->flags = flags;
/* Set to UINT32_MAX manually to denote 'unset', as the default 0 is
used for mails currently being saved. */
storage->last_internal_error_mail_uid = UINT32_MAX;
storage->event = event_create(ns->user->event);
if (storage_class->event_category != NULL)
event_add_category(storage->event, storage_class->event_category);
event_set_append_log_prefix(
storage->event, t_strdup_printf("%s: ", storage_class->name));
p_array_init(&storage->module_contexts, storage->pool, 5);
if (storage->v.create != NULL &&
storage->v.create(storage, ns, error_r) < 0) {
*error_r = t_strdup_printf("%s: %s", storage->name, *error_r);
event_unref(&storage->event);
pool_unref(&storage->pool);
return -1;
}
/* If storage supports list index rebuild,
provide default mailboxes_fs unless storage
wants to use its own. */
if (storage->v.list_index_rebuild != NULL &&
storage->mailboxes_fs == NULL) {
struct fs_settings fs_set;
struct ssl_iostream_settings ssl_set;
const char *error;
i_zero(&fs_set);
mail_user_init_fs_settings(storage->user, &fs_set, &ssl_set);
if (fs_init("posix", "", &fs_set, &storage->mailboxes_fs,
&error) < 0) {
*error_r = t_strdup_printf("fs_init(posix) failed: %s", error);
storage->v.destroy(storage);
return -1;
}
}
T_BEGIN {
hook_mail_storage_created(storage);
} T_END;
i_assert(storage->unique_root_dir != NULL ||
(storage->class_flags & MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT) == 0);
DLLIST_PREPEND(&ns->user->storages, storage);
mail_namespace_add_storage(ns, storage);
*storage_r = storage;
return 0;
}
int mail_storage_create_full(struct mail_namespace *ns, const char *driver,
const char *data, enum mail_storage_flags flags,
struct mail_storage **storage_r,
const char **error_r)
{
int ret;
T_BEGIN {
ret = mail_storage_create_full_real(ns, driver, data, flags,
storage_r, error_r);
} T_END_PASS_STR_IF(ret < 0, error_r);
return ret;
}
int mail_storage_create(struct mail_namespace *ns, const char *driver,
enum mail_storage_flags flags, const char **error_r)
{
struct mail_storage *storage;
return mail_storage_create_full(ns, driver, ns->set->location,
flags, &storage, error_r);
}
void mail_storage_unref(struct mail_storage **_storage)
{
struct mail_storage *storage = *_storage;
i_assert(storage->refcount > 0);
/* set *_storage=NULL only after calling destroy() callback.
for example mdbox wants to access ns->storage */
if (--storage->refcount > 0) {
*_storage = NULL;
return;
}
if (storage->mailboxes != NULL) {
i_panic("Trying to deinit storage without freeing mailbox %s",
storage->mailboxes->vname);
}
if (storage->obj_refcount != 0)
i_panic("Trying to deinit storage before freeing its objects");
DLLIST_REMOVE(&storage->user->storages, storage);
storage->v.destroy(storage);
mail_storage_clear_error(storage);
if (array_is_created(&storage->error_stack)) {
i_assert(array_count(&storage->error_stack) == 0);
array_free(&storage->error_stack);
}
fs_unref(&storage->mailboxes_fs);
event_unref(&storage->event);
*_storage = NULL;
pool_unref(&storage->pool);
mail_index_alloc_cache_destroy_unrefed();
}
void mail_storage_obj_ref(struct mail_storage *storage)
{
i_assert(storage->refcount > 0);
if (storage->obj_refcount++ == 0)
mail_user_ref(storage->user);
}
void mail_storage_obj_unref(struct mail_storage *storage)
{
i_assert(storage->refcount > 0);
i_assert(storage->obj_refcount > 0);
if (--storage->obj_refcount == 0) {
struct mail_user *user = storage->user;
mail_user_unref(&user);
}
}
void mail_storage_clear_error(struct mail_storage *storage)
{
i_free_and_null(storage->error_string);
i_free(storage->last_internal_error);
i_free(storage->last_internal_error_mailbox);
storage->last_error_is_internal = FALSE;
storage->error = MAIL_ERROR_NONE;
storage->last_internal_error_mail_uid = UINT32_MAX;
}
void mail_storage_set_error(struct mail_storage *storage,
enum mail_error error, const char *string)
{
if (storage->error_string != string) {
i_free(storage->error_string);
storage->error_string = i_strdup(string);
}
storage->last_error_is_internal = FALSE;
storage->error = error;
storage->last_internal_error_mail_uid = UINT32_MAX;
}
void mail_storage_set_internal_error(struct mail_storage *storage)
{
const char *str;
str = t_strflocaltime(MAIL_ERRSTR_CRITICAL_MSG_STAMP, ioloop_time);
i_free(storage->error_string);
storage->error_string = i_strdup(str);
storage->error = MAIL_ERROR_TEMP;
/* this function doesn't set last_internal_error, so
last_error_is_internal can't be TRUE. */
storage->last_error_is_internal = FALSE;
i_free(storage->last_internal_error);
i_free(storage->last_internal_error_mailbox);
storage->last_internal_error_mail_uid = UINT32_MAX;
}
static void
mail_storage_set_critical_error(struct mail_storage *storage, const char *str,
const char *mailbox_vname, uint32_t mail_uid)
{
char *old_error = storage->error_string;
char *old_internal_error = storage->last_internal_error;
char *old_internal_error_mailbox = storage->last_internal_error_mailbox;
storage->error_string = NULL;
storage->last_internal_error = NULL;
storage->last_internal_error_mailbox = NULL;
/* critical errors may contain sensitive data, so let user
see only "Internal error" with a timestamp to make it
easier to look from log files the actual error message. */
mail_storage_set_internal_error(storage);
storage->last_internal_error = i_strdup(str);
storage->last_internal_error_mailbox = i_strdup(mailbox_vname);
storage->last_internal_error_mail_uid = mail_uid;
storage->last_error_is_internal = TRUE;
/* free the old_error and old_internal_error only after the new error
is generated, because they may be one of the parameters. */
i_free(old_error);
i_free(old_internal_error);
i_free(old_internal_error_mailbox);
}
void mail_storage_set_critical(struct mail_storage *storage,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
T_BEGIN {
const char *str = t_strdup_vprintf(fmt, va);
mail_storage_set_critical_error(storage, str, NULL, UINT32_MAX);
e_error(storage->event, "%s", str);
} T_END;
va_end(va);
}
void mailbox_set_critical(struct mailbox *box, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
T_BEGIN {
const char *str = t_strdup_vprintf(fmt, va);
mail_storage_set_critical_error(box->storage, str, box->vname,
UINT32_MAX);
e_error(box->event, "%s", str);
} T_END;
va_end(va);
}
void mail_set_critical(struct mail *mail, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
T_BEGIN {
const char *formatted_msg = t_strdup_vprintf(fmt, va);
mail_storage_set_critical_error(mail->box->storage, formatted_msg,
mail->box->vname, mail->uid);
e_error(mail_event(mail), "%s", formatted_msg);
} T_END;
va_end(va);
}
/* Note: mail_storage_get_last_internal_error() will always include
the mailbox prefix, while mailbox_get_last_internal_error() and
mail_get_last_internal_error() usually will not. */
const char *mail_storage_get_last_internal_error(struct mail_storage *storage,
enum mail_error *error_r)
{
if (error_r != NULL)
*error_r = storage->error;
if (storage->last_error_is_internal) {
i_assert(storage->last_internal_error != NULL);
bool is_mailbox_error_set = storage->last_internal_error_mailbox != NULL;
bool is_mail_error_set =
storage->last_internal_error_mail_uid != UINT32_MAX;
if (is_mail_error_set) {
i_assert(is_mailbox_error_set);
return t_strdup_printf(
"Mailbox %s: UID %u: %s",
mailbox_name_sanitize(storage->last_internal_error_mailbox),
storage->last_internal_error_mail_uid,
storage->last_internal_error);
}
if (is_mailbox_error_set)
return t_strdup_printf(
"Mailbox %s: %s",
mailbox_name_sanitize(storage->last_internal_error_mailbox),
storage->last_internal_error);
return storage->last_internal_error;
}
return mail_storage_get_last_error(storage, error_r);
}
/* Note: mailbox_get_last_internal_error() will include the mailbox prefix only
when mailbox->vname does not match last_internal_error_mailbox, which
might happen with e.g. virtual mailboxes logging about physical
mailboxes, while mail_storage_get_last_internal_error() always does. */
const char *mailbox_get_last_internal_error(struct mailbox *box,
enum mail_error *error_r)
{
struct mail_storage *storage = mailbox_get_storage(box);
const char *last_mailbox = storage->last_internal_error_mailbox;
if (last_mailbox != NULL &&
strcmp(last_mailbox, box->vname) != 0)
return mail_storage_get_last_internal_error(storage, error_r);
if (error_r != NULL)
*error_r = storage->error;
if (storage->last_error_is_internal) {
i_assert(storage->last_internal_error != NULL);
if (storage->last_internal_error_mail_uid != UINT32_MAX)
return t_strdup_printf("UID %u: %s",
storage->last_internal_error_mail_uid,
storage->last_internal_error);
return storage->last_internal_error;
}
return mail_storage_get_last_error(storage, error_r);
}
/* Note: mail_get_last_internal_error() will include the mail prefix only when
mail->uid does not match last_internal_error_mail_uid, while
mail_storage_get_last_internal_error() always does. */
const char *
mail_get_last_internal_error(struct mail *mail, enum mail_error *error_r)
{
struct mail_storage *storage = mailbox_get_storage(mail->box);
const char *last_mailbox = storage->last_internal_error_mailbox;
if (last_mailbox != NULL &&
strcmp(last_mailbox, mail->box->vname) != 0)
return mail_storage_get_last_internal_error(storage, error_r);
uint32_t last_mail_uid = storage->last_internal_error_mail_uid;
if (last_mail_uid == UINT32_MAX || last_mail_uid != mail->uid)
return mailbox_get_last_internal_error(mail->box, error_r);
if (error_r != NULL)
*error_r = storage->error;
if (storage->last_error_is_internal) {
i_assert(storage->last_internal_error != NULL);
return storage->last_internal_error;
}
return mail_storage_get_last_error(storage, error_r);
}
void mail_storage_copy_error(struct mail_storage *dest,
struct mail_storage *src)
{
const char *str;
enum mail_error error;
if (src == dest)
return;
str = mail_storage_get_last_error(src, &error);
mail_storage_set_error(dest, error, str);
}
void mail_storage_copy_list_error(struct mail_storage *storage,
struct mailbox_list *list)
{
const char *str;
enum mail_error error;
str = mailbox_list_get_last_error(list, &error);
mail_storage_set_error(storage, error, str);
}
void mailbox_set_index_error(struct mailbox *box)
{
if (mail_index_is_deleted(box->index)) {
mailbox_set_deleted(box);
mail_index_reset_error(box->index);
} else {
i_free(box->storage->last_internal_error_mailbox);
box->storage->last_internal_error_mailbox = i_strdup(box->vname);
mail_storage_set_index_error(box->storage, box->index);
}
}
void mail_storage_set_index_error(struct mail_storage *storage,
struct mail_index *index)
{
const char *index_error;
mail_storage_set_internal_error(storage);
/* use the lib-index's error as our internal error string */
index_error = mail_index_get_last_error(index, NULL);
if (index_error == NULL)
index_error = "BUG: Unknown internal index error";
storage->last_internal_error = i_strdup(index_error);
storage->last_error_is_internal = TRUE;
mail_index_reset_error(index);
}
const struct mail_storage_settings *
mail_storage_get_settings(struct mail_storage *storage)
{
return storage->set;
}
struct mail_user *mail_storage_get_user(struct mail_storage *storage)
{
return storage->user;
}
void mail_storage_set_callbacks(struct mail_storage *storage,
struct mail_storage_callbacks *callbacks,
void *context)
{
storage->callbacks = *callbacks;
storage->callback_context = context;
}
int mail_storage_purge(struct mail_storage *storage)
{
if (storage->v.purge == NULL)
return 0;
int ret;
T_BEGIN {
ret = storage->v.purge(storage);
} T_END;
return ret;
}
const char *mail_storage_get_last_error(struct mail_storage *storage,
enum mail_error *error_r)
{
/* We get here only in error situations, so we have to return some
error. If storage->error is NONE, it means we forgot to set it at
some point.. */
if (storage->error == MAIL_ERROR_NONE) {
if (error_r != NULL)
*error_r = MAIL_ERROR_TEMP;
return storage->error_string != NULL ? storage->error_string :
"BUG: Unknown internal error";
}
if (storage->error_string == NULL) {
/* This shouldn't happen.. */
storage->error_string =
i_strdup_printf("BUG: Unknown 0x%x error",
storage->error);
}
if (error_r != NULL)
*error_r = storage->error;
return storage->error_string;
}
const char *mailbox_get_last_error(struct mailbox *box,
enum mail_error *error_r)
{
return mail_storage_get_last_error(box->storage, error_r);
}
enum mail_error mailbox_get_last_mail_error(struct mailbox *box)
{
enum mail_error error;
mail_storage_get_last_error(box->storage, &error);
return error;
}
void mail_storage_last_error_push(struct mail_storage *storage)
{
struct mail_storage_error *err;
if (!array_is_created(&storage->error_stack))
i_array_init(&storage->error_stack, 2);
err = array_append_space(&storage->error_stack);
err->error_string = i_strdup(storage->error_string);
err->error = storage->error;
err->last_error_is_internal = storage->last_error_is_internal;
/* Initially set to UINT32_MAX manually to denote 'unset', as the
default 0 is used for mails currently being saved. If there is no
internal error, the attribute would not be set otherwise. */
err->last_internal_error_mail_uid = UINT32_MAX;
if (err->last_error_is_internal) {
err->last_internal_error = i_strdup(storage->last_internal_error);
err->last_internal_error_mailbox =
i_strdup(storage->last_internal_error_mailbox);
err->last_internal_error_mail_uid =
storage->last_internal_error_mail_uid;
}
}
void mail_storage_last_error_pop(struct mail_storage *storage)
{
unsigned int count = array_count(&storage->error_stack);
const struct mail_storage_error *err =
array_idx(&storage->error_stack, count-1);
i_free(storage->error_string);
i_free(storage->last_internal_error);
i_free(storage->last_internal_error_mailbox);
storage->error_string = err->error_string;
storage->error = err->error;
storage->last_error_is_internal = err->last_error_is_internal;
storage->last_internal_error = err->last_internal_error;
storage->last_internal_error_mailbox = err->last_internal_error_mailbox;
storage->last_internal_error_mail_uid = err->last_internal_error_mail_uid;
array_delete(&storage->error_stack, count-1, 1);
}
bool mail_storage_is_mailbox_file(struct mail_storage *storage)
{
return (storage->class_flags &
MAIL_STORAGE_CLASS_FLAG_MAILBOX_IS_FILE) != 0;
}
bool mail_storage_set_error_from_errno(struct mail_storage *storage)
{
const char *error_string;
enum mail_error error;
if (!mail_error_from_errno(&error, &error_string))
return FALSE;
if (event_want_debug_log(storage->event) && error != MAIL_ERROR_NOTFOUND) {
/* debugging is enabled - admin may be debugging a
(permission) problem, so return FALSE to get the caller to
log the full error message. */
return FALSE;
}
mail_storage_set_error(storage, error, error_string);
return TRUE;
}
const struct mailbox_settings *
mailbox_settings_find(struct mail_namespace *ns, const char *vname)
{
struct mailbox_settings *box_set;
if (!array_is_created(&ns->set->mailboxes))
return NULL;
if (ns->prefix_len > 0 &&
strncmp(ns->prefix, vname, ns->prefix_len-1) == 0) {
if (vname[ns->prefix_len-1] == mail_namespace_get_sep(ns))
vname += ns->prefix_len;
else if (vname[ns->prefix_len-1] == '\0') {
/* namespace prefix itself */
vname = "";
}
}
array_foreach_elem(&ns->set->mailboxes, box_set) {
if (strcmp(box_set->name, vname) == 0)
return box_set;
}
return NULL;
}
struct mailbox *mailbox_alloc(struct mailbox_list *list, const char *vname,
enum mailbox_flags flags)
{
struct mailbox_list *new_list = list;
struct mail_storage *storage;
struct mailbox *box;
enum mail_error open_error = 0;
const char *suffix, *errstr = NULL;
i_assert(uni_utf8_str_is_valid(vname));
if (str_begins_icase(vname, "INBOX", &suffix) &&
!str_begins_with(vname, "INBOX")) {
/* make sure INBOX shows up in uppercase everywhere. do this
regardless of whether we're in inbox=yes namespace, because
clients expect INBOX to be case insensitive regardless of
server's internal configuration. */
if (suffix[0] == '\0')
vname = "INBOX";
else if (suffix[0] != mail_namespace_get_sep(list->ns))
/* not INBOX prefix */ ;
else if (strncasecmp(list->ns->prefix, vname, 6) == 0 &&
!str_begins_with(list->ns->prefix, "INBOX")) {
mailbox_list_set_critical(list,
"Invalid server configuration: "
"Namespace prefix=%s must be uppercase INBOX",
list->ns->prefix);
open_error = MAIL_ERROR_TEMP;
} else {
vname = t_strconcat("INBOX", suffix, NULL);
}
}
T_BEGIN {
const char *orig_vname = vname;
enum mailbox_list_get_storage_flags storage_flags = 0;
if ((flags & MAILBOX_FLAG_SAVEONLY) != 0)