| 1 | package Mail::Abuse::Incident::Received; | ||||||||||||||||||||||||||||
| 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 | 2 | ▶ | use NetAddr::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Date::Parse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use base 'Mail::Abuse::Incident'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # The code below should be in a single line | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | our $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =pod | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Mail::Abuse::Incident::Received - Parses Received: headers in an abuse report | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | use Mail::Abuse::Report; | ||||||||||||||||||||||||||||
| 26 | use Mail::Abuse::Incident::Received; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | my $i = new Mail::Abuse::Incident::Received; | ||||||||||||||||||||||||||||
| 29 | my $report = new Mail::Abuse::Report (incidents => [$i] ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | This class parses standard Received: headers included in a given abuse | ||||||||||||||||||||||||||||
| 34 | report. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Entries in the configuration file can control the behavior of this | ||||||||||||||||||||||||||||
| 37 | module. The following entries are recognized: | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | use constant DEBUG => 'debug received'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =pod | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =over | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =item B<debug received> | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | When set to a true value, causes some debug information to be produced | ||||||||||||||||||||||||||||
| 50 | via C<warn()>. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub _add_time ($$$) | ||||||||||||||||||||||||||||
| 56 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_add_time: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 57 | 6 | ▶ | my $time = str2time($_[1], $_[2]) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | for (@{$_[0]}) | |||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return if $_ == $time; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 6 | push @{$_[0]}, $time; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub _add_ip ($$) | ||||||||||||||||||||||||||||
| 68 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_add_ip: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 69 | 6 | ▶ | my $ip = new NetAddr::IP $_[1] or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | for (@{$_[0]}) | |||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return if $_ == $ip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | push @{$_[0]}, $ip; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =pod | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =back | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | The functions provided by this module, are described below | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =over | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =item C<parse($report)> | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Parses all the Received: headers, creating an instance with each | ||||||||||||||||||||||||||||
| 90 | combination of IP address and timestamp found. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Returns a list of objects of the same class, with the incident data | ||||||||||||||||||||||||||||
| 93 | (IP address, timestamp and other information) filled. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub parse | ||||||||||||||||||||||||||||
| 98 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=16 SCAR=31.7
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 3 | my $rep = shift; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | my @ret = (); # Default return | |||||||||||||||||||||||||||
| 103 | 3 | my $count = 0; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | my $text = undef; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | ▶ | my $debug = $rep->config ? $rep->config->{&DEBUG} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | warn "M::A::I::Received: debug\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | if ($rep->normalized) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 3 | $text = $rep->body; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else | ||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | # Skip the report headers and focus | ||||||||||||||||||||||||||||
| 118 | # on the offender's | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($ {$rep->text} =~ m!^\s*\n(.*)!xms) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 0 | my $t = $1; | |||||||||||||||||||||||||||
| 123 | 0 | $text = \$t; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | else | ||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||
| 127 | 0 | $text = $rep->text; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | unless ($$text and $$text =~ m!Received: !m) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$$text and $$text =~ /Received: /m
MC/DC0%0/2
$$text and $$text =~ /Received: /m
$$text
$$text =~ /Received: /m
Truth table33%1/3
A and B
A= $$textB= $$text =~ /Received: /m
| |||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | warn "M::A::I::Received: No Received: headers in sight\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | if $debug; | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 3 | ▶ | while ($$text and $$text =~ m!^(Received: .*?)(?=([-\w]+: |^\s*$))!msg) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$$text and $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
MC/DC50%1/2
$$text and $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
$$text
$$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
Truth table66%2/3
A and B
A= $$textB= $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
| |||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | # Analyze this Received: header | ||||||||||||||||||||||||||||
| 140 | 6 | my $header = $1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 6 | ▶ | warn "M::A::I::Received: Matching header [$header]\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | if $debug; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 6 | my @a = (); | |||||||||||||||||||||||||||
| 146 | 6 | my @t = (); | |||||||||||||||||||||||||||
| 147 | # Add numeric IP addresses | ||||||||||||||||||||||||||||
| 148 | 6 | ▶ | _add_ip \@a, $1 while $header =~ m/(\d+\.\d+\.\d+\.\d+)/g; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 6 | ▶ | _add_time \@t, $1, $rep->tz | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | while $header =~ m/((\w+,\s+)?\d+\s\w+\s\d+\s[\d:]+\s[-+]?\w+)/g; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 6 | for my $a (@a) | |||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 6 | for my $t (@t) | |||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | 6 | my $i = $self->new(); | |||||||||||||||||||||||||||
| 158 | 6 | $i->ip($a); | |||||||||||||||||||||||||||
| 159 | 6 | $i->time($t); | |||||||||||||||||||||||||||
| 160 | 6 | $i->type('spam/Received'); | |||||||||||||||||||||||||||
| 161 | 6 | $i->data($header); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 6 | ▶ | warn "M::A::I::Received: Add incident for ", $i->ip, ", ", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | $i->time, "\n" if $debug; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 6 | push @ret, $i; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | return @ret; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||