blib/lib/Apache/AuthPOP3.pm

filter: Statementstmt 31.8% 21 / 66 Branchbran 3.5% 1 / 28 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 3 totaltotal 25.6% 30 / 117 CC 19 SCAR 37.2
CC
19
Cov
23%
authorize
90.0
authenticate
42.0
handler
20.0
CRAP 41.4
↑ Summary
1 package Apache::AuthPOP3;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.02';
7  
8 1 ▶use constant MP2 => (exists $ENV{MOD_PERL_API_VERSION} and
Subroutine BEGIN: called CC=2 SCAR=7.6
9 1 ▶ $ENV{MOD_PERL_API_VERSION} >= 2);
Condition33%1/3
exists $ENV{'MOD_PERL_API_VERSION'} && $ENV{'MOD_PERL_API_VERSION'} >= 2
!ll&&!rl&&r
100
MC/DC0%0/2
exists $ENV{'MOD_PERL_API_VERSION'} && $ENV{'MOD_PERL_API_VERSION'} >= 2
exists $ENV{'MOD_PERL_API_VERSION'} $ENV{'MOD_PERL_API_VERSION'} >= 2
Truth table33%1/3
A && B
A= exists $ENV{'MOD_PERL_API_VERSION'}B= $ENV{'MOD_PERL_API_VERSION'} >= 2
ABresult
0X0
100
111
10  
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=13.0
12 1 ▶ if ($ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOD_PERL"}) 0 1 1
13 0 my @constants = qw(OK DECLINED HTTP_UNAUTHORIZED);
14 0 if (MP2) {
15 require Apache2::Access; # for note_basic_auth_failure, get_basic_auth_pw, and requires
16 require Apache2::RequestUtil; # for push_handlers, and dir_config
17 require Apache2::RequestRec; # for user, and filename
18 require Apache2::Log; # for log_error
19 require Apache2::Const;
20 Apache2::Const->import(-compile => @constants);
21 } else {
22 0 require Apache;
23 0 require Apache::Constants;
24 0 Apache::Constants->import(@constants);
25 }
26 }
27 }
28  
29 1 ▶use Net::POP3;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Cache::FileCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Digest::SHA1 qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 0 ▶sub handler {
Subroutine handler: not called CC=4 SCAR=30.0
Pod undocumented
34 0 my $r = shift;
35  
36 0 $r->push_handlers(PerlAuthzHandler => \&authorize);
37  
38 # check if MailHost config variable is present
39 0 ▶ return MP2 ? Apache2::Const::DECLINED() : Apache::Constants::DECLINED() unless (my $mailhost = $r->dir_config('MailHost'));
Branch: 1 branch
BranchTrueFalseTotal
unless my $mailhost = $r->dir_config("MailHost") 0 0 0
40  
41 # get user's authentication credentials
42 0 my ($res, $passwd_sent) = $r->get_basic_auth_pw;
43 0 ▶ return $res if (MP2 and $res != Apache2::Const::OK() or !MP2 and $res != Apache::Constants::OK());
Branch: 1 branch
BranchTrueFalseTotal
if $res != Apache::Constants::OK() 0 0 0
44 0 my $user_sent = $r->user;
45  
46 0 my $reason = authenticate($mailhost, $user_sent, $passwd_sent);
47 0 ▶ if ($reason) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason) 0 0 0
48 0 $r->note_basic_auth_failure;
49 0 $r->log_reason($reason, $r->filename);
50 0 return MP2 ? Apache2::Const::HTTP_UNAUTHORIZED() : Apache::Constants::HTTP_UNAUTHORIZED();
51 }
52  
53 0 return MP2 ? Apache2::Const::OK() : Apache::Constants::OK();
54 }
55  
56 0 ▶sub authenticate {
Subroutine authenticate: not called CC=6 SCAR=37.4
Pod undocumented
57 0 my ($mailhost, $user_sent, $passwd_sent) = @_;
58  
59 0 ▶ $user_sent and $passwd_sent or return 'either username or password is empty';
Branch: 1 branch
BranchTrueFalseTotal
$user_sent and $passwd_sent or return "either username or password is empty" 0 0 0
Condition0%0/3
$user_sent and $passwd_sent
!ll&&!rl&&r
000
MC/DC0%0/2
$user_sent and $passwd_sent
$user_sent $passwd_sent
Truth table0%0/3
A and B
A= $user_sentB= $passwd_sent
ABresult
0X0
100
111
60  
61 # cache sha1-ed password
62 0 my $cache = new Cache::FileCache({ 'namespace' => __PACKAGE__, 'default_expires_in' => 120 });
63 0 my $passwd_cached_sha1 = $cache->get($user_sent);
64 0 my $passwd_sent_sha1 = sha1_hex($passwd_sent);
65 0 ▶ if (defined $passwd_cached_sha1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $passwd_cached_sha1) { } 0 0 0
66 0 ▶ return "user $user_sent: POP3 login failed" if $passwd_cached_sha1 ne $passwd_sent_sha1;
Branch: 1 branch
BranchTrueFalseTotal
if $passwd_cached_sha1 ne $passwd_sent_sha1 0 0 0
67 } else {
68 0 ▶ return "user $user_sent: POP3 login failed" unless Net::POP3->new($mailhost)->login($user_sent, $passwd_sent);
Branch: 1 branch
BranchTrueFalseTotal
unless "Net::POP3"->new($mailhost)->login($user_sent, $passwd_sent) 0 0 0
69 0 $cache->set($user_sent, $passwd_sent_sha1);
70 }
71  
72 0 return '';
73 }
74  
75 0 ▶sub authorize {
Subroutine authorize: not called CC=9 SCAR=45.0
Pod undocumented
76 0 my $r = shift;
77  
78 0 ▶ return MP2 ? Apache2::Const::DECLINED() : Apache::Constants::DECLINED() unless (my $requires = $r->requires);
Branch: 1 branch
BranchTrueFalseTotal
unless my $requires = $r->requires 0 0 0
79 0 my $user_sent = $r->user;
80  
81 0 for my $entry (@$requires) {
82 0 my ($requirement, @rest) = split /\s+/, $entry->{requirement};
83 0 ▶ return MP2 ? Apache2::Const::OK() : Apache::Constants::OK() if (lc $requirement eq 'valid-user');
Branch: 1 branch
BranchTrueFalseTotal
if lc $requirement eq "valid-user" 0 0 0
84  
85 0 ▶ if (lc $requirement eq 'user') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $requirement eq "user") 0 0 0
86 0 foreach (@rest) {
87 0 ▶ if ($user_sent eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_sent eq $_) 0 0 0
88  
89 # change the username seen by apache to the one defined in UserMap
90 0 ▶ if (my $usermap = $r->dir_config('UserMap')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $usermap = $r->dir_config("UserMap")) 0 0 0
91 0 my %usermap = split /\s*(?:=>|,)\s*/, $usermap;
92 0 ▶ $r->user($usermap{$user_sent}) if defined $usermap{$user_sent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $usermap{$user_sent} 0 0 0
93 }
94 0 return MP2 ? Apache2::Const::OK() : Apache::Constants::OK();
95 }
96 }
97 0 $r->log_error("user $user_sent: invalid user");
98 }
99  
100 0 $r->log_error("user $user_sent: failed requirement");
101 }
102  
103 0 $r->note_basic_auth_failure;
104 0 $r->log_reason("user $user_sent: not authorized", $r->filename);
105 0 return MP2 ? Apache2::Const::HTTP_UNAUTHORIZED() : Apache::Constants::HTTP_UNAUTHORIZED();
106 }
107  
108 1 1;
109  
110 __END__
↑ Summary