| 1 | package Mail::Abuse::Processor::TableDBI; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | require 5.005_62; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use NetAddr::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use base 'Mail::Abuse::Processor'; | ||||||||||||||||||||||||||
|
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.3 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =pod | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Mail::Abuse::ProcessorTableDBI - Match incidents to other data using a DBI table | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | use Mail::Abuse::ProcessorTableDBI; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use Mail::Abuse::Report; | ||||||||||||||||||||||||||||
| 28 | my $p = new Mail::Abuse::ProcessorTableDBI; | ||||||||||||||||||||||||||||
| 29 | my $report = new Mail::Abuse::Report (processors => [ $p ]); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # ... other pieces of code that configure the report ... | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | This class matches incidents to data gathered into a DBI table. The | ||||||||||||||||||||||||||||
| 36 | most recent entry in the DBI table will be used as the result. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item B<debug dbi table> | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | If set to a true value, causes this module to emit debugging info | ||||||||||||||||||||||||||||
| 43 | using C<warn()>. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | use constant DEBUG => 'debug dbi table'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =pod | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =item B<dbi table dsn> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Specifies the DSN used to connect to the DBI database holding the | ||||||||||||||||||||||||||||
| 54 | table we will be using. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | use constant DSN => 'dbi table dsn'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =pod | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =item B<dbi table user> | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | The username used to connect to the DSN. If specified in the DSN | ||||||||||||||||||||||||||||
| 65 | itself, can be left blank. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | use constant USER => 'dbi table user'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =pod | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item B<dbi table pass> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | The password used to connect to the DSN. If specified in the DSN, it | ||||||||||||||||||||||||||||
| 76 | can be left blank. Note that this is not good, as the DSN will be | ||||||||||||||||||||||||||||
| 77 | printed along DBI-related error messages. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | use constant PASS => 'dbi table pass'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =pod | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =item B<dbi table name> | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | The name of the table to query. This table must define the following | ||||||||||||||||||||||||||||
| 88 | columns. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =over | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item B<CIDR_Start> | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | The beginning of the CIDR range for which the remaining columns | ||||||||||||||||||||||||||||
| 95 | apply. This is supposed to be the network IP address, as an unsigned | ||||||||||||||||||||||||||||
| 96 | int. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item B<CIDR_End> | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | The end of the CIDR range for which the remaining columns apply. This | ||||||||||||||||||||||||||||
| 101 | is supposed to be the broadcast IP address, as an unsigned int. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item B<TIME_Start> | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | The number of seconds since the epoch for the start of the time window | ||||||||||||||||||||||||||||
| 106 | in which this entry is valid. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item B<TIME_End> | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | The number of seconds since the epoch for the end of the time window | ||||||||||||||||||||||||||||
| 111 | in which this entry is valid. If this value is C<NULL>, the entry is | ||||||||||||||||||||||||||||
| 112 | assumed to be valid forever. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | The rest of the columns will be included in the resulting information | ||||||||||||||||||||||||||||
| 117 | stored into the incident. For instance, a column named "Foo" will | ||||||||||||||||||||||||||||
| 118 | cause the following structure to be added to the incident | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | { Foo => column contents } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | This is similar to the way in which L<Mail::Abuse::Processor::Table> | ||||||||||||||||||||||||||||
| 123 | works. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | use constant TABLE => 'dbi table name'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Perform initialization of the DBI connection and fetch the | ||||||||||||||||||||||||||||
| 130 | # information we need for the subsequent queries. Caching is | ||||||||||||||||||||||||||||
| 131 | # done to prevent spurious connect/disconnect cycles. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub _init_dbi | ||||||||||||||||||||||||||||
| 134 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init_dbi: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 1 | my $rep = shift; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | my $dsn = $rep->config->{&DSN}; | |||||||||||||||||||||||||||
| 139 | 1 | my $user = $rep->config->{&USER}; | |||||||||||||||||||||||||||
| 140 | 1 | my $pass = $rep->config->{&PASS}; | |||||||||||||||||||||||||||
| 141 | 1 | my $table = $rep->config->{&TABLE}; | |||||||||||||||||||||||||||
| 142 | 1 | my $debug = $rep->config->{&DEBUG}; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | return if $self->{_table_dbi}->{dbh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Perform the connection and prepare - die on failure | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | $self->{_table_dbi}->{dbh} = DBI->connect($dsn, $user, $pass, | |||||||||||||||||||||||||||
| 149 | { RaiseError => 1 }); | ||||||||||||||||||||||||||||
| 150 | die "Failed to connect to $dsn: $DBI::errstr\n" | ||||||||||||||||||||||||||||
| 151 | 1 | ▶ | unless $self->{_table_dbi}->{dbh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | $self->{_table_dbi}->{table} = $table; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Prepare the query that we will be using all the time, | ||||||||||||||||||||||||||||
| 156 | # for speed | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | my $q_sql = qq{ | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | SELECT * FROM $table | ||||||||||||||||||||||||||||
| 161 | WHERE | ||||||||||||||||||||||||||||
| 162 | ? >= CIDR_Start | ||||||||||||||||||||||||||||
| 163 | AND ? <= CIDR_End | ||||||||||||||||||||||||||||
| 164 | AND ? >= CIDR_Start | ||||||||||||||||||||||||||||
| 165 | AND ? <= CIDR_End | ||||||||||||||||||||||||||||
| 166 | AND ? >= TIME_Start | ||||||||||||||||||||||||||||
| 167 | AND (? <= TIME_End | ||||||||||||||||||||||||||||
| 168 | OR TIME_End IS NULL) | ||||||||||||||||||||||||||||
| 169 | ORDER BY TIME_Start DESC; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | ▶ | if ($debug) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||||||||
| 175 | 0 | warn "TableDBI: Query is:\n$q_sql\n\n"; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | $self->{_table_dbi}->{q_sth} = $self->{_table_dbi}->{dbh}->prepare($q_sql); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | undef; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =pod | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =back | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | The following functions are implemented. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =over | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item C<process($report)> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Takes a C<Mail::Abuse::Report> object as an argument and, for each | ||||||||||||||||||||||||||||
| 194 | C<Mail::Abuse::Incident> collected, perform a lookup in the given | ||||||||||||||||||||||||||||
| 195 | table, attempting to match it by IP address and timestamp. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | If a match is found, all the columns found in the DBI query, are | ||||||||||||||||||||||||||||
| 198 | added to the incident. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub process | ||||||||||||||||||||||||||||
| 203 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=12 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 205 | 1 | my $rep = shift; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 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"
| |||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||
| 209 | 0 | warn "Invalid or no config"; | |||||||||||||||||||||||||||
| 210 | 0 | return; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 1 | my $dsn = $rep->config->{&DSN}; | |||||||||||||||||||||||||||
| 214 | 1 | my $debug = $rep->config->{&DEBUG}; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | $self->_init_dbi($rep); | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # For each incident... | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 1 | for my $i (@{$rep->incidents}) | |||||||||||||||||||||||||||
| 221 | { | ||||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||
| 223 | # Execute the query with the required data | ||||||||||||||||||||||||||||
| 224 | 3 | $self->{_table_dbi}->{q_sth}->execute | |||||||||||||||||||||||||||
| 225 | 3 | ▶ | (scalar $i->ip->network->numeric, | ||||||||||||||||||||||||||
|
Condition50%2/4
$i->time || 0
$i->time || 0
MC/DC0%0/1
$i->time || 0
$i->time
MC/DC0%0/1
$i->time || 0
$i->time
Truth table50%1/2
A
A= $i->time
Truth table50%1/2
A
A= $i->time
| |||||||||||||||||||||||||||||
| 226 | scalar $i->ip->network->numeric, | ||||||||||||||||||||||||||||
| 227 | scalar $i->ip->broadcast->numeric, | ||||||||||||||||||||||||||||
| 228 | scalar $i->ip->broadcast->numeric, | ||||||||||||||||||||||||||||
| 229 | $i->time || 0, | ||||||||||||||||||||||||||||
| 230 | $i->time || 0); | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 3 | my $result = $self->{_table_dbi}->{q_sth}->fetchrow_hashref; | |||||||||||||||||||||||||||
| 233 | 3 | $self->{_table_dbi}->{q_sth}->finish; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 3 | ▶ | unless ($result) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||
| 237 | 2 | ▶ | warn "TableDBI: Incident (", $i->ip, ", ", $i->time, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | ") did not match\n" | ||||||||||||||||||||||||||||
| 239 | if $debug; | ||||||||||||||||||||||||||||
| 240 | 2 | next; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 1 | ▶ | if ($debug) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | { | ||||||||||||||||||||||||||||
| 245 | 0 | warn "TableDBI: Incident (", $i->ip, ", ", $i->time, | |||||||||||||||||||||||||||
| 246 | ") matched with results:\n"; | ||||||||||||||||||||||||||||
| 247 | 0 | ▶ | while (my ($k, $v) = each %$result) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | { | ||||||||||||||||||||||||||||
| 249 | 0 | print " $k => $v\n"; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 1 | ▶ | $i->tabledbi({}) unless $i->tabledbi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 1 | $i->tabledbi->{$_} = $result->{$_} for keys %$result; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | 1 | return 1; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Perform the query | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # Update the incident if matched | ||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | __END__ | ||||||||||||||||||||||||||||