@@ -51,6 +51,15 @@ struct parser_state parse_message(struct session_data *sdata, int take_into_piec
51
51
52
52
if (take_into_pieces == 1 ){
53
53
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
+
54
63
}
55
64
56
65
fclose (f );
@@ -218,13 +227,16 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
218
227
219
228
220
229
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 ) ){
222
231
if (len + state -> abufpos > abuffersize - 1 ){
223
232
flush_attachment_buffer (state , abuffer , abuffersize );
224
233
}
225
234
memcpy (abuffer + state -> abufpos , buf , len ); state -> abufpos += len ;
226
235
227
236
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 ;
228
240
}
229
241
else {
230
242
state -> saved_size += len ;
@@ -238,7 +250,7 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
238
250
}
239
251
240
252
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 ){
242
254
state -> pushed_pointer = 1 ;
243
255
244
256
@@ -340,7 +352,13 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
340
352
else if (strncasecmp (buf , "Content-Type:" , strlen ("Content-Type:" )) == 0 ){
341
353
state -> message_state = MSG_CONTENT_TYPE ;
342
354
}
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
+ }
344
362
345
363
/*
346
364
* 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
408
426
snprintf (state -> message_id , SMALLBUFSIZE - 1 , "%s" , p );
409
427
}
410
428
429
+ if (state -> message_state == MSG_CONTENT_TYPE || state -> message_state == MSG_CONTENT_DISPOSITION ){
430
+ fill_attachment_name_buf (state , buf );
431
+ }
432
+
411
433
/* we are interested in only From:, To:, Subject:, Received:, Content-*: header lines */
412
434
if (state -> message_state <= 0 ) return 0 ;
413
435
}
@@ -420,7 +442,7 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
420
442
}
421
443
422
444
423
- /*
445
+ /*
424
446
* A normal journal looks like this:
425
447
*
426
448
* Sender: sender@domain
@@ -526,21 +548,6 @@ int parse_line(char *buf, struct parser_state *state, struct session_data *sdata
526
548
}
527
549
528
550
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
-
544
551
if (state -> message_state == MSG_CONTENT_TRANSFER_ENCODING ){
545
552
if (strcasestr (buf , "base64" )) state -> base64 = 1 ;
546
553
if (strcasestr (buf , "quoted-printable" )) state -> qp = 1 ;
0 commit comments