| 1 | package Mail::Abuse::Processor::Table; | ||||||||||||||||||||||||||||
| 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 PerlIO::gzip; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use NetAddr::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Tie::NetAddr::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use base 'Mail::Abuse::Processor'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # The code below should be in a single line | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | our $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r }; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | our %Table = (); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =pod | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Mail::Abuse::Processor::Table - Match incidents to users using a static table | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use Mail::Abuse::Processor::Table; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use Mail::Abuse::Report; | ||||||||||||||||||||||||||||
| 32 | my $p = new Mail::Abuse::Processor::Table; | ||||||||||||||||||||||||||||
| 33 | my $report = new Mail::Abuse::Report (processors => [ $p ]); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # ... other pieces of code that configure the report ... | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This class matches incidents to | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =over | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =item B<debug table> | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | If set to a true value, causes this module to emit debugging info | ||||||||||||||||||||||||||||
| 46 | using C<warn()>. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | use constant DEBUG => 'debug table'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =pod | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item B<table location> | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | The path of a file where the information table is to be found. The | ||||||||||||||||||||||||||||
| 57 | file consists on columns separated by whitespace and should have the | ||||||||||||||||||||||||||||
| 58 | following format: | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | IP-range var=value;var=value;... | ||||||||||||||||||||||||||||
| 61 | IP-range var=value;var=value;... | ||||||||||||||||||||||||||||
| 62 | IP-range var1.var2=value;var=value;... | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | B<IP-range> should be an IP subnet in any format that can be | ||||||||||||||||||||||||||||
| 65 | understood by C<NetAddr::IP>. Tipically, this should be CIDR location, | ||||||||||||||||||||||||||||
| 66 | for readability. The following example: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 10.0.0.0 foo=bar;baz=camel;fumble.foo=pivot | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Would yield the following structure as result when a match occurs: | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | { foo => 'bar', baz = 'camel', fumble => { foo = 'pivot' }} | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | These values should not be changed, as currently they are references | ||||||||||||||||||||||||||||
| 75 | to the actual data read. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | On the last column, a number of variables and its values can be | ||||||||||||||||||||||||||||
| 78 | specified. Multiple tuples can be separated by a ';' character. The | ||||||||||||||||||||||||||||
| 79 | dot in the name can be used in place of the C<-E<gt>> operator, to | ||||||||||||||||||||||||||||
| 80 | easily create hashrefs. These hashrefs are stored in the | ||||||||||||||||||||||||||||
| 81 | C<Mail::Abuse::Incident> object that is passed to the C<process()> | ||||||||||||||||||||||||||||
| 82 | method. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Comments are delimited by a '#' character, which causes the text up to | ||||||||||||||||||||||||||||
| 85 | the end of line to be ignored. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | use constant LOCATION => 'table location'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =pod | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | The following functions are implemented. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =over | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =item C<process($report)> | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Takes a C<Mail::Abuse::Report> object as an argument and, for each | ||||||||||||||||||||||||||||
| 102 | C<Mail::Abuse::Incident> collected, perform a lookup in the given | ||||||||||||||||||||||||||||
| 103 | table, attempting to match it by IP address. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | If a match is found, all the supplied hashrefs are introduced in the | ||||||||||||||||||||||||||||
| 106 | C<Mail::Abuse::Incident> under the key C<table>. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | ; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub _parse_table ($) | ||||||||||||||||||||||||||||
| 112 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_parse_table: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||
| 113 | 1 | my $loc = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | return if %Table; # do nothing if this is populated | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | my $fh = new IO::File; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | unless ($fh->open($loc, '<:gzip(autopop)')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | die "M::A::P::Table: Failed to open table $loc: $!\n"; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | while (my $line = <$fh>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 1 | chomp $line; | |||||||||||||||||||||||||||
| 127 | 1 | $line =~ s/^\s+//; | |||||||||||||||||||||||||||
| 128 | 1 | $line =~ s/#.*$//; # Get rid of comments and ignore blanks | |||||||||||||||||||||||||||
| 129 | 1 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my ($spec, $data) = split /\s+/, $line, 2; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | next unless $spec = new NetAddr::IP $spec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | my $hash = {}; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | for my $tuple (split /;/, $data) | |||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 4 | my $where = $hash; | |||||||||||||||||||||||||||
| 140 | 4 | my ($var, $val) = split /=/, $tuple, 2; | |||||||||||||||||||||||||||
| 141 | 4 | my @keys = split /\./, $var; | |||||||||||||||||||||||||||
| 142 | 4 | my $last = pop @keys; | |||||||||||||||||||||||||||
| 143 | 4 | for my $item (@keys) | |||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 5 | ▶ | $where->{$item} = {} unless $where->{$item}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 5 | $where = $where->{$item}; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 4 | $where->{$last} = $val; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 1 | $Table{$spec} = $hash; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | $fh->close; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub process | ||||||||||||||||||||||||||||
| 157 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=10 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 1 | my $rep = shift; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | ▶ | unless ($rep->config or ref $rep->config ne 'HASH') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rep->config or ref $rep->config ne "HASH"
MC/DC0%0/2
$rep->config or ref $rep->config ne "HASH"
$rep->config
ref $rep->config ne "HASH"
Truth table33%1/3
A or B
A= $rep->configB= ref $rep->config ne "HASH"
| |||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 0 | warn "Invalid or no config"; | |||||||||||||||||||||||||||
| 164 | 0 | return; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 1 | my $loc = $rep->config->{&LOCATION}; | |||||||||||||||||||||||||||
| 168 | 1 | my $debug = $rep->config->{&DEBUG}; | |||||||||||||||||||||||||||
| 169 | 1 | my $fh; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | ▶ | unless ($loc and -f $loc) # Bail out if given garbage detail path | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$loc and -f $loc
MC/DC0%0/2
$loc and -f $loc
$loc
-f $loc
Truth table33%1/3
A and B
A= $locB= -f $loc
| |||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 0 | warn "M::A::P::Table: ", &LOCATION, | |||||||||||||||||||||||||||
| 174 | " does not point to a valid file\n"; | ||||||||||||||||||||||||||||
| 175 | 0 | return; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | _parse_table $loc; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | return if @{$rep->incidents} == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | for my $i (@{$rep->incidents}) | |||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||
| 184 | 5 | ▶ | if (my $result = $Table{$i->ip}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | { # Match! | ||||||||||||||||||||||||||||
| 186 | 3 | ▶ | $i->table({}) unless $i->table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 3 | $i->table->{$_} = $result->{$_} for keys %$result; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 1 | return 1; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 2 | tie %Table, 'Tie::NetAddr::IP'; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||