bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "lib.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "istream.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "str.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "dbox-file.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "dbox-save.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "dbox-attachment.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenvoid dbox_attachment_save_write_metadata(struct mail_save_context *ctx,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen string_t *str)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen{
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen const ARRAY_TYPE(mail_attachment_extref) *extrefs;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen extrefs = index_attachment_save_get_extrefs(ctx);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen if (extrefs == NULL || array_count(extrefs) == 0)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen str_append_c(str, DBOX_METADATA_EXT_REF);
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen index_attachment_append_extrefs(str, extrefs);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen str_append_c(str, '\n');
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenstatic int
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainendbox_attachment_file_get_stream_from(struct dbox_file *file,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen const char *ext_refs,
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen struct istream **stream,
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen const char **error_r)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen{
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen const char *path_suffix;
338088fe2875e8039d2b3df32cbd7a8396b24ea6Timo Sirainen uoff_t msg_size;
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen
84af637240881b8c1cc4df472832f49de952e4c6Timo Sirainen if (file->storage->attachment_dir == NULL) {
66a1eb655401702405cd8adc23142efe7a1dd9a5Timo Sirainen mail_storage_set_critical(&file->storage->storage,
66a1eb655401702405cd8adc23142efe7a1dd9a5Timo Sirainen "%s contains references to external attachments, "
66a1eb655401702405cd8adc23142efe7a1dd9a5Timo Sirainen "but mail_attachment_dir is unset", file->cur_path);
66a1eb655401702405cd8adc23142efe7a1dd9a5Timo Sirainen return -1;
66a1eb655401702405cd8adc23142efe7a1dd9a5Timo Sirainen }
66a1eb655401702405cd8adc23142efe7a1dd9a5Timo Sirainen
338088fe2875e8039d2b3df32cbd7a8396b24ea6Timo Sirainen msg_size = dbox_file_get_plaintext_size(file);
338088fe2875e8039d2b3df32cbd7a8396b24ea6Timo Sirainen path_suffix = file->storage->v.get_attachment_path_suffix(file);
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen if (index_attachment_stream_get(file->storage->attachment_fs,
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen file->storage->attachment_dir,
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen path_suffix, stream, msg_size,
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen ext_refs, error_r) < 0)
52de839a8249bff5eace53dc1401b28baa0c124bTimo Sirainen return 0;
338088fe2875e8039d2b3df32cbd7a8396b24ea6Timo Sirainen return 1;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenint dbox_attachment_file_get_stream(struct dbox_file *file,
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen struct istream **stream)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen{
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen const char *ext_refs, *error;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen int ret;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* need to read metadata in case there are external references */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen if ((ret = dbox_file_metadata_read(file)) <= 0)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return ret;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen i_stream_seek(file->input, file->cur_offset + file->msg_header_size);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ext_refs = dbox_file_metadata_get(file, DBOX_METADATA_EXT_REF);
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen if (ext_refs == NULL)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return 1;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* we have external references. */
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen T_BEGIN {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ret = dbox_attachment_file_get_stream_from(file, ext_refs,
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen stream, &error);
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen if (ret == 0) {
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen dbox_file_set_corrupted(file,
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen "Corrupted ext-refs metadata %s: %s",
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen ext_refs, error);
7d73c8f29360df6f1705c0f6670b9030632772f6Timo Sirainen }
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen } T_END;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return ret;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}