mailbox-list.c revision 847caf605dc11acfb1861586b558d9cca4a85cb0
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "lib.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "array.h"
dfa2201c6ac8ddb2d2798dee15662cfe774e644eMartti Rannanjärvi#include "path-util.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "ioloop.h"
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen#include "file-create-locked.h"
472369cba85d9f7c995dda60e7cd01d78b4a960aTimo Sirainen#include "mkdir-parents.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen#include "hex-binary.h"
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include "str.h"
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen#include "sha1.h"
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen#include "hash.h"
b1f37113a5760bee842c5a7678bb5fa6f5bd8b60Timo Sirainen#include "home-expand.h"
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen#include "time-util.h"
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen#include "unichar.h"
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen#include "settings-parser.h"
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen#include "iostream-ssl.h"
e28fa207d1a097fa6e4a867f74ee0761472ef1ceTimo Sirainen#include "fs-api-private.h"
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen#include "imap-utf7.h"
ef50336eefcb9ba99f73c6af37420eaf8857a39bTimo Sirainen#include "mailbox-log.h"
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen#include "mailbox-tree.h"
13d98ffa534f2e7d04a832c9d0153fc9c568b878Timo Sirainen#include "mail-storage-private.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "mail-storage-hooks.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include "mailbox-list-private.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen#include <time.h>
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen#include <ctype.h>
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include <unistd.h>
5694eeb99b69dea8033ca77ad69743c6b4871370Timo Sirainen#include <dirent.h>
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#include <sys/stat.h>
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen#define MAILBOX_LIST_LOCK_FNAME "mailboxes.lock"
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen#define MAILBOX_LIST_LOCK_SECS 60
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen#define MAILBOX_LIST_FS_CONTEXT(obj) \
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen MODULE_CONTEXT(obj, mailbox_list_fs_module)
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainenstruct mailbox_list_fs_context {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen union fs_api_module_context module_ctx;
220e21750948941dc6e33b8f11b552fa21d7f81eTimo Sirainen struct mailbox_list *list;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen};
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainenstruct mailbox_list_module_register mailbox_list_module_register = { 0 };
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenstatic ARRAY(const struct mailbox_list *) mailbox_list_drivers;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(mailbox_list_fs_module,
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen &fs_api_module_register);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenvoid mailbox_lists_init(void)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen i_array_init(&mailbox_list_drivers, 4);
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenvoid mailbox_lists_deinit(void)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen array_free(&mailbox_list_drivers);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstatic bool mailbox_list_driver_find(const char *name, unsigned int *idx_r)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const struct mailbox_list *const *drivers;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int i, count;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen drivers = array_get(&mailbox_list_drivers, &count);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen for (i = 0; i < count; i++) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (strcasecmp(drivers[i]->name, name) == 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *idx_r = i;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return TRUE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return FALSE;
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen}
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainenvoid mailbox_list_register(const struct mailbox_list *list)
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen{
8a0ad174adb1eb5108511b90e97f4e5f9089b0eeTimo Sirainen unsigned int idx;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (mailbox_list_driver_find(list->name, &idx)) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_fatal("mailbox_list_register(%s): duplicate driver",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen list->name);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen array_append(&mailbox_list_drivers, &list, 1);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid mailbox_list_unregister(const struct mailbox_list *list)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned int idx;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (!mailbox_list_driver_find(list->name, &idx)) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_fatal("mailbox_list_unregister(%s): unknown driver",
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen list->name);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen }
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen array_delete(&mailbox_list_drivers, idx, 1);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen}
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenconst struct mailbox_list *
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenmailbox_list_find_class(const char *driver)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen{
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen const struct mailbox_list *const *class_p;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen unsigned int idx;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen if (!mailbox_list_driver_find(driver, &idx))
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen return NULL;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen class_p = array_idx(&mailbox_list_drivers, idx);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return *class_p;
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen}
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenint mailbox_list_create(const char *driver, struct mail_namespace *ns,
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek const struct mailbox_list_settings *set,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum mailbox_list_flags flags,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct mailbox_list **list_r, const char **error_r)
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen{
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen const struct mailbox_list *class;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct mailbox_list *list;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_assert(ns->list == NULL ||
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen (flags & MAILBOX_LIST_FLAG_SECONDARY) != 0);
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen i_assert(set->subscription_fname == NULL ||
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *set->subscription_fname != '\0');
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if ((class = mailbox_list_find_class(driver)) == NULL) {
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek *error_r = "Unknown driver name";
63e207529879438e9f4412d97cdc34bdc82a3702Timo Sirainen return -1;
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen }
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if ((class->props & MAILBOX_LIST_PROP_NO_MAILDIR_NAME) != 0 &&
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek *set->maildir_name != '\0') {
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen *error_r = "maildir_name not supported by this driver";
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen return -1;
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen }
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen if ((class->props & MAILBOX_LIST_PROP_NO_ALT_DIR) != 0 &&
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen set->alt_dir != NULL) {
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen *error_r = "alt_dir not supported by this driver";
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek return -1;
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen }
ac2defed599a97c4a71a9e90ba185929dfe59226Josef 'Jeff' Sipek
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen i_assert(set->root_dir == NULL || *set->root_dir != '\0' ||
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen (class->props & MAILBOX_LIST_PROP_NO_ROOT) != 0);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list = class->v.alloc();
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen array_create(&list->module_contexts, list->pool, sizeof(void *), 5);
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen list->ns = ns;
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen list->mail_set = ns->mail_set;
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen list->flags = flags;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->root_permissions.file_create_mode = (mode_t)-1;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->root_permissions.dir_create_mode = (mode_t)-1;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen list->root_permissions.file_create_gid = (gid_t)-1;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen list->changelog_timestamp = (time_t)-1;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen if (set->no_noselect)
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen list->props |= MAILBOX_LIST_PROP_NO_NOSELECT;
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen /* copy settings */
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen if (set->root_dir != NULL) {
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen list->set.root_dir = p_strdup(list->pool, set->root_dir);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen list->set.index_dir = set->index_dir == NULL ||
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen strcmp(set->index_dir, set->root_dir) == 0 ? NULL :
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen p_strdup(list->pool, set->index_dir);
2a6dcd984104fed84bed8795ccdfabb20e41ce52Timo Sirainen list->set.index_pvt_dir = set->index_pvt_dir == NULL ||
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen strcmp(set->index_pvt_dir, set->root_dir) == 0 ? NULL :
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen p_strdup(list->pool, set->index_pvt_dir);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.index_cache_dir = set->index_cache_dir == NULL ||
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen strcmp(set->index_cache_dir, set->root_dir) == 0 ? NULL :
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen p_strdup(list->pool, set->index_cache_dir);
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen list->set.control_dir = set->control_dir == NULL ||
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen strcmp(set->control_dir, set->root_dir) == 0 ? NULL :
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen p_strdup(list->pool, set->control_dir);
5214b67a7dabab87da74e04bb8b227f94b95bce4Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.inbox_path = p_strdup(list->pool, set->inbox_path);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.subscription_fname =
27586e4785d56aeb76e1fd96af8db799688dc64aTimo Sirainen p_strdup(list->pool, set->subscription_fname);
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen list->set.list_index_fname =
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen p_strdup(list->pool, set->list_index_fname);
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen list->set.list_index_dir =
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen p_strdup(list->pool, set->list_index_dir);
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen list->set.maildir_name =
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen p_strdup(list->pool, set->maildir_name);
5214b67a7dabab87da74e04bb8b227f94b95bce4Timo Sirainen list->set.mailbox_dir_name =
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen p_strdup(list->pool, set->mailbox_dir_name);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.alt_dir = p_strdup(list->pool, set->alt_dir);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.alt_dir_nocheck = set->alt_dir_nocheck;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.volatile_dir = p_strdup(list->pool, set->volatile_dir);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.index_control_use_maildir_name =
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen set->index_control_use_maildir_name;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.iter_from_index_dir = set->iter_from_index_dir;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.no_noselect = set->no_noselect;
f0d09be40bd0c4423873128ae2f88a4020075dc4Timo Sirainen
f0d09be40bd0c4423873128ae2f88a4020075dc4Timo Sirainen if (*set->mailbox_dir_name == '\0')
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen list->set.mailbox_dir_name = "";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen else if (set->mailbox_dir_name[strlen(set->mailbox_dir_name)-1] == '/') {
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen list->set.mailbox_dir_name =
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen p_strdup(list->pool, set->mailbox_dir_name);
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen } else {
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen list->set.mailbox_dir_name =
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen p_strconcat(list->pool, set->mailbox_dir_name, "/", NULL);
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen }
917498e6f84969d2b93410c1e479735abe8e0ed7Timo Sirainen list->set.escape_char = set->escape_char;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.broken_char = set->broken_char;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen list->set.utf8 = set->utf8;
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk if (list->v.init != NULL) {
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk if (list->v.init(list, error_r) < 0) {
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen list->v.deinit(list);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk return -1;
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk }
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk }
7920a47321690c932ffd4d286cd16b4048d22d41Timo Sirainen
7920a47321690c932ffd4d286cd16b4048d22d41Timo Sirainen if (ns->mail_set->mail_debug) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen i_debug("%s: root=%s, index=%s, indexpvt=%s, control=%s, inbox=%s, alt=%s",
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen list->name,
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen list->set.root_dir == NULL ? "" : list->set.root_dir,
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen list->set.index_dir == NULL ? "" : list->set.index_dir,
db8b0a3f74a20528d66a3c4be7df920e5c4554c2Timo Sirainen list->set.index_pvt_dir == NULL ? "" : list->set.index_pvt_dir,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen list->set.control_dir == NULL ?
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen "" : list->set.control_dir,
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen list->set.inbox_path == NULL ?
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen "" : list->set.inbox_path,
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen list->set.alt_dir == NULL ? "" : list->set.alt_dir);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen }
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if ((flags & MAILBOX_LIST_FLAG_SECONDARY) == 0)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen mail_namespace_finish_list_init(ns, list);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen *list_r = list;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen hook_mailbox_list_created(list);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen return 0;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen}
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainenstatic int fix_path(struct mail_user *user, const char *path, bool expand_home,
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen const char **path_r, const char **error_r)
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen{
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen size_t len = strlen(path);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if (len > 1 && path[len-1] == '/')
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen path = t_strndup(path, len-1);
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if (!expand_home) {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* no ~ expansion */
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen } else if (path[0] == '~' && path[1] != '/' && path[1] != '\0') {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen /* ~otheruser/dir */
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen if (home_try_expand(&path) < 0) {
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen *error_r = t_strconcat(
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen "No home directory for system user. "
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen "Can't expand ", t_strcut(path, '/'),
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen " for ", NULL);
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen return -1;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen } else {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (mail_user_try_home_expand(user, &path) < 0) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen *error_r = "Home directory not set for user. "
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen "Can't expand ~/ for ";
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen return -1;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen *path_r = path;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen return 0;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen}
5c597df6aa8d81de4053c6986fab7739f3b44b20Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainenstatic const char *split_next_arg(const char *const **_args)
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen{
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen const char *const *args = *_args;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen const char *str = args[0];
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen args++;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen while (*args != NULL && **args == '\0') {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen args++;
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (*args == NULL) {
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen /* string ends with ":", just ignore it. */
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen break;
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch }
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen str = t_strconcat(str, ":", *args, NULL);
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen args++;
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen }
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen *_args = args;
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainen return str;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen}
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainenvoid mailbox_list_settings_init_defaults(struct mailbox_list_settings *set_r)
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen{
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen i_zero(set_r);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen set_r->mailbox_dir_name = "";
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen set_r->maildir_name = "";
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen set_r->list_index_fname = MAILBOX_LIST_INDEX_DEFAULT_PREFIX;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen}
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
33525312d3f45995686aa0b538dea1cd6eb936e2Timo Sirainenstatic int
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenmailbox_list_settings_parse_full(struct mail_user *user, const char *data,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen bool expand_home,
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen struct mailbox_list_settings *set_r,
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen const char **error_r)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen{
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen const char *const *tmp, *key, *value, **dest, *str, *fname, *error;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen *error_r = NULL;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen mailbox_list_settings_init_defaults(set_r);
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen if (*data == '\0')
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen return 0;
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen /* <root dir> */
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen tmp = t_strsplit(data, ":");
c58c12049c883b281c088d47a2a7278c21c390e1Timo Sirainen str = split_next_arg(&tmp);
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (fix_path(user, str, expand_home, &set_r->root_dir, &error) < 0) {
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen *error_r = t_strconcat(error, "mail root dir in: ", data, NULL);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen return -1;
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen }
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen if (strncmp(set_r->root_dir, "INBOX=", 6) == 0) {
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen /* probably mbox user trying to avoid root_dir */
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen *error_r = t_strconcat("Mail root directory not given: ",
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen data, NULL);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen return -1;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen while (*tmp != NULL) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen str = split_next_arg(&tmp);
c1d19144dd7b1de6822df6ed1d10af0c9cb38840Timo Sirainen if (strcmp(str, "UTF-8") == 0) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen set_r->utf8 = TRUE;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen continue;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen value = strchr(str, '=');
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen if (value == NULL) {
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen key = str;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen value = "";
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen } else {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen key = t_strdup_until(str, value);
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen value++;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen }
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen if (strcmp(key, "INBOX") == 0)
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen dest = &set_r->inbox_path;
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen else if (strcmp(key, "INDEX") == 0)
360123b1b41b7aa8af6c4a91c39046be646cd349Timo Sirainen dest = &set_r->index_dir;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen else if (strcmp(key, "INDEXPVT") == 0)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen dest = &set_r->index_pvt_dir;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen else if (strcmp(key, "INDEXCACHE") == 0)
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen dest = &set_r->index_cache_dir;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen else if (strcmp(key, "CONTROL") == 0)
5494a6bc149da8f02fd25c0434a9d612ac33f659Timo Sirainen dest = &set_r->control_dir;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen else if (strcmp(key, "ALT") == 0)
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen dest = &set_r->alt_dir;
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen else if (strcmp(key, "ALTNOCHECK") == 0) {
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen set_r->alt_dir_nocheck = TRUE;
fa780a18c41881036af582f7a3473d6399e9d34dTimo Sirainen continue;
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen } else if (strcmp(key, "LAYOUT") == 0)
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen dest = &set_r->layout;
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen else if (strcmp(key, "SUBSCRIPTIONS") == 0)
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen dest = &set_r->subscription_fname;
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen else if (strcmp(key, "DIRNAME") == 0)
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen dest = &set_r->maildir_name;
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen else if (strcmp(key, "MAILBOXDIR") == 0)
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen dest = &set_r->mailbox_dir_name;
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen else if (strcmp(key, "VOLATILEDIR") == 0)
7d2d0ae1131c8b632cc7e86000adaaf8c9ef42a9Timo Sirainen dest = &set_r->volatile_dir;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen else if (strcmp(key, "LISTINDEX") == 0)
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen dest = &set_r->list_index_fname;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen else if (strcmp(key, "FULLDIRNAME") == 0) {
12b4dbf933ee54f7b96968ba150095baa985fdafTimo Sirainen set_r->index_control_use_maildir_name = TRUE;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen dest = &set_r->maildir_name;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen } else if (strcmp(key, "BROKENCHAR") == 0) {
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen if (strlen(value) != 1) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen *error_r = "BROKENCHAR value must be a single character";
94b0ff77495c3ed14bdd4b5d7ae1eb37e8c9efb5Timo Sirainen return -1;
02e61e13a8360a9d3ec92c5fa5ae60c0f0181b71Timo Sirainen }
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen set_r->broken_char = value[0];
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen continue;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen } else if (strcmp(key, "ITERINDEX") == 0) {
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen set_r->iter_from_index_dir = TRUE;
b365bd121cdc87f63e1dd47c5085a27091118e00Timo Sirainen continue;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen } else if (strcmp(key, "NO-NOSELECT") == 0) {
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen set_r->no_noselect = TRUE;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen continue;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen } else {
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen *error_r = t_strdup_printf("Unknown setting: %s", key);
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen return -1;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen }
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (fix_path(user, value, expand_home, dest, &error) < 0) {
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen *error_r = t_strconcat(error, key, " in: ", data, NULL);
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen return -1;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen }
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen }
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (set_r->index_dir != NULL && strcmp(set_r->index_dir, "MEMORY") == 0)
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen set_r->index_dir = "";
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (set_r->iter_from_index_dir &&
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen (set_r->index_dir == NULL || set_r->index_dir[0] == '\0')) {
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen *error_r = "ITERINDEX requires INDEX to be explicitly set";
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen return -1;
adb6413686e52e00dded4932babcc08ff041876bTimo Sirainen }
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (set_r->list_index_fname != NULL &&
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen (fname = strrchr(set_r->list_index_fname, '/')) != NULL) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen /* non-default LISTINDEX directory */
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen set_r->list_index_dir =
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen t_strdup_until(set_r->list_index_fname, fname);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen set_r->list_index_fname = fname+1;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (set_r->list_index_dir[0] != '/' &&
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen set_r->index_dir != NULL && set_r->index_dir[0] == '\0') {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen *error_r = "LISTINDEX directory is relative but INDEX=MEMORY";
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return -1;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainenint mailbox_list_settings_parse(struct mail_user *user, const char *data,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mailbox_list_settings *set_r,
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen const char **error_r)
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen{
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen return mailbox_list_settings_parse_full(user, data, TRUE,
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen set_r, error_r);
1433bf361ddb0bba8878c8ada5726d0284edad57Timo Sirainen}
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenconst char *mailbox_list_get_unexpanded_path(struct mailbox_list *list,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen enum mailbox_list_path_type type)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const struct mail_storage_settings *mail_set;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *location = list->ns->unexpanded_set->location;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mail_user *user = list->ns->user;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen struct mailbox_list_settings set;
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen const char *p, *path, *error;
4a514fb20e04df397842cde11cc9ea92abfe9728Timo Sirainen
94d8e51119003d2bc5a100c663f90141f297385dTimo Sirainen if (*location == SETTING_STRVAR_EXPANDED[0]) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* set using -o or userdb lookup. */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return "";
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen i_assert(*location == SETTING_STRVAR_UNEXPANDED[0]);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen location++;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (*location == '\0') {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen mail_set = mail_user_set_get_driver_settings(user->set_info,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen user->unexpanded_set, MAIL_STORAGE_SET_DRIVER_NAME);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen i_assert(mail_set != NULL);
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen location = mail_set->mail_location;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (*location == SETTING_STRVAR_EXPANDED[0])
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return "";
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen i_assert(*location == SETTING_STRVAR_UNEXPANDED[0]);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen location++;
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen }
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen /* type:settings */
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen p = strchr(location, ':');
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen if (p == NULL)
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen return "";
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen if (mailbox_list_settings_parse_full(user, p + 1, FALSE,
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen &set, &error) < 0)
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen return "";
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen if (!mailbox_list_set_get_root_path(&set, type, &path))
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen return "";
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen return path;
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen}
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainenstatic bool need_escape_dirstart(const char *vname, const char *maildir_name)
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen{
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen size_t len;
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen
d3e5a14ea363264dcc7640ca7226249d0c27a793Timo Sirainen if (vname[0] == '.') {
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (vname[1] == '\0' || vname[1] == '/')
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen return TRUE; /* "." */
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (vname[1] == '.' && (vname[2] == '\0' || vname[2] == '/'))
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return TRUE; /* ".." */
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (*maildir_name != '\0') {
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen len = strlen(maildir_name);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (strncmp(maildir_name, vname, len) == 0 &&
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen (vname[len] == '\0' || vname[len] == '/'))
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen return TRUE; /* e.g. dbox-Mails */
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen }
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen return FALSE;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenconst char *
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenmailbox_list_escape_name_params(const char *vname, const char *ns_prefix,
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen char ns_sep, char list_sep, char escape_char,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen const char *maildir_name)
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen{
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen size_t ns_prefix_len = strlen(ns_prefix);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen string_t *escaped_name = t_str_new(64);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen bool dirstart = TRUE;
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen /* no escaping of namespace prefix */
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (strncmp(ns_prefix, vname, ns_prefix_len) == 0) {
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen str_append_n(escaped_name, vname, ns_prefix_len);
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen vname += ns_prefix_len;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen /* escape the mailbox name */
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (*vname == '~') {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen str_printfa(escaped_name, "%c%02x", escape_char, *vname);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen vname++;
d508ab8db2b0f74b5e225d199b4aaa5293342746Timo Sirainen dirstart = FALSE;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen for (; *vname != '\0'; vname++) {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (*vname == ns_sep)
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen str_append_c(escaped_name, list_sep);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen else if (*vname == list_sep ||
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen *vname == escape_char ||
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen *vname == '/' ||
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen (dirstart &&
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen need_escape_dirstart(vname, maildir_name))) {
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen str_printfa(escaped_name, "%c%02x",
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen escape_char, *vname);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen } else {
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen str_append_c(escaped_name, *vname);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen dirstart = *vname == '/';
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return str_c(escaped_name);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenconst char *
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenmailbox_list_escape_name(struct mailbox_list *list, const char *vname)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return mailbox_list_escape_name_params(vname, list->ns->prefix,
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen mail_namespace_get_sep(list->ns),
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen mailbox_list_get_hierarchy_sep(list),
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen list->set.escape_char, list->set.maildir_name);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenstatic int
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenmailbox_list_unescape_broken_chars(struct mailbox_list *list, char *name)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen char *src, *dest;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen unsigned char chr;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if ((src = strchr(name, list->set.broken_char)) == NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return 0;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen dest = src;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen while (*src != '\0') {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (*src == list->set.broken_char) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (src[1] >= '0' && src[1] <= '9')
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen chr = (src[1]-'0') * 0x10;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen else if (src[1] >= 'a' && src[1] <= 'f')
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen chr = (src[1]-'a' + 10) * 0x10;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen else
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return -1;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (src[2] >= '0' && src[2] <= '9')
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen chr += src[2]-'0';
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen else if (src[2] >= 'a' && src[2] <= 'f')
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen chr += src[2]-'a' + 10;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen else
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return -1;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen *dest++ = chr;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen src += 3;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen } else {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen *dest++ = *src++;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen *dest++ = '\0';
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return 0;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen}
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenstatic char *mailbox_list_convert_sep(const char *storage_name, char src, char dest)
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen{
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen char *ret, *p;
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen ret = p_strdup(unsafe_data_stack_pool, storage_name);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen for (p = ret; *p != '\0'; p++) {
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen if (*p == src)
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen *p = dest;
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return ret;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
494a5de15db3b2806ab31d5ecc3e1c306ae14d06Timo Sirainen
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainenconst char *mailbox_list_default_get_storage_name(struct mailbox_list *list,
e58f291e777b25c1286965b80e142ada6fdacb03Timo Sirainen const char *vname)
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen{
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen struct mail_namespace *ns = list->ns;
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen size_t prefix_len = strlen(ns->prefix);
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen const char *storage_name = vname;
9d4c027e7de01ab948d6221bc27c9b45d32d1ea5Timo Sirainen string_t *str;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen char list_sep, ns_sep, *ret;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen if (strcasecmp(storage_name, "INBOX") == 0 &&
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0)
08ed4ab71fd2a4e800d9025a736f0f46b771ea90Timo Sirainen storage_name = "INBOX";
08ed4ab71fd2a4e800d9025a736f0f46b771ea90Timo Sirainen else if (list->set.escape_char != '\0')
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen storage_name = mailbox_list_escape_name(list, vname);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (prefix_len > 0 && (strcmp(storage_name, "INBOX") != 0 ||
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_USER) == 0)) {
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen /* skip namespace prefix, except if this is INBOX */
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen if (strncmp(ns->prefix, storage_name, prefix_len) == 0)
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen storage_name += prefix_len;
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen else if (strncmp(ns->prefix, storage_name, prefix_len-1) == 0 &&
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen strlen(storage_name) == prefix_len-1 &&
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen ns->prefix[prefix_len-1] == mail_namespace_get_sep(ns)) {
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen /* trying to access the namespace prefix itself */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen storage_name = "";
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* we're converting a nonexistent mailbox name,
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen such as a LIST pattern. */
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen }
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen }
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen if (!list->set.utf8) {
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen /* UTF-8 -> mUTF-7 conversion */
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen str = t_str_new(strlen(storage_name)*2);
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen if (imap_utf8_to_utf7(storage_name, str) < 0)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i_panic("Mailbox name not UTF-8: %s", vname);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen storage_name = str_c(str);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen list_sep = mailbox_list_get_hierarchy_sep(list);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen ns_sep = mail_namespace_get_sep(ns);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (*storage_name == '\0' && ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_ANY) != 0 &&
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen !list->mail_set->mail_shared_explicit_inbox) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen /* opening shared/$user. it's the same as INBOX. */
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen storage_name = "INBOX";
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (list_sep != ns_sep && list->set.escape_char == '\0') {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) == 0) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen /* shared namespace root. the backend storage's
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen hierarchy separator isn't known yet, so do
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen nothing. */
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return storage_name;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ret = mailbox_list_convert_sep(storage_name, ns_sep, list_sep);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else if (list->set.broken_char == '\0' ||
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen strchr(storage_name, list->set.broken_char) == NULL) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* no need to convert broken chars */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return storage_name;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ret = p_strdup(unsafe_data_stack_pool, storage_name);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
d3e5a14ea363264dcc7640ca7226249d0c27a793Timo Sirainen if (list->set.broken_char != '\0') {
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (mailbox_list_unescape_broken_chars(list, ret) < 0) {
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen ret = mailbox_list_convert_sep(storage_name,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen ns_sep, list_sep);
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return ret;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainenconst char *mailbox_list_get_storage_name(struct mailbox_list *list,
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen const char *vname)
8f70c97f7ab7b7e1683ed5cfcd96721a899c2520Timo Sirainen{
8f70c97f7ab7b7e1683ed5cfcd96721a899c2520Timo Sirainen return list->v.get_storage_name(list, vname);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen}
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenconst char *
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainenmailbox_list_unescape_name_params(const char *src, const char *ns_prefix,
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen char ns_sep, char list_sep, char escape_char)
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen{
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen size_t ns_prefix_len = strlen(ns_prefix);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen string_t *dest = t_str_new(strlen(src));
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen unsigned int num;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (strncmp(src, ns_prefix, ns_prefix_len) == 0) {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen str_append_n(dest, src, ns_prefix_len);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen src += ns_prefix_len;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen for (; *src != '\0'; src++) {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (*src == escape_char &&
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen i_isxdigit(src[1]) && i_isxdigit(src[2])) {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (src[1] >= '0' && src[1] <= '9')
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen num = src[1] - '0';
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen else
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen num = i_toupper(src[1]) - 'A' + 10;
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen num *= 16;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen if (src[2] >= '0' && src[2] <= '9')
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen num += src[2] - '0';
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen else
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen num += i_toupper(src[2]) - 'A' + 10;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen str_append_c(dest, num);
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen src += 2;
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen } else if (*src == list_sep)
a4e2101473cfd7ce960fc49b3ce097c3f89ec2adTimo Sirainen str_append_c(dest, ns_sep);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen else
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen str_append_c(dest, *src);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return str_c(dest);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenconst char *
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenmailbox_list_unescape_name(struct mailbox_list *list, const char *src)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return mailbox_list_unescape_name_params(src, list->ns->prefix,
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen mail_namespace_get_sep(list->ns),
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen mailbox_list_get_hierarchy_sep(list),
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen list->set.escape_char);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenstatic void
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainenmailbox_list_escape_broken_chars(struct mailbox_list *list, string_t *str)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen{
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen unsigned int i;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen char buf[3];
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (strchr(str_c(str), list->set.broken_char) == NULL)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen for (i = 0; i < str_len(str); i++) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (str_c(str)[i] == list->set.broken_char) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i_snprintf(buf, sizeof(buf), "%02x",
1f4399a277b861419b82758ab0462e90c00a4c41Timo Sirainen list->set.broken_char);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen str_insert(str, i+1, buf);
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen i += 2;
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen}
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenstatic void
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenmailbox_list_escape_broken_name(struct mailbox_list *list,
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen const char *vname, string_t *str)
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen{
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen str_truncate(str, 0);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen for (; *vname != '\0'; vname++) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (*vname == '&' || (unsigned char)*vname >= 0x80) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen str_printfa(str, "%c%02x", list->set.broken_char,
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen (unsigned char)*vname);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else {
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen str_append_c(str, *vname);
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen }
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen }
9b00ecffbe74fd864d0d72e6112ec53b86f619baTimo Sirainen}
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainenconst char *mailbox_list_default_get_vname(struct mailbox_list *list,
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen const char *storage_name)
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen{
0bd259973f98837cf0e41fdee3e2a578e51ad09eTimo Sirainen size_t i, prefix_len, name_len;
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen const char *vname = storage_name;
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen char list_sep, ns_sep, *ret;
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen
e4cb3bfcd42f1f2c9e676ece6f7f53803f5c6a16Timo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen strcmp(vname, "INBOX") == 0 &&
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen list->ns->user == list->ns->owner) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* user's INBOX - use as-is. NOTE: don't do case-insensitive
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen comparison, otherwise we can't differentiate between INBOX
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen and <ns prefix>/inBox. */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return vname;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (strcmp(vname, "INBOX") == 0 &&
1c1cecd3dfaf71b0c9499b044023e631841e88aaTimo Sirainen list->ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen (list->ns->flags & NAMESPACE_FLAG_INBOX_ANY) != 0 &&
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen !list->mail_set->mail_shared_explicit_inbox) {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen /* convert to shared/$user, we don't really care about the
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen INBOX suffix here. */
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen vname = "";
636f017be100bce67d66fd3ae1544a47681efd33Timo Sirainen }
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (*vname == '\0') {
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen /* return namespace prefix without the separator */
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen if (list->ns->prefix_len == 0)
69af83d4e6c2c5c825a17edd7a41a4fb014caa8fTimo Sirainen return list->ns->prefix;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen else {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return t_strndup(list->ns->prefix,
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen list->ns->prefix_len - 1);
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen }
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen } else if (!list->set.utf8) {
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen /* mUTF-7 -> UTF-8 conversion */
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen string_t *str = t_str_new(strlen(vname));
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen if (imap_utf7_to_utf8(vname, str) == 0) {
6f970b9a0dadb80e120d017c75c637b5a3879dacTimo Sirainen if (list->set.broken_char != '\0')
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen mailbox_list_escape_broken_chars(list, str);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen vname = str_c(str);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen } else if (list->set.broken_char != '\0') {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen mailbox_list_escape_broken_name(list, vname, str);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen vname = str_c(str);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
e7d0bea63a08b08c47c4b5c187d2cb7127859657Timo Sirainen }
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen prefix_len = strlen(list->ns->prefix);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen if (list->set.escape_char != '\0') {
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen vname = mailbox_list_unescape_name(list, vname);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen return prefix_len == 0 ? vname :
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen t_strconcat(list->ns->prefix, vname, NULL);
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
dc5606fb66d30a659459446b6ca1a8b4f1146052Timo Sirainen list_sep = mailbox_list_get_hierarchy_sep(list);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ns_sep = mail_namespace_get_sep(list->ns);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (list_sep != ns_sep || prefix_len > 0) {
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen /* @UNSAFE */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen name_len = strlen(vname);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ret = t_malloc_no0(MALLOC_ADD(prefix_len, name_len) + 1);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen memcpy(ret, list->ns->prefix, prefix_len);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen for (i = 0; i < name_len; i++) {
1701e3f91107051b1704721bf1dc1e32491faaf9Timo Sirainen ret[i + prefix_len] =
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen vname[i] == list_sep ? ns_sep : vname[i];
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ret[i + prefix_len] = '\0';
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen vname = ret;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return vname;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen}
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainenconst char *mailbox_list_get_vname(struct mailbox_list *list, const char *name)
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen{
bd4e36a8cd7257cca7d1434c49a1e343ed7c5100Timo Sirainen return list->v.get_vname(list, name);
cb933f0a570a9cef5c975eadb818aa6b1002a269Timo Sirainen}
cb933f0a570a9cef5c975eadb818aa6b1002a269Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenvoid mailbox_list_destroy(struct mailbox_list **_list)
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen{
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen struct mailbox_list *list = *_list;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen *_list = NULL;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen i_free_and_null(list->error_string);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen i_free(list->last_internal_error);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (hash_table_is_created(list->guid_cache)) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen hash_table_destroy(&list->guid_cache);
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen pool_unref(&list->guid_cache_pool);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (list->subscriptions != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen mailbox_tree_deinit(&list->subscriptions);
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen if (list->changelog != NULL)
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen mailbox_log_free(&list->changelog);
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen
e36574dadcac802d6780fa94ee45951e75594c96Timo Sirainen if (array_is_created(&list->error_stack)) {
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen i_assert(array_count(&list->error_stack) == 0);
ae2b61a8c6318e56dabd44de17e227c95985aedaTimo Sirainen array_free(&list->error_stack);
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen }
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen list->v.deinit(list);
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen}
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainenconst char *mailbox_list_get_driver_name(const struct mailbox_list *list)
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen{
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen return list->name;
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen}
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainen
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainenconst struct mailbox_list_settings *
c58906589cafc32df4c04ffbef933baadd3f2276Timo Sirainenmailbox_list_get_settings(const struct mailbox_list *list)
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen{
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen return &list->set;
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen}
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainenenum mailbox_list_flags mailbox_list_get_flags(const struct mailbox_list *list)
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen{
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen return list->flags;
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen}
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainenstruct mail_namespace *
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainenmailbox_list_get_namespace(const struct mailbox_list *list)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen{
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen return list->ns;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen}
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainenstatic mode_t get_dir_mode(mode_t mode)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen{
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen /* add the execute bit if either read or write bit is set */
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if ((mode & 0600) != 0) mode |= 0100;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if ((mode & 0060) != 0) mode |= 0010;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if ((mode & 0006) != 0) mode |= 0001;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen return mode;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen}
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainenstruct mail_user *
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainenmailbox_list_get_user(const struct mailbox_list *list)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen{
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen return list->ns->user;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen}
4b1781e4c64be52e25b5994e5242dbe696cc7d29Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainenstatic int
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainenmailbox_list_get_storage_driver(struct mailbox_list *list, const char *driver,
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen struct mail_storage **storage_r)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen{
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen struct mail_storage *const *storagep;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen const char *error, *data;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen array_foreach(&list->ns->all_storages, storagep) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (strcmp((*storagep)->name, driver) == 0) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen *storage_r = *storagep;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen return 0;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen data = i_strchr_to_next(list->ns->set->location, ':');
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen if (data == NULL)
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen data = "";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (mail_storage_create_full(list->ns, driver, data, 0,
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen storage_r, &error) < 0) {
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen mailbox_list_set_critical(list,
15bfe73fb3988bb80e6afe6a60b9a715c7207600Timo Sirainen "Namespace %s: Failed to create storage '%s': %s",
4809537f0c5a2e1cee9559ec842cc869884d2cb7Timo Sirainen list->ns->prefix, driver, error);
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen return -1;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen }
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return 0;
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen}
d2e74f2af690b8e2d536400f02f397cbed1334b7Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenint mailbox_list_get_storage(struct mailbox_list **list, const char *vname,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct mail_storage **storage_r)
8709b2fe6ec2b5ca1d90a63490f8371472062efdTimo Sirainen{
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const struct mailbox_settings *set;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
949fa97a4ab5c62e4db73c3973e35ae3b73a2b23Timo Sirainen if ((*list)->v.get_storage != NULL)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen return (*list)->v.get_storage(list, vname, storage_r);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen set = mailbox_settings_find((*list)->ns, vname);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen if (set != NULL && set->driver != NULL && set->driver[0] != '\0') {
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen return mailbox_list_get_storage_driver(*list, set->driver,
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen storage_r);
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen }
e353d03d3643f380dc7e6dc29a512dec86b8a3deTimo Sirainen *storage_r = mail_namespace_get_default_storage((*list)->ns);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen return 0;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen}
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainenvoid mailbox_list_get_default_storage(struct mailbox_list *list,
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen struct mail_storage **storage)
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen{
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen *storage = mail_namespace_get_default_storage(list->ns);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen}
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainenchar mailbox_list_get_hierarchy_sep(struct mailbox_list *list)
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen{
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* the current API doesn't allow returning an error, so imap code
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen looks at the list's last error. make sure the error is cleared
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen so the error-check doesn't return something irrelevant */
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen mailbox_list_clear_error(list);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen return list->v.get_hierarchy_sep(list);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen}
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainenstatic bool
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainenmailbox_list_get_permissions_stat(struct mailbox_list *list, const char *path,
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen struct mailbox_permissions *permissions_r)
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen{
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen struct stat st;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (stat(path, &st) < 0) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (errno == EACCES) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen mailbox_list_set_critical(list, "%s",
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen mail_error_eacces_msg("stat", path));
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen } else if (!ENOTFOUND(errno)) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen mailbox_list_set_critical(list, "stat(%s) failed: %m",
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen path);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen } else if (list->mail_set->mail_debug) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen i_debug("Namespace %s: %s doesn't exist yet, "
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen "using default permissions",
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen list->ns->prefix, path);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen return FALSE;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_uid = st.st_uid;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_gid = st.st_gid;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_mode = (st.st_mode & 0666) | 0600;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->dir_create_mode = (st.st_mode & 0777) | 0700;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_gid_origin = path;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (!S_ISDIR(st.st_mode)) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* we're getting permissions from a file.
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen apply +x modes as necessary. */
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->dir_create_mode =
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen get_dir_mode(permissions_r->dir_create_mode);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (S_ISDIR(st.st_mode) && (st.st_mode & S_ISGID) != 0) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* directory's GID is used automatically for new files */
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen } else if ((st.st_mode & 0070) >> 3 == (st.st_mode & 0007)) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* group has same permissions as world, so don't bother
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen changing it */
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen } else if (getegid() == st.st_gid) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* using our own gid, no need to change it */
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen } else {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_gid = st.st_gid;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (!S_ISDIR(st.st_mode) &&
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen permissions_r->file_create_gid != (gid_t)-1) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* we need to stat() the parent directory to see if
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen it has setgid-bit set */
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen const char *p = strrchr(path, '/');
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen const char *parent_path = p == NULL ? NULL :
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen t_strdup_until(path, p);
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen if (parent_path != NULL &&
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen stat(parent_path, &st) == 0 &&
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen (st.st_mode & S_ISGID) != 0) {
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch /* directory's GID is used automatically for
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen new files */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen }
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen }
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen return TRUE;
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen}
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainenstatic void ATTR_NULL(2)
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainenmailbox_list_get_permissions_internal(struct mailbox_list *list,
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen const char *name,
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen struct mailbox_permissions *permissions_r)
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen{
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen const char *path = NULL, *parent_name, *p;
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen i_zero(permissions_r);
4fce6ce68d13836faf6d68f35855eba4573b42b2Timo Sirainen
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen /* use safe defaults */
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen permissions_r->file_uid = (uid_t)-1;
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen permissions_r->file_gid = (gid_t)-1;
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen permissions_r->file_create_mode = 0600;
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen permissions_r->dir_create_mode = 0700;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen permissions_r->file_create_gid_origin = "defaults";
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (list->set.iter_from_index_dir ||
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen (list->flags & MAILBOX_LIST_FLAG_NO_MAIL_FILES) != 0) {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen /* a) iterating from index dir. Use the index dir's permissions
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen as well, since they might be in a faster storage.
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
758d8b46f9e8fd87cf58bb4912cddf6bf28918cfTimo Sirainen b) mail files don't exist in storage, but index files
6763c5f6246a0705b141d99d32fe9e2096042bd3Timo Sirainen might. */
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen (void)mailbox_list_get_path(list, name,
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen MAILBOX_LIST_PATH_TYPE_INDEX, &path);
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (name != NULL && path == NULL) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_DIR,
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen &path) < 0)
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen name = NULL;
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (name == NULL && path == NULL) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen (void)mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_DIR,
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen &path);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen }
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen if (path == NULL) {
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen /* no filesystem support in storage */
4225ae95dd2110fe678ee70c64be14784a53c364Timo Sirainen } else if (mailbox_list_get_permissions_stat(list, path, permissions_r)) {
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen /* got permissions from the given path */
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen permissions_r->gid_origin_is_mailbox_path = name != NULL;
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen } else if (name != NULL) {
47d5cc09738defd0020c797866e2a25a344aa65aTimo Sirainen /* path couldn't be stat()ed, try parent mailbox */
6dd77763f5451269ace733579cf58f2f3b18bca4Timo Sirainen p = strrchr(name, mailbox_list_get_hierarchy_sep(list));
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen if (p == NULL) {
2f552aa8fb0fc86473c2d7dea85d8237373fb35fTimo Sirainen /* return root defaults */
2f552aa8fb0fc86473c2d7dea85d8237373fb35fTimo Sirainen parent_name = NULL;
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen } else {
d02d34e138e32b4266f5a403d6c51d7803bf322fTimo Sirainen parent_name = t_strdup_until(name, p);
fe813f74aaccb12f38e1bd9cd338c6a37fa646e5Timo Sirainen }
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volk mailbox_list_get_permissions(list, parent_name,
dbc86748934bc34826d9cc8c43d24df09b3ba402Timo Sirainen permissions_r);
a24519c36d5f8fa22f58b2c693ba547e8d175a54Timo Sirainen return;
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen } else {
dbc86748934bc34826d9cc8c43d24df09b3ba402Timo Sirainen /* assume current defaults for mailboxes that don't exist or
dbc86748934bc34826d9cc8c43d24df09b3ba402Timo Sirainen can't be looked up for some other reason */
c78e7a94528078728cc639b26a1c83e11b4d7e1bTimo Sirainen permissions_r->file_uid = geteuid();
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen permissions_r->file_gid = getegid();
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen }
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen if (name == NULL) {
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen mailbox_permissions_copy(&list->root_permissions, permissions_r,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen list->pool);
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen }
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen if (list->mail_set->mail_debug && name == NULL) {
17cf149e5f47183bfcc1503649dfb92a14f9dcd9Timo Sirainen i_debug("Namespace %s: Using permissions from %s: "
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen "mode=0%o gid=%s", list->ns->prefix,
17cf149e5f47183bfcc1503649dfb92a14f9dcd9Timo Sirainen path != NULL ? path : "",
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen (int)permissions_r->dir_create_mode,
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen permissions_r->file_create_gid == (gid_t)-1 ? "default" :
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen dec2str(permissions_r->file_create_gid));
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen }
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen}
490f66d6476d51cc02333d6eb398a5cd94b67f48Timo Sirainen
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainenvoid mailbox_list_get_permissions(struct mailbox_list *list, const char *name,
fea7b8b3fc182e415b1875d79587c0aa1adb09d7Timo Sirainen struct mailbox_permissions *permissions_r)
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen{
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen mailbox_list_get_permissions_internal(list, name, permissions_r);
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen}
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainenvoid mailbox_list_get_root_permissions(struct mailbox_list *list,
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen struct mailbox_permissions *permissions_r)
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen{
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen if (list->root_permissions.file_create_mode != (mode_t)-1)
5069b6adc4acb0efb3c6e87e778b820bae9bae9bTimo Sirainen *permissions_r = list->root_permissions;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen else {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen mailbox_list_get_permissions_internal(list, NULL,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen permissions_r);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenvoid mailbox_permissions_copy(struct mailbox_permissions *dest,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const struct mailbox_permissions *src,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen pool_t pool)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen *dest = *src;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen dest->file_create_gid_origin =
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen p_strdup(pool, src->file_create_gid_origin);
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenstatic const char *
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainenget_expanded_path(const char *unexpanded_start, const char *unexpanded_stop,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *expanded_full)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen{
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen const char *ret;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen unsigned int i, slash_count = 0, slash2_count = 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* get the expanded path up to the same amount of '/' characters.
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if there isn't the same amount of '/' characters, it means %variable
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen expansion added more of them and we can't handle this. */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen for (i = 0; unexpanded_start+i != unexpanded_stop; i++) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (unexpanded_start[i] == '/')
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen slash_count++;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen for (; unexpanded_start[i] != '\0'; i++) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (unexpanded_start[i] == '/')
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen slash2_count++;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen for (i = 0; expanded_full[i] != '\0'; i++) {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (expanded_full[i] == '/') {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (slash_count == 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen break;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen slash_count--;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (slash_count != 0)
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen return "";
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen ret = t_strndup(expanded_full, i);
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen for (; expanded_full[i] != '\0'; i++) {
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen if (expanded_full[i] == '/') {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (slash2_count == 0)
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen return "";
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen slash2_count--;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (slash2_count != 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return "";
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return ret;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen}
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainenstatic int
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainenmailbox_list_try_mkdir_root_parent(struct mailbox_list *list,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen enum mailbox_list_path_type type,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen struct mailbox_permissions *perm,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen const char **error_r)
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen{
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen const char *expanded, *unexpanded, *root_dir, *p;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen struct stat st;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen bool home = FALSE;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* get the directory path up to last %variable. for example
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen unexpanded path may be "/var/mail/%d/%2n/%n/Maildir", and we want
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen to get expanded="/var/mail/domain/nn" */
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen unexpanded = mailbox_list_get_unexpanded_path(list, type);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen p = strrchr(unexpanded, '%');
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if ((p == unexpanded && p[1] == 'h') ||
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen (p == NULL && unexpanded[0] == '~')) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen /* home directory used */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (!mailbox_list_get_root_path(list, type, &expanded))
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen i_unreached();
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen home = TRUE;
70a2782edc5cb342ed67a46ce30a95ca316f86c9Timo Sirainen } else if (p == NULL) {
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen return 0;
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen } else {
c905571984fe8ccdb6f2a266b813f19b0643a66cTimo Sirainen while (p != unexpanded && *p != '/') p--;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (p == unexpanded)
70a2782edc5cb342ed67a46ce30a95ca316f86c9Timo Sirainen return 0;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (!mailbox_list_get_root_path(list, type, &expanded))
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen i_unreached();
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen expanded = get_expanded_path(unexpanded, p, expanded);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (*expanded == '\0')
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen return 0;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen }
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen /* get the first existing parent directory's permissions */
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (stat_first_parent(expanded, &root_dir, &st) < 0) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen *error_r = errno == EACCES ?
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen mail_error_eacces_msg("stat", root_dir) :
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen t_strdup_printf("stat(%s) failed: %m", root_dir);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen return -1;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen /* if the parent directory doesn't have setgid-bit enabled, we don't
4b8a6aec5fe2daa564799867f31e39e7ea46b9c3Timo Sirainen copy any permissions from it. */
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if ((st.st_mode & S_ISGID) == 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (!home) {
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen /* assuming we have e.g. /var/vmail/%d/%n directory, here we
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen want to create up to /var/vmail/%d with permissions from
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen the parent directory. we never want to create the %n
2cb0a0438b625cd399a4aaa0615d69f4b54d349aTimo Sirainen directory itself. */
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen if (root_dir == expanded) {
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen /* this is the %n directory */
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen } else {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (mkdir_parents_chgrp(expanded, st.st_mode,
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen (gid_t)-1, root_dir) < 0 &&
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen errno != EEXIST) {
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen *error_r = t_strdup_printf(
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen "mkdir(%s) failed: %m", expanded);
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen return -1;
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen }
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen }
0bf25546c91ccafff9e2cc93368d2d25acb5c39eTimo Sirainen if (perm->file_create_gid == (gid_t)-1 &&
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen (perm->dir_create_mode & S_ISGID) == 0) {
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen /* change the group for user directories */
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->dir_create_mode |= S_ISGID;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->file_create_gid = getegid();
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->file_create_gid_origin = "egid";
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->gid_origin_is_mailbox_path = FALSE;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen }
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen } else {
4a14ae55292476bc0b8eb314d602a262ab094a52Timo Sirainen /* when using %h and the parent has setgid-bit,
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen copy the permissions from it for the home we're creating */
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->file_create_mode = st.st_mode & 0666;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->dir_create_mode = st.st_mode;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->file_create_gid = (gid_t)-1;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->file_create_gid_origin = "parent";
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm->gid_origin_is_mailbox_path = FALSE;
d85f37fe02fadcd5144560495a7196133c8ec947Timo Sirainen }
d85f37fe02fadcd5144560495a7196133c8ec947Timo Sirainen return 0;
d85f37fe02fadcd5144560495a7196133c8ec947Timo Sirainen}
d85f37fe02fadcd5144560495a7196133c8ec947Timo Sirainen
d85f37fe02fadcd5144560495a7196133c8ec947Timo Sirainenint mailbox_list_try_mkdir_root(struct mailbox_list *list, const char *path,
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen enum mailbox_list_path_type type,
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen const char **error_r)
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen{
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen const char *root_dir;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen struct stat st;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen struct mailbox_permissions perm;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen if (stat(path, &st) == 0) {
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen /* looks like it already exists, don't bother checking
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen further. */
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen if (!S_ISDIR(st.st_mode)) {
4a14ae55292476bc0b8eb314d602a262ab094a52Timo Sirainen *error_r = t_strdup_printf(
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen "Root directory is a file: %s", path);
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen return -1;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen }
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen return 0;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen mailbox_list_get_root_permissions(list, &perm);
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen if (!mailbox_list_get_root_path(list, type, &root_dir))
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen i_unreached();
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen i_assert(strncmp(root_dir, path, strlen(root_dir)) == 0);
4b8a6aec5fe2daa564799867f31e39e7ea46b9c3Timo Sirainen if (strcmp(root_dir, path) != 0 && stat(root_dir, &st) == 0) {
4b8a6aec5fe2daa564799867f31e39e7ea46b9c3Timo Sirainen /* creating a subdirectory under an already existing root dir.
4b8a6aec5fe2daa564799867f31e39e7ea46b9c3Timo Sirainen use the root's permissions */
4b8a6aec5fe2daa564799867f31e39e7ea46b9c3Timo Sirainen } else {
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen if (mailbox_list_try_mkdir_root_parent(list, type,
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen &perm, error_r) < 0)
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen return -1;
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen }
0b4e1043e596bfb36d999dacbf1d4d63ee96d75fTimo Sirainen
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen /* the rest of the directories exist only for one user. create them
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen with default directory permissions */
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen if (mkdir_parents_chgrp(path, perm.dir_create_mode,
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm.file_create_gid,
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen perm.file_create_gid_origin) < 0 &&
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen errno != EEXIST) {
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen if (errno == EACCES)
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen *error_r = mail_error_create_eacces_msg("mkdir", path);
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen else
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen *error_r = t_strdup_printf("mkdir(%s) failed: %m", path);
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen return -1;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen }
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen return 0;
94f84d1c3f786d1b92dd2a1507f83a2dad887c56Timo Sirainen}
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainenint mailbox_list_mkdir_root(struct mailbox_list *list, const char *path,
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen enum mailbox_list_path_type type)
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen{
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen const char *error;
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen if (mailbox_list_try_mkdir_root(list, path, type, &error) < 0) {
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen mailbox_list_set_critical(list, "%s", error);
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen return -1;
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen }
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen if (type == MAILBOX_LIST_PATH_TYPE_INDEX)
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen list->index_root_dir_created = TRUE;
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen return 0;
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen}
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainenstatic bool
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainenmailbox_list_is_valid_fs_name(struct mailbox_list *list, const char *name,
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen const char **error_r)
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen{
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen bool ret, allow_internal_dirs;
6d931bbce16786df431e9ae8201a78a95084316dTimo Sirainen
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen *error_r = NULL;
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen if (list->mail_set->mail_full_filesystem_access)
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen return TRUE;
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen /* make sure it's not absolute path */
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen if (*name == '/') {
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen *error_r = "Begins with '/'";
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen return FALSE;
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen }
a58e63b4928017b5af123967c1c98f8bcb242a9aTimo Sirainen if (*name == '~') {
6d931bbce16786df431e9ae8201a78a95084316dTimo Sirainen *error_r = "Begins with '~'";
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen return FALSE;
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen }
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen /* make sure the mailbox name doesn't contain any foolishness:
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen "../" could give access outside the mailbox directory.
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen "./" and "//" could fool ACL checks.
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen some mailbox formats have reserved directory names, such as
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen Maildir's cur/new/tmp. if any of those would conflict with the
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen mailbox directory name, it's not valid. maildir++ is kludged here as
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen a special case because all of its mailbox dirs begin with "." */
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen allow_internal_dirs = list->v.is_internal_name == NULL ||
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen *list->set.maildir_name != '\0' ||
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen strcmp(list->name, MAILBOX_LIST_NAME_MAILDIRPLUSPLUS) == 0;
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen T_BEGIN {
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen const char *const *names;
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen names = t_strsplit(name, "/");
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen for (; *names != NULL; names++) {
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen const char *n = *names;
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen if (*n == '\0') {
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen *error_r = "Has adjacent '/' chars";
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen break; /* // */
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen }
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen if (*n == '.') {
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen if (n[1] == '\0') {
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen *error_r = "Contains '.' part";
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen break; /* ./ */
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen }
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen if (n[1] == '.' && n[2] == '\0') {
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen *error_r = "Contains '..' part";
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen break; /* ../ */
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen }
6d931bbce16786df431e9ae8201a78a95084316dTimo Sirainen }
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen if (*list->set.maildir_name != '\0' &&
6d931bbce16786df431e9ae8201a78a95084316dTimo Sirainen strcmp(list->set.maildir_name, n) == 0) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* don't allow maildir_name to be used as part
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen of the mailbox name */
442b4c6c0d69e04297c00beb1a06da9855cb5ebeTimo Sirainen *error_r = "Contains reserved name";
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen break;
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (!allow_internal_dirs &&
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen list->v.is_internal_name(list, n)) {
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen *error_r = "Contains reserved name";
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen break;
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen }
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen }
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen ret = *names == NULL;
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen } T_END;
64ee8113a88e5d6e3cba7b1c4abd537cc2632bb9Timo Sirainen
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen return ret;
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen}
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainenbool mailbox_list_is_valid_name(struct mailbox_list *list,
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen const char *name, const char **error_r)
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen{
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen if (*name == '\0') {
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen if (*list->ns->prefix != '\0') {
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen /* an ugly way to get to mailbox root (e.g. Maildir/
625042f95d7eb71f6b94a61ea4f461866be1bef9Timo Sirainen when it's not the INBOX) */
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen return TRUE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *error_r = "Name is empty";
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return FALSE;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen }
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* either the list backend uses '/' as the hierarchy separator or
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen it doesn't use filesystem at all (PROP_NO_ROOT) */
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if ((list->props & MAILBOX_LIST_PROP_NO_ROOT) == 0 &&
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen mailbox_list_get_hierarchy_sep(list) != '/' &&
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen strchr(name, '/') != NULL) {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen *error_r = "Name must not have '/' characters";
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return FALSE;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return mailbox_list_is_valid_fs_name(list, name, error_r);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen}
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainenint mailbox_list_get_path(struct mailbox_list *list, const char *name,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen enum mailbox_list_path_type type,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen const char **path_r)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen{
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen int ret;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if ((ret = list->v.get_path(list, name, type, path_r)) <= 0)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen *path_r = NULL;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen else
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_assert(*path_r != NULL);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return ret;
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen}
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainenbool mailbox_list_get_root_path(struct mailbox_list *list,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen enum mailbox_list_path_type type,
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen const char **path_r)
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen{
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen int ret;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if ((ret = list->v.get_path(list, NULL, type, path_r)) < 0)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_unreached();
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (ret == 0)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen *path_r = NULL;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen else
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_assert(*path_r != NULL);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return ret > 0;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen}
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainenconst char *mailbox_list_get_root_forced(struct mailbox_list *list,
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen enum mailbox_list_path_type type)
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen{
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen const char *path;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen if (!mailbox_list_get_root_path(list, type, &path))
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_unreached();
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return path;
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen}
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainenbool mailbox_list_set_get_root_path(const struct mailbox_list_settings *set,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen enum mailbox_list_path_type type,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen const char **path_r)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen{
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen const char *path = NULL;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen switch (type) {
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen case MAILBOX_LIST_PATH_TYPE_DIR:
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen path = set->root_dir;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen break;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen case MAILBOX_LIST_PATH_TYPE_ALT_DIR:
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen path = set->alt_dir;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen break;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen case MAILBOX_LIST_PATH_TYPE_MAILBOX:
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (*set->mailbox_dir_name == '\0')
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen path = set->root_dir;
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen else {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen path = t_strconcat(set->root_dir, "/",
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen set->mailbox_dir_name, NULL);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen path = t_strndup(path, strlen(path)-1);
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen }
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen break;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen case MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX:
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (*set->mailbox_dir_name == '\0')
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen path = set->root_dir;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen else if (set->alt_dir != NULL) {
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen path = t_strconcat(set->alt_dir, "/",
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen set->mailbox_dir_name, NULL);
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen path = t_strndup(path, strlen(path)-1);
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen }
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen break;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen case MAILBOX_LIST_PATH_TYPE_CONTROL:
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen path = set->control_dir != NULL ?
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen set->control_dir : set->root_dir;
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen break;
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen case MAILBOX_LIST_PATH_TYPE_LIST_INDEX:
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen if (set->list_index_dir != NULL) {
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen if (set->list_index_dir[0] == '/') {
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen path = set->list_index_dir;
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen break;
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen }
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen /* relative path */
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen if (!mailbox_list_set_get_root_path(set,
5c95939662dc6e4317f4eec60289bb12fd51982bTimo Sirainen MAILBOX_LIST_PATH_TYPE_INDEX, &path))
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_unreached();
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen path = t_strconcat(path, "/", set->list_index_dir, NULL);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen break;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen }
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen /* fall through - default to index directory */
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen case MAILBOX_LIST_PATH_TYPE_INDEX_CACHE:
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (set->index_cache_dir != NULL &&
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen type == MAILBOX_LIST_PATH_TYPE_INDEX_CACHE) {
e0ca8f2484847b57e20798a9f9c7040708696a90Timo Sirainen path = set->index_cache_dir;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen break;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen /* fall through */
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen case MAILBOX_LIST_PATH_TYPE_INDEX:
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen if (set->index_dir != NULL) {
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen if (set->index_dir[0] == '\0') {
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen /* in-memory indexes */
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen return 0;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen }
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen path = set->index_dir;
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen } else {
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen path = set->root_dir;
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen }
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen break;
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen case MAILBOX_LIST_PATH_TYPE_INDEX_PRIVATE:
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen path = set->index_pvt_dir;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen break;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen }
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen *path_r = path;
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen return path != NULL;
5160580b0ec3f3288a320987abdf12a990f09df5Timo Sirainen}
533bfba437e4120aa29dd45bca2aa87e30ee28a2Timo Sirainen
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainenconst char *mailbox_list_get_temp_prefix(struct mailbox_list *list)
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen{
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen return list->v.get_temp_prefix(list, FALSE);
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen}
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainenconst char *mailbox_list_get_global_temp_prefix(struct mailbox_list *list)
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen{
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen return list->v.get_temp_prefix(list, TRUE);
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen}
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainenconst char *mailbox_list_join_refpattern(struct mailbox_list *list,
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen const char *ref, const char *pattern)
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen{
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen if (list->v.join_refpattern != NULL)
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen return list->v.join_refpattern(list, ref, pattern);
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen /* the default implementation: */
9b2bbd89073e5b78f3f39a19f3f8d7c6d03bbd4cTimo Sirainen if (*ref != '\0') {
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen /* merge reference and pattern */
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen pattern = t_strconcat(ref, pattern, NULL);
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen }
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen return pattern;
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen}
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen
688d869f3449827ac9b5a0a02dbc1b27177b6d20Timo Sirainenint mailbox_has_children(struct mailbox_list *list, const char *name)
688d869f3449827ac9b5a0a02dbc1b27177b6d20Timo Sirainen{
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen struct mailbox_list_iterate_context *iter;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen const char *pattern;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen int ret;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen pattern = t_strdup_printf("%s%c%%", name,
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen mail_namespace_get_sep(list->ns));
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen iter = mailbox_list_iter_init(list, pattern,
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen ret = mailbox_list_iter_next(iter) != NULL ? 1 : 0;
7cf1c7dd3dfd989cba1ed32a8e17c1b031c4629bTimo Sirainen if (mailbox_list_iter_deinit(&iter) < 0)
7cf1c7dd3dfd989cba1ed32a8e17c1b031c4629bTimo Sirainen ret = -1;
7cf1c7dd3dfd989cba1ed32a8e17c1b031c4629bTimo Sirainen return ret;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen}
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainenint mailbox_list_mailbox(struct mailbox_list *list, const char *name,
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen enum mailbox_info_flags *flags_r)
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen{
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen const char *path, *fname, *rootdir, *dir, *inbox;
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen size_t len;
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen *flags_r = 0;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen strcasecmp(name, "INBOX") == 0) {
d3d2e50d6217e29fbc9c72d14c812c02dee291efTimo Sirainen /* special handling for INBOX, mainly because with Maildir++
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen layout it needs to check if the cur/ directory exists,
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen which the Maildir++ layout backend itself can't do.. */
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen struct mailbox *box;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen enum mailbox_existence existence;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen int ret;
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen /* kludge: with imapc backend we can get here with
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen list=Maildir++ (for indexes), but list->ns->list=imapc */
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen box = mailbox_alloc(list->ns->list, "INBOX", 0);
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen ret = mailbox_exists(box, FALSE, &existence);
9ba91492d2eb29199674b669c673332ae2b01438Timo Sirainen if (ret < 0) {
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen const char *errstr;
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen enum mail_error error;
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen /* internal error or with imapc we can get here with
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen login failures */
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen errstr = mailbox_get_last_error(box, &error);
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen mailbox_list_set_error(list, error, errstr);
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen }
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen mailbox_free(&box);
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen if (ret < 0)
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen return -1;
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen switch (existence) {
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen case MAILBOX_EXISTENCE_NONE:
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen case MAILBOX_EXISTENCE_NOSELECT:
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen *flags_r |= MAILBOX_NONEXISTENT;
ba36444cdf332c75ecdd2696e34c391eea75597cTimo Sirainen return 0;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen case MAILBOX_EXISTENCE_SELECT:
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen break;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen }
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen return 1;
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen }
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen if (list->v.get_mailbox_flags == NULL) {
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen /* can't do this optimized. do it the slow way. */
1728ff34ee03de825ad3aeed67d19f8ae140ee2eTimo Sirainen struct mailbox_list_iterate_context *iter;
b6eced3d628ad0c50a3cbc9f966da0edc20108abTimo Sirainen const struct mailbox_info *info;
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen const char *vname;
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen vname = mailbox_list_get_vname(list, name);
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen iter = mailbox_list_iter_init(list, vname, 0);
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen info = mailbox_list_iter_next(iter);
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen if (info == NULL)
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen *flags_r = MAILBOX_NONEXISTENT;
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen else
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen *flags_r = info->flags;
b6eced3d628ad0c50a3cbc9f966da0edc20108abTimo Sirainen return mailbox_list_iter_deinit(&iter);
b6eced3d628ad0c50a3cbc9f966da0edc20108abTimo Sirainen }
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen if (!list->set.iter_from_index_dir) {
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen rootdir = mailbox_list_get_root_forced(list, MAILBOX_LIST_PATH_TYPE_MAILBOX);
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen if (mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_DIR, &path) <= 0)
dca6d617a23e3f93af3b8df59acb46478179fe55Timo Sirainen i_unreached();
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen } else {
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen rootdir = mailbox_list_get_root_forced(list, MAILBOX_LIST_PATH_TYPE_INDEX);
b6eced3d628ad0c50a3cbc9f966da0edc20108abTimo Sirainen if (mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_INDEX, &path) <= 0)
dca6d617a23e3f93af3b8df59acb46478179fe55Timo Sirainen i_unreached();
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen }
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen fname = strrchr(path, '/');
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen if (fname == NULL) {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen fname = path;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen dir = "/";
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen } else {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen dir = t_strdup_until(path, fname);
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen fname++;
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen }
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen len = strlen(rootdir);
e22efc90e109ade2936eea0b062a99480310fd41Timo Sirainen if (strncmp(path, rootdir, len) == 0 && path[len] == '/') {
f7ee94201b23b4bd5764cb85cb51d5c995e475ecTimo Sirainen /* looking up a regular mailbox under mail root dir */
dca6d617a23e3f93af3b8df59acb46478179fe55Timo Sirainen } else if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
fb365addee8210c28e6856e34d690dd8d1636993Timo Sirainen strcasecmp(name, "INBOX") == 0) {
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen /* looking up INBOX that's elsewhere */
1c885b304f060e3ac4fe04195a2f53457d0ac99eTimo Sirainen } else {
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen /* looking up the root dir itself */
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen dir = path;
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen fname = "";
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen }
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen if (*fname == '\0' && *name == '\0' &&
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen (list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0) {
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen /* if INBOX is in e.g. ~/Maildir, it shouldn't be possible to
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen access it also via namespace prefix. */
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen if (mailbox_list_get_path(list, "INBOX",
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen &inbox) <= 0)
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen i_unreached();
32f4b9ab23148bec295196a8029a43ff89ce124dTimo Sirainen if (strcmp(inbox, dir) == 0) {
32f4b9ab23148bec295196a8029a43ff89ce124dTimo Sirainen *flags_r |= MAILBOX_NONEXISTENT;
32f4b9ab23148bec295196a8029a43ff89ce124dTimo Sirainen return 0;
32f4b9ab23148bec295196a8029a43ff89ce124dTimo Sirainen }
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen }
db7ca286e3ef53f73a3e8e46104a31247ebfe213Timo Sirainen return list->v.get_mailbox_flags(list, dir, fname,
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen MAILBOX_LIST_FILE_TYPE_UNKNOWN,
76b91bac787101e6b0075122ab6478dd98c8a884Timo Sirainen flags_r);
32f4b9ab23148bec295196a8029a43ff89ce124dTimo Sirainen}
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainenstatic bool mailbox_list_init_changelog(struct mailbox_list *list)
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen{
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen struct mailbox_permissions perm;
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen const char *path;
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen if (list->changelog != NULL)
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen return TRUE;
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen /* don't do this in mailbox_list_create(), because _get_path() might be
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen overridden by storage (mbox). */
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen if (!mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_INDEX, &path))
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen return FALSE;
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen path = t_strconcat(path, "/"MAILBOX_LOG_FILE_NAME, NULL);
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen list->changelog = mailbox_log_alloc(path);
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen mailbox_list_get_root_permissions(list, &perm);
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen mailbox_log_set_permissions(list->changelog, perm.file_create_mode,
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen perm.file_create_gid,
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen perm.file_create_gid_origin);
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen return TRUE;
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen}
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainenint mailbox_list_mkdir_missing_index_root(struct mailbox_list *list)
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen{
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen const char *index_dir;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (list->index_root_dir_created)
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen return 1;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen /* If index root dir hasn't been created yet, do it now.
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen Do this here even if the index directory is the same as mail root
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen directory, because it may not have been created elsewhere either. */
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (!mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_INDEX,
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen &index_dir))
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen return 0;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen if (mailbox_list_mkdir_root(list, index_dir,
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen MAILBOX_LIST_PATH_TYPE_INDEX) < 0)
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen return -1;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen list->index_root_dir_created = TRUE;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen return 1;
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen}
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainenint mailbox_list_mkdir_missing_list_index_root(struct mailbox_list *list)
49c48631cfd07017d5f93d83713fffe4f13730c4Timo Sirainen{
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen const char *index_dir;
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (list->set.list_index_dir == NULL)
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen return mailbox_list_mkdir_missing_index_root(list);
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen /* LISTINDEX points outside the index root directory */
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen if (list->list_index_root_dir_created)
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen return 1;
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen if (!mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_LIST_INDEX,
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen &index_dir))
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen return 0;
43487bb7de9e907ad61e186714956a83f5be4a15Timo Sirainen if (mailbox_list_mkdir_root(list, index_dir,
4d84348ffcbb60de566108562c95ad64629e7a53Timo Sirainen MAILBOX_LIST_PATH_TYPE_LIST_INDEX) < 0)
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen return -1;
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen list->list_index_root_dir_created = TRUE;
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen return 1;
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch}
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainenvoid mailbox_list_add_change(struct mailbox_list *list,
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen enum mailbox_log_record_type type,
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen const guid_128_t mailbox_guid)
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen{
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen struct mailbox_log_record rec;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen time_t stamp;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (!mailbox_list_init_changelog(list) ||
5a0fc34c5cc525334c5a10531713017ae321c573Timo Sirainen guid_128_is_empty(mailbox_guid))
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen return;
b13f738e8eb3f24dc2abf2c804f954b4d864ac6fTimo Sirainen
b13f738e8eb3f24dc2abf2c804f954b4d864ac6fTimo Sirainen if (mailbox_list_mkdir_missing_index_root(list) <= 0)
b13f738e8eb3f24dc2abf2c804f954b4d864ac6fTimo Sirainen return;
5a0fc34c5cc525334c5a10531713017ae321c573Timo Sirainen
5a0fc34c5cc525334c5a10531713017ae321c573Timo Sirainen stamp = list->changelog_timestamp != (time_t)-1 ?
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen list->changelog_timestamp : ioloop_time;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_zero(&rec);
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen rec.type = type;
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen memcpy(rec.mailbox_guid, mailbox_guid, sizeof(rec.mailbox_guid));
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen mailbox_log_record_set_timestamp(&rec, stamp);
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen (void)mailbox_log_append(list->changelog, &rec);
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen}
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainenint mailbox_list_set_subscribed(struct mailbox_list *list,
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen const char *name, bool set)
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen{
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen int ret;
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen
2f30b72d49fbff0c4096125c139e4bdfef45669cTimo Sirainen /* make sure we'll refresh the file on next list */
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen list->subscriptions_mtime = (time_t)-1;
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen if ((ret = list->v.set_subscribed(list, name, set)) <= 0)
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen return ret;
e9df0f285429d2b896ecdb4cd873d25e5e895620Timo Sirainen return 0;
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen}
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainenint mailbox_list_delete_dir(struct mailbox_list *list, const char *name)
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen{
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen const char *error;
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen
0b25846ba794ce19536a24d4065beaf2a0bd9464Timo Sirainen if (!mailbox_list_is_valid_name(list, name, &error) || *name == '\0') {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
0185427dd52fddec6fc76d6e99c7659620d4366eTimo Sirainen "Invalid mailbox name");
9c66dd5c3e2ba484a5c7cdb98a139bda31fdfaa4Timo Sirainen return -1;
6bc5fed79741503437c6d46d9f282b66bd029c6bTimo Sirainen }
9c66dd5c3e2ba484a5c7cdb98a139bda31fdfaa4Timo Sirainen return list->v.delete_dir(list, name);
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen}
6bc5fed79741503437c6d46d9f282b66bd029c6bTimo Sirainen
6bc5fed79741503437c6d46d9f282b66bd029c6bTimo Sirainenint mailbox_list_delete_symlink(struct mailbox_list *list, const char *name)
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen{
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen const char *error;
78bb4a443b4ca97ce27976916953e4898aa8a125Timo Sirainen
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen if (!mailbox_list_is_valid_name(list, name, &error) || *name == '\0') {
37847ec8eaec9ad55c9df10ae109efe7b37ac573Timo Sirainen mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen "Invalid mailbox name");
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen return -1;
5a0b68eeaf48cd1655057f36f19357146bf08e1dTimo Sirainen }
3fe67ec75ccae1230bb9eb9f16affc48377f6441Timo Sirainen return list->v.delete_symlink(list, name);
5a0b68eeaf48cd1655057f36f19357146bf08e1dTimo Sirainen}
5a0b68eeaf48cd1655057f36f19357146bf08e1dTimo Sirainen
ff4bb2dfb5714eeb0408d3bb862de1646351d097Timo Sirainenvoid mailbox_name_get_sha128(const char *name, guid_128_t guid_128_r)
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen unsigned char sha[SHA1_RESULTLEN];
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen sha1_get_digest(name, strlen(name), sha);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen memcpy(guid_128_r, sha, I_MIN(GUID_128_SIZE, sizeof(sha)));
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstruct mailbox_log *mailbox_list_get_changelog(struct mailbox_list *list)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return !mailbox_list_init_changelog(list) ? NULL : list->changelog;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid mailbox_list_set_changelog_timestamp(struct mailbox_list *list,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen time_t stamp)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen list->changelog_timestamp = stamp;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenenum mailbox_list_file_type
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenmailbox_list_get_file_type(const struct dirent *d ATTR_UNUSED)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum mailbox_list_file_type type;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#ifdef HAVE_DIRENT_D_TYPE
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen switch (d->d_type) {
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen case DT_UNKNOWN:
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_UNKNOWN;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen break;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen case DT_REG:
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_FILE;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen break;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen case DT_DIR:
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_DIR;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen break;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen case DT_LNK:
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_SYMLINK;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen break;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen default:
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_OTHER;
dd645357a6b851a3a9527d16e2bced731e46dcaaMartti Rannanjärvi break;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen }
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen#else
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_UNKNOWN;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen#endif
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen return type;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen}
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainenint mailbox_list_dirent_is_alias_symlink(struct mailbox_list *list,
dd645357a6b851a3a9527d16e2bced731e46dcaaMartti Rannanjärvi const char *dir_path,
dd645357a6b851a3a9527d16e2bced731e46dcaaMartti Rannanjärvi const struct dirent *d)
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen{
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen struct stat st;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen int ret;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen if (mailbox_list_get_file_type(d) == MAILBOX_LIST_FILE_TYPE_SYMLINK)
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen return 1;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen T_BEGIN {
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen const char *path, *linkpath, *error;
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen
77b5fd56e5a06d624f3ab92198272287333114f4Timo Sirainen path = t_strconcat(dir_path, "/", d->d_name, NULL);
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen if (lstat(path, &st) < 0) {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen mailbox_list_set_critical(list,
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen "lstat(%s) failed: %m", path);
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen ret = -1;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen } else if (!S_ISLNK(st.st_mode)) {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen ret = 0;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen } else if (t_readlink(path, &linkpath, &error) < 0) {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen i_error("t_readlink(%s) failed: %s", path, error);
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen ret = -1;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen } else {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen /* it's an alias only if it points to the same
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen directory */
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen ret = strchr(linkpath, '/') == NULL ? 1 : 0;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen }
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen } T_END;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen return ret;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen}
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainenstatic bool
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainenmailbox_list_try_get_home_path(struct mailbox_list *list, const char **name)
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen{
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen if ((*name)[1] == '/') {
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen /* ~/dir - use the configured home directory */
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen if (mail_user_try_home_expand(list->ns->user, name) < 0)
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen return FALSE;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen } else {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen /* ~otheruser/dir - assume we're using system users */
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen if (home_try_expand(name) < 0)
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen return FALSE;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen }
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen return TRUE;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen}
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainenbool mailbox_list_try_get_absolute_path(struct mailbox_list *list,
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen const char **name)
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen{
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen const char *root_dir, *path, *mailbox_name;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen size_t len;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen if (!list->mail_set->mail_full_filesystem_access)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen return FALSE;
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (**name == '~') {
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen /* try to expand home directory */
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen if (!mailbox_list_try_get_home_path(list, name)) {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen /* fallback to using actual "~name" mailbox */
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen return FALSE;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen }
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen } else {
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen if (**name != '/')
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen return FALSE;
ff1777f61db8b45c72d8bcb843f9106eb0227ab6Timo Sirainen }
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen /* okay, we have an absolute path now. but check first if it points to
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen same directory as one of our regular mailboxes. */
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen root_dir = mailbox_list_get_root_forced(list, MAILBOX_LIST_PATH_TYPE_MAILBOX);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen len = strlen(root_dir);
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen if (strncmp(root_dir, *name, len) == 0 && (*name)[len] == '/') {
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen mailbox_name = *name + len + 1;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (mailbox_list_get_path(list, mailbox_name,
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX,
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen &path) <= 0)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return FALSE;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen if (strcmp(path, *name) == 0) {
3e0b3ae576483a96b88802b27e93f3caeba09ddcTimo Sirainen /* yeah, we can replace the full path with mailbox
3e0b3ae576483a96b88802b27e93f3caeba09ddcTimo Sirainen name. this way we can use indexes. */
3e0b3ae576483a96b88802b27e93f3caeba09ddcTimo Sirainen *name = mailbox_name;
3e0b3ae576483a96b88802b27e93f3caeba09ddcTimo Sirainen return FALSE;
3e0b3ae576483a96b88802b27e93f3caeba09ddcTimo Sirainen }
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi }
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi return TRUE;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi}
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärviconst char *mailbox_list_get_last_error(struct mailbox_list *list,
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi enum mail_error *error_r)
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi{
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi if (error_r != NULL)
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi *error_r = list->error;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi return list->error_string != NULL ? list->error_string :
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi "Unknown internal list error";
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainenenum mail_error mailbox_list_get_last_mail_error(struct mailbox_list *list)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi return list->error;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi}
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenconst char *mailbox_list_get_last_internal_error(struct mailbox_list *list,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum mail_error *error_r)
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen{
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen if (error_r != NULL)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen *error_r = list->error;
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen if (list->last_error_is_internal) {
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen i_assert(list->last_internal_error != NULL);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return list->last_internal_error;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi }
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen return mailbox_list_get_last_error(list, error_r);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainenvoid mailbox_list_clear_error(struct mailbox_list *list)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
b1f37113a5760bee842c5a7678bb5fa6f5bd8b60Timo Sirainen i_free_and_null(list->error_string);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
b1f37113a5760bee842c5a7678bb5fa6f5bd8b60Timo Sirainen i_free(list->last_internal_error);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen list->last_error_is_internal = FALSE;
b1f37113a5760bee842c5a7678bb5fa6f5bd8b60Timo Sirainen list->error = MAIL_ERROR_NONE;
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen}
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenvoid mailbox_list_set_error(struct mailbox_list *list,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen enum mail_error error, const char *string)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_free(list->error_string);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen list->error_string = i_strdup(string);
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen list->last_error_is_internal = FALSE;
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi list->error = error;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen}
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvi
8ca217bf3aa23c7922d0d4aa44fcd2320416d61cMartti Rannanjärvivoid mailbox_list_set_internal_error(struct mailbox_list *list)
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen{
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen const char *str;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen str = t_strflocaltime(MAIL_ERRSTR_CRITICAL_MSG_STAMP, ioloop_time);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen i_free(list->error_string);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen list->error_string = i_strdup(str);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen list->error = MAIL_ERROR_TEMP;
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen}
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainenvoid mailbox_list_set_critical(struct mailbox_list *list, const char *fmt, ...)
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen{
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen va_list va;
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen i_free(list->last_internal_error);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen va_start(va, fmt);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen list->last_internal_error = i_strdup_vprintf(fmt, va);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen va_end(va);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen list->last_error_is_internal = TRUE;
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen i_error("%s", list->last_internal_error);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen /* critical errors may contain sensitive data, so let user
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen see only "Internal error" with a timestamp to make it
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen easier to look from log files the actual error message. */
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen mailbox_list_set_internal_error(list);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen}
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainenbool mailbox_list_set_error_from_errno(struct mailbox_list *list)
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen{
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen const char *error_string;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen enum mail_error error;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen if (!mail_error_from_errno(&error, &error_string))
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen return FALSE;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen mailbox_list_set_error(list, error, error_string);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen return TRUE;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen}
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainenvoid mailbox_list_last_error_push(struct mailbox_list *list)
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen{
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen struct mail_storage_error *err;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen if (!array_is_created(&list->error_stack))
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen i_array_init(&list->error_stack, 2);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen err = array_append_space(&list->error_stack);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen err->error_string = i_strdup(list->error_string);
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen err->error = list->error;
4654cf737f538f5de032b8c9908913f121917366Timo Sirainen err->last_error_is_internal = list->last_error_is_internal;
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen if (err->last_error_is_internal)
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen err->last_internal_error = i_strdup(list->last_internal_error);
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen}
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainenvoid mailbox_list_last_error_pop(struct mailbox_list *list)
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen{
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen unsigned int count = array_count(&list->error_stack);
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen const struct mail_storage_error *err =
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen array_idx(&list->error_stack, count-1);
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_free(list->error_string);
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen i_free(list->last_internal_error);
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen list->error_string = err->error_string;
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen list->error = err->error;
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen list->last_error_is_internal = err->last_error_is_internal;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen list->last_internal_error = err->last_internal_error;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen array_delete(&list->error_stack, count-1, 1);
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen}
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainenint mailbox_list_init_fs(struct mailbox_list *list, const char *driver,
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen const char *args, const char *root_dir,
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen struct fs **fs_r, const char **error_r)
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen{
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen struct fs_settings fs_set;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen struct ssl_iostream_settings ssl_set;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen struct mailbox_list_fs_context *ctx;
e0f57c552d2f436458163f722a7d0cc9a95d0709Timo Sirainen struct fs *parent_fs;
e0f57c552d2f436458163f722a7d0cc9a95d0709Timo Sirainen
e0f57c552d2f436458163f722a7d0cc9a95d0709Timo Sirainen i_zero(&ssl_set);
e0f57c552d2f436458163f722a7d0cc9a95d0709Timo Sirainen i_zero(&fs_set);
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen mail_user_init_fs_settings(list->ns->user, &fs_set, &ssl_set);
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen fs_set.root_path = root_dir;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen fs_set.temp_file_prefix = mailbox_list_get_global_temp_prefix(list);
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen if (fs_init(driver, args, &fs_set, fs_r, error_r) < 0)
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen return -1;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen /* add mailbox_list context to the parent fs, which allows
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen mailbox_list_fs_get_list() to work */
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen for (parent_fs = *fs_r; parent_fs->parent != NULL;
697ff56bf3cdc9e7989ea2a70accf866b14b64d1Timo Sirainen parent_fs = parent_fs->parent) ;
a41f6dd2a2434206d2a445ac73c79861d3496a2aTimo Sirainen
9393445a6dabd17ce62ebfc12fd73545b0065824Timo Sirainen ctx = p_new(list->pool, struct mailbox_list_fs_context, 1);
ctx->list = list;
MODULE_CONTEXT_SET(parent_fs, mailbox_list_fs_module, ctx);
/* a bit kludgy notification to the fs that we're now finished setting
up the module context. */
(void)fs_get_properties(*fs_r);
return 0;
}
struct mailbox_list *mailbox_list_fs_get_list(struct fs *fs)
{
struct mailbox_list_fs_context *ctx;
while (fs->parent != NULL)
fs = fs->parent;
ctx = MAILBOX_LIST_FS_CONTEXT(fs);
return ctx == NULL ? NULL : ctx->list;
}
int mailbox_list_lock(struct mailbox_list *list)
{
struct mailbox_permissions perm;
struct file_create_settings set;
const char *lock_dir, *lock_fname, *lock_path, *error;
if (list->lock_refcount > 0) {
list->lock_refcount++;
return 0;
}
mailbox_list_get_root_permissions(list, &perm);
i_zero(&set);
set.lock_timeout_secs = list->mail_set->mail_max_lock_timeout == 0 ?
MAILBOX_LIST_LOCK_SECS :
I_MIN(MAILBOX_LIST_LOCK_SECS, list->mail_set->mail_max_lock_timeout);
set.lock_method = list->mail_set->parsed_lock_method;
set.mode = perm.file_create_mode;
set.gid = perm.file_create_gid;
set.gid_origin = perm.file_create_gid_origin;
lock_fname = MAILBOX_LIST_LOCK_FNAME;
if (list->set.volatile_dir != NULL) {
/* Use VOLATILEDIR. It's shared with all mailbox_lists, so use
hash of the namespace prefix as a way to make this lock name
unique across the namespaces. */
unsigned char ns_prefix_hash[SHA1_RESULTLEN];
sha1_get_digest(list->ns->prefix, list->ns->prefix_len,
ns_prefix_hash);
lock_fname = t_strconcat(MAILBOX_LIST_LOCK_FNAME,
binary_to_hex(ns_prefix_hash, sizeof(ns_prefix_hash)), NULL);
lock_dir = list->set.volatile_dir;
set.mkdir_mode = 0700;
} else if (mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_INDEX,
&lock_dir)) {
/* use index root directory */
if (mailbox_list_mkdir_missing_index_root(list) < 0)
return -1;
} else if (mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_DIR,
&lock_dir)) {
/* use mailbox root directory */
if (mailbox_list_mkdir_root(list, lock_dir,
MAILBOX_LIST_PATH_TYPE_DIR) < 0)
return -1;
} else {
/* No filesystem used by mailbox list (e.g. imapc).
Just assume it's locked */
list->lock_refcount = 1;
return 0;
}
lock_path = t_strdup_printf("%s/%s", lock_dir, lock_fname);
if (mail_storage_lock_create(lock_path, &set, list->mail_set,
&list->lock, &error) <= 0) {
mailbox_list_set_critical(list,
"Couldn't create mailbox list lock %s: %s",
lock_path, error);
return -1;
}
list->lock_refcount = 1;
return 0;
}
void mailbox_list_unlock(struct mailbox_list *list)
{
i_assert(list->lock_refcount > 0);
if (--list->lock_refcount > 0)
return;
file_lock_free(&list->lock);
}