MtPolicyd/Plugin/SqlList.pm

filter: Statementstmt 42.5% 20 / 47 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 3 / 6 Podpod 100% 1 / 1 totaltotal 33.8% 24 / 71 CC 8 SCAR 38.0
CC
8
Cov
32%
run
72.0
_query_db
2.0
__ANON__
2.0
CRAP 44.8
« MtPolicyd/Plugin/RBLAction.pm ↑ Summary MtPolicyd.pm »
1 package Mail::MtPolicyd::Plugin::SqlList;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.12'; # VERSION
7 # ABSTRACT: mtpolicyd plugin for accessing a SQL white/black/access list
8  
9 2 extends 'Mail::MtPolicyd::Plugin';
10 2 with 'Mail::MtPolicyd::Plugin::Role::Scoring';
11 2 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
12 'uc_attributes' => [ 'enabled' ],
13 };
14  
15 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
19  
20 2 has 'sql_query' => (
21 is => 'rw', isa => 'Str',
22 default => 'SELECT client_ip FROM whitelist WHERE client_ip=INET_ATON(?)',
23 );
24  
25 2 has 'score' => ( is => 'rw', isa => 'Maybe[Num]' );
26 2 has 'match_action' => ( is => 'rw', isa => 'Maybe[Str]' );
27 2 has 'not_match_action' => ( is => 'rw', isa => 'Maybe[Str]' );
28  
29 sub _query_db {
30 0 ▶ my ( $self, $r, $ip ) = @_;
Subroutine _query_db: not called CC=1 SCAR=6.9
31  
32 0 my $dbh = $r->server->get_dbh;
33 0 my $sth = $dbh->prepare( $self->sql_query );
34 0 $sth->execute( $ip );
35 0 my ( $value ) = $sth->fetchrow_array();
36 0 return $value;
37 }
38  
39 sub run {
40 0 ▶ my ( $self, $r ) = @_;
Subroutine run: not called CC=8 SCAR=42.8
Pod documented
41 0 my $ip = $r->attr('client_address');
42 0 my $session = $r->session;
43 0 my $config;
44  
45 0 ▶ if( $self->get_uc( $session, 'enabled') eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_uc($session, "enabled") eq "off") 0 0 0
46 0 return;
47 }
48  
49 0 ▶ if( ! defined $ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 0 0 0
50 0 $self->log($r, 'no attribute \'client_address\' in request');
51 0 return;
52 }
53  
54 my $value = $r->do_cached( $self->name.'-result',
55 0 ▶ sub { $self->_query_db($r, $ip) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 ▶ if( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 0 0
57 0 $self->log($r, 'client_address '.$ip.' matched SqlList '.$self->name);
58 0 ▶ if( defined $self->score
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->score and not $r->is_already_done($self->name . '-score')) 0 0 0
Condition0%0/3
defined $self->score and not $r->is_already_done($self->name . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->score and not $r->is_already_done($self->name . '-score')
defined $self->score not $r->is_already_done($self->name . '-score')
Truth table0%0/3
A and not(B)
A= defined $self->scoreB= not $r->is_already_done($self->name . '-score')
ABresult
0X0
100
111
59 && ! $r->is_already_done($self->name.'-score') ) {
60 0 $self->add_score($r, $self->name , $self->score);
61 }
62 0 ▶ if( defined $self->match_action ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->match_action) 0 0 0
63 0 return Mail::MtPolicyd::Plugin::Result->new(
64 action => $self->match_action,
65 abort => 1,
66 );
67 }
68 } else {
69 0 $self->log($r, 'client_address '.$ip.' did not match SqlList '.$self->name);
70 0 ▶ if( defined $self->not_match_action ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->not_match_action) 0 0 0
71 0 return Mail::MtPolicyd::Plugin::Result->new(
72 action => $self->not_match_action,
73 abort => 1,
74 );
75 }
76 }
77  
78 0 return;
79 }
80  
81  
82 2 __PACKAGE__->meta->make_immutable;
83  
84 2 1;
85  
86  
87 __END__
88 =pod
89  
90 =head1 NAME
91  
92 Mail::MtPolicyd::Plugin::SqlList - mtpolicyd plugin for accessing a SQL white/black/access list
93  
94 =head1 VERSION
95  
96 version 1.12
97  
98 =head1 SYNOPSIS
99  
100 <Plugin whitelist>
101 module="SqlList"
102 sql_query="SELECT client_ip FROM whitelist WHERE client_ip=?"
103 match_action=dunno
104 </Plugin>
105  
106 <Plugin blacklist>
107 module="SqlList"
108 sql_query="SELECT client_ip FROM blacklist WHERE client_ip=?"
109 match_action="reject you are blacklisted!"
110 </Plugin>
111  
112 =head1 DESCRIPTION
113  
114 Plugin checks the client_address against a SQL table.
115  
116 Depending on wether a supplied SQL query matched actions can be taken.
117  
118 =head2 PARAMETERS
119  
120 The module takes the following parameters:
121  
122 =over
123  
124 =item (uc_)enabled (default: "on")
125  
126 Could be set to 'off' to deactivate check. Could be used to activate/deactivate check per user.
127  
128 =item sql_query (default: "SELECT client_ip FROM whitelist WHERE client_ip=INET_ATON(?)")
129  
130 Prepared SQL statement to use for checking an IP address.
131  
132 ? will be replaced by the IP address.
133  
134 The module will match if the statement returns one or more rows.
135  
136 =back
137  
138 By default the plugin will do nothing. One of the following actions should be specified:
139  
140 =over
141  
142 =item match_action (default: empty)
143  
144 If given this action will be returned to the MTA if the SQL query matched.
145  
146 =item not_match_action (default: empty)
147  
148 If given this action will be returned to the MTA if the SQL query DID NOT matched.
149  
150 =item score (default: empty)
151  
152 If given this score will be applied to the session.
153  
154 =back
155  
156 =head1 EXAMPLE WITH A MYSQL TABLE
157  
158 You may use the following table for storing ipv4 addresses in MySQL:
159  
160 CREATE TABLE `whitelist` (
161 `id` int(11) NOT NULL AUTO_INCREMENT,
162 `client_ip` INT UNSIGNED NOT NULL,
163 PRIMARY KEY (`id`),
164 UNIQUE KEY `client_ip` (`client_ip`)
165 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
166  
167 INSERT INTO whitelist VALUES(NULL, INET_ATON('127.0.0.1'));
168  
169 And use it as a whitelist in mtpolicyd:
170  
171 <VirtualHost 12345>
172 name="reputation"
173 <Plugin whitelist>
174 module="SqlList"
175 sql_query="SELECT client_ip FROM whitelist WHERE client_ip=INET_ATON(?)"
176 match_action="dunno"
177 </Plugin>
178 <Plugin trigger-greylisting>
179 ...
180 </VirtualHost>
181  
182 =head1 AUTHOR
183  
184 Markus Benning <ich@markusbenning.de>
185  
186 =head1 COPYRIGHT AND LICENSE
187  
188 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
189  
190 This is free software, licensed under:
191  
192 The GNU General Public License, Version 2, June 1991
193  
194 =cut
195  
« MtPolicyd/Plugin/RBLAction.pm ↑ Summary MtPolicyd.pm »