mail-user.c revision a8d47e2427558d5011dfc75694b704760c1ef8ba
45312f52ff3a3d4c137447be4c7556500c2f8bf2Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "lib.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "array.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "hostpid.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "network.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "module-dir.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "home-expand.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "str.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "strescape.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "var-expand.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "settings-parser.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "auth-master.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "master-service.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-storage-settings.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-storage-private.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-namespace.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-storage.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "mail-user.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include <stdlib.h>
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct auth_master_connection *mail_user_auth_master_conn;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *mail_user_get_temp_prefix_base(struct mail_user *user);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_namespaces_deinit(&user->namespaces);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen pool_unref(&user->pool);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user *mail_user_alloc(const char *username,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct setting_parser_info *set_info,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct mail_user_settings *set)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_user *user;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *error;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen pool_t pool;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen i_assert(username != NULL);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen i_assert(*username != '\0');
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen pool = pool_alloconly_create("mail user", 16*1024);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user = p_new(pool, struct mail_user, 1);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->pool = pool;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->refcount = 1;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->username = p_strdup(pool, username);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->set_info = set_info;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->unexpanded_set = settings_dup(set_info, set, pool);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->set = settings_dup(set_info, set, pool);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
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
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen user->v.deinit = mail_user_deinit_base;
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 Sirainen return user;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen}
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
942302b0247403645394d848b3c620ead262a2a5Timo Sirainenstatic int
942302b0247403645394d848b3c620ead262a2a5Timo Sirainenmail_user_expand_plugins_envs(struct mail_user *user, const char **error_r)
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen{
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen const char **envs, *home;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen string_t *str;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i, count;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!array_is_created(&user->set->plugin_envs))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str = t_str_new(256);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen envs = array_get_modifiable(&user->set->plugin_envs, &count);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_assert((count % 2) == 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; i < count; i += 2) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (user->_home == NULL &&
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen var_has_key(envs[i+1], 'h', "home") &&
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_user_get_home(user, &home) <= 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *error_r = t_strdup_printf(
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "userdb didn't return a home directory, "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "but plugin setting %s used it (%%h): %s",
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen envs[i], envs[i+1]);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_truncate(str, 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen var_expand(str, envs[i+1], mail_user_var_expand_table(user));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen envs[i+1] = p_strdup(user->pool, str_c(str));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen return 0;
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_init(struct mail_user *user, const char **error_r)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct mail_storage_settings *mail_set;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *home, *key, *value;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (user->_home == NULL &&
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen settings_vars_have_key(user->set_info, user->set,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen 'h', "home", &key, &value) &&
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_user_get_home(user, &home) <= 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *error_r = t_strdup_printf(
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen "userdb didn't return a home directory, "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "but %s used it (%%h): %s", key, value);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
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 return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_set = mail_user_set_get_storage_set(user);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->mail_debug = mail_set->mail_debug;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen user->initialized = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen hook_mail_user_created(user);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_ref(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_assert(user->refcount > 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->refcount++;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
8c998bc4198f203fd1bb2af1f04c367bd4c508e8Timo Sirainenvoid mail_user_unref(struct mail_user **_user)
8c998bc4198f203fd1bb2af1f04c367bd4c508e8Timo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_user *user = *_user;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_assert(user->refcount > 0);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *_user = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (--user->refcount == 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->v.deinit(user);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_namespace *ns;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ns->owner;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen return NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_set_vars(struct mail_user *user, uid_t uid, const char *service,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct ip_addr *local_ip,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const struct ip_addr *remote_ip)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->uid = uid;
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 *user->local_ip = *local_ip;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (remote_ip != NULL && remote_ip->family != 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->remote_ip = p_new(user->pool, struct ip_addr, 1);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *user->remote_ip = *remote_ip;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst struct var_expand_table *
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenmail_user_var_expand_table(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen static struct var_expand_table static_tab[] = {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'u', NULL, "user" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'n', NULL, "username" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'd', NULL, "domain" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 's', NULL, "service" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'h', NULL, "home" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'l', NULL, "lip" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'r', NULL, "rip" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'p', NULL, "pid" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { 'i', NULL, "uid" },
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen { '\0', NULL, NULL }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen };
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct var_expand_table *tab;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen if (user->var_expand_table != NULL)
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen return user->var_expand_table;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab = p_malloc(user->pool, sizeof(static_tab));
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen memcpy(tab, static_tab, sizeof(static_tab));
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[0].value = user->username;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[2].value = strchr(user->username, '@');
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen if (tab[2].value != NULL) tab[2].value++;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen tab[3].value = user->service;
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));
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen tab[7].value = my_pid;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen tab[8].value = p_strdup(user->pool, dec2str(user->uid));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
63110c906fdb5b4e8c870e76fa3f244dac4b043dTimo Sirainen user->var_expand_table = tab;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return user->var_expand_table;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
90f29b3af0a645a4ef71889640f6eb6daac8c310Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->_home = p_strdup(user->pool, home);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->home_looked_up = TRUE;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
90f29b3af0a645a4ef71889640f6eb6daac8c310Timo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
63110c906fdb5b4e8c870e76fa3f244dac4b043dTimo Sirainen struct mail_namespace **namespaces)
63110c906fdb5b4e8c870e76fa3f244dac4b043dTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (; ns != NULL; ns = next) {
63110c906fdb5b4e8c870e76fa3f244dac4b043dTimo Sirainen next = ns->next;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen
63110c906fdb5b4e8c870e76fa3f244dac4b043dTimo Sirainen tmp = &user->namespaces;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen for (; *tmp != NULL; tmp = &(*tmp)->next) {
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen break;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ns->next = *tmp;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *tmp = ns;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen *namespaces = user->namespaces;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct mail_namespace *ns, *next;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen next = ns->next;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mail_namespace_destroy(ns);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen (void)mail_user_try_home_expand(user, &path);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return path;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen{
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen struct auth_user_info info;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen struct auth_user_reply reply;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen pool_t userdb_pool;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen const char *username, *const *fields;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen int ret;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen memset(&info, 0, sizeof(info));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen info.service = "lib-storage";
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (user->local_ip != NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen info.local_ip = *user->local_ip;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (user->remote_ip != NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen info.remote_ip = *user->remote_ip;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (user->home_looked_up) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *home_r = user->_home;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return user->_home != NULL ? 1 : 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mail_user_auth_master_conn == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 512);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = auth_master_user_lookup(mail_user_auth_master_conn,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->username, &info, userdb_pool,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen &username, &fields);
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen if (ret < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *home_r = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen else {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen auth_user_fields_parse(fields, userdb_pool, &reply);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->_home = ret == 0 ? NULL :
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen p_strdup(user->pool, reply.home);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->home_looked_up = TRUE;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen ret = user->_home != NULL ? 1 : 0;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen *home_r = user->_home;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen }
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen pool_unref(&userdb_pool);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen return ret;
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen}
db0735f9b388c5bcfb781b1b25015e898d63d953Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const *plugins;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bool ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen T_BEGIN {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ret = str_array_find(plugins, module_get_plugin_name(module));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen } T_END;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return ret;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return mail_user_set_plugin_getenv(user->set, name);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *name)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *const *envs;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen unsigned int i, count;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!array_is_created(&set->plugin_envs))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen envs = array_get(&set->plugin_envs, &count);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (i = 0; i < count; i += 2) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (strcmp(envs[i], name) == 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return envs[i+1];
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
8c998bc4198f203fd1bb2af1f04c367bd4c508e8Timo Sirainen return NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char *home, *path = *pathp;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mail_user_get_home(user, &home) < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen path = home_expand_tilde(path, home);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen if (path == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *pathp = path;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
db0735f9b388c5bcfb781b1b25015e898d63d953Timo Sirainen}
db0735f9b388c5bcfb781b1b25015e898d63d953Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic const char *mail_user_get_temp_prefix_base(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
c399b462ce6b81c78eaf0fa9c7c35bca157daa1aTimo Sirainen struct mail_namespace *ns;
c399b462ce6b81c78eaf0fa9c7c35bca157daa1aTimo Sirainen const char *dir;
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen if (user->_home != NULL) {
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen return t_strconcat(user->_home, "/.temp.", my_hostname, ".",
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen my_pid, ".", NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen ns = mail_namespace_find_inbox(user->namespaces);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (ns == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen ns = user->namespaces;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen if (ns->storage->temp_path_prefix != NULL)
db0735f9b388c5bcfb781b1b25015e898d63d953Timo Sirainen return ns->storage->temp_path_prefix;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen dir = mailbox_list_get_path(ns->list, NULL,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen MAILBOX_LIST_PATH_TYPE_DIR);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return t_strconcat(dir, "/",
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_list_get_temp_prefix(ns->list), NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_get_temp_prefix(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return user->v.get_temp_prefix(user);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_get_anvil_userip_ident(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (user->remote_ip == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return t_strconcat(net_ip2addr(user->remote_ip), "/",
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_tabescape(user->username), NULL);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen