mail-user.c revision 72276c90ac2a38c9db7b4458acd3a2f5b61892bb
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "lib.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "array.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "hostpid.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "network.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "module-dir.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "home-expand.h"
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen#include "str.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen#include "strescape.h"
905457e0982fc15930d90e174f271dc69f9afcf9Timo Sirainen#include "var-expand.h"
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo 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"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "mail-namespace.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "mail-storage.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen#include "mail-user.h"
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo 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 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)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen{
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen struct mail_user *user;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen const char *error;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen pool_t pool;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_assert(username != NULL);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen i_assert(*username != '\0');
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen pool = pool_alloconly_create("mail user", 16*1024);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user = p_new(pool, struct mail_user, 1);
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen user->pool = pool;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->refcount = 1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->username = p_strdup(pool, username);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->set_info = set_info;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->unexpanded_set = settings_dup(set_info, set, pool);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->set = settings_dup(set_info, set, pool);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* check settings so that the duplicated structure will again
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen contain the parsed fields */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (!settings_check(set_info, pool, user->set, &error))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_panic("Settings check unexpectedly failed: %s", error);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->v.deinit = mail_user_deinit_base;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return user;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic int
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenmail_user_expand_plugins_envs(struct mail_user *user, const char **error_r)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const char **envs, *home;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen string_t *str;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen unsigned int i, count;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (!array_is_created(&user->set->plugin_envs))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo 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 &&
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen var_has_key(envs[i+1], 'h', "home") &&
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_user_get_home(user, &home) <= 0) {
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo 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 }
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo 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,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen 'h', "home", &key, &value) &&
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_user_get_home(user, &home) <= 0) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *error_r = t_strdup_printf(
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "userdb didn't return a home directory, "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "but %s used it (%%h): %s", key, value);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return -1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen }
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen settings_var_expand(user->set_info, user->set,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen user->pool, mail_user_var_expand_table(user));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (mail_user_expand_plugins_envs(user, error_r) < 0)
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen return -1;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen mail_set = mail_user_set_get_storage_set(user);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen user->mail_debug = mail_set->mail_debug;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen user->initialized = TRUE;
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen hook_mail_user_created(user);
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen if (user->error != NULL) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *error_r = t_strdup(user->error);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen }
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen return 0;
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainenvoid mail_user_ref(struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
7a24bdc1a5e2d5368c2569b4852192f2bdb5a31fTimo Sirainen i_assert(user->refcount > 0);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->refcount++;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenvoid mail_user_unref(struct mail_user **_user)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen{
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_user *user = *_user;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen i_assert(user->refcount > 0);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen *_user = NULL;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (--user->refcount == 0)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen user->v.deinit(user);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen}
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen{
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct mail_namespace *ns;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen return ns->owner;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen return NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo 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);
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo 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}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenconst struct var_expand_table *
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainenmail_user_var_expand_table(struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen static struct var_expand_table static_tab[] = {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'u', NULL, "user" },
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen { 'n', NULL, "username" },
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen { 'd', NULL, "domain" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 's', NULL, "service" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'h', NULL, "home" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'l', NULL, "lip" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'r', NULL, "rip" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'p', NULL, "pid" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { 'i', NULL, "uid" },
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen { '\0', NULL, NULL }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen };
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct var_expand_table *tab;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen if (user->var_expand_table != NULL)
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen return user->var_expand_table;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab = p_malloc(user->pool, sizeof(static_tab));
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen memcpy(tab, static_tab, sizeof(static_tab));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[0].value = user->username;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[2].value = strchr(user->username, '@');
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (tab[2].value != NULL) tab[2].value++;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[3].value = user->service;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[4].value = user->_home; /* don't look it up unless we need it */
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[5].value = user->local_ip == NULL ? NULL :
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen p_strdup(user->pool, net_ip2addr(user->local_ip));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[6].value = user->remote_ip == NULL ? NULL :
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen p_strdup(user->pool, net_ip2addr(user->remote_ip));
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen tab[7].value = my_pid;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[8].value = p_strdup(user->pool, dec2str(user->uid));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->var_expand_table = tab;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return user->var_expand_table;
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen}
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->_home = p_strdup(user->pool, home);
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen user->home_looked_up = TRUE;
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace **namespaces)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (; ns != NULL; ns = next) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen next = ns->next;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen tmp = &user->namespaces;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (; *tmp != NULL; tmp = &(*tmp)->next) {
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen break;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen }
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen ns->next = *tmp;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen *tmp = ns;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *namespaces = user->namespaces;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainen{
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainen struct mail_namespace *ns, *next;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen next = ns->next;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen mail_namespace_destroy(ns);
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen }
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainen{
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen (void)mail_user_try_home_expand(user, &path);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return path;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct auth_user_info info;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct auth_user_reply reply;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen pool_t userdb_pool;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *username, *const *fields;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen int ret;
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen memset(&info, 0, sizeof(info));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen info.service = "lib-storage";
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (user->local_ip != NULL)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen info.local_ip = *user->local_ip;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (user->remote_ip != NULL)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen info.remote_ip = *user->remote_ip;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (user->home_looked_up) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *home_r = user->_home;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return user->_home != NULL ? 1 : 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *home_r = NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mail_user_auth_master_conn == NULL)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 2048);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = auth_master_user_lookup(mail_user_auth_master_conn,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->username, &info, userdb_pool,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen &username, &fields);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (ret >= 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen auth_user_fields_parse(fields, userdb_pool, &reply);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->_home = ret == 0 ? NULL :
7e209b78ca757294dbbc15604c88673b3a6b0c39Timo Sirainen p_strdup(user->pool, reply.home);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->home_looked_up = TRUE;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = user->_home != NULL ? 1 : 0;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen *home_r = user->_home;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen pool_unref(&userdb_pool);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return ret;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *plugins;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bool ret;
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen T_BEGIN {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = str_array_find(plugins, module_get_plugin_name(module));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen } T_END;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return ret;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return mail_user_set_plugin_getenv(user->set, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenconst char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *name)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *envs;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int i, count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (!array_is_created(&set->plugin_envs))
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen envs = array_get(&set->plugin_envs, &count);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < count; i += 2) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (strcmp(envs[i], name) == 0)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return envs[i+1];
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen }
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return NULL;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen}
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen{
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *home, *path = *pathp;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (mail_user_get_home(user, &home) < 0)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen path = home_expand_tilde(path, home);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen if (path == NULL)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return -1;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen *pathp = path;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen return 0;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen}
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_set_get_temp_prefix(string_t *dest,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen const struct mail_user_settings *set)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen{
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_append(dest, set->mail_temp_dir);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_append(dest, "/dovecot.");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_append(dest, master_service_get_name(master_service));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_append_c(dest, '.');
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