Connector/Builtin/Authentication/PasswordScheme.pm

filter: Statementstmt 85.5% 95 / 111 Branchbran 76.3% 29 / 38 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 80.5% 141 / 175 CC 22 SCAR 22.3
CC
22
Cov
81%
get
16.9
exists
6.3
_build_config
6.0
CRAP 9.3
« Connector/Proxy/Net/SFTP.pm ↑ Summary Connector/Proxy/Proc/SafeExec.pm »
1 # Connector::Builtin::Authentication::PasswordScheme
2 #
3 # Check passwords against a file with salted hashes and scheme prefix
4 #
5 package Connector::Builtin::Authentication::PasswordScheme;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 extends 'Connector::Builtin';
18  
19 0 ▶sub _build_config {
Subroutine _build_config: not called CC=2 SCAR=17.9
20 0 my $self = shift;
21  
22 0 ▶ if (! -r $self->{LOCATION}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $self->{"LOCATION"}) 0 0 0
23 0 confess("Cannot open input file " . $self->{LOCATION} . " for reading.");
24 }
25  
26 0 return 1;
27 }
28  
29 10 ▶sub get {
Subroutine get: called CC=15 SCAR=28.3
Pod documented
30 10 my $self = shift;
31 10 my $arg = shift;
32 10 my $params = shift;
33  
34 10 my @path = $self->_build_path( $arg );
35 10 my $user = shift @path;
36  
37 10 my $password = $params->{password};
38  
39  
40 10 ▶ if (!$user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 10 10
41 0 $self->log()->error('No username');
42 0 die "no username given";
43 }
44  
45 10 ▶ if (!$password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 1 9 10
46 1 $self->log()->error('No password');
47 1 die "no password given";
48 }
49  
50  
51 9 $self->log()->debug('verify password for ' . $user );
52  
53 9 ▶ if ($user =~ /[^a-zA-Z0-9_\-\.\@]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user =~ /[^a-zA-Z0-9_\-\.\@]/) 1 8 9
54 1 $self->log()->error('Invalid chars in username ('.$user.')');
55 1 return $self->_node_not_exists( $user );
56 }
57  
58 8 my $filename = $self->{LOCATION};
59  
60 8 ▶ if (! -r $filename || ! open FILE, "$filename") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $filename and open FILE, "$filename") 0 8 8
Condition33%1/3
-r $filename and open FILE, "$filename"
!ll&&!rl&&r
008
MC/DC0%0/2
-r $filename and open FILE, "$filename"
-r $filename open FILE, "$filename"
Truth table33%1/3
A and B
A= -r $filenameB= open FILE, "$filename"
ABresult
0X0
100
111
61 0 $self->log()->error('Can\'t open/read from file ' . $filename);
62 0 die 'Can\'t open/read from file ' . $filename;
63 }
64  
65 8 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 23 2 25
66 23 ▶ if (/^$user:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$user:/) 6 17 23
67 6 chomp;
68 6 my @t = split(/:/, $_, 3);
69 6 $self->log()->trace('found line ' . Dumper @t);
70  
71 # This code is mainly a copy of OpenXPKI::Server::Authentication::Password
72 # but we do not support unsalted passwords
73 # digest specified in RFC 2307 userPassword notation?
74 6 my $encrypted;
75 my $scheme;
76 6 ▶ if ($t[1] =~ m{ \{ (\w+) \} (.+) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[1] =~ / \{ (\w+) \} (.+) /msx) { } 6 0 6
77 6 $scheme = lc($1);
78 6 $encrypted = $2;
79 } else {
80 0 $self->log()->error('unparsable entry ' . $t[1]);
81 0 return 0;
82 }
83  
84 6 my ($computed_secret, $salt);
85 6 eval {
86 6 ▶ if ($scheme eq 'ssha') {
Branch: 3 branches
BranchTrueFalseTotal
if ($scheme eq 'ssha') { } 2 4 6
elsif ($scheme eq 'smd5') { } 1 3 4
elsif ($scheme eq 'crypt') { } 3 0 3
87 2 $salt = substr(decode_base64($encrypted), 20);
88 2 my $ctx = Digest::SHA->new();
89 2 $ctx->add($password);
90 2 $ctx->add($salt);
91 2 $computed_secret = encode_base64($ctx->digest() . $salt, '');
92 } elsif ($scheme eq 'smd5') {
93 1 $salt = substr(decode_base64($encrypted), 16);
94 1 my $ctx = Digest::MD5->new();
95 1 $ctx->add($password);
96 1 $ctx->add($salt);
97 1 $computed_secret = encode_base64($ctx->digest() . $salt, '');
98 } elsif ($scheme eq 'crypt') {
99 3 $computed_secret = crypt($password, $encrypted);
100 } else {
101 0 $self->log()->error('unsupported scheme' . $scheme);
102 0 return 0;
103 }
104 };
105  
106 6 ▶ $self->log()->debug('eval failed ' . $EVAL_ERROR->message()) if ($EVAL_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
if $Connector::Builtin::Authentication::PasswordScheme::EVAL_ERROR 0 6 6
107  
108 6 ▶ if (! defined $computed_secret) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $computed_secret) 0 6 6
109 0 $self->log()->error('unable to compute secret using scheme ' . $scheme);
110 0 return 0;
111 }
112  
113 ##! 2: "ident user ::= $account and digest ::= $computed_secret"
114 6 $computed_secret =~ s{ =+ \z }{}xms;
115 6 $encrypted =~ s{ =+ \z }{}xms;
116  
117 ## compare passphrases
118 6 ▶ if ($computed_secret eq $encrypted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($computed_secret eq $encrypted) { } 4 2 6
119 4 $self->log()->info('Password accepted for ' . $user);
120 4 return 1;
121 } else {
122 2 $self->log()->info('Password mismatch for ' . $user);
123 2 return 0;
124 }
125 }
126 }
127 2 return $self->_node_not_exists( $user );
128 }
129  
130 2 ▶sub get_meta {
Subroutine get_meta: called CC=2 SCAR=6.9
Pod documented
131 2 my $self = shift;
132  
133 # If we have no path, we tell the caller that we are a connector
134 2 my @path = $self->_build_path( shift );
135 2 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 1 1 2
136 1 return { TYPE => "connector" };
137 }
138  
139 1 return {TYPE => "scalar" };
140 }
141  
142 4 ▶sub exists {
Subroutine exists: called CC=6 SCAR=18.4
Pod documented
143  
144 4 my $self = shift;
145  
146 # No path = connector root which always exists
147 4 my @path = $self->_build_path( shift );
148 4 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 1 3 4
149 1 return 1;
150 }
151  
152 3 my $user = shift @path;
153  
154 3 my $filename = $self->{LOCATION};
155 3 ▶ if (! -r $filename || ! open FILE, "$filename") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $filename and open FILE, "$filename") 0 3 3
Condition33%1/3
-r $filename and open FILE, "$filename"
!ll&&!rl&&r
003
MC/DC0%0/2
-r $filename and open FILE, "$filename"
-r $filename open FILE, "$filename"
Truth table33%1/3
A and B
A= -r $filenameB= open FILE, "$filename"
ABresult
0X0
100
111
156 0 $self->log()->error('Can\'t open/read from file ' . $filename);
157 0 return 0;
158 }
159  
160 3 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 7 1 8
161 7 ▶ if (/^$user:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$user:/) 2 5 7
162 2 return 1;
163 }
164 }
165 1 return 0;
166 }
167  
168 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 __PACKAGE__->meta->make_immutable;
170  
171 1 1;
172 __END__
« Connector/Proxy/Net/SFTP.pm ↑ Summary Connector/Proxy/Proc/SafeExec.pm »