shared-storage.c revision e930af34580510d2fe58628d270dbaf786e86248
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "lib.h"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "array.h"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "str.h"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "ioloop.h"
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen#include "var-expand.h"
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen#include "index-storage.h"
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen#include "shared-storage.h"
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen
6daca8888bbf2b5bf26903cf397d5219ea752241Timo Sirainen#include <stdlib.h>
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen#include <ctype.h>
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen#define SHARED_LIST_CONTEXT(obj) \
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen MODULE_CONTEXT(obj, shared_mailbox_list_module)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenextern struct mail_storage shared_storage;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenextern struct mailbox shared_mailbox;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(shared_mailbox_list_module,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen &mailbox_list_module_register);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenstatic struct mail_storage *shared_alloc(void)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen{
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct shared_storage *storage;
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen pool_t pool;
d8eedfaad386a8776e4931086b039b72e1ad38c4Timo Sirainen
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen pool = pool_alloconly_create("shared storage", 1024);
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen storage = p_new(pool, struct shared_storage, 1);
acc8c0647873b1c847bfa362ddefd0d219d0aa91Timo Sirainen storage->storage = shared_storage;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->storage.pool = pool;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->storage.storage_class = &shared_storage;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->base_dir = p_strdup(pool, getenv("BASE_DIR"));
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (storage->base_dir == NULL)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->base_dir = PKG_RUNDIR;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return &storage->storage;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen}
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenstatic int shared_create(struct mail_storage *_storage, const char *data,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen const char **error_r)
e6bdf53eb0143af99e3eb977ff0f8a496ecd1a8dTimo Sirainen{
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct mailbox_list_settings list_set;
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen const char *driver, *p;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen char *wildcardp;
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen bool have_username;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen /* data must begin with the actual mailbox driver */
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen p = strchr(data, ':');
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen if (p == NULL) {
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen *error_r = "Shared mailbox location not prefixed with driver";
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen return -1;
f3e1593a7d9b02090575fb20db90a235e10145a5Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen driver = t_strdup_until(data, p);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->location = p_strdup(_storage->pool, data);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->storage_class = mail_storage_find_class(driver);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (storage->storage_class == NULL) {
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen *error_r = t_strconcat("Unknown shared storage driver: ",
ad3a1b8f8e2a5596afb1b099a69ae6f688887eecTimo Sirainen driver, NULL);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen _storage->mailbox_is_file = storage->storage_class->mailbox_is_file;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen wildcardp = strchr(_storage->ns->prefix, '%');
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (wildcardp == NULL) {
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen *error_r = "Shared namespace prefix doesn't contain %";
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage->ns_prefix_pattern = p_strdup(_storage->pool, wildcardp);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen *wildcardp = '\0';
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen have_username = FALSE;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen for (p = storage->ns_prefix_pattern; *p != '\0'; p++) {
e6bdf53eb0143af99e3eb977ff0f8a496ecd1a8dTimo Sirainen if (*p != '%')
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen continue;
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen if (*++p == '\0')
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen break;
9446c7a5d400cba60d097c528bd08312552438e3Timo Sirainen if (*p == 'u' || *p == 'n')
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen have_username = TRUE;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen else if (*p != '%' && *p != 'd')
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen break;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (*p != '\0') {
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen *error_r = "Shared namespace prefix contains unknown variables";
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (!have_username) {
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen *error_r = "Shared namespace prefix doesn't contain %u or %n";
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen if (mailbox_list_alloc("shared", &_storage->list, error_r) < 0)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return -1;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen MODULE_CONTEXT_SET_FULL(_storage->list, shared_mailbox_list_module,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen storage, &storage->list_module_ctx);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen memset(&list_set, 0, sizeof(list_set));
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen list_set.mail_storage_flags = &_storage->flags;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen list_set.lock_method = &_storage->lock_method;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen mailbox_list_init(_storage->list, _storage->ns, &list_set,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen mail_storage_get_list_flags(_storage->flags));
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen return 0;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen}
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenstatic void
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenget_nonexisting_user_location(struct shared_storage *storage,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen const char *username, string_t *location)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen{
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen /* user wasn't found. we'll still need to create the storage
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen to avoid exposing which users exist and which don't. */
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen str_append(location, storage->storage_class->name);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen str_append_c(location, ':');
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen /* use a reachable but non-existing path as the mail root directory */
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen str_append(location, storage->base_dir);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen str_append(location, "/user-not-found/");
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen str_append(location, username);
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen}
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainenint shared_storage_get_namespace(struct mail_storage *_storage,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen const char **_name,
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct mail_namespace **ns_r)
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen{
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct mail_user *user = _storage->ns->user;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen static struct var_expand_table static_tab[] = {
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen { 'u', NULL, "user" },
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen { 'n', NULL, "username" },
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen { 'd', NULL, "domain" },
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen { 'h', NULL, "home" },
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen { '\0', NULL, NULL }
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen };
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct var_expand_table *tab;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct mail_namespace *ns;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen struct mail_user *owner;
b494ffed8ded8d170d9ace3dc607b1d278048241Timo Sirainen const char *domain = NULL, *username = NULL, *userdomain = NULL;
const char *name, *p, *next, **dest, *error;
string_t *prefix, *location;
int ret;
*ns_r = NULL;
p = storage->ns_prefix_pattern;
for (name = *_name; *p != '\0';) {
if (*p != '%') {
if (*p != *name)
return -1;
p++; name++;
continue;
}
switch (*++p) {
case 'd':
dest = &domain;
break;
case 'n':
dest = &username;
break;
case 'u':
dest = &userdomain;
break;
default:
/* we checked this already above */
i_unreached();
}
p++;
next = strchr(name, *p != '\0' ? *p : _storage->ns->sep);
if (next == NULL)
return -1;
*dest = t_strdup_until(name, next);
name = next;
}
/* successfully matched the name. */
if (userdomain == NULL) {
i_assert(username != NULL);
userdomain = domain == NULL ? username :
t_strconcat(username, "@", domain, NULL);
} else {
domain = strchr(userdomain, '@');
if (domain == NULL)
username = userdomain;
else {
username = t_strdup_until(userdomain, domain);
domain++;
}
}
/* expand the namespace prefix and see if it already exists.
this should normally happen only when the mailbox is being opened */
tab = t_malloc(sizeof(static_tab));
memcpy(tab, static_tab, sizeof(static_tab));
tab[0].value = userdomain;
tab[1].value = username;
tab[2].value = domain;
prefix = t_str_new(128);
str_append(prefix, _storage->ns->prefix);
var_expand(prefix, storage->ns_prefix_pattern, tab);
ns = mail_namespace_find_prefix(user->namespaces, str_c(prefix));
if (ns != NULL) {
*_name = mail_namespace_fix_sep(ns, name);
*ns_r = ns;
return 0;
}
owner = mail_user_init(userdomain);
if (!var_has_key(storage->location, 'h', "home"))
ret = 1;
else {
/* we'll need to look up the user's home directory */
if ((ret = mail_user_get_home(owner, &tab[3].value)) < 0) {
mail_storage_set_critical(_storage, "Namespace '%s': "
"Could not lookup home for user %s",
_storage->ns->prefix, userdomain);
mail_user_unref(&owner);
return -1;
}
}
/* create the new namespace */
ns = i_new(struct mail_namespace, 1);
ns->type = NAMESPACE_SHARED;
ns->user = user;
ns->prefix = i_strdup(str_c(prefix));
ns->owner = owner;
ns->flags = NAMESPACE_FLAG_LIST_PREFIX | NAMESPACE_FLAG_HIDDEN |
NAMESPACE_FLAG_AUTOCREATED | NAMESPACE_FLAG_INBOX;
ns->sep = _storage->ns->sep;
location = t_str_new(256);
if (ret > 0)
var_expand(location, storage->location, tab);
else
get_nonexisting_user_location(storage, userdomain, location);
if (mail_storage_create(ns, NULL, str_c(location), _storage->flags,
_storage->lock_method, &error) < 0) {
mail_storage_set_critical(_storage, "Namespace '%s': %s",
ns->prefix, error);
mail_namespace_destroy(ns);
return -1;
}
_storage->ns->flags |= NAMESPACE_FLAG_USABLE;
*_name = mail_namespace_fix_sep(ns, name);
*ns_r = ns;
mail_user_add_namespace(user, &ns);
return 0;
}
static void shared_mailbox_copy_error(struct mail_storage *shared_storage,
struct mail_namespace *backend_ns)
{
const char *str;
enum mail_error error;
str = mail_storage_get_last_error(backend_ns->storage, &error);
mail_storage_set_error(shared_storage, error, str);
}
static struct mailbox *
shared_mailbox_open(struct mail_storage *storage, const char *name,
struct istream *input, enum mailbox_open_flags flags)
{
struct mail_namespace *ns;
struct mailbox *box;
if (input != NULL) {
mail_storage_set_critical(storage,
"Shared storage doesn't support streamed mailboxes");
return NULL;
}
if (shared_storage_get_namespace(storage, &name, &ns) < 0)
return NULL;
/* if we call the normal mailbox_open() here the plugins will see
mailbox_open() called twice and they could break. */
box = ns->storage->storage_class->v.
mailbox_open(ns->storage, name, NULL, flags);
if (box == NULL)
shared_mailbox_copy_error(storage, ns);
else
ns->flags |= NAMESPACE_FLAG_USABLE;
return box;
}
static int shared_mailbox_create(struct mail_storage *storage,
const char *name, bool directory)
{
struct mail_namespace *ns;
int ret;
if (shared_storage_get_namespace(storage, &name, &ns) < 0)
return -1;
ret = mail_storage_mailbox_create(ns->storage, name, directory);
if (ret < 0)
shared_mailbox_copy_error(storage, ns);
return ret;
}
struct mail_storage shared_storage = {
MEMBER(name) SHARED_STORAGE_NAME,
MEMBER(mailbox_is_file) FALSE, /* unknown at this point */
{
NULL,
NULL,
shared_alloc,
shared_create,
index_storage_destroy,
NULL,
shared_mailbox_open,
shared_mailbox_create
}
};