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