/
MIME.pm
565 lines (479 loc) · 13.5 KB
/
MIME.pm
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
#
# This program may be distributed under the terms of the GNU General
# Public License, Version 2.
#
=head1 NAME
Mail::MIMEDefang::MIME - MIME objects interface methods for email filters
=head1 DESCRIPTION
Mail::MIMEDefang::MIME are a set of methods that can be called
from F<mimedefang-filter> to operate on MIME objects.
=head1 METHODS
=over 4
=cut
package Mail::MIMEDefang::MIME;
require Exporter;
use MIME::Parser;
use MIME::Words qw(:all);
use Mail::MIMEDefang;
our @ISA = qw(Exporter);
our @EXPORT;
our @EXPORT_OK;
@EXPORT = qw(builtin_create_parser find_part append_to_part takeStabAtFilename
remove_redundant_html_parts append_to_html_part append_html_boilerplate
append_text_boilerplate collect_parts anonymize_uri);
sub builtin_create_parser {
my $parser = MIME::Parser->new();
$parser->extract_nested_messages(1);
$parser->extract_uuencode(1);
$parser->output_to_core(0);
$parser->tmp_to_core(0);
return $parser;
}
=item collect_parts
Method that adds parts to the array C<@FlatParts> for flattening.
=cut
#***********************************************************************
# %PROCEDURE: collect_parts
# %ARGUMENTS:
# entity -- root entity to rebuild
# skip_pgp_mime -- If true, skip multipart/signed and multipart/encrypted
# parts
# %RETURNS:
# Nothing
# %DESCRIPTION:
# Adds parts to the array @FlatParts for flattening.
#***********************************************************************
sub collect_parts {
my($entity, $skip_pgp_mime) = @_;
my(@parts) = $entity->parts;
my($part);
if ($#parts >= 0) {
if (! $skip_pgp_mime ||
(lc($entity->head->mime_type) ne "multipart/signed" and
lc($entity->head->mime_type) ne "multipart/encrypted")) {
foreach $part (@parts) {
collect_parts($part, $skip_pgp_mime);
}
}
} else {
push(@FlatParts, $entity);
}
}
=item takeStabAtFilename ( $entity )
Makes a guess at a filename for the attachment. Calls MIME::Head's
recommended_filename() method, which tries 'Content-Disposition.filename'and if
not found, 'Content-Type.name'.
Returns a MIME-decoded filename, or a blank string if none found.
=cut
sub takeStabAtFilename
{
my ($entity) = @_;
my $guess = $entity->head->recommended_filename();
if( defined $guess ) {
$guess =~ tr/\x00-\x7F/#/c;
return scalar( decode_mimewords( $guess ) );
}
return '';
}
=item find_part
Method that returns the first MIME entity of type C<$content_type>,
C<undef> if none exists.
=cut
#***********************************************************************
# %PROCEDURE: find_part
# %ARGUMENTS:
# entity -- root MIME part
# content_type -- desired MIME content type
# skip_pgp_mime -- If true, do not descend into multipart/signed or
# multipart/encrypted parts
# %RETURNS:
# First MIME entity of type "$content_type"; undef if none exists.
#***********************************************************************
sub find_part {
my($entity, $content_type, $skip_pgp_mime) = @_;
my(@parts);
my($part);
my($ans);
if (!($entity->is_multipart)) {
if (lc($entity->head->mime_type) eq lc($content_type)) {
return $entity;
} else {
return undef;
}
}
if ($skip_pgp_mime &&
(lc($entity->head->mime_type) eq "multipart/signed" or
lc($entity->head->mime_type) eq "multipart/encrypted")) {
return undef;
}
@parts = $entity->parts;
foreach $part (@parts) {
$ans = find_part($part, $content_type, $skip_pgp_mime);
return $ans if defined($ans);
}
return undef;
}
=item append_to_part
Method that appends text to C<$part>
=cut
#***********************************************************************
# %PROCEDURE: append_to_part
# %ARGUMENTS:
# part -- a mime entity
# msg -- text to append to the entity
# %RETURNS:
# 1 on success; 0 on failure.
# %DESCRIPTION:
# Appends text to $part
#***********************************************************************
sub append_to_part {
my($part, $boilerplate) = @_;
return 0 unless defined($part->bodyhandle);
my($path) = $part->bodyhandle->path;
return 0 unless (defined($path));
return 0 unless (open(OUT, ">>", "$path"));
print OUT "\n$boilerplate\n";
close(OUT);
$Changed = 1;
return 1;
}
=item remove_redundant_html_parts
Method that rebuilds the email message without redundant HTML parts.
That is, if a multipart/alternative entity contains text/plain and text/html
parts, the text/html part will be removed.
=cut
#***********************************************************************
# %PROCEDURE: remove_redundant_html_parts
# %ARGUMENTS:
# e -- entity
# %RETURNS:
# Nothing
# %DESCRIPTION:
# Rebuilds $e without redundant HTML parts. That is, if
# a multipart/alternative entity contains text/plain and text/html
# parts, we nuke the text/html part.
#***********************************************************************
sub remove_redundant_html_parts {
my($e) = @_;
return 0 unless in_filter_end("remove_redundant_html_parts");
my(@parts) = $e->parts;
my($type) = lc($e->mime_type);
# Don't recurse into multipart/signed or multipart/encrypted
return 0 if ($type eq "multipart/signed" or
$type eq "multipart/encrypted");
my(@keep, $part);
my($didsomething);
$didsomething = 0;
my($have_text_plain);
if ($type eq "multipart/alternative" && $#parts >= 0) {
# First look for a text/plain part
$have_text_plain = 0;
foreach $part (@parts) {
$type = lc($part->mime_type);
if ($type eq "text/plain") {
$have_text_plain = 1;
last;
}
}
# If we have a text/plain part, delete any text/html part
if ($have_text_plain) {
foreach $part (@parts) {
$type = lc($part->mime_type);
if ($type ne "text/html") {
push(@keep, $part);
} else {
$didsomething = 1;
}
}
if ($didsomething) {
$e->parts(\@keep);
@parts = @keep;
$Changed = 1;
}
}
}
if ($#parts >= 0) {
foreach $part (@parts) {
$didsomething = 1 if (remove_redundant_html_parts($part));
}
}
return $didsomething;
}
# HTML parser callbacks
sub html_echo {
my($text) = @_;
print OUT $text;
}
sub html_end {
my($text) = @_;
if (!$HTMLFoundEndBody) {
if ($text =~ m+<\s*/body+i) {
print OUT "$HTMLBoilerplate\n";
$HTMLFoundEndBody = 1;
}
}
if (!$HTMLFoundEndBody) {
if ($text =~ m+<\s*/html+i) {
print OUT "$HTMLBoilerplate\n";
$HTMLFoundEndBody = 1;
}
}
print OUT $text;
}
=item append_to_html_part
Method that appends text to the spicified mime part, but does so by
parsing HTML and adding the text before </body> or </html> tags.
=cut
#***********************************************************************
# %PROCEDURE: append_to_html_part
# %ARGUMENTS:
# part -- a mime entity (of type text/html)
# msg -- text to append to the entity
# %RETURNS:
# 1 on success; 0 on failure.
# %DESCRIPTION:
# Appends text to $part, but does so by parsing HTML and adding the
# text before </body> or </html>
#***********************************************************************
sub append_to_html_part {
my($part, $boilerplate) = @_;
if (!$Features{"HTML::Parser"}) {
md_syslog('warning', "Attempt to call append_to_html_part, but HTML::Parser Perl module not installed");
return 0;
}
return 0 unless defined($part->bodyhandle);
my($path) = $part->bodyhandle->path;
return 0 unless (defined($path));
return 0 unless (open(IN, "<", "$path"));
if (!open(OUT, ">", "$path.tmp")) {
close(IN);
return(0);
}
$HTMLFoundEndBody = 0;
$HTMLBoilerplate = $boilerplate;
my($p);
$p = HTML::Parser->new(api_version => 3,
default_h => [\&html_echo, "text"],
end_h => [\&html_end, "text"]);
$p->unbroken_text(1);
$p->parse_file(*IN);
if (!$HTMLFoundEndBody) {
print OUT "\n$boilerplate\n";
}
close(IN);
close(OUT);
# Rename the path
return 0 unless rename($path, "$path.old");
unless (rename("$path.tmp", $path)) {
rename ("$path.old", $path);
return 0;
}
unlink "$path.old";
$Changed = 1;
return 1;
}
=item append_text_boilerplate
Method that appends text to text/plain part or parts.
=cut
#***********************************************************************
# %PROCEDURE: append_text_boilerplate
# %ARGUMENTS:
# msg -- root MIME entity.
# boilerplate -- boilerplate text to append
# all -- if 1, append to ALL text/plain parts. If 0, append only to
# FIRST text/plain part.
# %RETURNS:
# 1 if text was appended to at least one part; 0 otherwise.
# %DESCRIPTION:
# Appends text to text/plain part or parts.
#***********************************************************************
sub append_text_boilerplate {
my($msg, $boilerplate, $all) = @_;
my($part);
if (!$all) {
$part = find_part($msg, "text/plain", 1);
if (defined($part)) {
if (append_to_part($part, $boilerplate)) {
$Actions{'append_text_boilerplate'}++;
return 1;
}
}
return 0;
}
@FlatParts = ();
my($ok) = 0;
collect_parts($msg, 1);
foreach $part (@FlatParts) {
if (lc($part->head->mime_type) eq "text/plain") {
if (append_to_part($part, $boilerplate)) {
$ok = 1;
$Actions{'append_text_boilerplate'}++;
}
}
}
return $ok;
}
=item append_html_boilerplate
Method that appends text to text/html part or parts.
It tries to be clever and inserts the text before the </body> tag
to be able of being seen.
=cut
#***********************************************************************
# %PROCEDURE: append_html_boilerplate
# %ARGUMENTS:
# msg -- root MIME entity.
# boilerplate -- boilerplate text to append
# all -- if 1, append to ALL text/html parts. If 0, append only to
# FIRST text/html part.
# %RETURNS:
# 1 if text was appended to at least one part; 0 otherwise.
# %DESCRIPTION:
# Appends text to text/html part or parts. Tries to be clever and
# insert the text before the </body> tag so it has a hope in hell of
# being seen.
#***********************************************************************
sub append_html_boilerplate {
my($msg, $boilerplate, $all) = @_;
my($part);
if (!$all) {
$part = find_part($msg, "text/html", 1);
if (defined($part)) {
if (append_to_html_part($part, $boilerplate)) {
$Actions{'append_html_boilerplate'}++;
return 1;
}
}
return 0;
}
@FlatParts = ();
my($ok) = 0;
collect_parts($msg, 1);
foreach $part (@FlatParts) {
if (lc($part->head->mime_type) eq "text/html") {
if (append_to_html_part($part, $boilerplate)) {
$ok = 1;
$Actions{'append_html_boilerplate'}++;
}
}
}
return $ok;
}
sub _anonymize_text_uri {
my ($part) = @_;
return 0 unless defined($part->bodyhandle);
my($path) = $part->bodyhandle->path;
my $npath = $path . '.tmp';
return 0 unless (defined($path));
my $body = $part->bodyhandle;
# If there's no body, then we can't add
return 0 unless $body;
my $ifh = $body->open('r');
return 0 unless $ifh;
my $ofh;
if (!open($ofh, '>', $npath)) {
$ifh->close();
return 0;
}
my $line;
my $nline;
while (defined($line = $ifh->getline())) {
if($line =~ /https?\:\/\/.{3,512}\/(.{1,30})?(\&|\?)utm([_a-z0-9=]+)/) {
my @params = split(/(\?|\&|\s+)/, $line);
foreach my $p ( @params ) {
if($p =~ /(\?|\&)?utm_.{1,20}\=.{1,64}/) {
next;
} else {
$nline .= $p;
}
}
$nline =~ s/(\&{2,}|\?{2,}|\n)//g;
$ofh->print($nline);
} else {
$ofh->print($line);
}
}
$ifh->close();
$ofh->close();
# Rename over the old path
return 1 if rename($npath, $path);
# Rename failed
unlink($npath);
$Changed = 1;
return 0;
}
sub html_utm_filter {
my($text) = @_;
my $nline;
if($text =~ /https?\:\/\/.{3,512}\/(.{1,30})?(\&|\?)utm([_a-z0-9=]+)/) {
my @params = split(/(\?|\&|\s+|\>|\"|\')/, $text);
foreach my $p ( @params ) {
if($p =~ /(\?|\&)?utm_.{1,20}\=.{1,64}/) {
next;
} else {
$nline .= $p;
}
}
$nline =~ s/(\&{2,}|\?{2,}|\n)//g;
print OUT $nline;
} else {
print OUT $text;
}
}
sub _anonymize_html_uri {
my ($part) = @_;
if (!$Features{"HTML::Parser"}) {
md_syslog('warning', "Attempt to call append_to_html_part, but HTML::Parser Perl module not installed");
return 0;
}
return 0 unless defined($part->bodyhandle);
my($path) = $part->bodyhandle->path;
return 0 unless (defined($path));
return 0 unless (open(IN, "<", "$path"));
if (!open(OUT, ">", "$path.tmp")) {
close(IN);
return(0);
}
my($p);
$p = HTML::Parser->new(api_version => 3,
default_h => [\&html_utm_filter, "text"],
end_h => [\&html_echo, "text"]);
$p->unbroken_text(1);
$p->parse_file(*IN);
close(IN);
close(OUT);
# Rename the path
return 0 unless rename($path, "$path.old");
unless (rename("$path.tmp", $path)) {
rename ("$path.old", $path);
return 0;
}
unlink "$path.old";
$Changed = 1;
return 1;
}
=item anonymize_uri
Anonymize urls by removing all utm_* parameters,
takes the message part as parameter and returns
a boolean value if the sub succeeded or not.
=cut
sub anonymize_uri {
my ($msg) = @_;
@FlatParts = ();
my($ok) = 0;
collect_parts($msg, 1);
foreach $part (@FlatParts) {
if (lc($part->head->mime_type) =~ /text\/html/) {
if (_anonymize_html_uri($part)) {
$ok = 1;
}
} elsif (lc($part->head->mime_type) =~ /text\/plain/) {
if (_anonymize_text_uri($part)) {
$ok = 1;
}
}
}
return $ok;
}
=back
=cut
1;