bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch#include "imap-common.h"
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch#include "imap-resp-code.h"
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch#include "imap-commands.h"
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch#include "imap-urlauth.h"
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Boschstatic bool cmd_resetkey_all(struct client_command_context *cmd)
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch{
fd81c8f5a38df19d1ecdfd5d6605b114ab56841eTimo Sirainen if (imap_urlauth_reset_all_keys(cmd->client->urlauth_ctx) < 0) {
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_internal_error(cmd);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_tagline(cmd, "OK All keys removed.");
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch}
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Boschstatic bool
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Boschcmd_resetkey_mailbox(struct client_command_context *cmd,
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch const char *mailbox, const struct imap_arg *mech_args)
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch{
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch struct mail_namespace *ns;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch enum mailbox_flags flags = MAILBOX_FLAG_READONLY;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch struct mailbox *box;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch /* check mechanism arguments (we support only INTERNAL mechanism) */
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch while (!IMAP_ARG_IS_EOL(mech_args)) {
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch const char *mechanism;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (imap_arg_get_astring(mech_args, &mechanism)) {
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (strcasecmp(mechanism, "INTERNAL") != 0) {
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_tagline(cmd,
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch "NO Unsupported URLAUTH mechanism.");
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch } else {
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_command_error(cmd, "Invalid arguments.");
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch mech_args++;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch /* find mailbox namespace */
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch ns = client_find_namespace(cmd, &mailbox);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (ns == NULL)
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
005eb7a9e93a8799dd00341df9e06938371e644aTimo Sirainen /* open mailbox */
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch box = mailbox_alloc(ns->list, mailbox, flags);
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen mailbox_set_reason(box, "RESETKEY");
005eb7a9e93a8799dd00341df9e06938371e644aTimo Sirainen if (mailbox_open(box) < 0) {
08837f59c1466ec0f533f120b167f2a3e87da738Timo Sirainen client_send_box_error(cmd, box);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch mailbox_free(&box);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch /* check urlauth environment and reset requested key */
fd81c8f5a38df19d1ecdfd5d6605b114ab56841eTimo Sirainen if (imap_urlauth_reset_mailbox_key(cmd->client->urlauth_ctx, box) < 0) {
fd81c8f5a38df19d1ecdfd5d6605b114ab56841eTimo Sirainen client_send_internal_error(cmd);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch mailbox_free(&box);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch /* confirm success */
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch /* FIXME: RFC Says: `Any current IMAP session logged in as the user
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch that has the mailbox selected will receive an untagged OK response
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch with the URLMECH status response code'. We currently don't do that
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch at all. We could probably do it by communicating via mailbox list
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch index. */
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_tagline(cmd, "OK [URLMECH INTERNAL] Key removed.");
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch mailbox_free(&box);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch}
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Boschbool cmd_resetkey(struct client_command_context *cmd)
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch{
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch const struct imap_arg *args;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch const char *mailbox;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (cmd->client->urlauth_ctx == NULL) {
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_command_error(cmd, "URLAUTH disabled.");
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch }
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (!client_read_args(cmd, 0, 0, &args))
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return FALSE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch if (IMAP_ARG_IS_EOL(&args[0]))
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return cmd_resetkey_all(cmd);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch else if (imap_arg_get_astring(&args[0], &mailbox))
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return cmd_resetkey_mailbox(cmd, mailbox, &args[1]);
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch client_send_command_error(cmd, "Invalid arguments.");
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch return TRUE;
f9511e684858bf5f6ac77ab12254b85b737beae8Stephan Bosch}