dbox-file-fix.c revision 54c4c55a728f1ffb0a3a8a0666346f6d98c86eb6
/* Copyright (c) 2009-2014 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "hex-dec.h"
#include "istream.h"
#include "ostream.h"
#include "message-size.h"
#include "dbox-storage.h"
#include "dbox-file.h"
#include <stdio.h>
#define DBOX_MAIL_FILE_BROKEN_COPY_SUFFIX ".broken"
static int
{
const struct dbox_message_header *hdr;
const unsigned char *data;
if (data[0] == '\n') {
}
return -1;
}
return 0;
return 0;
return 0;
return 0;
*pre_offset = offset;
return 1;
}
{
unsigned int i;
for (i = 0; i < len; i++) {
return TRUE;
}
if (data[i] < ' ')
return FALSE;
}
return TRUE;
}
static int
{
const unsigned char *data, *p;
bool allow_control;
return -1;
}
return 0;
/* see if the metadata block looks valid */
switch (data[i]) {
case '\n':
return 1;
case DBOX_METADATA_GUID:
case DBOX_METADATA_POP3_UIDL:
/* these could contain anything */
break;
case DBOX_METADATA_POP3_ORDER:
case DBOX_METADATA_EXT_REF:
/* no control chars */
break;
default:
return 0;
/* unknown */
break;
}
if (allow_control) {
} else {
return 0;
}
if (p == NULL) {
/* LF not found - try to find the end-of-metadata LF */
if (input_full) {
/* can't look any further - assume it's ok */
return 1;
}
return -1;
}
i = p - data+1;
}
return -1;
}
static int
{
/* We're scanning message bodies here, trying to find the beginning of
the next message. Although our magic strings are very unlikely to
be found in regular emails, they are much more likely when emails
are stored compressed.. So try to be sure we find the correct
magic markers. */
ret == -2) {
continue;
}
/* beginning of the file */
} else {
continue;
}
if (match < 0) {
/* more data needed */
if (ret == -2) {
}
continue;
}
if (match > 0)
if (match < 0) {
/* more data needed */
if (ret == -2) {
}
continue;
}
if (match > 0) {
post_offset < pre_offset) {
}
}
*offset_r = pre_offset;
ret = 1;
break;
}
}
if (ret <= 0) {
if (input->stream_errno != 0)
else {
ret = 0;
}
}
}
static int
{
if (errno != 0) {
return -1;
}
if (o_stream_nfinish(output) < 0) {
"write(%s) failed: %m", out_path);
return -1;
}
"o_stream_send_istream(%s) copied only %"
return -1;
}
return 0;
}
{
const unsigned char *data;
/* if there's LF close to our position, assume that the header ends
there. */
for (i = 0; i < size; i++) {
if (data[i] == '\n') {
return;
}
}
/* skip at least the magic bytes if possible */
}
static void
bool *have_guid_r)
{
const char *line;
*have_guid_r = FALSE;
/* end of metadata */
return;
}
if (*line < 32) {
/* broken - possibly a new pre-magic block */
return;
}
if (*line == DBOX_METADATA_VIRTUAL_SIZE) {
/* it may be wrong - recreate it */
continue;
}
if (*line == DBOX_METADATA_GUID)
*have_guid_r = TRUE;
}
}
static int
{
struct dbox_message_header msg_hdr;
struct message_size body;
bool has_nuls;
struct istream *body_input;
int ret;
if (start_offset > 0) {
/* copy the valid data */
return -1;
} else {
/* the file header is broken. recreate it */
return -1;
}
}
/* probably some garbage or some broken headers.
we most likely don't miss anything by skipping
over this data. */
if (ret <= 0) {
if (ret < 0)
return -1;
} else {
}
if (ret <= 0)
break;
/* msg header ok, copy it */
file->msg_header_size) < 0)
return -1;
} else {
/* msg header is broken. write our own. */
/* previous magic find might have
skipped too much. seek back and
make sure */
if (ret <= 0)
break;
}
write_header = TRUE;
}
} else {
/* treat this data as a separate message. */
write_header = TRUE;
}
/* write msg header */
if (write_header) {
}
/* write msg body */
return -1;
/* get message body size */
if (ret < 0) {
return -1;
}
/* write msg metadata. */
if (ret < 0)
return -1;
if (ret == 0)
else
if (!have_guid) {
}
(unsigned long long)body.virtual_size));
if (output->stream_errno != 0)
break;
}
if (o_stream_nfinish(output) < 0) {
"write(%s) failed: %m", temp_path);
ret = -1;
}
return ret;
}
{
bool deleted, have_messages;
if (fd == -1)
return -1;
if (ret < 0)
"close(%s) failed: %m", temp_path);
ret = -1;
}
if (ret < 0) {
"unlink(%s) failed: %m", temp_path);
}
return -1;
}
/* keep a copy of the original file in case someone wants to look
at it */
"link(%s, %s) failed: %m",
} else {
i_warning("dbox: Copy of the broken file saved to %s",
}
if (!have_messages) {
/* the resulting file has no messages. just delete the file. */
return 0;
}
"rename(%s, %s) failed: %m",
return -1;
}
/* file was successfully recreated - reopen it */
"dbox_file_fix(%s): reopening file failed",
return -1;
}
return 1;
}