mail-namespace.c revision eae6e66545ee1847d5977a6dbebc31df84563e0c
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "lib.h"
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen#include "array.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "str.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "file-lock.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mail-storage-private.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mail-storage-settings.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "mail-namespace.h"
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen#include <stdlib.h>
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenvoid (*hook_mail_namespaces_created)(struct mail_namespace *namespaces);
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainenvoid mail_namespace_add_storage(struct mail_namespace *ns,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct mail_storage *storage)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen /* currently we support only a single storage */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_assert(ns->storage == NULL);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->storage = storage;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (storage->v.add_list != NULL)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen storage->v.add_list(storage, ns->list);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenvoid mail_namespace_finish_list_init(struct mail_namespace *ns,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct mailbox_list *list)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen ns->list = list;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen /* allow plugins to override real_sep */
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if (ns->real_sep == '\0')
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen ns->real_sep = mailbox_list_get_hierarchy_sep(list);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->prefix_len = strlen(ns->prefix);
2c25e1360d4b5cc55eda969a3a7204d950de5a8fTimo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->set->separator != NULL)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->sep = *ns->set->separator;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen if (ns->sep == '\0')
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->sep = ns->real_sep;
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen if (ns->sep == '"' || ns->sep == '\\') {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->sep_str[0] = '\\';
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->sep_str[1] = ns->sep;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen } else {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen ns->sep_str[0] = ns->sep;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen }
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstatic void mail_namespace_free(struct mail_namespace *ns)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->storage != NULL)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mail_storage_unref(&ns->storage);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->list != NULL)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mailbox_list_destroy(&ns->list);
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->owner != ns->user && ns->owner != NULL)
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen mail_user_unref(&ns->owner);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_free(ns->prefix);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_free(ns);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainenstatic int
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainennamespace_add(struct mail_user *user,
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen struct mail_namespace_settings *ns_set,
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen const struct mail_storage_settings *mail_set,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct mail_namespace **ns_p, const char **error_r)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen struct mail_namespace *ns;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen const char *driver, *error;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen ns = i_new(struct mail_namespace, 1);
af81f402ddc897c74c1e85abd02879612ce44882Timo Sirainen ns->user = user;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (strncmp(ns_set->type, "private", 7) == 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->owner = user;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->type = NAMESPACE_PRIVATE;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen } else if (strncmp(ns_set->type, "shared", 6) == 0)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen ns->type = NAMESPACE_SHARED;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen else if (strncmp(ns_set->type, "public", 6) == 0)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen ns->type = NAMESPACE_PUBLIC;
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen else {
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen *error_r = t_strdup_printf("Unknown namespace type: %s",
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen ns_set->type);
9e095dd6a77097356aca8216356d4d71ef1bea45Timo Sirainen mail_namespace_free(ns);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (strcmp(ns_set->list, "children") == 0)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->flags |= NAMESPACE_FLAG_LIST_CHILDREN;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen else if (strcmp(ns_set->list, "yes") == 0)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->flags |= NAMESPACE_FLAG_LIST_PREFIX;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen else if (strcmp(ns_set->list, "no") != 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = t_strdup_printf("Invalid list setting value: %s",
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns_set->list);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mail_namespace_free(ns);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen return -1;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen }
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if (ns_set->inbox)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->flags |= NAMESPACE_FLAG_INBOX;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns_set->hidden)
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen ns->flags |= NAMESPACE_FLAG_HIDDEN;
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainen if (ns_set->subscriptions)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->flags |= NAMESPACE_FLAG_SUBSCRIPTIONS;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen if (mail_set->mail_debug) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_info("Namespace: type=%s, prefix=%s, sep=%s, "
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen "inbox=%s, hidden=%s, list=%s, subscriptions=%s",
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns_set->type, ns_set->prefix,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns_set->separator == NULL ? "" : ns_set->separator,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns_set->inbox ? "yes" : "no",
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns_set->hidden ? "yes" : "no",
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns_set->list ? "yes" : "no",
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns_set->subscriptions ? "yes" : "no");
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen if (*ns_set->location == '\0')
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns_set->location = mail_set->mail_location;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns->set = ns_set;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns->mail_set = mail_set;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns->prefix = i_strdup(ns_set->prefix);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if (ns->type == NAMESPACE_SHARED && strchr(ns->prefix, '%') != NULL) {
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen /* dynamic shared namespace */
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen ns->flags |= NAMESPACE_FLAG_NOQUOTA | NAMESPACE_FLAG_NOACL;
67c24901ac5e1521e38a91efc452faeb3e2135a1Timo Sirainen driver = "shared";
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen } else {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen driver = NULL;
e11a64ffc7f08b4cb05bcc27668d154d33d0c2e0Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if (mail_storage_create(ns, driver, 0, &error) < 0) {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen *error_r = t_strdup_printf("Namespace '%s': %s",
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen ns->prefix, error);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen mail_namespace_free(ns);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen *ns_p = ns;
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return 0;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainenstatic int
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainennamespace_set_alias_for(struct mail_namespace *ns,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct mail_namespace *all_namespaces,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen const char **error_r)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->set->alias_for != NULL) {
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen ns->alias_for = mail_namespace_find_prefix(all_namespaces,
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen ns->set->alias_for);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ns->alias_for == NULL) {
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen *error_r = t_strdup_printf("Invalid namespace alias_for: %s",
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->set->alias_for);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
b3142c8e513bc78da821fa70f479016148fa95e5Timo Sirainen if (ns->alias_for->alias_for != NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = t_strdup_printf("Chained namespace alias_for: %s",
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->set->alias_for);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return -1;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ns->alias_chain_next = ns->alias_for->alias_chain_next;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen ns->alias_for->alias_chain_next = ns;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen}
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainenstatic bool
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainennamespaces_check(struct mail_namespace *namespaces, const char **error_r)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen{
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen struct mail_namespace *ns, *inbox_ns = NULL;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen unsigned int subscriptions_count = 0;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen char list_sep = '\0';
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen for (ns = namespaces; ns != NULL; ns = ns->next) {
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if (namespace_set_alias_for(ns, namespaces, error_r) < 0)
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return FALSE;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if ((ns->flags & NAMESPACE_FLAG_INBOX) != 0) {
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if (inbox_ns != NULL) {
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen *error_r = "namespace configuration error: "
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen "There can be only one namespace with "
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen "inbox=yes";
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return FALSE;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen inbox_ns = ns;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if (*ns->prefix != '\0' &&
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen (ns->flags & NAMESPACE_FLAG_LIST_PREFIX) != 0 &&
7d207b1e77a7b5e3fda640e353acfc86d261fedfTimo Sirainen ns->prefix[strlen(ns->prefix)-1] != ns->sep) {
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen *error_r = t_strdup_printf(
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "namespace configuration error: "
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen "list=yes requires prefix=%s "
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen "to end with separator", ns->prefix);
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen return FALSE;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen }
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen if ((ns->flags & (NAMESPACE_FLAG_LIST_PREFIX |
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen NAMESPACE_FLAG_LIST_CHILDREN)) != 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (list_sep == '\0')
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen list_sep = ns->sep;
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen else if (list_sep != ns->sep) {
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen *error_r = "namespace configuration error: "
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen "All list=yes namespaces must use "
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "the same separator";
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen return FALSE;
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if ((ns->flags & NAMESPACE_FLAG_SUBSCRIPTIONS) != 0)
e11a64ffc7f08b4cb05bcc27668d154d33d0c2e0Timo Sirainen subscriptions_count++;
e11a64ffc7f08b4cb05bcc27668d154d33d0c2e0Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (inbox_ns == NULL) {
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen *error_r = "namespace configuration error: "
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "inbox=yes namespace missing";
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return FALSE;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (list_sep == '\0') {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *error_r = "namespace configuration error: "
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "no list=yes namespaces";
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen return FALSE;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (subscriptions_count == 0) {
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen *error_r = "namespace configuration error: "
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "no subscriptions=yes namespaces";
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen return FALSE;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen }
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen return TRUE;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
7ef5ca6fb59a318c821a852ae48a2edbb671d7ddTimo Sirainenint mail_namespaces_init(struct mail_user *user, const char **error_r)
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen{
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen const struct mail_storage_settings *mail_set;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen struct mail_namespace_settings *const *ns_set;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen struct mail_namespace *namespaces, *ns, **ns_p;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct mail_namespace_settings *inbox_set;
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainen const char *error, *driver, *location_source;
9511a40d933181045343110c8101b75887062aaeTimo Sirainen unsigned int i, count;
9511a40d933181045343110c8101b75887062aaeTimo Sirainen
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen i_assert(user->initialized);
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen
411d6baa37f31d90730e90c4a28c43e1974bbe58Timo Sirainen namespaces = NULL; ns_p = &namespaces;
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mail_set = mail_user_set_get_storage_set(user->set);
if (array_is_created(&user->set->namespaces))
ns_set = array_get(&user->set->namespaces, &count);
else {
ns_set = NULL;
count = 0;
}
for (i = 0; i < count; i++) {
if (namespace_add(user, ns_set[i], mail_set, ns_p, error_r) < 0)
return -1;
ns_p = &(*ns_p)->next;
}
if (namespaces != NULL) {
if (!namespaces_check(namespaces, error_r)) {
while (namespaces != NULL) {
ns = namespaces;
namespaces = ns->next;
mail_namespace_free(ns);
}
return -1;
}
mail_user_add_namespace(user, &namespaces);
if (hook_mail_namespaces_created != NULL) {
T_BEGIN {
hook_mail_namespaces_created(namespaces);
} T_END;
}
return 0;
}
/* no namespaces defined, create a default one */
ns = i_new(struct mail_namespace, 1);
ns->type = NAMESPACE_PRIVATE;
ns->flags = NAMESPACE_FLAG_INBOX | NAMESPACE_FLAG_LIST_PREFIX |
NAMESPACE_FLAG_SUBSCRIPTIONS;
ns->owner = user;
inbox_set = p_new(user->pool, struct mail_namespace_settings, 1);
*inbox_set = mail_namespace_default_settings;
inbox_set->inbox = TRUE;
driver = NULL;
if (*mail_set->mail_location != '\0') {
inbox_set->location = mail_set->mail_location;
location_source = "mail_location setting";
} else {
location_source = "environment MAIL";
inbox_set->location = getenv("MAIL");
}
if (inbox_set->location == NULL) {
/* support also maildir-specific environment */
inbox_set->location = getenv("MAILDIR");
if (inbox_set->location == NULL)
inbox_set->location = "";
else {
driver = "maildir";
location_source = "environment MAILDIR";
}
}
ns->set = inbox_set;
ns->mail_set = mail_set;
ns->prefix = i_strdup(ns->set->prefix);
ns->user = user;
if (mail_storage_create(ns, driver, 0, &error) < 0) {
if (*inbox_set->location != '\0') {
*error_r = t_strdup_printf(
"Initializing mail storage from %s "
"failed: %s", location_source, error);
} else {
*error_r = t_strdup_printf("mail_location not set and "
"autodetection failed: %s", error);
}
mail_namespace_free(ns);
return -1;
}
user->namespaces = ns;
if (hook_mail_namespaces_created != NULL) {
T_BEGIN {
hook_mail_namespaces_created(ns);
} T_END;
}
return 0;
}
struct mail_namespace *mail_namespaces_init_empty(struct mail_user *user)
{
struct mail_namespace *ns;
ns = i_new(struct mail_namespace, 1);
ns->user = user;
ns->owner = user;
ns->prefix = i_strdup("");
ns->flags = NAMESPACE_FLAG_INBOX | NAMESPACE_FLAG_LIST_PREFIX |
NAMESPACE_FLAG_SUBSCRIPTIONS;
ns->mail_set = mail_user_set_get_storage_set(user->set);
user->namespaces = ns;
return ns;
}
void mail_namespaces_deinit(struct mail_namespace **_namespaces)
{
struct mail_namespace *ns, *namespaces = *_namespaces;
*_namespaces = NULL;
while (namespaces != NULL) {
ns = namespaces;
namespaces = namespaces->next;
mail_namespace_free(ns);
}
}
void mail_namespaces_set_storage_callbacks(struct mail_namespace *namespaces,
struct mail_storage_callbacks *callbacks,
void *context)
{
struct mail_namespace *ns;
for (ns = namespaces; ns != NULL; ns = ns->next)
mail_storage_set_callbacks(ns->storage, callbacks, context);
}
void mail_namespace_destroy(struct mail_namespace *ns)
{
struct mail_namespace **nsp;
/* remove from user's namespaces list */
for (nsp = &ns->user->namespaces; *nsp != NULL; nsp = &(*nsp)->next) {
if (*nsp == ns) {
*nsp = ns->next;
break;
}
}
mail_namespace_free(ns);
}
const char *mail_namespace_fix_sep(struct mail_namespace *ns, const char *name)
{
char *ret, *p;
if (ns->sep == ns->real_sep)
return name;
if (ns->type == NAMESPACE_SHARED &&
(ns->flags & NAMESPACE_FLAG_AUTOCREATED) == 0) {
/* shared namespace root. the backend storage's hierarchy
separator isn't known yet, so do nothing. */
return name;
}
ret = p_strdup(unsafe_data_stack_pool, name);
for (p = ret; *p != '\0'; p++) {
if (*p == ns->sep)
*p = ns->real_sep;
}
return ret;
}
const char *mail_namespace_get_storage_name(struct mail_namespace *ns,
const char *name)
{
unsigned int len = strlen(ns->prefix);
if (len > 0) {
if (strncmp(ns->prefix, name, len) == 0)
name += len;
else {
i_assert(strcasecmp(name, "INBOX") == 0);
}
}
return mail_namespace_fix_sep(ns, name);
}
const char *mail_namespace_get_vname(struct mail_namespace *ns, string_t *dest,
const char *name)
{
str_truncate(dest, 0);
if ((ns->flags & NAMESPACE_FLAG_INBOX) == 0 ||
strcasecmp(name, "INBOX") != 0 ||
ns->user != ns->owner)
str_append(dest, ns->prefix);
for (; *name != '\0'; name++) {
if (*name == ns->real_sep)
str_append_c(dest, ns->sep);
else
str_append_c(dest, *name);
}
return str_c(dest);
}
struct mail_storage *
mail_namespace_get_default_storage(struct mail_namespace *ns)
{
/* currently we don't support more than one storage per namespace */
return ns->storage;
}
char mail_namespaces_get_root_sep(const struct mail_namespace *namespaces)
{
while ((namespaces->flags & NAMESPACE_FLAG_LIST_PREFIX) == 0)
namespaces = namespaces->next;
return namespaces->sep;
}
static bool mail_namespace_is_usable_prefix(struct mail_namespace *ns,
const char *mailbox, bool inbox)
{
if (strncmp(ns->prefix, mailbox, ns->prefix_len) == 0) {
/* true exact prefix match */
return TRUE;
}
if (inbox && strncmp(ns->prefix, "INBOX", 5) == 0 &&
strncmp(ns->prefix+5, mailbox+5, ns->prefix_len-5) == 0) {
/* we already checked that mailbox begins with case-insensitive
INBOX. this namespace also begins with INBOX and the rest
of the prefix matches too. */
return TRUE;
}
if (strncmp(ns->prefix, mailbox, ns->prefix_len-1) == 0 &&
mailbox[ns->prefix_len-1] == '\0' &&
ns->prefix[ns->prefix_len-1] == ns->sep) {
/* we're trying to access the namespace prefix itself */
return TRUE;
}
return FALSE;
}
static struct mail_namespace *
mail_namespace_find_mask(struct mail_namespace *namespaces,
const char **mailbox,
enum namespace_flags flags,
enum namespace_flags mask)
{
struct mail_namespace *ns = namespaces;
const char *box = *mailbox;
struct mail_namespace *best = NULL;
unsigned int len, best_len = 0;
bool inbox;
inbox = strncasecmp(box, "INBOX", 5) == 0;
if (inbox && box[5] == '\0') {
/* find the INBOX namespace */
*mailbox = "INBOX";
while (ns != NULL) {
if ((ns->flags & NAMESPACE_FLAG_INBOX) != 0 &&
(ns->flags & mask) == flags)
return ns;
if (*ns->prefix == '\0')
best = ns;
ns = ns->next;
}
return best;
}
for (; ns != NULL; ns = ns->next) {
if (ns->prefix_len >= best_len && (ns->flags & mask) == flags &&
mail_namespace_is_usable_prefix(ns, box, inbox)) {
best = ns;
best_len = ns->prefix_len;
}
}
if (best != NULL) {
if (best_len > 0) {
len = strlen(*mailbox);
*mailbox += I_MIN(len, best_len);
} else if (inbox && (box[5] == best->sep || box[5] == '\0'))
*mailbox = t_strconcat("INBOX", box+5, NULL);
*mailbox = mail_namespace_fix_sep(best, *mailbox);
}
return best;
}
struct mail_namespace *
mail_namespace_find(struct mail_namespace *namespaces, const char **mailbox)
{
return mail_namespace_find_mask(namespaces, mailbox, 0, 0);
}
struct mail_namespace *
mail_namespace_find_visible(struct mail_namespace *namespaces,
const char **mailbox)
{
return mail_namespace_find_mask(namespaces, mailbox, 0,
NAMESPACE_FLAG_HIDDEN);
}
struct mail_namespace *
mail_namespace_find_subscribable(struct mail_namespace *namespaces,
const char **mailbox)
{
return mail_namespace_find_mask(namespaces, mailbox,
NAMESPACE_FLAG_SUBSCRIPTIONS,
NAMESPACE_FLAG_SUBSCRIPTIONS);
}
struct mail_namespace *
mail_namespace_find_unsubscribable(struct mail_namespace *namespaces,
const char **mailbox)
{
return mail_namespace_find_mask(namespaces, mailbox,
0, NAMESPACE_FLAG_SUBSCRIPTIONS);
}
struct mail_namespace *
mail_namespace_find_inbox(struct mail_namespace *namespaces)
{
while ((namespaces->flags & NAMESPACE_FLAG_INBOX) == 0)
namespaces = namespaces->next;
return namespaces;
}
bool mail_namespace_update_name(const struct mail_namespace *ns,
const char **mailbox)
{
struct mail_namespace tmp_ns = *ns;
/* FIXME: a bit kludgy.. */
tmp_ns.next = NULL;
return mail_namespace_find_mask(&tmp_ns, mailbox, 0, 0) != NULL;
}
struct mail_namespace *
mail_namespace_find_prefix(struct mail_namespace *namespaces,
const char *prefix)
{
struct mail_namespace *ns;
unsigned int len = strlen(prefix);
for (ns = namespaces; ns != NULL; ns = ns->next) {
if (ns->prefix_len == len &&
strcmp(ns->prefix, prefix) == 0)
return ns;
}
return NULL;
}
struct mail_namespace *
mail_namespace_find_prefix_nosep(struct mail_namespace *namespaces,
const char *prefix)
{
struct mail_namespace *ns;
unsigned int len = strlen(prefix);
for (ns = namespaces; ns != NULL; ns = ns->next) {
if (ns->prefix_len == len + 1 &&
strncmp(ns->prefix, prefix, len) == 0 &&
ns->prefix[len] == ns->sep)
return ns;
}
return NULL;
}