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