File Coverage

blib/lib/Mail/Milter/Authentication/Handler/TrustedIP.pm
Criterion Covered Total %
statement 45 46 97.8
branch 7 8 87.5
condition 8 11 72.7
subroutine 12 12 100.0
pod 2 6 33.3
total 74 83 89.1


line stmt bran cond sub pod time code
1             package Mail::Milter::Authentication::Handler::TrustedIP;
2 30     30   17035 use 5.20.0;
  30         140  
3 30     30   235 use strict;
  30         107  
  30         737  
4 30     30   163 use warnings;
  30         181  
  30         932  
5 30     30   276 use Mail::Milter::Authentication::Pragmas;
  30         119  
  30         299  
6             # ABSTRACT: Handler class for Trusted IP addresses
7             our $VERSION = '3.20230911'; # VERSION
8 30     30   7189 use base 'Mail::Milter::Authentication::Handler';
  30         138  
  30         3084  
9 30     30   278 use Net::IP;
  30         83  
  30         22270  
10              
11             sub default_config {
12             return {
13 1     1 0 1744 'trusted_ip_list' => [],
14             };
15             }
16              
17             sub grafana_rows {
18 1     1 0 4752 my ( $self ) = @_;
19 1         3 my @rows;
20 1         8 push @rows, $self->get_json( 'TrustedIP_metrics' );
21 1         4 return \@rows;
22             }
23              
24             sub is_trusted_ip_address {
25 74     74 1 294 my ( $self, $ip_obj ) = @_;
26 74         707 my $config = $self->handler_config();
27 74 100       443 return 0 if not exists( $config->{'trusted_ip_list'} );
28 66         171 my $trusted = 0;
29 66         167 foreach my $trusted_ip ( @{ $config->{'trusted_ip_list'} } ) {
  66         359  
30 84         423 my $trusted_obj = Net::IP->new($trusted_ip);
31 84 50       81409 if ( !$trusted_obj ) {
32 0         0 $self->log_error( 'TrustedIP: Could not parse Trusted IP '.$trusted_ip );
33             }
34             else {
35 84   100     717 my $is_overlap = $ip_obj->overlaps($trusted_obj) || 0;
36 84 100 66     13920 if (
      66        
      66        
37             $is_overlap == $IP_A_IN_B_OVERLAP
38             || $is_overlap == $IP_B_IN_A_OVERLAP # Should never happen
39             || $is_overlap == $IP_PARTIAL_OVERLAP # Should never happen
40             || $is_overlap == $IP_IDENTICAL
41             )
42             {
43 6         42 $trusted = 1;
44             }
45             }
46             }
47 66         446 return $trusted;
48             }
49              
50             sub register_metrics {
51             return {
52 29     29 1 261 'trustedip_connect_total' => 'The number of connections from a trusted IP',
53             };
54             }
55              
56             sub connect_callback {
57 74     74 0 333 my ( $self, $hostname, $ip ) = @_;
58 74         514 $self->{'is_trusted_ip_address'} = 0;
59 74 100       936 if ( $self->is_trusted_ip_address($ip) ) {
60 6         61 $self->dbgout( 'TrustedIP', 'pass', LOG_DEBUG );
61 6         82 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-trusted-ip' )->safe_set_value( 'pass' );
62 6         615 $self->add_c_auth_header( $header );
63 6         15 $self->{'is_trusted_ip_address'} = 1;
64 6         34 $self->metric_count( 'trustedip_connect_total' );
65             }
66             }
67              
68             sub close_callback {
69 105     105 0 358 my ( $self ) = @_;
70 105         417 delete $self->{'is_trusted_ip_address'};
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Mail::Milter::Authentication::Handler::TrustedIP - Handler class for Trusted IP addresses
84              
85             =head1 VERSION
86              
87             version 3.20230911
88              
89             =head1 DESCRIPTION
90              
91             Detect a trusted IP address and act accordingly.
92              
93             =head1 CONFIGURATION
94              
95             "TrustedIP" : { | Config the the TruetedIP Module
96             | Check for TrustedIP Addresses
97             "trusted_ip_list" : [ | List of IP Addresses considered to be trusted
98             "100.200.100.2", | CIDR Ranges are valid syntax
99             "2001:44c2:3881:aa00::/56",
100             "2001:44b8:3021:123:dead:beef:abcd:1234"
101             ],
102             },
103              
104             =head1 AUTHOR
105              
106             Marc Bradshaw <marc@marcbradshaw.net>
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2020 by Marc Bradshaw.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut