]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Created mail_user_[try_]home_expand(). Used them for expanding mail directories.
authorTimo Sirainen <tss@iki.fi>
Sun, 31 Aug 2008 09:23:43 +0000 (12:23 +0300)
committerTimo Sirainen <tss@iki.fi>
Sun, 31 Aug 2008 09:23:43 +0000 (12:23 +0300)
--HG--
branch : HEAD

17 files changed:
src/lib-storage/index/cydir/cydir-storage.c
src/lib-storage/index/dbox/dbox-storage.c
src/lib-storage/index/maildir/maildir-storage.c
src/lib-storage/index/mbox/mbox-storage.c
src/lib-storage/index/raw/raw-storage.c
src/lib-storage/list/mailbox-list-fs-iter.c
src/lib-storage/list/mailbox-list-fs.c
src/lib-storage/list/mailbox-list-maildir-iter.c
src/lib-storage/list/mailbox-list-maildir.c
src/lib-storage/mail-user.c
src/lib-storage/mail-user.h
src/lib-storage/mailbox-list-private.h
src/lib-storage/mailbox-list.c
src/plugins/mbox-snarf/mbox-snarf-plugin.c
src/plugins/trash/trash-plugin.c
src/plugins/virtual/virtual-storage.c
src/plugins/zlib/zlib-plugin.c

