AuthorizedKey/Base.pm

filter: Statementstmt 91.7% 166 / 181 Branchbran 79.4% 54 / 68 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 95.0% 19 / 20 Podpod 9.0% 1 / 11 totaltotal 85.5% 248 / 290 CC 26 SCAR 15.1
CC
26
Cov
88%
option
10.1
parse
5.1
options_as_string
5.0
CRAP 4.5
« AuthorizedKeysFile.pm ↑ Summary AuthorizedKey/SSH1.pm »
1 ###########################################
2 package Net::SSH::AuthorizedKey::Base;
3 ###########################################
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Accessors common for both ssh1 and ssh2 keys
11 11 our @accessors = qw(key type error email comment);
12 11 __PACKAGE__->make_accessor( $_ ) for @accessors;
13  
14 # Some functions must be implemented in the subclass
15 do {
16 11 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 *{__PACKAGE__ . "::$_"} = sub {
19 0 ▶ die "Whoa! '$_' in the virtual base class has to be ",
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 " implemented by a real subclass.";
21 22 };
22  
23 11 } for qw(option_type as_string);
24  
25 # Options accepted by all keys
26 11 our %VALID_OPTIONS = (
27 "no-port-forwarding" => 1,
28 "no-agent-forwarding" => 1,
29 "no-x11-forwarding" => 1,
30 "no-pty" => 1,
31 "no-user-rc" => 1,
32 command => "s",
33 environment => "s",
34 from => "s",
35 permitopen => "s",
36 tunnel => "s",
37 );
38  
39 ###########################################
40 sub new {
41 ###########################################
42 49 ▶ my($class, %options) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
43  
44 49 my $self = {
45 error => "(no error)",
46 option_order => [],
47 %options,
48 };
49  
50 49 bless $self, $class;
51 49 return $self;
52 }
53  
54 ###########################################
55 sub option_type_global {
56 ###########################################
57 70 ▶ my($self, $key) = @_;
Subroutine option_type_global: called CC=2 SCAR=7.6
Pod undocumented
58  
59 70 ▶ if(exists $VALID_OPTIONS{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Net::SSH::AuthorizedKey::Base::VALID_OPTIONS{$key}) 70 0 70
60 70 return $VALID_OPTIONS{ $key };
61 }
62  
63 # Maybe the subclass knows about it
64 0 return $self->option_type($key);
65 }
66  
67 ###########################################
68 sub options {
69 ###########################################
70 2 ▶ my($self) = @_;
Subroutine options: called CC=1 SCAR=0.0
Pod documented
71  
72 return {
73 2 map { $_ => $self->option( $_ ) }
74 2 keys %{ $self->{ options } }
75 };
76 }
77  
78 ###########################################
79 sub option {
80 ###########################################
81 70 ▶ my($self, $key, $value, $append) = @_;
Subroutine option: called CC=10 SCAR=23.1
Pod undocumented
82  
83 70 $key = lc $key;
84  
85 70 my $option_type = $self->option_type_global($key);
86  
87 70 ▶ if(! defined $option_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option_type) 0 70 70
88 0 LOGWARN "Illegal option '$key'";
89 0 return undef;
90 }
91  
92 70 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 57 13 70
93  
94 57 ▶ if( $append ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 45 12 57
95 45 ▶ if( $self->{options}->{$key} and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{$key} and ref $self->{"options"}{$key} ne "ARRAY") 3 42 45
Condition100%3/3
$self->{"options"}{$key} and ref $self->{"options"}{$key} ne "ARRAY"
!ll&&!rl&&r
4023
MC/DC100%2/2
$self->{"options"}{$key} and ref $self->{"options"}{$key} ne "ARRAY"
$self->{"options"}{$key} ref $self->{"options"}{$key} ne "ARRAY"
Truth table100%3/3
A and B
A= $self->{"options"}{$key}B= ref $self->{"options"}{$key} ne "ARRAY"
ABresult
0X0
100
111
96 ref($self->{options}->{$key}) ne "ARRAY" ) {
97 3 $self->{options}->{$key} = [ $self->{options}->{$key} ];
98 }
99 } else {
100 12 $self->option_delete( $key );
101 }
102  
103 57 ▶ if($option_type eq "s") {
Branch: 1 branch
BranchTrueFalseTotal
if ($option_type eq 's') { } 35 22 57
104 35 ▶ if( $self->{options}->{$key} and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{$key} and ref $self->{'options'}{$key} eq 'ARRAY') { } 5 30 35
Condition66%2/3
$self->{"options"}{$key} and ref $self->{"options"}{$key} eq "ARRAY"
!ll&&!rl&&r
3005
MC/DC50%1/2
$self->{"options"}{$key} and ref $self->{"options"}{$key} eq "ARRAY"
$self->{"options"}{$key} ref $self->{"options"}{$key} eq "ARRAY"
Truth table66%2/3
A and B
A= $self->{"options"}{$key}B= ref $self->{"options"}{$key} eq "ARRAY"
ABresult
0X0
100
111
105 ref($self->{options}->{$key}) eq "ARRAY" ) {
106 5 DEBUG "Adding option $key to $value";
107 5 push @{ $self->{options}->{$key} }, $value;
108 } else {
109 30 DEBUG "Setting option $key to $value";
110 30 $self->{options}->{$key} = $value;
111 }
112 } else {
113 22 $self->{options}->{$key} = undef;
114 }
115 57 push @{ $self->{option_order} }, $key;
116 }
117  
118 70 ▶ if( "$option_type" eq "1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$option_type" eq 1) 25 45 70
119 25 return exists $self->{options}->{$key};
120 }
121  
122 45 return $self->{options}->{$key};
123 }
124  
125 ###########################################
126 sub option_delete {
127 ###########################################
128 15 ▶ my($self, $key) = @_;
Subroutine option_delete: called CC=1 SCAR=0.0
Pod undocumented
129  
130 15 $key = lc $key;
131  
132 15 @{ $self->{option_order} } =
133 15 grep { $_ ne $key } @{ $self->{option_order} };
134  
135 15 delete $self->{options}->{$key};
136 }
137  
138 ###########################################
139 sub options_as_string {
140 ###########################################
141 66 ▶ my($self) = @_;
Subroutine options_as_string: called CC=5 SCAR=16.1
Pod undocumented
142  
143 66 my $string = "";
144 66 my @parts = ();
145  
146 66 for my $option ( @{ $self->{option_order} } ) {
147 76 ▶ if(defined $self->{options}->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'options'}{$option}) { } 46 30 76
148 46 ▶ if(ref($self->{options}->{$option}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'options'}{$option} eq 'ARRAY') { } 10 36 46
149 10 for (@{ $self->{options}->{$option} }) {
150 28 push @parts, option_quote($option, $_);
151 }
152 } else {
153 36 push @parts, option_quote($option, $self->{options}->{$option});
154 }
155 } else {
156 30 push @parts, $option;
157 }
158 }
159 66 return join(',', @parts);
160 }
161  
162 ###########################################
163 sub option_quote {
164 ###########################################
165 64 ▶ my($option, $text) = @_;
Subroutine option_quote: called CC=1 SCAR=0.0
Pod undocumented
166  
167 64 $text =~ s/([\\"])/\\$1/g;
168 64 return "$option=\"" . $text . "\"";
169 }
170  
171 ###########################################
172 sub parse {
173 ###########################################
174 122 ▶ my($class, $string) = @_;
Subroutine parse: called CC=5 SCAR=16.2
Pod undocumented
175  
176 122 DEBUG "Parsing line '$string'";
177  
178 # Clean up leading whitespace
179 122 $string =~ s/^\s+//;
180 122 $string =~ s/^#.*//;
181
182 122 ▶ if(! length $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $string) 0 122 122
183 0 DEBUG "Nothing to parse";
184 0 return;
185 }
186  
187 122 ▶ if(my $key = $class->key_read( $string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $class->key_read($string)) 31 91 122
188 # We found a key without options
189 31 $key->{options} = {};
190 31 DEBUG "Found ", $key->type(), " key: ", $key->as_string();
191 31 return $key;
192 }
193  
194 # No key found. Probably there are options in front of the key.
195 # By the way: the openssh-5.x parser doesn't allow escaped
196 # backslashes (\\), so we don't either.
197 91 my $rc = (
198 (my $key_string = $string) =~
199 s/^((?:
200 (?:"(?:\\"|.)*?)"|
201 \S
202 )+
203 )
204 //x );
205 91 ▶ my $options_string = ($rc ? $1 : "");
Branch: 1 branch
BranchTrueFalseTotal
$rc ? : 91 0 91
206 91 $key_string =~ s/^\s+//;
207  
208 91 DEBUG "Trying line with options stripped: [$key_string]";
209  
210 91 ▶ if(my $key = $class->key_read( $key_string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $class->key_read($key_string)) 18 73 91
211 # We found a key with options
212 18 $key->{options} = {};
213 18 $key->options_parse( $options_string );
214 18 DEBUG "Found ", $key->type(), " key: ", $key->as_string();
215 18 return $key;
216 }
217  
218 73 DEBUG "$class cannot parse line: $string";
219  
220 73 return undef;
221 }
222  
223 ###########################################
224 sub options_parse {
225 ###########################################
226 18 ▶ my($self, $string) = @_;
Subroutine options_parse: called CC=4 SCAR=13.9
Pod undocumented
227  
228 18 DEBUG "Parsing options: [$string]";
229 18 my @options = parse_line(qr/\s*,\s*/, 0, $string);
230  
231 # delete empty/undefined fields
232 18 ▶ @options = grep { defined $_ and length $_ } @options;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 47 1 48
233  
234 18 DEBUG "Parsed options: ", join(' ', map { "[$_]" } @options);
235  
236 18 for my $option (@options) {
237 45 my($key, $value) = split /=/, $option, 2;
238 45 ▶ $value = 1 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 17 28 45
239 45 $value =~ s/^"(.*)"$/$1/; # remove quotes
240  
241 45 $self->option($key, $value, 1);
242 }
243 }
244  
245 ###########################################
246 sub fingerprint {
247 ###########################################
248 1 ▶ my($self) = @_;
Subroutine fingerprint: called CC=1 SCAR=0.0
Pod undocumented
249  
250 1 my $data = $self->options();
251  
252 1 my $string = join '', map { $_ => $data->{$_} } sort keys %$data;
253 1 $string .= $self->key();
254  
255 1 return md5_hex($string);
256 }
257  
258 ##################################################
259 # Poor man's Class::Struct
260 ##################################################
261 sub make_accessor {
262 ##################################################
263 88 ▶ my($package, $name) = @_;
Subroutine make_accessor: called CC=3 SCAR=11.2
Pod undocumented
264  
265 11 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
266  
267 88 my $code = <<EOT;
268 *{"$package\\::$name"} = sub {
269 my(\$self, \$value) = \@_;
270  
271 if(defined \$value) {
272 \$self->{$name} = \$value;
273 }
274 if(exists \$self->{$name}) {
275 return (\$self->{$name});
276 } else {
277 return "";
278 }
279 }
280 EOT
281 88 ▶ if(! defined *{"$package\::$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined *{"${package}::$name";}) 88 0 88
282 88 ▶ eval $code or die "$@";
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 17 branches
BranchTrueFalseTotal
eval $code or die "$@" 0 88 88
if (defined $value) 31 4 35
if (exists $self->{'email'}) { } 35 0 35
if (defined $value) 48 18 66
if (exists $self->{'keylen'}) { } 66 0 66
if (defined $value) 21 4 25
if (exists $self->{'comment'}) { } 25 0 25
if (defined $value) 17 7 24
if (exists $self->{'key'}) { } 24 0 24
if (defined $value) 26 16 42
if (exists $self->{'encryption'}) { } 42 0 42
if (defined $value) 7 34 41
if (exists $self->{'type'}) { } 41 0 41
if (defined $value) 18 11 29
if (exists $self->{'error'}) { } 29 0 29
if (defined $value) 45 12 57
if (exists $self->{'exponent'}) { } 57 0 57
283 }
284 }
285  
286 11 1;
287  
288 __END__
289  
290 =head1 NAME
291  
292 Net::SSH::AuthorizedKey::Base - Virtual Base Class for ssh keys
293  
294 =head1 SYNOPSIS
295  
296 # Documentation to understand methods shared
297 # by all parsers. Not for direct use.
298  
299 =head1 DESCRIPTION
300  
301 This is the key parser base class, offering methods common to all
302 parsers. Don't use it directly, but read the documentation below to
303 see what functionality all parsers offer.
304  
305 =over 4
306  
307 =item error()
308  
309 If a parser fails for any reason, it will leave a textual description of
310 the error that threw it off. This methods retrieves the error text.
311  
312 =item options()
313  
314 =item key()
315  
316 The actual content of the key, either a big number in case of ssh-1 or
317 a base64-encoded string for ssh-2.
318  
319 =item type()
320  
321 Type of a key. (Somewhat redundant, as you could also check what subclass
322 a key is of). Either set to C<"ssh-1"> or C<"ssh-2">.
323  
324 =item email()
325  
326 Identical with comment().
327  
328 =item comment()
329  
330 Identical with email(). This is the text that follows in the authorized_keys
331 file after the key content. Mostly used for emails and host names.
332  
333 =back
334  
335 =head1 LEGALESE
336  
337 Copyright 2005-2009 by Mike Schilli, all rights reserved.
338 This program is free software, you can redistribute it and/or
339 modify it under the same terms as Perl itself.
340  
341 =head1 AUTHOR
342  
343 2005, Mike Schilli <m@perlmeister.com>
« AuthorizedKeysFile.pm ↑ Summary AuthorizedKey/SSH1.pm »