Authentication/Handler/RBLDNS.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 103 CC 9 SCAR 31.6
CC
9
Cov
0%
connect_callback
56.0
setup_callback
12.0
default_config
2.0
CRAP 23.5
1 package Mail::Milter::Authentication::Handler::RBLDNS;
2 use 5.20.0;
3 use strict;
4 use warnings;
5 use Mail::Milter::Authentication::Pragmas;
6 # ABSTRACT: Handler class for RBLDNS checks
7 our $VERSION = '4.20261116'; # VERSION
8 use base 'Mail::Milter::Authentication::Handler';
9  
10 sub default_config {
11 return {
12 'rbls' => {
13 'list1' => {
14 'base_url' => 'list1.example.com',
15 'default_state' => 'clean',
16 'add_authresults' => 1,
17 'add_header' => 'X-List1',
18 'states' => {
19 '127.0.0.2' => 'foo',
20 '127.0.0.3' => 'bar',
21 '*' => 'baz',
22 }
23 },
24 'list2' => {
25 'base_url' => 'list2.example.com',
26 'default_state' => 'clean',
27 'add_header' => 'X-List2',
28 'states' => {
29 '127.0.0.2' => 'foo',
30 '127.0.0.3' => 'bar',
31 '*' => 'baz',
32 }
33 },
34 }
35 }
36 }
37  
38 #sub grafana_rows {
39 # my ( $self ) = @_;
40 # my @rows;
41 # push @rows, $self->get_json( 'RBLDNS_metrics' );
42 # return \@rows;
43 #}
44  
45 sub register_metrics {
46 return {
47 'rbldns_total' => 'The number of emails processed for RBL DNS',
48 };
49 }
50  
51 sub setup_callback {
52 my ($self) = @_;
53 my $config = $self->handler_config();
54 $config = $config->{rbls} if exists $config->{rbls};
55 foreach my $rbl ( sort keys $config->%* ) {
56 my $sanitize_header = $config->{$rbl}->{sanitize_header} // 'yes';
57 $self->add_header_to_sanitize_list(lc $config->{$rbl}->{add_header}, $sanitize_header eq 'silent') if $config->{$rbl}->{add_header} && $sanitize_header ne 'no';
58 }
59 return;
60 }
61  
62 sub connect_callback {
63 my ( $self, $hostname, $ip ) = @_;
64 my $config = $self->handler_config();
65 $config = $config->{rbls} if exists $config->{rbls};
66  
67 my @states;
68  
69 foreach my $rbl ( sort keys $config->%* ) {
70 my $rbl_config = $config->{$rbl};
71  
72 my $state = 'unknown';
73 my $rbl_check = $self->rbl_check_ip( $ip, $rbl_config->{base_url} );
74  
75 if ( ! $rbl_check ) {
76 $state = $rbl_config->{default_state};
77 }
78 elsif ( exists( $rbl_config->{states}->{$rbl_check} ) ) {
79 $state = $rbl_config->{states}->{$rbl_check};
80 }
81 elsif ( exists( $rbl_config->{states}->{'*'} ) ) {
82 $state = $rbl_config->{states}->{'*'};
83 }
84  
85 $self->dbgout( 'RBLDNS', "$rbl:$rbl_check:$state", LOG_DEBUG );
86 if ( $rbl_config->{add_authresults} ) {
87 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key($rbl)->safe_set_value( $state );
88 $self->add_c_auth_header( $header );
89 }
90 if ( $rbl_config->{add_header} ) {
91 push @states, [ $rbl_config->{add_header}, $state ];
92 }
93 $self->metric_count( 'rbldns_total', { rbl => $rbl, result => $state } );
94 }
95 $self->{'states'} = \@states;
96  
97 return;
98 }
99  
100 sub eoh_callback {
101 my ( $self ) = @_;
102 foreach my $add_state ( $self->{states}->@* ) {
103 $self->prepend_header( $add_state->@* );
104 }
105 return;
106 }
107  
108 sub close_callback {
109 my ( $self ) = @_;
110 delete $self->{'states'};
111 return;
112 }
113  
114 1;
115  
116 __END__