Cross Reference: /dovecot/src/lib-storage/mailbox-list.c
mailbox-list.c revision 3ac0d8763b06a39c95187fe6170b1bf822b28a1f
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
15394N/A/* Copyright (c) 2006-2007 Dovecot authors, see the included COPYING file */
15394N/A
15394N/A#include "lib.h"
15394N/A#include "array.h"
15394N/A#include "ioloop.h"
15394N/A#include "home-expand.h"
15394N/A#include "unlink-directory.h"
17185N/A#include "imap-match.h"
15394N/A#include "mailbox-tree.h"
15394N/A#include "mailbox-list-private.h"
17176N/A
17176N/A#include <time.h>
15394N/A#include <unistd.h>
15394N/A#include <dirent.h>
19051N/A#include <sys/stat.h>
15394N/A
18810N/A/* 20 * (200+1) < 4096 which is the standard PATH_MAX. Having these settings
15394N/A prevents malicious user from creating eg. "a/a/a/.../a" mailbox name and
15394N/A then start renaming them to larger names from end to beginning, which
18615N/A eventually would start causing the failures when trying to use too
19052N/A long mailbox names. */
19052N/A#define MAILBOX_MAX_HIERARCHY_LEVELS 20
15394N/A#define MAILBOX_MAX_HIERARCHY_NAME_LENGTH 200
15394N/A
15394N/A/* Message to show to users when critical error occurs */
15394N/A#define CRITICAL_MSG \
15394N/A "Internal error occurred. Refer to server log for more information."
19052N/A#define CRITICAL_MSG_STAMP CRITICAL_MSG " [%Y-%m-%d %H:%M:%S]"
15394N/A
15394N/Astruct mailbox_list_module_register mailbox_list_module_register = { 0 };
15394N/A
15394N/Avoid (*hook_mailbox_list_created)(struct mailbox_list *list);
15394N/A
15394N/Astatic ARRAY_DEFINE(mailbox_list_drivers, const struct mailbox_list *);
15394N/A
15394N/Astatic bool mailbox_list_driver_find(const char *name, unsigned int *idx_r)
15394N/A{
15394N/A const struct mailbox_list *const *drivers;
15394N/A unsigned int i, count;
15394N/A
15394N/A drivers = array_get(&mailbox_list_drivers, &count);
15394N/A for (i = 0; i < count; i++) {
15394N/A if (strcasecmp(drivers[i]->name, name) == 0) {
15394N/A *idx_r = i;
15394N/A return TRUE;
15394N/A }
15394N/A }
15394N/A return FALSE;
15394N/A}
15394N/A
15394N/Avoid mailbox_list_register(const struct mailbox_list *list)
15394N/A{
15394N/A if (!array_is_created(&mailbox_list_drivers))
19051N/A i_array_init(&mailbox_list_drivers, 4);
19051N/A else {
18810N/A unsigned int idx;
18810N/A
18476N/A if (mailbox_list_driver_find(list->name, &idx)) {
18476N/A i_fatal("mailbox_list_register(%s): duplicate driver",
17094N/A list->name);
17094N/A }
16716N/A }
16716N/A
15956N/A array_append(&mailbox_list_drivers, &list, 1);
15956N/A}
15394N/A
15394N/Avoid mailbox_list_unregister(const struct mailbox_list *list)
15394N/A{
15394N/A unsigned int idx;
if (!mailbox_list_driver_find(list->name, &idx)) {
i_fatal("mailbox_list_unregister(%s): unknown driver",
list->name);
}
array_delete(&mailbox_list_drivers, idx, 1);
if (array_count(&mailbox_list_drivers) == 0)
array_free(&mailbox_list_drivers);
}
int mailbox_list_alloc(const char *driver, struct mailbox_list **list_r,
const char **error_r)
{
const struct mailbox_list *const *class_p;
struct mailbox_list *list;
unsigned int idx;
if (!mailbox_list_driver_find(driver, &idx)) {
*error_r = t_strdup_printf("Unknown mailbox list driver: %s",
driver);
return -1;
}
class_p = array_idx(&mailbox_list_drivers, idx);
list = *list_r = (*class_p)->v.alloc();
array_create(&list->module_contexts, list->pool, sizeof(void *), 5);
return 0;
}
static const char *fix_path(const char *path)
{
size_t len = strlen(path);
if (len > 1 && path[len-1] == '/')
path = t_strndup(path, len-1);
return home_expand(path);
}
int mailbox_list_settings_parse(const char *data,
struct mailbox_list_settings *set,
const char **layout, const char **alt_dir_r,
const char **error_r)
{
const char *const *tmp, *key, *value;
i_assert(*data != '\0');
*error_r = NULL;
if (alt_dir_r != NULL)
*alt_dir_r = NULL;
/* <root dir> */
tmp = t_strsplit(data, ":");
set->root_dir = fix_path(*tmp);
tmp++;
for (; *tmp != NULL; tmp++) {
value = strchr(*tmp, '=');
if (value == NULL) {
key = *tmp;
value = "";
} else {
key = t_strdup_until(*tmp, value);
value++;
}
if (strcmp(key, "INBOX") == 0)
set->inbox_path = fix_path(value);
else if (strcmp(key, "INDEX") == 0)
set->index_dir = fix_path(value);
else if (strcmp(key, "CONTROL") == 0)
set->control_dir = fix_path(value);
else if (strcmp(key, "ALT") == 0 && alt_dir_r != NULL)
*alt_dir_r = fix_path(value);
else if (strcmp(key, "LAYOUT") == 0)
*layout = value;
else if (strcmp(key, "SUBSCRIPTIONS") == 0)
set->subscription_fname = fix_path(value);
else if (strcmp(key, "DIRNAME") == 0)
set->maildir_name = value;
else {
*error_r = t_strdup_printf("Unknown setting: %s", key);
return -1;
}
}
if (set->index_dir != NULL && strcmp(set->index_dir, "MEMORY") == 0)
set->index_dir = "";
return 0;
}
void mailbox_list_init(struct mailbox_list *list, struct mail_namespace *ns,
const struct mailbox_list_settings *set,
enum mailbox_list_flags flags)
{
i_assert(set->root_dir == NULL || *set->root_dir != '\0');
i_assert(set->subscription_fname == NULL ||
*set->subscription_fname != '\0');
list->ns = ns;
list->flags = flags;
list->file_create_mode = (mode_t)-1;
list->file_create_gid = (gid_t)-1;
/* copy settings */
list->set.root_dir = p_strdup(list->pool, set->root_dir);
list->set.index_dir = set->index_dir == NULL ||
strcmp(set->index_dir, set->root_dir) == 0 ? NULL :
p_strdup(list->pool, set->index_dir);
list->set.control_dir = set->control_dir == NULL ||
strcmp(set->control_dir, set->root_dir) == 0 ? NULL :
p_strdup(list->pool, set->control_dir);
list->set.inbox_path = p_strdup(list->pool, set->inbox_path);
list->set.subscription_fname =
p_strdup(list->pool, set->subscription_fname);
list->set.maildir_name = p_strdup(list->pool, set->maildir_name);
list->set.mail_storage_flags = set->mail_storage_flags;
list->set.lock_method = set->lock_method;
if ((flags & MAILBOX_LIST_FLAG_DEBUG) != 0) {
i_info("%s: root=%s, index=%s, control=%s, inbox=%s",
list->name, list->set.root_dir,
list->set.index_dir == NULL ? "" : list->set.index_dir,
list->set.control_dir == NULL ?
"" : list->set.control_dir,
list->set.inbox_path == NULL ?
"" : list->set.inbox_path);
}
if (hook_mailbox_list_created != NULL)
hook_mailbox_list_created(list);
list->set.mail_storage_flags = NULL;
list->set.lock_method = NULL;
}
void mailbox_list_deinit(struct mailbox_list *list)
{
i_free_and_null(list->error_string);
list->v.deinit(list);
}
const char *mailbox_list_get_driver_name(struct mailbox_list *list)
{
return list->name;
}
char mailbox_list_get_hierarchy_sep(struct mailbox_list *list)
{
return list->hierarchy_sep;
}
enum mailbox_list_flags mailbox_list_get_flags(struct mailbox_list *list)
{
return list->flags;
}
struct mail_namespace *mailbox_list_get_namespace(struct mailbox_list *list)
{
return list->ns;
}
void mailbox_list_get_permissions(struct mailbox_list *list,
mode_t *mode_r, gid_t *gid_r)
{
const char *path;
struct stat st;
if (list->file_create_mode != (mode_t)-1) {
*mode_r = list->file_create_mode;
*gid_r = list->file_create_gid;
return;
}
path = mailbox_list_get_path(list, NULL, MAILBOX_LIST_PATH_TYPE_DIR);
if (stat(path, &st) < 0) {
if (!ENOTFOUND(errno)) {
mailbox_list_set_critical(list, "stat(%s) failed: %m",
path);
}
/* return safe defaults */
*mode_r = 0600;
*gid_r = (gid_t)-1;
return;
}
list->file_create_mode = st.st_mode & 0666;
if (S_ISDIR(st.st_mode) && (st.st_mode & S_ISGID) != 0) {
/* directory's GID is used automatically for new files */
list->file_create_gid = (gid_t)-1;
} else if ((st.st_mode & 0060) == 0) {
/* group doesn't have any permissions, so don't bother
changing it */
list->file_create_gid = (gid_t)-1;
} else if (getegid() == st.st_gid) {
/* using our own gid, no need to change it */
list->file_create_gid = (gid_t)-1;
} else {
list->file_create_gid = st.st_gid;
}
*mode_r = list->file_create_mode;
*gid_r = list->file_create_gid;
}
bool mailbox_list_is_valid_pattern(struct mailbox_list *list,
const char *pattern)
{
return list->v.is_valid_pattern(list, pattern);
}
bool mailbox_list_is_valid_existing_name(struct mailbox_list *list,
const char *name)
{
return list->v.is_valid_existing_name(list, name);
}
bool mailbox_list_is_valid_create_name(struct mailbox_list *list,
const char *name)
{
return list->v.is_valid_create_name(list, name);
}
const char *mailbox_list_get_path(struct mailbox_list *list, const char *name,
enum mailbox_list_path_type type)
{
mailbox_list_clear_error(list);
return list->v.get_path(list, name, type);
}
const char *mailbox_list_get_temp_prefix(struct mailbox_list *list)
{
return list->v.get_temp_prefix(list);
}
const char *mailbox_list_join_refpattern(struct mailbox_list *list,
const char *ref, const char *pattern)
{
if (list->v.join_refpattern != NULL)
return list->v.join_refpattern(list, ref, pattern);
/* the default implementation: */
if (*ref != '\0') {
/* merge reference and pattern */
pattern = t_strconcat(ref, pattern, NULL);
}
return pattern;
}
int mailbox_list_get_mailbox_name_status(struct mailbox_list *list,
const char *name,
enum mailbox_name_status *status)
{
mailbox_list_clear_error(list);
if (!mailbox_list_is_valid_existing_name(list, name)) {
*status = MAILBOX_NAME_INVALID;
return 0;
}
return list->v.get_mailbox_name_status(list, name, status);
}
struct mailbox_list_iterate_context *
mailbox_list_iter_init(struct mailbox_list *list, const char *pattern,
enum mailbox_list_iter_flags flags)
{
const char *patterns[2];
patterns[0] = pattern;
patterns[1] = NULL;
return mailbox_list_iter_init_multiple(list, patterns, flags);
}
struct mailbox_list_iterate_context *
mailbox_list_iter_init_multiple(struct mailbox_list *list,
const char *const *patterns,
enum mailbox_list_iter_flags flags)
{
i_assert(*patterns != NULL);
mailbox_list_clear_error(list);
return list->v.iter_init(list, patterns, flags);
}
const struct mailbox_info *
mailbox_list_iter_next(struct mailbox_list_iterate_context *ctx)
{
return ctx->list->v.iter_next(ctx);
}
int mailbox_list_iter_deinit(struct mailbox_list_iterate_context **_ctx)
{
struct mailbox_list_iterate_context *ctx = *_ctx;
*_ctx = NULL;
return ctx->list->v.iter_deinit(ctx);
}
int mailbox_list_set_subscribed(struct mailbox_list *list,
const char *name, bool set)
{
mailbox_list_clear_error(list);
return list->v.set_subscribed(list, name, set);
}
int mailbox_list_delete_mailbox(struct mailbox_list *list, const char *name)
{
if (!mailbox_list_is_valid_existing_name(list, name)) {
mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
if (strcmp(name, "INBOX") == 0) {
mailbox_list_set_error(list, MAIL_ERROR_NOTPOSSIBLE,
"INBOX can't be deleted.");
return -1;
}
return list->v.delete_mailbox(list, name);
}
int mailbox_list_rename_mailbox(struct mailbox_list *list,
const char *oldname, const char *newname)
{
if (!mailbox_list_is_valid_existing_name(list, oldname) ||
!mailbox_list_is_valid_create_name(list, newname)) {
mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
return list->v.rename_mailbox(list, oldname, newname);
}
static int mailbox_list_try_delete(struct mailbox_list *list, const char *dir)
{
if (unlink_directory(dir, TRUE) == 0 || errno == ENOENT)
return 0;
if (errno == ENOTEMPTY) {
/* We're most likely using NFS and we can't delete
.nfs* files. */
mailbox_list_set_error(list, MAIL_ERROR_NOTPOSSIBLE,
"Mailbox is still open in another session, "
"can't delete it.");
} else {
mailbox_list_set_critical(list,
"unlink_directory(%s) failed: %m", dir);
}
return -1;
}
int mailbox_list_delete_index_control(struct mailbox_list *list,
const char *name)
{
const char *path, *index_dir, *dir;
path = mailbox_list_get_path(list, name,
MAILBOX_LIST_PATH_TYPE_MAILBOX);
/* delete the index directory first, so that if we crash we don't
leave indexes for deleted mailboxes lying around */
index_dir = mailbox_list_get_path(list, name,
MAILBOX_LIST_PATH_TYPE_INDEX);
if (*index_dir != '\0' && strcmp(index_dir, path) != 0) {
if (mailbox_list_try_delete(list, index_dir) < 0)
return -1;
}
/* control directory next */
dir = mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_CONTROL);
if (*dir != '\0' && strcmp(dir, path) != 0 &&
strcmp(dir, index_dir) != 0) {
if (mailbox_list_try_delete(list, index_dir) < 0)
return -1;
}
return 0;
}
static void node_fix_parents(struct mailbox_node *node)
{
/* If we happened to create any of the parents, we need to mark them
nonexistent. */
node = node->parent;
for (; node != NULL; node = node->parent) {
if ((node->flags & MAILBOX_MATCHED) == 0)
node->flags |= MAILBOX_NONEXISTENT;
}
}
void mailbox_list_iter_update(struct mailbox_list_iterate_context *ctx,
struct mailbox_tree_context *tree_ctx,
struct imap_match_glob *glob, bool update_only,
bool match_parents, const char *name)
{
struct mail_namespace *ns = ctx->list->ns;
struct mailbox_node *node;
enum mailbox_info_flags create_flags, always_flags;
enum imap_match_result match;
const char *p;
bool created, add_matched;
create_flags = (update_only ||
(ctx->flags & MAILBOX_LIST_ITER_RETURN_NO_FLAGS) == 0) ?
(MAILBOX_NONEXISTENT | MAILBOX_NOCHILDREN) : 0;
always_flags = MAILBOX_SUBSCRIBED;
add_matched = TRUE;
t_push();
for (;;) {
created = FALSE;
match = imap_match(glob, name);
if (match == IMAP_MATCH_YES) {
node = update_only ?
mailbox_tree_lookup(tree_ctx, name) :
mailbox_tree_get(tree_ctx, name, &created);
if (created) {
node->flags = create_flags;
if (create_flags != 0)
node_fix_parents(node);
}
if (node != NULL) {
if (!update_only && add_matched)
node->flags |= MAILBOX_MATCHED;
node->flags |= always_flags;
}
/* We don't want to show the parent mailboxes unless
something else matches them, but if they are matched
we want to show them having child subscriptions */
add_matched = FALSE;
} else {
if ((match & IMAP_MATCH_PARENT) == 0)
break;
/* We've a (possibly) non-subscribed parent mailbox
which has a subscribed child mailbox. Make sure we
return the parent mailbox. */
}
if (!match_parents)
break;
/* see if parent matches */
p = strrchr(name, ns->sep);
if (p == NULL)
break;
name = t_strdup_until(name, p);
create_flags &= ~MAILBOX_NOCHILDREN;
always_flags = MAILBOX_CHILDREN | MAILBOX_CHILD_SUBSCRIBED;
}
t_pop();
}
bool mailbox_list_name_is_too_large(const char *name, char sep)
{
unsigned int levels = 1, level_len = 0;
for (; *name != '\0'; name++) {
if (*name == sep) {
if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
return TRUE;
levels++;
level_len = 0;
} else {
level_len++;
}
}
if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
return TRUE;
if (levels > MAILBOX_MAX_HIERARCHY_LEVELS)
return TRUE;
return FALSE;
}
enum mailbox_list_file_type
mailbox_list_get_file_type(const struct dirent *d ATTR_UNUSED)
{
enum mailbox_list_file_type type;
#ifdef HAVE_DIRENT_D_TYPE
switch (d->d_type) {
case DT_UNKNOWN:
type = MAILBOX_LIST_FILE_TYPE_UNKNOWN;
break;
case DT_REG:
type = MAILBOX_LIST_FILE_TYPE_FILE;
break;
case DT_DIR:
type = MAILBOX_LIST_FILE_TYPE_DIR;
break;
case DT_LNK:
type = MAILBOX_LIST_FILE_TYPE_SYMLINK;
break;
default:
type = MAILBOX_LIST_FILE_TYPE_OTHER;
break;
}
#else
type = MAILBOX_LIST_FILE_TYPE_UNKNOWN;
#endif
return type;
}
const char *mailbox_list_get_last_error(struct mailbox_list *list,
enum mail_error *error_r)
{
*error_r = list->error;
return list->error_string != NULL ? list->error_string :
"Unknown internal list error";
}
void mailbox_list_clear_error(struct mailbox_list *list)
{
i_free_and_null(list->error_string);
list->error = MAIL_ERROR_NONE;
}
void mailbox_list_set_error(struct mailbox_list *list,
enum mail_error error, const char *string)
{
i_free(list->error_string);
list->error_string = i_strdup(string);
list->error = error;
}
void mailbox_list_set_internal_error(struct mailbox_list *list)
{
struct tm *tm;
char str[256];
tm = localtime(&ioloop_time);
i_free(list->error_string);
list->error_string =
strftime(str, sizeof(str), CRITICAL_MSG_STAMP, tm) > 0 ?
i_strdup(str) : i_strdup(CRITICAL_MSG);
list->error = MAIL_ERROR_TEMP;
}
void mailbox_list_set_critical(struct mailbox_list *list, 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. */
mailbox_list_set_internal_error(list);
}
bool mailbox_list_set_error_from_errno(struct mailbox_list *list)
{
const char *error_string;
enum mail_error error;
if (!mail_error_from_errno(&error, &error_string))
return FALSE;
mailbox_list_set_error(list, error, error_string);
return TRUE;
}