mail-user.c revision 72276c90ac2a38c9db7b4458acd3a2f5b61892bb
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen/* Copyright (c) 2008-2010 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user_module_register mail_user_module_register = { 0 };
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct auth_master_connection *mail_user_auth_master_conn;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void mail_user_deinit_base(struct mail_user *user)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstruct mail_user *mail_user_alloc(const char *username,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen pool = pool_alloconly_create("mail user", 16*1024);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->unexpanded_set = settings_dup(set_info, set, pool);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen user->set = settings_dup(set_info, set, pool);
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 p_array_init(&user->module_contexts, user->pool, 5);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenmail_user_expand_plugins_envs(struct mail_user *user, const char **error_r)
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen unsigned int i, count;
3b49aee9ced3b0370a3be396aca53acd5f21418cTimo Sirainen if (!array_is_created(&user->set->plugin_envs))
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen envs = array_get_modifiable(&user->set->plugin_envs, &count);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "userdb didn't return a home directory, "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "but plugin setting %s used it (%%h): %s",
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen var_expand(str, envs[i+1], mail_user_var_expand_table(user));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenint mail_user_init(struct mail_user *user, const char **error_r)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen settings_vars_have_key(user->set_info, user->set,
dee43975a70bcdb9dc83d34d6a2b177d37bb7194Timo Sirainen "userdb didn't return a home directory, "
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 mail_set = mail_user_set_get_storage_set(user);
d22301419109ed4a38351715e6760011421dadecTimo Sirainenstruct mail_user *mail_user_find(struct mail_user *user, const char *name)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = ns->next) {
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (ns->owner != NULL && strcmp(ns->owner->username, name) == 0)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvoid mail_user_set_vars(struct mail_user *user, uid_t uid, const char *service,
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 if (remote_ip != NULL && remote_ip->family != 0) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen user->remote_ip = p_new(user->pool, struct ip_addr, 1);
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainenmail_user_var_expand_table(struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen static struct var_expand_table static_tab[] = {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab = p_malloc(user->pool, sizeof(static_tab));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[1].value = p_strdup(user->pool, t_strcut(user->username, '@'));
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));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen tab[8].value = p_strdup(user->pool, dec2str(user->uid));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvoid mail_user_set_home(struct mail_user *user, const char *home)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvoid mail_user_add_namespace(struct mail_user *user,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen struct mail_namespace **tmp, *next, *ns = *namespaces;
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen if (strlen(ns->prefix) < strlen((*tmp)->prefix))
6d6c1517ef20e340a3aace406724fc8916f2d13fTimo Sirainenvoid mail_user_drop_useless_namespaces(struct mail_user *user)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (ns = user->namespaces; ns != NULL; ns = next) {
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if ((ns->flags & NAMESPACE_FLAG_USABLE) == 0 &&
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen (ns->flags & NAMESPACE_FLAG_AUTOCREATED) != 0)
939a0d82523538b2de38a02bc9f790a67b7ebf47Timo Sirainenconst char *mail_user_home_expand(struct mail_user *user, const char *path)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenint mail_user_get_home(struct mail_user *user, const char **home_r)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen userdb_pool = pool_alloconly_create("userdb lookup", 2048);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = auth_master_user_lookup(mail_user_auth_master_conn,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen auth_user_fields_parse(fields, userdb_pool, &reply);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenbool mail_user_is_plugin_loaded(struct mail_user *user, struct module *module)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *plugins;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen plugins = t_strsplit_spaces(user->set->mail_plugins, ", ");
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen ret = str_array_find(plugins, module_get_plugin_name(module));
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenconst char *mail_user_plugin_getenv(struct mail_user *user, const char *name)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen return mail_user_set_plugin_getenv(user->set, name);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenconst char *mail_user_set_plugin_getenv(const struct mail_user_settings *set,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen const char *const *envs;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int i, count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenint mail_user_try_home_expand(struct mail_user *user, const char **pathp)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvoid mail_user_set_get_temp_prefix(string_t *dest,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen str_append(dest, master_service_get_name(master_service));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenconst char *mail_user_get_anvil_userip_ident(struct mail_user *user)