mail-user.c revision a8d47e2427558d5011dfc75694b704760c1ef8ba
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct auth_master_connection *mail_user_auth_master_conn;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *mail_user_get_temp_prefix_base(struct mail_user *user);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user *mail_user_alloc(const char *username,
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen pool = pool_alloconly_create("mail user", 16*1024);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->unexpanded_set = settings_dup(set_info, set, pool);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->set = settings_dup(set_info, set, pool);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen /* check settings so that the duplicated structure will again
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen contain the parsed fields */
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen if (!settings_check(set_info, pool, user->set, &error))
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen i_panic("Settings check unexpectedly failed: %s", error);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->v.get_temp_prefix = mail_user_get_temp_prefix_base;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainenmail_user_expand_plugins_envs(struct mail_user *user, const char **error_r)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i, count;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!array_is_created(&user->set->plugin_envs))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen envs = array_get_modifiable(&user->set->plugin_envs, &count);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "userdb didn't return a home directory, "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "but plugin setting %s used it (%%h): %s",
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen var_expand(str, envs[i+1], mail_user_var_expand_table(user));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_init(struct mail_user *user, const char **error_r)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen settings_vars_have_key(user->set_info, user->set,
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen "userdb didn't return a home directory, "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen settings_var_expand(user->set_info, user->set,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->pool, mail_user_var_expand_table(user));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mail_user_expand_plugins_envs(user, error_r) < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_set = mail_user_set_get_storage_set(user);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_set_vars(struct mail_user *user, uid_t uid, const char *service,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->service = p_strdup(user->pool, service);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (local_ip != NULL && local_ip->family != 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->local_ip = p_new(user->pool, struct ip_addr, 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (remote_ip != NULL && remote_ip->family != 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->remote_ip = p_new(user->pool, struct ip_addr, 1);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenmail_user_var_expand_table(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen static struct var_expand_table static_tab[] = {
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab = p_malloc(user->pool, sizeof(static_tab));
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[4].value = user->_home; /* don't look it up unless we need it */
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[5].value = user->local_ip == NULL ? NULL :
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen p_strdup(user->pool, net_ip2addr(user->local_ip));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen tab[6].value = user->remote_ip == NULL ? NULL :
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen p_strdup(user->pool, net_ip2addr(user->remote_ip));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen tab[8].value = p_strdup(user->pool, dec2str(user->uid));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 512);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = auth_master_user_lookup(mail_user_auth_master_conn,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen auth_user_fields_parse(fields, userdb_pool, &reply);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const *plugins;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = str_array_find(plugins, module_get_plugin_name(module));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return mail_user_set_plugin_getenv(user->set, name);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const *envs;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i, count;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *mail_user_get_temp_prefix_base(struct mail_user *user)
c399b462ce6b81c78eaf0fa9c7c35bca157daa1aTimo Sirainen const char *dir;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen return t_strconcat(user->_home, "/.temp.", my_hostname, ".",
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ns = mail_namespace_find_inbox(user->namespaces);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_list_get_temp_prefix(ns->list), NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_get_temp_prefix(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_get_anvil_userip_ident(struct mail_user *user)