bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen#include "lib.h"
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen#include "istream.h"
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen#include "mail-storage.h"
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen#include "doveadm-mail.h"
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainenstruct save_cmd_context {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct doveadm_mail_cmd_context ctx;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen const char *mailbox;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen};
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainenstatic int
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainencmd_save_to_mailbox(struct save_cmd_context *ctx, struct mailbox *box,
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct istream *input)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen{
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct mail_storage *storage = mailbox_get_storage(box);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct mailbox_transaction_context *trans;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct mail_save_context *save_ctx;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ssize_t ret;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen bool save_failed = FALSE;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi if (input->stream_errno != 0) {
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi i_error("open(%s) failed: %s",
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi i_stream_get_name(input),
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi i_stream_get_error(input));
60bea13e11a4e95e915c64112783d86204697b19Aki Tuomi ctx->ctx.exit_code = EX_TEMPFAIL;
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi return -1;
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi }
0bff25fd39f3d1e859f71d45695f98c9a95593d8Aki Tuomi
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen if (mailbox_open(box) < 0) {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen i_error("Failed to open mailbox %s: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_vname(box),
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return -1;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen }
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi trans = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL,
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi __func__);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen save_ctx = mailbox_save_alloc(trans);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen if (mailbox_save_begin(&save_ctx, input) < 0) {
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi i_error("Saving failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen mailbox_transaction_rollback(&trans);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return -1;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen }
7f74811b78f8915e73dffc88bb49009e98b6846dTimo Sirainen do {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen if (mailbox_save_continue(save_ctx) < 0) {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen save_failed = TRUE;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ret = -1;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen break;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen }
7f74811b78f8915e73dffc88bb49009e98b6846dTimo Sirainen } while ((ret = i_stream_read(input)) > 0);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen i_assert(ret == -1);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen if (input->stream_errno != 0) {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen i_error("read(msg input) failed: %s", i_stream_get_error(input));
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_failed_error(&ctx->ctx, MAIL_ERROR_TEMP);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen } else if (save_failed) {
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi i_error("Saving failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen } else if (mailbox_save_finish(&save_ctx) < 0) {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen i_error("Saving failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen } else if (mailbox_transaction_commit(&trans) < 0) {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen i_error("Save transaction commit failed: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen } else {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ret = 0;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen }
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen if (save_ctx != NULL)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen mailbox_save_cancel(&save_ctx);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen if (trans != NULL)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen mailbox_transaction_rollback(&trans);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen i_assert(input->eof);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return ret < 0 ? -1 : 0;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen}
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainenstatic int
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainencmd_save_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen{
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct mail_namespace *ns;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct mailbox *box;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen int ret;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ns = mail_namespace_find(user->namespaces, ctx->mailbox);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen box = mailbox_alloc(ns->list, ctx->mailbox, MAILBOX_FLAG_SAVEONLY);
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen mailbox_set_reason(box, _ctx->cmd->name);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ret = cmd_save_to_mailbox(ctx, box, _ctx->cmd_input);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen mailbox_free(&box);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return ret;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen}
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainenstatic void cmd_save_init(struct doveadm_mail_cmd_context *_ctx,
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen const char *const args[] ATTR_UNUSED)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen{
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen doveadm_mail_get_input(_ctx);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen}
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainenstatic bool
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainencmd_mailbox_save_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen{
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen switch (c) {
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen case 'm':
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx->mailbox = optarg;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen break;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen default:
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return FALSE;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen }
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return TRUE;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen}
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainenstatic struct doveadm_mail_cmd_context *cmd_save_alloc(void)
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen{
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen struct save_cmd_context *ctx;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct save_cmd_context);
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx->ctx.getopt_args = "m:";
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx->ctx.v.parse_arg = cmd_mailbox_save_parse_arg;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx->ctx.v.init = cmd_save_init;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx->ctx.v.run = cmd_save_run;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen ctx->mailbox = "INBOX";
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen return &ctx->ctx;
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen}
e617d6e7db43c176502ac02b65d2dcb4d27103deTimo Sirainen
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki Tuomistruct doveadm_cmd_ver2 doveadm_cmd_save_ver2 = {
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki Tuomi .name = "save",
c45a841bee3f42ec6524b8f62c3fd457115c3f97Timo Sirainen .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX"[-m mailbox]",
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki Tuomi .mail_cmd = cmd_save_alloc,
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki TuomiDOVEADM_CMD_PARAMS_START
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki TuomiDOVEADM_CMD_MAIL_COMMON
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki TuomiDOVEADM_CMD_PARAM('m', "mailbox", CMD_PARAM_STR, 0)
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki TuomiDOVEADM_CMD_PARAM('\0', "file", CMD_PARAM_ISTREAM, CMD_PARAM_FLAG_POSITIONAL)
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki TuomiDOVEADM_CMD_PARAMS_END
5ba6009f4e5493c4e6be9ffb3134525004a7975cAki Tuomi};