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
|
#-*- perl -*-
#
# Copyright (C) 2001,2002,2003,2004,2005 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Filter.pm,v 1.47 2005/08/19 11:18:36 fukachan Exp $
#
package FML::Filter;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
use FML::Filter::ErrorStatus qw(error_set error error_clear);
# debug
my $debug = 0;
# ERROR CODES
my $FILTER_OK = 0;
my $FILTER_ERR_REJECT = 1;
my $FILTER_ERR_IGNORE = 2;
my $FILTER_ERR_HEADER_REWRITE = 3;
=head1 NAME
FML::Filter - entry point for FML::Filter::* modules.
=head1 SYNOPSIS
... under reconstruction now. synopsis disabled once ...
where C<$message> is C<Mail::Message> object.
=head1 DESCRIPTION
top level dispatcher for FML filtering engine.
It consists of two types, header and body filtering engines.
The detail of rules is found in
L<FML::Filter::Header>,
L<FML::Filter::TextPlain>,
and
L<FML::Filter::MimeComponent>.
=head1 METHODS
=head2 new()
constructor.
=cut
# Descriptions: constructor.
# Arguments: OBJ($self) OBJ($curproc)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self, $curproc) = @_;
my ($type) = ref($self) || $self;
my $me = { _curproc => $curproc };
return bless $me, $type;
}
# Descriptions: entry point for FML::Filter::* modules.
# Arguments: OBJ($self) OBJ($curproc)
# Side Effects: none
# Return Value: STR or UNDEF, error reason (string). return undef if ok.
sub article_filter
{
my ($self, $curproc) = @_;
my $message = $curproc->incoming_message();
my $config = $curproc->config();
if (defined $message) {
my $functions = $config->get_as_array_ref('article_filter_functions');
my $status = 0;
FUNCTION:
for my $function (@$functions) {
if ($config->yes( "use_${function}" )) {
$curproc->log("filter: check by $function") if $debug;
my $fp = "_apply_$function";
$status = $self->$fp($curproc, $message);
}
else {
if ($debug) {
$curproc->log("filter: $function check disabled.");
}
}
last FUNCTION if $status;
}
return $status if $status;
}
return undef;
}
# Descriptions: size based filtering.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_article_size_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
use FML::Filter::Size;
my $obj = new FML::Filter::Size $curproc;
if (defined $obj) {
$obj->set_class('incoming_article');
# overwrite filter rules based on FML::Config
my $rules = $config->get_as_array_ref('article_size_filter_rules');
# overwrite rules
if (defined $rules) {
$obj->set_rules( $rules );
}
# go check
$obj->size_check($mesg);
if ($obj->error()) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_REJECT);
my $x;
$x = $obj->error();
$x =~ s/\s*at .*$//o;
$x =~ s/[\n\s]*$//mo;
$self->error_set($x);
return $x;
}
}
return 0;
}
# Descriptions: header based filter.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_article_header_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
use FML::Filter::Header;
my $obj = new FML::Filter::Header;
if (defined $obj) {
# overwrite filter rules based on FML::Config
my $rules = $config->get_as_array_ref('article_header_filter_rules');
# overwrite rules
if (defined $rules) {
$obj->set_rules( $rules );
}
# go check
$obj->header_check($mesg);
if ($obj->error()) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_REJECT);
my $x;
$x = $obj->error();
$x =~ s/\s*at .*$//o;
$x =~ s/[\n\s]*$//mo;
$self->error_set($x);
return $x;
}
}
return 0;
}
# Descriptions: filter non MIME format message.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: 0 (always ok, anyway)
sub _apply_article_non_mime_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
if ($config->yes( 'use_article_non_mime_filter' )) {
my $hdr = $curproc->incoming_message_header();
my $rules =
$config->get_as_array_ref('article_non_mime_filter_rules');
RULE:
for my $rule (@$rules) {
if ($debug) {
$curproc->log("filter: article_non_mime_filter.check $rule");
}
if ($rule eq 'permit') {
return 0;
}
if ($rule eq 'reject_empty_content_type') {
if (defined $hdr) {
my $type = $hdr->get('content-type') || '';
unless ($type) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_REJECT);
my $s = "no Content-Type:";
$self->error_set($s);
return $s;
}
}
}
}
}
return 0;
}
# Descriptions: syntax check for text(/plain).
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_article_text_plain_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
if ($config->yes( 'use_article_text_plain_filter' )) {
use FML::Filter::TextPlain;
my $obj = new FML::Filter::TextPlain;
# overwrite filter rules based on FML::Config
my $rules =
$config->get_as_array_ref('article_text_plain_filter_rules');
if (defined $rules) {
$obj->set_rules( $rules );
}
# go check
$obj->body_check($mesg);
if ($obj->error()) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_REJECT);
my $x;
$x = $obj->error();
$x =~ s/\s*at .*$//o;
$x =~ s/[\n\s]*$//mo;
$self->error_set($x);
return $x;
}
}
return 0;
}
# Descriptions: analyze MIME structure and filter it if matched.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_article_mime_component_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
if ($config->yes( 'use_article_mime_component_filter' )) {
use FML::Filter::MimeComponent;
my $obj = new FML::Filter::MimeComponent $curproc;
my $file = $config->get('article_mime_component_filter_rules');
if (-f $file) {
$obj->read_filter_rule_file($file);
$obj->mime_component_check($mesg);
}
else {
if ($debug) {
$curproc->log("filter: disabled since rule file not found");
}
return 0;
}
if ($obj->error()) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_REJECT);
my $x;
$x = $obj->error();
$x =~ s/\s*at .*$//o;
$x =~ s/[\n\s]*$//mo;
$self->error_set($x);
return $x;
}
}
return 0;
}
# Descriptions: analyze external spam/virus checker.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_article_spam_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
if ($config->yes( 'use_article_spam_filter' )) {
my $drivers = $config->get_as_array_ref('article_spam_filter_drivers');
DRIVER:
for my $driver (@$drivers) {
$curproc->logdebug("call external spam driver: $driver");
my $r = $self->_external_article_filter($curproc, $mesg, $driver);
if ($r) {
# XXX-TODO: $FILTER_ERR_IGNORE ok?
# set reject if action unspecified.
unless ($curproc->filter_state_get_error("article_filter")) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_IGNORE);
}
return $r;
}
}
}
else {
$curproc->logdebug("spam filter disabled");
}
return 0;
}
# Descriptions: analyze external virus checker.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_article_virus_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
if ($config->yes( 'use_article_virus_filter' )) {
my $drivers =
$config->get_as_array_ref('article_virus_filter_drivers');
DRIVER:
for my $driver (@$drivers) {
$curproc->logdebug("call external virus driver: $driver");
my $r = $self->_external_article_filter($curproc, $mesg, $driver);
if ($r) {
# XXX-TODO: $FILTER_ERR_IGNORE ok?
# set reject if action unspecified.
unless ($curproc->filter_state_get_error("article_filter")) {
$curproc->filter_state_set_error("article_filter",
$FILTER_ERR_IGNORE);
}
return $r;
}
}
}
else {
$curproc->logdebug("virus filter disabled");
}
return 0;
}
# Descriptions: call external filter.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg) STR($driver)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _external_article_filter
{
my ($self, $curproc, $mesg, $driver) = @_;
my $ext_filter = undef;
eval q{
use FML::Filter::External;
$ext_filter = new FML::Filter::External;
$ext_filter->spawn($curproc, $mesg, $driver);
};
if ($@) {
$curproc->logerror($@);
}
if (defined $ext_filter) {
my $x;
if ($x = $ext_filter->error()) {
$x =~ s/\s*at .*$//o;
$x =~ s/[\n\s]*$//mo;
$self->error_set($x);
return $x;
}
}
else {
$curproc->logerror("FML::Filter::External failed");
}
return 0;
}
=head1 REJECT NOTIFICATION
=head2 article_filter_reject_notice($curproc, $msg_args)
send back message on rejection, with reason if could.
=cut
# Descriptions: send back message on rejection, with reason if could.
# Arguments: OBJ($self) OBJ($curproc) HASH_REF($msg_args)
# Side Effects: update reply message queue
# Return Value: none
sub article_filter_reject_notice
{
my ($self, $curproc, $msg_args) = @_;
$self->_filter_reject_notice($curproc, $msg_args, "article");
}
# Descriptions: send back message on rejection, with reason if could.
# Arguments: OBJ($self) OBJ($curproc) HASH_REF($msg_args)
# Side Effects: update reply message queue
# Return Value: none
sub command_mail_filter_reject_notice
{
my ($self, $curproc, $msg_args) = @_;
$self->_filter_reject_notice($curproc, $msg_args, "command_mail");
}
# Descriptions: send back message on rejection, with reason if could.
# Arguments: OBJ($self) OBJ($curproc) HASH_REF($msg_args) STR($class)
# Side Effects: update reply message queue
# Return Value: none
sub _filter_reject_notice
{
my ($self, $curproc, $msg_args, $class) = @_;
my $cred = $curproc->credential();
my $config = $curproc->config();
my $msg = $curproc->incoming_message();
my $r = $msg_args->{ _arg_reason } || 'unknown';
my $type = $config->{ "${class}_filter_reject_notice_data_type" } ||
'string';
my $size = 2048;
# recipients
my $list =
$config->get_as_array_ref("${class}_filter_reject_notice_recipients");
my $rcpts = $curproc->address_resolve($list);
$msg_args->{ recipient } = $rcpts;
$msg_args->{ _arg_address } = $cred->sender();
$msg_args->{ _arg_size } = $size;
$curproc->log("filter notice: [ @$rcpts ]");
$curproc->reply_message_nl("error.reject_post",
"your post is rejected.",
$msg_args);
$curproc->reply_message_nl("error.reject_post_reason",
"reason(s) for rejection: $r",
$msg_args);
# XXX-TODO: need swtich to forward whether header or header+body ?
# what we forward ?
if ($type eq 'multipart') {
$curproc->reply_message($msg, $msg_args);
}
elsif ($type eq 'string') {
my $s = $msg->whole_message_as_str( {
indent => ' ',
size => $size,
});
my $r = "The first $size bytes of this message follows:";
$curproc->reply_message_nl("error.reject_notice_preamble",
$r,
$msg_args);
$curproc->reply_message(sprintf("\n\n%s", $s), $msg_args);
}
else {
my $s = "unknown ${class}_filter_reject_notice_data_type: $type";
$curproc->logerror($s);
}
}
=head1 COMMAND MAIL
=head2 command_mail_filter($curproc)
entry point for FML::Filter::* modules.
=cut
# Descriptions: entry point for FML::Filter::* modules.
# Arguments: OBJ($self) OBJ($curproc)
# Side Effects: none
# Return Value: STR or UNDEF, error reason (string). return undef if ok.
sub command_mail_filter
{
my ($self, $curproc) = @_;
my $message = $curproc->incoming_message();
my $config = $curproc->config();
if (defined $message) {
my $functions =
$config->get_as_array_ref('command_mail_filter_functions');
my $status = 0;
FUNCTION:
for my $function (@$functions) {
if ($config->yes( "use_${function}" )) {
$curproc->log("filter: check by $function") if $debug;
my $fp = "_apply_$function";
$status = $self->$fp($curproc, $message);
}
else {
if ($debug) {
$curproc->log("filter: $function check disabled.");
}
}
last FUNCTION if $status;
}
return $status if $status;
}
return undef;
}
# Descriptions: size based filtering.
# Arguments: OBJ($self) OBJ($curproc) OBJ($mesg)
# Side Effects: none
# Return Value: STR(reason) or 0 (not trapped, ok)
sub _apply_command_mail_size_filter
{
my ($self, $curproc, $mesg) = @_;
my $config = $curproc->config();
use FML::Filter::Size;
my $obj = new FML::Filter::Size $curproc;
if (defined $obj) {
$obj->set_class('incoming_command_mail');
# overwrite filter rules based on FML::Config
my $rules =
$config->get_as_array_ref('command_mail_size_filter_rules');
# overwrite rules
if (defined $rules) {
$obj->set_rules( $rules );
}
# go check
$obj->size_check($mesg);
if ($obj->error()) {
$curproc->filter_state_set_error("commnd_mail",
$FILTER_ERR_REJECT);
my $x;
$x = $obj->error();
$x =~ s/\s*at .*$//o;
$x =~ s/[\n\s]*$//mo;
$self->error_set($x);
return $x;
}
}
return 0;
}
=head1 CODING STYLE
See C<http://www.fml.org/software/FNF/> on fml coding style guide.
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001,2002,2003,2004,2005 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::Filter first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|