shared-storage.c revision cda217260716cfd8d8ec5e56f91708c64c140538
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2008 Dovecot authors, see the included COPYING file */
56561d472db25ebda35ae6afdc7f7deb75c323fcTimo Sirainen MODULE_CONTEXT(obj, shared_mailbox_list_module)
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(shared_mailbox_list_module,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen pool = pool_alloconly_create("shared storage", 256);
b6612c334604eeb27e1ca2bd804ac66dcbc2eaadTimo Sirainen storage = p_new(pool, struct shared_storage, 1);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen storage->storage.storage_class = &shared_storage;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen storage->base_dir = p_strdup(pool, getenv("BASE_DIR"));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int shared_create(struct mail_storage *_storage, const char *data,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char **error_r)
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen const char *driver, *p;
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen /* data must begin with the actual mailbox driver */
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen *error_r = "Shared mailbox location not prefixed with driver";
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen storage->location = p_strdup(_storage->pool, data);
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen storage->storage_class = mail_storage_find_class(driver);
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen *error_r = t_strconcat("Unknown shared storage driver: ",
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen _storage->mailbox_is_file = storage->storage_class->mailbox_is_file;
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen *error_r = "Shared namespace prefix doesn't contain %";
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen storage->ns_prefix_pattern = p_strdup(_storage->pool, p);
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen _storage->ns->prefix = p_strdup_until(_storage->ns->user->pool,
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen for (p = storage->ns_prefix_pattern; *p != '\0'; p++) {
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen if (*p != '%')
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen if (*++p == '\0')
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen else if (*p != 'd')
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen if (*p != '\0') {
94a8cb0ee1d85569ad1a2acacd92d3ce22f8a1cbTimo Sirainen *error_r = "Shared namespace prefix contains unknown variables";
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen *error_r = "Shared namespace prefix doesn't contain %u or %n";
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen if (mailbox_list_alloc("shared", &_storage->list, error_r) < 0)
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen MODULE_CONTEXT_SET_FULL(_storage->list, shared_mailbox_list_module,
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen list_set.mail_storage_flags = &_storage->flags;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen list_set.lock_method = &_storage->lock_method;
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen mailbox_list_init(_storage->list, _storage->ns, &list_set,
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainen mail_storage_get_list_flags(_storage->flags));
b032dc80e358f09893f09999f172ff12f5dbbb8eTimo Sirainenstatic void shared_storage_destroy(struct mail_storage *_storage)
49e9acb52bb5d328f8cf10bce1082c4bc213caeaTimo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
8d131435ba4648c8821160ec38d508c97177c715Timo Sirainen auth_master_deinit(&storage->auth_master_conn);
a050ca9def13949dbaa67bd6574a41c4f397ae26Timo Sirainenstatic void shared_storage_auth_master_init(struct shared_storage *storage)
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen auth_socket_path = getenv("AUTH_SOCKET_PATH");
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen auth_socket_path = t_strconcat(storage->base_dir,
49e9acb52bb5d328f8cf10bce1082c4bc213caeaTimo Sirainen debug = (storage->storage.flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen storage->auth_master_conn = auth_master_init(auth_socket_path, debug);
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainenshared_storage_lookup_home(struct shared_storage *storage,
c680a6b35b459045e92814778908da5a93922107Timo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 512);
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen ret = auth_master_user_lookup(storage->auth_master_conn, user,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainenstatic void get_nonexisting_user_location(struct shared_storage *storage,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen /* user wasn't found. we'll still need to create the storage
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen to avoid exposing which users exist and which don't. */
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen str_append(location, storage->storage_class->name);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen /* use a reachable but non-existing path as the mail root directory */
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen str_append(location, PKG_RUNDIR"/user-not-found");
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainenint shared_storage_get_namespace(struct mail_storage *_storage,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen const char **_name,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen struct shared_storage *storage = (struct shared_storage *)_storage;
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen static struct var_expand_table static_tab[] = {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *domain = NULL, *username = NULL, *userdomain = NULL;
b43bb773ee6534c1013b01a62fbd5703e3b0d17dTimo Sirainen if (*p != '%') {
e063aca6bc2f08bec516d4b631052ea9191f011dTimo Sirainen switch (*++p) {
b43bb773ee6534c1013b01a62fbd5703e3b0d17dTimo Sirainen /* we checked this already above */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen next = strchr(name, *p != '\0' ? *p : _storage->ns->sep);
ba94d6d7e2f7b07d51f5c27e7532f6502ac9a298Timo Sirainen /* successfully matched the name. */
ba94d6d7e2f7b07d51f5c27e7532f6502ac9a298Timo Sirainen username = t_strdup_until(userdomain, domain);
ba94d6d7e2f7b07d51f5c27e7532f6502ac9a298Timo Sirainen /* expand the namespace prefix and see if it already exists.
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen this should normally happen only when the mailbox is being opened */
ba94d6d7e2f7b07d51f5c27e7532f6502ac9a298Timo Sirainen var_expand(prefix, storage->ns_prefix_pattern, tab);
ba94d6d7e2f7b07d51f5c27e7532f6502ac9a298Timo Sirainen ns = mail_namespace_find_prefix(user->namespaces, str_c(prefix));
ba94d6d7e2f7b07d51f5c27e7532f6502ac9a298Timo Sirainen /* we'll need to look up the user's home directory */
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen ret = shared_storage_lookup_home(storage, userdomain,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen mail_storage_set_critical(_storage, "Namespace '%s': "
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen "Could not lookup home for user %s",
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen /* create the new namespace */
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen ns = p_new(user->pool, struct mail_namespace, 1);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen ns->prefix = p_strdup(user->pool, str_c(prefix));
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen ns->flags = NAMESPACE_FLAG_LIST | NAMESPACE_FLAG_HIDDEN |
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen get_nonexisting_user_location(storage, location);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen if (mail_storage_create(ns, NULL, str_c(location), _storage->flags,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen mail_storage_set_critical(_storage, "Namespace '%s': %s",
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen /* FIXME: we could remove namespaces here that don't have usable
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen mailboxes. otherwise the memory usage could just keep growing. */
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainenstatic void shared_mailbox_copy_error(struct mail_storage *shared_storage,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen const char *str;
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen str = mail_storage_get_last_error(backend_ns->storage, &error);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen mail_storage_set_error(shared_storage, error, str);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainenstatic struct mailbox *
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainenshared_mailbox_open(struct mail_storage *storage, const char *name,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen struct istream *input, enum mailbox_open_flags flags)
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen "Shared storage doesn't support streamed mailboxes");
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen if (shared_storage_get_namespace(storage, &name, &ns) < 0)
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen /* if we call the normal mailbox_open() here the plugins will see
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen mailbox_open() called twice and they could break. */
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainenstatic int shared_mailbox_create(struct mail_storage *storage,
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen if (shared_storage_get_namespace(storage, &name, &ns) < 0)
919875067c26fb261a15b3f7afdb67d7eeddb226Timo Sirainen ret = mail_storage_mailbox_create(ns->storage, name, directory);
20c892309312df8f4f73cfcaf8acd2ededda8b05Timo Sirainen MEMBER(mailbox_is_file) FALSE, /* unknown at this point */