Fixed parser issue for attachment directly in the email, no mime

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2020-03-14 07:43:19 +01:00
parent 39fc249596
commit a01a3b9eb5
4 changed files with 46 additions and 19 deletions

View File

@ -166,6 +166,7 @@ struct parser_state {
int style; int style;
int skip_html; int skip_html;
int has_to_dump; int has_to_dump;
int has_to_dump_whole_body;
int fd; int fd;
int b64fd; int b64fd;
int mfd; int mfd;

View File

@ -51,6 +51,15 @@ struct parser_state parse_message(struct session_data *sdata, int take_into_piec
if(take_into_pieces == 1){ if(take_into_pieces == 1){
close(state.mfd); state.mfd = 0; close(state.mfd); state.mfd = 0;
if(state.has_to_dump_whole_body == 1){
if(state.abufpos > 0){
flush_attachment_buffer(&state, &abuffer[0], sizeof(abuffer));
}
if(state.fd != -1) close(state.fd);
if(state.b64fd != -1) close(state.b64fd);
}
} }
fclose(f); fclose(f);
@ -218,13 +227,16 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
if(take_into_pieces == 1){ if(take_into_pieces == 1){
if(state->message_state == MSG_BODY && state->fd != -1 && is_substr_in_hash(state->boundaries, buf) == 0){ if(state->message_state == MSG_BODY && state->fd != -1 && (state->has_to_dump_whole_body == 1 || is_substr_in_hash(state->boundaries, buf) == 0) ){
if(len + state->abufpos > abuffersize-1){ if(len + state->abufpos > abuffersize-1){
flush_attachment_buffer(state, abuffer, abuffersize); flush_attachment_buffer(state, abuffer, abuffersize);
} }
memcpy(abuffer+state->abufpos, buf, len); state->abufpos += len; memcpy(abuffer+state->abufpos, buf, len); state->abufpos += len;
state->attachments[state->n_attachments].size += len; state->attachments[state->n_attachments].size += len;
// When processing the body and writing to an attachment file, then we finish here
return 0;
} }
else { else {
state->saved_size += len; state->saved_size += len;
@ -340,7 +352,13 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
else if(strncasecmp(buf, "Content-Type:", strlen("Content-Type:")) == 0){ else if(strncasecmp(buf, "Content-Type:", strlen("Content-Type:")) == 0){
state->message_state = MSG_CONTENT_TYPE; state->message_state = MSG_CONTENT_TYPE;
} }
else if(strncasecmp(buf, "Content-Transfer-Encoding:", strlen("Content-Transfer-Encoding:")) == 0) state->message_state = MSG_CONTENT_TRANSFER_ENCODING; else if(strncasecmp(buf, "Content-Transfer-Encoding:", strlen("Content-Transfer-Encoding:")) == 0){
state->message_state = MSG_CONTENT_TRANSFER_ENCODING;
if(state->is_1st_header == 1 && strcasestr(buf, "base64")){
state->has_to_dump = 1;
state->has_to_dump_whole_body = 1;
}
}
/* /*
* We only enter MSG_CONTENT_DISPOSITION state if we couldn't find * We only enter MSG_CONTENT_DISPOSITION state if we couldn't find
@ -408,6 +426,10 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
snprintf(state->message_id, SMALLBUFSIZE-1, "%s", p); snprintf(state->message_id, SMALLBUFSIZE-1, "%s", p);
} }
if(state->message_state == MSG_CONTENT_TYPE || state->message_state == MSG_CONTENT_DISPOSITION){
fill_attachment_name_buf(state, buf);
}
/* we are interested in only From:, To:, Subject:, Received:, Content-*: header lines */ /* we are interested in only From:, To:, Subject:, Received:, Content-*: header lines */
if(state->message_state <= 0) return 0; if(state->message_state <= 0) return 0;
} }
@ -526,21 +548,6 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
} }
if(state->message_state == MSG_CONTENT_TYPE || state->message_state == MSG_CONTENT_DISPOSITION){
p = &buf[0];
for(; *p; p++){
if(*p != ' ' && *p != '\t') break;
}
len = strlen(p);
if(len + state->anamepos < SMALLBUFSIZE-2){
memcpy(&(state->attachment_name_buf[state->anamepos]), p, len);
state->anamepos += len;
}
}
if(state->message_state == MSG_CONTENT_TRANSFER_ENCODING){ if(state->message_state == MSG_CONTENT_TRANSFER_ENCODING){
if(strcasestr(buf, "base64")) state->base64 = 1; if(strcasestr(buf, "base64")) state->base64 = 1;
if(strcasestr(buf, "quoted-printable")) state->qp = 1; if(strcasestr(buf, "quoted-printable")) state->qp = 1;

View File

@ -36,5 +36,7 @@ void parse_reference(struct parser_state *state, char *s);
int base64_decode_attachment_buffer(char *p, unsigned char *b, int blen); int base64_decode_attachment_buffer(char *p, unsigned char *b, int blen);
void fix_plus_sign_in_email_address(char *puf, char **at_sign, unsigned int *len); void fix_plus_sign_in_email_address(char *puf, char **at_sign, unsigned int *len);
void tokenize(char *buf, struct parser_state *state, struct session_data *sdata, struct data *data, struct config *cfg); void tokenize(char *buf, struct parser_state *state, struct session_data *sdata, struct data *data, struct config *cfg);
void flush_attachment_buffer(struct parser_state *state, char *abuffer, unsigned int abuffersize);
void fill_attachment_name_buf(struct parser_state *state, char *buf);
#endif /* _PARSER_H */ #endif /* _PARSER_H */

View File

@ -56,6 +56,7 @@ void init_state(struct parser_state *state){
state->anamepos = 0; state->anamepos = 0;
state->has_to_dump = 0; state->has_to_dump = 0;
state->has_to_dump_whole_body = 0;
state->fd = -1; state->fd = -1;
state->b64fd = -1; state->b64fd = -1;
state->mfd = -1; state->mfd = -1;
@ -1055,3 +1056,19 @@ void fix_plus_sign_in_email_address(char *puf, char **at_sign, unsigned int *len
*at_sign = r; *at_sign = r;
} }
} }
void fill_attachment_name_buf(struct parser_state *state, char *buf){
char *p = &buf[0];
for(; *p; p++){
if(*p != ' ' && *p != '\t') break;
}
int len = strlen(p);
if(len + state->anamepos < SMALLBUFSIZE-2){
memcpy(&(state->attachment_name_buf[state->anamepos]), p, len);
state->anamepos += len;
}
}