mail-storage.c revision f8e4a8eac59e2847b63b57d290fa8223f4a4ce67
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2002-2011 Dovecot authors, see the included COPYING file */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "lib.h"
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen#include "ioloop.h"
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen#include "array.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "llist.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "unichar.h"
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#include "istream.h"
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#include "eacces-error.h"
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#include "mkdir-parents.h"
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#include "var-expand.h"
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#include "mail-index-private.h"
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#include "mail-index-alloc-cache.h"
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen#include "mailbox-list-private.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mail-storage-private.h"
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen#include "mail-storage-settings.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mail-namespace.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mail-search.h"
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen#include "mail-search-register.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mailbox-search-result-private.h"
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen#include "mailbox-guid-cache.h"
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen#include <stdlib.h>
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen#include <ctype.h>
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen#define MAILBOX_DELETE_RETRY_SECS (60*5)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenextern struct mail_search_register *mail_search_register_imap;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenextern struct mail_search_register *mail_search_register_human;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainenstruct mail_storage_module_register mail_storage_module_register = { 0 };
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainenstruct mail_module_register mail_module_register = { 0 };
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainenstruct mail_storage_mail_index_module mail_storage_mail_index_module =
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen MODULE_CONTEXT_INIT(&mail_index_module_register);
5ac0b0bf32898c63da086ae169674ecac151a31eTimo SirainenARRAY_TYPE(mail_storage) mail_storage_classes;
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainenvoid mail_storage_init(void)
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen{
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen mailbox_lists_init();
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen mail_storage_hooks_init();
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_array_init(&mail_storage_classes, 8);
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenvoid mail_storage_deinit(void)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (mail_search_register_human != NULL)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen mail_search_register_deinit(&mail_search_register_human);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (mail_search_register_imap != NULL)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen mail_search_register_deinit(&mail_search_register_imap);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (array_is_created(&mail_storage_classes))
7db932bd4934cd967eeae643300aef5b91caeaeaTimo Sirainen array_free(&mail_storage_classes);
7db932bd4934cd967eeae643300aef5b91caeaeaTimo Sirainen mail_storage_hooks_deinit();
74066569545099304b20e790df7c261883d1746bTimo Sirainen mailbox_lists_deinit();
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenvoid mail_storage_class_register(struct mail_storage *storage_class)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen i_assert(mail_storage_find_class(storage_class->name) == NULL);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* append it after the list, so the autodetection order is correct */
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen array_append(&mail_storage_classes, &storage_class, 1);
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen}
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainenvoid mail_storage_class_unregister(struct mail_storage *storage_class)
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mail_storage *const *classes;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen unsigned int i, count;
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen
0892446b45c195461bb7be6599f02d97e1e2c9b2Timo Sirainen classes = array_get(&mail_storage_classes, &count);
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen for (i = 0; i < count; i++) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (classes[i] == storage_class) {
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen array_delete(&mail_storage_classes, i, 1);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen break;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen }
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen}
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen
0892446b45c195461bb7be6599f02d97e1e2c9b2Timo Sirainenstruct mail_storage *mail_storage_find_class(const char *name)
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen{
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen struct mail_storage *const *classes;
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen unsigned int i, count;
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen i_assert(name != NULL);
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen classes = array_get(&mail_storage_classes, &count);
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen for (i = 0; i < count; i++) {
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen if (strcasecmp(classes[i]->name, name) == 0)
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen return classes[i];
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen }
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen return NULL;
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen}
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainenstatic struct mail_storage *
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainenmail_storage_autodetect(const struct mail_namespace *ns,
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen struct mailbox_list_settings *set)
e4c81823af1fc43ca3f2ce9eb4af7fc8f57b13a5Timo Sirainen{
e4c81823af1fc43ca3f2ce9eb4af7fc8f57b13a5Timo Sirainen struct mail_storage *const *classes;
0892446b45c195461bb7be6599f02d97e1e2c9b2Timo Sirainen unsigned int i, count;
e4c81823af1fc43ca3f2ce9eb4af7fc8f57b13a5Timo Sirainen
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen classes = array_get(&mail_storage_classes, &count);
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen for (i = 0; i < count; i++) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (classes[i]->v.autodetect != NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (classes[i]->v.autodetect(ns, set))
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen return classes[i];
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen }
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen }
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen return NULL;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen}
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainenstatic void
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainenmail_storage_set_autodetection(const char **data, const char **driver)
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen{
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen const char *p;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen /* check if data is in driver:data format (eg. mbox:~/mail) */
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen p = *data;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen while (i_isalnum(*p)) p++;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen if (*p == ':' && p != *data) {
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen /* no autodetection if the storage driver is given. */
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen *driver = t_strdup_until(*data, p);
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen *data = p + 1;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen }
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen}
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainenstatic struct mail_storage *
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainenmail_storage_get_class(struct mail_namespace *ns, const char *driver,
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen struct mailbox_list_settings *list_set,
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen enum mail_storage_flags flags, const char **error_r)
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen{
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen struct mail_storage *storage_class = NULL;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen const char *home;
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen if (driver == NULL) {
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen /* no mail_location, autodetect */
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen } else if (strcmp(driver, "auto") == 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* explicit autodetection with "auto" driver. */
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen if (list_set->root_dir != NULL &&
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *list_set->root_dir == '\0') {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* handle the same as with driver=NULL */
20344c0e814139e3c365fbb9287478f91512089eTimo Sirainen list_set->root_dir = NULL;
20344c0e814139e3c365fbb9287478f91512089eTimo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen } else {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen storage_class = mail_storage_find_class(driver);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (storage_class == NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = t_strdup_printf(
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "Unknown mail storage driver %s", driver);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return NULL;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
20344c0e814139e3c365fbb9287478f91512089eTimo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (list_set->root_dir == NULL || *list_set->root_dir == '\0') {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* no root directory given. is this allowed? */
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen const struct mailbox_list *list;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen list = list_set->layout == NULL ? NULL :
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen mailbox_list_find_class(list_set->layout);
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen if (storage_class == NULL &&
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen (flags & MAIL_STORAGE_FLAG_NO_AUTODETECTION) == 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* autodetection should take care of this */
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen } else if (storage_class != NULL &&
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen (storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_ROOT) != 0) {
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen /* root not required for this storage */
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen } else if (list != NULL &&
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen (list->props & MAILBOX_LIST_PROP_NO_ROOT) != 0) {
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen /* root not required for this layout */
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen } else {
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen *error_r = "Root mail directory not given";
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen return NULL;
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen }
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen }
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen if (storage_class != NULL) {
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen storage_class->v.get_list_settings(ns, list_set);
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen return storage_class;
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen }
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen storage_class = mail_storage_autodetect(ns, list_set);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (storage_class != NULL)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen return storage_class;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen if (ns->set->location == NULL || *ns->set->location == '\0') {
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen (void)mail_user_get_home(ns->user, &home);
601f5f14c6cde28f0e0c6ca7c5d735315d3d48dfTimo Sirainen if (home == NULL || *home == '\0') home = "(not set)";
601f5f14c6cde28f0e0c6ca7c5d735315d3d48dfTimo Sirainen
601f5f14c6cde28f0e0c6ca7c5d735315d3d48dfTimo Sirainen *error_r = t_strdup_printf(
601f5f14c6cde28f0e0c6ca7c5d735315d3d48dfTimo Sirainen "Mail storage autodetection failed with home=%s", home);
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen } else {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = t_strdup_printf(
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "Ambiguous mail location setting, "
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen "don't know what to do with it: %s "
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen "(try prefixing it with mbox: or maildir:)",
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen ns->set->location);
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen }
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen return NULL;
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen}
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainenstatic int
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainenmail_storage_verify_root(const char *root_dir, bool autocreate,
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen const char **error_r)
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen{
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen struct stat st;
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (stat(root_dir, &st) == 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* exists */
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return 1;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen } else if (errno == EACCES) {
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen *error_r = mail_error_eacces_msg("stat", root_dir);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return -1;
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen } else if (errno != ENOENT && errno != ENOTDIR) {
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen *error_r = t_strdup_printf("stat(%s) failed: %m", root_dir);
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen return -1;
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen } else if (!autocreate) {
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen *error_r = t_strdup_printf(
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen "Root mail directory doesn't exist: %s", root_dir);
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return -1;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen } else {
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen /* doesn't exist */
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen return 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen}
548e394330621952db0f03dd667b70184c4a37b6Timo Sirainen
548e394330621952db0f03dd667b70184c4a37b6Timo Sirainenstatic int
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainenmail_storage_create_root(struct mailbox_list *list,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mail_storage_flags flags, const char **error_r)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen const char *root_dir, *origin, *error;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen mode_t file_mode, dir_mode;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen gid_t gid;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen bool autocreate;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen int ret;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen root_dir = mailbox_list_get_path(list, NULL,
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen MAILBOX_LIST_PATH_TYPE_MAILBOX);
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen if (root_dir == NULL) {
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen /* storage doesn't use directories (e.g. shared root) */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen return 0;
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen }
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen if ((flags & MAIL_STORAGE_FLAG_NO_AUTOVERIFY) != 0) {
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen if (!list->mail_set->mail_debug)
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen return 0;
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen /* we don't need to verify, but since debugging is
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen enabled, check and log if the root doesn't exist */
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen if (mail_storage_verify_root(root_dir, FALSE, &error) < 0) {
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen i_debug("Namespace %s: Creating storage despite: %s",
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen list->ns->prefix, error);
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen }
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen return 0;
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen }
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen autocreate = (flags & MAIL_STORAGE_FLAG_NO_AUTOCREATE) == 0;
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen ret = mail_storage_verify_root(root_dir, autocreate, error_r);
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen if (ret != 0)
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen return ret;
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen /* we need to create the root directory. */
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen mailbox_list_get_root_permissions(list, &file_mode, &dir_mode,
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen &gid, &origin);
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen if (mkdir_parents_chgrp(root_dir, dir_mode, gid, origin) < 0 &&
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen errno != EEXIST) {
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen *error_r = mail_error_create_eacces_msg("mkdir", root_dir);
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen return -1;
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen } else {
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen /* created */
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen return 0;
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen }
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen}
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainenstatic bool
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainenmail_storage_match_class(struct mail_storage *storage,
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen const struct mail_storage *storage_class,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen const struct mailbox_list_settings *set)
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen{
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen if (strcmp(storage->name, storage_class->name) != 0)
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen return FALSE;
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen if ((storage->class_flags & MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT) != 0 &&
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen strcmp(storage->unique_root_dir, set->root_dir) != 0)
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen return FALSE;
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen if (strcmp(storage->name, "shared") == 0) {
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen /* allow multiple independent shared namespaces */
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen return FALSE;
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen }
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen return storage;
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen}
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainenstatic struct mail_storage *
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainenmail_storage_find(struct mail_user *user,
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen const struct mail_storage *storage_class,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen const struct mailbox_list_settings *set)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen struct mail_storage *storage = user->storages;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen for (; storage != NULL; storage = storage->next) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (mail_storage_match_class(storage, storage_class, set))
0611067f385a37773800225256dcd5cf6aa34212Timo Sirainen return storage;
0611067f385a37773800225256dcd5cf6aa34212Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return NULL;
0611067f385a37773800225256dcd5cf6aa34212Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainenint mail_storage_create(struct mail_namespace *ns, const char *driver,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen enum mail_storage_flags flags, const char **error_r)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct mail_storage *storage_class, *storage = NULL;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct mailbox_list *list;
5254d77805cd35b9356d072ba325c356c43b0d51Timo Sirainen struct mailbox_list_settings list_set;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen enum mailbox_list_flags list_flags = 0;
3e25b17126e9536736d5da03697613e4c3af5f76Timo Sirainen const char *data = ns->set->location;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen const char *p;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen if ((flags & MAIL_STORAGE_FLAG_KEEP_HEADER_MD5) == 0 &&
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->mail_set->pop3_uidl_format != NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* if pop3_uidl_format contains %m, we want to keep the
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen header MD5 sums stored even if we're not running POP3
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen right now. */
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen p = ns->mail_set->pop3_uidl_format;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen while ((p = strchr(p, '%')) != NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (p[1] == '%')
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen p += 2;
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen else if (var_get_key(++p) == 'm') {
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen flags |= MAIL_STORAGE_FLAG_KEEP_HEADER_MD5;
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen break;
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen }
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen }
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen }
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen memset(&list_set, 0, sizeof(list_set));
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen if (data == NULL) {
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen /* autodetect */
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen } else if (driver != NULL && strcmp(driver, "shared") == 0) {
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen /* internal shared namespace */
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen list_set.root_dir = ns->user->set->base_dir;
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen } else {
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen if (driver == NULL)
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen mail_storage_set_autodetection(&data, &driver);
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen if (mailbox_list_settings_parse(ns->user, data, &list_set,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen error_r) < 0)
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen return -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen storage_class = mail_storage_get_class(ns, driver, &list_set, flags,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen error_r);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (storage_class == NULL)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -1;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen i_assert(list_set.layout != NULL);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->list == NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* first storage for namespace */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen if (mail_storage_is_mailbox_file(storage_class))
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen list_flags |= MAILBOX_LIST_FLAG_MAILBOX_FILES;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (mailbox_list_create(list_set.layout, ns, &list_set,
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen list_flags, &list, error_r) < 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = t_strdup_printf("Mailbox list driver %s: %s",
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen list_set.layout, *error_r);
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen return -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (mail_storage_create_root(ns->list, flags, error_r) < 0)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -1;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen storage = mail_storage_find(ns->user, storage_class, &list_set);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (storage != NULL) {
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen /* using an existing storage */
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen storage->refcount++;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mail_namespace_add_storage(ns, storage);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return 0;
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
37ab3cde96bfa4bc5304c0c348fc420aec79572dTimo Sirainen storage = storage_class->v.alloc();
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen storage->refcount = 1;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen storage->storage_class = storage_class;
37ab3cde96bfa4bc5304c0c348fc420aec79572dTimo Sirainen storage->user = ns->user;
37ab3cde96bfa4bc5304c0c348fc420aec79572dTimo Sirainen storage->set = ns->mail_set;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen storage->flags = flags;
f4bbeadda12fbd7c219063db68f3e78646d83c2cTimo Sirainen p_array_init(&storage->module_contexts, storage->pool, 5);
0b47e9f5e0181053b4d9ca7b426b0e5c185e820eTimo Sirainen
0b47e9f5e0181053b4d9ca7b426b0e5c185e820eTimo Sirainen if (storage->v.create != NULL &&
abe8754852e70763e92f74caabbcc13d0917714cTimo Sirainen storage->v.create(storage, ns, error_r) < 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = t_strdup_printf("%s: %s", storage->name, *error_r);
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen pool_unref(&storage->pool);
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen return -1;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen }
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen
90b8f131849540fa374aede95edd86d47d35c09dTimo Sirainen T_BEGIN {
90b8f131849540fa374aede95edd86d47d35c09dTimo Sirainen hook_mail_storage_created(storage);
90b8f131849540fa374aede95edd86d47d35c09dTimo Sirainen } T_END;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen DLLIST_PREPEND(&ns->user->storages, storage);
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen mail_namespace_add_storage(ns, storage);
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen return 0;
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen}
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainenvoid mail_storage_unref(struct mail_storage **_storage)
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen{
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen struct mail_storage *storage = *_storage;
90b8f131849540fa374aede95edd86d47d35c09dTimo Sirainen
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen i_assert(storage->refcount > 0);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* set *_storage=NULL only after calling destroy() callback.
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen for example mdbox wants to access ns->storage */
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen if (--storage->refcount > 0) {
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen *_storage = NULL;
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen return;
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen }
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen if (storage->obj_refcount != 0)
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen i_panic("Trying to deinit storage before freeing its objects");
904f9d5654b9c39edcdf32883e5e88771faf4d69Timo Sirainen
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen DLLIST_REMOVE(&storage->user->storages, storage);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen if (storage->v.destroy != NULL)
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen storage->v.destroy(storage);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen i_free(storage->error_string);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen *_storage = NULL;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen pool_unref(&storage->pool);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen mail_index_alloc_cache_destroy_unrefed();
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen}
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainenvoid mail_storage_obj_ref(struct mail_storage *storage)
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen{
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen i_assert(storage->refcount > 0);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen if (storage->obj_refcount++ == 0)
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen mail_user_ref(storage->user);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen}
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
14175321ddb88619015866978c05a27786ca4814Timo Sirainenvoid mail_storage_obj_unref(struct mail_storage *storage)
14175321ddb88619015866978c05a27786ca4814Timo Sirainen{
14175321ddb88619015866978c05a27786ca4814Timo Sirainen i_assert(storage->refcount > 0);
14175321ddb88619015866978c05a27786ca4814Timo Sirainen i_assert(storage->obj_refcount > 0);
14175321ddb88619015866978c05a27786ca4814Timo Sirainen
14175321ddb88619015866978c05a27786ca4814Timo Sirainen if (--storage->obj_refcount == 0) {
14175321ddb88619015866978c05a27786ca4814Timo Sirainen struct mail_user *user = storage->user;
14175321ddb88619015866978c05a27786ca4814Timo Sirainen mail_user_unref(&user);
14175321ddb88619015866978c05a27786ca4814Timo Sirainen }
14175321ddb88619015866978c05a27786ca4814Timo Sirainen}
14175321ddb88619015866978c05a27786ca4814Timo Sirainen
14175321ddb88619015866978c05a27786ca4814Timo Sirainenvoid mail_storage_clear_error(struct mail_storage *storage)
14175321ddb88619015866978c05a27786ca4814Timo Sirainen{
14175321ddb88619015866978c05a27786ca4814Timo Sirainen i_free_and_null(storage->error_string);
14175321ddb88619015866978c05a27786ca4814Timo Sirainen
14175321ddb88619015866978c05a27786ca4814Timo Sirainen storage->error = MAIL_ERROR_NONE;
14175321ddb88619015866978c05a27786ca4814Timo Sirainen}
14175321ddb88619015866978c05a27786ca4814Timo Sirainen
14175321ddb88619015866978c05a27786ca4814Timo Sirainenvoid mail_storage_set_error(struct mail_storage *storage,
14175321ddb88619015866978c05a27786ca4814Timo Sirainen enum mail_error error, const char *string)
14175321ddb88619015866978c05a27786ca4814Timo Sirainen{
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen i_free(storage->error_string);
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen storage->error_string = i_strdup(string);
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen storage->error = error;
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen}
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainenvoid mail_storage_set_internal_error(struct mail_storage *storage)
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen{
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen struct tm *tm;
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen char str[256];
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen tm = localtime(&ioloop_time);
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen i_free(storage->error_string);
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen storage->error_string =
282a436a74d8835edb45cc019b1c916013013fd3Timo Sirainen strftime(str, sizeof(str),
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen MAIL_ERRSTR_CRITICAL_MSG_STAMP, tm) > 0 ?
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen i_strdup(str) : i_strdup(MAIL_ERRSTR_CRITICAL_MSG);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen storage->error = MAIL_ERROR_TEMP;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen}
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainenvoid mail_storage_set_critical(struct mail_storage *storage,
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen const char *fmt, ...)
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen{
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen va_list va;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen va_start(va, fmt);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen i_error("%s", t_strdup_vprintf(fmt, va));
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen va_end(va);
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen /* critical errors may contain sensitive data, so let user
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen see only "Internal error" with a timestamp to make it
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen easier to look from log files the actual error message. */
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen mail_storage_set_internal_error(storage);
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen}
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainenvoid mail_storage_copy_list_error(struct mail_storage *storage,
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen struct mailbox_list *list)
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen{
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen const char *str;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen enum mail_error error;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen str = mailbox_list_get_last_error(list, &error);
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen mail_storage_set_error(storage, error, str);
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen}
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainenvoid mail_storage_set_index_error(struct mailbox *box)
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen{
9511a40d933181045343110c8101b75887062aaeTimo Sirainen if (mail_index_is_deleted(box->index))
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mailbox_set_deleted(box);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen else
9511a40d933181045343110c8101b75887062aaeTimo Sirainen mail_storage_set_internal_error(box->storage);
9511a40d933181045343110c8101b75887062aaeTimo Sirainen mail_index_reset_error(box->index);
9511a40d933181045343110c8101b75887062aaeTimo Sirainen}
9511a40d933181045343110c8101b75887062aaeTimo Sirainen
9511a40d933181045343110c8101b75887062aaeTimo Sirainenconst struct mail_storage_settings *
9511a40d933181045343110c8101b75887062aaeTimo Sirainenmail_storage_get_settings(struct mail_storage *storage)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return storage->set;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen}
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainenstruct mail_user *mail_storage_get_user(struct mail_storage *storage)
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainen{
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen return storage->user;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen}
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenvoid mail_storage_set_callbacks(struct mail_storage *storage,
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen struct mail_storage_callbacks *callbacks,
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen void *context)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen{
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen storage->callbacks = *callbacks;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen storage->callback_context = context;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen}
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainenint mail_storage_purge(struct mail_storage *storage)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return storage->v.purge == NULL ? 0 :
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen storage->v.purge(storage);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenconst char *mail_storage_get_last_error(struct mail_storage *storage,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mail_error *error_r)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* We get here only in error situations, so we have to return some
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen error. If storage->error is NONE, it means we forgot to set it at
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen some point.. */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (storage->error == MAIL_ERROR_NONE) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (error_r != NULL)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen *error_r = MAIL_ERROR_TEMP;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return storage->error_string != NULL ? storage->error_string :
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen "BUG: Unknown internal error";
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (storage->error_string == NULL) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* This shouldn't happen.. */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen storage->error_string =
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen i_strdup_printf("BUG: Unknown 0x%x error",
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen storage->error);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (error_r != NULL)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen *error_r = storage->error;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return storage->error_string;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenconst char *mailbox_get_last_error(struct mailbox *box,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mail_error *error_r)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return mail_storage_get_last_error(box->storage, error_r);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenenum mail_error mailbox_get_last_mail_error(struct mailbox *box)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mail_error error;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen (void)mail_storage_get_last_error(box->storage, &error);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return error;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenbool mail_storage_is_mailbox_file(struct mail_storage *storage)
2526d52441ef368215ab6bf04fd0356d3b09d235Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return (storage->class_flags &
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen MAIL_STORAGE_CLASS_FLAG_MAILBOX_IS_FILE) != 0;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenbool mail_storage_set_error_from_errno(struct mail_storage *storage)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen const char *error_string;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mail_error error;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (!mail_error_from_errno(&error, &error_string))
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return FALSE;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (storage->set->mail_debug && error != MAIL_ERROR_NOTFOUND) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* debugging is enabled - admin may be debugging a
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen (permission) problem, so return FALSE to get the caller to
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen log the full error message. */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return FALSE;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen mail_storage_set_error(storage, error, error_string);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return TRUE;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
2526d52441ef368215ab6bf04fd0356d3b09d235Timo Sirainenstruct mailbox *mailbox_alloc(struct mailbox_list *list, const char *vname,
2526d52441ef368215ab6bf04fd0356d3b09d235Timo Sirainen enum mailbox_flags flags)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen struct mailbox_list *new_list = list;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen struct mail_storage *storage;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen struct mailbox *box;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen i_assert(uni_utf8_str_is_valid(vname));
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if ((list->ns->flags & NAMESPACE_FLAG_INBOX_USER) != 0 &&
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen strncasecmp(vname, "INBOX", 5) == 0 &&
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen strncmp(vname, "INBOX", 5) != 0) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* make sure INBOX shows up in uppercase everywhere */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (vname[5] == '\0')
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen vname = "INBOX";
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen else if (vname[5] == mail_namespace_get_sep(list->ns))
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen vname = t_strconcat("INBOX", vname + 5, NULL);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (mailbox_list_get_storage(&new_list, vname, &storage) < 0) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* just use the first storage. FIXME: does this break? */
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen storage = list->ns->storage;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen T_BEGIN {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen box = storage->v.mailbox_alloc(storage, new_list, vname, flags);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen hook_mailbox_allocated(box);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen } T_END;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen mail_storage_obj_ref(box->storage);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return box;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen}
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenstruct mailbox *mailbox_alloc_guid(struct mailbox_list *list,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen const guid_128_t guid,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mailbox_flags flags)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen struct mailbox *box = NULL;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen struct mailbox_metadata metadata;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen enum mail_error open_error = MAIL_ERROR_TEMP;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen const char *vname;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (mailbox_guid_cache_find(list, guid, &vname) < 0) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen vname = NULL;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen } else if (vname != NULL) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen box = mailbox_alloc(list, vname, flags);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen &metadata) < 0) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen } else if (memcmp(metadata.guid, guid,
sizeof(metadata.guid)) != 0) {
/* GUID mismatch, refresh cache and try again */
mailbox_free(&box);
mailbox_guid_cache_refresh(list);
return mailbox_alloc_guid(list, guid, flags);
} else {
/* successfully opened the correct mailbox */
return box;
}
i_error("mailbox_alloc_guid(%s): "
"Couldn't verify mailbox GUID: %s",
guid_128_to_string(guid),
mailbox_get_last_error(box, NULL));
vname = NULL;
mailbox_free(&box);
} else {
vname = t_strdup_printf("(nonexistent mailbox with GUID=%s)",
guid_128_to_string(guid));
open_error = MAIL_ERROR_NOTFOUND;
}
if (vname == NULL) {
vname = t_strdup_printf("(error in mailbox with GUID=%s)",
guid_128_to_string(guid));
}
box = mailbox_alloc(list, vname, flags);
box->open_error = open_error;
return box;
}
static bool have_listable_namespace_prefix(struct mail_namespace *ns,
const char *name)
{
unsigned int name_len = strlen(name);
for (; ns != NULL; ns = ns->next) {
if ((ns->flags & (NAMESPACE_FLAG_LIST_PREFIX |
NAMESPACE_FLAG_LIST_CHILDREN)) == 0)
continue;
if (ns->prefix_len <= name_len)
continue;
/* if prefix has multiple hierarchies, match
any of the hierarchies */
if (strncmp(ns->prefix, name, name_len) == 0 &&
ns->prefix[name_len] == mail_namespace_get_sep(ns))
return TRUE;
}
return FALSE;
}
int mailbox_exists(struct mailbox *box, bool auto_boxes,
enum mailbox_existence *existence_r)
{
switch (box->open_error) {
case 0:
break;
case MAIL_ERROR_NOTFOUND:
*existence_r = MAILBOX_EXISTENCE_NONE;
return 0;
default:
/* unsure if this exists or not */
return -1;
}
if (!mailbox_list_is_valid_existing_name(box->list, box->name)) {
/* report it as not selectable, since it exists but we won't
let it be opened. */
*existence_r = MAILBOX_EXISTENCE_NOSELECT;
return 0;
}
if (!box->inbox_user &&
have_listable_namespace_prefix(box->storage->user->namespaces,
box->vname)) {
/* listable namespace prefix always exists */
*existence_r = MAILBOX_EXISTENCE_NOSELECT;
return 0;
}
if (strcmp(box->name, "INBOX") == 0 && box->inbox_user && auto_boxes) {
/* INBOX always exists */
*existence_r = MAILBOX_EXISTENCE_SELECT;
return 0;
}
return box->v.exists(box, auto_boxes, existence_r);
}
static int mailbox_check_mismatching_separators(struct mailbox *box)
{
struct mail_namespace *ns = box->list->ns;
const char *p, *vname = box->vname;
char list_sep, ns_sep;
if (box->inbox_user) {
/* this is INBOX - don't bother with further checks */
return 0;
}
list_sep = mailbox_list_get_hierarchy_sep(box->list);
ns_sep = mail_namespace_get_sep(ns);
if (ns->prefix_len > 0) {
/* vname is either "namespace/box" or "namespace" */
i_assert(strncmp(vname, ns->prefix, ns->prefix_len-1) == 0);
vname += ns->prefix_len - 1;
if (vname[0] != '\0') {
i_assert(vname[0] == ns->prefix[ns->prefix_len-1]);
vname++;
if (vname[0] == '\0') {
/* "namespace/" isn't a valid mailbox name. */
mail_storage_set_error(box->storage,
MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
}
}
if (ns_sep == list_sep || box->list->set.escape_char != '\0')
return 0;
for (p = vname; *p != '\0'; p++) {
if (*p == list_sep) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
t_strdup_printf("Character not allowed "
"in mailbox name: '%c'",
list_sep));
return -1;
}
}
return 0;
}
static int mailbox_open_full(struct mailbox *box, struct istream *input)
{
int ret;
if (box->opened)
return 0;
switch (box->open_error) {
case 0:
break;
case MAIL_ERROR_NOTFOUND:
mail_storage_set_error(box->storage, MAIL_ERROR_NOTFOUND,
T_MAIL_ERR_MAILBOX_NOT_FOUND(box->vname));
return -1;
default:
mail_storage_set_internal_error(box->storage);
box->storage->error = box->open_error;
return -1;
}
if (mailbox_check_mismatching_separators(box) < 0)
return -1;
if (!mailbox_list_is_valid_existing_name(box->list, box->name)) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
if (input != NULL) {
if ((box->storage->class_flags &
MAIL_STORAGE_CLASS_FLAG_OPEN_STREAMS) == 0) {
mail_storage_set_critical(box->storage,
"Storage doesn't support streamed mailboxes");
return -1;
}
box->input = input;
box->flags |= MAILBOX_FLAG_READONLY;
i_stream_ref(box->input);
}
T_BEGIN {
ret = box->v.open(box);
} T_END;
if (ret < 0 && box->storage->error == MAIL_ERROR_NOTFOUND &&
box->input == NULL && box->inbox_user) T_BEGIN {
/* INBOX should always exist. try to create it and retry. */
(void)mailbox_create(box, NULL, FALSE);
mailbox_close(box);
ret = box->v.open(box);
if (ret < 0 && !box->storage->user->inbox_open_error_logged) {
box->storage->user->inbox_open_error_logged = TRUE;
i_error("Opening INBOX failed: %s",
mailbox_get_last_error(box, NULL));
}
} T_END;
if (ret < 0) {
if (box->input != NULL)
i_stream_unref(&box->input);
return -1;
}
box->list->ns->flags |= NAMESPACE_FLAG_USABLE;
return 0;
}
int mailbox_open(struct mailbox *box)
{
return mailbox_open_full(box, NULL);
}
int mailbox_open_stream(struct mailbox *box, struct istream *input)
{
return mailbox_open_full(box, input);
}
int mailbox_enable(struct mailbox *box, enum mailbox_feature features)
{
return box->v.enable(box, features);
}
enum mailbox_feature mailbox_get_enabled_features(struct mailbox *box)
{
return box->enabled_features;
}
void mailbox_close(struct mailbox *box)
{
if (!box->opened)
return;
if (box->transaction_count != 0) {
i_panic("Trying to close mailbox %s with open transactions",
box->name);
}
box->v.close(box);
box->opened = FALSE;
box->mailbox_deleted = FALSE;
array_clear(&box->search_results);
}
void mailbox_free(struct mailbox **_box)
{
struct mailbox *box = *_box;
*_box = NULL;
mailbox_close(box);
box->v.free(box);
mail_storage_obj_unref(box->storage);
pool_unref(&box->pool);
}
int mailbox_create(struct mailbox *box, const struct mailbox_update *update,
bool directory)
{
enum mailbox_dir_create_type type;
int ret;
if (!mailbox_list_is_valid_create_name(box->list, box->name)) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
type = directory ? MAILBOX_DIR_CREATE_TYPE_TRY_NOSELECT :
MAILBOX_DIR_CREATE_TYPE_MAILBOX;
if (box->list->v.create_mailbox_dir(box->list, box->name, type) < 0) {
mail_storage_copy_list_error(box->storage, box->list);
if (directory ||
mailbox_get_last_mail_error(box) != MAIL_ERROR_EXISTS)
return -1;
/* the directory already exists, but the mailbox might not */
}
mailbox_refresh_permissions(box);
box->creating = TRUE;
ret = box->v.create(box, update, directory);
box->creating = FALSE;
return ret;
}
int mailbox_update(struct mailbox *box, const struct mailbox_update *update)
{
i_assert(update->min_next_uid == 0 ||
update->min_first_recent_uid == 0 ||
update->min_first_recent_uid <= update->min_next_uid);
return box->v.update(box, update);
}
int mailbox_mark_index_deleted(struct mailbox *box, bool del)
{
struct mail_index_transaction *trans;
enum mail_index_transaction_flags trans_flags = 0;
enum mailbox_flags old_flag;
int ret;
if (box->marked_deleted && del) {
/* we already marked it deleted. this allows plugins to
"lock" the deletion earlier. */
return 0;
}
old_flag = box->flags & MAILBOX_FLAG_OPEN_DELETED;
box->flags |= MAILBOX_FLAG_OPEN_DELETED;
ret = mailbox_open(box);
box->flags = (box->flags & ~MAILBOX_FLAG_OPEN_DELETED) | old_flag;
if (ret < 0)
return -1;
trans_flags = del ? 0 : MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL;
trans = mail_index_transaction_begin(box->view, trans_flags);
if (del)
mail_index_set_deleted(trans);
else
mail_index_set_undeleted(trans);
if (mail_index_transaction_commit(&trans) < 0) {
mail_storage_set_index_error(box);
return -1;
}
/* sync the mailbox. this finishes the index deletion and it can
succeed only for a single session. we do it here, so the rest of
the deletion code doesn't have to worry about race conditions. */
if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FULL_READ) < 0)
return -1;
box->marked_deleted = del;
return 0;
}
static bool mailbox_try_undelete(struct mailbox *box)
{
time_t mtime;
if (mail_index_get_modification_time(box->index, &mtime) < 0)
return FALSE;
if (mtime + MAILBOX_DELETE_RETRY_SECS > time(NULL))
return FALSE;
if (mailbox_mark_index_deleted(box, FALSE) < 0)
return FALSE;
box->mailbox_deleted = FALSE;
return TRUE;
}
int mailbox_delete(struct mailbox *box)
{
int ret;
if (*box->name == '\0') {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Storage root can't be deleted");
return -1;
}
if (box->inbox_any) {
mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
"INBOX can't be deleted.");
return -1;
}
box->deleting = TRUE;
if (mailbox_open(box) < 0) {
if (mailbox_get_last_mail_error(box) != MAIL_ERROR_NOTFOUND)
return -1;
if (!box->mailbox_deleted) {
/* \noselect mailbox */
} else {
/* if deletion happened a long time ago, it means it
crashed while doing it. undelete the mailbox in
that case. */
if (!mailbox_try_undelete(box))
return -1;
/* retry */
if (mailbox_open(box) < 0)
return -1;
}
}
ret = box->v.delete(box);
if (ret < 0 && box->marked_deleted) {
/* deletion failed. revert the mark so it can maybe be
tried again later. */
if (mailbox_mark_index_deleted(box, FALSE) < 0)
return -1;
}
box->deleting = FALSE;
mailbox_close(box);
return ret;
}
static bool
mail_storages_rename_compatible(struct mail_storage *storage1,
struct mail_storage *storage2,
const char **error_r)
{
if (storage1 == storage2)
return TRUE;
if (strcmp(storage1->name, storage2->name) != 0) {
*error_r = t_strdup_printf("storage %s != %s",
storage1->name, storage2->name);
return FALSE;
}
if ((storage1->class_flags & MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT) != 0) {
/* e.g. mdbox where all mails are in storage/ directory and
they can't be easily moved from there. */
*error_r = t_strdup_printf("storage %s uses unique root",
storage1->name);
return FALSE;
}
return TRUE;
}
static bool nullequals(const void *p1, const void *p2)
{
return (p1 == NULL && p2 == NULL) || (p1 != NULL && p2 != NULL);
}
static bool
mailbox_lists_rename_compatible(struct mailbox_list *list1,
struct mailbox_list *list2,
const char **error_r)
{
if (!nullequals(list1->set.alt_dir, list2->set.alt_dir)) {
*error_r = "one namespace has alt dir and another doesn't";
return FALSE;
}
if (!nullequals(list1->set.index_dir, list2->set.index_dir)) {
*error_r = "one namespace has index dir and another doesn't";
return FALSE;
}
if (!nullequals(list1->set.control_dir, list2->set.control_dir)) {
*error_r = "one namespace has control dir and another doesn't";
return FALSE;
}
return TRUE;
}
int mailbox_rename(struct mailbox *src, struct mailbox *dest,
bool rename_children)
{
const char *error = NULL;
if (!mailbox_list_is_valid_existing_name(src->list, src->name) ||
*src->name == '\0' ||
!mailbox_list_is_valid_create_name(dest->list, dest->name)) {
mail_storage_set_error(src->storage, MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
if (!mail_storages_rename_compatible(src->storage,
dest->storage, &error) ||
!mailbox_lists_rename_compatible(src->list,
dest->list, &error)) {
if (src->storage->set->mail_debug) {
i_debug("Can't rename '%s' to '%s': %s",
src->vname, dest->vname, error);
}
mail_storage_set_error(src->storage, MAIL_ERROR_NOTPOSSIBLE,
"Can't rename mailboxes across specified storages.");
return -1;
}
if (src->list != dest->list &&
(src->list->ns->type != NAMESPACE_PRIVATE ||
dest->list->ns->type != NAMESPACE_PRIVATE)) {
mail_storage_set_error(src->storage, MAIL_ERROR_NOTPOSSIBLE,
"Renaming not supported across non-private namespaces.");
return -1;
}
return src->v.rename(src, dest, rename_children);
}
int mailbox_set_subscribed(struct mailbox *box, bool set)
{
struct mail_namespace *ns;
struct mailbox_list *list = box->list;
const char *subs_name;
if (!mailbox_list_is_valid_existing_name(list, box->name)) {
mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS,
"Invalid mailbox name");
return -1;
}
if ((list->ns->flags & NAMESPACE_FLAG_SUBSCRIPTIONS) != 0)
subs_name = box->name;
else {
/* subscriptions=no namespace, find another one where we can
add the subscription to */
ns = mail_namespace_find_subscribable(list->ns->user->namespaces,
box->vname);
if (ns == NULL) {
mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
"This namespace has no subscriptions");
return -1;
}
/* use <orig ns prefix><orig storage name> as the
subscription name */
subs_name = t_strconcat(list->ns->prefix, box->name, NULL);
/* drop the common prefix (typically there isn't one) */
i_assert(strncmp(ns->prefix, subs_name, strlen(ns->prefix)) == 0);
subs_name += strlen(ns->prefix);
list = ns->list;
}
return mailbox_list_set_subscribed(list, subs_name, set);
}
struct mail_storage *mailbox_get_storage(const struct mailbox *box)
{
return box->storage;
}
struct mail_namespace *
mailbox_get_namespace(const struct mailbox *box)
{
return box->list->ns;
}
const struct mail_storage_settings *mailbox_get_settings(struct mailbox *box)
{
return box->storage->set;
}
const char *mailbox_get_name(const struct mailbox *box)
{
return box->name;
}
const char *mailbox_get_vname(const struct mailbox *box)
{
return box->vname;
}
bool mailbox_is_readonly(struct mailbox *box)
{
return box->v.is_readonly(box);
}
bool mailbox_backends_equal(const struct mailbox *box1,
const struct mailbox *box2)
{
struct mail_namespace *ns1 = box1->list->ns, *ns2 = box2->list->ns;
if (strcmp(box1->name, box2->name) != 0)
return FALSE;
while (ns1->alias_for != NULL)
ns1 = ns1->alias_for;
while (ns2->alias_for != NULL)
ns2 = ns2->alias_for;
return ns1 == ns2;
}
int mailbox_get_status(struct mailbox *box,
enum mailbox_status_items items,
struct mailbox_status *status_r)
{
memset(status_r, 0, sizeof(*status_r));
return box->v.get_status(box, items, status_r);
}
void mailbox_get_open_status(struct mailbox *box,
enum mailbox_status_items items,
struct mailbox_status *status_r)
{
i_assert(box->opened);
memset(status_r, 0, sizeof(*status_r));
if (box->v.get_status(box, items, status_r) < 0)
i_unreached();
}
int mailbox_get_metadata(struct mailbox *box, enum mailbox_metadata_items items,
struct mailbox_metadata *metadata_r)
{
memset(metadata_r, 0, sizeof(*metadata_r));
if (box->v.get_metadata(box, items, metadata_r) < 0)
return -1;
i_assert((items & MAILBOX_METADATA_GUID) == 0 ||
!guid_128_is_empty(metadata_r->guid));
return 0;
}
enum mail_flags mailbox_get_private_flags_mask(struct mailbox *box)
{
if (box->v.get_private_flags_mask == NULL)
return 0;
else
return box->v.get_private_flags_mask(box);
}
struct mailbox_sync_context *
mailbox_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
{
struct mailbox_sync_context *ctx;
if (box->transaction_count != 0) {
i_panic("Trying to sync mailbox %s with open transactions",
box->name);
}
T_BEGIN {
ctx = box->v.sync_init(box, flags);
} T_END;
return ctx;
}
bool mailbox_sync_next(struct mailbox_sync_context *ctx,
struct mailbox_sync_rec *sync_rec_r)
{
return ctx->box->v.sync_next(ctx, sync_rec_r);
}
int mailbox_sync_deinit(struct mailbox_sync_context **_ctx,
struct mailbox_sync_status *status_r)
{
struct mailbox_sync_context *ctx = *_ctx;
struct mailbox *box = ctx->box;
const char *errormsg;
enum mail_error error;
int ret;
*_ctx = NULL;
memset(status_r, 0, sizeof(*status_r));
ret = box->v.sync_deinit(ctx, status_r);
if (ret < 0 && box->inbox_user &&
!box->storage->user->inbox_open_error_logged) {
errormsg = mailbox_get_last_error(box, &error);
if (error == MAIL_ERROR_NOTPOSSIBLE) {
box->storage->user->inbox_open_error_logged = TRUE;
i_error("Syncing INBOX failed: %s", errormsg);
}
}
return ret;
}
int mailbox_sync(struct mailbox *box, enum mailbox_sync_flags flags)
{
struct mailbox_sync_context *ctx;
struct mailbox_sync_status status;
if (array_count(&box->search_results) == 0) {
/* we don't care about mailbox's current state, so we might
as well fix inconsistency state */
flags |= MAILBOX_SYNC_FLAG_FIX_INCONSISTENT;
}
ctx = mailbox_sync_init(box, flags);
return mailbox_sync_deinit(&ctx, &status);
}
#undef mailbox_notify_changes
void mailbox_notify_changes(struct mailbox *box, unsigned int min_interval,
mailbox_notify_callback_t *callback, void *context)
{
box->notify_min_interval = min_interval;
box->notify_callback = callback;
box->notify_context = context;
box->v.notify_changes(box);
}
void mailbox_notify_changes_stop(struct mailbox *box)
{
mailbox_notify_changes(box, 0, NULL, NULL);
}
struct mail_search_context *
mailbox_search_init(struct mailbox_transaction_context *t,
struct mail_search_args *args,
const enum mail_sort_type *sort_program,
enum mail_fetch_field wanted_fields,
struct mailbox_header_lookup_ctx *wanted_headers)
{
mail_search_args_ref(args);
if (!args->simplified)
mail_search_args_simplify(args);
return t->box->v.search_init(t, args, sort_program,
wanted_fields, wanted_headers);
}
int mailbox_search_deinit(struct mail_search_context **_ctx)
{
struct mail_search_context *ctx = *_ctx;
struct mail_search_args *args = ctx->args;
int ret;
*_ctx = NULL;
mailbox_search_results_initial_done(ctx);
ret = ctx->transaction->box->v.search_deinit(ctx);
mail_search_args_unref(&args);
return ret;
}
bool mailbox_search_next(struct mail_search_context *ctx, struct mail **mail_r)
{
bool tryagain;
while (!mailbox_search_next_nonblock(ctx, mail_r, &tryagain)) {
if (!tryagain)
return FALSE;
}
return TRUE;
}
bool mailbox_search_next_nonblock(struct mail_search_context *ctx,
struct mail **mail_r, bool *tryagain_r)
{
struct mailbox *box = ctx->transaction->box;
*mail_r = NULL;
if (!box->v.search_next_nonblock(ctx, mail_r, tryagain_r))
return FALSE;
else {
mailbox_search_results_add(ctx, (*mail_r)->uid);
return TRUE;
}
}
bool mailbox_search_seen_lost_data(struct mail_search_context *ctx)
{
return ctx->seen_lost_data;
}
int mailbox_search_result_build(struct mailbox_transaction_context *t,
struct mail_search_args *args,
enum mailbox_search_result_flags flags,
struct mail_search_result **result_r)
{
struct mail_search_context *ctx;
struct mail *mail;
int ret;
ctx = mailbox_search_init(t, args, NULL, 0, NULL);
*result_r = mailbox_search_result_save(ctx, flags);
while (mailbox_search_next(ctx, &mail)) ;
ret = mailbox_search_deinit(&ctx);
if (ret < 0)
mailbox_search_result_free(result_r);
return ret;
}
struct mailbox_transaction_context *
mailbox_transaction_begin(struct mailbox *box,
enum mailbox_transaction_flags flags)
{
struct mailbox_transaction_context *trans;
box->transaction_count++;
trans = box->v.transaction_begin(box, flags);
trans->flags = flags;
return trans;
}
int mailbox_transaction_commit(struct mailbox_transaction_context **t)
{
struct mail_transaction_commit_changes changes;
int ret;
/* Store changes temporarily so that plugins overriding
transaction_commit() can look at them. */
changes.pool = NULL;
ret = mailbox_transaction_commit_get_changes(t, &changes);
if (changes.pool != NULL)
pool_unref(&changes.pool);
return ret;
}
int mailbox_transaction_commit_get_changes(
struct mailbox_transaction_context **_t,
struct mail_transaction_commit_changes *changes_r)
{
struct mailbox_transaction_context *t = *_t;
int ret;
t->box->transaction_count--;
*_t = NULL;
T_BEGIN {
ret = t->box->v.transaction_commit(t, changes_r);
} T_END;
return ret;
}
void mailbox_transaction_rollback(struct mailbox_transaction_context **_t)
{
struct mailbox_transaction_context *t = *_t;
t->box->transaction_count--;
*_t = NULL;
t->box->v.transaction_rollback(t);
}
unsigned int mailbox_transaction_get_count(const struct mailbox *box)
{
return box->transaction_count;
}
void mailbox_transaction_set_max_modseq(struct mailbox_transaction_context *t,
uint64_t max_modseq,
ARRAY_TYPE(seq_range) *seqs)
{
mail_index_transaction_set_max_modseq(t->itrans, max_modseq, seqs);
}
struct mailbox *
mailbox_transaction_get_mailbox(const struct mailbox_transaction_context *t)
{
return t->box;
}
struct mail_save_context *
mailbox_save_alloc(struct mailbox_transaction_context *t)
{
struct mail_save_context *ctx;
ctx = t->box->v.save_alloc(t);
ctx->received_date = (time_t)-1;
ctx->save_date = (time_t)-1;
return ctx;
}
void mailbox_save_set_flags(struct mail_save_context *ctx,
enum mail_flags flags,
struct mail_keywords *keywords)
{
ctx->flags = flags;
ctx->keywords = keywords;
if (keywords != NULL)
mailbox_keywords_ref(keywords);
}
void mailbox_save_copy_flags(struct mail_save_context *ctx, struct mail *mail)
{
const char *const *keywords_list;
keywords_list = mail_get_keywords(mail);
ctx->keywords = str_array_length(keywords_list) == 0 ? NULL :
mailbox_keywords_create_valid(ctx->transaction->box,
keywords_list);
ctx->flags = mail_get_flags(mail);
}
void mailbox_save_set_min_modseq(struct mail_save_context *ctx,
uint64_t min_modseq)
{
ctx->min_modseq = min_modseq;
}
void mailbox_save_set_received_date(struct mail_save_context *ctx,
time_t received_date, int timezone_offset)
{
ctx->received_date = received_date;
ctx->received_tz_offset = timezone_offset;
}
void mailbox_save_set_save_date(struct mail_save_context *ctx,
time_t save_date)
{
ctx->save_date = save_date;
}
void mailbox_save_set_from_envelope(struct mail_save_context *ctx,
const char *envelope)
{
i_free(ctx->from_envelope);
ctx->from_envelope = i_strdup(envelope);
}
void mailbox_save_set_uid(struct mail_save_context *ctx, uint32_t uid)
{
ctx->uid = uid;
}
void mailbox_save_set_guid(struct mail_save_context *ctx, const char *guid)
{
i_assert(guid == NULL || *guid != '\0');
i_free(ctx->guid);
ctx->guid = i_strdup(guid);
}
void mailbox_save_set_pop3_uidl(struct mail_save_context *ctx, const char *uidl)
{
i_assert(*uidl != '\0');
i_assert(strchr(uidl, '\n') == NULL);
i_free(ctx->pop3_uidl);
ctx->pop3_uidl = i_strdup(uidl);
}
void mailbox_save_set_dest_mail(struct mail_save_context *ctx,
struct mail *mail)
{
ctx->dest_mail = mail;
}
int mailbox_save_begin(struct mail_save_context **ctx, struct istream *input)
{
struct mailbox *box = (*ctx)->transaction->box;
int ret;
if (mail_index_is_deleted(box->index)) {
mailbox_set_deleted(box);
return -1;
}
if (box->v.save_begin == NULL) {
mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
"Saving messages not supported");
ret = -1;
} else {
ret = box->v.save_begin(*ctx, input);
}
if (ret < 0) {
mailbox_save_cancel(ctx);
return -1;
}
return 0;
}
int mailbox_save_continue(struct mail_save_context *ctx)
{
return ctx->transaction->box->v.save_continue(ctx);
}
int mailbox_save_finish(struct mail_save_context **_ctx)
{
struct mail_save_context *ctx = *_ctx;
struct mailbox *box = ctx->transaction->box;
struct mail_keywords *keywords = ctx->keywords;
int ret;
*_ctx = NULL;
ret = box->v.save_finish(ctx);
if (keywords != NULL)
mailbox_keywords_unref(&keywords);
return ret;
}
void mailbox_save_cancel(struct mail_save_context **_ctx)
{
struct mail_save_context *ctx = *_ctx;
struct mail_keywords *keywords = ctx->keywords;
*_ctx = NULL;
ctx->transaction->box->v.save_cancel(ctx);
if (keywords != NULL)
mailbox_keywords_unref(&keywords);
}
struct mailbox_transaction_context *
mailbox_save_get_transaction(struct mail_save_context *ctx)
{
return ctx->transaction;
}
int mailbox_copy(struct mail_save_context **_ctx, struct mail *mail)
{
struct mail_save_context *ctx = *_ctx;
struct mailbox *box = ctx->transaction->box;
struct mail_keywords *keywords = ctx->keywords;
int ret;
*_ctx = NULL;
if (mail_index_is_deleted(box->index)) {
mailbox_set_deleted(box);
mailbox_save_cancel(_ctx);
return -1;
}
ret = ctx->transaction->box->v.copy(ctx, mail);
if (keywords != NULL)
mailbox_keywords_unref(&keywords);
return ret;
}
bool mailbox_is_inconsistent(struct mailbox *box)
{
return box->mailbox_deleted || box->v.is_inconsistent(box);
}
void mailbox_set_deleted(struct mailbox *box)
{
mail_storage_set_error(box->storage, MAIL_ERROR_NOTFOUND,
"Mailbox was deleted under us");
box->mailbox_deleted = TRUE;
}
const char *mailbox_get_path(struct mailbox *box)
{
const char *path;
if (box->_path == NULL) {
path = mailbox_list_get_path(box->list, box->name,
MAILBOX_LIST_PATH_TYPE_MAILBOX);
box->_path = p_strdup(box->pool, path);
}
return box->_path;
}
static void mailbox_get_permissions_if_not_set(struct mailbox *box)
{
if (box->_perm.file_create_mode != 0)
return;
if (box->input != NULL) {
box->_perm.file_uid = geteuid();
box->_perm.file_create_mode = 0600;
box->_perm.dir_create_mode = 0700;
box->_perm.file_create_gid = (gid_t)-1;
box->_perm.file_create_gid_origin = "defaults";
return;
}
mailbox_list_get_permissions(box->list, box->name, &box->_perm);
box->_perm.file_create_gid_origin =
p_strdup(box->pool, box->_perm.file_create_gid_origin);
}
const struct mailbox_permissions *mailbox_get_permissions(struct mailbox *box)
{
mailbox_get_permissions_if_not_set(box);
if (!box->_perm.mail_index_permissions_set && box->index != NULL) {
box->_perm.mail_index_permissions_set = TRUE;
mail_index_set_permissions(box->index,
box->_perm.file_create_mode,
box->_perm.file_create_gid,
box->_perm.file_create_gid_origin);
}
return &box->_perm;
}
void mailbox_refresh_permissions(struct mailbox *box)
{
memset(&box->_perm, 0, sizeof(box->_perm));
(void)mailbox_get_permissions(box);
}
int mailbox_create_fd(struct mailbox *box, const char *path, int flags,
int *fd_r)
{
const struct mailbox_permissions *perm = mailbox_get_permissions(box);
mode_t old_mask;
int fd;
i_assert((flags & O_CREAT) != 0);
*fd_r = -1;
old_mask = umask(0);
fd = open(path, flags, perm->file_create_mode);
umask(old_mask);
if (fd != -1) {
/* ok */
} else if (errno == EEXIST) {
/* O_EXCL used, caller will handle this error */
return 0;
} else if (errno == ENOENT) {
mailbox_set_deleted(box);
return -1;
} else if (errno == ENOTDIR) {
mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
"Mailbox doesn't allow inferior mailboxes");
return -1;
} else if (mail_storage_set_error_from_errno(box->storage)) {
return -1;
} else {
mail_storage_set_critical(box->storage,
"open(%s, O_CREAT) failed: %m", path);
return -1;
}
if (perm->file_create_gid != (gid_t)-1) {
if (fchown(fd, (uid_t)-1, perm->file_create_gid) == 0) {
/* ok */
} else if (errno == EPERM) {
mail_storage_set_critical(box->storage, "%s",
eperm_error_get_chgrp("fchown", path,
perm->file_create_gid,
perm->file_create_gid_origin));
} else {
mail_storage_set_critical(box->storage,
"fchown(%s) failed: %m", path);
}
}
*fd_r = fd;
return 1;
}
unsigned int mail_storage_get_lock_timeout(struct mail_storage *storage,
unsigned int secs)
{
return storage->set->mail_max_lock_timeout == 0 ? secs :
I_MIN(secs, storage->set->mail_max_lock_timeout);
}