| 1 | package Mail::POP3::Security::Connection; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | Params: Config hash-ref with keys: | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =item trusted_networks | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Filename. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =item allow_non_fqdn | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | Boolean. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =item hosts_allow_deny | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Filename. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | my ($class, $config) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | $self->{CONFIG} = $config; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head2 check | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | Params: C<$client_ip>, C<$fqdn>. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # return ($was_ok, \@log_entry) | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=24 SCAR=59.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | my ($self, $client_ip, $fqdn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | # Get the client's IP and FQDN. We don't have tcpwrapper | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # protection in daemon mode and therefore need to do a reverse | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # lookup. $self->{CONFIG}->{allow_non_fqdn} can be set to 1 to | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # effectively disable reverse lookups. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # Make an exception for trusted networks | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | my $secure = 0; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | if (-f $self->{CONFIG}->{trusted_networks}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | local *SECURENETS; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | open SECURENETS, $self->{CONFIG}->{trusted_networks}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | while (<SECURENETS>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | next if /^\#/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | next if /^\s+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | s/\s+|\*//g; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ($client_ip =~ /^$_/ || $fqdn =~ /^$_$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$client_ip =~ /^$_/ or $fqdn =~ /^$_$/
MC/DC0%0/2
$client_ip =~ /^$_/ or $fqdn =~ /^$_$/
$client_ip =~ /^$_/
$fqdn =~ /^$_$/
Truth table0%0/3
A or B
A= $client_ip =~ /^$_/B= $fqdn =~ /^$_$/
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $secure = 1; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | close SECURENETS; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my @addr = gethostbyname($fqdn); | |||||||||||||||||||||||||||||||||||||||||
| 67 | # See if any of the domain names returned matches the IP | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # and return false if none does. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | my $lookup_ok = grep { $client_ip eq inet_ntoa($_) } @addr[4..$#addr]; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | if (!$lookup_ok and !$self->{CONFIG}->{allow_non_fqdn} and !$secure) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$lookup_ok or $self->{"CONFIG"}{"allow_non_fqdn"} or $secure
$lookup_ok or $self->{"CONFIG"}{"allow_non_fqdn"}
MC/DC0%0/3
$lookup_ok or $self->{"CONFIG"}{"allow_non_fqdn"} or $secure
$lookup_ok
$self->{"CONFIG"}{"allow_non_fqdn"}
$secure
Truth table25%1/4
A or B or C
A= $lookup_okB= $self->{"CONFIG"}{"allow_non_fqdn"}C= $secure
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return (0, [ "$client_ip\tFAILED reverse lookup at" ]); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | my $log_entry = []; | |||||||||||||||||||||||||||||||||||||||||
| 74 | # Check a seperate blocking list for particular client's/networks | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | if (-s $self->{CONFIG}->{hosts_allow_deny}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $deny_all = 0; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $allowed = 0; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | local *ALLOWDENY; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | open ALLOWDENY, $self->{CONFIG}->{hosts_allow_deny}; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | while (<ALLOWDENY>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | next if /^\#/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | next if /^\s+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 84 | # Each line can be one action, DENY, ALLOW or WARN, followed by | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # an IP, subnet or hostname, whereby 'ALL' is a special case. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # If the special rule 'DENY ALL' appears anywhere then | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # a client will be refused unless they match an 'ALLOW' line. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # Lines starting with '#' or whitespace are skipped. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my ($action,$peer) = split /\s+/, $_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $action =~ s/\s+//g; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $peer =~ s/\s+|\*//g; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($action =~ /deny/i and $peer =~ /all/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$action =~ /deny/i and $peer =~ /all/i
MC/DC0%0/2
$action =~ /deny/i and $peer =~ /all/i
$action =~ /deny/i
$peer =~ /all/i
Truth table0%0/3
A and B
A= $action =~ /deny/iB= $peer =~ /all/i
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $deny_all = 1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | ▶ | } elsif ($client_ip =~ /^$peer/ || $fqdn =~ /^$peer$/i) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$client_ip =~ /^$peer/ or $fqdn =~ /^$peer$/i
MC/DC0%0/2
$client_ip =~ /^$peer/ or $fqdn =~ /^$peer$/i
$client_ip =~ /^$peer/
$fqdn =~ /^$peer$/i
Truth table0%0/3
A or B
A= $client_ip =~ /^$peer/B= $fqdn =~ /^$peer$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($action =~ /allow/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | push @$log_entry, "$client_ip\tALLOWED connection at"; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $allowed = 1; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } elsif ($action =~ /warn/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | push @$log_entry, "$client_ip\tWARN connected at"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | ▶ | } elsif ($action =~ /deny/i and $peer !~ /all/i) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$action =~ /deny/i and not $peer =~ /all/i
MC/DC0%0/2
$action =~ /deny/i and not $peer =~ /all/i
$action =~ /deny/i
not $peer =~ /all/i
Truth table0%0/3
A and not(B)
A= $action =~ /deny/iB= not $peer =~ /all/i
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | 0, [ @$log_entry, "$client_ip\tDENIED connection at" ] | |||||||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | close ALLOWDENY; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($deny_all == 1 and $allowed == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$deny_all == 1 and $allowed == 0
MC/DC0%0/2
$deny_all == 1 and $allowed == 0
$deny_all == 1
$allowed == 0
Truth table0%0/3
A and B
A= $deny_all == 1B= $allowed == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return (0, [ @$log_entry, "$client_ip\tDENIED connection at" ]); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return (1, $log_entry); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | return (1, $log_entry); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||