-
Notifications
You must be signed in to change notification settings - Fork 3
/
scrutmime.c
681 lines (595 loc) · 17.4 KB
/
scrutmime.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
/* scrutmime.c - Look at MIME mails.
* Copyright (C) 2004 g10 Code GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/* Utility to help filter out spam. This one identifies attachments. */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "rfc822parse.h"
#define PGM "scrutmime"
#define VERSION "1.0"
/* Option flags. */
static int verbose;
static int quiet;
static int debug;
static int opt_match_zip;
static int opt_match_exe;
static int opt_match_html;
enum mime_types
{
MT_NONE = 0,
MT_OCTET_STREAM,
MT_AUDIO,
MT_IMAGE,
MT_TEXT_HTML
};
enum transfer_encodings
{
TE_NONE = 0,
TE_BASE64
};
/* Structure used to communicate with the parser callback. */
struct parse_info_s {
enum mime_types mime_type;
enum transfer_encodings transfer_encoding;
int test_base64; /* Set if we should decode and test base64 data. */
int got_probe;
int no_mime; /* Set if this is not a MIME message. */
int top_seen;
int wk_seen;
};
/* Base64 conversion tables. */
static unsigned char bintoasc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static unsigned char asctobin[256]; /* runtime initialized */
/* Print diagnostic message and exit with failure. */
static void
die (const char *format, ...)
{
va_list arg_ptr;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
putc ('\n', stderr);
exit (1);
}
/* Print diagnostic message. */
static void
err (const char *format, ...)
{
va_list arg_ptr;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
putc ('\n', stderr);
}
/* static void * */
/* xmalloc (size_t n) */
/* { */
/* void *p = malloc (n); */
/* if (!p) */
/* die ("out of core: %s", strerror (errno)); */
/* return p; */
/* } */
/* static void * */
/* xcalloc (size_t n, size_t m) */
/* { */
/* void *p = calloc (n, m); */
/* if (!p) */
/* die ("out of core: %s", strerror (errno)); */
/* return p; */
/* } */
/* static void * */
/* xrealloc (void *old, size_t n) */
/* { */
/* void *p = realloc (old, n); */
/* if (!p) */
/* die ("out of core: %s", strerror (errno)); */
/* return p; */
/* } */
/* static char * */
/* xstrdup (const char *string) */
/* { */
/* void *p = malloc (strlen (string)+1); */
/* if (!p) */
/* die ("out of core: %s", strerror (errno)); */
/* strcpy (p, string); */
/* return p; */
/* } */
/* static char * */
/* stpcpy (char *a,const char *b) */
/* { */
/* while (*b) */
/* *a++ = *b++; */
/* *a = 0; */
/* return (char*)a; */
/* } */
/* Simple but sufficient and locale independend lowercase function. */
static void
lowercase_string (unsigned char *string)
{
for (; *string; string++)
if (*string >= 'A' && *string <= 'Z')
*string = *string - 'A' + 'a';
}
/* Inplace Base64 decoder. Returns the length of the valid databytes
in BUFFER. Note, that BUFFER should initially be a C-string. */
static size_t
decode_base64 (unsigned char *buffer)
{
int state, c, value=0;
unsigned char *d, *s;
for (state=0, d=s=buffer; *s; s++)
{
if ((c = asctobin[*s]) == 255 )
continue; /* Simply skip invalid base64 characters. */
switch (state)
{
case 0:
value = c << 2;
break;
case 1:
value |= (c>>4)&3;
*d++ = value;
value = (c<<4)&0xf0;
break;
case 2:
value |= (c>>2)&15;
*d++ = value;
value = (c<<6)&0xc0;
break;
case 3:
value |= c&0x3f;
*d++ = value;
break;
}
state++;
state = state & 3;
}
return d - buffer;
}
/* Given a Buffer starting with the magic MZ, check whethere thsi is a
Windows PE executable. */
static int
is_windows_pe (const unsigned char *buffer, size_t buflen)
{
unsigned long off;
if ( buflen < 0x3c + 4 )
return 0;
/* The offset is little endian. */
off = ((buffer[0x3c]) | (buffer[0x3d] << 8)
| (buffer[0x3e] << 16) | (buffer[0x3f] << 24));
return (off < buflen - 4 && !memcmp (buffer+off, "PE\0", 4));
}
/* See whether we can identify the binary data in BUFFER. */
static void
identify_binary (const unsigned char *buffer, size_t buflen)
{
if (buflen > 5 && !memcmp (buffer, "PK\x03\x04", 4))
{
if (!quiet)
fputs ("ZIP\n", stdout);
if (opt_match_zip)
exit (0);
}
else if (buflen > 132 && buffer[0] == 'M' && buffer[1] == 'Z'
&& is_windows_pe (buffer, buflen))
{
if (!quiet)
fputs ("EXE (Windows PE)\n", stdout);
if (opt_match_exe)
exit (0);
}
}
/* Print the event received by the parser for debugging as comment
line. */
static void
show_event (rfc822parse_event_t event)
{
const char *s;
switch (event)
{
case RFC822PARSE_OPEN: s= "Open"; break;
case RFC822PARSE_CLOSE: s= "Close"; break;
case RFC822PARSE_CANCEL: s= "Cancel"; break;
case RFC822PARSE_T2BODY: s= "T2Body"; break;
case RFC822PARSE_FINISH: s= "Finish"; break;
case RFC822PARSE_RCVD_SEEN: s= "Rcvd_Seen"; break;
case RFC822PARSE_LEVEL_DOWN: s= "Level_Down"; break;
case RFC822PARSE_LEVEL_UP: s= "Level_Up"; break;
case RFC822PARSE_BOUNDARY: s= "Boundary"; break;
case RFC822PARSE_LAST_BOUNDARY: s= "Last_Boundary"; break;
case RFC822PARSE_BEGIN_HEADER: s= "Begin_Header"; break;
case RFC822PARSE_PREAMBLE: s= "Preamble"; break;
case RFC822PARSE_EPILOGUE: s= "Epilogue"; break;
default: s= "[unknown event]"; break;
}
printf ("# *** got RFC822 event %s\n", s);
}
/* This function is called by the parser to communicate events. This
callback communicates with the main program using a structure
passed in OPAQUE. Should retrun 0 or set errno and return -1. */
static int
message_cb (void *opaque, rfc822parse_event_t event, rfc822parse_t msg)
{
struct parse_info_s *info = opaque;
if (debug)
show_event (event);
if (event == RFC822PARSE_T2BODY)
{
rfc822parse_field_t ctx;
size_t off;
char *p;
info->mime_type = MT_NONE;
info->transfer_encoding = TE_NONE;
info->test_base64 = 0;
info->got_probe = 0;
info->no_mime = 0;
ctx = rfc822parse_parse_field (msg, "Content-Type", -1);
if (ctx)
{
const char *s1, *s2;
s1 = rfc822parse_query_media_type (ctx, &s2);
if (!s1)
;
else if (!strcmp (s1, "application")
&& !strcmp (s2, "octet-stream"))
info->mime_type = MT_OCTET_STREAM;
else if (!strcmp (s1, "text")
&& !strcmp (s2, "html"))
info->mime_type = MT_TEXT_HTML;
else if (!strcmp (s1, "audio"))
info->mime_type = MT_AUDIO;
else if (!strcmp (s1, "image"))
info->mime_type = MT_IMAGE;
if (verbose)
{
printf ("# Content-Type: %s/%s", s1?s1:"", s2?s2:"");
s1 = rfc822parse_query_parameter (ctx, "charset", 0);
if (s1)
printf ("; charset=%s", s1);
putchar ('\n');
}
rfc822parse_release_field (ctx);
}
else
{
p = rfc822parse_get_field (msg, "MIME-Version", -1, NULL);
if (p)
free (p);
else
info->no_mime = 1;
}
if (verbose)
{
const char *s1;
p = rfc822parse_get_field (msg, "Content-Disposition", -1, NULL);
if (p)
{
printf ("# %s\n", p);
free (p);
}
ctx = rfc822parse_parse_field (msg, "Content-Disposition", -1);
if (ctx)
{
s1 = rfc822parse_query_parameter (ctx, "filename", 0);
if (s1)
printf ("# Content-Disposition has filename=`%s'\n", s1);
rfc822parse_release_field (ctx);
}
}
p = rfc822parse_get_field (msg, "Content-Transfer-Encoding", -1, &off);
if (p)
{
lowercase_string (p+off);
if (!strcmp (p+off, "base64"))
info->transfer_encoding = TE_BASE64;
free (p);
}
if (!info->top_seen)
{
info->top_seen = 1;
p = rfc822parse_get_field (msg, "To", -1, NULL);
if (p)
{
if ( strstr (p, "Werner Koch") )
{
if (verbose)
fputs ("# Found known name in To\n", stdout);
info->wk_seen = 1;
}
free (p);
}
if (!info->wk_seen)
{
p = rfc822parse_get_field (msg, "Cc", -1, NULL);
if (p)
{
if ( strstr (p, "Werner Koch") )
{
if (verbose)
fputs ("# Found known name in Cc\n", stdout);
info->wk_seen = 1;
}
free (p);
}
}
}
if ((info->mime_type == MT_OCTET_STREAM
|| info->mime_type == MT_AUDIO
|| info->mime_type == MT_IMAGE)
&& info->transfer_encoding == TE_BASE64)
info->test_base64 = 1;
else if (info->mime_type == MT_TEXT_HTML)
{
if (!quiet)
fputs ("HTML\n", stdout);
if (opt_match_html && !info->wk_seen)
exit (0);
}
}
else if (event == RFC822PARSE_PREAMBLE)
;
else if (event == RFC822PARSE_BOUNDARY || event == RFC822PARSE_LAST_BOUNDARY)
{
if (info->test_base64)
info->got_probe = 1;
info->test_base64 = 0;
}
else if (event == RFC822PARSE_BEGIN_HEADER)
{
}
return 0;
}
/* Read a message from FP and process it according to the global
options. */
static void
parse_message (FILE *fp)
{
char line[2000];
unsigned char buffer[1000];
size_t buflen = 0;
size_t length;
rfc822parse_t msg;
unsigned int lineno = 0;
int no_cr_reported = 0;
struct parse_info_s info;
int body_lines = 0;
int skip_leading_empty_lines = 0;
restart:
memset (&info, 0, sizeof info);
msg = rfc822parse_open (message_cb, &info);
if (!msg)
die ("can't open parser: %s", strerror (errno));
/* Fixme: We should not use fgets because it can't cope with
embedded nul characters. */
while (fgets (line, sizeof (line), fp))
{
lineno++;
if (lineno == 1 && !strncmp (line, "From ", 5))
continue; /* We better ignore a leading From line. */
length = strlen (line);
if (length && line[length - 1] == '\n')
line[--length] = 0;
else if (verbose)
err ("line number %u too long or last line not terminated", lineno);
if (length && line[length - 1] == '\r')
line[--length] = 0;
else if (verbose && !no_cr_reported)
{
err ("non canonical ended line detected (line %u)", lineno);
no_cr_reported = 1;
}
if (skip_leading_empty_lines)
{
if (skip_leading_empty_lines == 1)
{
/* Sometimes additional information follows the
indication line indicated by 6 dashes. Skip them
before detecting empty lines. */
if (length && !strncmp (line , "------ ", 7))
continue;
skip_leading_empty_lines++;
}
if (!length)
continue;
skip_leading_empty_lines = 0;
}
if (rfc822parse_insert (msg, line, length))
die ("parser failed: %s", strerror (errno));
if (info.no_mime && body_lines < 50)
{
body_lines++;
if (!strncmp (line, "------ This is a copy of the message, "
"including all the headers.", 64))
{
/* This may be followed by empty lines, thus we set a
flag to skip them. */
skip_leading_empty_lines = 1;
body_lines = 500; /* Avoid going here a second time. */
rfc822parse_close (msg);
goto restart;
}
}
if (info.got_probe && buflen)
{
info.got_probe = 0;
buffer[buflen] = 0;
buflen = decode_base64 (buffer);
if (debug)
{
int i;
printf ("# %4d bytes base64:", (int)buflen);
for (i=0; i < buflen; i++)
{
if (i && !(i % 16))
printf ("\n# 0x%04X:", i);
printf (" %02X", buffer[i]);
}
putchar ('\n');
}
identify_binary (buffer, buflen);
}
if (info.test_base64)
{
if (info.test_base64 == 1)
{
/* This is the empty marker line. */
buflen = 0;
}
else
{
if (length > sizeof buffer - 1 - buflen)
{
length = sizeof buffer - 1 - buflen;
info.got_probe = 1; /* We got enough. */
}
if (length)
{
memcpy (buffer+buflen, line, length);
buflen += length;
}
}
if (info.got_probe)
info.test_base64 = 0;
else
info.test_base64++;
}
}
rfc822parse_close (msg);
}
int
main (int argc, char **argv)
{
int last_argc = -1;
int any_match = 0;
if (argc)
{
argc--; argv++;
}
while (argc && last_argc != argc )
{
last_argc = argc;
if (!strcmp (*argv, "--"))
{
argc--; argv++;
break;
}
else if (!strcmp (*argv, "--help"))
{
puts (
"Usage: " PGM " [OPTION] [FILE]\n"
"Scrutinize a mail message.\n\n"
" --match-zip return true if a ZIP body was found\n"
" --match-exe return true if an EXE body was found\n"
" --match-html return true if a HTML body was found\n"
" --verbose enable extra informational output\n"
" --debug enable additional debug output\n"
" --help display this help and exit\n\n"
"With no FILE, or when FILE is -, read standard input.\n\n"
"Report bugs to <[email protected]>.");
exit (0);
}
else if (!strcmp (*argv, "--version"))
{
puts (PGM " " VERSION "\n"
"Copyright (C) 2004 g10 Code GmbH\n"
"This program comes with ABSOLUTELY NO WARRANTY.\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions. See the file COPYING for details.");
exit (0);
}
else if (!strcmp (*argv, "--verbose"))
{
verbose = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--quiet"))
{
quiet = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--debug"))
{
verbose = debug = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--match-zip"))
{
opt_match_zip = 1;
any_match = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--match-exe"))
{
opt_match_exe = 1;
any_match = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--match-html"))
{
opt_match_html = 1;
any_match = 1;
argc--; argv++;
}
}
if (argc > 1)
die ("usage: " PGM " [OPTION] [FILE] (try --help for more information)\n");
signal (SIGPIPE, SIG_IGN);
/* Build the helptable for radix64 to bin conversion. */
{
int i;
unsigned char *s;
for (i=0; i < 256; i++ )
asctobin[i] = 255; /* Used to detect invalid characters. */
for (s=bintoasc, i=0; *s; s++, i++)
asctobin[*s] = i;
}
/* Start processing. */
if (argc && strcmp (*argv, "-"))
{
FILE *fp = fopen (*argv, "rb");
if (!fp)
die ("can't open `%s': %s", *argv, strerror (errno));
parse_message (fp);
fclose (fp);
}
else
parse_message (stdin);
/* If any match option was used and we reach this here we return
false. True is returned immediately on a match. */
return any_match? 1:0;
}
/*
Local Variables:
compile-command: "gcc -Wall -Wno-pointer-sign -g -o scrutmime rfc822parse.c scrutmime.c"
End:
*/