Mail/Abuse/Filter/IP.pm

filter: Statementstmt 91.8% 68 / 74 Branchbran 57.8% 22 / 38 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 76.7% 109 / 142 CC 26 SCAR 34.5
CC
26
Cov
78%
criteria
41.0
BEGIN
1.0
BEGIN
1.0
CRAP 31.6
« Mail/Abuse/Processor/Explain.pm ↑ Summary Template/Plugin/Abuse.pm »
1 package Mail::Abuse::Filter::IP;
2  
3 2 require 5.005_62;
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use base 'Mail::Abuse::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use constant DEBUG => 'debug ip filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use constant WITHIN => 'source ip within';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use constant OUTSIDE => 'source ip outside';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # The code below should be in a single line
18  
19 2 our $VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
20  
21 =pod
22  
23 =head1 NAME
24  
25 Mail::Abuse::Filter::IP - Filter incidents according to its origin IP
26  
27 =head1 SYNOPSIS
28  
29 use Mail::Abuse::Filter::IP;
30 my $f = new Mail::Abuse::Filter::IP;
31  
32 $report->filter([$f]);
33  
34 =head1 DESCRIPTION
35  
36 Removes those events from a C<Mail::Abuse::Report> whose origin does
37 not match the rules enforced by this module. The actual rules must be
38 specified in the configuration file for the abuse report.
39  
40 The following configuration keys are recognized:
41  
42 =over
43  
44 =item B<source ip within>
45  
46 If specified, the source IP address must fall within the subnets given
47 as aguments to this configuration keys. Multiple subnets can be
48 specified by separating them with whitespace or commas.
49  
50 If left unspecified, this field defaults to "0/0", which matches any
51 source IP address.
52  
53 Subnets can be written in any format supported by L<NetAddr::IP>.
54  
55 =item B<source ip outside>
56  
57 If specified, the source IP address must not lie within the subnets
58 specified. Subnets can be separated with spaces or commas.
59  
60 =item B<debug ip filter>
61  
62 Set to a true value to see various debugging messages.
63  
64 =back
65  
66 The following methods are implemented in this class.
67  
68 =over
69  
70 =item C<criteria($report, $incident)>
71  
72 This function receives a C<Mail::Abuse::Report> and a
73 C<Mail::Abuse::Incident> object. It returns a true value if the
74 incident should be handled or false otherwise. This function will be
75 generally called by the C<Mail::Abuse::Report> object when requested
76 to filter its events.
77  
78 The key C<filtered> in the C<Mail::Abuse::Report> object will be
79 incremented for each incident removed.
80  
81 =cut
82  
83 sub criteria
84 9 ▶{
Subroutine criteria: called CC=26 SCAR=37.1
Pod documented
85 9 my $self = shift;
86 9 my $rep = shift;
87 9 my $inc = shift;
88  
89 9 ▶ if (!$self->within and $rep->config->{&WITHIN})
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->within and $rep->config->{&WITHIN}) 3 6 9
Condition66%2/3
not $self->within and $rep->config->{&WITHIN}
!ll&&!rl&&r
603
MC/DC50%1/2
not $self->within and $rep->config->{&WITHIN}
not $self->within $rep->config->{&WITHIN}
Truth table66%2/3
not(A) and B
A= not $self->withinB= $rep->config->{&WITHIN}
ABresult
0X0
100
111
90 {
91 # unless (ref $rep->config->{&WITHIN} eq 'ARRAY')
92 # {
93 # $rep->config->{&WITHIN} = [ $rep->config->{&WITHIN} ];
94 # }
95 3 $self->within([]);
96 3 for my $ip (map { new NetAddr::IP $_ }
97 split m/[\s,]+/, $rep->config->{&WITHIN})
98 {
99 4 ▶ unless ($ip)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 4 4
100 {
101 0 die "Filter::IP: Please check your '", &WITHIN,
102 "' clause for errors\n";
103 }
104 warn "Filter::IP: Adding $ip to 'within' clause\n"
105 4 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 4 4
106 4 push @{$self->within}, $ip;
107 }
108 0 warn "Filter::IP: 'within' clause contains ", scalar @{$self->within},
109 3 ▶ " subnets\n" if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 3 3
110 }
111  
112 9 ▶ if (!$self->outside and $rep->config->{&OUTSIDE})
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->outside and $rep->config->{&OUTSIDE}) 3 6 9
Condition66%2/3
not $self->outside and $rep->config->{&OUTSIDE}
!ll&&!rl&&r
603
MC/DC50%1/2
not $self->outside and $rep->config->{&OUTSIDE}
not $self->outside $rep->config->{&OUTSIDE}
Truth table66%2/3
not(A) and B
A= not $self->outsideB= $rep->config->{&OUTSIDE}
ABresult
0X0
100
111
113 {
114 # unless (ref $rep->config->{&OUTSIDE} eq 'ARRAY')
115 # {
116 # $rep->config->{&OUTSIDE} = [ $rep->config->{&OUTSIDE} ];
117 # }
118 3 $self->outside([]);
119 3 for my $ip (map { new NetAddr::IP $_ }
120 split /[\s,]+/, $rep->config->{&OUTSIDE})
121 {
122 3 ▶ unless ($ip)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 3 3
123 {
124 0 die "Filter::IP: Please check your '", &OUTSIDE,
125 "' clause for errors\n";
126 }
127 warn "Filter::IP: Adding $ip to 'outside' clause\n"
128 3 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 3 3
129 3 push @{$self->outside}, $ip;
130 }
131 warn "Filter::IP: 'outside' clause contains ",
132 0 scalar @{$self->outside}, " subnets\n"
133 3 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 3 3
134 }
135  
136 9 ▶ if ($self->within)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->within) 9 0 9
137 {
138 9 ▶ if (grep
Branch: 1 branch
BranchTrueFalseTotal
if (grep {my $c = $_->contains($inc->ip); warn "Filter::IP: (within) $_ contains " . $inc->ip . "\n" if $c and $rep->config->{&DEBUG}; $c;} @{$self->within;}) { } 6 3 9
139 {
140 12 my $c = $_->contains($inc->ip);
141 warn "Filter::IP: (within) $_ contains " . $inc->ip . "\n"
142 12 ▶ if $c and $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $c and $rep->config->{&DEBUG} 0 12 12
Condition66%2/3
$c and $rep->config->{&DEBUG}
!ll&&!rl&&r
660
MC/DC0%0/2
$c and $rep->config->{&DEBUG}
$c $rep->config->{&DEBUG}
Truth table66%2/3
A and B
A= $cB= $rep->config->{&DEBUG}
ABresult
0X0
100
111
143 12 $c;
144 9 } @{$self->within})
145 {
146 warn "Filter::IP: 'within' clause allows " . $inc->ip . "\n"
147 6 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 6 6
148 }
149 else
150 {
151 warn "Filter::IP: 'within' clause denies " . $inc->ip . "\n"
152 3 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 3 3
153 3 ▶ $rep->filtered(0) unless $rep->filtered;
Branch: 1 branch
BranchTrueFalseTotal
unless $rep->filtered 2 1 3
154 3 $rep->filtered($rep->filtered + 1);
155 3 return;
156 }
157 }
158  
159 6 ▶ if ($self->outside)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->outside) 6 0 6
160 {
161 6 ▶ if (grep
Branch: 1 branch
BranchTrueFalseTotal
if (grep {my $c = $_->contains($inc->ip); warn "Filter::IP: (outside) $_ contains " . $inc->ip . "\n" if $c and $rep->config->{&DEBUG}; $c;} @{$self->outside;}) { } 0 6 6
162 {
163 6 my $c = $_->contains($inc->ip);
164 warn "Filter::IP: (outside) $_ contains " . $inc->ip . "\n"
165 6 ▶ if $c and $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $c and $rep->config->{&DEBUG} 0 6 6
Condition33%1/3
$c and $rep->config->{&DEBUG}
!ll&&!rl&&r
600
MC/DC0%0/2
$c and $rep->config->{&DEBUG}
$c $rep->config->{&DEBUG}
Truth table33%1/3
A and B
A= $cB= $rep->config->{&DEBUG}
ABresult
0X0
100
111
166 6 $c;
167 6 } @{$self->outside})
168 {
169 warn "Filter::IP: 'outside' clause denies " . $inc->ip . "\n"
170 0 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 0 0
171 0 return;
172 }
173 else
174 {
175 warn "Filter::IP: 'outside' clause allows " . $inc->ip . "\n"
176 6 ▶ if $rep->config->{&DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $rep->config->{&DEBUG} 0 6 6
177 }
178 }
179  
180 6 return $inc;
181 }
182  
183 __END__
« Mail/Abuse/Processor/Explain.pm ↑ Summary Template/Plugin/Abuse.pm »