Authkey/Entry.pm

filter: Statementstmt 96.4% 135 / 140 Branchbran 89.0% 57 / 64 Conditioncond 70.9% 22 / 31 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 100% 22 / 22 Podpod 100% 16 / 16 totaltotal 89.7% 263 / 293 CC 44 SCAR 20.4
CC
44
Cov
91%
__ANON__
18.0
as_string
8.6
key
5.9
CRAP 7.7
↑ Summary Authkey/Entry/Options.pm »
1 # -*- Perl -*-
2 #
3 # representation of individual OpenSSH authorized_keys entries, based on
4 # a study of the sshd(8) manual, along with the OpenSSH 5.2 sources.
5 # this module only weakly validates the data; in particular, no effort
6 # is made to confirm whether the key options are actual valid options
7 # for the version of OpenSSH in question
8  
9 package Config::OpenSSH::Authkey::Entry;
10  
11 2 ▶use 5.006000;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Config::OpenSSH::Authkey::Entry::Options ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = '1.06';
20  
21 # This limit is set for various things under OpenSSH code. Used here to
22 # limit length of authorized_keys lines.
23 2 my $MAX_PUBKEY_BYTES = 8192;
24  
25 # Sanity check to ensure at least some data exists in the key field
26 2 my $MIN_KEY_LENGTH = 42;
27  
28 ######################################################################
29 #
30 # Data Parsing & Utility Methods - Internal
31  
32 28 ▶my $_parse_entry = sub {
Subroutine __ANON__: called CC=18 SCAR=28.9
33 28 my $self = shift;
34 28 ▶ my $data = shift || q{};
Condition50%1/2
shift() || ''
l!l
280
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
35  
36 28 my ( $options, $key, $comment, $protocol, $keytype );
37  
38 28 chomp $data;
39  
40 28 ▶ if ( $data =~ m/^\s*$/ or $data =~ m/^\s*#/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^\s*$/ or $data =~ /^\s*#/) { } 1 27 28
elsif (length $data >= $MAX_PUBKEY_BYTES) { } 1 26 27
Condition66%2/3
$data =~ /^\s*$/ or $data =~ /^\s*#/
l!l&&r!l&&!r
0127
MC/DC50%1/2
$data =~ /^\s*$/ or $data =~ /^\s*#/
$data =~ /^\s*$/ $data =~ /^\s*#/
Truth table66%2/3
A or B
A= $data =~ /^\s*$/B= $data =~ /^\s*#/
ABresult
1X1
011
000
41 1 return ( 0, 'no public key data' );
42 } elsif ( length $data >= $MAX_PUBKEY_BYTES ) {
43 1 return ( 0, 'exceeds size limit' );
44 }
45  
46 # OpenSSH supports leading whitespace before options or key. Strip
47 # this optional whitespace to simplify parsing.
48 26 $data =~ s/^[ \t]+//;
49  
50 ENTRY_LEXER: {
51 # Optional trailing comment (user@host, usually)
52 26 ▶ if ( defined $key and $data =~ m/ \G (.+) /cgx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and $data =~ / \G (.+) /cgx) 6 349 355
Condition100%3/3
defined $key and $data =~ / \G (.+) /cgx
!ll&&!rl&&r
330196
MC/DC100%2/2
defined $key and $data =~ / \G (.+) /cgx
defined $key $data =~ / \G (.+) /cgx
Truth table100%3/3
A and B
A= defined $keyB= $data =~ / \G (.+) /cgx
ABresult
0X0
100
111
53 6 $comment = $1;
54  
55 6 last ENTRY_LEXER;
56 }
57  
58 # SSH2 public keys
59 349 ▶ if ( !defined $key
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key and $data =~ m[ \G ( (ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp256) [ \t]+? [A-Za-z0-9+/]+ =* ) [ \t]* ]cgx) 19 330 349
Condition100%3/3
not defined $key and $data =~ m[ \G ( (ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp256) [ \t]+? [A-Za-z0-9+/]+ =* ) [ \t]* ]cgx
!ll&&!rl&&r
1931119
MC/DC100%2/2
not defined $key and $data =~ m[ \G ( (ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp256) [ \t]+? [A-Za-z0-9+/]+ =* ) [ \t]* ]cgx
not defined $key $data =~ m[ \G ( (ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp256) [ \t]+? [A-Za-z0-9+/]+ =* ) [ \t]* ]cgx
Truth table100%3/3
not(A) and B
A= not defined $keyB= $data =~ m[ \G ( (ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp256) [ \t]+? [A-Za-z0-9+/]+ =* ) [ \t]* ]cgx
ABresult
0X0
100
111
60 and $data =~
61 m/ \G ( (ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp256) [ \t]+? [A-Za-z0-9+\/]+ =* ) [ \t]* /cgx
62 ) {
63  
64 19 $key = $1;
65 19 my $type = $2;
66 19 my $subtype = $3;
67 # follow the -t argument option to ssh-keygen(1)
68 19 ▶ if ( $type =~ m/^ssh-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^ssh-/) { } 17 2 19
69 17 ▶ if ( $subtype eq 'dss' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtype eq 'dss') { } 6 11 17
70 6 $keytype = 'dsa';
71 } else {
72 11 $keytype = $subtype;
73 }
74 } else {
75 2 $keytype = 'ecdsa';
76 }
77 19 $protocol = 2;
78  
79 19 redo ENTRY_LEXER;
80 }
81  
82 # SSH1 RSA public key
83 330 ▶ if ( !defined $key
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key and $data =~ / \G ( \d{3,5} [ \t]+? \d+ [ \t]+? \d+ ) [ \t]* /cgx) 6 324 330
Condition100%3/3
not defined $key and $data =~ / \G ( \d{3,5} [ \t]+? \d+ [ \t]+? \d+ ) [ \t]* /cgx
!ll&&!rl&&r
193056
MC/DC100%2/2
not defined $key and $data =~ / \G ( \d{3,5} [ \t]+? \d+ [ \t]+? \d+ ) [ \t]* /cgx
not defined $key $data =~ / \G ( \d{3,5} [ \t]+? \d+ [ \t]+? \d+ ) [ \t]* /cgx
Truth table100%3/3
not(A) and B
A= not defined $keyB= $data =~ / \G ( \d{3,5} [ \t]+? \d+ [ \t]+? \d+ ) [ \t]* /cgx
ABresult
0X0
100
111
84 and $data =~ m/ \G ( \d{3,5} [ \t]+? \d+ [ \t]+? \d+ ) [ \t]* /cgx ) {
85  
86 6 $key = $1;
87 6 $keytype = 'rsa1';
88 6 $protocol = 1;
89  
90 6 redo ENTRY_LEXER;
91 }
92  
93 # Optional leading options - may contain whitespace inside ""
94 324 ▶ if ( !defined $key and $data =~ m/ \G ([^ \t]+? [ \t]*) /cgx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key and $data =~ / \G ([^ \t]+? [ \t]*) /cgx) 304 20 324
Condition100%3/3
not defined $key and $data =~ / \G ([^ \t]+? [ \t]*) /cgx
!ll&&!rl&&r
191304
MC/DC100%2/2
not defined $key and $data =~ / \G ([^ \t]+? [ \t]*) /cgx
not defined $key $data =~ / \G ([^ \t]+? [ \t]*) /cgx
Truth table100%3/3
not(A) and B
A= not defined $keyB= $data =~ / \G ([^ \t]+? [ \t]*) /cgx
ABresult
0X0
100
111
95 304 $options .= $1;
96  
97 304 redo ENTRY_LEXER;
98 }
99 }
100  
101 26 ▶ if ( !defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key) { } 1 25 26
102 1 return ( 0, 'unable to parse public key' );
103  
104 } else {
105 25 $self->{_key} = $key;
106 25 $self->{_protocol} = $protocol;
107 25 $self->{_keytype} = $keytype;
108  
109 25 ▶ if ( defined $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) 6 19 25
110 6 $options =~ s/\s*$//;
111 6 $self->{_options} = $options;
112 }
113  
114 25 ▶ if ( defined $comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 6 19 25
115 6 $comment =~ s/\s*$//;
116 6 $self->{_comment} = $comment;
117 }
118 }
119  
120 25 return ( 1, 'ok' );
121 2 };
122  
123 ######################################################################
124 #
125 # Class methods
126  
127 28 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
128 28 my $class = shift;
129 28 my $data = shift;
130  
131 28 my $self = { _dup_of => 0 };
132  
133 28 ▶ if ( defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 22 6 28
134 22 my ( $is_parsed, $err_msg ) = $_parse_entry->( $self, $data );
135 22 ▶ if ( !$is_parsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_parsed) 3 19 22
136 3 croak $err_msg;
137 }
138 }
139  
140 25 bless $self, $class;
141 25 return $self;
142 }
143  
144 1 ▶sub split_options {
Subroutine split_options: called CC=1 SCAR=0.0
Pod documented
145 1 my $class = shift;
146 1 Config::OpenSSH::Authkey::Entry::Options->split_options(@_);
147 }
148  
149 ######################################################################
150 #
151 # Instance methods
152  
153 3 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.8
Pod documented
154 3 my $self = shift;
155 3 ▶ my $data = shift || croak 'no data supplied to parse';
Condition50%1/2
shift() || croak('no data supplied to parse')
l!l
30
MC/DC0%0/1
shift() || croak('no data supplied to parse')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
156  
157 3 my ( $is_parsed, $err_msg ) = $_parse_entry->( $self, $data );
158 3 ▶ if ( !$is_parsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_parsed) 0 3 3
159 0 croak $err_msg;
160 }
161  
162 3 return $self;
163 }
164  
165 19 ▶sub as_string {
Subroutine as_string: called CC=8 SCAR=21.6
Pod documented
166 19 my $self = shift;
167 19 my $string = q{};
168  
169 19 ▶ if ( exists $self->{_parsed_options} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'_parsed_options'}) { } 1 18 19
elsif (exists $self->{'_options'} and length $self->{'_options'} > 0) { } 9 18 27
170 1 $string .= $self->{_parsed_options}->as_string . q{ };
171  
172 ▶ } elsif ( exists $self->{_options} and length $self->{_options} > 0 ) {
Condition66%2/3
exists $self->{"_options"} and length $self->{"_options"} > 0
!ll&&!rl&&r
909
MC/DC50%1/2
exists $self->{"_options"} and length $self->{"_options"} > 0
exists $self->{"_options"} length $self->{"_options"} > 0
Truth table66%2/3
A and B
A= exists $self->{"_options"}B= length $self->{"_options"} > 0
ABresult
0X0
100
111
173 9 $string .= $self->{_options} . q{ };
174 }
175  
176 19 ▶ if ( !defined $self->{_key} or length $self->{_key} < $MIN_KEY_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_key"} or length $self->{"_key"} < $MIN_KEY_LENGTH) 0 19 19
Condition33%1/3
not defined $self->{"_key"} or length $self->{"_key"} < $MIN_KEY_LENGTH
l!l&&r!l&&!r
0019
MC/DC0%0/2
not defined $self->{"_key"} or length $self->{"_key"} < $MIN_KEY_LENGTH
not defined $self->{"_key"} length $self->{"_key"} < $MIN_KEY_LENGTH
Truth table33%1/3
not(A) or B
A= not defined $self->{"_key"}B= length $self->{"_key"} < $MIN_KEY_LENGTH
ABresult
1X1
011
000
177 0 croak 'no key material present';
178 }
179 19 $string .= $self->{_key};
180  
181 19 ▶ if ( exists $self->{_comment} and length $self->{_comment} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_comment"} and length $self->{"_comment"} > 0) 9 10 19
Condition66%2/3
exists $self->{"_comment"} and length $self->{"_comment"} > 0
!ll&&!rl&&r
1009
MC/DC50%1/2
exists $self->{"_comment"} and length $self->{"_comment"} > 0
exists $self->{"_comment"} length $self->{"_comment"} > 0
Truth table66%2/3
A and B
A= exists $self->{"_comment"}B= length $self->{"_comment"} > 0
ABresult
0X0
100
111
182 9 $string .= q{ } . $self->{_comment};
183 }
184  
185 19 return $string;
186 }
187  
188 20 ▶sub key {
Subroutine key: called CC=5 SCAR=17.8
Pod documented
189 20 my $self = shift;
190 20 my $key = shift;
191 20 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 3 17 20
192 3 my ( $is_parsed, $err_msg ) = $_parse_entry->( $self, $key );
193 3 ▶ if ( !$is_parsed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_parsed) 0 3 3
194 0 croak $err_msg;
195 }
196 }
197 20 ▶ if ( !defined $self->{_key} or length $self->{_key} < $MIN_KEY_LENGTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_key"} or length $self->{"_key"} < $MIN_KEY_LENGTH) 0 20 20
Condition33%1/3
not defined $self->{"_key"} or length $self->{"_key"} < $MIN_KEY_LENGTH
l!l&&r!l&&!r
0020
MC/DC0%0/2
not defined $self->{"_key"} or length $self->{"_key"} < $MIN_KEY_LENGTH
not defined $self->{"_key"} length $self->{"_key"} < $MIN_KEY_LENGTH
Truth table33%1/3
not(A) or B
A= not defined $self->{"_key"}B= length $self->{"_key"} < $MIN_KEY_LENGTH
ABresult
1X1
011
000
198 0 croak 'no key material present';
199 }
200 20 return $self->{_key};
201 }
202  
203 3 ▶sub protocol {
Subroutine protocol: called CC=2 SCAR=7.6
Pod documented
204 3 ▶ shift->{_protocol} || 0;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"_protocol"} or 0 3 0 3
205 }
206  
207 3 ▶sub keytype {
Subroutine keytype: called CC=2 SCAR=7.6
Pod documented
208 3 ▶ shift->{_keytype} || '';
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"_keytype"} or "" 3 0 3
209 }
210  
211 18 ▶sub comment {
Subroutine comment: called CC=3 SCAR=11.0
Pod documented
212 18 my $self = shift;
213 18 my $comment = shift;
214 18 ▶ if ( defined $comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 3 15 18
215 3 $self->{_comment} = $comment;
216 }
217 18 ▶ return defined $self->{_comment} ? $self->{_comment} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_comment'} ? : 9 9 18
218 }
219  
220 3 ▶sub unset_comment {
Subroutine unset_comment: called CC=1 SCAR=0.0
Pod documented
221 3 my $self = shift;
222 3 delete $self->{_comment};
223 3 return 1;
224 }
225  
226 # The leading (optional!) options can be dealt with as a string
227 # (options, unset_options), or if parsed, as individual options
228 # (get_option, set_option, unset_option).
229  
230 26 ▶sub options {
Subroutine options: called CC=4 SCAR=13.9
Pod documented
231 26 my $self = shift;
232 26 my $new_options = shift;
233  
234 26 ▶ if ( defined $new_options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_options) 4 22 26
235 4 delete $self->{_parsed_options};
236 4 $self->{_options} = $new_options;
237 }
238  
239 my $options_str =
240 exists $self->{_parsed_options}
241 ? $self->{_parsed_options}->as_string
242 26 ▶ : $self->{_options};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_parsed_options'} ? : 5 21 26
243 26 ▶ return defined $options_str ? $options_str : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $options_str ? : 16 10 26
244 }
245  
246 3 ▶sub unset_options {
Subroutine unset_options: called CC=1 SCAR=0.0
Pod documented
247 3 my $self = shift;
248 3 delete $self->{_parsed_options};
249 3 delete $self->{_options};
250 3 return 1;
251 }
252  
253 5 ▶sub get_option {
Subroutine get_option: called CC=2 SCAR=6.9
Pod documented
254 5 my $self = shift;
255  
256 5 ▶ if ( !exists $self->{_parsed_options} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_parsed_options"}) 1 4 5
257 $self->{_parsed_options} =
258 1 Config::OpenSSH::Authkey::Entry::Options->new( $self->{_options} );
259 }
260  
261 5 $self->{_parsed_options}->get_option(@_);
262 }
263  
264 4 ▶sub set_option {
Subroutine set_option: called CC=2 SCAR=6.9
Pod documented
265 4 my $self = shift;
266  
267 4 ▶ if ( !exists $self->{_parsed_options} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_parsed_options"}) 1 3 4
268 $self->{_parsed_options} =
269 1 Config::OpenSSH::Authkey::Entry::Options->new( $self->{_options} );
270 }
271  
272 4 $self->{_parsed_options}->set_option(@_);
273 }
274  
275 3 ▶sub unset_option {
Subroutine unset_option: called CC=2 SCAR=7.6
Pod documented
276 3 my $self = shift;
277  
278 3 ▶ if ( !exists $self->{_parsed_options} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_parsed_options"}) 0 3 3
279 $self->{_parsed_options} =
280 0 Config::OpenSSH::Authkey::Entry::Options->new( $self->{_options} );
281 }
282  
283 3 $self->{_parsed_options}->unset_option(@_);
284 }
285  
286 12 ▶sub duplicate_of {
Subroutine duplicate_of: called CC=2 SCAR=6.9
Pod documented
287 12 my $self = shift;
288 12 my $ref = shift;
289  
290 12 ▶ if ( defined $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) 2 10 12
291 2 $self->{_dup_of} = $ref;
292 }
293  
294 12 return $self->{_dup_of};
295 }
296  
297 1 ▶sub unset_duplicate {
Subroutine unset_duplicate: called CC=1 SCAR=0.0
Pod documented
298 1 my $self = shift;
299 1 $self->{_dup_of} = 0;
300 1 return 1;
301 }
302  
303 2 1;
304  
305 __END__
↑ Summary Authkey/Entry/Options.pm »