mail-user.c revision 65889a7d8c059e2feb159aee1633b847aba84831
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen/* Copyright (c) 2008-2011 Dovecot authors, see the included COPYING file */
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "lib.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "array.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "hostpid.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "network.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "module-dir.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "home-expand.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "str.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "strescape.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "var-expand.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "settings-parser.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "auth-master.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "master-service.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "mail-storage-settings.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "mail-storage-private.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "mail-namespace.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "mail-storage.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include "mail-user.h"
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen#include <stdlib.h>
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenstruct auth_master_connection *mail_user_auth_master_conn;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen mail_namespaces_deinit(&user->namespaces);
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen pool_unref(&user->pool);
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen}
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainenstruct mail_user *mail_user_alloc(const char *username,
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen const struct setting_parser_info *set_info,
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen const struct mail_user_settings *set)
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen{
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen struct mail_user *user;
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen const char *error;
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen pool_t pool;
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen i_assert(username != NULL);
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen i_assert(*username != '\0');
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen pool = pool_alloconly_create("mail user", 16*1024);
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen user = p_new(pool, struct mail_user, 1);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->pool = pool;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->refcount = 1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->username = p_strdup(pool, username);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->set_info = set_info;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->unexpanded_set = settings_dup(set_info, set, pool);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->set = settings_dup(set_info, set, pool);
583c5314e426542f5e0fb5bf8314c31519429ef7Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen /* check settings so that the duplicated structure will again
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen contain the parsed fields */
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (!settings_check(set_info, pool, user->set, &error))
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen i_panic("Settings check unexpectedly failed: %s", error);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->v.deinit = mail_user_deinit_base;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return user;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenstatic int
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenmail_user_expand_plugins_envs(struct mail_user *user, const char **error_r)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char **envs, *home;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen string_t *str;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen unsigned int i, count;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (!array_is_created(&user->set->plugin_envs))
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen str = t_str_new(256);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen envs = array_get_modifiable(&user->set->plugin_envs, &count);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen i_assert((count % 2) == 0);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen for (i = 0; i < count; i += 2) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->_home == NULL &&
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen var_has_key(envs[i+1], 'h', "home") &&
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen mail_user_get_home(user, &home) <= 0) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *error_r = t_strdup_printf(
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen "userdb didn't return a home directory, "
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen "but plugin setting %s used it (%%h): %s",
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen envs[i], envs[i+1]);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return -1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen str_truncate(str, 0);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen var_expand(str, envs[i+1], mail_user_var_expand_table(user));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen envs[i+1] = p_strdup(user->pool, str_c(str));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenint mail_user_init(struct mail_user *user, const char **error_r)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const struct mail_storage_settings *mail_set;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char *home, *key, *value;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->_home == NULL &&
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen settings_vars_have_key(user->set_info, user->set,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen 'h', "home", &key, &value) &&
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen mail_user_get_home(user, &home) <= 0) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *error_r = t_strdup_printf(
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen "userdb didn't return a home directory, "
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen "but %s used it (%%h): %s", key, value);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return -1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen settings_var_expand(user->set_info, user->set,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->pool, mail_user_var_expand_table(user));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (mail_user_expand_plugins_envs(user, error_r) < 0)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return -1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen mail_set = mail_user_set_get_storage_set(user);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->mail_debug = mail_set->mail_debug;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->service = master_service_get_name(master_service);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->initialized = TRUE;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen hook_mail_user_created(user);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->error != NULL) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *error_r = t_strdup(user->error);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return -1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_ref(struct mail_user *user)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen i_assert(user->refcount > 0);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->refcount++;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_unref(struct mail_user **_user)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct mail_user *user = *_user;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen i_assert(user->refcount > 0);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *_user = NULL;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (--user->refcount == 0)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->v.deinit(user);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct mail_namespace *ns;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return ns->owner;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return NULL;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_set_vars(struct mail_user *user, uid_t uid, const char *service,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const struct ip_addr *local_ip,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const struct ip_addr *remote_ip)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->uid = uid;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->service = p_strdup(user->pool, service);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (local_ip != NULL && local_ip->family != 0) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->local_ip = p_new(user->pool, struct ip_addr, 1);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *user->local_ip = *local_ip;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (remote_ip != NULL && remote_ip->family != 0) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->remote_ip = p_new(user->pool, struct ip_addr, 1);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *user->remote_ip = *remote_ip;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenconst struct var_expand_table *
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenmail_user_var_expand_table(struct mail_user *user)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen static struct var_expand_table static_tab[] = {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'u', NULL, "user" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'n', NULL, "username" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'd', NULL, "domain" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 's', NULL, "service" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'h', NULL, "home" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'l', NULL, "lip" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'r', NULL, "rip" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'p', NULL, "pid" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { 'i', NULL, "uid" },
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen { '\0', NULL, NULL }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen };
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct var_expand_table *tab;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->var_expand_table != NULL)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return user->var_expand_table;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab = p_malloc(user->pool, sizeof(static_tab));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen memcpy(tab, static_tab, sizeof(static_tab));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[0].value = user->username;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[2].value = strchr(user->username, '@');
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (tab[2].value != NULL) tab[2].value++;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[3].value = user->service;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[4].value = user->_home; /* don't look it up unless we need it */
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[5].value = user->local_ip == NULL ? NULL :
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen p_strdup(user->pool, net_ip2addr(user->local_ip));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[6].value = user->remote_ip == NULL ? NULL :
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen p_strdup(user->pool, net_ip2addr(user->remote_ip));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[7].value = my_pid;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tab[8].value = p_strdup(user->pool, dec2str(user->uid));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->var_expand_table = tab;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return user->var_expand_table;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->_home = p_strdup(user->pool, home);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->home_looked_up = TRUE;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct mail_namespace **namespaces)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen for (; ns != NULL; ns = next) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen next = ns->next;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen tmp = &user->namespaces;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen for (; *tmp != NULL; tmp = &(*tmp)->next) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen break;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen ns->next = *tmp;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *tmp = ns;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *namespaces = user->namespaces;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct mail_namespace *ns, *next;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen next = ns->next;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen mail_namespace_destroy(ns);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen (void)mail_user_try_home_expand(user, &path);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return path;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct auth_user_info info;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen struct auth_user_reply reply;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen pool_t userdb_pool;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char *username, *const *fields;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen int ret;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen memset(&info, 0, sizeof(info));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen info.service = "lib-storage";
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->local_ip != NULL)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen info.local_ip = *user->local_ip;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->remote_ip != NULL)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen info.remote_ip = *user->remote_ip;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (user->home_looked_up) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *home_r = user->_home;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return user->_home != NULL ? 1 : 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *home_r = NULL;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (mail_user_auth_master_conn == NULL)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 2048);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen ret = auth_master_user_lookup(mail_user_auth_master_conn,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->username, &info, userdb_pool,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen &username, &fields);
e36634029ba3563713aaf639abaf85200f68d635Timo Sirainen if (ret >= 0) {
e36634029ba3563713aaf639abaf85200f68d635Timo Sirainen auth_user_fields_parse(fields, userdb_pool, &reply);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->_home = ret == 0 ? NULL :
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen p_strdup(user->pool, reply.home);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen user->home_looked_up = TRUE;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen ret = user->_home != NULL ? 1 : 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *home_r = user->_home;
e36634029ba3563713aaf639abaf85200f68d635Timo Sirainen }
e36634029ba3563713aaf639abaf85200f68d635Timo Sirainen pool_unref(&userdb_pool);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return ret;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char *const *plugins;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen bool ret;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen T_BEGIN {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen ret = str_array_find(plugins, module_get_plugin_name(module));
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen } T_END;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return ret;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return mail_user_set_plugin_getenv(user->set, name);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenconst char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char *name)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char *const *envs;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen unsigned int i, count;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (!array_is_created(&set->plugin_envs))
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return NULL;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen envs = array_get(&set->plugin_envs, &count);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen for (i = 0; i < count; i += 2) {
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (strcmp(envs[i], name) == 0)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return envs[i+1];
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen }
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return NULL;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const char *home, *path = *pathp;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (mail_user_get_home(user, &home) < 0)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return -1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen path = home_expand_tilde(path, home);
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen if (path == NULL)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return -1;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen *pathp = path;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen return 0;
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen}
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainenvoid mail_user_set_get_temp_prefix(string_t *dest,
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen const struct mail_user_settings *set)
b63e20ea9bc84f1aa90a551f217d01385e070b73Timo Sirainen{
str_append(dest, set->mail_temp_dir);
str_append(dest, "/dovecot.");
str_append(dest, master_service_get_name(master_service));
str_append_c(dest, '.');
}
const char *mail_user_get_anvil_userip_ident(struct mail_user *user)
{
if (user->remote_ip == NULL)
return NULL;
return t_strconcat(net_ip2addr(user->remote_ip), "/",
str_tabescape(user->username), NULL);
}