AuthorizedKey.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 34 / 34 CC 3 SCAR 5.6
CC
3
Cov
100%
parse
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
« AuthorizedKey/SSH1.pm ↑ Summary
1 ###########################################
2 package Net::SSH::AuthorizedKey;
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  
8 11 ▶use Net::SSH::AuthorizedKey::SSH1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Net::SSH::AuthorizedKey::SSH2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ###########################################
12 sub parse {
13 ###########################################
14 67 ▶ my($class, $string) = @_;
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
15  
16 67 my @subclasses = qw(
17 Net::SSH::AuthorizedKey::SSH1
18 Net::SSH::AuthorizedKey::SSH2
19 );
20  
21 67 for my $subclass ( @subclasses ) {
22 120 DEBUG "Parsing with $subclass: $string";
23 120 my $pk = $subclass->parse( $string );
24 120 ▶ if($pk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk) 46 74 120
25 46 DEBUG "Successfully parsed $subclass key";
26 46 return $pk;
27 }
28 }
29  
30 21 return undef;
31 }
32  
33 11 1;
34  
35 __END__
36  
37 =head1 NAME
38  
39 Net::SSH::AuthorizedKey - Virtual Base Class for SSH Public Keys
40  
41 =head1 SYNOPSIS
42  
43 use Net::SSH::AuthorizedKey;
44  
45 # Either parse a string (without leading whitespace or comments):
46 my $key = Net::SSH::AuthorizedKey->parse( $line );
47  
48 if(defined $key) {
49 # ssh-1 or ssh-2
50 print "Key parsed, type is ", $key->type(), "\n";
51 } else {
52 die "Cannot parse key '$line'";
53 }
54  
55 # ... or create an object yourself:
56 my $pubkey = Net::SSH::AuthorizedKey->new(
57 options => { from => 'foo@bar.com',
58 "no-agent-forwarding" => 1 },
59 key => "123....890",
60 keylen => 1024,
61 exponent => 35,
62 type => "ssh-1",
63 );
64  
65 =head1 DESCRIPTION
66  
67 Net::SSH::AuthorizedKey is a virtual base class for ssh public keys.
68 Real implementations of it are Net::SSH::AuthorizedKey::SSH1 and
69 Net::SSH::AuthorizedKey::SSH2.
70  
71 The only way to using it directly is by calling its parse() method, and passing
72 it an authorized_keys string (aka a line from an authorized_keys file). If it
73 recognizes either a ssh-1 or a ssh-2 type key, it will return a
74 Net::SSH::AuthorizedKey::SSH1 or a Net::SSH::AuthorizedKey::SSH2 object, both
75 of which support the accessor methods defined in the FIELDS section below.
76  
77 The as_string() method will cobble the (perhaps modified) fields together
78 and return them as a string suitable as a line for an authorized_keys file.
79  
80 =head2 METHODS
81  
82 =over 4
83  
84 =item C<parse( $line )>
85  
86 Reads in a single text line containing a ssh-1 or ssh-2 key.
87 Returns a Net::SSH::AuthorizedKey::SSH1 or a Net::SSH::AuthorizedKey::SSH2
88 object, or C<undef> in case of an error.
89  
90 =item C<fingerprint()>
91  
92 Returns a MD5 hex hash of the parsed key. The hash is unique for functionally
93 identical keys. Fields not contributing to the key's functional uniqueness
94 are ignored.
95  
96 =item C<error()>
97  
98 Returns the last parsing error encountered as a text string.
99  
100 =item C<as_string( )>
101  
102 Return the object as a string suitable as a autorized_keys line.
103  
104 =back
105  
106 =head2 FIELDS
107  
108 All of the following fields are available via accessors:
109  
110 =over 4
111  
112 =item C<type>
113  
114 Type of ssh key, usually C<"ssh-1"> or C<"ssh-2">.
115  
116 =item C<key>
117  
118 Public key, either a long number (ssh-1) or a line of alphanumeric
119 characters (ssh-2).
120  
121 =item C<keylen>
122  
123 Length of the key in bit (e.g. 1024).
124  
125 =item C<exponent>
126  
127 Two-digit number in front of the key in ssh-1 authorized_keys lines.
128  
129 =item C<options>
130  
131 Returns a reference to a hash with options key/value pairs, listed in
132 front of the key.
133  
134 =back
135  
136 =head2 IMPLEMENTATION REFERENCE
137  
138 The key parsers implemented in this distribution are implemented similarily
139 as the authorized_keys file parser in the openssh source distribution.
140  
141 Openssh contains the authorized_keys parser
142 in its auth2_pubkey.c file. The user_key_allowed2() function opens
143 the file and reads it line by line, ignoring leading whitespace, empty
144 and comment lines.
145  
146 After that, if a line doesn't contain a plain key, the parser skips ahead until
147 the first whitespace (zooming through quoted areas "..." and interpreting '\"'
148 as an escaped quote), then skips this whitespace and tries to read a key one
149 more time.
150  
151 Regarding options, the Perl parser isn't as elaborate with semantic
152 peculiarities as openssh's auth_parse_options(), but this might be
153 added in the future.
154  
155 =head1 NOTES FOR SUBCLASS DEVELOPERS
156  
157 If you're just using Net::SSH::AuthorizedKey to parse keys, the
158 following section doesn't concern you. It's only relevant if you add
159 new subclasses to this package, on top of what's already provided.
160  
161 Net::SSH::AuthorizedKey is a (semi-)virtual base class that implements
162 options handling for its SSH1 and SSH2 subclasses.
163  
164 SSH key lines can contain options that carry values (like command="ls") and
165 binary options that are either set or unset (like "no_agent_forwarding"). To
166 distinguish the two, and to provide a set of allowed option names, the subclass
167 has to implement the method option_type(), which takes an option name, and
168 returns
169  
170 =over 4
171  
172 =item *
173  
174 undef if the option is not supported
175  
176 =item *
177  
178 "s" if the option is a "string" option that carries a value
179  
180 =item *
181  
182 1 if the option is a binary option
183  
184 =back
185  
186 The subclasses Net::SSH::AuthorizedKey::SSH1 and Net::SSH::AuthorizedKey::SSH2
187 are doing this already.
188  
189 =head1 LEGALESE
190  
191 Copyright 2005-2009 by Mike Schilli, all rights reserved.
192 This program is free software, you can redistribute it and/or
193 modify it under the same terms as Perl itself.
194  
195 =head1 AUTHOR
196  
197 2005, Mike Schilli <m@perlmeister.com>
« AuthorizedKey/SSH1.pm ↑ Summary