diff --git a/src/config.h b/src/config.h index 39014d95..ca056dfe 100644 --- a/src/config.h +++ b/src/config.h @@ -13,7 +13,7 @@ #define VERSION "0.1.23-master-branch" -#define BUILD 752 +#define BUILD 761 #define HOSTID "mailarchiver" @@ -87,6 +87,10 @@ #define SQL_MESSAGES_VIEW "v_messages" #define SQL_ATTACHMENTS_VIEW "v_attachment" +#define SQL_PREPARED_STMT_SELECT_MESSAGE_ID "SELECT message_id FROM " SQL_METADATA_TABLE " WHERE message_id=?" +#define SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID "SELECT id FROM " SQL_METADATA_TABLE " WHERE message_id=?" +#define SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE "INSERT INTO " SQL_RECIPIENT_TABLE " (`id`,`to`,`todomain`) VALUES(?,?,?)" + /* Error codes */ diff --git a/src/defs.h b/src/defs.h index 79bba2e4..9e68dbec 100644 --- a/src/defs.h +++ b/src/defs.h @@ -189,7 +189,7 @@ struct session_data { char bodydigest[2*DIGEST_LENGTH+1]; char digest[2*DIGEST_LENGTH+1]; time_t now, sent, delivered, retained; - char ms_journal, ms_journal_dropped; + char ms_journal; int journal_envelope_length, journal_bottom_length; #ifdef NEED_MYSQL MYSQL mysql; diff --git a/src/digest.c b/src/digest.c index 2bbd1f80..f28a685f 100644 --- a/src/digest.c +++ b/src/digest.c @@ -47,16 +47,10 @@ int make_digests(struct session_data *sdata, struct __config *cfg){ fd = open(sdata->filename, O_RDONLY); if(fd == -1) return -1; - if(sdata->ms_journal == 1 && sdata->journal_envelope_length < sizeof(buf)){ - n = read(fd, buf, sdata->journal_envelope_length); - } - while((n = read(fd, buf, sizeof(buf))) > 0){ len += n; - if(sdata->ms_journal == 1 && len > sdata->tot_len) n -= len - sdata->tot_len; - SHA256_Update(&context2, buf, n); body = (char *)&buf[0]; diff --git a/src/misc.c b/src/misc.c index 32fa4523..b83ea6c7 100644 --- a/src/misc.c +++ b/src/misc.c @@ -480,7 +480,6 @@ void init_session_data(struct session_data *sdata, unsigned char server_id){ sdata->num_of_rcpt_to = 0; sdata->ms_journal = 0; - sdata->ms_journal_dropped = 0; sdata->journal_envelope_length = 0; sdata->journal_bottom_length = 0; diff --git a/src/parser.c b/src/parser.c index 92d20a5d..f18d3279 100644 --- a/src/parser.c +++ b/src/parser.c @@ -84,8 +84,6 @@ struct _state parse_message(struct session_data *sdata, int take_into_pieces, st } if(take_into_pieces == 1 && state.writebufpos > 0){ - if(sdata->ms_journal == 1) remove_trailing_journal_boundary(sdata, &state, &writebuffer[0]); - len = write(state.mfd, writebuffer, state.writebufpos); memset(writebuffer, 0, sizeof(writebuffer)); state.writebufpos = 0; @@ -95,10 +93,6 @@ struct _state parse_message(struct session_data *sdata, int take_into_pieces, st close(state.mfd); state.mfd = 0; } - if(sdata->ms_journal == 1){ - sdata->tot_len -= sdata->journal_envelope_length + sdata->journal_bottom_length; - } - fclose(f); return state; @@ -201,11 +195,6 @@ int parse_line(char *buf, struct _state *state, struct session_data *sdata, int } - if(sdata->ms_journal_dropped == 0){ - sdata->journal_envelope_length += len; - } - - if(state->message_rfc822 == 0 && (buf[0] == '\r' || buf[0] == '\n') ){ state->message_state = MSG_BODY; @@ -243,7 +232,6 @@ int parse_line(char *buf, struct _state *state, struct session_data *sdata, int state->saved_size += len; //n = write(state->mfd, buf, len); // WRITE if(len + state->writebufpos > writebuffersize-1){ - if(sdata->ms_journal == 1) remove_trailing_journal_boundary(sdata, state, writebuffer); write(state->mfd, writebuffer, state->writebufpos); state->writebufpos = 0; memset(writebuffer, 0, writebuffersize); } memcpy(writebuffer+state->writebufpos, buf, len); state->writebufpos += len; @@ -388,19 +376,6 @@ int parse_line(char *buf, struct _state *state, struct session_data *sdata, int if(p) *p = '\0'; } - if(state->message_state == MSG_RECEIVED && sdata->ms_journal == 1 && sdata->ms_journal_dropped == 0){ - sdata->ms_journal_dropped = 1; - sdata->journal_envelope_length -= len; - state->writebufpos = 0; memset(writebuffer, 0, writebuffersize); - memcpy(writebuffer+state->writebufpos, buf, strlen(buf)); state->writebufpos += strlen(buf); - if(t == 2){ - memcpy(writebuffer+state->writebufpos, "\r\n", 2); state->writebufpos += 2; - } else { - memcpy(writebuffer+state->writebufpos, "\n", 1); state->writebufpos++; - } - - } - if(state->is_1st_header == 1 && state->message_state == MSG_REFERENCES){ if(strncasecmp(buf, "References:", 11) == 0) parse_reference(state, buf+11); diff --git a/src/parser.h b/src/parser.h index 6c47eaf2..7ebad407 100644 --- a/src/parser.h +++ b/src/parser.h @@ -34,6 +34,5 @@ char *determine_attachment_type(char *filename, char *type); char *get_attachment_extractor_by_filename(char *filename); void parse_reference(struct _state *state, char *s); int base64_decode_attachment_buffer(char *p, int plen, unsigned char *b, int blen); -void remove_trailing_journal_boundary(struct session_data *sdata, struct _state *state, char *writebuffer); #endif /* _PARSER_H */ diff --git a/src/parser_utils.c b/src/parser_utils.c index 56cd2f28..7b5d730d 100644 --- a/src/parser_utils.c +++ b/src/parser_utils.c @@ -811,25 +811,3 @@ int base64_decode_attachment_buffer(char *p, int plen, unsigned char *b, int ble } -void remove_trailing_journal_boundary(struct session_data *sdata, struct _state *state, char *writebuffer){ - int len; - char *p; - - p = strstr(writebuffer, state->boundaries->s); - if(p){ - len = strlen(p); - sdata->journal_bottom_length = len; - state->writebufpos -= len; - *p = '\0'; - p = strrchr(writebuffer, '\n'); - if(p){ - len = strlen(p); - sdata->journal_bottom_length += len; - state->writebufpos -= len; - *p = '\0'; - } - } - -} - - diff --git a/webui/controller/message/bulkrestore.php b/webui/controller/message/bulkrestore.php index 15a569a6..beef75dd 100644 --- a/webui/controller/message/bulkrestore.php +++ b/webui/controller/message/bulkrestore.php @@ -58,8 +58,11 @@ class ControllerMessageBulkrestore extends Controller { if(count($rcpt) > 0) { $piler_id = $this->model_search_message->get_piler_id_by_id($id); + $msg = $this->model_search_message->get_raw_message($piler_id); + $this->model_search_message->remove_journal($msg); + $x = $this->model_mail_mail->send_smtp_email(SMARTHOST, SMARTHOST_PORT, SMTP_DOMAIN, SMTP_FROMADDR, $rcpt, - "Received: by piler" . EOL . PILER_HEADER_FIELD . $id . EOL . $this->model_search_message->get_raw_message($piler_id) ); + "Received: by piler" . EOL . PILER_HEADER_FIELD . $id . EOL . $msg ); if($x == 1) { $this->data['restored']++; } } diff --git a/webui/controller/message/download.php b/webui/controller/message/download.php index 58b2126f..27825c07 100644 --- a/webui/controller/message/download.php +++ b/webui/controller/message/download.php @@ -41,10 +41,9 @@ class ControllerMessageDownload extends Controller { header("Content-Transfer-Encoding: binary\n"); - print $this->model_search_message->get_raw_message($this->data['piler_id']); - exit; - - $this->render(); + $msg = $this->model_search_message->get_raw_message($this->data['piler_id']); + $this->model_search_message->remove_journal($msg); + print $msg; } diff --git a/webui/controller/message/restore.php b/webui/controller/message/restore.php index f26b8b42..30e8588b 100644 --- a/webui/controller/message/restore.php +++ b/webui/controller/message/restore.php @@ -58,8 +58,11 @@ class ControllerMessageRestore extends Controller { $this->data['piler_id'] = $this->model_search_message->get_piler_id_by_id($this->data['id']); + $msg = $this->model_search_message->get_raw_message($this->data['piler_id']); + $this->model_search_message->remove_journal($msg); + $x = $this->model_mail_mail->send_smtp_email(SMARTHOST, SMARTHOST_PORT, SMTP_DOMAIN, SMTP_FROMADDR, $rcpt, - "Received: by piler" . EOL . PILER_HEADER_FIELD . $this->data['id'] . EOL . $this->model_search_message->get_raw_message($this->data['piler_id']) ); + "Received: by piler" . EOL . PILER_HEADER_FIELD . $this->data['id'] . EOL . $msg ); if($x == 1) { $this->data['data'] = $this->data['text_restored']; } } diff --git a/webui/model/search/message.php b/webui/model/search/message.php index 1971234f..83209ee9 100644 --- a/webui/model/search/message.php +++ b/webui/model/search/message.php @@ -68,6 +68,8 @@ class ModelSearchMessage extends Model { $msg = $this->get_raw_message($id); + $this->remove_journal($msg); + $pos = strpos($msg, "\n\r\n"); if($pos == false) { $pos = strpos($msg, "\n\n"); @@ -85,6 +87,38 @@ class ModelSearchMessage extends Model { } + public function remove_journal(&$msg = '') { + $p = $q = ''; + $boundary = ''; + + $hdr = substr($msg, 0, 4096); + + $s = preg_split("/\n/", $hdr); + while(list($k, $v) = each($s)) { + if(preg_match("/boundary\s{0,}=\s{0,}\"{0,}([\w\_\-\@\.]+)\"{0,}/i", $v, $m)) { + if(isset($m[1])) { $boundary = $m[1]; break; } + } + } + + $p = strstr($msg, "\nX-MS-Journal-Report:"); + if($p) { + $msg = ''; + $q = strstr($p, "Received: from"); + if($q) { + $p = ''; + $msg = $q; + $q = ''; + } + else { + $msg = $p; + $p = ''; + } + } + + $msg = substr($msg, 0, strlen($msg) - strlen($boundary) - 6); + } + + public function extract_message($id = '') { $header = ""; $body_chunk = ""; @@ -102,6 +136,8 @@ class ModelSearchMessage extends Model { $msg = $this->get_raw_message($id); + $this->remove_journal($msg); + $a = explode("\n", $msg); $msg = ""; while(list($k, $l) = each($a)){