mail-user.c revision 7b64db32b95286235612eebb5d37d296a49306f7
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2008-2013 Dovecot authors, see the included COPYING file */
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch#include "lib.h"
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch#include "array.h"
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch#include "hostpid.h"
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch#include "net.h"
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch#include "module-dir.h"
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch#include "home-expand.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "safe-mkstemp.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "str.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "strescape.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "var-expand.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "settings-parser.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "auth-master.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "master-service.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mountpoint-list.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "dict.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mail-storage-settings.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mail-storage-private.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mail-storage-service.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mail-namespace.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mail-storage.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include "mail-user.h"
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch#include <stdlib.h>
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Boschstruct mail_user_module_register mail_user_module_register = { 0 };
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Boschstruct auth_master_connection *mail_user_auth_master_conn;
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Boschstatic void mail_user_deinit_base(struct mail_user *user)
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch{
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch if (user->_attr_dict != NULL) {
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch (void)dict_wait(user->_attr_dict);
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch dict_deinit(&user->_attr_dict);
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch }
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch mail_namespaces_deinit(&user->namespaces);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (user->mountpoints != NULL)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch mountpoint_list_deinit(&user->mountpoints);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Boschstruct mail_user *mail_user_alloc(const char *username,
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch const struct setting_parser_info *set_info,
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch const struct mail_user_settings *set)
45324f1eafa565dbc65e4dd335de9507dead55e6Timo Sirainen{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct mail_user *user;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch const char *error;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch pool_t pool;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch i_assert(username != NULL);
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch i_assert(*username != '\0');
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
833bed942977673526c72e79bccc09314fc57104Phil Carmody pool = pool_alloconly_create("mail user", 16*1024);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user = p_new(pool, struct mail_user, 1);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->pool = pool;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->refcount = 1;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->username = p_strdup(pool, username);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->set_info = set_info;
e9228a3918aa0243eff4aae1ff5462bd3198417fTimo Sirainen user->unexpanded_set = settings_dup(set_info, set, pool);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->set = settings_dup(set_info, set, pool);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->service = master_service_get_name(master_service);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->default_normalizer = uni_utf8_to_decomposed_titlecase;
1e9296de32c9ddda40f33c06556cd568ddadf71fTimo Sirainen
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch /* check settings so that the duplicated structure will again
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch contain the parsed fields */
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (!settings_check(set_info, pool, user->set, &error))
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch i_panic("Settings check unexpectedly failed: %s", error);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->v.deinit = mail_user_deinit_base;
1e9296de32c9ddda40f33c06556cd568ddadf71fTimo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return user;
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch}
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch
833bed942977673526c72e79bccc09314fc57104Phil Carmodystatic void
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Boschmail_user_expand_plugins_envs(struct mail_user *user)
833bed942977673526c72e79bccc09314fc57104Phil Carmody{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch const char **envs, *home;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch string_t *str;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch unsigned int i, count;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (!array_is_created(&user->set->plugin_envs))
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch str = t_str_new(256);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch envs = array_get_modifiable(&user->set->plugin_envs, &count);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch i_assert((count % 2) == 0);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch for (i = 0; i < count; i += 2) {
1e9296de32c9ddda40f33c06556cd568ddadf71fTimo Sirainen if (user->_home == NULL &&
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch var_has_key(envs[i+1], 'h', "home") &&
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch mail_user_get_home(user, &home) <= 0) {
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen user->error = p_strdup_printf(user->pool,
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen "userdb didn't return a home directory, "
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen "but plugin setting %s used it (%%h): %s",
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch envs[i], envs[i+1]);
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen return;
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch }
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch str_truncate(str, 0);
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch var_expand(str, envs[i+1], mail_user_var_expand_table(user));
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch envs[i+1] = p_strdup(user->pool, str_c(str));
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch }
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch}
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Boschint mail_user_init(struct mail_user *user, const char **error_r)
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch{
14bd2410de3a0261d9c53c6120915027262216bdTimo Sirainen const struct mail_storage_settings *mail_set;
14bd2410de3a0261d9c53c6120915027262216bdTimo Sirainen const char *home, *key, *value;
14bd2410de3a0261d9c53c6120915027262216bdTimo Sirainen bool need_home_dir;
9f8cef4cbc49797053c343209ea13022fdbc5a63Stephan Bosch
9f8cef4cbc49797053c343209ea13022fdbc5a63Stephan Bosch need_home_dir = user->_home == NULL &&
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen settings_vars_have_key(user->set_info, user->set,
9f8cef4cbc49797053c343209ea13022fdbc5a63Stephan Bosch 'h', "home", &key, &value);
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen /* expand mail_home setting before calling mail_user_get_home() */
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen settings_var_expand(user->set_info, user->set,
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen user->pool, mail_user_var_expand_table(user));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (need_home_dir && mail_user_get_home(user, &home) <= 0) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->error = p_strdup_printf(user->pool,
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch "userdb didn't return a home directory, "
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch "but %s used it (%%h): %s", key, value);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch }
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch mail_user_expand_plugins_envs(user);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch /* autocreated users for shared mailboxes need to be fully initialized
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch if they don't exist, since they're going to be used anyway */
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (user->error == NULL || user->nonexistent) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch mail_set = mail_user_set_get_storage_set(user);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->mail_debug = mail_set->mail_debug;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch user->initialized = TRUE;
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch hook_mail_user_created(user);
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch }
833bed942977673526c72e79bccc09314fc57104Phil Carmody
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch if (user->error != NULL) {
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch *error_r = t_strdup(user->error);
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch return -1;
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch }
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch return 0;
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch}
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Boschvoid mail_user_ref(struct mail_user *user)
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch{
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch i_assert(user->refcount > 0);
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch user->refcount++;
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch}
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Boschvoid mail_user_unref(struct mail_user **_user)
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch{
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch struct mail_user *user = *_user;
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch i_assert(user->refcount > 0);
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch *_user = NULL;
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch if (user->refcount > 1) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->refcount--;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch }
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch /* call deinit() with refcount=1, otherwise we may assert-crash in
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch mail_user_ref() that is called by some deinit() handler. */
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch user->v.deinit(user);
ee2633056e67353157bfbce4d9e0d1c3ceaa627aStephan Bosch i_assert(user->refcount == 1);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch pool_unref(&user->pool);
91a482473f200152d6713181c0e36f7a4f03ef6dTimo Sirainen}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Boschstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct mail_namespace *ns;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch for (ns = user->namespaces; ns != NULL; ns = ns->next) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return ns->owner;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch }
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return NULL;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Boschvoid mail_user_set_vars(struct mail_user *user, const char *service,
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch const struct ip_addr *local_ip,
85f3bd5926fff0e70b6d259a5c8074bd8cdeb9adTimo Sirainen const struct ip_addr *remote_ip)
85f3bd5926fff0e70b6d259a5c8074bd8cdeb9adTimo Sirainen{
85f3bd5926fff0e70b6d259a5c8074bd8cdeb9adTimo Sirainen i_assert(service != NULL);
85f3bd5926fff0e70b6d259a5c8074bd8cdeb9adTimo Sirainen
85f3bd5926fff0e70b6d259a5c8074bd8cdeb9adTimo Sirainen user->service = p_strdup(user->pool, service);
85f3bd5926fff0e70b6d259a5c8074bd8cdeb9adTimo Sirainen if (local_ip != NULL && local_ip->family != 0) {
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch user->local_ip = p_new(user->pool, struct ip_addr, 1);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch *user->local_ip = *local_ip;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch }
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch if (remote_ip != NULL && remote_ip->family != 0) {
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch user->remote_ip = p_new(user->pool, struct ip_addr, 1);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch *user->remote_ip = *remote_ip;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch }
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch}
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Boschconst struct var_expand_table *
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Boschmail_user_var_expand_table(struct mail_user *user)
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch{
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch static struct var_expand_table static_tab[] = {
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch { 'u', NULL, "user" },
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch { 'n', NULL, "username" },
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch { 'd', NULL, "domain" },
93ed69606237a08623f8294c060fa148880058f8Timo Sirainen { 's', NULL, "service" },
93ed69606237a08623f8294c060fa148880058f8Timo Sirainen { 'h', NULL, "home" },
93ed69606237a08623f8294c060fa148880058f8Timo Sirainen { 'l', NULL, "lip" },
93ed69606237a08623f8294c060fa148880058f8Timo Sirainen { 'r', NULL, "rip" },
93ed69606237a08623f8294c060fa148880058f8Timo Sirainen { 'p', NULL, "pid" },
93ed69606237a08623f8294c060fa148880058f8Timo Sirainen { 'i', NULL, "uid" },
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch { '\0', NULL, "gid" },
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch { '\0', NULL, NULL }
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch };
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct var_expand_table *tab;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch /* use a cached table, unless home directory has been set afterwards */
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (user->var_expand_table != NULL &&
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->var_expand_table[4].value == user->_home)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return user->var_expand_table;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab = p_malloc(user->pool, sizeof(static_tab));
711e8e4c5c5d702dfa062f42a1ede5de14c151c9Stephan Bosch memcpy(tab, static_tab, sizeof(static_tab));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[0].value = user->username;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[2].value = strchr(user->username, '@');
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (tab[2].value != NULL) tab[2].value++;
711e8e4c5c5d702dfa062f42a1ede5de14c151c9Stephan Bosch tab[3].value = user->service;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[4].value = user->_home; /* don't look it up unless we need it */
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[5].value = user->local_ip == NULL ? NULL :
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch p_strdup(user->pool, net_ip2addr(user->local_ip));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[6].value = user->remote_ip == NULL ? NULL :
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch p_strdup(user->pool, net_ip2addr(user->remote_ip));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[7].value = my_pid;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[8].value = p_strdup(user->pool, dec2str(user->uid));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tab[9].value = p_strdup(user->pool, dec2str(user->gid));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->var_expand_table = tab;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return user->var_expand_table;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Boschvoid mail_user_set_home(struct mail_user *user, const char *home)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->_home = p_strdup(user->pool, home);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch user->home_looked_up = TRUE;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Boschvoid mail_user_add_namespace(struct mail_user *user,
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct mail_namespace **namespaces)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct mail_namespace **tmp, *next, *ns = *namespaces;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch for (; ns != NULL; ns = next) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch next = ns->next;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch tmp = &user->namespaces;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch for (; *tmp != NULL; tmp = &(*tmp)->next) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (strlen(ns->prefix) < strlen((*tmp)->prefix))
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch break;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch }
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch ns->next = *tmp;
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch *tmp = ns;
2f64a4c88de91c483fb378bc80d10e1caa6f2305Stephan Bosch }
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch *namespaces = user->namespaces;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
833bed942977673526c72e79bccc09314fc57104Phil Carmodyvoid mail_user_drop_useless_namespaces(struct mail_user *user)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct mail_namespace *ns, *next;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch /* drop all autocreated unusable (typically shared) namespaces.
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch don't drop the autocreated prefix="" namespace that we explicitly
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch created for being the fallback namespace. */
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch for (ns = user->namespaces; ns != NULL; ns = next) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch next = ns->next;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0 &&
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch ns->prefix_len > 0)
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainen mail_namespace_destroy(ns);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch }
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
faa8995f1d300e7a8917407a52bbd1b98e10bf25Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch (void)mail_user_try_home_expand(user, &path);
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch return path;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch}
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
711e8e4c5c5d702dfa062f42a1ede5de14c151c9Stephan Boschstatic int mail_user_userdb_lookup_home(struct mail_user *user)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch{
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch struct auth_user_info info;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch struct auth_user_reply reply;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch pool_t userdb_pool;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch const char *username, *const *fields;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch int ret;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch i_assert(!user->home_looked_up);
b37e11d37fb1ebf50511eef5d9d96d1205818458Stephan Bosch
b37e11d37fb1ebf50511eef5d9d96d1205818458Stephan Bosch memset(&info, 0, sizeof(info));
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch info.service = user->service;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (user->local_ip != NULL)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch info.local_ip = *user->local_ip;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (user->remote_ip != NULL)
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch info.remote_ip = *user->remote_ip;
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch userdb_pool = pool_alloconly_create("userdb lookup", 2048);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch ret = auth_master_user_lookup(mail_user_auth_master_conn,
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch user->username, &info, userdb_pool,
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch &username, &fields);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch if (ret > 0) {
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch auth_user_fields_parse(fields, userdb_pool, &reply);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch user->_home = p_strdup(user->pool, reply.home);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch }
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch pool_unref(&userdb_pool);
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch return ret;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch}
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen{
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen int ret;
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch if (user->home_looked_up) {
3fcb3d2d1f3583025ff62bae95ec706920f398b1Stephan Bosch *home_r = user->_home;
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen return user->_home != NULL ? 1 : 0;
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen }
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen if (mail_user_auth_master_conn == NULL) {
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen /* no userdb connection. we can only use mail_home setting. */
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen user->_home = user->set->mail_home;
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen } else if ((ret = mail_user_userdb_lookup_home(user)) < 0) {
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen /* userdb lookup failed */
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen return -1;
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen } else if (ret == 0) {
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen /* user doesn't exist */
95e0b82fdff1bb511067d703bb8b67c22f242c38Timo Sirainen user->nonexistent = TRUE;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch } else if (user->_home == NULL && *user->set->mail_home != '\0') {
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch /* no home returned by userdb lookup, fallback to mail_home
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch setting. */
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch user->_home = user->set->mail_home;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch }
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch user->home_looked_up = TRUE;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch *home_r = user->_home;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch return user->_home != NULL ? 1 : 0;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch}
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Boschbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch{
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch const char *const *plugins;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch bool ret;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch T_BEGIN {
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch ret = str_array_find(plugins, module_get_plugin_name(module));
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch } T_END;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch return ret;
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch}
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Boschconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
564e117d86ce5b659f9b9570edddc566f9ebb5dfStephan Bosch{
return mail_user_set_plugin_getenv(user->set, name);
}
const char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
const char *name)
{
const char *const *envs;
unsigned int i, count;
if (!array_is_created(&set->plugin_envs))
return NULL;
envs = array_get(&set->plugin_envs, &count);
for (i = 0; i < count; i += 2) {
if (strcmp(envs[i], name) == 0)
return envs[i+1];
}
return NULL;
}
int mail_user_try_home_expand(struct mail_user *user, const char **pathp)
{
const char *home, *path = *pathp;
if (*path != '~') {
/* no need to expand home */
return 0;
}
if (mail_user_get_home(user, &home) <= 0)
return -1;
path = home_expand_tilde(path, home);
if (path == NULL)
return -1;
*pathp = path;
return 0;
}
void mail_user_set_get_temp_prefix(string_t *dest,
const struct mail_user_settings *set)
{
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);
}
bool mail_user_is_path_mounted(struct mail_user *user, const char *path,
const char **error_r)
{
struct mountpoint_list_rec *rec;
const char *mounts_path;
*error_r = NULL;
if (user->mountpoints == NULL) {
mounts_path = t_strdup_printf("%s/"MOUNTPOINT_LIST_FNAME,
user->set->base_dir);
user->mountpoints = mountpoint_list_init_readonly(mounts_path);
} else {
(void)mountpoint_list_refresh(user->mountpoints);
}
rec = mountpoint_list_find(user->mountpoints, path);
if (rec == NULL || strcmp(rec->state, MOUNTPOINT_STATE_IGNORE) == 0) {
/* we don't have any knowledge of this path's mountpoint.
assume it's fine. */
return TRUE;
}
/* record exists for this mountpoint. see if it's mounted */
if (mountpoint_list_update_mounted(user->mountpoints) == 0 &&
!rec->mounted) {
*error_r = t_strdup_printf("Mountpoint %s isn't mounted. "
"Mount it or remove it with doveadm mount remove",
rec->mount_path);
return FALSE;
}
return TRUE;
}
static void
mail_user_try_load_class_plugin(struct mail_user *user, const char *name)
{
struct module_dir_load_settings mod_set;
struct module *module;
unsigned int name_len = strlen(name);
memset(&mod_set, 0, sizeof(mod_set));
mod_set.abi_version = DOVECOT_ABI_VERSION;
mod_set.binary_name = master_service_get_name(master_service);
mod_set.setting_name = "<built-in storage lookup>";
mod_set.require_init_funcs = TRUE;
mod_set.debug = user->mail_debug;
mail_storage_service_modules =
module_dir_load_missing(mail_storage_service_modules,
user->set->mail_plugin_dir,
name, &mod_set);
/* initialize the module (and only this module!) immediately so that
the class gets registered */
for (module = mail_storage_service_modules; module != NULL; module = module->next) {
if (strncmp(module->name, name, name_len) == 0 &&
strcmp(module->name + name_len, "_plugin") == 0) {
if (!module->initialized) {
module->initialized = TRUE;
module->init(module);
}
break;
}
}
}
struct mail_storage *
mail_user_get_storage_class(struct mail_user *user, const char *name)
{
struct mail_storage *storage;
storage = mail_storage_find_class(name);
if (storage == NULL || storage->v.alloc != NULL)
return storage;
/* it's implemented by a plugin. load it and check again. */
mail_user_try_load_class_plugin(user, name);
storage = mail_storage_find_class(name);
if (storage != NULL && storage->v.alloc == NULL) {
i_error("Storage driver '%s' exists as a stub, "
"but its plugin couldn't be loaded", name);
return NULL;
}
return storage;
}