-
Notifications
You must be signed in to change notification settings - Fork 24
/
rfc822.c
1583 lines (1466 loc) · 42.4 KB
/
rfc822.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005,2006,2007,2010
* rfc2047 decode:
* Copyright (C) Mikael Ylikoski 2002
* gzip mbox support:
* Copyright (C) Ico Doornekamp 2005
* Copyright (C) Felipe Gustavo de Almeida 2005
* bzip2 mbox support:
* Copyright (C) Paramjit Oberoi 2005
* caching uncompressed mbox data:
* Copyright (C) Chris Mason 2006
* memory leak fixes:
* Copyright (C) Samuel Tardieu 2008
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********************************************************************
*/
#include "mairix.h"
#include "nvp.h"
#include <assert.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#ifdef USE_GZIP_MBOX
# include <zlib.h>
#endif
#ifdef USE_BZIP_MBOX
# include <bzlib.h>
#endif
#ifdef USE_XZ_MBOX
# include "xzfile.h"
#endif
struct DLL {/*{{{*/
struct DLL *next;
struct DLL *prev;
};
/*}}}*/
static void enqueue(void *head, void *x)/*{{{*/
{
/* Declare this way so it can be used with any kind of double linked list
* having next & prev pointers in its first two words. */
struct DLL *h = (struct DLL *) head;
struct DLL *xx = (struct DLL *) x;
xx->next = h;
xx->prev = h->prev;
h->prev->next = xx;
h->prev = xx;
return;
}
/*}}}*/
enum encoding_type {/*{{{*/
ENC_UNKNOWN,
ENC_NONE,
ENC_BINARY,
ENC_7BIT,
ENC_8BIT,
ENC_QUOTED_PRINTABLE,
ENC_BASE64,
ENC_UUENCODE
};
/*}}}*/
struct content_type_header {/*{{{*/
const char *major; /* e.g. text */
const char *minor; /* e.g. plain */
const char *boundary; /* for multipart */
/* charset? */
};
/*}}}*/
struct line {/*{{{*/
struct line *next;
struct line *prev;
char *text;
};
/*}}}*/
static void init_headers(struct headers *hdrs)/*{{{*/
{
hdrs->to = NULL;
hdrs->cc = NULL;
hdrs->from = NULL;
hdrs->subject = NULL;
hdrs->message_id = NULL;
hdrs->in_reply_to = NULL;
hdrs->references = NULL;
hdrs->date = 0;
hdrs->flags.seen = 0;
hdrs->flags.replied = 0;
hdrs->flags.flagged = 0;
};
/*}}}*/
static void splice_header_lines(struct line *header)/*{{{*/
{
/* Deal with newline then tab in header */
struct line *x, *next;
for (x=header->next; x!=header; x=next) {
#if 0
printf("next header, x->text=%08lx\n", x->text);
printf("header=<%s>\n", x->text);
#endif
next = x->next;
if (isspace(x->text[0] & 0xff)) {
/* Glue to previous line */
char *p, *newbuf, *oldbuf;
struct line *y;
for (p=x->text; *p; p++) {
if (!isspace(*(unsigned char *)p)) break;
}
p--; /* point to final space */
y = x->prev;
#if 0
printf("y=%08lx p=%08lx\n", y->text, p);
#endif
newbuf = new_array(char, strlen(y->text) + strlen(p) + 1);
strcpy(newbuf, y->text);
strcat(newbuf, p);
oldbuf = y->text;
y->text = newbuf;
free(oldbuf);
y->next = x->next;
x->next->prev = y;
free(x->text);
free(x);
}
}
return;
}
/*}}}*/
static int audit_header(struct line *header)/*{{{*/
{
/* Check for obvious broken-ness
* 1st line has no leading spaces, single word then colon
* following lines have leading spaces or single word followed by colon
* */
struct line *x;
int first=1;
int count=1;
for (x=header->next; x!=header; x=x->next) {
int has_leading_space=0;
int is_blank;
int has_word_colon=0;
if (1 || first) {
/* Ignore any UUCP or mbox style From line at the start */
if (!strncmp("From ", x->text, 5)) {
continue;
}
/* Ignore escaped From line at the start */
if (!strncmp(">From ", x->text, 6)) {
continue;
}
}
is_blank = !(x->text[0]);
if (!is_blank) {
char *p;
int saw_char = 0;
has_leading_space = isspace(x->text[0] & 0xff);
has_word_colon = 0; /* default */
p = x->text;
while(*p) {
if(*p == ':') {
has_word_colon = saw_char;
break;
} else if (isspace(*(unsigned char *) p)) {
has_word_colon = 0;
break;
} else {
saw_char = 1;
}
p++;
}
}
if (( first && (is_blank || has_leading_space || !has_word_colon)) ||
(!first && (is_blank || !(has_leading_space || has_word_colon)))) {
#if 0
fprintf(stderr, "Header line %d <%s> fails because:", count, x->text);
if (first && is_blank) { fprintf(stderr, " [first && is_blank]"); }
if (first && has_leading_space) { fprintf(stderr, " [first && has_leading_space]"); }
if (first && !has_word_colon) { fprintf(stderr, " [first && !has_word_colon]"); }
if (!first && is_blank) { fprintf(stderr, " [!first && is_blank]"); }
if (!first && !(has_leading_space||has_word_colon)) { fprintf(stderr, " [!first && !has_leading_space||has_word_colon]"); }
fprintf(stderr, "\n");
#endif
/* Header fails the audit */
return 0;
}
first = 0;
count++;
}
/* If we get here the header must have been OK */
return 1;
}/*}}}*/
static int match_string(const char *ref, const char *candidate)/*{{{*/
{
int len = strlen(ref);
return !strncasecmp(ref, candidate, len);
}
/*}}}*/
static char equal_table[] = {/*{{{*/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00-0f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10-1f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20-2f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 30-3f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-4f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-5f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-6f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70-7f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90-9f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* a0-af */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* b0-bf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* c0-cf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d0-df */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* e0-ef */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* f0-ff */
};
/*}}}*/
static int base64_table[] = {/*{{{*/
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00-0f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10-1f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 20-2f */
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, /* 30-3f */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 40-4f */
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 50-5f */
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 60-6f */
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 70-7f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80-8f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90-9f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0-af */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0-bf */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0-cf */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0-df */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0-ef */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* f0-ff */
};
/*}}}*/
static int hex_to_val(char x) {/*{{{*/
switch (x) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return (x - '0');
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return 10 + (x - 'a');
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return 10 + (x - 'A');
break;
default:
return 0;
}
}
/*}}}*/
static void decode_header_value(char *text){/*{{{*/
/* rfc2047 decode, written by Mikael Ylikoski */
char *s, *a, *b, *e, *p, *q;
for (p = q = s = text; (s = strstr(s, "=?")); s = e + 2) {
if (p == q)
p = q = s;
else
while (q != s)
*p++ = *q++;
s += 2;
a = strchr(s, '?');
if (!a) break;
a++;
b = strchr(a, '?');
if (!b) break;
b++;
e = strstr(b, "?=");
if (!e) break;
/* have found an encoded-word */
if (b - a != 2)
continue; /* unknown encoding */
if (*a == 'q' || *a == 'Q') {
int val;
q = b;
while (q < e) {
if (*q == '_') {
*p++ = 0x20;
q++;
} else if (*q == '=') {
q++;
val = hex_to_val(*q++) << 4;
val += hex_to_val(*q++);
*p++ = val;
} else
*p++ = *q++;
}
} else if (*a == 'b' || *a == 'B') {
int reg, nc, eq; /* register, #characters in reg, #equals */
int dc; /* decoded character */
eq = reg = nc = 0;
for (q = b; q < e; q++) {
unsigned char cq = *(unsigned char *)q;
dc = base64_table[cq];
eq += equal_table[cq];
if (dc >= 0) {
reg <<= 6;
reg += dc;
nc++;
if (nc == 4) {
*p++ = ((reg >> 16) & 0xff);
if (eq < 2) *p++ = ((reg >> 8) & 0xff);
if (eq < 1) *p++ = reg & 0xff;
nc = reg = 0;
if (eq) break;
}
}
}
} else {
continue; /* unknown encoding */
}
q = e + 2;
}
if (p == q) return;
while (*q != '\0')
*p++ = *q++;
*p = '\0';
}
/*}}}*/
static char *copy_header_value(char *text){/*{{{*/
char *p;
for (p = text; *p && (*p != ':'); p++) ;
if (!*p) return NULL;
p++;
p = new_string(p);
decode_header_value(p);
return p;
}
/*}}}*/
static void copy_or_concat_header_value(char **previous, char *text){/*{{{*/
char *p = copy_header_value(text);
if (*previous)
{
*previous = extend_string(*previous, ", ");
*previous = extend_string(*previous, p);
free(p);
}
else
*previous = p;
}
/*}}}*/
static enum encoding_type decode_encoding_type(const char *e)/*{{{*/
{
enum encoding_type result;
const char *p;
if (!e) {
result = ENC_NONE;
} else {
for (p=e; *p && isspace(*(unsigned char *)p); p++) ;
if ( match_string("7bit", p)
|| match_string("7-bit", p)
|| match_string("7 bit", p)) {
result = ENC_7BIT;
} else if (match_string("8bit", p)
|| match_string("8-bit", p)
|| match_string("8 bit", p)) {
result = ENC_8BIT;
} else if (match_string("quoted-printable", p)) {
result = ENC_QUOTED_PRINTABLE;
} else if (match_string("base64", p)) {
result = ENC_BASE64;
} else if (match_string("binary", p)) {
result = ENC_BINARY;
} else if (match_string("x-uuencode", p)) {
result = ENC_UUENCODE;
} else {
fprintf(stderr, "Warning: unknown encoding type: '%s'\n", e);
result = ENC_UNKNOWN;
}
}
return result;
}
/*}}}*/
static void parse_content_type(struct nvp *ct_nvp, struct content_type_header *result)/*{{{*/
{
result->major = NULL;
result->minor = NULL;
result->boundary = NULL;
result->major = nvp_major(ct_nvp);
if (result->major) {
result->minor = nvp_minor(ct_nvp);
} else {
result->minor = NULL;
result->major = nvp_first(ct_nvp);
}
result->boundary = nvp_lookupcase(ct_nvp, "boundary");
}
/*}}}*/
static char *looking_at_ws_then_newline(char *start)/*{{{*/
{
char *result;
result = start;
do {
if (*result == '\n') return result;
else if (!isspace(*(unsigned char *) result)) return NULL;
else result++;
} while (1);
/* Can't get here */
assert(0);
}
/*}}}*/
static char *unencode_data(struct msg_src *src, char *input, int input_len, const char *enc, int *output_len)/*{{{*/
{
enum encoding_type encoding;
char *result, *end_result;
char *end_input;
encoding = decode_encoding_type(enc);
end_input = input + input_len;
/* All mime encodings result in expanded data, so this is guaranteed to
* safely oversize the output array */
result = new_array(char, input_len + 1);
/* Now decode */
switch (encoding) {
case ENC_7BIT:/*{{{*/
case ENC_8BIT:
case ENC_BINARY:
case ENC_NONE:
{
memcpy(result, input, input_len);
end_result = result + input_len;
}
break;
/*}}}*/
case ENC_QUOTED_PRINTABLE:/*{{{*/
{
char *p, *q;
p = result;
for (p=result, q=input;
q<end_input; ) {
if (*q == '=') {
/* followed by optional whitespace then \n? discard them. */
char *r;
int val;
q++;
r = looking_at_ws_then_newline(q);
if (r) {
q = r + 1; /* Point into next line */
continue;
}
/* not that case. */
val = hex_to_val(*q++) << 4;
val += hex_to_val(*q++);
*p++ = val;
} else {
/* Normal character */
*p++ = *q++;
}
}
end_result = p;
}
break;
/*}}}*/
case ENC_BASE64:/*{{{*/
{
char *p, *q;
int reg, nc, eq; /* register, #characters in reg, #equals */
int dc; /* decoded character */
eq = reg = nc = 0;
for (q=input, p=result; q<end_input; q++) {
unsigned char cq = * (unsigned char *)q;
/* Might want a 256 entry array instead of this sub-optimal mess
* eventually. */
dc = base64_table[cq];
eq += equal_table[cq];
if (dc >= 0) {
reg <<= 6;
reg += dc;
nc++;
if (nc == 4) {
*p++ = ((reg >> 16) & 0xff);
if (eq < 2) *p++ = ((reg >> 8) & 0xff);
if (eq < 1) *p++ = reg & 0xff;
nc = reg = 0;
if (eq) goto done_base_64;
}
}
}
done_base_64:
end_result = p;
}
break;
/*}}}*/
case ENC_UUENCODE:/*{{{*/
{
char *p, *q;
/* Find 'begin ' */
for (q = input; q < end_input - 6 && memcmp(q, "begin ", 6); q++)
;
q += 6;
/* skip to EOL */
while (q < end_input && *q != '\n')
q++;
p = result;
while (q < end_input) { /* process line */
#define DEC(c) (((c) - ' ') & 077)
int len = DEC(*q++);
if (len == 0)
break;
for (; len > 0; q += 4, len -= 3) {
if (len >= 3) {
*p++ = DEC(q[0]) << 2 | DEC(q[1]) >> 4;
*p++ = DEC(q[1]) << 4 | DEC(q[2]) >> 2;
*p++ = DEC(q[2]) << 6 | DEC(q[3]);
} else {
if (len >= 1)
*p++ = DEC(q[0]) << 2 | DEC(q[1]) >> 4;
if (len >= 2)
*p++ = DEC(q[1]) << 4 | DEC(q[2]) >> 2;
}
}
while (q < end_input && *q != '\n')
q++;
}
end_result = p;
}
break;
/*}}}*/
case ENC_UNKNOWN:/*{{{*/
fprintf(stderr, "Unknown encoding type in %s\n", format_msg_src(src));
/* fall through - ignore this data */
/*}}}*/
default:/*{{{*/
end_result = result;
break;
/*}}}*/
}
*output_len = end_result - result;
result[*output_len] = '\0'; /* for convenience with text/plain etc to make it printable */
return result;
}
/*}}}*/
char *format_msg_src(struct msg_src *src)/*{{{*/
{
static char *buffer = NULL;
static int buffer_len = 0;
char *result;
int len;
switch (src->type) {
case MS_FILE:
result = src->filename;
break;
case MS_MBOX:
len = strlen(src->filename);
len += 32;
if (!buffer || (len > buffer_len)) {
free(buffer);
buffer = new_array(char, len);
buffer_len = len;
}
sprintf(buffer, "%s[%d,%d)", src->filename,
(int) src->start, (int) (src->start + src->len));
result = buffer;
break;
default:
result = NULL;
break;
}
return result;
}
/*}}}*/
static int split_and_splice_header(struct msg_src *src, char *data, size_t data_len, struct line *header, char **body_start)/*{{{*/
{
char *sol, *eol;
int blank_line;
header->next = header->prev = header;
sol = data;
do {
if ((data_len == 0) || (!*sol)) break;
blank_line = 1; /* until proven otherwise */
eol = sol;
while ((data_len > 0) && *eol && (*eol != '\n')) {
if (!isspace(*(unsigned char *) eol)) blank_line = 0;
eol++;
data_len--;
}
if (data_len == 0) {
fprintf(stderr, "Found end of message while still in header processing %s\n",
format_msg_src(src));
return -1; /* & leak memory */
} else if (*eol == '\n') {
if (!blank_line) {
int line_length = eol - sol;
char *line_text = new_array(char, 1 + line_length);
struct line *new_header;
strncpy(line_text, sol, line_length);
line_text[line_length] = '\0';
new_header = new(struct line);
new_header->text = line_text;
enqueue(header, new_header);
}
sol = eol + 1; /* Start of next line */
data_len--;
} else { /* must be null char */
fprintf(stderr, "Got null character whilst processing header of %s\n",
format_msg_src(src));
return -1; /* & leak memory */
}
} while (!blank_line);
*body_start = sol;
if (audit_header(header)) {
splice_header_lines(header);
return 0;
} else {
#if 0
/* Caller generates message */
fprintf(stderr, "Message had bad rfc822 headers, ignoring\n");
#endif
return -1;
}
}
/*}}}*/
/* Forward prototypes */
static void do_multipart(struct msg_src *src, char *input, int input_len,
const char *boundary, struct attachment *atts,
enum data_to_rfc822_error *error);
/*{{{ do_body() */
static void do_body(struct msg_src *src,
char *body_start, int body_len,
struct nvp *ct_nvp, struct nvp *cte_nvp,
struct nvp *cd_nvp,
struct attachment *atts,
enum data_to_rfc822_error *error)
{
char *decoded_body;
int decoded_body_len;
const char *content_transfer_encoding;
content_transfer_encoding = NULL;
if (cte_nvp) {
content_transfer_encoding = nvp_first(cte_nvp);
if (!content_transfer_encoding) {
fprintf(stderr, "Giving up on %s, content_transfer_encoding header not parseable\n",
format_msg_src(src));
return;
}
}
decoded_body = unencode_data(src, body_start, body_len, content_transfer_encoding, &decoded_body_len);
if (ct_nvp) {
struct content_type_header ct;
parse_content_type(ct_nvp, &ct);
if (ct.major && !strcasecmp(ct.major, "multipart")) {
do_multipart(src, decoded_body, decoded_body_len, ct.boundary, atts, error);
/* Don't need decoded body any longer - copies have been taken if
* required when handling multipart attachments. */
free(decoded_body);
if (error && (*error == DTR8_MISSING_END)) return;
} else {
/* unipart */
struct attachment *new_att;
const char *disposition;
new_att = new(struct attachment);
disposition = cd_nvp ? nvp_first(cd_nvp) : NULL;
if (disposition && !strcasecmp(disposition, "attachment")) {
const char *lookup;
lookup = nvp_lookupcase(cd_nvp, "filename");
if (lookup) {
new_att->filename = new_string(lookup);
} else {
/* Some messages have name=... in content-type: instead of
* filename=... in content-disposition. */
lookup = nvp_lookup(ct_nvp, "name");
if (lookup) {
new_att->filename = new_string(lookup);
} else {
new_att->filename = NULL;
}
}
} else {
new_att->filename = NULL;
}
if (ct.major && !strcasecmp(ct.major, "text")) {
if (ct.minor && !strcasecmp(ct.minor, "plain")) {
new_att->ct = CT_TEXT_PLAIN;
} else if (ct.minor && !strcasecmp(ct.minor, "html")) {
new_att->ct = CT_TEXT_HTML;
} else {
new_att->ct = CT_TEXT_OTHER;
}
} else if (ct.major && !strcasecmp(ct.major, "message") &&
ct.minor && !strcasecmp(ct.minor, "rfc822")) {
new_att->ct = CT_MESSAGE_RFC822;
} else {
new_att->ct = CT_OTHER;
}
if (new_att->ct == CT_MESSAGE_RFC822) {
new_att->data.rfc822 = data_to_rfc822(src, decoded_body, decoded_body_len, error);
free(decoded_body); /* data no longer needed */
} else {
new_att->data.normal.len = decoded_body_len;
new_att->data.normal.bytes = decoded_body;
}
enqueue(atts, new_att);
}
} else {
/* Treat as text/plain {{{*/
struct attachment *new_att;
new_att = new(struct attachment);
new_att->filename = NULL;
new_att->ct = CT_TEXT_PLAIN;
new_att->data.normal.len = decoded_body_len;
/* Add null termination on the end */
new_att->data.normal.bytes = new_array(char, decoded_body_len + 1);
memcpy(new_att->data.normal.bytes, decoded_body, decoded_body_len + 1);
free(decoded_body);
enqueue(atts, new_att);/*}}}*/
}
}
/*}}}*/
/*{{{ do_attachment() */
static void do_attachment(struct msg_src *src,
char *start, char *after_end,
struct attachment *atts)
{
/* decode attachment and add to attachment list */
struct line header, *x, *nx;
char *body_start;
int body_len;
struct nvp *ct_nvp, *cte_nvp, *cd_nvp, *nvp;
if (split_and_splice_header(src, start, after_end-start, &header, &body_start) < 0) {
fprintf(stderr, "Giving up on attachment with bad header in %s\n",
format_msg_src(src));
return;
}
/* Extract key headers */
ct_nvp = cte_nvp = cd_nvp = NULL;
for (x=header.next; x!=&header; x=x->next) {
if ((nvp = make_nvp(src, x->text, "content-type:"))) {
ct_nvp = nvp;
} else if ((nvp = make_nvp(src, x->text, "content-transfer-encoding:"))) {
cte_nvp = nvp;
} else if ((nvp = make_nvp(src, x->text, "content-disposition:"))) {
cd_nvp = nvp;
}
}
#if 0
if (ct_nvp) {
fprintf(stderr, "======\n");
fprintf(stderr, "Dump of content-type hdr\n");
nvp_dump(ct_nvp, stderr);
free(ct_nvp);
}
if (cte_nvp) {
fprintf(stderr, "======\n");
fprintf(stderr, "Dump of content-transfer-encoding hdr\n");
nvp_dump(cte_nvp, stderr);
free(cte_nvp);
}
#endif
if (body_start > after_end) {
/* This is a (maliciously?) b0rken attachment, e.g. maybe empty */
if (verbose) {
fprintf(stderr, "Message %s contains an invalid attachment, length=%d bytes\n",
format_msg_src(src), (int)(after_end - start));
}
} else {
body_len = after_end - body_start;
/* Ignore errors in nested body parts. */
do_body(src, body_start, body_len, ct_nvp, cte_nvp, cd_nvp, atts, NULL);
}
/* Free header memory */
for (x=header.next; x!=&header; x=nx) {
nx = x->next;
free(x->text);
free(x);
}
if (ct_nvp) free_nvp(ct_nvp);
if (cte_nvp) free_nvp(cte_nvp);
if (cd_nvp) free_nvp(cd_nvp);
}
/*}}}*/
/*{{{ do_multipart() */
static void do_multipart(struct msg_src *src,
char *input, int input_len,
const char *boundary,
struct attachment *atts,
enum data_to_rfc822_error *error)
{
char *b0, *b1, *be, *bx;
char *line_after_b0, *start_b1_search_from;
int boundary_len;
int looking_at_end_boundary;
if (!boundary) {
fprintf(stderr, "Can't process multipart message %s with no boundary string\n",
format_msg_src(src));
if (error) *error = DTR8_MULTIPART_SANS_BOUNDARY;
return;
}
boundary_len = strlen(boundary);
b0 = NULL;
line_after_b0 = input;
be = input + input_len;
do {
int boundary_ok;
start_b1_search_from = line_after_b0;
do {
/* reject boundaries that aren't a whole line */
b1 = NULL;
for (bx = start_b1_search_from; bx < be - (boundary_len + 2); bx++) {
if (bx[0] == '-' && bx[1] == '-' &&
!strncmp(bx+2, boundary, boundary_len)) {
b1 = bx;
break;
}
}
if (!b1) {
if (error)
*error = DTR8_MISSING_END;
return;
}
looking_at_end_boundary = (b1+boundary_len+3 < be) && (b1[boundary_len+2] == '-' &&
b1[boundary_len+3] == '-');
boundary_ok = 1;
if ((b1 > input) && (*(b1-1) != '\n'))
boundary_ok = 0;
if (!looking_at_end_boundary && (b1 + boundary_len + 3 < be) && !(
((b1 + boundary_len + 2 < input + input_len) && (*(b1 + boundary_len + 2) == '\n')) ||
((b1 + boundary_len + 3 < input + input_len) && (*(b1 + boundary_len + 2) == '\r') && (*(b1 + boundary_len + 3) == '\n'))
))
boundary_ok = 0;
if (!boundary_ok) {
char *eol = strchr(b1, '\n');
if (!eol) {
fprintf(stderr, "Oops, didn't find another normal boundary in %s\n",
format_msg_src(src));
return;
}
start_b1_search_from = 1 + eol;
}
} while (!boundary_ok);
/* b1 is now looking at a good boundary, which might be the final one */
if (b0) {
/* don't treat preamble as an attachment */
do_attachment(src, line_after_b0, b1, atts);
}
b0 = b1;
line_after_b0 = strchr(b0, '\n');
if (line_after_b0 == 0)
line_after_b0 = b0 + strlen(b0);
else
++line_after_b0;
} while (b1 < be && !looking_at_end_boundary);
}
/*}}}*/
static time_t parse_rfc822_date(char *date_string)/*{{{*/
{
struct tm tm;
char *s, *z;
/* Format [weekday ,] day-of-month month year hour:minute:second timezone.
Some of the ideas, sanity checks etc taken from parse.c in the mutt
sources, credit to Michael R. Elkins et al
*/
s = date_string;
z = strchr(s, ',');
if (z) s = z + 1;
while (*s && isspace(*s)) s++;
/* Should now be looking at day number */
if (!isdigit(*s)) goto tough_cheese;
tm.tm_mday = atoi(s);
if (tm.tm_mday > 31) goto tough_cheese;
while (isdigit(*s)) s++;
while (*s && isspace(*s)) s++;
if (!*s) goto tough_cheese;
if (!strncasecmp(s, "jan", 3)) tm.tm_mon = 0;
else if (!strncasecmp(s, "feb", 3)) tm.tm_mon = 1;
else if (!strncasecmp(s, "mar", 3)) tm.tm_mon = 2;
else if (!strncasecmp(s, "apr", 3)) tm.tm_mon = 3;
else if (!strncasecmp(s, "may", 3)) tm.tm_mon = 4;
else if (!strncasecmp(s, "jun", 3)) tm.tm_mon = 5;
else if (!strncasecmp(s, "jul", 3)) tm.tm_mon = 6;
else if (!strncasecmp(s, "aug", 3)) tm.tm_mon = 7;
else if (!strncasecmp(s, "sep", 3)) tm.tm_mon = 8;
else if (!strncasecmp(s, "oct", 3)) tm.tm_mon = 9;
else if (!strncasecmp(s, "nov", 3)) tm.tm_mon = 10;
else if (!strncasecmp(s, "dec", 3)) tm.tm_mon = 11;
else goto tough_cheese;
while (*s && !isspace(*s)) s++;
while (*s && isspace(*s)) s++;
if (!isdigit(*s)) goto tough_cheese;
tm.tm_year = atoi(s);
if (tm.tm_year < 70) {
tm.tm_year += 100;
} else if (tm.tm_year >= 1900) {
tm.tm_year -= 1900;
}
while (isdigit(*s)) s++;
while (*s && isspace(*s)) s++;
if (!*s) goto tough_cheese;
/* Now looking at hms */
/* For now, forget this. The searching will be vague enough that nearest day is good enough. */
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = 0;
return mktime(&tm);
tough_cheese:
return (time_t) -1; /* default value */
}
/*}}}*/
static void scan_status_flags(const char *s, struct headers *hdrs)/*{{{*/
{
const char *p;
for (p=s; *p; p++) {
switch (*p) {
case 'R': hdrs->flags.seen = 1; break;
case 'A': hdrs->flags.replied = 1; break;
case 'F': hdrs->flags.flagged = 1; break;
default: break;
}
}
}