mail-user.c revision 8633c38d4c5e62ca8d68d64a8e07e85dc11d035a
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2008-2009 Dovecot authors, see the included COPYING file */
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "lib.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "array.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "hostpid.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "network.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "str.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "strescape.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "var-expand.h"
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen#include "settings-parser.h"
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen#include "auth-master.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "master-service.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "mail-storage-settings.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "mail-namespace.h"
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen#include "mail-storage.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include "mail-user.h"
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen#include <stdlib.h>
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenvoid (*hook_mail_user_created)(struct mail_user *user) = NULL;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic struct auth_master_connection *auth_master_conn;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mail_namespaces_deinit(&user->namespaces);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen pool_unref(&user->pool);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainenstruct mail_user *mail_user_alloc(const char *username,
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen const struct mail_user_settings *set)
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen{
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen struct mail_user *user;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen pool_t pool;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_assert(username != NULL);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_assert(*username != '\0');
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen pool = pool_alloconly_create("mail user", 8192);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user = p_new(pool, struct mail_user, 1);
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen user->pool = pool;
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen user->refcount = 1;
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen user->username = p_strdup(pool, username);
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen user->unexpanded_set =
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen settings_dup(&mail_user_setting_parser_info, set, pool);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->set = settings_dup(&mail_user_setting_parser_info, set, pool);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->v.deinit = mail_user_deinit_base;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen p_array_init(&user->module_contexts, user->pool, 5);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return user;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenstatic int
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenmail_user_expand_plugins_envs(struct mail_user *user, const char **error_r)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char **envs, *home;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen string_t *str;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen unsigned int i, count;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (!array_is_created(&user->set->plugin_envs))
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return 0;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen str = t_str_new(256);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen envs = array_get_modifiable(&user->set->plugin_envs, &count);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen i_assert((count % 2) == 0);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen for (i = 0; i < count; i += 2) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (user->_home == NULL &&
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen var_has_key(envs[i+1], 'h', "home") &&
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mail_user_get_home(user, &home) <= 0) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen *error_r = t_strdup_printf(
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "userdb didn't return a home directory, "
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen "but plugin setting %s used it (%%h): %s",
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen envs[i], envs[i+1]);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return -1;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen str_truncate(str, 0);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen var_expand(str, envs[i+1], mail_user_var_expand_table(user));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen envs[i+1] = p_strdup(user->pool, str_c(str));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return 0;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen}
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainenint mail_user_init(struct mail_user *user, const char **error_r)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const struct mail_storage_settings *mail_set;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const char *home, *key, *value;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (user->_home == NULL &&
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen settings_vars_have_key(&mail_user_setting_parser_info, user->set,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen 'h', "home", &key, &value) &&
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mail_user_get_home(user, &home) <= 0) {
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen *error_r = t_strdup_printf(
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen "userdb didn't return a home directory, "
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen "but %s used it (%%h): %s", key, value);
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen return -1;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen settings_var_expand(&mail_user_setting_parser_info, user->set,
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen user->pool, mail_user_var_expand_table(user));
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen if (mail_user_expand_plugins_envs(user, error_r) < 0)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return -1;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen mail_set = mail_user_set_get_storage_set(user->set);
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen user->mail_debug = mail_set->mail_debug;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen user->initialized = TRUE;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (hook_mail_user_created != NULL)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen hook_mail_user_created(user);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return 0;
597dba3488c648ffb375ee4a552bd52ac4346979Timo Sirainen}
74f810327aca91b3375d3fc963bce8076785b1cbTimo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenvoid mail_user_ref(struct mail_user *user)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen i_assert(user->refcount > 0);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->refcount++;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenvoid mail_user_unref(struct mail_user **_user)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mail_user *user = *_user;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen i_assert(user->refcount > 0);
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen *_user = NULL;
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen if (--user->refcount == 0)
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen user->v.deinit(user);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen{
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen struct mail_namespace *ns;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
63a2f941c1ad029061129ba6384d1d2a02382220Timo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen return ns->owner;
63a2f941c1ad029061129ba6384d1d2a02382220Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return NULL;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainenvoid mail_user_set_vars(struct mail_user *user, uid_t uid, const char *service,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const struct ip_addr *local_ip,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen const struct ip_addr *remote_ip)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen user->uid = uid;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->service = p_strdup(user->pool, service);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (local_ip != NULL && local_ip->family != 0) {
0a70a2e71658d168f75d1f075661d7936701343cTimo Sirainen user->local_ip = p_new(user->pool, struct ip_addr, 1);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen *user->local_ip = *local_ip;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (remote_ip != NULL && remote_ip->family != 0) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->remote_ip = p_new(user->pool, struct ip_addr, 1);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen *user->remote_ip = *remote_ip;
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen }
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainen}
63a2f941c1ad029061129ba6384d1d2a02382220Timo Sirainen
f8512649cd4db427a8dc5dc8f45c8e379ffd4d76Timo Sirainenconst struct var_expand_table *
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainenmail_user_var_expand_table(struct mail_user *user)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen static struct var_expand_table static_tab[] = {
4fdf60e22b5340fe35f083b9ea5eb3fd1cf2e742Timo Sirainen { 'u', NULL, "user" },
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen { 'n', NULL, "username" },
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen { 'd', NULL, "domain" },
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen { 's', NULL, "service" },
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen { 'h', NULL, "home" },
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen { 'l', NULL, "lip" },
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen { 'r', NULL, "rip" },
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen { 'p', NULL, "pid" },
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen { 'i', NULL, "uid" },
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen { '\0', NULL, NULL }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen };
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct var_expand_table *tab;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (user->var_expand_table != NULL)
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen return user->var_expand_table;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab = p_malloc(user->pool, sizeof(static_tab));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen memcpy(tab, static_tab, sizeof(static_tab));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab[0].value = user->username;
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab[2].value = strchr(user->username, '@');
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen if (tab[2].value != NULL) tab[2].value++;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab[3].value = user->service;
74f810327aca91b3375d3fc963bce8076785b1cbTimo Sirainen tab[4].value = user->_home; /* don't look it up unless we need it */
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab[5].value = user->local_ip == NULL ? NULL :
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen p_strdup(user->pool, net_ip2addr(user->local_ip));
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen tab[6].value = user->remote_ip == NULL ? NULL :
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen p_strdup(user->pool, net_ip2addr(user->remote_ip));
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab[7].value = my_pid;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen tab[8].value = p_strdup(user->pool, dec2str(user->uid));
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen
106e86d8bfdfebd3ff55241845920ea3b47e0ba2Timo Sirainen user->var_expand_table = tab;
106e86d8bfdfebd3ff55241845920ea3b47e0ba2Timo Sirainen return user->var_expand_table;
106e86d8bfdfebd3ff55241845920ea3b47e0ba2Timo Sirainen}
106e86d8bfdfebd3ff55241845920ea3b47e0ba2Timo Sirainen
106e86d8bfdfebd3ff55241845920ea3b47e0ba2Timo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
106e86d8bfdfebd3ff55241845920ea3b47e0ba2Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->_home = p_strdup(user->pool, home);
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen user->home_looked_up = TRUE;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mail_namespace **namespaces)
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen{
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen for (; ns != NULL; ns = next) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen next = ns->next;
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen tmp = &user->namespaces;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen for (; *tmp != NULL; tmp = &(*tmp)->next) {
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen break;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen }
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen ns->next = *tmp;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen *tmp = ns;
a94936bafd127680184da114c6a177b37ff656e5Timo Sirainen }
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen *namespaces = user->namespaces;
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen}
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen{
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen struct mail_namespace *ns, *next;
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
ea37a9aedfc3a6ff5f4ea10bc4eff4ca23f62a15Timo Sirainen next = ns->next;
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen mail_namespace_destroy(ns);
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen }
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen}
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainen
34435ce4a468dc13b23db69dbdd09fe20be88816Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
{
(void)mail_user_try_home_expand(user, &path);
return path;
}
int mail_user_get_home(struct mail_user *user, const char **home_r)
{
struct auth_user_reply reply;
pool_t userdb_pool;
int ret;
if (user->home_looked_up) {
*home_r = user->_home;
return user->_home != NULL ? 1 : 0;
}
userdb_pool = pool_alloconly_create("userdb lookup", 512);
ret = auth_master_user_lookup(auth_master_conn, user->username,
"lib-storage", userdb_pool, &reply);
if (ret < 0)
*home_r = NULL;
else {
user->_home = ret == 0 ? NULL :
p_strdup(user->pool, reply.home);
user->home_looked_up = TRUE;
ret = user->_home != NULL ? 1 : 0;
*home_r = user->_home;
}
pool_unref(&userdb_pool);
return ret;
}
const char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
{
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 (mail_user_get_home(user, &home) < 0)
return -1;
if (path[0] == '~' && (path[1] == '/' || path[1] == '\0')) {
if (home == NULL)
return -1;
*pathp = t_strconcat(home, path + 1, NULL);
}
return 0;
}
const char *mail_user_get_temp_prefix(struct mail_user *user)
{
struct mail_namespace *ns;
if (user->_home != NULL) {
return t_strconcat(user->_home, "/.temp.", my_hostname, ".",
my_pid, ".", NULL);
}
ns = mail_namespace_find_inbox(user->namespaces);
if (ns == NULL)
ns = user->namespaces;
return mail_storage_get_temp_prefix(ns->storage);
}
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);
}
void mail_users_init(const char *auth_socket_path, bool debug)
{
auth_master_conn = auth_master_init(auth_socket_path, debug);
}
void mail_users_deinit(void)
{
auth_master_deinit(&auth_master_conn);
}