AuthorizedKeysFile.pm

filter: Statementstmt 77.0% 114 / 148 Branchbran 60.3% 35 / 58 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.9% 17 / 21 Podpod 71.4% 10 / 14 totaltotal 71.9% 177 / 246 CC 33 SCAR 17.6
CC
33
Cov
72%
ssh_dir
20.0
sanity_check
8.2
line_parse
6.4
CRAP 5.8
« AuthorizedKey/SSH2.pm ↑ Summary AuthorizedKey/Base.pm »
1 ###########################################
2 package Net::SSH::AuthorizedKeysFile;
3 ###########################################
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Net::SSH::AuthorizedKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Net::SSH::AuthorizedKey::SSH1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Net::SSH::AuthorizedKey::SSH2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 our $VERSION = "0.18";
13  
14 ###########################################
15 24 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
16 ###########################################
17 24 my($class, @options) = @_;
18  
19 24 my $self = {
20 default_file => "$ENV{HOME}/.ssh/authorized_keys",
21 strict => 0,
22 abort_on_error => 0,
23 append => 0,
24 ridiculous_line_len => 100_000,
25 @options,
26 };
27  
28 24 bless $self, $class;
29  
30 # We allow keys to be set in the constructor
31 24 ▶ my $keys = $self->{keys} if exists $self->{keys};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"keys"} 0 24 24
32  
33 24 $self->reset();
34  
35 24 ▶ $self->{keys} = $keys if defined $keys;
Branch: 1 branch
BranchTrueFalseTotal
if defined $keys 0 24 24
36  
37 24 return $self;
38 }
39  
40 ###########################################
41 3 ▶sub sanity_check {
Subroutine sanity_check: called CC=8 SCAR=21.0
Pod documented
42 ###########################################
43 3 my($self, $file) = @_;
44  
45 3 ▶ $self->{file} = $file if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 2 1 3
46 3 ▶ $self->{file} = $self->{default_file} if !defined $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 0 3 3
47  
48 3 my $result = undef;
49  
50 3 my $fh;
51  
52 3 ▶ if(! open $fh, "<$self->{file}") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, qq[<$self->{"file"}]) 0 3 3
53 0 ERROR "Cannot open file $self->{file}";
54 0 return undef;
55 }
56  
57 3 ▶ while(
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rc = sysread($fh, my $chunk, $self->{"ridiculous_line_len"}))) 4 0 4
58 defined(my $rc =
59 sysread($fh, my $chunk, $self->{ridiculous_line_len}))) {
60 4 ▶ if($rc < $self->{ridiculous_line_len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc < $self->{"ridiculous_line_len"}) 2 2 4
61 2 $result = 1;
62 2 last;
63 }
64  
65 2 ▶ if(index( $chunk, "\n" ) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($chunk, "\n") != -1) 1 1 2
66 # contains a newline, looks good
67 1 next;
68 }
69  
70 # we've got a line that's between ridiculous_line_len and
71 # 2*ridiculous_line_len characters long. Pull the plug.
72 1 $self->error("File $self->{file} contains insanely long lines " .
73 "(> $self->{ridiculous_line_len} chars");
74 1 last;
75 }
76  
77 DONE:
78 3 close $fh;
79  
80 3 ▶ if(!$result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 1 2 3
81 1 ERROR "Sanity check of file $self->{file} failed";
82 }
83 3 return $result;
84 }
85  
86 ###########################################
87 16 ▶sub keys {
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
88 ###########################################
89 16 my($self) = @_;
90  
91 16 return @{$self->{keys}};
92 }
93  
94 ###########################################
95 47 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
96 ###########################################
97 47 my($self) = @_;
98  
99 47 $self->{keys} = [];
100 47 $self->{content} = "";
101 47 $self->{error} = undef;
102 }
103  
104 ###########################################
105 0 ▶sub content {
Subroutine content: not called CC=2 SCAR=17.9
Pod documented
106 ###########################################
107 0 my($self, $new_content) = @_;
108  
109 0 ▶ if( defined $new_content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_content) 0 0 0
110 0 $self->reset();
111 0 $self->{content} = $new_content;
112 }
113  
114 0 return $self->{content};
115 }
116  
117 ###########################################
118 57 ▶sub line_parse {
Subroutine line_parse: called CC=5 SCAR=18.6
Pod undocumented
119 ###########################################
120 57 my($self, $line, $line_number) = @_;
121  
122 57 chomp $line;
123  
124 57 DEBUG "Parsing line [$line]";
125  
126 57 $self->error( "" );
127  
128 57 my $pk = Net::SSH::AuthorizedKey->parse( $line );
129  
130 57 ▶ if( !$pk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk) 12 45 57
131 12 my $msg = "[$line] rejected by all parsers";
132 12 WARN $msg;
133 12 $self->error($msg);
134 12 return undef;
135 }
136  
137 45 ▶ if(! $self->{strict} or $pk->sanity_check()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"strict"} or $pk->sanity_check) 45 0 45
Condition33%1/3
not $self->{"strict"} or $pk->sanity_check
l!l&&r!l&&!r
4500
MC/DC0%0/2
not $self->{"strict"} or $pk->sanity_check
not $self->{"strict"} $pk->sanity_check
Truth table33%1/3
not(A) or B
A= not $self->{"strict"}B= $pk->sanity_check
ABresult
1X1
011
000
138 45 return $pk;
139 }
140  
141 0 WARN "Key [$line] failed sanity check";
142  
143 0 ▶ if($self->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"}) 0 0 0
144 0 $self->error( $pk->error() );
145 0 return undef;
146 }
147  
148 # Key is corrupted, but ok in non-strict mode
149 0 return $pk;
150 }
151  
152 ###########################################
153 23 ▶sub parse {
Subroutine parse: called CC=6 SCAR=17.9
Pod undocumented
154 ###########################################
155 23 my($self) = @_;
156  
157 23 $self->{keys} = [];
158 23 $self->{error} = "";
159  
160 23 my $line_number = 0;
161  
162 23 for my $line (split /\n/, $self->{content}) {
163 66 $line_number++;
164  
165 66 $line =~ s/^\s+//; # Remove leading blanks
166 66 $line =~ s/\s+$//; # Remove trailing blanks
167 66 ▶ next if $line =~ /^$/; # Ignore empty lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$/ 4 62 66
168 62 ▶ next if $line =~ /^#/; # Ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 5 57 62
169  
170 57 my $key = $self->line_parse($line, $line_number);
171  
172 57 ▶ if( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 45 12 57
173 45 push @{$self->{keys}}, $key;
174 } else {
175 12 ▶ if($self->{abort_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"abort_on_error"}) 2 10 12
176 2 $self->error("Line $line_number: " . $self->error());
177 2 return undef;
178 }
179 }
180 }
181  
182 21 return 1;
183 }
184  
185 ###########################################
186 23 ▶sub read {
Subroutine read: called CC=5 SCAR=16.3
Pod documented
187 ###########################################
188 23 my($self, $file) = @_;
189  
190 23 $self->reset();
191  
192 23 ▶ $self->{file} = $file if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 0 23 23
193 23 ▶ $self->{file} = $self->{default_file} if !defined $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 0 23 23
194 23 $self->{content} = "";
195  
196 23 DEBUG "Reading in $self->{file}";
197  
198 23 ▶ open FILE, "<$self->{file}" or LOGDIE "Cannot open $self->{file}";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, qq[<$self->{"file"}] or LOGDIE(qq[Cannot open $self->{"file"}]) 23 0 23
199  
200 23 ▶ while(<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 66 23 89
201 66 $self->{content} .= $_;
202 }
203  
204 23 close FILE;
205  
206 23 return $self->parse();
207 }
208  
209 ###########################################
210 5 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
211 ###########################################
212 5 my($self) = @_;
213  
214 5 my $string = "";
215  
216 5 for my $key ( @{ $self->{keys} } ) {
217 14 $string .= $key->as_string . "\n";
218 }
219  
220 5 return $string;
221 }
222  
223 ###########################################
224 4 ▶sub save {
Subroutine save: called CC=3 SCAR=12.6
Pod documented
225 ###########################################
226 4 my($self, $file) = @_;
227  
228 4 ▶ if(!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 4 0 4
229 4 $file = $self->{file};
230 }
231  
232 4 ▶ if(! open FILE, ">$file") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, ">$file") 0 4 4
233 0 $self->error("Cannot open $file ($!)");
234 0 WARN $self->error();
235 0 return undef;
236 }
237  
238 4 print FILE $self->as_string();
239 4 close FILE;
240 }
241  
242 ###########################################
243 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
Pod undocumented
244 ###########################################
245 0 my($self, $key) = @_;
246  
247 0 $self->{append} = 1;
248 }
249  
250 ###########################################
251 76 ▶sub error {
Subroutine error: called CC=3 SCAR=11.0
Pod documented
252 ###########################################
253 76 my($self, $text) = @_;
254  
255  
256 76 ▶ if(defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 72 4 76
257 72 $self->{error} = $text;
258  
259 72 ▶ if(length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 15 57 72
260 15 ERROR "$text";
261 }
262 }
263  
264 76 return $self->{error};
265 }
266  
267 ###########################################
268 0 ▶sub ssh_dir {
Subroutine ssh_dir: not called CC=4 SCAR=30.0
Pod documented
269 ###########################################
270 0 my($self, $user) = @_;
271  
272 0 ▶ if(!defined $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 0 0
273 0 my $uid = $>;
274 0 $user = getpwuid($uid);
275 0 ▶ if(!defined $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 0 0
276 0 ERROR "getpwuid of $uid failed ($!)";
277 0 return undef;
278 }
279 }
280  
281 0 my @pwent = getpwnam($user);
282  
283 0 ▶ if(! defined $pwent[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pwent[0]) 0 0 0
284 0 ERROR "getpwnam of $user failed ($!)";
285 0 return undef;
286 }
287  
288 0 my $home = $pwent[7];
289  
290 0 return File::Spec->catfile($home, ".ssh");
291 }
292  
293 ###########################################
294 0 ▶sub path_locate {
Subroutine path_locate: not called CC=2 SCAR=17.9
Pod documented
295 ###########################################
296 0 my($self, $user) = @_;
297  
298 0 my $ssh_dir = $self->ssh_dir($user);
299  
300 0 ▶ return undef if !defined $ssh_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ssh_dir 0 0 0
301  
302 0 return File::Spec->catfile($ssh_dir, "authorized_keys");
303 }
304  
305 8 1;
306  
307 __END__
« AuthorizedKey/SSH2.pm ↑ Summary AuthorizedKey/Base.pm »