last-login-plugin.c revision 2b1a3f0eb782068490f90a2c25379b6c0e15b0b1
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
2b1a3f0eb782068490f90a2c25379b6c0e15b0b1Aki Tuomi MODULE_CONTEXT_REQUIRE(obj, last_login_user_module)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen#define LAST_LOGIN_DEFAULT_KEY_PREFIX "last-login/"
8aa049fd90cd03a66ec43018554bf3ba06815a10Timo Sirainenconst char *last_login_plugin_version = DOVECOT_ABI_VERSION;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(last_login_user_module,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_dict_deinit(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
477082109e06b912d868a2c05674f44cb880875bTimo Sirainen /* remove timeout after dict_wait(), which may trigger
477082109e06b912d868a2c05674f44cb880875bTimo Sirainen last_login_dict_commit() */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic void last_login_user_deinit(struct mail_user *user)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
f9258697fe2573b087d09645dfade044dfb27052Aki Tuomilast_login_dict_commit(const struct dict_commit_result *result,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen struct last_login_user *luser = LAST_LOGIN_USER_CONTEXT(user);
f9258697fe2573b087d09645dfade044dfb27052Aki Tuomi i_error("last_login_dict: Failed to write value for user %s: %s",
f9258697fe2573b087d09645dfade044dfb27052Aki Tuomi i_error("last_login_dict: Write was unconfirmed (timeout or disconnect) for user %s: %s",
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 Sirainenstatic void last_login_mail_user_created(struct mail_user *user)
4209bd278ff41fe352e8b993d849a061152829c8Timo Sirainen const char *dict_value, *key_name, *precision, *error;
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen /* we want to handle only logged in users,
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen not lda's raw user or accessed shared users */
0c5ed44cf93523f5ae6d118501a79fff6dddf7d7Timo Sirainen /* This is IMAP unhibernation, not a real login. */
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_value = mail_user_plugin_getenv(user, "last_login_dict");
ce3a152df54ff6bcb08af5d489a873bd85fcecddTimo Sirainen if (dict_value == NULL || dict_value[0] == '\0')
9346506a9f4dd9a6285fe8595588e73161849235Timo Sirainen if (mail_user_get_home(user, &set.home_dir) <= 0)
20e04227229970d148801c507946666e2a9bd838Timo Sirainen if (dict_init(dict_value, &set, &dict, &error) < 0) {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen i_error("last_login_dict: dict_init(%s) failed: %s",
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen luser = p_new(user->pool, struct last_login_user, 1);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen MODULE_CONTEXT_SET(user, last_login_user_module, luser);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = mail_user_plugin_getenv(user, "last_login_key");
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = t_strdup_printf(LAST_LOGIN_DEFAULT_KEY_PREFIX"%s",
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen key_name = t_strconcat(DICT_PATH_SHARED, key_name, NULL);
4209bd278ff41fe352e8b993d849a061152829c8Timo Sirainen precision = mail_user_plugin_getenv(user, "last_login_precision");
4209bd278ff41fe352e8b993d849a061152829c8Timo Sirainen if (precision == NULL || strcmp(precision, "s") == 0)
4209bd278ff41fe352e8b993d849a061152829c8Timo Sirainen dict_set(trans, key_name, dec2str(ioloop_time));
4209bd278ff41fe352e8b993d849a061152829c8Timo Sirainen (unsigned int)(ioloop_timeval.tv_usec/1000)));
4209bd278ff41fe352e8b993d849a061152829c8Timo Sirainen i_error("last_login_dict: Invalid last_login_precision '%s'", precision);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen dict_transaction_commit_async(&trans, last_login_dict_commit, user);
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenstatic struct mail_storage_hooks last_login_mail_storage_hooks = {
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen .mail_user_created = last_login_mail_user_created
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainenvoid last_login_plugin_init(struct module *module)
3008b30900f4de3e76f1a6d289444eb694f3d17cTimo Sirainen mail_storage_hooks_add(module, &last_login_mail_storage_hooks);