mailbox-list.c revision 48ce03445eaea184426156440116ca1da978bb54
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2006-2012 Dovecot authors, see the included COPYING file */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "array.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ioloop.h"
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#include "mkdir-parents.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "str.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include "sha1.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "hash.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "home-expand.h"
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen#include "time-util.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "unichar.h"
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen#include "settings-parser.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-utf7.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mailbox-log.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mailbox-tree.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "mail-storage.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include "mail-storage-hooks.h"
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen#include "mailbox-list-private.h"
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen#include <time.h>
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen#include <ctype.h>
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen#include <unistd.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <dirent.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/stat.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* 20 * (200+1) < 4096 which is the standard PATH_MAX. Having these settings
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen prevents malicious user from creating eg. "a/a/a/.../a" mailbox name and
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen then start renaming them to larger names from end to beginning, which
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen eventually would start causing the failures when trying to use too
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen long mailbox names. */
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen#define MAILBOX_MAX_HIERARCHY_LEVELS 20
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen#define MAILBOX_MAX_HIERARCHY_NAME_LENGTH 200
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstruct mailbox_list_module_register mailbox_list_module_register = { 0 };
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenstatic ARRAY(const struct mailbox_list *) mailbox_list_drivers;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mailbox_lists_init(void)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_array_init(&mailbox_list_drivers, 4);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenvoid mailbox_lists_deinit(void)
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen array_free(&mailbox_list_drivers);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic bool mailbox_list_driver_find(const char *name, unsigned int *idx_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mailbox_list *const *drivers;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int i, count;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen drivers = array_get(&mailbox_list_drivers, &count);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen for (i = 0; i < count; i++) {
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (strcasecmp(drivers[i]->name, name) == 0) {
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen *idx_r = i;
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen return TRUE;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return FALSE;
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen}
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenvoid mailbox_list_register(const struct mailbox_list *list)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen unsigned int idx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_list_driver_find(list->name, &idx)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_fatal("mailbox_list_register(%s): duplicate driver",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen array_append(&mailbox_list_drivers, &list, 1);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen}
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenvoid mailbox_list_unregister(const struct mailbox_list *list)
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen{
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen unsigned int idx;
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (!mailbox_list_driver_find(list->name, &idx)) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_fatal("mailbox_list_unregister(%s): unknown driver",
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen list->name);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen array_delete(&mailbox_list_drivers, idx, 1);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen}
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenconst struct mailbox_list *
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainenmailbox_list_find_class(const char *driver)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen const struct mailbox_list *const *class_p;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen unsigned int idx;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (!mailbox_list_driver_find(driver, &idx))
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen return NULL;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen class_p = array_idx(&mailbox_list_drivers, idx);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen return *class_p;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen}
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenint mailbox_list_create(const char *driver, struct mail_namespace *ns,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const struct mailbox_list_settings *set,
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen enum mailbox_list_flags flags,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct mailbox_list **list_r, const char **error_r)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen{
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen const struct mailbox_list *const *class_p;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct mailbox_list *list;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int idx;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen i_assert(ns->list == NULL ||
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen (flags & MAILBOX_LIST_FLAG_SECONDARY) != 0);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen i_assert(set->subscription_fname == NULL ||
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen *set->subscription_fname != '\0');
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (!mailbox_list_driver_find(driver, &idx)) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen *error_r = "Unknown driver name";
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen return -1;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen }
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen class_p = array_idx(&mailbox_list_drivers, idx);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (((*class_p)->props & MAILBOX_LIST_PROP_NO_MAILDIR_NAME) != 0 &&
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen *set->maildir_name != '\0') {
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen *error_r = "maildir_name not supported by this driver";
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen return -1;
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen }
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (((*class_p)->props & MAILBOX_LIST_PROP_NO_ALT_DIR) != 0 &&
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen set->alt_dir != NULL) {
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen *error_r = "alt_dir not supported by this driver";
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen return -1;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen i_assert(set->root_dir == NULL || *set->root_dir != '\0' ||
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen ((*class_p)->props & MAILBOX_LIST_PROP_NO_ROOT) != 0);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen list = (*class_p)->v.alloc();
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen array_create(&list->module_contexts, list->pool, sizeof(void *), 5);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen list->ns = ns;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->mail_set = ns->mail_set;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen list->flags = flags;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen list->file_create_mode = (mode_t)-1;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen list->dir_create_mode = (mode_t)-1;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen list->file_create_gid = (gid_t)-1;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->changelog_timestamp = (time_t)-1;
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* copy settings */
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen if (set->root_dir != NULL) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.root_dir = p_strdup(list->pool, set->root_dir);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen list->set.index_dir = set->index_dir == NULL ||
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen strcmp(set->index_dir, set->root_dir) == 0 ? NULL :
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->index_dir);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen list->set.index_pvt_dir = set->index_pvt_dir == NULL ||
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen strcmp(set->index_pvt_dir, set->root_dir) == 0 ? NULL :
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->index_pvt_dir);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.control_dir = set->control_dir == NULL ||
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen strcmp(set->control_dir, set->root_dir) == 0 ? NULL :
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen p_strdup(list->pool, set->control_dir);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen list->set.inbox_path = p_strdup(list->pool, set->inbox_path);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen list->set.subscription_fname =
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->subscription_fname);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.maildir_name =
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->maildir_name);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.mailbox_dir_name =
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->mailbox_dir_name);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.alt_dir = p_strdup(list->pool, set->alt_dir);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (*set->mailbox_dir_name == '\0')
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.mailbox_dir_name = "";
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen else if (set->mailbox_dir_name[strlen(set->mailbox_dir_name)-1] == '/') {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.mailbox_dir_name =
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->mailbox_dir_name);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen } else {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.mailbox_dir_name =
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strconcat(list->pool, set->mailbox_dir_name, "/", NULL);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen list->set.utf8 = set->utf8;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen if (ns->mail_set->mail_debug) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen i_debug("%s: root=%s, index=%s, indexpvt=%s, control=%s, inbox=%s, alt=%s",
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen list->name,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen list->set.root_dir == NULL ? "" : list->set.root_dir,
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen list->set.index_dir == NULL ? "" : list->set.index_dir,
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen list->set.index_pvt_dir == NULL ? "" : list->set.index_pvt_dir,
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen list->set.control_dir == NULL ?
367c05967091a2cbfce59b7f274f55b1a0f9e8c9Timo Sirainen "" : list->set.control_dir,
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen list->set.inbox_path == NULL ?
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen "" : list->set.inbox_path,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen list->set.alt_dir == NULL ? "" : list->set.alt_dir);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if ((flags & MAILBOX_LIST_FLAG_SECONDARY) == 0)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mail_namespace_finish_list_init(ns, list);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen *list_r = list;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen hook_mailbox_list_created(list);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int fix_path(struct mail_user *user, const char *path,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen const char **path_r, const char **error_r)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen{
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen size_t len = strlen(path);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (len > 1 && path[len-1] == '/')
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen path = t_strndup(path, len-1);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (path[0] == '~' && path[1] != '/' && path[1] != '\0') {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen /* ~otheruser/dir */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (home_try_expand(&path) < 0) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen *error_r = t_strconcat(
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen "No home directory for system user. "
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen "Can't expand ", t_strcut(path, '/'),
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen " for ", NULL);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return -1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen } else {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (mail_user_try_home_expand(user, &path) < 0) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen *error_r = "Home directory not set for user. "
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen "Can't expand ~/ for ";
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return -1;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen *path_r = path;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic const char *split_next_arg(const char *const **_args)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen const char *const *args = *_args;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const char *str = args[0];
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen args++;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen while (*args != NULL && **args == '\0') {
053843989f13d9013b265fb401a4bde7e0e6568eTimo Sirainen args++;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (*args == NULL) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* string ends with ":", just ignore it. */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen break;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen str = t_strconcat(str, ":", *args, NULL);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen args++;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *_args = args;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return str;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen}
ab286a8b58306eb8d22fc18342b6c199fd428e1eTimo Sirainen
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenint mailbox_list_settings_parse(struct mail_user *user, const char *data,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen struct mailbox_list_settings *set_r,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const char **error_r)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen{
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const char *const *tmp, *key, *value, **dest, *str, *error;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *error_r = NULL;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen memset(set_r, 0, sizeof(*set_r));
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen set_r->maildir_name = "";
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen set_r->mailbox_dir_name = "";
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (*data == '\0')
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return 0;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* <root dir> */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen tmp = t_strsplit(data, ":");
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen str = split_next_arg(&tmp);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (fix_path(user, str, &set_r->root_dir, &error) < 0) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen *error_r = t_strconcat(error, "mail root dir in: ", data, NULL);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return -1;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (strncmp(set_r->root_dir, "INBOX=", 6) == 0) {
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* probably mbox user trying to avoid root_dir */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *error_r = t_strconcat("Mail root directory not given: ",
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen data, NULL);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen while (*tmp != NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen str = split_next_arg(&tmp);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (strcmp(str, "UTF-8") == 0) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen set_r->utf8 = TRUE;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen continue;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen value = strchr(str, '=');
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (value == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen key = str;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen value = "";
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen } else {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen key = t_strdup_until(str, value);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen value++;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (strcmp(key, "INBOX") == 0)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen dest = &set_r->inbox_path;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen else if (strcmp(key, "INDEX") == 0)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen dest = &set_r->index_dir;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen else if (strcmp(key, "INDEXPVT") == 0)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen dest = &set_r->index_pvt_dir;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen else if (strcmp(key, "CONTROL") == 0)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen dest = &set_r->control_dir;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen else if (strcmp(key, "ALT") == 0)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen dest = &set_r->alt_dir;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen else if (strcmp(key, "LAYOUT") == 0)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen dest = &set_r->layout;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen else if (strcmp(key, "SUBSCRIPTIONS") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen dest = &set_r->subscription_fname;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen else if (strcmp(key, "DIRNAME") == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen dest = &set_r->maildir_name;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen else if (strcmp(key, "MAILBOXDIR") == 0)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen dest = &set_r->mailbox_dir_name;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen else {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *error_r = t_strdup_printf("Unknown setting: %s", key);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return -1;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (fix_path(user, value, dest, &error) < 0) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen *error_r = t_strconcat(error, key, " in: ", data, NULL);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen return -1;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen }
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (set_r->index_dir != NULL && strcmp(set_r->index_dir, "MEMORY") == 0)
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen set_r->index_dir = "";
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen return 0;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen}
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainenconst char *mailbox_list_get_unexpanded_path(struct mailbox_list *list,
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen enum mailbox_list_path_type type)
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen{
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen const struct mail_storage_settings *mail_set;
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen const char *location = list->ns->unexpanded_set->location;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen struct mail_user *user = list->ns->user;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen struct mailbox_list_settings set;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen const char *p, *error;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen if (*location == SETTING_STRVAR_EXPANDED[0]) {
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen /* set using -o or userdb lookup. */
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen return "";
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen }
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_assert(*location == SETTING_STRVAR_UNEXPANDED[0]);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen location++;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (*location == '\0') {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_set = mail_user_set_get_driver_settings(user->set_info,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen user->unexpanded_set, MAIL_STORAGE_SET_DRIVER_NAME);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_assert(mail_set != NULL);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen location = mail_set->mail_location;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (*location == SETTING_STRVAR_EXPANDED[0])
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return "";
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen i_assert(*location == SETTING_STRVAR_UNEXPANDED[0]);
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen location++;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* type:settings */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen p = strchr(location, ':');
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (p == NULL)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return "";
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (mailbox_list_settings_parse(user, p + 1, &set, &error) < 0)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return "";
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return mailbox_list_set_get_root_path(&set, type);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic bool need_escape_dirstart(const char *vname, const char *maildir_name)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int len;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (vname[0] == '.') {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (vname[1] == '\0' || vname[1] == '/')
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return TRUE; /* "." */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (vname[1] == '.' && (vname[2] == '\0' || vname[2] == '/'))
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return TRUE; /* ".." */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (*maildir_name != '\0') {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen len = strlen(maildir_name);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (strncmp(maildir_name, vname, len) == 0 &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (vname[len] == '\0' || vname[len] == '/'))
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return TRUE; /* e.g. dbox-Mails */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return FALSE;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstatic const char *
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenmailbox_list_escape_name(struct mailbox_list *list, const char *vname)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen char ns_sep = mail_namespace_get_sep(list->ns);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen char list_sep = mailbox_list_get_hierarchy_sep(list);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen string_t *escaped_name = t_str_new(64);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen char dirstart = TRUE;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* no escaping of namespace prefix */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (strncmp(list->ns->prefix, vname, list->ns->prefix_len) == 0) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append_n(escaped_name, vname, list->ns->prefix_len);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen vname += list->ns->prefix_len;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* escape the mailbox name */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (*vname == '~') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen str_printfa(escaped_name, "%c%02x",
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen list->set.escape_char, *vname);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen vname++;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen dirstart = FALSE;
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen for (; *vname != '\0'; vname++) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (*vname == ns_sep)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen str_append_c(escaped_name, *vname);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen else if (*vname == list_sep ||
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen *vname == list->set.escape_char ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen *vname == '/' ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (dirstart &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen need_escape_dirstart(vname, list->set.maildir_name))) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen str_printfa(escaped_name, "%c%02x",
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen list->set.escape_char, *vname);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen } else {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append_c(escaped_name, *vname);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen dirstart = *vname == '/';
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return str_c(escaped_name);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenconst char *mailbox_list_default_get_storage_name(struct mailbox_list *list,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const char *vname)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct mail_namespace *ns = list->ns;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int prefix_len = strlen(ns->prefix);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen const char *storage_name = vname;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen string_t *str;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen char list_sep, ns_sep, *ret, *p;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (strcasecmp(storage_name, "INBOX") == 0 &&
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen storage_name = "INBOX";
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen else if (list->set.escape_char != '\0')
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen storage_name = mailbox_list_escape_name(list, vname);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (prefix_len > 0 && (strcmp(storage_name, "INBOX") != 0 ||
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_USER) == 0)) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* skip namespace prefix, except if this is INBOX */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (strncmp(ns->prefix, storage_name, prefix_len) == 0)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen storage_name += prefix_len;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen else if (strncmp(ns->prefix, storage_name, prefix_len-1) == 0 &&
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen ns->prefix[prefix_len-1] == mail_namespace_get_sep(ns)) {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* trying to access the namespace prefix itself */
871c7b8969e8627dc4c8b3e56fd126f948e6bce6Timo Sirainen storage_name = "";
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen } else {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* we're converting a nonexistent mailbox name,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen such as a LIST pattern. */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!list->set.utf8) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* UTF-8 -> mUTF-7 conversion */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen str = t_str_new(strlen(storage_name)*2);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (imap_utf8_to_utf7(storage_name, str) < 0)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_panic("Mailbox name not UTF-8: %s", vname);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen storage_name = str_c(str);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen list_sep = mailbox_list_get_hierarchy_sep(list);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ns_sep = mail_namespace_get_sep(ns);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (*storage_name == '\0' && ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (ns->flags & NAMESPACE_FLAG_INBOX_ANY) != 0 &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen !list->mail_set->mail_shared_explicit_inbox) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* opening shared/$user. it's the same as INBOX. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen storage_name = "INBOX";
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (list_sep == ns_sep)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return storage_name;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) == 0) {
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen /* shared namespace root. the backend storage's hierarchy
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen separator isn't known yet, so do nothing. */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return storage_name;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ret = p_strdup(unsafe_data_stack_pool, storage_name);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (p = ret; *p != '\0'; p++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (*p == ns_sep)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen *p = list_sep;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return ret;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenconst char *mailbox_list_get_storage_name(struct mailbox_list *list,
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen const char *vname)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen return list->v.get_storage_name(list, vname);
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstatic const char *
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenmailbox_list_unescape_name(struct mailbox_list *list, const char *src)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen char ns_sep = mail_namespace_get_sep(list->ns);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen char list_sep = mailbox_list_get_hierarchy_sep(list);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen string_t *dest = t_str_new(strlen(src));
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen unsigned int num;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (strncmp(src, list->ns->prefix, list->ns->prefix_len) == 0) {
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen str_append_n(dest, src, list->ns->prefix_len);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen src += list->ns->prefix_len;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (; *src != '\0'; src++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (*src == list->set.escape_char &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_isxdigit(src[1]) && i_isxdigit(src[2])) {
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen if (src[1] >= '0' && src[1] <= '9')
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen num = src[1] - '0';
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen num = i_toupper(src[1]) - 'A' + 10;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen num *= 16;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (src[2] >= '0' && src[2] <= '9')
8830fab191cab8440281eb641dfdd93974b2933bTimo Sirainen num += src[2] - '0';
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen else
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen num += i_toupper(src[2]) - 'A' + 10;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append_c(dest, num);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen src += 2;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (*src == list_sep)
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen str_append_c(dest, ns_sep);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen else
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append_c(dest, *src);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return str_c(dest);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen}
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainenconst char *mailbox_list_default_get_vname(struct mailbox_list *list,
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen const char *storage_name)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen{
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen unsigned int i, prefix_len, name_len;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen const char *vname = storage_name;
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen char list_sep, ns_sep, *ret;
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen strcmp(vname, "INBOX") == 0 &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->ns->user == list->ns->owner) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* user's INBOX - use as-is. NOTE: don't do case-insensitive
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen comparison, otherwise we can't differentiate between INBOX
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen and <ns prefix>/inBox. */
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen return vname;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (strcmp(vname, "INBOX") == 0 &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (list->ns->flags & NAMESPACE_FLAG_INBOX_ANY) != 0 &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen !list->mail_set->mail_shared_explicit_inbox) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* convert to shared/$user, we don't really care about the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen INBOX suffix here. */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen vname = "";
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (*vname == '\0') {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* return namespace prefix without the separator */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (list->ns->prefix_len == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return list->ns->prefix;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return t_strndup(list->ns->prefix,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->ns->prefix_len - 1);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen } else if (!list->set.utf8) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* mUTF-7 -> UTF-8 conversion */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen string_t *str = t_str_new(strlen(vname));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (imap_utf7_to_utf8(vname, str) == 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen vname = str_c(str);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen prefix_len = strlen(list->ns->prefix);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen list_sep = mailbox_list_get_hierarchy_sep(list);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen ns_sep = mail_namespace_get_sep(list->ns);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (list_sep != ns_sep || prefix_len > 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* @UNSAFE */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen name_len = strlen(vname);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = t_malloc(prefix_len + name_len + 1);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen memcpy(ret, list->ns->prefix, prefix_len);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen for (i = 0; i < name_len; i++) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret[i + prefix_len] =
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen vname[i] == list_sep ? ns_sep : vname[i];
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen }
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen ret[i + prefix_len] = '\0';
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen vname = ret;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (list->set.escape_char != '\0')
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen vname = mailbox_list_unescape_name(list, vname);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return vname;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen}
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenconst char *mailbox_list_get_vname(struct mailbox_list *list, const char *name)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return list->v.get_vname(list, name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mailbox_list_destroy(struct mailbox_list **_list)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen{
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen struct mailbox_list *list = *_list;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen *_list = NULL;
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen i_free_and_null(list->error_string);
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen if (hash_table_is_created(list->guid_cache)) {
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen hash_table_destroy(&list->guid_cache);
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen pool_unref(&list->guid_cache_pool);
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen }
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen if (list->subscriptions != NULL)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen mailbox_tree_deinit(&list->subscriptions);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (list->changelog != NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mailbox_log_free(&list->changelog);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen list->v.deinit(list);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenconst char *mailbox_list_get_driver_name(const struct mailbox_list *list)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen{
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen return list->name;
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen}
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenenum mailbox_list_flags mailbox_list_get_flags(const struct mailbox_list *list)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainen{
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen return list->flags;
5a4ab3d6e108a899c8b51bebd0094a37b738d5a1Timo Sirainen}
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstruct mail_namespace *
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenmailbox_list_get_namespace(const struct mailbox_list *list)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return list->ns;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic mode_t get_dir_mode(mode_t mode)
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen{
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* add the execute bit if either read or write bit is set */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen if ((mode & 0600) != 0) mode |= 0100;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen if ((mode & 0060) != 0) mode |= 0010;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen if ((mode & 0006) != 0) mode |= 0001;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen return mode;
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen}
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenstruct mail_user *
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenmailbox_list_get_user(const struct mailbox_list *list)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return list->ns->user;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenint mailbox_list_get_storage(struct mailbox_list **list, const char *vname,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen struct mail_storage **storage_r)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if ((*list)->v.get_storage != NULL)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return (*list)->v.get_storage(list, vname, storage_r);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen else {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen *storage_r = (*list)->ns->storage;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return 0;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
cd65dfcedc603fabbf644be793efb09096f6c1b5Timo Sirainen}
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenvoid mailbox_list_get_closest_storage(struct mailbox_list *list,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen struct mail_storage **storage)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen{
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen *storage = list->ns->storage;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainenchar mailbox_list_get_hierarchy_sep(struct mailbox_list *list)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen{
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return list->v.get_hierarchy_sep(list);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen}
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic void ATTR_NULL(2)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenmailbox_list_get_permissions_internal(struct mailbox_list *list,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *name,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mailbox_permissions *permissions_r)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *path, *parent_name, *p;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct stat st;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen memset(permissions_r, 0, sizeof(*permissions_r));
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* use safe defaults */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->file_uid = (uid_t)-1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->file_gid = (gid_t)-1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->file_create_mode = 0600;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->dir_create_mode = 0700;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->file_create_gid = (gid_t)-1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->file_create_gid_origin = "defaults";
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen path = name == NULL ?
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_DIR) :
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_DIR);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (path == NULL) {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* no filesystem support in storage */
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen } else if (stat(path, &st) < 0) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (!ENOTFOUND(errno)) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mailbox_list_set_critical(list, "stat(%s) failed: %m",
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen path);
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen } else if (list->mail_set->mail_debug) {
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen i_debug("Namespace %s: %s doesn't exist yet, "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "using default permissions",
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen list->ns->prefix, path);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
05751847e5e493fecfd0634d35d21722aad44a0bTimo Sirainen if (name != NULL) {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* return parent mailbox */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen p = strrchr(name, mailbox_list_get_hierarchy_sep(list));
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen if (p == NULL) {
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* return root defaults */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen parent_name = NULL;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen } else {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen parent_name = t_strdup_until(name, p);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mailbox_list_get_permissions(list, parent_name,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen permissions_r);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* assume current defaults for mailboxes that don't exist or
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen can't be looked up for some other reason */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen permissions_r->file_uid = geteuid();
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen permissions_r->file_gid = getegid();
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen } else {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen permissions_r->file_uid = st.st_uid;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen permissions_r->file_gid = st.st_gid;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen permissions_r->file_create_mode = (st.st_mode & 0666) | 0600;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen permissions_r->dir_create_mode = (st.st_mode & 0777) | 0700;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen permissions_r->file_create_gid_origin = path;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (!S_ISDIR(st.st_mode)) {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* we're getting permissions from a file.
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen apply +x modes as necessary. */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen permissions_r->dir_create_mode =
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen get_dir_mode(permissions_r->dir_create_mode);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen if (S_ISDIR(st.st_mode) && (st.st_mode & S_ISGID) != 0) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* directory's GID is used automatically for new
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen files */
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen permissions_r->file_create_gid = (gid_t)-1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen } else if ((st.st_mode & 0070) >> 3 == (st.st_mode & 0007)) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* group has same permissions as world, so don't bother
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen changing it */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen } else if (getegid() == st.st_gid) {
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen /* using our own gid, no need to change it */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen permissions_r->file_create_gid = (gid_t)-1;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen } else {
28dd1eeb9fdb93051bb5899c80cf300d69ba9f9eTimo Sirainen permissions_r->file_create_gid = st.st_gid;
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (name == NULL) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen list->file_create_mode = permissions_r->file_create_mode;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen list->dir_create_mode = permissions_r->dir_create_mode;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen list->file_create_gid = permissions_r->file_create_gid;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen list->file_create_gid_origin =
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen p_strdup(list->pool,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen permissions_r->file_create_gid_origin);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (list->mail_set->mail_debug && name == NULL) {
b47c57ee07ccfe76cc23b23bc720e17fe9a46d5cTimo Sirainen i_debug("Namespace %s: Using permissions from %s: "
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen "mode=0%o gid=%ld", list->ns->prefix,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen path != NULL ? path : "",
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen (int)list->dir_create_mode,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen list->file_create_gid == (gid_t)-1 ? -1L :
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen (long)list->file_create_gid);
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenvoid mailbox_list_get_permissions(struct mailbox_list *list, const char *name,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mailbox_permissions *permissions_r)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mailbox_list_get_permissions_internal(list, name, permissions_r);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainenvoid mailbox_list_get_root_permissions(struct mailbox_list *list,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen mode_t *file_mode_r, mode_t *dir_mode_r,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen gid_t *gid_r, const char **gid_origin_r)
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct mailbox_permissions perm;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (list->file_create_mode != (mode_t)-1) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *file_mode_r = list->file_create_mode;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *dir_mode_r = list->dir_create_mode;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *gid_r = list->file_create_gid;
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen *gid_origin_r = list->file_create_gid_origin;
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen } else {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mailbox_list_get_permissions_internal(list, NULL, &perm);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen *file_mode_r = perm.file_create_mode;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *dir_mode_r = perm.dir_create_mode;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *gid_r = perm.file_create_gid;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *gid_origin_r = perm.file_create_gid_origin;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic int
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenmailbox_list_stat_parent(const char *path, const char **root_dir_r,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen struct stat *st_r, const char **error_r)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *p;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen while (stat(path, st_r) < 0) {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (errno != ENOENT || strcmp(path, "/") == 0) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *error_r = t_strdup_printf("stat(%s) failed: %m", path);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return -1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen p = strrchr(path, '/');
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (p == NULL)
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen path = "/";
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen else
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen path = t_strdup_until(path, p);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *root_dir_r = path;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen return 0;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic const char *
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenget_expanded_path(const char *unexpanded_start, const char *unexpanded_stop,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *expanded_full)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen unsigned int i, slash_count = 0, slash2_count = 0;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* get the expanded path up to the same amount of '/' characters.
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if there isn't the same amount of '/' characters, it means %variable
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen expansion added more of them and we can't handle this. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen for (i = 0; unexpanded_start+i != unexpanded_stop; i++) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (unexpanded_start[i] == '/')
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen slash_count++;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen }
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen for (; unexpanded_start[i] != '\0'; i++) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (unexpanded_start[i] == '/')
a050ca9def13949dbaa67bd6574a41c4f397ae26Timo Sirainen slash2_count++;
a050ca9def13949dbaa67bd6574a41c4f397ae26Timo Sirainen }
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen for (i = 0; expanded_full[i] != '\0'; i++) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (expanded_full[i] == '/') {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (slash_count == 0)
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen break;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen slash_count--;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (slash_count != 0)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return "";
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen ret = t_strndup(expanded_full, i);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen for (; expanded_full[i] != '\0'; i++) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (expanded_full[i] == '/') {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (slash2_count == 0)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return "";
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen slash2_count--;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen }
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen if (slash2_count != 0)
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen return "";
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen return ret;
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen}
f2786c07cbd4a7a0a6a46c3e06dc4545aaf2f278Timo Sirainen
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainenint mailbox_list_mkdir_root(struct mailbox_list *list, const char *path,
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen enum mailbox_list_path_type type,
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen const char **error_r)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen{
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen const char *expanded, *unexpanded, *root_dir, *p, *origin, *error;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen struct stat st;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mode_t file_mode, dir_mode;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen gid_t gid;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (stat(path, &st) == 0) {
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen /* looks like it already exists, don't bother checking
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen further. */
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen return 0;
5e2cf2e665bb0c3c68fa34bc6d0e9ce93426fdd0Timo Sirainen }
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen if (!mail_user_is_path_mounted(list->ns->user, path, &error)) {
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen *error_r = t_strdup_printf(
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen "Can't create mailbox root dir %s: %s", path, error);
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen return -1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mailbox_list_get_root_permissions(list, &file_mode, &dir_mode,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen &gid, &origin);
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* get the directory path up to last %variable. for example
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen unexpanded path may be "/var/mail/%d/%2n/%n/Maildir", and we want
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen to get expanded="/var/mail/domain/nn" */
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen unexpanded = mailbox_list_get_unexpanded_path(list, type);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen p = strrchr(unexpanded, '%');
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen if (p == NULL)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen expanded = "";
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen else {
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen while (p != unexpanded && *p != '/') p--;
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen if (p == unexpanded)
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen expanded = "";
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen else {
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen expanded = mailbox_list_get_root_path(list, type);
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen expanded = get_expanded_path(unexpanded, p, expanded);
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen }
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen }
b9ce555e8624a5593b3bfd81b572b7d2e1e1fca5Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (*expanded != '\0') {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* up to this directory get the permissions from the first
b9ce555e8624a5593b3bfd81b572b7d2e1e1fca5Timo Sirainen parent directory that exists, if it has setgid bit
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen enabled. */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mailbox_list_stat_parent(expanded, &root_dir, &st,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen error_r) < 0)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return -1;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if ((st.st_mode & S_ISGID) != 0 && root_dir != expanded) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (mkdir_parents_chgrp(expanded, st.st_mode,
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen (gid_t)-1, root_dir) < 0 &&
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen errno != EEXIST) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen *error_r = t_strdup_printf(
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen "mkdir(%s) failed: %m", expanded);
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen return -1;
e4fb5bfcdff32d337d054cce36e00e1cdfaae9f8Timo Sirainen }
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (gid == (gid_t)-1 && (dir_mode & S_ISGID) == 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* change the group for user directories */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen gid = getegid();
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* the rest of the directories exist only for one user. create them
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen with default directory permissions */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (mkdir_parents_chgrp(path, dir_mode, gid, origin) < 0 &&
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen errno != EEXIST) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (errno == EACCES)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen *error_r = mail_error_create_eacces_msg("mkdir", path);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen else
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen *error_r = t_strdup_printf("mkdir(%s) failed: %m", path);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return -1;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen }
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen return 0;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen}
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenstatic bool
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenmailbox_list_is_valid_fs_name(struct mailbox_list *list, const char *name,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen const char **error_r)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen{
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen bool ret, allow_internal_dirs;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen *error_r = NULL;
19593623a2278039be23fc93e3404c012920b664Timo Sirainen
19593623a2278039be23fc93e3404c012920b664Timo Sirainen if (list->mail_set->mail_full_filesystem_access)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return TRUE;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* make sure it's not absolute path */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if (*name == '/') {
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen *error_r = "Begins with '/'";
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return FALSE;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if (*name == '~') {
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen *error_r = "Begins with '~'";
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen return FALSE;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* make sure the mailbox name doesn't contain any foolishness:
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen "../" could give access outside the mailbox directory.
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen "./" and "//" could fool ACL checks. */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen allow_internal_dirs = list->v.is_internal_name == NULL ||
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen *list->set.maildir_name != '\0';
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen T_BEGIN {
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen const char *const *names;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen names = t_strsplit(name, "/");
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen for (; *names != NULL; names++) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen const char *n = *names;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (*n == '\0') {
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen *error_r = "Has adjacent '/' chars";
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen break; /* // */
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (*n == '.') {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (n[1] == '\0') {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen *error_r = "Contains '.' part";
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen break; /* ./ */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (n[1] == '.' && n[2] == '\0') {
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen *error_r = "Contains '..' part";
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen break; /* ../ */
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen }
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen if (*list->set.maildir_name != '\0' &&
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen strcmp(list->set.maildir_name, n) == 0) {
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen /* don't allow maildir_name to be used as part
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen of the mailbox name */
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen *error_r = "Contains reserved name";
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen break;
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen }
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (!allow_internal_dirs &&
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen list->v.is_internal_name(list, n)) {
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen *error_r = "Contains reserved name";
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen break;
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen }
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen ret = *names == NULL;
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen } T_END;
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenbool mailbox_list_is_valid_name(struct mailbox_list *list,
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainen const char *name, const char **error_r)
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainen{
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if (*name == '\0' && *list->ns->prefix != '\0') {
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen /* an ugly way to get to mailbox root (e.g. Maildir/ when
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen it's not the INBOX) */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen return TRUE;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen }
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen return mailbox_list_is_valid_fs_name(list, name, error_r);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen}
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainenconst char *mailbox_list_get_path(struct mailbox_list *list, const char *name,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen enum mailbox_list_path_type type)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen{
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen return list->v.get_path(list, name, type);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen}
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenconst char *mailbox_list_get_root_path(struct mailbox_list *list,
19593623a2278039be23fc93e3404c012920b664Timo Sirainen enum mailbox_list_path_type type)
19593623a2278039be23fc93e3404c012920b664Timo Sirainen{
19593623a2278039be23fc93e3404c012920b664Timo Sirainen return list->v.get_path(list, NULL, type);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmailbox_list_set_get_root_path(const struct mailbox_list_settings *set,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mailbox_list_path_type type)
3e4bbbceed7882b8dab558aa90913a491eb15cc6Timo Sirainen{
3e4bbbceed7882b8dab558aa90913a491eb15cc6Timo Sirainen const char *path;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen switch (type) {
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen case MAILBOX_LIST_PATH_TYPE_DIR:
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen return set->root_dir;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen case MAILBOX_LIST_PATH_TYPE_ALT_DIR:
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return set->alt_dir;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen case MAILBOX_LIST_PATH_TYPE_MAILBOX:
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (*set->mailbox_dir_name == '\0')
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return set->root_dir;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen path = t_strconcat(set->root_dir, "/",
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen set->mailbox_dir_name, NULL);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return t_strndup(path, strlen(path)-1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case MAILBOX_LIST_PATH_TYPE_ALT_MAILBOX:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (*set->mailbox_dir_name == '\0')
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return set->root_dir;
0e17a51126ed6f376f9207217797103b610ff8ebTimo Sirainen path = t_strconcat(set->alt_dir, "/",
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen set->mailbox_dir_name, NULL);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return path == NULL ? NULL : t_strndup(path, strlen(path)-1);
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen case MAILBOX_LIST_PATH_TYPE_CONTROL:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return set->control_dir != NULL ?
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen set->control_dir : set->root_dir;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen case MAILBOX_LIST_PATH_TYPE_INDEX:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return set->index_dir != NULL ?
20c26f4fcf9ef87434761829cc209c2f84ff5716Timo Sirainen set->index_dir : set->root_dir;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen case MAILBOX_LIST_PATH_TYPE_INDEX_PRIVATE:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return set->index_pvt_dir;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_unreached();
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen}
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenconst char *mailbox_list_get_temp_prefix(struct mailbox_list *list)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen{
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen return list->v.get_temp_prefix(list, FALSE);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen}
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenconst char *mailbox_list_get_global_temp_prefix(struct mailbox_list *list)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen{
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen return list->v.get_temp_prefix(list, TRUE);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen}
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenconst char *mailbox_list_join_refpattern(struct mailbox_list *list,
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen const char *ref, const char *pattern)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen{
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen if (list->v.join_refpattern != NULL)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen return list->v.join_refpattern(list, ref, pattern);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen /* the default implementation: */
d7095f3a4466fbb78b2d5eb3d322bc15a5b0ab1fTimo Sirainen if (*ref != '\0') {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* merge reference and pattern */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen pattern = t_strconcat(ref, pattern, NULL);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return pattern;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen}
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenint mailbox_has_children(struct mailbox_list *list, const char *name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen struct mailbox_list_iterate_context *iter;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *pattern;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen pattern = t_strdup_printf("%s%c%%", name,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen mail_namespace_get_sep(list->ns));
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen iter = mailbox_list_iter_init(list, pattern,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ret = mailbox_list_iter_next(iter) != NULL ? 1 : 0;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (mailbox_list_iter_deinit(&iter) < 0)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen ret = -1;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen return ret;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen}
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainenint mailbox_list_mailbox(struct mailbox_list *list, const char *name,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen enum mailbox_info_flags *flags_r)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *path, *fname, *rootdir, *dir, *inbox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int len;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *flags_r = 0;
4b058f90f9e8a2c6b2eed275de4eb8cc5195a71dTimo Sirainen
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen strcasecmp(name, "INBOX") == 0) {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* special handling for INBOX, mainly because with Maildir++
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen layout it needs to check if the cur/ directory exists,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen which the Maildir++ layout backend itself can't do.. */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen struct mailbox *box;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen enum mailbox_existence existence;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen int ret;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* kludge: with imapc backend we can get here with
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen list=Maildir++ (for indexes), but list->ns->list=imapc */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen box = mailbox_alloc(list->ns->list, "INBOX", 0);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ret = mailbox_exists(box, FALSE, &existence);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mailbox_free(&box);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if (ret < 0) {
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* this can only be an internal error */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mailbox_list_set_internal_error(list);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen return -1;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen switch (existence) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen case MAILBOX_EXISTENCE_NONE:
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen case MAILBOX_EXISTENCE_NOSELECT:
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen *flags_r |= MAILBOX_NONEXISTENT;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen return 0;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen case MAILBOX_EXISTENCE_SELECT:
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen break;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen }
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return 1;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen }
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (list->v.get_mailbox_flags == NULL) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* can't do this optimized. do it the slow way. */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct mailbox_list_iterate_context *iter;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mailbox_info *info;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *vname;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen vname = mailbox_list_get_vname(list, name);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen iter = mailbox_list_iter_init(list, vname, 0);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen info = mailbox_list_iter_next(iter);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen if (info == NULL)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *flags_r = MAILBOX_NONEXISTENT;
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen else
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen *flags_r = info->flags;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen return mailbox_list_iter_deinit(&iter);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen }
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen rootdir = mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_MAILBOX);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen i_assert(rootdir != NULL);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen path = mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_DIR);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen fname = strrchr(path, '/');
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (fname == NULL) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen fname = path;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen dir = "/";
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen } else {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen dir = t_strdup_until(path, fname);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen fname++;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen len = strlen(rootdir);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (strncmp(path, rootdir, len) == 0 && path[len] == '/') {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* looking up a regular mailbox under mail root dir */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen } else if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen strcasecmp(name, "INBOX") == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* looking up INBOX that's elsewhere */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen } else {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* looking up the root dir itself */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen dir = path;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen fname = "";
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (*fname == '\0' && *name == '\0' &&
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen (list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* if INBOX is in e.g. ~/Maildir, it shouldn't be possible to
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen access it also via namespace prefix. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen inbox = mailbox_list_get_path(list, "INBOX",
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (strcmp(inbox, dir) == 0) {
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen *flags_r |= MAILBOX_NONEXISTENT;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return 0;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen }
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return list->v.get_mailbox_flags(list, dir, fname,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen MAILBOX_LIST_FILE_TYPE_UNKNOWN,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen flags_r);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic bool mailbox_list_init_changelog(struct mailbox_list *list)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen const char *path;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mode_t file_mode, dir_mode;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen gid_t gid;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const char *gid_origin;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (list->changelog != NULL)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return TRUE;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* don't do this in mailbox_list_create(), because _get_path() might be
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen overridden by storage (mbox). */
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen path = mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_INDEX);
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen if (*path == '\0')
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return FALSE;
c9f1a617593eb569fb02f45041bad3c13e534496Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen path = t_strconcat(path, "/"MAILBOX_LOG_FILE_NAME, NULL);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen list->changelog = mailbox_log_alloc(path);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mailbox_list_get_root_permissions(list, &file_mode, &dir_mode,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen &gid, &gid_origin);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mailbox_log_set_permissions(list->changelog, dir_mode, gid, gid_origin);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return TRUE;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_add_change(struct mailbox_list *list,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen enum mailbox_log_record_type type,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen const guid_128_t mailbox_guid)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen struct mailbox_log_record rec;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen time_t stamp;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (!mailbox_list_init_changelog(list) ||
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen guid_128_is_empty(mailbox_guid))
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen return;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!list->index_root_dir_created) {
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (mailbox_list_create_missing_index_dir(list, NULL) < 0)
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen return;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen }
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen stamp = list->changelog_timestamp != (time_t)-1 ?
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen list->changelog_timestamp : ioloop_time;
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memset(&rec, 0, sizeof(rec));
ea9e10039e23a4926984a56860b5cc88f7b1f75eTimo Sirainen rec.type = type;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memcpy(rec.mailbox_guid, mailbox_guid, sizeof(rec.mailbox_guid));
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_log_record_set_timestamp(&rec, stamp);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen (void)mailbox_log_append(list->changelog, &rec);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen}
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenint mailbox_list_set_subscribed(struct mailbox_list *list,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const char *name, bool set)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen guid_128_t guid;
2bc9f8181d576165cd30ce0522754420097037cdTimo Sirainen int ret;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* make sure we'll refresh the file on next list */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->subscriptions_mtime = (time_t)-1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((ret = list->v.set_subscribed(list, name, set)) <= 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* subscriptions are about names, not about mailboxes. it's possible
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen to have a subscription to nonexistent mailbox. renames also don't
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen change subscriptions. so instead of using actual GUIDs, we'll use
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hash of the name. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_name_get_sha128(name, guid);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_list_add_change(list, set ? MAILBOX_LOG_RECORD_SUBSCRIBE :
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen MAILBOX_LOG_RECORD_UNSUBSCRIBE, guid);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainenint mailbox_list_delete_dir(struct mailbox_list *list, const char *name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *error;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen if (!mailbox_list_is_valid_name(list, name, &error) || *name == '\0') {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Invalid mailbox name");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return list->v.delete_dir(list, name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenint mailbox_list_delete_symlink(struct mailbox_list *list, const char *name)
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *error;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!mailbox_list_is_valid_name(list, name, &error) || *name == '\0') {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Invalid mailbox name");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen return list->v.delete_symlink(list, name);
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mailbox_name_get_sha128(const char *name, guid_128_t guid_128_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned char sha[SHA1_RESULTLEN];
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen sha1_get_digest(name, strlen(name), sha);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memcpy(guid_128_r, sha, I_MIN(GUID_128_SIZE, sizeof(sha)));
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mailbox_log *mailbox_list_get_changelog(struct mailbox_list *list)
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen{
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen return !mailbox_list_init_changelog(list) ? NULL : list->changelog;
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen}
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainenvoid mailbox_list_set_changelog_timestamp(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t stamp)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->changelog_timestamp = stamp;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenbool mailbox_list_name_is_too_large(const char *name, char sep)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int levels = 1, level_len = 0;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (; *name != '\0'; name++) {
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen if (*name == sep) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen levels++;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen level_len = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen level_len++;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (levels > MAILBOX_MAX_HIERARCHY_LEVELS)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainenenum mailbox_list_file_type
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainenmailbox_list_get_file_type(const struct dirent *d ATTR_UNUSED)
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen{
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen enum mailbox_list_file_type type;
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen#ifdef HAVE_DIRENT_D_TYPE
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen switch (d->d_type) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case DT_UNKNOWN:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_UNKNOWN;
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case DT_REG:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen type = MAILBOX_LIST_FILE_TYPE_FILE;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen break;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen case DT_DIR:
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen type = MAILBOX_LIST_FILE_TYPE_DIR;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen break;
264bc938e956288ec7a2a43417c01c3a3ce840fdTimo Sirainen case DT_LNK:
264bc938e956288ec7a2a43417c01c3a3ce840fdTimo Sirainen type = MAILBOX_LIST_FILE_TYPE_SYMLINK;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen break;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen default:
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen type = MAILBOX_LIST_FILE_TYPE_OTHER;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen break;
024815ea2ffdda9ea79919f18e865663977f73eaTimo Sirainen }
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen#else
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen type = MAILBOX_LIST_FILE_TYPE_UNKNOWN;
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen#endif
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen return type;
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen}
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainenstatic bool
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainenmailbox_list_try_get_home_path(struct mailbox_list *list, const char **name)
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen{
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen if ((*name)[1] == '/') {
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen /* ~/dir - use the configured home directory */
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen if (mail_user_try_home_expand(list->ns->user, name) < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return FALSE;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen } else {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* ~otheruser/dir - assume we're using system users */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (home_try_expand(name) < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return FALSE;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return TRUE;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen}
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenbool mailbox_list_try_get_absolute_path(struct mailbox_list *list,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const char **name)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen{
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const char *root_dir, *path, *mailbox_name;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen unsigned int len;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!list->mail_set->mail_full_filesystem_access)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return FALSE;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (**name == '~') {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* try to expand home directory */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!mailbox_list_try_get_home_path(list, name)) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen /* fallback to using actual "~name" mailbox */
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen return FALSE;
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen if (**name != '/')
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen return FALSE;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* okay, we have an absolute path now. but check first if it points to
40ef82c46f6652412b068ebcdac7c3e74840a284Timo Sirainen same directory as one of our regular mailboxes. */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen root_dir = mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_MAILBOX);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen len = strlen(root_dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (strncmp(root_dir, *name, len) == 0 && (*name)[len] == '/') {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_name = *name + len + 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = mailbox_list_get_path(list, mailbox_name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (strcmp(path, *name) == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* yeah, we can replace the full path with mailbox
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen name. this way we can use indexes. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *name = mailbox_name;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenint mailbox_list_mkdir(struct mailbox_list *list,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen const char *mailbox, const char *path)
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen{
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen struct mailbox_permissions perm;
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen if (mailbox != NULL)
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen mailbox_list_get_permissions(list, mailbox, &perm);
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen else {
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen mailbox_list_get_root_permissions(list,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen &perm.file_create_mode,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen &perm.dir_create_mode,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen &perm.file_create_gid,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen &perm.file_create_gid_origin);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen }
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen if (mkdir_parents_chgrp(path, perm.dir_create_mode,
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen perm.file_create_gid,
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen perm.file_create_gid_origin) == 0)
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen return 1;
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen else if (errno == EEXIST)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen return 0;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen else if (errno == ENOTDIR) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_list_set_error(list, MAIL_ERROR_NOTPOSSIBLE,
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen "Mailbox doesn't allow inferior mailboxes");
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen return -1;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen } else if (mailbox_list_set_error_from_errno(list)) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen return -1;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen } else {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen mailbox_list_set_critical(list, "mkdir_parents(%s) failed: %m",
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen path);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen return -1;
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen }
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen}
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainenint mailbox_list_mkdir_parent(struct mailbox_list *list,
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen const char *mailbox, const char *path)
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *p;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen p = strrchr(path, '/');
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen if (p == NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mailbox_list_mkdir(list, mailbox, t_strdup_until(path, p));
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen}
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainenstatic int ATTR_NULL(2)
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainenmailbox_list_create_missing_dir(struct mailbox_list *list, const char *name,
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen enum mailbox_list_path_type type)
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen{
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen const char *root_dir, *index_dir, *parent_dir, *p, *error;
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen struct mailbox_permissions perm;
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen unsigned int n = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen root_dir = name == NULL ?
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_MAILBOX) :
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_MAILBOX);
ee246b46953e4b94b2f22e093373674fa9155500Timo Sirainen index_dir = mailbox_list_get_path(list, name, type);
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen if (index_dir == NULL || *index_dir == '\0')
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (strcmp(index_dir, root_dir) == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((list->props & MAILBOX_LIST_PROP_AUTOCREATE_DIRS) == 0)
ec1e30ecc38f0deddaf655413cf02d5972ddbc70Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* the directory might not have been created yet */
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (name == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_list_mkdir_root(list, index_dir, type,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen &error) < 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mailbox_list_set_critical(list,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen "Couldn't create index root dir %s: %s",
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen index_dir, error);
a53cb86b4d733d9c48ee4d285bed477c80825804Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen return 0;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen }
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen mailbox_list_get_permissions(list, name, &perm);
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen while (mkdir_chgrp(index_dir, perm.dir_create_mode,
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen perm.file_create_gid,
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen perm.file_create_gid_origin) < 0) {
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen if (errno == EEXIST)
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen break;
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen p = strrchr(index_dir, '/');
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno != ENOENT || p == NULL || ++n == 2) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_list_set_critical(list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "mkdir(%s) failed: %m", index_dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* create the parent directory first */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen parent_dir = t_strdup_until(index_dir, p);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_list_mkdir_root(list, parent_dir, type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen &error) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mailbox_list_set_critical(list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Couldn't create index dir %s: %s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen parent_dir, error);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
8afec4d1a32b78f540257a27769b372aad753384Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mailbox_list_create_missing_index_dir(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen list->index_root_dir_created = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mailbox_list_create_missing_dir(list, name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MAILBOX_LIST_PATH_TYPE_INDEX);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenint mailbox_list_create_missing_index_pvt_dir(struct mailbox_list *list,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen const char *name)
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen{
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen return mailbox_list_create_missing_dir(list, name,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen MAILBOX_LIST_PATH_TYPE_INDEX_PRIVATE);
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *mailbox_list_get_last_error(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_error *error_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (error_r != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *error_r = list->error;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen return list->error_string != NULL ? list->error_string :
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Unknown internal list error";
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mailbox_list_clear_error(struct mailbox_list *list)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen i_free_and_null(list->error_string);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen list->error = MAIL_ERROR_NONE;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_set_error(struct mailbox_list *list,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen enum mail_error error, const char *string)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen i_free(list->error_string);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen list->error_string = i_strdup(string);
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen list->error = error;
325d4ad220bd13f6d176391d962a0e33c856a7f6Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_set_internal_error(struct mailbox_list *list)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen const char *str;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen str = t_strflocaltime(MAIL_ERRSTR_CRITICAL_MSG_STAMP, ioloop_time);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen i_free(list->error_string);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen list->error_string = i_strdup(str);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen list->error = MAIL_ERROR_TEMP;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_set_critical(struct mailbox_list *list, const char *fmt, ...)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen{
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen va_list va;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen va_start(va, fmt);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen i_error("%s", t_strdup_vprintf(fmt, va));
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen va_end(va);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* critical errors may contain sensitive data, so let user
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen see only "Internal error" with a timestamp to make it
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen easier to look from log files the actual error message. */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mailbox_list_set_internal_error(list);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainenbool mailbox_list_set_error_from_errno(struct mailbox_list *list)
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen{
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen const char *error_string;
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen enum mail_error error;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (!mail_error_from_errno(&error, &error_string))
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return FALSE;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mailbox_list_set_error(list, error, error_string);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return TRUE;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen}
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen