MtPolicyd/Plugin/CtIpRep.pm

filter: Statementstmt 36.9% 31 / 84 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 5 / 10 Podpod 100% 1 / 1 totaltotal 25.8% 37 / 143 CC 19 SCAR 47.0
CC
19
Cov
25%
run
210.0
_scan_ip
20.0
_build_action
12.0
CRAP 109.5
↑ Summary MtPolicyd/Plugin/RBLAction.pm »
1 package Mail::MtPolicyd::Plugin::CtIpRep;
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 the Commtouch IP reputation service (ctipd)
8  
9  
10 2 extends 'Mail::MtPolicyd::Plugin';
11 2 with 'Mail::MtPolicyd::Plugin::Role::Scoring';
12 2 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
13 'uc_attributes' => [ 'enabled' ],
14 };
15  
16 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 2 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
23  
24 has '_agent' => (
25 is => 'ro', isa => 'LWP::UserAgent', lazy => 1,
26 0 ▶ default => sub { LWP::UserAgent->new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 2 );
28 2 has 'url' => (
29 is => 'ro', isa => 'Str', default => 'http://localhost:8080/ctipd/iprep',
30 );
31 2 has 'key' => ( is => 'ro', isa => 'Maybe[Str]' );
32  
33 2 has 'reject_message' => (
34 is => 'rw', isa => 'Str', default => '550 delivery from %IP% is rejected. Check at http://www.commtouch.com/Site/Resources/Check_IP_Reputation.asp. Reference code: %REFID%',
35 );
36 2 has 'defer_message' => (
37 is => 'rw', isa => 'Str', default => '450 delivery from %IP% is deferred,repeatedly. Send again or check at http://www.commtouch.com/Site/Resources/Check_IP_Reputation.asp. Reference code: %REFID%',
38 );
39  
40 2 has 'permfail_score' => ( is => 'rw', isa => 'Maybe[Num]' );
41 2 has 'permfail_mode' => ( is => 'rw', isa => 'Str', default => 'reject' );
42  
43 2 has 'tempfail_score' => ( is => 'rw', isa => 'Maybe[Num]' );
44 2 has 'tempfail_mode' => ( is => 'rw', isa => 'Str', default => 'defer' );
45  
46 sub _scan_ip {
47 0 ▶ my ( $self, $ip ) = @_;
Subroutine _scan_ip: not called CC=4 SCAR=30.0
48 0 my $request = "x-ctch-request-type: classifyip\r\n".
49 "x-ctch-pver: 1.0\r\n";
50 0 ▶ if( defined $self->key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->key) 0 0 0
51 0 $request .= 'x-ctch-key: '.$self->key."\r\n";
52 }
53 0 $request .= "\r\n";
54 0 $request .= 'x-ctch-ip: '.$ip."\r\n";
55  
56 0 my $response = $self->_agent->request(POST $self->url, Content => $request );
57 0 ▶ if( $response->code ne 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code ne 200) 0 0 0
58 0 die('error while accessing Commtouch ctipd: '.$response->status_line);
59 }
60 0 my $content = $response->content;
61 0 my ( $action ) = $content =~ m/^x-ctch-dm-action:(.*)\r$/m;
62 0 my ( $refid ) = $content =~ m/^x-ctch-refid:(.*)\r$/m;
63 0 ▶ if( ! defined $action ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 0 0 0
64 0 die('could not find action in response: '.$content);
65 }
66  
67 0 return( $action, $refid );
68 }
69  
70 sub run {
71 0 ▶ my ( $self, $r ) = @_;
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
72 0 my $ip = $r->attr('client_address');
73 0 my $session = $r->session;
74 0 my $mode;
75  
76 0 ▶ if( ! defined $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 0 0 0
77 0 die('no client_address in request!');
78 }
79  
80 0 my $enabled = $self->get_uc($session, 'enabled');
81 0 ▶ if( $enabled eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enabled eq "off") 0 0 0
82 0 return;
83 }
84
85 my ( $result, $refid ) = $r->do_cached( $self->name.'-result',
86 0 ▶ sub{ $self->_scan_ip( $ip ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
87  
88 0 ▶ if( $result eq 'accept') {
Branch: 3 branches
BranchTrueFalseTotal
if ($result eq 'accept') { } 0 0 0
elsif ($result eq 'permfail') { } 0 0 0
elsif ($result eq 'tempfail') { } 0 0 0
89 0 $self->log( $r, 'CtIpRep: sender IP is ok' );
90 0 return; # do nothing
91 } elsif( $result eq 'permfail' ) {
92 0 $mode = $self->permfail_mode;
93 0 ▶ if( $self->permfail_score
Branch: 1 branch
BranchTrueFalseTotal
if ($self->permfail_score and not $r->is_already_done($self->name . '-score')) 0 0 0
Condition0%0/3
$self->permfail_score and not $r->is_already_done($self->name . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
$self->permfail_score and not $r->is_already_done($self->name . '-score')
$self->permfail_score not $r->is_already_done($self->name . '-score')
Truth table0%0/3
A and not(B)
A= $self->permfail_scoreB= not $r->is_already_done($self->name . '-score')
ABresult
0X0
100
111
94 && ! $r->is_already_done($self->name.'-score') ) {
95 0 $self->add_score($r, $self->name => $self->permfail_score);
96 }
97 } elsif ($result eq 'tempfail' ) {
98 0 $mode = $self->tempfail_mode;
99 0 ▶ if( $self->tempfail_score
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tempfail_score and not $r->is_already_done($self->name . '-score')) 0 0 0
Condition0%0/3
$self->tempfail_score and not $r->is_already_done($self->name . '-score')
!ll&&!rl&&r
000
MC/DC0%0/2
$self->tempfail_score and not $r->is_already_done($self->name . '-score')
$self->tempfail_score not $r->is_already_done($self->name . '-score')
Truth table0%0/3
A and not(B)
A= $self->tempfail_scoreB= not $r->is_already_done($self->name . '-score')
ABresult
0X0
100
111
100 && ! $r->is_already_done($self->name.'-score') ) {
101 0 $self->add_score($r, $self->name => $self->tempfail_score);
102 }
103 } else {
104 0 die('unknown ctiprep action: '.$result);
105 }
106 0 $self->log($r, 'CtIpRep: result='.$result.', mode='.$mode);
107  
108 0 ▶ if ( $mode eq 'reject' || $mode eq 'defer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "reject" or $mode eq "defer") 0 0 0
Condition0%0/3
$mode eq "reject" or $mode eq "defer"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "reject" or $mode eq "defer"
$mode eq "reject" $mode eq "defer"
Truth table0%0/3
A or B
A= $mode eq "reject"B= $mode eq "defer"
ABresult
1X1
011
000
109 0 return Mail::MtPolicyd::Plugin::Result->new(
110 action => $self->_build_action($mode, $ip, $refid),
111 abort => 1,
112 );
113 }
114 0 ▶ if( $mode eq 'passive' || $mode eq 'accept' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "passive" or $mode eq "accept") 0 0 0
Condition0%0/3
$mode eq "passive" or $mode eq "accept"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "passive" or $mode eq "accept"
$mode eq "passive" $mode eq "accept"
Truth table0%0/3
A or B
A= $mode eq "passive"B= $mode eq "accept"
ABresult
1X1
011
000
115 0 return; # do nothing
116 }
117  
118 0 $self->log($r, 'unknown mode for CtIpRep: '.$mode);
119 0 return;
120 }
121  
122 sub _build_action {
123 0 ▶ my ( $self, $action, $ip, $refid ) = @_;
Subroutine _build_action: not called CC=3 SCAR=24.8
124 0 my $message;
125 0 ▶ if( $action eq 'reject' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'reject') { } 0 0 0
elsif ($action eq 'defer') { } 0 0 0
126 0 $message = $self->reject_message;
127 } elsif ( $action eq 'defer' ) {
128 0 $message = $self->defer_message;
129 } else {
130 0 die('unknown action: '.$action);
131 }
132 0 $message =~ s/%IP%/$ip/;
133 0 $message =~ s/%REFID%/$refid/;
134  
135 0 return($action.' '.$message);
136 }
137  
138 2 __PACKAGE__->meta->make_immutable;
139  
140 2 1;
141  
142  
143 __END__
144 =pod
145  
146 =head1 NAME
147  
148 Mail::MtPolicyd::Plugin::CtIpRep - mtpolicyd plugin for the Commtouch IP reputation service (ctipd)
149  
150 =head1 VERSION
151  
152 version 1.12
153  
154 =head1 DESCRIPTION
155  
156 This plugin will query the Commtouch IP Reputation service (ctipd).
157  
158 The used protocol is HTTP.
159  
160 The services will return a status permfail or tempfail.
161  
162 =head1 PARAMETERS
163  
164 =over
165  
166 =item (uc_)enabled (default: on)
167  
168 Enable/disable the plugin.
169  
170 =item url (default: http://localhost:8080/ctipd/iprep)
171  
172 The URL to access the ctipd daemon.
173  
174 =item key (default: empty)
175  
176 If an authentication key is required by the ctipd.
177  
178 =item reject_message (default: 550 delivery from %IP% is rejected. Check at http://www.commtouch.com/Site/Resources/Check_IP_Reputation.asp. Reference code: %REFID%)
179  
180 This parameter could be used to specifiy a custom reject message if message is rejected.
181  
182 =item defer_message (default: 450 delivery from %IP% is deferred,repeatedly. Send again or check at http://www.commtouch.com/Site/Resources/Check_IP_Reputation.asp. Reference code: %REFID%)
183  
184 This parameter could be used to specifiy a custom message is a message is to be defered.
185  
186 =item permfail_mode, tempfail_mode (default: reject, defer)
187  
188 Action to take when the service return permfail/tempfail status:
189  
190 =over
191  
192 =item reject
193  
194 =item defer
195  
196 =item passive
197  
198 =back
199  
200 =item permfail_score, tempfail_score (default: empty)
201  
202 Apply the specified score.
203  
204 =back
205  
206 =head1 AUTHOR
207  
208 Markus Benning <ich@markusbenning.de>
209  
210 =head1 COPYRIGHT AND LICENSE
211  
212 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
213  
214 This is free software, licensed under:
215  
216 The GNU General Public License, Version 2, June 1991
217  
218 =cut
219  
↑ Summary MtPolicyd/Plugin/RBLAction.pm »