Skip to content

Commit a01a3b9

Browse files
committed
Fixed parser issue for attachment directly in the email, no mime
Signed-off-by: Janos SUTO <[email protected]>
1 parent 39fc249 commit a01a3b9

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

src/defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct parser_state {
166166
int style;
167167
int skip_html;
168168
int has_to_dump;
169+
int has_to_dump_whole_body;
169170
int fd;
170171
int b64fd;
171172
int mfd;

src/parser.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ struct parser_state parse_message(struct session_data *sdata, int take_into_piec
5151

5252
if(take_into_pieces == 1){
5353
close(state.mfd); state.mfd = 0;
54+
55+
if(state.has_to_dump_whole_body == 1){
56+
if(state.abufpos > 0){
57+
flush_attachment_buffer(&state, &abuffer[0], sizeof(abuffer));
58+
}
59+
if(state.fd != -1) close(state.fd);
60+
if(state.b64fd != -1) close(state.b64fd);
61+
}
62+
5463
}
5564

5665
fclose(f);
@@ -218,13 +227,16 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
218227

219228

220229
if(take_into_pieces == 1){
221-
if(state->message_state == MSG_BODY && state->fd != -1 && is_substr_in_hash(state->boundaries, buf) == 0){
230+
if(state->message_state == MSG_BODY && state->fd != -1 && (state->has_to_dump_whole_body == 1 || is_substr_in_hash(state->boundaries, buf) == 0) ){
222231
if(len + state->abufpos > abuffersize-1){
223232
flush_attachment_buffer(state, abuffer, abuffersize);
224233
}
225234
memcpy(abuffer+state->abufpos, buf, len); state->abufpos += len;
226235

227236
state->attachments[state->n_attachments].size += len;
237+
238+
// When processing the body and writing to an attachment file, then we finish here
239+
return 0;
228240
}
229241
else {
230242
state->saved_size += len;
@@ -238,7 +250,7 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
238250
}
239251

240252

241-
if(state->message_state == MSG_BODY && state->has_to_dump == 1 && state->pushed_pointer == 0){
253+
if(state->message_state == MSG_BODY && state->has_to_dump == 1 && state->pushed_pointer == 0){
242254
state->pushed_pointer = 1;
243255

244256

@@ -340,7 +352,13 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
340352
else if(strncasecmp(buf, "Content-Type:", strlen("Content-Type:")) == 0){
341353
state->message_state = MSG_CONTENT_TYPE;
342354
}
343-
else if(strncasecmp(buf, "Content-Transfer-Encoding:", strlen("Content-Transfer-Encoding:")) == 0) state->message_state = MSG_CONTENT_TRANSFER_ENCODING;
355+
else if(strncasecmp(buf, "Content-Transfer-Encoding:", strlen("Content-Transfer-Encoding:")) == 0){
356+
state->message_state = MSG_CONTENT_TRANSFER_ENCODING;
357+
if(state->is_1st_header == 1 && strcasestr(buf, "base64")){
358+
state->has_to_dump = 1;
359+
state->has_to_dump_whole_body = 1;
360+
}
361+
}
344362

345363
/*
346364
* 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
408426
snprintf(state->message_id, SMALLBUFSIZE-1, "%s", p);
409427
}
410428

429+
if(state->message_state == MSG_CONTENT_TYPE || state->message_state == MSG_CONTENT_DISPOSITION){
430+
fill_attachment_name_buf(state, buf);
431+
}
432+
411433
/* we are interested in only From:, To:, Subject:, Received:, Content-*: header lines */
412434
if(state->message_state <= 0) return 0;
413435
}
@@ -420,7 +442,7 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
420442
}
421443

422444

423-
/*
445+
/*
424446
* A normal journal looks like this:
425447
*
426448
* Sender: sender@domain
@@ -526,21 +548,6 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
526548
}
527549

528550

529-
if(state->message_state == MSG_CONTENT_TYPE || state->message_state == MSG_CONTENT_DISPOSITION){
530-
p = &buf[0];
531-
for(; *p; p++){
532-
if(*p != ' ' && *p != '\t') break;
533-
}
534-
535-
len = strlen(p);
536-
537-
if(len + state->anamepos < SMALLBUFSIZE-2){
538-
memcpy(&(state->attachment_name_buf[state->anamepos]), p, len);
539-
state->anamepos += len;
540-
}
541-
}
542-
543-
544551
if(state->message_state == MSG_CONTENT_TRANSFER_ENCODING){
545552
if(strcasestr(buf, "base64")) state->base64 = 1;
546553
if(strcasestr(buf, "quoted-printable")) state->qp = 1;

src/parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ void parse_reference(struct parser_state *state, char *s);
3636
int base64_decode_attachment_buffer(char *p, unsigned char *b, int blen);
3737
void fix_plus_sign_in_email_address(char *puf, char **at_sign, unsigned int *len);
3838
void tokenize(char *buf, struct parser_state *state, struct session_data *sdata, struct data *data, struct config *cfg);
39+
void flush_attachment_buffer(struct parser_state *state, char *abuffer, unsigned int abuffersize);
40+
void fill_attachment_name_buf(struct parser_state *state, char *buf);
3941

4042
#endif /* _PARSER_H */

src/parser_utils.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void init_state(struct parser_state *state){
5656
state->anamepos = 0;
5757

5858
state->has_to_dump = 0;
59+
state->has_to_dump_whole_body = 0;
5960
state->fd = -1;
6061
state->b64fd = -1;
6162
state->mfd = -1;
@@ -1055,3 +1056,19 @@ void fix_plus_sign_in_email_address(char *puf, char **at_sign, unsigned int *len
10551056
*at_sign = r;
10561057
}
10571058
}
1059+
1060+
1061+
void fill_attachment_name_buf(struct parser_state *state, char *buf){
1062+
char *p = &buf[0];
1063+
1064+
for(; *p; p++){
1065+
if(*p != ' ' && *p != '\t') break;
1066+
}
1067+
1068+
int len = strlen(p);
1069+
1070+
if(len + state->anamepos < SMALLBUFSIZE-2){
1071+
memcpy(&(state->attachment_name_buf[state->anamepos]), p, len);
1072+
state->anamepos += len;
1073+
}
1074+
}

0 commit comments

Comments
 (0)