mailbox-list.c revision 48ce03445eaea184426156440116ca1da978bb54
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (c) 2006-2012 Dovecot authors, see the included COPYING file */
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. */
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstruct mailbox_list_module_register mailbox_list_module_register = { 0 };
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenstatic ARRAY(const struct mailbox_list *) mailbox_list_drivers;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic bool mailbox_list_driver_find(const char *name, unsigned int *idx_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int i, count;
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) {
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainenvoid mailbox_list_register(const struct mailbox_list *list)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen unsigned int idx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_list_driver_find(list->name, &idx)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_fatal("mailbox_list_register(%s): duplicate driver",
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen array_append(&mailbox_list_drivers, &list, 1);
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainenvoid mailbox_list_unregister(const struct mailbox_list *list)
20a802016205bbcafc90f164f769ea801f88d014Timo Sirainen unsigned int idx;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if (!mailbox_list_driver_find(list->name, &idx)) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_fatal("mailbox_list_unregister(%s): unknown driver",
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen unsigned int idx;
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen class_p = array_idx(&mailbox_list_drivers, idx);
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenint mailbox_list_create(const char *driver, struct mail_namespace *ns,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen struct mailbox_list **list_r, const char **error_r)
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen unsigned int idx;
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (!mailbox_list_driver_find(driver, &idx)) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen class_p = array_idx(&mailbox_list_drivers, idx);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (((*class_p)->props & MAILBOX_LIST_PROP_NO_MAILDIR_NAME) != 0 &&
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen *error_r = "maildir_name not supported by this driver";
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen if (((*class_p)->props & MAILBOX_LIST_PROP_NO_ALT_DIR) != 0 &&
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainen *error_r = "alt_dir not supported by this driver";
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen i_assert(set->root_dir == NULL || *set->root_dir != '\0' ||
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen ((*class_p)->props & MAILBOX_LIST_PROP_NO_ROOT) != 0);
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen array_create(&list->module_contexts, list->pool, sizeof(void *), 5);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen /* copy settings */
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 :
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 list->set.control_dir = set->control_dir == NULL ||
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen strcmp(set->control_dir, set->root_dir) == 0 ? NULL :
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen list->set.inbox_path = p_strdup(list->pool, set->inbox_path);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strdup(list->pool, set->subscription_fname);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen list->set.alt_dir = p_strdup(list->pool, set->alt_dir);
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen else if (set->mailbox_dir_name[strlen(set->mailbox_dir_name)-1] == '/') {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen p_strconcat(list->pool, set->mailbox_dir_name, "/", NULL);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen i_debug("%s: root=%s, index=%s, indexpvt=%s, control=%s, inbox=%s, alt=%s",
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,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen list->set.alt_dir == NULL ? "" : list->set.alt_dir);
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen if ((flags & MAILBOX_LIST_FLAG_SECONDARY) == 0)
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainenstatic int fix_path(struct mail_user *user, const char *path,
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (path[0] == '~' && path[1] != '/' && path[1] != '\0') {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen "No home directory for system user. "
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 ";
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainenstatic const char *split_next_arg(const char *const **_args)
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen /* string ends with ":", just ignore it. */
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainenint mailbox_list_settings_parse(struct mail_user *user, const char *data,
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const char **error_r)
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen const char *const *tmp, *key, *value, **dest, *str, *error;
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen /* <root dir> */
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);
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: ",
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen *error_r = t_strdup_printf("Unknown setting: %s", key);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (fix_path(user, value, dest, &error) < 0) {
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen *error_r = t_strconcat(error, key, " in: ", data, NULL);
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if (set_r->index_dir != NULL && strcmp(set_r->index_dir, "MEMORY") == 0)
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainenconst char *mailbox_list_get_unexpanded_path(struct mailbox_list *list,
b79ec51bdeef6ef950eb5e890e65cc0491cf5fe9Timo Sirainen const char *location = list->ns->unexpanded_set->location;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen const char *p, *error;
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen if (*location == SETTING_STRVAR_EXPANDED[0]) {
64541374b58e4c702b1926e87df421d180ffa006Timo Sirainen /* set using -o or userdb lookup. */
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen i_assert(*location == SETTING_STRVAR_UNEXPANDED[0]);
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen mail_set = mail_user_set_get_driver_settings(user->set_info,
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen user->unexpanded_set, MAIL_STORAGE_SET_DRIVER_NAME);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen i_assert(*location == SETTING_STRVAR_UNEXPANDED[0]);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* type:settings */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (mailbox_list_settings_parse(user, p + 1, &set, &error) < 0)
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen return mailbox_list_set_get_root_path(&set, type);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic bool need_escape_dirstart(const char *vname, const char *maildir_name)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int len;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (vname[1] == '.' && (vname[2] == '\0' || vname[2] == '/'))
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstatic const char *
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenmailbox_list_escape_name(struct mailbox_list *list, const char *vname)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen char ns_sep = mail_namespace_get_sep(list->ns);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen char list_sep = mailbox_list_get_hierarchy_sep(list);
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);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* escape the mailbox name */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen need_escape_dirstart(vname, list->set.maildir_name))) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenconst char *mailbox_list_default_get_storage_name(struct mailbox_list *list,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen storage_name = mailbox_list_escape_name(list, vname);
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 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 */
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen /* we're converting a nonexistent mailbox name,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen such as a LIST pattern. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* UTF-8 -> mUTF-7 conversion */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen list_sep = mailbox_list_get_hierarchy_sep(list);
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 (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. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ret = p_strdup(unsafe_data_stack_pool, storage_name);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenconst char *mailbox_list_get_storage_name(struct mailbox_list *list,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenstatic const char *
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenmailbox_list_unescape_name(struct mailbox_list *list, const char *src)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen char ns_sep = mail_namespace_get_sep(list->ns);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen char list_sep = mailbox_list_get_hierarchy_sep(list);
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen unsigned int num;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (strncmp(src, list->ns->prefix, list->ns->prefix_len) == 0) {
811f2e26d9782d9cb99fdf82e18ffa0a77564fe2Timo Sirainen str_append_n(dest, src, list->ns->prefix_len);
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainenconst char *mailbox_list_default_get_vname(struct mailbox_list *list,
e12648867876aaec17e06ee4caef0bb60363449dTimo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
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. */
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. */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* return namespace prefix without the separator */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* mUTF-7 -> UTF-8 conversion */
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen list_sep = mailbox_list_get_hierarchy_sep(list);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* @UNSAFE */
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen for (i = 0; i < name_len; i++) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen vname = mailbox_list_unescape_name(list, vname);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenconst char *mailbox_list_get_vname(struct mailbox_list *list, const char *name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mailbox_list_destroy(struct mailbox_list **_list)
0ce3bbb0f03fb0ee99258b41b5a1d689c1158a75Timo Sirainen if (hash_table_is_created(list->guid_cache)) {
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenconst char *mailbox_list_get_driver_name(const struct mailbox_list *list)
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenenum mailbox_list_flags mailbox_list_get_flags(const struct mailbox_list *list)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenmailbox_list_get_namespace(const struct mailbox_list *list)
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* add the execute bit if either read or write bit is set */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenmailbox_list_get_user(const struct mailbox_list *list)
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenint mailbox_list_get_storage(struct mailbox_list **list, const char *vname,
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen return (*list)->v.get_storage(list, vname, storage_r);
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainenvoid mailbox_list_get_closest_storage(struct mailbox_list *list,
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainenchar mailbox_list_get_hierarchy_sep(struct mailbox_list *list)
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenmailbox_list_get_permissions_internal(struct mailbox_list *list,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen memset(permissions_r, 0, sizeof(*permissions_r));
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen /* use safe defaults */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen permissions_r->file_create_gid_origin = "defaults";
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);
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen /* no filesystem support in storage */
b2105c78f0fd58281317e6d777ded860f33153a3Timo Sirainen mailbox_list_set_critical(list, "stat(%s) failed: %m",
4c1827d5d718d6f610df3209a2eb95a6591698afTimo Sirainen i_debug("Namespace %s: %s doesn't exist yet, "
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen "using default permissions",
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* return parent mailbox */
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen p = strrchr(name, mailbox_list_get_hierarchy_sep(list));
91b5eae18db48ebb70eee5407a7ab52bf798ee12Timo Sirainen /* return root defaults */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen mailbox_list_get_permissions(list, parent_name,
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* assume current defaults for mailboxes that don't exist or
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen can't be looked up for some other reason */
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 /* we're getting permissions from a file.
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen apply +x modes as necessary. */
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 } 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 /* using our own gid, no need to change it */
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;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (list->mail_set->mail_debug && name == NULL) {
b47c57ee07ccfe76cc23b23bc720e17fe9a46d5cTimo Sirainen i_debug("Namespace %s: Using permissions from %s: "
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenvoid mailbox_list_get_permissions(struct mailbox_list *list, const char *name,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen mailbox_list_get_permissions_internal(list, name, permissions_r);
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainenvoid mailbox_list_get_root_permissions(struct mailbox_list *list,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mailbox_list_get_permissions_internal(list, NULL, &perm);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenmailbox_list_stat_parent(const char *path, const char **root_dir_r,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *p;
45edf6cad665a5c270322516c587708a0c630b80Timo Sirainen if (errno != ENOENT || strcmp(path, "/") == 0) {
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen *error_r = t_strdup_printf("stat(%s) failed: %m", path);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenstatic const char *
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenget_expanded_path(const char *unexpanded_start, const char *unexpanded_stop,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen const char *ret;
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen unsigned int i, slash_count = 0, slash2_count = 0;
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++) {
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainenint mailbox_list_mkdir_root(struct mailbox_list *list, const char *path,
0b38c17fb10e2ad1d081369d34b93d0448d971a2Timo Sirainen const char **error_r)
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen const char *expanded, *unexpanded, *root_dir, *p, *origin, *error;
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen /* looks like it already exists, don't bother checking
a80241dc44ee43bc809ed5ec6dedb74711966dafTimo Sirainen if (!mail_user_is_path_mounted(list->ns->user, path, &error)) {
09714f2f8d4c8cc7d89930f7eb836816d8dc9eb8Timo Sirainen "Can't create mailbox root dir %s: %s", path, error);
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen mailbox_list_get_root_permissions(list, &file_mode, &dir_mode,
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);
673ab5815277e6f9ac09227c866c90ab57438912Timo Sirainen expanded = mailbox_list_get_root_path(list, type);
2ef8fac74c9d0bfc330bf53bb06df7e9d5f08e7dTimo Sirainen expanded = get_expanded_path(unexpanded, p, expanded);
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 if (mailbox_list_stat_parent(expanded, &root_dir, &st,
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainen if ((st.st_mode & S_ISGID) != 0 && root_dir != expanded) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if (gid == (gid_t)-1 && (dir_mode & S_ISGID) == 0) {
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen /* change the group for user directories */
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 *error_r = mail_error_create_eacces_msg("mkdir", path);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen *error_r = t_strdup_printf("mkdir(%s) failed: %m", path);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenmailbox_list_is_valid_fs_name(struct mailbox_list *list, const char *name,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen const char **error_r)
19593623a2278039be23fc93e3404c012920b664Timo Sirainen if (list->mail_set->mail_full_filesystem_access)
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen /* make sure it's not absolute path */
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 const char *const *names;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen const char *n = *names;
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen if (*n == '\0') {
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainen break; /* // */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (*n == '.') {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen break; /* ./ */
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen break; /* ../ */
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen /* don't allow maildir_name to be used as part
e2ea49bd487d76b425e270321e9cf1e546642de0Timo Sirainen of the mailbox name */
e86d0d34fe365da4c7ca4312d575bfcbf3a01c0eTimo Sirainenbool mailbox_list_is_valid_name(struct mailbox_list *list,
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) */
c48140ca508ebc9642737e7fd6c8d9e52f95df32Timo Sirainen return mailbox_list_is_valid_fs_name(list, name, error_r);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainenconst char *mailbox_list_get_path(struct mailbox_list *list, const char *name,
3c24d47ad5ff02ea00684233bef314ef2eefda4aTimo Sirainenconst char *mailbox_list_get_root_path(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmailbox_list_set_get_root_path(const struct mailbox_list_settings *set,
ed3ce1282f6bc35d20e82c2c23a2990c8dfe876fTimo Sirainen return path == NULL ? NULL : t_strndup(path, strlen(path)-1);
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenconst char *mailbox_list_get_temp_prefix(struct mailbox_list *list)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenconst char *mailbox_list_get_global_temp_prefix(struct mailbox_list *list)
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainenconst char *mailbox_list_join_refpattern(struct mailbox_list *list,
75ef04fc62a3955d3a5310410e09735cbd4e972bTimo Sirainen return list->v.join_refpattern(list, ref, pattern);
8e7da21696c9f8a6d5e601243fb6172ec85d47b2Timo Sirainen /* the default implementation: */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* merge reference and pattern */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainenint mailbox_has_children(struct mailbox_list *list, const char *name)
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen ret = mailbox_list_iter_next(iter) != NULL ? 1 : 0;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainenint mailbox_list_mailbox(struct mailbox_list *list, const char *name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *path, *fname, *rootdir, *dir, *inbox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int len;
1b3bb8d39686ed24730cbc31cc9a33dc62c8c6c3Timo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 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.. */
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 /* this can only be an internal error */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* can't do this optimized. do it the slow way. */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen iter = mailbox_list_iter_init(list, vname, 0);
287ba82a8da3eaa473b5735d4eeac2fb4c5d8117Timo Sirainen rootdir = mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_MAILBOX);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen path = mailbox_list_get_path(list, name, MAILBOX_LIST_PATH_TYPE_DIR);
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 /* looking up INBOX that's elsewhere */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* looking up the root dir itself */
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 return list->v.get_mailbox_flags(list, dir, fname,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic bool mailbox_list_init_changelog(struct mailbox_list *list)
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);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen path = t_strconcat(path, "/"MAILBOX_LOG_FILE_NAME, NULL);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mailbox_list_get_root_permissions(list, &file_mode, &dir_mode,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen mailbox_log_set_permissions(list->changelog, dir_mode, gid, gid_origin);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_add_change(struct mailbox_list *list,
7e94cf9d70ce9fdeccb7a85ff400b899e6386f36Timo Sirainen if (mailbox_list_create_missing_index_dir(list, NULL) < 0)
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen stamp = list->changelog_timestamp != (time_t)-1 ?
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 Sirainenint mailbox_list_set_subscribed(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* make sure we'll refresh the file on next list */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((ret = list->v.set_subscribed(list, name, set)) <= 0)
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. */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_list_add_change(list, set ? MAILBOX_LOG_RECORD_SUBSCRIBE :
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainenint mailbox_list_delete_dir(struct mailbox_list *list, const char *name)
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");
93b29720c5141f787bd1861796867e4595c9d084Timo Sirainenint mailbox_list_delete_symlink(struct mailbox_list *list, const char *name)
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 Sirainenvoid mailbox_name_get_sha128(const char *name, guid_128_t guid_128_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen memcpy(guid_128_r, sha, I_MIN(GUID_128_SIZE, sizeof(sha)));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mailbox_log *mailbox_list_get_changelog(struct mailbox_list *list)
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainen return !mailbox_list_init_changelog(list) ? NULL : list->changelog;
9fc077d3886b1584019bcc9b92c717cfffce5c67Timo Sirainenvoid mailbox_list_set_changelog_timestamp(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenbool mailbox_list_name_is_too_large(const char *name, char sep)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (level_len > MAILBOX_MAX_HIERARCHY_NAME_LENGTH)
86bc22621dcce6d9f1ca7216c9cc958381a61277Timo Sirainenmailbox_list_get_file_type(const struct dirent *d ATTR_UNUSED)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen switch (d->d_type) {
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainenmailbox_list_try_get_home_path(struct mailbox_list *list, const char **name)
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen /* ~/dir - use the configured home directory */
e687badfdd7f4001e9a89a80a2c4a79ec4bafc8dTimo Sirainen if (mail_user_try_home_expand(list->ns->user, name) < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* ~otheruser/dir - assume we're using system users */
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenbool mailbox_list_try_get_absolute_path(struct mailbox_list *list,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen const char **name)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen unsigned int len;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (!list->mail_set->mail_full_filesystem_access)
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 */
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 if (strncmp(root_dir, *name, len) == 0 && (*name)[len] == '/') {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen path = mailbox_list_get_path(list, mailbox_name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* yeah, we can replace the full path with mailbox
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen name. this way we can use indexes. */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainenint mailbox_list_mkdir(struct mailbox_list *list,
67b9db4e0bd93ffe24cc95f609193f6f642f6448Timo Sirainen mailbox_list_get_permissions(list, mailbox, &perm);
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen if (mkdir_parents_chgrp(path, perm.dir_create_mode,
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen mailbox_list_set_error(list, MAIL_ERROR_NOTPOSSIBLE,
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen "Mailbox doesn't allow inferior mailboxes");
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen } else if (mailbox_list_set_error_from_errno(list)) {
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen mailbox_list_set_critical(list, "mkdir_parents(%s) failed: %m",
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainenint mailbox_list_mkdir_parent(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *p;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mailbox_list_mkdir(list, mailbox, t_strdup_until(path, p));
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainenmailbox_list_create_missing_dir(struct mailbox_list *list, const char *name,
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen const char *root_dir, *index_dir, *parent_dir, *p, *error;
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen unsigned int n = 0;
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);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((list->props & MAILBOX_LIST_PROP_AUTOCREATE_DIRS) == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* the directory might not have been created yet */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_list_mkdir_root(list, index_dir, type,
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen "Couldn't create index root dir %s: %s",
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen mailbox_list_get_permissions(list, name, &perm);
6fcaeede31ef2292b3ff59c461eb6ef4ae989dfdTimo Sirainen while (mkdir_chgrp(index_dir, perm.dir_create_mode,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno != ENOENT || p == NULL || ++n == 2) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* create the parent directory first */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_list_mkdir_root(list, parent_dir, type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Couldn't create index dir %s: %s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mailbox_list_create_missing_index_dir(struct mailbox_list *list,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return mailbox_list_create_missing_dir(list, name,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenint mailbox_list_create_missing_index_pvt_dir(struct mailbox_list *list,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen return mailbox_list_create_missing_dir(list, name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenconst char *mailbox_list_get_last_error(struct mailbox_list *list,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen return list->error_string != NULL ? list->error_string :
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Unknown internal list error";
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mailbox_list_clear_error(struct mailbox_list *list)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_set_error(struct mailbox_list *list,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_set_internal_error(struct mailbox_list *list)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen const char *str;
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen str = t_strflocaltime(MAIL_ERRSTR_CRITICAL_MSG_STAMP, ioloop_time);
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenvoid mailbox_list_set_critical(struct mailbox_list *list, const char *fmt, ...)
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. */
68917b81c83e8a4d18a81cda188ffa5cb643c46cTimo Sirainenbool mailbox_list_set_error_from_errno(struct mailbox_list *list)
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (!mail_error_from_errno(&error, &error_string))