Authentication/Handler/Blocker.pm

filter: Statementstmt 90.7% 69 / 76 Branchbran 83.3% 15 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.1% 16 / 17 Podpod 12.5% 1 / 8 totaltotal 85.4% 106 / 124 CC 12 SCAR 13.7
CC
12
Cov
90%
_test_blocker
9.0
_load_blocker_config_file
3.2
_load_blocker_config
2.0
CRAP 3.9
« Authentication/Handler/TrustedIP.pm ↑ Summary Authentication/HTDocs.pm »
1 package Mail::Milter::Authentication::Handler::Blocker;
2 2 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Block mail based on simple rules
7 2 our $VERSION = '4.20261116'; # VERSION
8 2 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use TOML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
12 return {
13 4 'blocker_total' => 'The number of emails blocked by blocker',
14 };
15 }
16  
17 17 ▶sub _load_blocker_config_file {
Subroutine _load_blocker_config_file: called CC=3 SCAR=11.8
18 17 my ( $self, $filename ) = @_;
19 17 my $blocker_config = {};
20 17 ▶ if ( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 17 0 17
21 17 open ( my $inf, '<', $filename );
22 17 my $body = do { local $/; <$inf> };
23 17 close $inf;
24 17 my ( $data, $error ) = from_toml( $body );
25 17 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 17 17
26 0 $self->log_error( 'Invalid blocker toml file - ' . $error );
27 }
28 else {
29 17 $blocker_config = $data;
30 }
31 }
32 else {
33 0 open ( my $outf, '>', $filename ); ## no critic
34 0 print $outf qq(
35 # Authentication Milter Blocker quick config
36 #
37 # id for metrics and must be unique
38 # callbacks are connect,helo,envfrom,envrcpt,header
39 # value is applied as a regex
40 # percent is a percentage of matches to apply the block to
41 # with is the full SMTP reject string to send, 4xx or 5xx and MUST have an extended code 5.x.x or 4.x.x
42 # until (optional) is a unixtime after which the block will expire
43 #
44 # Example
45 #
46 # [flood]
47 # callback = "connect"
48 # value = "192\.168\.0\.1"
49 # with = "451 4.7.28 flood policy violation (HOTtest)"
50 # percent = 100
51 # until = 1573514783
52 #
53 # [rule2]
54 # callback = "connect"
55 # ...
56 );
57 0 close $outf;
58 }
59 17 return $blocker_config;
60 }
61  
62 57 ▶sub _load_blocker_config {
Subroutine _load_blocker_config: called CC=2 SCAR=6.9
63 57 my ( $self ) = @_;
64 57 my $config = $self->handler_config();
65 57 ▶ return $self->{'blocker_config'} if exists $self->{'blocker_config'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"blocker_config"} 42 15 57
66  
67 my %blocker_config = map {
68 17 %{ $self->_load_blocker_config_file( $_ ) }, ## no critic
69 15 } ( @{$config->{ 'blocker_configs' } } );
70  
71 15 $self->{'blocker_config'} = \%blocker_config;
72 15 return \%blocker_config;
73 }
74  
75 57 ▶sub _test_blocker {
Subroutine _test_blocker: called CC=9 SCAR=22.0
76 57 my ( $self, $callback, $value ) = @_;
77  
78 57 my $blocker_config = $self->_load_blocker_config();
79 57 foreach my $key ( sort keys %$blocker_config ) {
80 289 my $item = $blocker_config->{$key};
81 289 ▶ next if $item->{'callback'} ne $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"callback"} ne $callback 220 69 289
82 69 ▶ next if $item->{'until'} && $item->{'until'} < time;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"until"} and $item->{"until"} < time 4 65 69
Condition100%3/3
$item->{"until"} and $item->{"until"} < time
!ll&&!rl&&r
6144
MC/DC100%2/2
$item->{"until"} and $item->{"until"} < time
$item->{"until"} $item->{"until"} < time
Truth table100%3/3
A and B
A= $item->{"until"}B= $item->{"until"} < time
ABresult
0X0
100
111
83 65 my $value_regex = $item->{'value'};
84 65 ▶ if ( $value =~ /$value_regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$value_regex/u) 12 53 65
85 12 ▶ if ( rand(100) > $item->{'percent'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (rand 100 > $item->{'percent'}) { } 2 10 12
elsif ($item->{'with'} =~ /^5/u) { } 1 9 10
elsif ($item->{'with'} =~ /^4/u) { } 9 0 9
86 2 $self->dbgout( 'Blocker', 'sampled_out ' . $key, LOG_INFO );
87 2 $self->metric_count( 'blocker_total', { 'result' => 'sampled_out', 'id' => $key } );
88 }
89 elsif ( $item->{'with'} =~ /^5/ ) {
90 1 $self->dbgout( 'Blocker', 'reject ' . $key, LOG_INFO );
91 1 $self->metric_count( 'blocker_total', { 'result' => 'reject', 'id' => $key } );
92 1 $self->reject_mail( $item->{'with'} );
93 }
94 elsif ( $item->{'with'} =~ /^4/ ) {
95 9 $self->dbgout( 'Blocker', 'defer ' . $key, LOG_INFO );
96 9 $self->metric_count( 'blocker_total', { 'result' => 'defer', 'id' => $key } );
97 9 $self->defer_mail( $item->{'with'} );
98 }
99 else {
100 0 $self->log_error( 'Invalid blocker entry with ' . $item->{'with'} );
101 }
102 }
103 }
104 }
105  
106 1 ▶sub default_config {
Subroutine default_config: called CC=1 SCAR=0.0
Pod undocumented
107 return {
108 1 'blocker_configs' => [ '/tmpfs/authmilter-blocker.toml' ],
109 };
110 }
111  
112 15 ▶sub connect_callback {
Subroutine connect_callback: called CC=1 SCAR=0.0
Pod undocumented
113 15 my ( $self, $hostname, $ip ) = @_;
114 15 $self->_test_blocker( 'connect', $ip->ip );
115 }
116  
117 9 ▶sub helo_callback {
Subroutine helo_callback: called CC=1 SCAR=0.0
Pod undocumented
118 9 my ( $self, $helo_host ) = @_;
119 9 $self->_test_blocker( 'helo', $helo_host );
120 }
121  
122 8 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=1 SCAR=0.0
Pod undocumented
123 8 my ( $self, $env_from ) = @_;
124 8 $self->_test_blocker( 'envfrom', $env_from );
125 }
126  
127 7 ▶sub envrcpt_callback {
Subroutine envrcpt_callback: called CC=1 SCAR=0.0
Pod undocumented
128 7 my ( $self, $env_to ) = @_;
129 7 $self->_test_blocker( 'envrcpt', $env_to );
130 }
131  
132 18 ▶sub header_callback {
Subroutine header_callback: called CC=1 SCAR=0.0
Pod undocumented
133 18 my ( $self, $header, $value ) = @_;
134 18 $self->_test_blocker( 'header', "$header: $value" );
135 }
136  
137 0 ▶sub close_callback {
Subroutine close_callback: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self) = @_;
139 0 delete $self->{'blocker_config'};
140 }
141  
142 2 1;
143  
144 __END__
« Authentication/Handler/TrustedIP.pm ↑ Summary Authentication/HTDocs.pm »