doveadm-mail-save.c revision 5ba6009f4e5493c4e6be9ffb3134525004a7975c
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2015-2016 Dovecot authors, see the included COPYING file */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "lib.h"
b44650b0f48a4b5f0dc240ed836833a00b643b9fTimo Sirainen#include "istream.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "mail-storage.h"
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen#include "doveadm-mail.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenstruct save_cmd_context {
be5c76fabc7439fd33bc799bc3ab3f570799977bTimo Sirainen struct doveadm_mail_cmd_context ctx;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen const char *mailbox;
b0e9375a1ff97c9c7d40655922af5ccc73ecaa76Timo Sirainen};
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstatic int
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainencmd_save_to_mailbox(struct save_cmd_context *ctx, struct mailbox *box,
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct istream *input)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen{
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct mail_storage *storage = mailbox_get_storage(box);
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen struct mailbox_transaction_context *trans;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct mail_save_context *save_ctx;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ssize_t ret;
b624773984e35dd894db8dff976c1a2114c70782Timo Sirainen bool save_failed = FALSE;
5afc76d0215c5f7631dec06ef864d59f0686a0a8Timo Sirainen
b624773984e35dd894db8dff976c1a2114c70782Timo Sirainen if (mailbox_open(box) < 0) {
12d38e76ba7f70d6219c89ec7416fea0d5de7e02Timo Sirainen i_error("Failed to open mailbox %s: %s",
b624773984e35dd894db8dff976c1a2114c70782Timo Sirainen mailbox_get_vname(box), mailbox_get_last_error(box, NULL));
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen return -1;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen }
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen trans = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen save_ctx = mailbox_save_alloc(trans);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (mailbox_save_begin(&save_ctx, input) < 0) {
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen i_error("Saving failed: %s", mailbox_get_last_error(box, NULL));
3a7113e3e2dac0e333e1a3f62af7d682896f59c6Timo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen mailbox_transaction_rollback(&trans);
be5c76fabc7439fd33bc799bc3ab3f570799977bTimo Sirainen return -1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen }
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen while ((ret = i_stream_read(input)) > 0 || ret == -2) {
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen if (mailbox_save_continue(save_ctx) < 0) {
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen save_failed = TRUE;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ret = -1;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen break;
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen }
e9371f899a3d4207a0ffd3923ea5ec7250cf5e75Timo Sirainen }
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen i_assert(ret == -1);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen if (input->stream_errno != 0) {
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen i_error("read(msg input) failed: %s", i_stream_get_error(input));
788a0754cfd38dcfec1902844b085e4e84cfe7e6Timo Sirainen doveadm_mail_failed_error(&ctx->ctx, MAIL_ERROR_TEMP);
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen } else if (save_failed) {
788a0754cfd38dcfec1902844b085e4e84cfe7e6Timo Sirainen i_error("Saving failed: %s", mailbox_get_last_error(box, NULL));
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen } else if (mailbox_save_finish(&save_ctx) < 0) {
43d3ea2780b5f8557ede7b4c039e8f56cb8d357dTimo Sirainen i_error("Saving failed: %s",
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen mailbox_get_last_error(box, NULL));
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen } else if (mailbox_transaction_commit(&trans) < 0) {
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen i_error("Save transaction commit failed: %s",
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen mailbox_get_last_error(box, NULL));
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen doveadm_mail_failed_storage(&ctx->ctx, storage);
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen } else {
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen ret = 0;
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen }
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen if (save_ctx != NULL)
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen mailbox_save_cancel(&save_ctx);
f93c833d644ecff0b0f80bee4f1cdde3e697b5c8Timo Sirainen if (trans != NULL)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen mailbox_transaction_rollback(&trans);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen i_assert(input->eof);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen return ret < 0 ? -1 : 0;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenstatic int
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainencmd_save_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen{
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct mail_namespace *ns;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen struct mailbox *box;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen int ret;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ns = mail_namespace_find(user->namespaces, ctx->mailbox);
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen box = mailbox_alloc(ns->list, ctx->mailbox, MAILBOX_FLAG_SAVEONLY);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ret = cmd_save_to_mailbox(ctx, box, _ctx->cmd_input);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen mailbox_free(&box);
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen return ret;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenstatic void cmd_save_init(struct doveadm_mail_cmd_context *_ctx,
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen const char *const args[] ATTR_UNUSED)
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen{
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen doveadm_mail_get_input(_ctx);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen}
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainenstatic bool
83d2e37f065eabe38dc92db485c5ca39ee43ce05Timo Sirainencmd_mailbox_save_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
83d2e37f065eabe38dc92db485c5ca39ee43ce05Timo Sirainen{
83d2e37f065eabe38dc92db485c5ca39ee43ce05Timo Sirainen struct save_cmd_context *ctx = (struct save_cmd_context *)_ctx;
31597236d79ac38a5cea7ab65a9d0a3df64ed201Timo Sirainen
7569ab8537418b7fc369265f26595b0ef9e4cb35Timo Sirainen switch (c) {
7569ab8537418b7fc369265f26595b0ef9e4cb35Timo Sirainen case 'm':
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen ctx->mailbox = optarg;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen break;
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen default:
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen return FALSE;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen }
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen return TRUE;
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen}
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen
798cfe56c9871262770384da1239162b3800cce1Timo Sirainenstatic struct doveadm_mail_cmd_context *cmd_save_alloc(void)
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen{
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen struct save_cmd_context *ctx;
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct save_cmd_context);
3a7113e3e2dac0e333e1a3f62af7d682896f59c6Timo Sirainen ctx->ctx.getopt_args = "m:";
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen ctx->ctx.v.parse_arg = cmd_mailbox_save_parse_arg;
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen ctx->ctx.v.init = cmd_save_init;
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen ctx->ctx.v.run = cmd_save_run;
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen ctx->mailbox = "INBOX";
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen return &ctx->ctx;
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen}
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainenstruct doveadm_mail_cmd cmd_save = {
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen cmd_save_alloc, "save", "[-m mailbox]"
d6bffcdf187c155dccc04fb4267b4f82ce59347dTimo Sirainen};
c0757c70cfd2c9b44de3504b753a4d2f38690ef0Timo Sirainen
798cfe56c9871262770384da1239162b3800cce1Timo Sirainenstruct doveadm_cmd_ver2 doveadm_cmd_save_ver2 = {
798cfe56c9871262770384da1239162b3800cce1Timo Sirainen .name = "save",
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo Sirainen .usage = "[-m mailbox]",
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo Sirainen .mail_cmd = cmd_save_alloc,
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo SirainenDOVEADM_CMD_PARAMS_START
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo SirainenDOVEADM_CMD_MAIL_COMMON
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo SirainenDOVEADM_CMD_PARAM('m', "mailbox", CMD_PARAM_STR, 0)
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo SirainenDOVEADM_CMD_PARAM('\0', "file", CMD_PARAM_ISTREAM, CMD_PARAM_FLAG_POSITIONAL)
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo SirainenDOVEADM_CMD_PARAMS_END
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo Sirainen};
a138ac12134564b151f00fdef86fba9cd9ba8af0Timo Sirainen