last-login-plugin.c revision 477082109e06b912d868a2c05674f44cb880875b
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "lib.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "ioloop.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "dict.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "mail-user.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "mail-namespace.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "mail-storage-private.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "mail-storage-hooks.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#include "last-login-plugin.h"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#define LAST_LOGIN_USER_CONTEXT(obj) \
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen MODULE_CONTEXT(obj, last_login_user_module)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#define LAST_LOGIN_DEFAULT_KEY_PREFIX "last-login/"
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstruct last_login_user {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen union mail_user_module_context module_ctx;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct dict *dict;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct timeout *to;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen};
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(last_login_user_module,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen &mail_user_module_register);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_dict_deinit(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen{
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen if (luser->dict != NULL) {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen (void)dict_wait(luser->dict);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_deinit(&luser->dict);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen }
477082109e06b912d868a2c05674f44cb880875bTimo Sirainen /* remove timeout after dict_wait(), which may trigger
477082109e06b912d868a2c05674f44cb880875bTimo Sirainen last_login_dict_commit() */
477082109e06b912d868a2c05674f44cb880875bTimo Sirainen if (luser->to != NULL)
477082109e06b912d868a2c05674f44cb880875bTimo Sirainen timeout_remove(&luser->to);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen}
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_user_deinit(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen{
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen last_login_dict_deinit(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser->module_ctx.super.deinit(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen}
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_dict_commit(int ret ATTR_UNUSED, void *context)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen{
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct mail_user *user = context;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen /* don't deinit the dict immediately here, lib-dict will just crash */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser->to = timeout_add(0, last_login_dict_deinit, user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen}
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_mail_user_created(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen{
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct mail_user_vfuncs *v = user->vlast;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct dict *dict;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct dict_transaction_context *trans;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen const char *dict_value, *key_name, *error;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen if (user->autocreated) {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen /* we want to handle only logged in users,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen not lda's raw user or accessed shared users */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen return;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen }
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_value = mail_user_plugin_getenv(user, "last_login_dict");
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen if (dict_value == NULL)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen return;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen if (dict_init(dict_value, DICT_DATA_TYPE_STRING, user->username,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen user->set->base_dir, &dict, &error) < 0) {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen i_error("last_login_dict: dict_init(%s) failed: %s",
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_value, error);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen return;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen }
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser = p_new(user->pool, struct last_login_user, 1);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser->module_ctx.super = *v;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen user->vlast = &luser->module_ctx.super;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen v->deinit = last_login_user_deinit;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser->dict = dict;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen MODULE_CONTEXT_SET(user, last_login_user_module, luser);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = mail_user_plugin_getenv(user, "last_login_key");
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen if (key_name == NULL) {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = t_strdup_printf(LAST_LOGIN_DEFAULT_KEY_PREFIX"%s",
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen user->username);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen }
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = t_strconcat(DICT_PATH_SHARED, key_name, NULL);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen trans = dict_transaction_begin(dict);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_set(trans, key_name, dec2str(ioloop_time));
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_transaction_commit_async(&trans, last_login_dict_commit, user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen}
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic struct mail_storage_hooks last_login_mail_storage_hooks = {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen .mail_user_created = last_login_mail_user_created
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen};
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenvoid last_login_plugin_init(struct module *module)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen{
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen mail_storage_hooks_add(module, &last_login_mail_storage_hooks);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen}
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenvoid last_login_plugin_deinit(void)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen{
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen mail_storage_hooks_remove(&last_login_mail_storage_hooks);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen}