-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.c
561 lines (434 loc) · 14.7 KB
/
mime.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
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include "mail-internal.h"
enum content_type
{
ct_text_plain,
ct_message_rfc822,
ct_multipart_mixed,
ct_multipart_alternative,
ct_multipart_related,
ct_multipart_signed,
ct_multipart_other,
ct_other
};
enum content_transfer_encoding
{
cte_quoted_printable,
cte_base64,
cte_literal
};
#define MIN (a,b) ((a) < (b) ? (a) : (b))
void mime_parse_content_type (const char* cts, char* type, size_t type_size,
char* subtype, size_t subtype_size,
char* charset, size_t charset_size,
char* filename, size_t filename_size,
char* part_delimiter, size_t part_delimiter_size)
{
const char* type_delim = strchr (cts, '/');
size_t type_len;
if (!type_delim)
type_delim = cts;
type_len = type_delim - cts;
if (type_len >= type_size)
type_len = type_size - 1;
strncpy (type, cts, type_len);
type[type_len] = 0;
size_t subtype_len = strspn (type_delim + 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
if (subtype_len >= subtype_size)
subtype_len = subtype_size - 1;
strncpy (subtype, type_delim + 1, subtype_len);
subtype[subtype_len] = 0;
char* arg = strchr (type_delim, ';');
while (arg && *arg)
{
char* arg_end;
++arg;
while (isspace (*arg))
++arg;
if (!*arg)
break;
arg_end = strchr (arg + 1, ';');
if (!arg_end)
arg_end = strchr (arg + 1, 0);
char* arg_delim = strchr (arg, '=');
if (!arg_delim)
break;
char* key = strndupa (arg, arg_delim - arg);
char* value = strndupa (arg_delim + 1, arg_end - arg_delim - 1);
rtrim (value);
if (value[0] == '"' && value[strlen (value) - 1] == '"')
{
value[strlen (value) - 1] = 0;
++value;
}
if (part_delimiter && !strcasecmp (key, "boundary"))
{
strncpy (part_delimiter, value, part_delimiter_size);
part_delimiter[part_delimiter_size - 1] = 0;
}
else if (filename && !strcasecmp (key, "name"))
{
if (strchr (value, '/'))
strncpy (filename, strrchr (value, '/') + 1, filename_size);
else
strncpy (filename, value, filename_size);
filename[filename_size - 1] = 0;
}
else if (!strcasecmp (key, "charset"))
{
strncpy (charset, value, charset_size);
charset[charset_size - 1] = 0;
}
arg = arg_end;
}
}
void mime_parse_content_disposition (const char* arg, char* filename, size_t filename_size)
{
while (arg && *arg)
{
char* arg_end;
++arg;
while (isspace (*arg))
++arg;
if (!*arg)
break;
arg_end = strchr (arg + 1, ';');
if (!arg_end)
arg_end = strchr (arg + 1, 0);
char* arg_delim = strchr (arg, '=');
if (!arg_delim)
break;
char* key = strndupa (arg, arg_delim - arg);
char* value = strndupa (arg_delim + 1, arg_end - arg_delim - 1);
rtrim (value);
if (value[0] == '"' && value[strlen (value) - 1] == '"')
{
value[strlen (value) - 1] = 0;
++value;
}
if (filename && !strcasecmp (key, "filename"))
{
if (strchr (value, '/'))
strncpy (filename, strrchr (value, '/') + 1, filename_size);
else
strncpy (filename, value, filename_size);
filename[filename_size - 1] = 0;
}
arg = arg_end;
}
}
void mime_sanitize (struct mime_message* result, const char* input, size_t input_size, int flags)
{
char part_delimiter[256] = { 0 };
char filename[64] = { 0 };
char type[32] = "text";
char subtype[32] = "plain";
char charset[16] = { 0 };
struct tuple* headers;
size_t header_count;
const char* end;
const char* body;
size_t i;
int content_type_idx = -1;
int content_transfer_encoding_idx = -1;
int content_disposition_idx = -1;
end = input + input_size;
parse_headers (input, end, &headers, &header_count, &body);
for (i = 0; i < header_count; ++i)
{
if (content_type_idx == -1 && !strncasecmp (headers[i].key, "content-type", headers[i].key_size))
content_type_idx = i;
else if (content_transfer_encoding_idx == -1 && !strncasecmp (headers[i].key, "content-transfer-encoding", headers[i].key_size))
content_transfer_encoding_idx = i;
else if (content_disposition_idx == -1 && !strncasecmp (headers[i].key, "content-disposition", headers[i].key_size))
content_disposition_idx = i;
}
enum content_type ct = ct_other;
enum content_transfer_encoding cte = cte_literal;
if (-1 != content_type_idx)
{
char* cts = strndupa (headers[content_type_idx].value, headers[content_type_idx].value_size);
mime_parse_content_type (cts, type, sizeof (type), subtype, sizeof (subtype), charset, sizeof (charset), filename, sizeof (filename), part_delimiter, sizeof (part_delimiter));
if (!strcasecmp (type, "text"))
{
if (!strcasecmp (subtype, "plain"))
ct = ct_text_plain;
}
else if (!strcasecmp (type, "message"))
{
if (!strcasecmp (subtype, "rfc822"))
ct = ct_message_rfc822;
}
else if (!strcasecmp (type, "multipart"))
{
if (!strcasecmp (subtype, "mixed"))
ct = ct_multipart_mixed;
else if (!strcasecmp (subtype, "alternative"))
ct = ct_multipart_alternative;
else if (!strcasecmp (subtype, "related"))
ct = ct_multipart_related;
else if (!strcasecmp (subtype, "signed"))
ct = ct_multipart_signed;
else
ct = ct_multipart_other;
}
}
else
ct = ct_text_plain;
if (!charset[0])
strcpy (charset, "US-ASCII");
if (-1 != content_transfer_encoding_idx)
{
char* ctes = strndupa (headers[content_transfer_encoding_idx].value, headers[content_transfer_encoding_idx].value_size);
rfc2822_unfold (ctes);
if (!strcasecmp (ctes, "quoted-printable"))
cte = cte_quoted_printable;
else if (!strcasecmp (ctes, "base64"))
cte = cte_base64;
}
if (-1 != content_disposition_idx)
{
char* cd = strndupa (headers[content_disposition_idx].value, headers[content_disposition_idx].value_size);
mime_parse_content_disposition (cd, filename, sizeof (filename));
}
if (flags & MIME_INCLUDE_HEADERS)
{
for (i = 0; i < header_count; ++i)
{
char* decoded_header;
char* header = malloc (headers[i].value_size + 1);
memcpy (header, headers[i].value, headers[i].value_size);
header[headers[i].value_size] = 0;
if (flags & MIME_EXCLUDE_NOISE_HEADERS)
{
static const char* important_headers[] = { "subject", "from", "to", "cc", "date" };
size_t j;
for (j = 0; j < ARRAY_SIZE (important_headers); ++j)
{
if ( strlen (important_headers[j]) == headers[i].key_size
&& !strncasecmp (important_headers[j], headers[i].key, headers[i].key_size))
break;
}
if (j == ARRAY_SIZE (important_headers))
{
free (header);
continue;
}
}
if (!strncasecmp (headers[i].key, "subject", headers[i].key_size))
rfc2822_unfold (header);
if (-1 == rfc2047_decode (header, &decoded_header))
decoded_header = header;
char* buf = 0;
if (flags & MIME_EXCLUDE_HEADER_NAMES)
asprintf (&buf, "%s\n", decoded_header);
else
asprintf (&buf, "%.*s: %s\n", (int)headers[i].key_size, headers[i].key, decoded_header);
size_t offset = result->text_size;
result->text_size += strlen (buf);
result->text = realloc (result->text, result->text_size);
memcpy (result->text + offset, buf, result->text_size - offset);
free (buf);
if (decoded_header != header)
free (decoded_header);
free (header);
}
++result->text_size;
result->text = realloc (result->text, result->text_size);
result->text[result->text_size - 1] = '\n';
}
if ( ct == ct_multipart_mixed
|| ct == ct_multipart_alternative
|| ct == ct_multipart_related
|| ct == ct_multipart_signed
|| ct == ct_multipart_other)
{
if (!part_delimiter[0])
return;
size_t part_delimiter_len = strlen (part_delimiter);
const char* i;
const char* part_begin = 0;
const char* part_end = 0;
for (i = body; i < end - part_delimiter_len - 2; ++i)
{
if ((i == body || *(i - 1) == '\n')
&& *i == '-' && *(i + 1) == '-'
&& !memcmp (i + 2, part_delimiter, part_delimiter_len))
{
if (part_begin)
{
part_end = i;
if (part_begin != part_end)
mime_sanitize (result, part_begin, part_end - part_begin, flags & MIME_RECURSIVE_FLAGS);
}
part_begin = i + part_delimiter_len + 2;
while (isspace (*part_begin) && *part_begin != '\n')
++part_begin;
if (*part_begin == '\n')
++part_begin;
}
}
}
else if (ct == ct_text_plain || (flags & MIME_INCLUDE_ATTACHMENTS))
{
const char* decoded_body = body;
size_t body_size = end - body;
size_t offset = result->text_size;
if (cte == cte_quoted_printable)
{
char* tmp = malloc (body_size);
const char* i = body;
char* o = tmp;
while (i != end)
{
if (*i == '=' && i + 2 < end)
{
if (isspace (i[1]))
i += 2;
else
{
if (i[1] >= '0' && i[1] <= '9')
*o = (i[1] - '0') << 4;
else
*o = (i[1] - 'A' + 0xA) << 4;
if (i[2] >= '0' && i[2] <= '9')
*o |= (i[2] - '0');
else
*o |= (i[2] - 'A' + 0xA);
++o;
i += 3;
}
}
else
*o++ = *i++;
}
body_size = o - tmp;
decoded_body = tmp;
}
else if (cte == cte_base64)
{
char* tmp = malloc (body_size);
int result;
result = base64_decode (tmp, decoded_body, body_size);
if (result >= 0)
{
body_size = result;
decoded_body = tmp;
}
}
if (strcasecmp (charset, "utf-8") && strcasecmp (charset, "us-ascii"))
{
char* tmp;
size_t new_size;
if (-1 != convert_to_utf8 (decoded_body, body_size, &tmp, &new_size, charset))
{
if (decoded_body != body)
free ((char*) decoded_body);
decoded_body = tmp;
body_size = new_size;
}
}
if (ct == ct_text_plain)
{
result->text_size += body_size;
result->text = realloc (result->text, result->text_size);
memcpy (result->text + offset, decoded_body, body_size);
offset = result->text_size;
}
if (1)
{
char* desc;
size_t i = result->attachment_count++;
result->attachments = realloc (result->attachments, result->attachment_count * sizeof (struct mime_attachment));
asprintf (&result->attachments[i].content_type, "%s/%s", type, subtype);
if (filename[0])
result->attachments[i].filename = strdup (filename);
else
asprintf (&result->attachments[i].filename, "attachment_%04zu.%s", i + 1, subtype);
result->attachments[i].data = malloc (body_size);
memcpy (result->attachments[i].data, decoded_body, body_size);
result->attachments[i].data_size = body_size;
result->attachments[i].body_offset = offset;
asprintf (&desc, "[%s %s %'zu bytes]\n",
result->attachments[i].filename,
result->attachments[i].content_type,
result->attachments[i].data_size);
result->text_size += strlen (desc);
result->text = realloc (result->text, result->text_size);
memcpy (result->text + offset, desc, strlen (desc));
free (desc);
}
if (decoded_body != body)
free ((char*) decoded_body);
}
if (flags & MIME_NUL_TERMINATE)
{
++result->text_size;
result->text = realloc (result->text, result->text_size);
result->text[result->text_size - 1] = 0;
}
free (headers);
}
void mime_free (struct mime_message* result)
{
size_t i;
for (i = 0; i < result->attachment_count; ++i)
{
free (result->attachments[i].content_type);
free (result->attachments[i].filename);
free (result->attachments[i].data);
}
free (result->attachments);
free (result->text);
memset (result, 0, sizeof (struct mime_message));
}
#ifdef MIME_SANITIZE_MAIN
int main (int argc, char** argv)
{
if (argc != 2)
{
fprintf (stderr, "Use: %s <FILE>\n", argv[0]);
return EXIT_SUCCESS;
}
int fd = open (argv[1], O_RDONLY);
size_t size = lseek (fd, 0, SEEK_END);
char* buf = malloc (size);
pread (fd, buf, size, 0);
struct mime_message message;
memset (&message, 0, sizeof (struct mime_message));
mime_sanitize (&message, buf, size, MIME_INCLUDE_HEADERS);
write (1, message.text, message.text_size);
return EXIT_SUCCESS;
}
#endif
/*
struct vink_message *
vink_rfc28822_parse (const char *input, size_t size)
{
struct vink_arena arena, *vink_arena_copy;
struct vink_message *result;
struct tuples headers;
const char *end, *body;
char *tmp;
size_t i;
vink_arena_init (&arena);
result = vink_arena_calloc (&arena, sizeof (*result));
ARRAY_INIT (&headers);
end = input + size;
parse_headers (input, end, &headers, &body);
result->body_size = end - body;
tmp = vink_arena_alloc (&arena, result->body_size);
memcpy (tmp, body, result->body_size);
result->body = tmp;
vink_arena_copy = vink_arena_alloc (&arena, sizeof (arena));
memcpy (vink_arena_copy, &arena, sizeof (arena));
result->private = vink_arena_copy;
return result;
}
*/
/* vim: set ts=4 sw=4 et :*/