/
mail-storage.c
2593 lines (2231 loc) · 68 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-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "llist.h"
#include "str.h"
#include "str-sanitize.h"
#include "unichar.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 "settings-parser.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 "mailbox-search-result-private.h"
#include "mailbox-guid-cache.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 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;
void mail_storage_init(void)
{
dsasl_clients_init();
mailbox_attributes_init();
mailbox_lists_init();
mail_storage_hooks_init();
i_array_init(&mail_storage_classes, 8);
}
void mail_storage_deinit(void)
{
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);
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_append(&mail_storage_classes, &storage_class, 1);
}
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 (strncmp(ns->set->location, "auto:", 5) == 0) {
*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, bool autocreate,
const char **error_r)
{
struct stat st;
if (stat(root_dir, &st) == 0) {
/* exists */
return 1;
} else if (errno == EACCES) {
*error_r = mail_error_eacces_msg("stat", root_dir);
return -1;
} else if (errno != ENOENT && errno != ENOTDIR) {
*error_r = t_strdup_printf("stat(%s) failed: %m", root_dir);
return -1;
} else if (!autocreate) {
*error_r = t_strdup_printf(
"Root mail directory doesn't exist: %s", root_dir);
return -1;
} else {
/* doesn't exist */
return 0;
}
}
static int
mail_storage_create_root(struct mailbox_list *list,
enum mail_storage_flags flags, const char **error_r)
{
const char *root_dir, *error;
bool autocreate;
int ret;
if (!mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_MAILBOX,
&root_dir)) {
/* storage doesn't use directories (e.g. shared root) */
return 0;
}
if ((flags & MAIL_STORAGE_FLAG_NO_AUTOVERIFY) != 0) {
if (!list->mail_set->mail_debug)
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, FALSE, &error) < 0) {
i_debug("Namespace %s: Creating storage despite: %s",
list->ns->prefix, error);
}
return 0;
}
autocreate = (flags & MAIL_STORAGE_FLAG_NO_AUTOCREATE) == 0;
ret = mail_storage_verify_root(root_dir, autocreate, error_r);
if (ret == 0) {
ret = mailbox_list_try_mkdir_root(list, root_dir,
MAILBOX_LIST_PATH_TYPE_MAILBOX,
error_r);
}
return ret < 0 ? -1 : 0;
}
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;
}
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)
{
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;
}
}
}
memset(&list_set, 0, sizeof(list_set));
list_set.mailbox_dir_name = "";
list_set.maildir_name = "";
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();
storage->refcount = 1;
storage->storage_class = storage_class;
storage->user = ns->user;
storage->set = ns->mail_set;
storage->flags = flags;
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);
pool_unref(&storage->pool);
return -1;
}
T_BEGIN {
hook_mail_storage_created(storage);
} T_END;
DLLIST_PREPEND(&ns->user->storages, storage);
mail_namespace_add_storage(ns, storage);
*storage_r = storage;
return 0;
}
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);
i_free(storage->error_string);
if (array_is_created(&storage->error_stack)) {
i_assert(array_count(&storage->error_stack) == 0);
array_free(&storage->error_stack);
}
*_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);
storage->error = MAIL_ERROR_NONE;
}
void mail_storage_set_error(struct mail_storage *storage,
enum mail_error error, const char *string)
{
i_free(storage->error_string);
storage->error_string = i_strdup(string);
storage->error = error;
}
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;
}
void mail_storage_set_critical(struct mail_storage *storage,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
i_error("%s", t_strdup_vprintf(fmt, va));
va_end(va);
/* 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);
}
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);
else
mail_storage_set_internal_error(box->storage);
mail_index_reset_error(box->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)
{
return storage->v.purge == NULL ? 0 :
storage->v.purge(storage);
}
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;
}
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);
storage->error_string = err->error_string;
storage->error = err->error;
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 (storage->set->mail_debug && 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 *const *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(&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 *errstr = NULL;
i_assert(uni_utf8_str_is_valid(vname));
if (strncasecmp(vname, "INBOX", 5) == 0 &&
strncmp(vname, "INBOX", 5) != 0) {
/* 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 (vname[5] == '\0')
vname = "INBOX";
else if (vname[5] != mail_namespace_get_sep(list->ns))
/* not INBOX prefix */ ;
else if (strncasecmp(list->ns->prefix, vname, 6) == 0 &&
strncmp(list->ns->prefix, "INBOX", 5) != 0) {
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", vname + 5, NULL);
}
}
T_BEGIN {
if (mailbox_list_get_storage(&new_list, vname, &storage) < 0) {
/* do a delayed failure at mailbox_open() */
storage = mail_namespace_get_default_storage(list->ns);
errstr = mailbox_list_get_last_error(new_list, &open_error);
errstr = t_strdup(errstr);
}
box = storage->v.mailbox_alloc(storage, new_list, vname, flags);
box->set = mailbox_settings_find(new_list->ns, vname);
box->open_error = open_error;
if (open_error != 0)
mail_storage_set_error(storage, open_error, errstr);
hook_mailbox_allocated(box);
} T_END;
DLLIST_PREPEND(&box->storage->mailboxes, box);
mail_storage_obj_ref(box->storage);
return box;
}
struct mailbox *mailbox_alloc_guid(struct mailbox_list *list,
const guid_128_t guid,
enum mailbox_flags flags)
{
struct mailbox *box = NULL;
struct mailbox_metadata metadata;
enum mail_error open_error = MAIL_ERROR_TEMP;
const char *vname;
if (mailbox_guid_cache_find(list, guid, &vname) < 0) {
vname = NULL;
} else if (vname != NULL) {
box = mailbox_alloc(list, vname, flags);
if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID,
&metadata) < 0) {
} else if (memcmp(metadata.guid, guid,
sizeof(metadata.guid)) != 0) {
/* GUID mismatch, refresh cache and try again */
mailbox_free(&box);
mailbox_guid_cache_refresh(list);
return mailbox_alloc_guid(list, guid, flags);
} else {
/* successfully opened the correct mailbox */
return box;
}
i_error("mailbox_alloc_guid(%s): "
"Couldn't verify mailbox GUID: %s",
guid_128_to_string(guid),
mailbox_get_last_error(box, NULL));
vname = NULL;
mailbox_free(&box);
} else {
vname = t_strdup_printf("(nonexistent mailbox with GUID=%s)",
guid_128_to_string(guid));
open_error = MAIL_ERROR_NOTFOUND;
}
if (vname == NULL) {
vname = t_strdup_printf("(error in mailbox with GUID=%s)",
guid_128_to_string(guid));
}
box = mailbox_alloc(list, vname, flags);
box->open_error = open_error;
return box;
}
static bool mailbox_is_autocreated(struct mailbox *box)
{
if (box->inbox_user)
return TRUE;
return box->set != NULL &&
strcmp(box->set->autocreate, MAILBOX_SET_AUTO_NO) != 0;
}
static int mailbox_autocreate(struct mailbox *box)
{
const char *errstr;
enum mail_error error;
if (mailbox_create(box, NULL, FALSE) < 0) {
errstr = mailbox_get_last_error(box, &error);
if (error != MAIL_ERROR_EXISTS) {
mail_storage_set_critical(box->storage,
"Failed to autocreate mailbox %s: %s",
box->vname, errstr);
return -1;
}
} else if (box->set != NULL &&
strcmp(box->set->autocreate,
MAILBOX_SET_AUTO_SUBSCRIBE) == 0) {
if (mailbox_set_subscribed(box, TRUE) < 0) {
mail_storage_set_critical(box->storage,
"Failed to autosubscribe to mailbox %s: %s",
box->vname, mailbox_get_last_error(box, NULL));
return -1;
}
}
return 0;
}
static int mailbox_autocreate_and_reopen(struct mailbox *box)
{
int ret;
if (mailbox_autocreate(box) < 0)
return -1;
mailbox_close(box);
ret = box->v.open(box);
if (ret < 0 && box->inbox_user &&
!box->storage->user->inbox_open_error_logged) {
box->storage->user->inbox_open_error_logged = TRUE;
mail_storage_set_critical(box->storage,
"Opening INBOX failed: %s",
mailbox_get_last_error(box, NULL));
}
return ret;
}
static bool
mailbox_name_verify_separators(const char *vname, char sep,
const char **error_r)
{
unsigned int i;
bool prev_sep = FALSE;
/* Make sure the vname is correct: non-empty, doesn't begin or end
with separator and no adjacent separators */
for (i = 0; vname[i] != '\0'; i++) {
if (vname[i] == sep) {
if (prev_sep) {
*error_r = "Has adjacent hierarchy separators";
return FALSE;
}
prev_sep = TRUE;
} else {
prev_sep = FALSE;
}
}
if (prev_sep && i > 0) {
*error_r = "Ends with hierarchy separator";
return FALSE;
}
return TRUE;
}
static int mailbox_verify_name(struct mailbox *box)
{
struct mail_namespace *ns = box->list->ns;
const char *error, *vname = box->vname;
char list_sep, ns_sep;
if (box->inbox_user) {
/* this is INBOX - don't bother with further checks */
return 0;
}
list_sep = mailbox_list_get_hierarchy_sep(box->list);
ns_sep = mail_namespace_get_sep(ns);
if (ns->prefix_len > 0) {
/* vname is either "namespace/box" or "namespace" */
if (strncmp(vname, ns->prefix, ns->prefix_len-1) != 0 ||
(vname[ns->prefix_len-1] != '\0' &&
vname[ns->prefix_len-1] != ns->prefix[ns->prefix_len-1])) {
/* User input shouldn't normally be able to get us in
here. The main reason this isn't an assert is to
allow any input at all to mailbox_verify_*_name()
without crashing. */
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
t_strdup_printf("Invalid mailbox name '%s': "
"Missing namespace prefix '%s'",
str_sanitize(vname, 80), ns->prefix));
return -1;
}
vname += ns->prefix_len - 1;
if (vname[0] != '\0') {
i_assert(vname[0] == ns->prefix[ns->prefix_len-1]);
vname++;
if (vname[0] == '\0') {
/* "namespace/" isn't a valid mailbox name. */
mail_storage_set_error(box->storage,
MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
}
}
if (ns_sep != list_sep && box->list->set.escape_char == '\0' &&
strchr(vname, list_sep) != NULL) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS, t_strdup_printf(
"Character not allowed in mailbox name: '%c'", list_sep));
return -1;
}
if (vname[0] == ns_sep &&
!box->storage->set->mail_full_filesystem_access) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Invalid mailbox name: Begins with hierarchy separator");
return -1;
}
if (!mailbox_name_verify_separators(vname, ns_sep, &error) ||
!mailbox_list_is_valid_name(box->list, box->name, &error)) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
t_strdup_printf("Invalid mailbox name: %s", error));
return -1;
}
return 0;
}
static int mailbox_verify_existing_name(struct mailbox *box)
{
const char *path;
if (box->opened)
return 0;
if (mailbox_verify_name(box) < 0)
return -1;
/* Make sure box->_path is set, so mailbox_get_path() works from
now on. Note that this may also fail with some backends if the
mailbox doesn't exist. */
if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_MAILBOX, &path) < 0) {
if (box->storage->error != MAIL_ERROR_NOTFOUND ||
!mailbox_is_autocreated(box))
return -1;
/* if this is an autocreated mailbox, create it now */
if (mailbox_autocreate(box) < 0)
return -1;
mailbox_close(box);
if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_MAILBOX,
&path) < 0)
return -1;
}
return 0;
}
static bool mailbox_name_has_control_chars(const char *name)
{
const char *p;
for (p = name; *p != '\0'; p++) {
if ((unsigned char)*p < ' ')
return TRUE;
}
return FALSE;
}
void mailbox_skip_create_name_restrictions(struct mailbox *box, bool set)
{
box->skip_create_name_restrictions = set;
}
int mailbox_verify_create_name(struct mailbox *box)
{
/* mailbox_alloc() already checks that vname is valid UTF8,
so we don't need to verify that.
check vname instead of storage name, because vname is what is
visible to users, while storage name may be a fixed length GUID. */
if (mailbox_verify_name(box) < 0)
return -1;
if (box->skip_create_name_restrictions)
return 0;
if (mailbox_name_has_control_chars(box->vname)) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Control characters not allowed in new mailbox names");
return -1;
}
if (strlen(box->vname) > MAILBOX_LIST_NAME_MAX_LENGTH) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Mailbox name too long");
return -1;
}
/* check individual component names, too */
const char *old_name = box->name;
const char *name;
const char sep = mailbox_list_get_hierarchy_sep(box->list);
while((name = strchr(old_name, sep)) != NULL) {
if (name - old_name > MAILBOX_MAX_HIERARCHY_NAME_LENGTH) {