shared-storage.c revision c7194d1d3872ffb2901737e1df337cc227a3fa77
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2008 Dovecot authors, see the included COPYING file */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen MODULE_CONTEXT(obj, shared_mailbox_list_module)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(shared_mailbox_list_module,
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen pool = pool_alloconly_create("shared storage", 256);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen storage = p_new(pool, struct shared_storage, 1);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic int shared_create(struct mail_storage *_storage, const char *data,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen const char **error_r)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen const char *driver, *p;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* data must begin with the actual mailbox driver */
4462bd7b4c7ef3de006f060e155a90e5de7cae21Timo Sirainen *error_r = "Shared mailbox location not prefixed with driver";
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen storage->location = p_strdup(_storage->pool, data);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen storage->storage_class = mail_storage_find_class(driver);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *error_r = t_strconcat("Unknown shared storage driver: ",
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen _storage->mailbox_is_file = storage->storage_class->mailbox_is_file;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *error_r = "Shared namespace prefix doesn't contain %";
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen storage->ns_prefix_pattern = p_strdup(_storage->pool, p);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen _storage->ns->prefix = p_strdup_until(_storage->ns->user->pool,
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen for (p = storage->ns_prefix_pattern; *p != '\0'; p++) {
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (*p != '%')
cd83124e5d070a016c590bb0b1096d7828c7b6adTimo Sirainen if (*++p == '\0')
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen else if (*p != 'd')
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (*p != '\0') {
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen *error_r = "Shared namespace prefix contains unknown variables";
cbc61fcb33b370d049c16a3c44568b4deb4e2b33Timo Sirainen *error_r = "Shared namespace prefix doesn't contain %u or %n";
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen if (mailbox_list_alloc("shared", &_storage->list, error_r) < 0)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen MODULE_CONTEXT_SET_FULL(_storage->list, shared_mailbox_list_module,
e07bf3772a2bc075de4915ad0961beb8d083c22dTimo Sirainen list_set.mail_storage_flags = &_storage->flags;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen list_set.lock_method = &_storage->lock_method;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen mailbox_list_init(_storage->list, _storage->ns, &list_set,
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen mail_storage_get_list_flags(_storage->flags));
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainenint shared_storage_get_namespace(struct mail_storage *_storage,
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen const char **_name,
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen static struct var_expand_table static_tab[] = {
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen const char *domain = NULL, *username = NULL, *userdomain = NULL;
96308127e006bb3b1108093bcf4cc1fd9481cb7aTimo Sirainen if (*p != '%') {
d66be2bebfa96e7d3d20e2153f60e6e25dcc9a18Timo Sirainen switch (*++p) {
fd35227c47190afc832579ca5c76524792701bf7Timo Sirainen /* we checked this already above */
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainen next = strchr(name, *p != '\0' ? *p : _storage->ns->sep);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* successfully matched the name. */
5d49cbcf87354f0ddf3b71bc5f0cefdc02b14f68Timo Sirainen username = t_strdup_until(userdomain, domain);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen /* expand the namespace prefix and see if it already exists.
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen this should normally happen only when the mailbox is being opened */
3e7565a7b39694bcdf448d8eb2a7f0774733297bTimo Sirainen var_expand(prefix, storage->ns_prefix_pattern, tab);
3e7565a7b39694bcdf448d8eb2a7f0774733297bTimo Sirainen ns = mail_namespace_find_prefix(user->namespaces, str_c(prefix));
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen /* create the new namespace */
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen ns = p_new(user->pool, struct mail_namespace, 1);
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen ns->prefix = p_strdup(user->pool, str_c(prefix));
bbd4c4cf902539c25c471157eb9849459734759cTimo Sirainen ns->flags = NAMESPACE_FLAG_LIST | NAMESPACE_FLAG_HIDDEN;
2e263a9d901483a902720a30c474761bd3324fe8Timo Sirainen if (mail_storage_create(ns, NULL, str_c(location), _storage->flags,
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen i_error("Namespace '%s': %s", ns->prefix, error);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen /* FIXME: we could remove namespaces here that don't have usable
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen mailboxes. otherwise the memory usage could just keep growing. */
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic void shared_mailbox_copy_error(struct mail_storage *shared_storage,
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen const char *str;
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen str = mail_storage_get_last_error(backend_ns->storage, &error);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen mail_storage_set_error(shared_storage, error, str);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic struct mailbox *
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenshared_mailbox_open(struct mail_storage *storage, const char *name,
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen struct istream *input, enum mailbox_open_flags flags)
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainen "Shared storage doesn't support streamed mailboxes");
73b50eecfc31750a312e2f940023f522eb07178cTimo Sirainen if (shared_storage_get_namespace(storage, &name, &ns) < 0)
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen box = mailbox_open(ns->storage, name, NULL, flags);
cf7164ece50797a67fc4bfb5889022ac93a36a8aTimo Sirainenstatic int shared_mailbox_create(struct mail_storage *storage,
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen if (shared_storage_get_namespace(storage, &name, &ns) < 0)
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen ret = mail_storage_mailbox_create(ns->storage, name, directory);
cbc61fcb33b370d049c16a3c44568b4deb4e2b33Timo Sirainen MEMBER(mailbox_is_file) FALSE, /* unknown at this point */