index 15c36cec081d922156122db621c0556e8ac734b7..1314cf639cbfffeaab48b646c1a24fe8faaca94e 100644 (file)
@@ -34,10 +34,10 @@ static int cydir_list_iter_is_mailbox(struct mailbox_list_iterate_context *ctx,
 
 static int
 cydir_get_list_settings(struct mailbox_list_settings *list_set,
-                       const char *data, enum mail_storage_flags flags,
+                       const char *data, struct mail_storage *storage,
                        const char **layout_r, const char **error_r)
 {
-       bool debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
+       bool debug = (storage->flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
 
        *layout_r = "fs";
 
@@ -55,8 +55,8 @@ cydir_get_list_settings(struct mailbox_list_settings *list_set,
 
        if (debug)
                i_info("cydir: data=%s", data);
-       return mailbox_list_settings_parse(data, list_set, layout_r, NULL,
-                                          error_r);
+       return mailbox_list_settings_parse(data, list_set, storage->ns,
+                                          layout_r, NULL, error_r);
 }
 
 static struct mail_storage *cydir_alloc(void)
@@ -80,7 +80,7 @@ static int cydir_create(struct mail_storage *_storage, const char *data,
        struct stat st;
        const char *layout;
 
-       if (cydir_get_list_settings(&list_set, data, _storage->flags,
+       if (cydir_get_list_settings(&list_set, data, _storage,
                                    &layout, error_r) < 0)
                return -1;
        list_set.mail_storage_flags = &_storage->flags;
index 25dd5a4099f7d0195d13dad9b4dbabc4a903f1cf..de368a75137f1c299effc1cb8e59790acd12e570 100644 (file)
@@ -46,11 +46,11 @@ static int dbox_list_iter_is_mailbox(struct mailbox_list_iterate_context *ctx,
 
 static int
 dbox_get_list_settings(struct mailbox_list_settings *list_set,
-                      const char *data, enum mail_storage_flags flags,
+                      const char *data, struct mail_storage *storage,
                       const char **layout_r, const char **alt_dir_r,
                       const char **error_r)
 {
-       bool debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
+       bool debug = (storage->flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
 
        *layout_r = "fs";
 
@@ -68,8 +68,8 @@ dbox_get_list_settings(struct mailbox_list_settings *list_set,
 
        if (debug)
                i_info("dbox: data=%s", data);
-       return mailbox_list_settings_parse(data, list_set, layout_r, alt_dir_r,
-                                          error_r);
+       return mailbox_list_settings_parse(data, list_set, storage->ns,
+                                          layout_r, alt_dir_r, error_r);
 }
 
 static struct mail_storage *dbox_alloc(void)
@@ -93,7 +93,7 @@ static int dbox_create(struct mail_storage *_storage, const char *data,
        struct stat st;
        const char *layout, *alt_dir;
 
-       if (dbox_get_list_settings(&list_set, data, _storage->flags,
+       if (dbox_get_list_settings(&list_set, data, _storage,
                                   &layout, &alt_dir, error_r) < 0)
                return -1;
        list_set.mail_storage_flags = &_storage->flags;
index c2874d76b862b5e4968c69525c175fd50eb46263..d558336be2b735a98890628c64e225ffa5c03b73 100644 (file)
@@ -5,7 +5,6 @@
 #include "array.h"
 #include "hostpid.h"
 #include "str.h"
-#include "home-expand.h"
 #include "mkdir-parents.h"
 #include "unlink-directory.h"
 #include "unlink-old-files.h"
@@ -109,8 +108,8 @@ maildir_get_list_settings(struct mailbox_list_settings *list_set,
        } else {
                if (debug)
                        i_info("maildir: data=%s", data);
-               if (mailbox_list_settings_parse(data, list_set, layout_r, NULL,
-                                               error_r) < 0)
+               if (mailbox_list_settings_parse(data, list_set, storage->ns,
+                                               layout_r, NULL, error_r) < 0)
                        return -1;
        }
 
index 3a8a874f2cf9f5518bfd53db615551e1d6ec37f8..0ca1dd6e0ab2d8e20071b033d65f560e65886963 100644 (file)
@@ -7,7 +7,6 @@
 #include "restrict-access.h"
 #include "mkdir-parents.h"
 #include "unlink-directory.h"
-#include "home-expand.h"
 #include "mbox-storage.h"
 #include "mbox-lock.h"
 #include "mbox-file.h"
@@ -313,7 +312,7 @@ mbox_get_list_settings(struct mailbox_list_settings *list_set,
                if ((flags & MAIL_STORAGE_FLAG_NO_AUTODETECTION) == 0 &&
                    p == NULL && data[strlen(data)-1] != '/') {
                        /* if the data points to a file, treat it as an INBOX */
-                       data = home_expand(data);
+                       data = mail_user_home_expand(storage->ns->user, data);
                        if (stat(data, &st) < 0 || S_ISDIR(st.st_mode))
                                list_set->root_dir = data;
                        else {
@@ -322,6 +321,7 @@ mbox_get_list_settings(struct mailbox_list_settings *list_set,
                        }
                } else {
                        if (mailbox_list_settings_parse(data, list_set,
+                                                       storage->ns,
                                                        layout_r, NULL,
                                                        error_r) < 0)
                                return -1;
index df6e660d88348cac450af412c0d23fff7b48d56d..31a514268f3f6b5384cc7500ce62bcbf21dc3bd9 100644 (file)
@@ -27,10 +27,10 @@ static int raw_list_iter_is_mailbox(struct mailbox_list_iterate_context *ctx,
 
 static int
 raw_get_list_settings(struct mailbox_list_settings *list_set,
-                     const char *data, enum mail_storage_flags flags,
+                     const char *data, struct mail_storage *storage,
                      const char **layout_r, const char **error_r)
 {
-       bool debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
+       bool debug = (storage->flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
 
        *layout_r = "fs";
 
@@ -48,8 +48,8 @@ raw_get_list_settings(struct mailbox_list_settings *list_set,
 
        if (debug)
                i_info("raw: data=%s", data);
-       return mailbox_list_settings_parse(data, list_set, layout_r, NULL,
-                                          error_r);
+       return mailbox_list_settings_parse(data, list_set, storage->ns,
+                                          layout_r, NULL, error_r);
 }
 
 static struct mail_storage *raw_alloc(void)
@@ -73,7 +73,7 @@ static int raw_create(struct mail_storage *_storage, const char *data,
        struct stat st;
        const char *layout;
 
-       if (raw_get_list_settings(&list_set, data, _storage->flags,
+       if (raw_get_list_settings(&list_set, data, _storage,
                                  &layout, error_r) < 0)
                return -1;
        list_set.mail_storage_flags = &_storage->flags;
index 6ff2257db03dfc4201c2f779143ecc4afc551d92..b6352fdaf2ab61f2738d57d5da24fb7935961cf7 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "lib.h"
 #include "array.h"
-#include "home-expand.h"
 #include "unlink-directory.h"
 #include "imap-match.h"
 #include "mailbox-tree.h"
@@ -199,7 +198,8 @@ static const char *list_get_rootdir(struct fs_list_iterate_context *ctx,
                                        last = p;
                        }
                        name = p = t_strdup_until(name, last+1);
-                       if (home_try_expand(&name) == 0) {
+                       if (mailbox_list_try_get_absolute_path(ctx->ctx.list,
+                                                              &name)) {
                                *vpath = p;
                                return name;
                        }
index eff14501b3cec97b609f21e625d5652b94de31a5..ba7cdd00639a6d0a8bd7eddf16f458a69a819d74 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "lib.h"
 #include "hostpid.h"
-#include "home-expand.h"
 #include "mkdir-parents.h"
 #include "subscription-file.h"
 #include "mailbox-list-fs.h"
@@ -140,7 +139,6 @@ static const char *
 fs_list_get_path(struct mailbox_list *_list, const char *name,
                 enum mailbox_list_path_type type)
 {
-       struct fs_mailbox_list *list = (struct fs_mailbox_list *)_list;
        const struct mailbox_list_settings *set = &_list->set;
 
        if (name == NULL) {
@@ -161,12 +159,8 @@ fs_list_get_path(struct mailbox_list *_list, const char *name,
 
        i_assert(mailbox_list_is_valid_pattern(_list, name));
 
-       if ((list->list.flags & MAILBOX_LIST_FLAG_FULL_FS_ACCESS) != 0 &&
-           (*name == '/' || *name == '~')) {
-               if (home_try_expand(&name) == 0)
-                       return name;
-               /* fallback to using ~dir */
-       }
+       if (mailbox_list_try_get_absolute_path(_list, &name))
+               return name;
 
        switch (type) {
        case MAILBOX_LIST_PATH_TYPE_DIR:
index 2d3b40dbcc01f11e512b5e2edafb9ee025557e0f..8273e37277e38fbb5b4bcd7280ae432566a1a307 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "lib.h"
 #include "str.h"
-#include "home-expand.h"
 #include "imap-match.h"
 #include "mailbox-tree.h"
 #include "mailbox-list-subscriptions.h"
index ec25ba60f081316904af23facadbba27ec83b224..6bd9437c8078d988f69de392c300dc788bcc61fc 100644 (file)
@@ -3,7 +3,6 @@
 #include "lib.h"
 #include "array.h"
 #include "hostpid.h"
-#include "home-expand.h"
 #include "subscription-file.h"
 #include "mailbox-list-maildir.h"
 
@@ -71,7 +70,7 @@ maildir_list_get_absolute_path(struct mailbox_list *list, const char *name)
 {
        const char *p;
 
-       if (home_try_expand(&name) < 0) {
+       if (!mailbox_list_try_get_absolute_path(list, &name)) {
                /* fallback to using as ~name */
                return name;
        }
index d4eb16eec1451190abcee21e69550f18b6284015..c92e6c3e05dd3040fc7e56a543f0cbcb85f31ae0 100644 (file)
@@ -41,3 +41,22 @@ void mail_user_deinit(struct mail_user **_user)
        *_user = NULL;
        user->v.deinit(user);
 }
+
+const char *mail_user_home_expand(struct mail_user *user, const char *path)
+{
+       (void)mail_user_try_home_expand(user, &path);
+       return path;
+}
+
+int mail_user_try_home_expand(struct mail_user *user, const char **pathp)
+{
+       const char *path = *pathp;
+
+       if (path[0] == '~' && (path[1] == '/' || path[1] == '\0')) {
+               if (user->home == NULL)
+                       return -1;
+
+               *pathp = t_strconcat(user->home, path + 1, NULL);
+       }
+       return 0;
+}
index 79fc05b22e56e39b266e83aec4174ccdd7a8dabd..9d711af9ea3fb7e7d98114227e93a2a8dc5c5251 100644 (file)
@@ -36,4 +36,9 @@ extern void (*hook_mail_user_created)(struct mail_user *user);
 struct mail_user *mail_user_init(const char *username, const char *home);
 void mail_user_deinit(struct mail_user **user);
 
+/* Replace ~/ at the beginning of the path with the user's home directory. */
+const char *mail_user_home_expand(struct mail_user *user, const char *path);
+/* Returns 0 if ok, -1 if home directory isn't set. */
+int mail_user_try_home_expand(struct mail_user *user, const char **path);
+
 #endif
index 55d9514eb4beba05af6cb1e5569a4b64607a401a..0f043ddaa33b85765077a0b172df4ced74611340 100644 (file)
@@ -116,6 +116,7 @@ void mailbox_lists_deinit(void);
 
 int mailbox_list_settings_parse(const char *data,
                                struct mailbox_list_settings *set,
+                               struct mail_namespace *ns,
                                const char **layout, const char **alt_dir_r,
                                const char **error_r);
 
@@ -127,6 +128,8 @@ void mailbox_list_iter_update(struct mailbox_list_iter_update_context *ctx,
 
 bool mailbox_list_name_is_too_large(const char *name, char sep);
 enum mailbox_list_file_type mailbox_list_get_file_type(const struct dirent *d);
+bool mailbox_list_try_get_absolute_path(struct mailbox_list *list,
+                                       const char **name);
 
 void mailbox_list_clear_error(struct mailbox_list *list);
 void mailbox_list_set_error(struct mailbox_list *list,
index 224cf6ba0070d39734280ea352904f52aff2d7f8..f810ce289a531ca11388e4ac8da2ee67a3a35aa9 100644 (file)
@@ -108,17 +108,18 @@ int mailbox_list_alloc(const char *driver, struct mailbox_list **list_r,
        return 0;
 }
 
-static const char *fix_path(const char *path)
+static const char *fix_path(struct mail_namespace *ns, const char *path)
 {
        size_t len = strlen(path);
 
        if (len > 1 && path[len-1] == '/')
                path = t_strndup(path, len-1);
-       return home_expand(path);
+       return mail_user_home_expand(ns->user, path);
 }
 
 int mailbox_list_settings_parse(const char *data,
                                struct mailbox_list_settings *set,
+                               struct mail_namespace *ns,
                                const char **layout, const char **alt_dir_r,
                                const char **error_r)
 {
@@ -132,7 +133,7 @@ int mailbox_list_settings_parse(const char *data,
 
        /* <root dir> */
        tmp = t_strsplit(data, ":");
-       set->root_dir = fix_path(*tmp);
+       set->root_dir = fix_path(ns, *tmp);
        tmp++;
 
        for (; *tmp != NULL; tmp++) {
@@ -146,17 +147,17 @@ int mailbox_list_settings_parse(const char *data,
                }
 
                if (strcmp(key, "INBOX") == 0)
-                       set->inbox_path = fix_path(value);
+                       set->inbox_path = fix_path(ns, value);
                else if (strcmp(key, "INDEX") == 0)
-                       set->index_dir = fix_path(value);
+                       set->index_dir = fix_path(ns, value);
                else if (strcmp(key, "CONTROL") == 0)
-                       set->control_dir = fix_path(value);
+                       set->control_dir = fix_path(ns, value);
                else if (strcmp(key, "ALT") == 0 && alt_dir_r != NULL)
-                       *alt_dir_r = fix_path(value);
+                       *alt_dir_r = fix_path(ns, value);
                else if (strcmp(key, "LAYOUT") == 0)
                        *layout = value;
                else if (strcmp(key, "SUBSCRIPTIONS") == 0)
-                       set->subscription_fname = fix_path(value);
+                       set->subscription_fname = fix_path(ns, value);
                else if (strcmp(key, "DIRNAME") == 0)
                        set->maildir_name = value;
                else {
@@ -692,6 +693,31 @@ mailbox_list_get_file_type(const struct dirent *d ATTR_UNUSED)
        return type;
 }
 
+bool mailbox_list_try_get_absolute_path(struct mailbox_list *list,
+                                       const char **name)
+{
+       if ((list->flags & MAILBOX_LIST_FLAG_FULL_FS_ACCESS) == 0)
+               return FALSE;
+
+       if (**name == '/')
+               return TRUE;
+       if (**name != '~')
+               return FALSE;
+
+       /* try to expand home directory */
+       if ((*name)[1] == '/') {
+               /* ~/dir - use the configured home directory */
+               if (mail_user_try_home_expand(list->ns->user, name) == 0)
+                       return TRUE;
+       } else {
+               /* ~otheruser/dir - assume we're using system users */
+               if (home_try_expand(name) == 0)
+                       return TRUE;
+       }
+       /* fallback to using ~dir */
+       return FALSE;
+}
+
 const char *mailbox_list_get_last_error(struct mailbox_list *list,
                                        enum mail_error *error_r)
 {
index c005abb9921bb818b58959857f19505c2840e9fd..f01d4994bd42e834597097b5834064c64313f09e 100644 (file)
@@ -168,10 +168,11 @@ mbox_snarf_mailbox_open(struct mail_storage *storage, const char *name,
 static void mbox_snarf_mail_storage_created(struct mail_storage *storage)
 {
        struct mbox_snarf_mail_storage *mstorage;
+       const char *path;
 
+       path = mail_user_home_expand(storage->ns->user, getenv("MBOX_SNARF"));
        mstorage = p_new(storage->pool, struct mbox_snarf_mail_storage, 1);
-       mstorage->snarf_inbox_path =
-               p_strdup(storage->pool, home_expand(getenv("MBOX_SNARF")));
+       mstorage->snarf_inbox_path = p_strdup(storage->pool, path);
        mstorage->module_ctx.super = storage->v;
        storage->v.mailbox_open = mbox_snarf_mailbox_open;
 
index e3e340912bc58289a6bbd39e98ed5e644c8b06cb..0bc640d67d4e95961ed3cd6d8ab38b295e96cb15 100644 (file)
@@ -3,7 +3,6 @@
 #include "lib.h"
 #include "array.h"
 #include "istream.h"
-#include "home-expand.h"
 #include "mail-namespace.h"
 #include "mail-search-build.h"
 #include "quota-private.h"
index 81da117df60b681b7015ee8163657b5d24141134..d58a66887b35effb61d92571ca537b5ebe7e8fea 100644 (file)
@@ -38,10 +38,10 @@ virtual_list_iter_is_mailbox(struct mailbox_list_iterate_context *ctx,
 
 static int
 virtual_get_list_settings(struct mailbox_list_settings *list_set,
-                         const char *data, enum mail_storage_flags flags,
+                         const char *data, struct mail_storage *storage,
                          const char **layout_r, const char **error_r)
 {
-       bool debug = (flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
+       bool debug = (storage->flags & MAIL_STORAGE_FLAG_DEBUG) != 0;
 
        *layout_r = "fs";
 
@@ -59,8 +59,8 @@ virtual_get_list_settings(struct mailbox_list_settings *list_set,
 
        if (debug)
                i_info("virtual: data=%s", data);
-       return mailbox_list_settings_parse(data, list_set, layout_r, NULL,
-                                          error_r);
+       return mailbox_list_settings_parse(data, list_set, storage->ns,
+                                          layout_r, NULL, error_r);
 }
 
 static struct mail_storage *virtual_alloc(void)
@@ -85,7 +85,7 @@ static int virtual_create(struct mail_storage *_storage, const char *data,
        struct stat st;
        const char *layout;
 
-       if (virtual_get_list_settings(&list_set, data, _storage->flags,
+       if (virtual_get_list_settings(&list_set, data, _storage,
                                      &layout, error_r) < 0)
                return -1;
        list_set.mail_storage_flags = &_storage->flags;
index bc132791da5051d8e874f3328ff2694a98c974cd..980f5d6055f2574c88fa8f9b8331fb799c826548 100644 (file)
@@ -3,7 +3,6 @@
 #include "lib.h"
 #include "array.h"
 #include "istream-zlib.h"
-#include "home-expand.h"
 #include "istream.h"
 #include "maildir/maildir-storage.h"
 #include "maildir/maildir-uidlist.h"