last-login-plugin.c revision 0c5ed44cf93523f5ae6d118501a79fff6dddf7d7
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/* Copyright (c) 2014-2016 Dovecot authors, see the included COPYING file */
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk#define LAST_LOGIN_DEFAULT_KEY_PREFIX "last-login/"
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkconst char *last_login_plugin_version = DOVECOT_ABI_VERSION;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstatic MODULE_CONTEXT_DEFINE_INIT(last_login_user_module,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstatic void last_login_dict_deinit(struct mail_user *user)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* remove timeout after dict_wait(), which may trigger
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk last_login_dict_commit() */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstatic void last_login_user_deinit(struct mail_user *user)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenklast_login_dict_commit(const struct dict_commit_result *result,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk i_error("last_login_dict: Failed to write value for user %s: %s",
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk i_error("last_login_dict: Write was unconfirmed (timeout or disconnect) for user %s: %s",
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* don't deinit the dict immediately here, lib-dict will just crash */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk luser->to = timeout_add(0, last_login_dict_deinit, user);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstatic void last_login_mail_user_created(struct mail_user *user)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk const char *dict_value, *key_name, *precision, *error;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* we want to handle only logged in users,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk not lda's raw user or accessed shared users */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk /* This is IMAP unhibernation, not a real login. */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk dict_value = mail_user_plugin_getenv(user, "last_login_dict");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (dict_value == NULL || dict_value[0] == '\0')
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (mail_user_get_home(user, &set.home_dir) <= 0)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (dict_init(dict_value, &set, &dict, &error) < 0) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk i_error("last_login_dict: dict_init(%s) failed: %s",
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk luser = p_new(user->pool, struct last_login_user, 1);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk MODULE_CONTEXT_SET(user, last_login_user_module, luser);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk key_name = mail_user_plugin_getenv(user, "last_login_key");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk key_name = t_strdup_printf(LAST_LOGIN_DEFAULT_KEY_PREFIX"%s",
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk key_name = t_strconcat(DICT_PATH_SHARED, key_name, NULL);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk precision = mail_user_plugin_getenv(user, "last_login_precision");
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (precision == NULL || strcmp(precision, "s") == 0)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk dict_set(trans, key_name, dec2str(ioloop_time));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk i_error("last_login_dict: Invalid last_login_precision '%s'", precision);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk dict_transaction_commit_async(&trans, last_login_dict_commit, user);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkstatic struct mail_storage_hooks last_login_mail_storage_hooks = {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk .mail_user_created = last_login_mail_user_created
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkvoid last_login_plugin_init(struct module *module)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk mail_storage_hooks_add(module, &last_login_mail_storage_hooks);