bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen#include "lib.h"
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen#include "array.h"
70ead6466f9baa8294e71fc2fba0a4f54f488b5eTimo Sirainen#include "hostpid.h"
a020eb653b2620a989e4795adceb6136037327b2Timo Sirainen#include "ioloop.h"
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen#include "net.h"
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen#include "module-dir.h"
57f5683fd9dc9bc79816c418bb30fdbc33b68a8cTimo Sirainen#include "home-expand.h"
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen#include "file-create-locked.h"
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen#include "safe-mkstemp.h"
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen#include "str.h"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#include "strescape.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "var-expand.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "settings-parser.h"
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen#include "iostream-ssl.h"
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen#include "fs-api.h"
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen#include "auth-master.h"
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#include "master-service.h"
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen#include "dict.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "mail-storage-settings.h"
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen#include "mail-storage-private.h"
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen#include "mail-storage-service.h"
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen#include "mail-namespace.h"
70ead6466f9baa8294e71fc2fba0a4f54f488b5eTimo Sirainen#include "mail-storage.h"
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen#include "mailbox-list-private.h"
1ae5d61ec366fdb2f3c5b150ca378d6141b0f4bdTimo Sirainen#include "mail-autoexpunge.h"
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen#include "mail-user.h"
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainenstruct auth_master_connection *mail_user_auth_master_conn;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen{
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen if (user->_attr_dict != NULL) {
c4478af52de63804efef2055580adf1dfc8679c6Timo Sirainen dict_wait(user->_attr_dict);
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen dict_deinit(&user->_attr_dict);
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen }
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen mail_namespaces_deinit(&user->namespaces);
d1bf4ae66b8bf3b9e28df1823d6d4adda2b923b6Timo Sirainen if (user->_service_user != NULL)
d1bf4ae66b8bf3b9e28df1823d6d4adda2b923b6Timo Sirainen mail_storage_service_user_unref(&user->_service_user);
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen}
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipekstatic void mail_user_deinit_pre_base(struct mail_user *user ATTR_UNUSED)
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek{
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek}
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek
379175cfba8150d481d9898b78330b719d128d84Timo Sirainenstatic void mail_user_stats_fill_base(struct mail_user *user ATTR_UNUSED,
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen struct stats *stats ATTR_UNUSED)
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen{
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen}
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainenstatic struct mail_user *
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainenmail_user_alloc_int(struct event *parent_event,
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen const char *username,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen const struct setting_parser_info *set_info,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen const struct mail_user_settings *set, pool_t pool)
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen{
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen struct mail_user *user;
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen const char *error;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen i_assert(username != NULL);
4c892b0d94c5b1d6853dbe8e0b38059ea5b08ecaTimo Sirainen i_assert(*username != '\0');
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen user = p_new(pool, struct mail_user, 1);
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen user->pool = pool;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen user->refcount = 1;
4c892b0d94c5b1d6853dbe8e0b38059ea5b08ecaTimo Sirainen user->username = p_strdup(pool, username);
2fb9ae42f9e36388ec6db24188b9108434043fd0Timo Sirainen user->set_info = set_info;
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen user->unexpanded_set = set;
3c2e763ab035567b2081a805648537b27e7cbbcfTimo Sirainen user->set = settings_dup_with_pointers(set_info, user->unexpanded_set, pool);
013a8a91c83c6ea24bc75322b81235f19e26fa8fTimo Sirainen user->service = master_service_get_name(master_service);
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen user->default_normalizer = uni_utf8_to_decomposed_titlecase;
a020eb653b2620a989e4795adceb6136037327b2Timo Sirainen user->session_create_time = ioloop_time;
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen user->event = event_create(parent_event);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen event_add_category(user->event, &event_category_storage);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen event_add_str(user->event, "user", username);
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen /* check settings so that the duplicated structure will again
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen contain the parsed fields */
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen if (!settings_check(set_info, pool, user->set, &error))
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen i_panic("Settings check unexpectedly failed: %s", error);
a8d47e2427558d5011dfc75694b704760c1ef8baTimo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen user->v.deinit = mail_user_deinit_base;
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek user->v.deinit_pre = mail_user_deinit_pre_base;
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen user->v.stats_fill = mail_user_stats_fill_base;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return user;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainenstruct mail_user *
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainenmail_user_alloc_nodup_set(struct event *parent_event,
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen const char *username,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen const struct setting_parser_info *set_info,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen const struct mail_user_settings *set)
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen{
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen pool_t pool;
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen pool = pool_alloconly_create(MEMPOOL_GROWING"mail user", 16*1024);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen return mail_user_alloc_int(parent_event, username, set_info, set, pool);
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen}
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainenstruct mail_user *mail_user_alloc(struct event *parent_event,
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen const char *username,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen const struct setting_parser_info *set_info,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen const struct mail_user_settings *set)
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen{
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen pool_t pool;
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen pool = pool_alloconly_create(MEMPOOL_GROWING"mail user", 16*1024);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen return mail_user_alloc_int(parent_event, username, set_info,
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen settings_dup(set_info, set, pool), pool);
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen}
34512eaad8b1b2f929e6d6e3a2f7252c29fba97bTimo Sirainen
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainenstatic void
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainenmail_user_expand_plugins_envs(struct mail_user *user)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen{
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char **envs, *home, *error;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen string_t *str;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen unsigned int i, count;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (!array_is_created(&user->set->plugin_envs))
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen return;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen str = t_str_new(256);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen envs = array_get_modifiable(&user->set->plugin_envs, &count);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen i_assert((count % 2) == 0);
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen for (i = 0; i < count; i += 2) {
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen if (user->_home == NULL &&
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen var_has_key(envs[i+1], 'h', "home") &&
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen mail_user_get_home(user, &home) <= 0) {
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen user->error = p_strdup_printf(user->pool,
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen "userdb didn't return a home directory, "
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen "but plugin setting %s used it (%%h): %s",
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen envs[i], envs[i+1]);
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen return;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen }
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen str_truncate(str, 0);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (var_expand_with_funcs(str, envs[i+1],
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_table(user),
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_func_table, user,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen &error) <= 0) {
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen user->error = p_strdup_printf(user->pool,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen "Failed to expand plugin setting %s = '%s': %s",
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen envs[i], envs[i+1], error);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen }
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen envs[i+1] = p_strdup(user->pool, str_c(str));
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen }
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen}
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenint mail_user_init(struct mail_user *user, const char **error_r)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen{
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen const struct mail_storage_settings *mail_set;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char *home, *key, *value, *error;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen bool need_home_dir;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen need_home_dir = user->_home == NULL &&
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen settings_vars_have_key(user->set_info, user->set,
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen 'h', "home", &key, &value);
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen if (need_home_dir && mail_user_get_home(user, &home) <= 0) {
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen user->error = p_strdup_printf(user->pool,
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen "userdb didn't return a home directory, "
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen "but %s used it (%%h): %s", key, value);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen }
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen /* expand settings after we can expand %h */
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (settings_var_expand_with_funcs(user->set_info, user->set,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen user->pool, mail_user_var_expand_table(user),
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_func_table, user,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen &error) <= 0) {
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen user->error = p_strdup_printf(user->pool,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen "Failed to expand settings: %s", error);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen }
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen user->settings_expanded = TRUE;
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen mail_user_expand_plugins_envs(user);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen /* autocreated users for shared mailboxes need to be fully initialized
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen if they don't exist, since they're going to be used anyway */
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen if (user->error == NULL || user->nonexistent) {
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen mail_set = mail_user_set_get_storage_set(user);
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen user->mail_debug = mail_set->mail_debug;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen user->initialized = TRUE;
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen hook_mail_user_created(user);
7b64db32b95286235612eebb5d37d296a49306f7Timo Sirainen }
a26b7e87b4157cfa800f9bcd8c4c044462d21268Timo Sirainen
a26b7e87b4157cfa800f9bcd8c4c044462d21268Timo Sirainen if (user->error != NULL) {
a26b7e87b4157cfa800f9bcd8c4c044462d21268Timo Sirainen *error_r = t_strdup(user->error);
a26b7e87b4157cfa800f9bcd8c4c044462d21268Timo Sirainen return -1;
a26b7e87b4157cfa800f9bcd8c4c044462d21268Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return 0;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen}
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainenvoid mail_user_ref(struct mail_user *user)
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen{
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen i_assert(user->refcount > 0);
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen user->refcount++;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen}
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainenvoid mail_user_unref(struct mail_user **_user)
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen{
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen struct mail_user *user = *_user;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen i_assert(user->refcount > 0);
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen *_user = NULL;
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen if (user->refcount > 1) {
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen user->refcount--;
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen return;
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen }
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen
4f2fa2e5f5a27e1d59dd38fbbf38e420e006596dTimo Sirainen user->deinitializing = TRUE;
4f2fa2e5f5a27e1d59dd38fbbf38e420e006596dTimo Sirainen
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek /* call deinit() and deinit_pre() with refcount=1, otherwise we may
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek assert-crash in mail_user_ref() that is called by some handlers. */
5231ae561283595a73e9d838bdf4eb5d1b074679Josef 'Jeff' Sipek user->v.deinit_pre(user);
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen user->v.deinit(user);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen event_unref(&user->event);
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen i_assert(user->refcount == 1);
e51cfb5506de764499cb5b81a098b23cf46f90f1Timo Sirainen pool_unref(&user->pool);
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen}
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen{
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen struct mail_namespace *ns;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen return ns->owner;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen }
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen return NULL;
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen}
a443e5aaf632257bfd1e7aa9b3c42c09512bbe43Timo Sirainen
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Boschstatic void
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Boschmail_user_connection_init_from(struct mail_user_connection_data *conn,
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch pool_t pool, const struct mail_user_connection_data *src)
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch{
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch *conn = *src;
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch if (src->local_ip != NULL && src->local_ip->family != 0) {
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch conn->local_ip = p_new(pool, struct ip_addr, 1);
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch *conn->local_ip = *src->local_ip;
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch }
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch if (src->remote_ip != NULL && src->remote_ip->family != 0) {
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch conn->remote_ip = p_new(pool, struct ip_addr, 1);
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch *conn->remote_ip = *src->remote_ip;
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch }
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch}
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch
13b063ba3ea51256fd97d7fa883f14cb08842b0dTimo Sirainenvoid mail_user_set_vars(struct mail_user *user, const char *service,
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch const struct mail_user_connection_data *conn)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen{
013a8a91c83c6ea24bc75322b81235f19e26fa8fTimo Sirainen i_assert(service != NULL);
013a8a91c83c6ea24bc75322b81235f19e26fa8fTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen user->service = p_strdup(user->pool, service);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen event_add_str(user->event, "service", service);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch mail_user_connection_init_from(&user->conn, user->pool, conn);
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen if (user->conn.local_ip != NULL)
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen event_add_str(user->event, "local_ip",
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen net_ip2addr(user->conn.local_ip));
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen if (user->conn.remote_ip != NULL)
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen event_add_str(user->event, "remote_ip",
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen net_ip2addr(user->conn.remote_ip));
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenconst struct var_expand_table *
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenmail_user_var_expand_table(struct mail_user *user)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen{
f9142439f2b5e86065af7420e80fe52835227dc8Timo Sirainen /* use a cached table, unless home directory has been set afterwards */
f9142439f2b5e86065af7420e80fe52835227dc8Timo Sirainen if (user->var_expand_table != NULL &&
f9142439f2b5e86065af7420e80fe52835227dc8Timo Sirainen user->var_expand_table[4].value == user->_home)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return user->var_expand_table;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen const char *username =
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen p_strdup(user->pool, t_strcut(user->username, '@'));
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen const char *domain = i_strchr_to_next(user->username, '@');
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch const char *local_ip = user->conn.local_ip == NULL ? NULL :
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch p_strdup(user->pool, net_ip2addr(user->conn.local_ip));
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch const char *remote_ip = user->conn.remote_ip == NULL ? NULL :
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch p_strdup(user->pool, net_ip2addr(user->conn.remote_ip));
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen const char *auth_user, *auth_username, *auth_domain;
2f90189c6ee66a17f7bf838a8eb8a69868630fb8Timo Sirainen if (user->auth_user == NULL) {
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen auth_user = user->username;
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen auth_username = username;
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen auth_domain = domain;
2f90189c6ee66a17f7bf838a8eb8a69868630fb8Timo Sirainen } else {
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen auth_user = user->auth_user;
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen auth_username =
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen p_strdup(user->pool, t_strcut(user->auth_user, '@'));
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen auth_domain = i_strchr_to_next(user->auth_user, '@');
2f90189c6ee66a17f7bf838a8eb8a69868630fb8Timo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen const struct var_expand_table stack_tab[] = {
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'u', user->username, "user" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'n', username, "username" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'd', domain, "domain" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 's', user->service, "service" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'h', user->_home /* don't look it up unless we need it */, "home" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'l', local_ip, "lip" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'r', remote_ip, "rip" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'p', my_pid, "pid" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { 'i', p_strdup(user->pool, dec2str(user->uid)), "uid" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { '\0', p_strdup(user->pool, dec2str(user->gid)), "gid" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { '\0', user->session_id, "session" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { '\0', auth_user, "auth_user" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { '\0', auth_username, "auth_username" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { '\0', auth_domain, "auth_domain" },
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen /* NOTE: keep this synced with imap-hibernate's
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen imap_client_var_expand_table() */
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen { '\0', NULL, NULL }
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen };
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen struct var_expand_table *tab;
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen tab = p_malloc(user->pool, sizeof(stack_tab));
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen memcpy(tab, stack_tab, sizeof(stack_tab));
acfda38b75d0f0e899ef692fef01593bd56ed85eTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen user->var_expand_table = tab;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return user->var_expand_table;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen}
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenstatic int
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenmail_user_var_expand_func_userdb(const char *data, void *context,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **value_r,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **error_r ATTR_UNUSED)
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen{
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen struct mail_user *user = context;
d23dfc385f22d7a2c466d29501c9e0ce5a243deeTimo Sirainen
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen *value_r = mail_storage_service_fields_var_expand(data, user->userdb_fields);
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen return 1;
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen}
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen{
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen user->_home = p_strdup(user->pool, home);
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen user->home_looked_up = TRUE;
d80f37f025593d959bdfa9c378915e4322f4f504Timo Sirainen}
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
94ce7e7700cda14a8342cb08e7285507b4b531daTimo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
94ce7e7700cda14a8342cb08e7285507b4b531daTimo Sirainen struct mail_namespace **namespaces)
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen{
94ce7e7700cda14a8342cb08e7285507b4b531daTimo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen for (; ns != NULL; ns = next) {
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen next = ns->next;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen tmp = &user->namespaces;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen for (; *tmp != NULL; tmp = &(*tmp)->next) {
fc4e528886f47bf4d2dd0276fb8705c085c3b9aeTimo Sirainen i_assert(*tmp != ns);
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen break;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen }
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen ns->next = *tmp;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen *tmp = ns;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen }
94ce7e7700cda14a8342cb08e7285507b4b531daTimo Sirainen *namespaces = user->namespaces;
311d3dd2078c1b711a0cef013ba43a94078c115cTimo Sirainen
311d3dd2078c1b711a0cef013ba43a94078c115cTimo Sirainen T_BEGIN {
311d3dd2078c1b711a0cef013ba43a94078c115cTimo Sirainen hook_mail_namespaces_added(user->namespaces);
311d3dd2078c1b711a0cef013ba43a94078c115cTimo Sirainen } T_END;
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen}
4654f788834c9d7920a351306b89cf5d1c21772eTimo Sirainen
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen{
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen struct mail_namespace *ns, *next;
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen
b06633c63fde22b6c8837ae70b2f95fe60075b0aTimo Sirainen /* drop all autocreated unusable (typically shared) namespaces.
b06633c63fde22b6c8837ae70b2f95fe60075b0aTimo Sirainen don't drop the autocreated prefix="" namespace that we explicitly
b06633c63fde22b6c8837ae70b2f95fe60075b0aTimo Sirainen created for being the fallback namespace. */
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen next = ns->next;
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
b06633c63fde22b6c8837ae70b2f95fe60075b0aTimo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0 &&
b06633c63fde22b6c8837ae70b2f95fe60075b0aTimo Sirainen ns->prefix_len > 0)
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen mail_namespace_destroy(ns);
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen }
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen}
3398d5e2b883812de5d569721c8294b581e1d9e6Timo Sirainen
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen{
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen (void)mail_user_try_home_expand(user, &path);
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen return path;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen}
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainenstatic int mail_user_userdb_lookup_home(struct mail_user *user)
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen{
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen struct auth_user_info info;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen struct auth_user_reply reply;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen pool_t userdb_pool;
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen const char *username, *const *fields;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen int ret;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen i_assert(!user->home_looked_up);
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&info);
5736aef6d0abe6796e57c2eda68f5c25db677918Timo Sirainen info.service = user->service;
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch if (user->conn.local_ip != NULL)
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch info.local_ip = *user->conn.local_ip;
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch if (user->conn.remote_ip != NULL)
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch info.remote_ip = *user->conn.remote_ip;
fc464e5b2b2ab4d415a5d5b90ce4475d34620a75Timo Sirainen
5e751dbaecf7c337abc149f328c4a13ee5c15134Timo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 2048);
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen ret = auth_master_user_lookup(mail_user_auth_master_conn,
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen user->username, &info, userdb_pool,
4da8c6cdefabd31262318c32da3c13de1d9ea953Timo Sirainen &username, &fields);
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen if (ret > 0) {
9f10cc61ec303351b43e54155c86699ef53cb8beTimo Sirainen auth_user_fields_parse(fields, userdb_pool, &reply);
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen user->_home = p_strdup(user->pool, reply.home);
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen }
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen pool_unref(&userdb_pool);
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen return ret;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen}
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainenstatic bool mail_user_get_mail_home(struct mail_user *user)
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen{
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char *error, *home = user->set->mail_home;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen string_t *str;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen if (user->settings_expanded) {
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen user->_home = home[0] != '\0' ? home : NULL;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return TRUE;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen }
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen /* we're still initializing user. need to do the expansion ourself. */
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen i_assert(home[0] == SETTING_STRVAR_UNEXPANDED[0]);
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen home++;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen if (home[0] == '\0')
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return TRUE;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen str = t_str_new(128);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (var_expand_with_funcs(str, home,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_table(user),
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen mail_user_var_expand_func_table, user,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen &error) <= 0) {
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen i_error("Failed to expand mail_home=%s: %s", home, error);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return FALSE;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen }
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen user->_home = p_strdup(user->pool, str_c(str));
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return TRUE;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen}
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen{
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen int ret;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen if (user->home_looked_up) {
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen *home_r = user->_home;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen return user->_home != NULL ? 1 : 0;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen }
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen if (mail_user_auth_master_conn == NULL) {
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen /* no userdb connection. we can only use mail_home setting. */
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (!mail_user_get_mail_home(user))
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return -1;
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen } else if ((ret = mail_user_userdb_lookup_home(user)) < 0) {
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen /* userdb lookup failed */
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen return -1;
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen } else if (ret == 0) {
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen /* user doesn't exist */
117a55d4260651770705ecb96f68be2dab03b99bTimo Sirainen user->nonexistent = TRUE;
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen } else if (user->_home == NULL) {
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen /* no home returned by userdb lookup, fallback to
326c86b1cdb555957b236958e17142e82e34074eTimo Sirainen mail_home setting. */
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (!mail_user_get_mail_home(user))
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return -1;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen }
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen user->home_looked_up = TRUE;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen *home_r = user->_home;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen return user->_home != NULL ? 1 : 0;
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen}
39993536eaef0a23954105e41040dcf88afd2e7eTimo Sirainen
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainenbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen{
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen const char *const *plugins;
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen bool ret;
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen T_BEGIN {
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen ret = str_array_find(plugins, module_get_plugin_name(module));
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen } T_END;
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen return ret;
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen}
dd7cbb32412c2f4d2d223af66672535bc1237246Timo Sirainen
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wangbool mail_user_plugin_getenv_bool(struct mail_user *user, const char *name)
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang{
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainen return mail_user_set_plugin_getenv_bool(user->set, name);
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainen}
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainen
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainenbool mail_user_set_plugin_getenv_bool(const struct mail_user_settings *set,
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainen const char *name)
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainen{
287b5ba23f182fd98e7a6ba3a63669c1572f2ca4Timo Sirainen const char *env = mail_user_set_plugin_getenv(set, name);
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang if (env == NULL)
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang return FALSE;
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang switch (env[0]) {
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang case 'n':
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang case 'N':
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang case '0':
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang case 'f':
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang case 'F':
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang return FALSE;
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang }
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang //any other value including empty string will be treated as TRUE.
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang return TRUE;
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang}
527c2b071dca35b91648344e867ab1af9c988281Baofeng Wang
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainenconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen{
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen return mail_user_set_plugin_getenv(user->set, name);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen}
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenconst char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen const char *name)
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen{
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen const char *const *envs;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int i, count;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen if (!array_is_created(&set->plugin_envs))
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen return NULL;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen envs = array_get(&set->plugin_envs, &count);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen for (i = 0; i < count; i += 2) {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen if (strcmp(envs[i], name) == 0)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen return envs[i+1];
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen }
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen return NULL;
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen}
0df9428baed48afaff90b4d4f03792d2fd756a43Timo Sirainen
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen{
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen const char *home, *path = *pathp;
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
08a33e7c0cf5ab2c4a0c96a55056cc3251d14c5eTimo Sirainen if (*path != '~') {
cb17980a661554ebb3fd099c77e92a5be4d304ecTimo Sirainen /* no need to expand home */
cb17980a661554ebb3fd099c77e92a5be4d304ecTimo Sirainen return 0;
cb17980a661554ebb3fd099c77e92a5be4d304ecTimo Sirainen }
cb17980a661554ebb3fd099c77e92a5be4d304ecTimo Sirainen
d2d5871fa9e7226df694ff7a4be511167b35b305Timo Sirainen if (mail_user_get_home(user, &home) <= 0)
d2c853636ec2d99c9f96da877ff520a3b86a18baTimo Sirainen return -1;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
57f5683fd9dc9bc79816c418bb30fdbc33b68a8cTimo Sirainen path = home_expand_tilde(path, home);
57f5683fd9dc9bc79816c418bb30fdbc33b68a8cTimo Sirainen if (path == NULL)
57f5683fd9dc9bc79816c418bb30fdbc33b68a8cTimo Sirainen return -1;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
57f5683fd9dc9bc79816c418bb30fdbc33b68a8cTimo Sirainen *pathp = path;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen return 0;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen}
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainen
55a14bce15b9f44441b5f56616d73651a294d770Timo Sirainenvoid mail_user_set_get_temp_prefix(string_t *dest,
55a14bce15b9f44441b5f56616d73651a294d770Timo Sirainen const struct mail_user_settings *set)
b16ee3cbbcd18cb86f2f73b5cc163ebfb995ffafTimo Sirainen{
55a14bce15b9f44441b5f56616d73651a294d770Timo Sirainen str_append(dest, set->mail_temp_dir);
55a14bce15b9f44441b5f56616d73651a294d770Timo Sirainen str_append(dest, "/dovecot.");
55a14bce15b9f44441b5f56616d73651a294d770Timo Sirainen str_append(dest, master_service_get_name(master_service));
55a14bce15b9f44441b5f56616d73651a294d770Timo Sirainen str_append_c(dest, '.');
b16ee3cbbcd18cb86f2f73b5cc163ebfb995ffafTimo Sirainen}
b16ee3cbbcd18cb86f2f73b5cc163ebfb995ffafTimo Sirainen
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainenint mail_user_lock_file_create(struct mail_user *user, const char *lock_fname,
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen unsigned int lock_secs,
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen struct file_lock **lock_r, const char **error_r)
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen{
9eb5adf5cc773c1e27ae30dd6aa2bc150ae81ac1Timo Sirainen const char *home, *path;
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen int ret;
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen if ((ret = mail_user_get_home(user, &home)) < 0) {
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen /* home lookup failed - shouldn't really happen */
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen *error_r = "Failed to lookup home directory";
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen errno = EINVAL;
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen return -1;
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen }
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen if (ret == 0) {
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen *error_r = "User has no home directory";
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen errno = EINVAL;
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen return -1;
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen }
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen const struct mail_storage_settings *mail_set =
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen mail_user_set_get_storage_set(user);
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen struct file_create_settings lock_set = {
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen .lock_timeout_secs = lock_secs,
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen .lock_method = mail_set->parsed_lock_method,
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen };
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen struct mailbox_list *inbox_list =
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen mail_namespace_find_inbox(user->namespaces)->list;
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen if (inbox_list->set.volatile_dir == NULL)
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen path = t_strdup_printf("%s/%s", home, lock_fname);
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen else {
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen path = t_strdup_printf("%s/%s", inbox_list->set.volatile_dir,
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen lock_fname);
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen lock_set.mkdir_mode = 0700;
fd8b93ca5cbeba64fe6fa5dc98a3e580fb046791Timo Sirainen }
74943466e541d8bfe3916bb0eb9fb9cb0599dfe7Timo Sirainen return mail_storage_lock_create(path, &lock_set, mail_set, lock_r, error_r);
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen}
55cec51dba1352482e93db30d7ced6bc4fe15f3aTimo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenconst char *mail_user_get_anvil_userip_ident(struct mail_user *user)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen{
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch if (user->conn.remote_ip == NULL)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen return NULL;
60670187b0dd0e7f23f99a58feab11b862ad77acStephan Bosch return t_strconcat(net_ip2addr(user->conn.remote_ip), "/",
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen str_tabescape(user->username), NULL);
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen}
318ef3683d67683173f1b552cf5f9af4375b3017Timo Sirainen
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainenstatic void
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainenmail_user_try_load_class_plugin(struct mail_user *user, const char *name)
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen{
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen struct module_dir_load_settings mod_set;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen struct module *module;
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen size_t name_len = strlen(name);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&mod_set);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mod_set.abi_version = DOVECOT_ABI_VERSION;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mod_set.binary_name = master_service_get_name(master_service);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mod_set.setting_name = "<built-in storage lookup>";
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mod_set.require_init_funcs = TRUE;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mod_set.debug = user->mail_debug;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mail_storage_service_modules =
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen module_dir_load_missing(mail_storage_service_modules,
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen user->set->mail_plugin_dir,
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen name, &mod_set);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen /* initialize the module (and only this module!) immediately so that
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen the class gets registered */
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen for (module = mail_storage_service_modules; module != NULL; module = module->next) {
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen if (strncmp(module->name, name, name_len) == 0 &&
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen strcmp(module->name + name_len, "_plugin") == 0) {
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen if (!module->initialized) {
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen module->initialized = TRUE;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen module->init(module);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen }
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen break;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen }
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen }
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen}
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainenstruct mail_storage *
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainenmail_user_get_storage_class(struct mail_user *user, const char *name)
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen{
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen struct mail_storage *storage;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen storage = mail_storage_find_class(name);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen if (storage == NULL || storage->v.alloc != NULL)
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen return storage;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen /* it's implemented by a plugin. load it and check again. */
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen mail_user_try_load_class_plugin(user, name);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen storage = mail_storage_find_class(name);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen if (storage != NULL && storage->v.alloc == NULL) {
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen i_error("Storage driver '%s' exists as a stub, "
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen "but its plugin couldn't be loaded", name);
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen return NULL;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen }
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen return storage;
87ca4b209c10954826b878da165d303d9b4dc5a2Timo Sirainen}
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainenstruct mail_user *mail_user_dup(struct mail_user *user)
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen{
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen struct mail_user *user2;
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen user2 = mail_user_alloc(event_get_parent(user->event), user->username,
677cc0d62b41c7506246bf1b992a400a11896474Timo Sirainen user->set_info, user->unexpanded_set);
d1bf4ae66b8bf3b9e28df1823d6d4adda2b923b6Timo Sirainen if (user2->_service_user != NULL) {
d1bf4ae66b8bf3b9e28df1823d6d4adda2b923b6Timo Sirainen user2->_service_user = user->_service_user;
d1bf4ae66b8bf3b9e28df1823d6d4adda2b923b6Timo Sirainen mail_storage_service_user_ref(user2->_service_user);
d1bf4ae66b8bf3b9e28df1823d6d4adda2b923b6Timo Sirainen }
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen if (user->_home != NULL)
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen mail_user_set_home(user2, user->_home);
ff4ba036fcdf28d4a1a7e3c1c6c0c71793547bd6Stephan Bosch mail_user_set_vars(user2, user->service, &user->conn);
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->uid = user->uid;
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->gid = user->gid;
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->anonymous = user->anonymous;
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->admin = user->admin;
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->auth_token = p_strdup(user2->pool, user->auth_token);
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->auth_user = p_strdup(user2->pool, user->auth_user);
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen user2->session_id = p_strdup(user2->pool, user->session_id);
a020eb653b2620a989e4795adceb6136037327b2Timo Sirainen user2->session_create_time = user->session_create_time;
9f7441a47863d44ec303c7980b499b46b3d1671bTimo Sirainen user2->userdb_fields = user->userdb_fields == NULL ? NULL :
9f7441a47863d44ec303c7980b499b46b3d1671bTimo Sirainen p_strarray_dup(user2->pool, user->userdb_fields);
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen return user2;
1e40531c1de45bc87e72a9d5866ff2af79b63cebTimo Sirainen}
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Boschvoid mail_user_init_ssl_client_settings(struct mail_user *user,
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen struct ssl_iostream_settings *ssl_set)
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen{
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen const struct mail_storage_settings *mail_set =
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen mail_user_set_get_storage_set(user);
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch ssl_set->ca_dir = mail_set->ssl_client_ca_dir;
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch ssl_set->ca_file = mail_set->ssl_client_ca_file;
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch}
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Boschvoid mail_user_init_fs_settings(struct mail_user *user,
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch struct fs_settings *fs_set,
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch struct ssl_iostream_settings *ssl_set)
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch{
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen fs_set->username = user->username;
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen fs_set->session_id = user->session_id;
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen fs_set->base_dir = user->set->base_dir;
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen fs_set->temp_dir = user->set->mail_temp_dir;
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen fs_set->debug = user->mail_debug;
c981098a515c62fdfb3261e532b5d6163a0f8946Timo Sirainen fs_set->enable_timing = user->stats_enabled;
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen fs_set->ssl_client_set = ssl_set;
26bdbffc35ee46779f15a537a757ed937a4d1c91Stephan Bosch mail_user_init_ssl_client_settings(user, ssl_set);
1701b354e81ff1dfd0b6c7bb4412b8d9c2b9f986Timo Sirainen}
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen
379175cfba8150d481d9898b78330b719d128d84Timo Sirainenvoid mail_user_stats_fill(struct mail_user *user, struct stats *stats)
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen{
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen user->v.stats_fill(user, stats);
379175cfba8150d481d9898b78330b719d128d84Timo Sirainen}
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainenstatic const struct var_expand_func_table mail_user_var_expand_func_table_arr[] = {
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen { "userdb", mail_user_var_expand_func_userdb },
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen { NULL, NULL }
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen};
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainenconst struct var_expand_func_table *mail_user_var_expand_func_table =
23152672e3ad7f3512c11df43e8dabba4fe6407dTimo Sirainen mail_user_var_expand_func_table_arr;