bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
7b9197b917d6f546bfea70ba8a443e61de90242aTimo Sirainen#define IMAP_URLAUTH_KEY MAILBOX_ATTRIBUTE_PREFIX_DOVECOT"imap-urlauth"
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomiimap_urlauth_backend_trans_set_mailbox_key(struct mailbox *box,
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi const char **error_r,
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi *error_r = mailbox_get_last_error(box, error_code_r);
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi /* create new key */
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi ret = mailbox_attribute_set(t, MAIL_ATTRIBUTE_TYPE_PRIVATE,
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi *error_r = mailbox_get_last_error(box, error_code_r);
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomiimap_urlauth_backend_trans_get_mailbox_key(struct mailbox *box,
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen const char **error_r,
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen struct mail_user *user = mail_storage_get_user(mailbox_get_storage(box));
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi ret = mailbox_attribute_get(box, MAIL_ATTRIBUTE_TYPE_PRIVATE,
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen i_debug("imap-urlauth: %skey found for mailbox %s",
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen (ret > 0 ? "" : "no "), mailbox_get_vname(box));
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi ret = imap_urlauth_backend_trans_set_mailbox_key(box,
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen i_debug("imap-urlauth: created key for mailbox %s",
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch /* read existing key */
d49214c55881d251a54edddb117ca7735360addcTimo Sirainen buffer_create_from_data(&key_buf, mailbox_key_r,
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (strlen(mailbox_key_hex) != 2*IMAP_URLAUTH_KEY_LEN ||
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch hex_to_binary(mailbox_key_hex, &key_buf) < 0 ||
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen i_error("imap-urlauth: key found for mailbox %s is invalid",
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainenint imap_urlauth_backend_get_mailbox_key(struct mailbox *box, bool create,
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen const char **error_r,
66c87722e0fd2a85cd59797326bad3d1c409dc3aAki Tuomi ret = imap_urlauth_backend_trans_get_mailbox_key(box, create, mailbox_key_r, error_r, error_code_r);
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainenint imap_urlauth_backend_reset_mailbox_key(struct mailbox *box)
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL,
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen ret = mailbox_attribute_unset(t, MAIL_ATTRIBUTE_TYPE_PRIVATE,
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainenstatic int imap_urlauth_backend_mailbox_reset_key(struct mailbox *box)
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi errstr = mailbox_get_last_internal_error(box, &error);
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen if (error == MAIL_ERROR_NOTFOUND || error == MAIL_ERROR_PERM)
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen i_error("urlauth key reset: Couldn't open mailbox %s: %s",
fd81c8f5a38df19d1ecdfd5d6605b114ab56841eTimo Sirainen return imap_urlauth_backend_reset_mailbox_key(box);
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainenint imap_urlauth_backend_reset_all_keys(struct mail_user *user)
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen while ((info = mailbox_list_iter_next(iter)) != NULL) {
4abdade2cb09266ea3d063d5cb6893cb0583f20eTimo Sirainen box = mailbox_alloc(info->ns->list, info->vname, 0);
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen mailbox_set_reason(box, "URLAUTH reset all keys");
50c617761ee9653bd44646a95178773a3686d62eTimo Sirainen if (imap_urlauth_backend_mailbox_reset_key(box) < 0)
168204692e01267b08ce69106331701669dea202Timo Sirainen i_error("urlauth key reset: Couldn't iterate mailboxes: %s",