AuthorizedKey/Base.pm

filter: Statementstmt 88.4% 160 / 181 Branchbran 72.7% 48 / 66 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 92.5% 25 / 27 Podpod 9.0% 1 / 11 totaltotal 82.3% 247 / 300 CC 40 SCAR 14.2
CC
40
Cov
84%
option
10.1
parse
5.1
options_as_string
5.0
CRAP 4.1
« AuthorizedKeysFile.pm ↑ Summary AuthorizedKey/SSH1.pm »
1 ###########################################
2 package Net::SSH::AuthorizedKey::Base;
3 ###########################################
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶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 12 our @accessors = qw(key type error email comment);
12 12 __PACKAGE__->make_accessor( $_ ) for @accessors;
13  
14 # Some functions must be implemented in the subclass
15 do {
16 12 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 24 ▶ *{__PACKAGE__ . "::$_"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 0 die "Whoa! '$_' in the virtual base class has to be ",
20 " implemented by a real subclass.";
21 24 };
22  
23 12 } for qw(option_type as_string);
24  
25 # Options accepted by all keys
26 12 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 52 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
41 ###########################################
42 52 my($class, %options) = @_;
43  
44 52 my $self = {
45 error => "(no error)",
46 option_order => [],
47 %options,
48 };
49  
50 52 bless $self, $class;
51 52 return $self;
52 }
53  
54 ###########################################
55 70 ▶sub option_type_global {
Subroutine option_type_global: called CC=2 SCAR=7.6
Pod undocumented
56 ###########################################
57 70 my($self, $key) = @_;
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 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
69 ###########################################
70 2 my($self) = @_;
71  
72 return {
73 2 map { $_ => $self->option( $_ ) }
74 2 keys %{ $self->{ options } }
75 };
76 }
77  
78 ###########################################
79 70 ▶sub option {
Subroutine option: called CC=10 SCAR=23.1
Pod undocumented
80 ###########################################
81 70 my($self, $key, $value, $append) = @_;
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 15 ▶sub option_delete {
Subroutine option_delete: called CC=1 SCAR=0.0
Pod undocumented
127 ###########################################
128 15 my($self, $key) = @_;
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 69 ▶sub options_as_string {
Subroutine options_as_string: called CC=5 SCAR=16.1
Pod undocumented
140 ###########################################
141 69 my($self) = @_;
142  
143 69 my $string = "";
144 69 my @parts = ();
145  
146 69 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 69 return join(',', @parts);
160 }
161  
162 ###########################################
163 64 ▶sub option_quote {
Subroutine option_quote: called CC=1 SCAR=0.0
Pod undocumented
164 ###########################################
165 64 my($option, $text) = @_;
166  
167 64 $text =~ s/([\\"])/\\$1/g;
168 64 return "$option=\"" . $text . "\"";
169 }
170  
171 ###########################################
172 126 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.2
Pod undocumented
173 ###########################################
174 126 my($class, $string) = @_;
175  
176 126 DEBUG "Parsing line '$string'";
177  
178 # Clean up leading whitespace
179 126 $string =~ s/^\s+//;
180 126 $string =~ s/^#.*//;
181
182 126 ▶ if(! length $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $string) 0 126 126
183 0 DEBUG "Nothing to parse";
184 0 return;
185 }
186  
187 126 ▶ if(my $key = $class->key_read( $string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $class->key_read($string)) 34 92 126
188 # We found a key without options
189 34 $key->{options} = {};
190 34 DEBUG "Found ", $key->type(), " key: ", $key->as_string();
191 34 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 92 my $rc = (
198 (my $key_string = $string) =~
199 s/^((?:
200 (?:"(?:\\"|.)*?)"|
201 \S
202 )+
203 )
204 //x );
205 92 ▶ my $options_string = ($rc ? $1 : "");
Branch: 1 branch
BranchTrueFalseTotal
$rc ? : 92 0 92
206 92 $key_string =~ s/^\s+//;
207  
208 92 DEBUG "Trying line with options stripped: [$key_string]";
209  
210 92 ▶ if(my $key = $class->key_read( $key_string ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $class->key_read($key_string)) 18 74 92
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 74 DEBUG "$class cannot parse line: $string";
219  
220 74 return undef;
221 }
222  
223 ###########################################
224 18 ▶sub options_parse {
Subroutine options_parse: called CC=4 SCAR=13.9
Pod undocumented
225 ###########################################
226 18 my($self, $string) = @_;
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;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
1245
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
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 1 ▶sub fingerprint {
Subroutine fingerprint: called CC=1 SCAR=0.0
Pod undocumented
247 ###########################################
248 1 my($self) = @_;
249  
250 1 my $data = $self->options();
251  
252 0 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 96 ▶sub make_accessor {
Subroutine make_accessor: called CC=3 SCAR=11.8
Pod undocumented
262 ##################################################
263 96 my($package, $name) = @_;
264  
265 12 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
266  
267 96 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 96 ▶ if(! defined *{"$package\::$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined *{"${package}::$name";}) 96 0 96
282 0 ▶ eval $code or die "$@";
Subroutine __ANON__: called CC=3 SCAR=12.0
Subroutine __ANON__: called CC=3 SCAR=12.0
Subroutine __ANON__: not called CC=3 SCAR=12.0
Subroutine __ANON__: called CC=3 SCAR=12.0
Subroutine __ANON__: called CC=3 SCAR=12.0
Subroutine __ANON__: called CC=3 SCAR=12.0
Subroutine __ANON__: called CC=3 SCAR=12.0
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 17 branches
BranchTrueFalseTotal
eval $code or die "$@" 96 0 96
if (defined $value) 53 15 68
if (exists $self->{'key'}) { } 68 0 68
if (defined $value) 0 61 61
if (exists $self->{'type'}) { } 61 0 61
if (defined $value) 0 0 0
if (exists $self->{'error'}) { } 0 0 0
if (defined $value) 52 16 68
if (exists $self->{'email'}) { } 68 0 68
if (defined $value) 52 2 54
if (exists $self->{'comment'}) { } 54 0 54
if (defined $value) 18 7 25
if (exists $self->{'keylen'}) { } 25 0 25
if (defined $value) 17 5 22
if (exists $self->{'exponent'}) { } 22 0 22
if (defined $value) 35 5 40
if (exists $self->{'encryption'}) { } 40 0 40
283 }
284 }
285  
286 12 1;
287  
288 __END__
« AuthorizedKeysFile.pm ↑ Summary AuthorizedKey/SSH1.pm »