shared-storage.c revision 7a7d2aa11e46195e2d92d6c337d7e78052a5ce67
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic struct mail_storage *shared_storage_alloc(void)
f325d795b52ce2053f914072b22ebca9c4f0dc7eTimo Sirainen pool = pool_alloconly_create("shared storage", 1024);
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen storage = p_new(pool, struct shared_storage, 1);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenshared_storage_create(struct mail_storage *_storage, struct mail_namespace *ns,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char **error_r)
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen const char *driver, *p;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* location must begin with the actual mailbox driver */
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen *error_r = "Shared mailbox location not prefixed with driver";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen driver = t_strdup_until(ns->set->location, p);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen storage->location = p_strdup(_storage->pool, ns->set->location);
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen storage->storage_class = mail_storage_find_class(driver);
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen *error_r = t_strconcat("Unknown shared storage driver: ",
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen _storage->class_flags = storage->storage_class->class_flags;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen *error_r = "Shared namespace prefix doesn't contain %";
c8cf8a605e0ddea7cb36fe04551aeca5090e684bTimo Sirainen storage->ns_prefix_pattern = p_strdup(_storage->pool, wildcardp);
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen for (p = storage->ns_prefix_pattern; *p != '\0'; p++) {
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen if (*p != '%')
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen if (*p != '\0') {
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen *error_r = "Shared namespace prefix contains unknown variables";
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen *error_r = "Shared namespace prefix doesn't contain %u or %n";
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen /* truncate prefix after the above checks are done, so they can log
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen the full prefix in error conditions */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenshared_storage_get_list_settings(const struct mail_namespace *ns ATTR_UNUSED,
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainenget_nonexisting_user_location(struct shared_storage *storage,
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen /* user wasn't found. we'll still need to create the storage
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen to avoid exposing which users exist and which don't. */
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen str_append(location, storage->storage_class->name);
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen /* use a reachable but non-existing path as the mail root directory */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen str_append(location, storage->storage.user->set->base_dir);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenint shared_storage_get_namespace(struct mail_namespace **_ns,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char **_name)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct mail_storage *_storage = (*_ns)->storage;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen static struct var_expand_table static_tab[] = {
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen const char *domain = NULL, *username = NULL, *userdomain = NULL;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen if (*p != '%') {
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen switch (*++p) {
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen /* we checked this already above */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen next = strchr(name, *p != '\0' ? *p : ns->sep);
b1c42176a65dbe9c83a0af766e6bd8315530f3a5Timo Sirainen if (*p != '\0') {
b1c42176a65dbe9c83a0af766e6bd8315530f3a5Timo Sirainen /* trying to open <prefix>/<user> mailbox */
b1c42176a65dbe9c83a0af766e6bd8315530f3a5Timo Sirainen "Invalid namespace prefix %s vs %s",
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen /* successfully matched the name. */
0671e0ae0cfd8d5d671a0c2a75a070c8e2a39fecTimo Sirainen /* user@domain given */
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen username = t_strdup_until(userdomain, domain);
0671e0ae0cfd8d5d671a0c2a75a070c8e2a39fecTimo Sirainen /* trying to open namespace "shared/domain"
0671e0ae0cfd8d5d671a0c2a75a070c8e2a39fecTimo Sirainen namespace prefix. */
0671e0ae0cfd8d5d671a0c2a75a070c8e2a39fecTimo Sirainen mailbox_list_set_error(list, MAIL_ERROR_NOTFOUND,
0671e0ae0cfd8d5d671a0c2a75a070c8e2a39fecTimo Sirainen /* no domain given, use ours (if we have one) */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen mailbox_list_set_error(list, MAIL_ERROR_PARAMS,
d927bc5618696157fc55eb1f11b5cab05400ed52Timo Sirainen "Empty username doesn't exist");
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen /* expand the namespace prefix and see if it already exists.
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen this should normally happen only when the mailbox is being opened */
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen var_expand(prefix, storage->ns_prefix_pattern, tab);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen *_ns = mail_namespace_find_prefix(user->namespaces, str_c(prefix));
2fb9ae42f9e36388ec6db24188b9108434043fd0Timo Sirainen owner = mail_user_alloc(userdomain, user->set_info,
f77ffa31038d46ca9c6d24d93e3d76c9aa8d4d0cTimo Sirainen if (!var_has_key(storage->location, 'h', "home"))
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen /* we'll need to look up the user's home directory */
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen if ((ret = mail_user_get_home(owner, &tab[3].value)) < 0) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen mailbox_list_set_critical(list, "Namespace '%s': "
c4c9be10781e1a16b3b001dc6b0461c4640da101Timo Sirainen "Could not lookup home for user %s",
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen "Couldn't create namespace '%s' for user %s: %s",
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen /* create the new namespace */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen new_ns->flags = (NAMESPACE_FLAG_SUBSCRIPTIONS & ns->flags) |
9762e4f86950549c8186c7d3d4fa4a6b533ea848Timo Sirainen NAMESPACE_FLAG_LIST_PREFIX | NAMESPACE_FLAG_HIDDEN |
e930af34580510d2fe58628d270dbaf786e86248Timo Sirainen NAMESPACE_FLAG_AUTOCREATED | NAMESPACE_FLAG_INBOX;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen get_nonexisting_user_location(storage, userdomain, location);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen ns_set = p_new(user->pool, struct mail_namespace_settings, 1);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ns_set->separator = p_strdup_printf(user->pool, "%c", new_ns->sep);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen ns_set->location = p_strdup(user->pool, str_c(location));
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen if (mail_storage_create(new_ns, NULL, _storage->flags, &error) < 0) {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen mailbox_list_set_critical(list, "Namespace '%s': %s",