| 1 | ########################################### | ||||||||||||||||||||||||||||
| 2 | package Net::SSH::AuthorizedKeysFile; | ||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Text::ParseWords; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Net::SSH::AuthorizedKey; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Net::SSH::AuthorizedKey::SSH1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Net::SSH::AuthorizedKey::SSH2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | our $VERSION = "0.17"; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ########################################### | ||||||||||||||||||||||||||||
| 15 | sub new { | ||||||||||||||||||||||||||||
| 16 | ########################################### | ||||||||||||||||||||||||||||
| 17 | 23 | ▶ | my($class, @options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 23 | my $self = { | |||||||||||||||||||||||||||
| 20 | default_file => "$ENV{HOME}/.ssh/authorized_keys", | ||||||||||||||||||||||||||||
| 21 | strict => 0, | ||||||||||||||||||||||||||||
| 22 | abort_on_error => 0, | ||||||||||||||||||||||||||||
| 23 | append => 0, | ||||||||||||||||||||||||||||
| 24 | ridiculous_line_len => 100_000, | ||||||||||||||||||||||||||||
| 25 | @options, | ||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 23 | bless $self, $class; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # We allow keys to be set in the constructor | ||||||||||||||||||||||||||||
| 31 | 23 | ▶ | my $keys = $self->{keys} if exists $self->{keys}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 23 | $self->reset(); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 23 | ▶ | $self->{keys} = $keys if defined $keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 23 | return $self; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | ########################################### | ||||||||||||||||||||||||||||
| 41 | sub sanity_check { | ||||||||||||||||||||||||||||
| 42 | ########################################### | ||||||||||||||||||||||||||||
| 43 | 3 | ▶ | my($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
sanity_check: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | $self->{file} = $file if defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | $self->{file} = $self->{default_file} if !defined $self->{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | my $result = undef; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 3 | my $fh; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | if(! open $fh, "<$self->{file}") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | ERROR "Cannot open file $self->{file}"; | |||||||||||||||||||||||||||
| 54 | 0 | return undef; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | while( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | defined(my $rc = | ||||||||||||||||||||||||||||
| 59 | sysread($fh, my $chunk, $self->{ridiculous_line_len}))) { | ||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if($rc < $self->{ridiculous_line_len}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 2 | $result = 1; | |||||||||||||||||||||||||||
| 62 | 2 | last; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if(index( $chunk, "\n" ) >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | # contains a newline, looks good | ||||||||||||||||||||||||||||
| 67 | 1 | next; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # we've got a line that's between ridiculous_line_len and | ||||||||||||||||||||||||||||
| 71 | # 2*ridiculous_line_len characters long. Pull the plug. | ||||||||||||||||||||||||||||
| 72 | 1 | $self->error("File $self->{file} contains insanely long lines " . | |||||||||||||||||||||||||||
| 73 | "(> $self->{ridiculous_line_len} chars"); | ||||||||||||||||||||||||||||
| 74 | 1 | last; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | DONE: | ||||||||||||||||||||||||||||
| 78 | 3 | close $fh; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | if(!$result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | ERROR "Sanity check of file $self->{file} failed"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 3 | return $result; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | ########################################### | ||||||||||||||||||||||||||||
| 87 | sub keys { | ||||||||||||||||||||||||||||
| 88 | ########################################### | ||||||||||||||||||||||||||||
| 89 | 16 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 16 | return @{$self->{keys}}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | ########################################### | ||||||||||||||||||||||||||||
| 95 | sub reset { | ||||||||||||||||||||||||||||
| 96 | ########################################### | ||||||||||||||||||||||||||||
| 97 | 45 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 45 | $self->{keys} = []; | |||||||||||||||||||||||||||
| 100 | 45 | $self->{content} = ""; | |||||||||||||||||||||||||||
| 101 | 45 | $self->{error} = undef; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ########################################### | ||||||||||||||||||||||||||||
| 105 | sub content { | ||||||||||||||||||||||||||||
| 106 | ########################################### | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my($self, $new_content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
content: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if( defined $new_content ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $self->reset(); | |||||||||||||||||||||||||||
| 111 | 0 | $self->{content} = $new_content; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | return $self->{content}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | ########################################### | ||||||||||||||||||||||||||||
| 118 | sub line_parse { | ||||||||||||||||||||||||||||
| 119 | ########################################### | ||||||||||||||||||||||||||||
| 120 | 55 | ▶ | my($self, $line, $line_number) = @_; | ||||||||||||||||||||||||||
|
Subroutine
line_parse: called CC=5 SCAR=18.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 55 | chomp $line; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 55 | DEBUG "Parsing line [$line]"; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 55 | $self->error( "" ); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 55 | my $pk = Net::SSH::AuthorizedKey->parse( $line ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 55 | ▶ | if( !$pk ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 12 | my $msg = "[$line] rejected by all parsers"; | |||||||||||||||||||||||||||
| 132 | 12 | WARN $msg; | |||||||||||||||||||||||||||
| 133 | 12 | $self->error($msg); | |||||||||||||||||||||||||||
| 134 | 12 | return undef; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 43 | ▶ | if(! $self->{strict} or $pk->sanity_check()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $self->{"strict"} or $pk->sanity_check
MC/DC0%0/2
not $self->{"strict"} or $pk->sanity_check
not $self->{"strict"}
$pk->sanity_check
Truth table33%1/3
not(A) or B
A= not $self->{"strict"}B= $pk->sanity_check
| |||||||||||||||||||||||||||||
| 138 | 43 | return $pk; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | WARN "Key [$line] failed sanity check"; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if($self->{strict}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | $self->error( $pk->error() ); | |||||||||||||||||||||||||||
| 145 | 0 | return undef; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Key is corrupted, but ok in non-strict mode | ||||||||||||||||||||||||||||
| 149 | 0 | return $pk; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | ########################################### | ||||||||||||||||||||||||||||
| 153 | sub parse { | ||||||||||||||||||||||||||||
| 154 | ########################################### | ||||||||||||||||||||||||||||
| 155 | 22 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 22 | $self->{keys} = []; | |||||||||||||||||||||||||||
| 158 | 22 | $self->{error} = ""; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 22 | my $line_number = 0; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 22 | for my $line (split /\n/, $self->{content}) { | |||||||||||||||||||||||||||
| 163 | 63 | $line_number++; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 63 | $line =~ s/^\s+//; # Remove leading blanks | |||||||||||||||||||||||||||
| 166 | 63 | $line =~ s/\s+$//; # Remove trailing blanks | |||||||||||||||||||||||||||
| 167 | 63 | ▶ | next if $line =~ /^$/; # Ignore empty lines | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 60 | ▶ | next if $line =~ /^#/; # Ignore comment lines | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 55 | my $key = $self->line_parse($line, $line_number); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 55 | ▶ | if( defined $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 43 | push @{$self->{keys}}, $key; | |||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||
| 175 | 12 | ▶ | if($self->{abort_on_error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 2 | $self->error("Line $line_number: " . $self->error()); | |||||||||||||||||||||||||||
| 177 | 2 | return undef; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 20 | return 1; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | ########################################### | ||||||||||||||||||||||||||||
| 186 | sub read { | ||||||||||||||||||||||||||||
| 187 | ########################################### | ||||||||||||||||||||||||||||
| 188 | 22 | ▶ | my($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 22 | $self->reset(); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 22 | ▶ | $self->{file} = $file if defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 22 | ▶ | $self->{file} = $self->{default_file} if !defined $self->{file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 22 | $self->{content} = ""; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 22 | DEBUG "Reading in $self->{file}"; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 22 | ▶ | open FILE, "<$self->{file}" or LOGDIE "Cannot open $self->{file}"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 22 | ▶ | while(<FILE>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 63 | $self->{content} .= $_; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 22 | close FILE; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 22 | return $self->parse(); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | ########################################### | ||||||||||||||||||||||||||||
| 210 | sub as_string { | ||||||||||||||||||||||||||||
| 211 | ########################################### | ||||||||||||||||||||||||||||
| 212 | 5 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 5 | my $string = ""; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 5 | for my $key ( @{ $self->{keys} } ) { | |||||||||||||||||||||||||||
| 217 | 14 | $string .= $key->as_string . "\n"; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 5 | return $string; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | ########################################### | ||||||||||||||||||||||||||||
| 224 | sub save { | ||||||||||||||||||||||||||||
| 225 | ########################################### | ||||||||||||||||||||||||||||
| 226 | 4 | ▶ | my($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=3 SCAR=12.6
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 4 | ▶ | if(!defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 4 | $file = $self->{file}; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 4 | ▶ | if(! open FILE, ">$file") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 0 | $self->error("Cannot open $file ($!)"); | |||||||||||||||||||||||||||
| 234 | 0 | WARN $self->error(); | |||||||||||||||||||||||||||
| 235 | 0 | return undef; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 4 | print FILE $self->as_string(); | |||||||||||||||||||||||||||
| 239 | 4 | close FILE; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | ########################################### | ||||||||||||||||||||||||||||
| 243 | sub append { | ||||||||||||||||||||||||||||
| 244 | ########################################### | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
append: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | $self->{append} = 1; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | ########################################### | ||||||||||||||||||||||||||||
| 251 | sub error { | ||||||||||||||||||||||||||||
| 252 | ########################################### | ||||||||||||||||||||||||||||
| 253 | 74 | ▶ | my($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 74 | ▶ | if(defined $text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 70 | $self->{error} = $text; | |||||||||||||||||||||||||||
| 257 | 70 | ERROR "$text"; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 74 | return $self->{error}; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | ########################################### | ||||||||||||||||||||||||||||
| 264 | sub ssh_dir { | ||||||||||||||||||||||||||||
| 265 | ########################################### | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my($self, $user) = @_; | ||||||||||||||||||||||||||
|
Subroutine
ssh_dir: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if(!defined $user) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | my $uid = $>; | |||||||||||||||||||||||||||
| 270 | 0 | $user = getpwuid($uid); | |||||||||||||||||||||||||||
| 271 | 0 | ▶ | if(!defined $user) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | ERROR "getpwuid of $uid failed ($!)"; | |||||||||||||||||||||||||||
| 273 | 0 | return undef; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | my @pwent = getpwnam($user); | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 0 | ▶ | if(! defined $pwent[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 0 | ERROR "getpwnam of $user failed ($!)"; | |||||||||||||||||||||||||||
| 281 | 0 | return undef; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | my $home = $pwent[7]; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | return File::Spec->catfile($home, ".ssh"); | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | ########################################### | ||||||||||||||||||||||||||||
| 290 | sub path_locate { | ||||||||||||||||||||||||||||
| 291 | ########################################### | ||||||||||||||||||||||||||||
| 292 | 0 | ▶ | my($self, $user) = @_; | ||||||||||||||||||||||||||
|
Subroutine
path_locate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | my $ssh_dir = $self->ssh_dir($user); | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | return undef if !defined $ssh_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | return File::Spec->catfile($ssh_dir, "authorized_keys"); | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 7 | 1; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | __END__ | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head1 NAME | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Net::SSH::AuthorizedKeysFile - Read and modify ssh's authorized_keys files | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | use Net::SSH::AuthorizedKeysFile; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # Reads $HOME/.ssh/authorized_keys by default | ||||||||||||||||||||||||||||
| 314 | my $akf = Net::SSH::AuthorizedKeysFile->new(); | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | $akf->read("authorized_keys"); | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # Iterate over entries | ||||||||||||||||||||||||||||
| 319 | for my $key ($akf->keys()) { | ||||||||||||||||||||||||||||
| 320 | print $key->as_string(), "\n"; | ||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # Modify entries: | ||||||||||||||||||||||||||||
| 324 | for my $key ($akf->keys()) { | ||||||||||||||||||||||||||||
| 325 | $key->option("from", 'quack@quack.com'); | ||||||||||||||||||||||||||||
| 326 | $key->keylen(1025); | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | # Save changes back to $HOME/.ssh/authorized_keys | ||||||||||||||||||||||||||||
| 329 | $akf->save() or die "Cannot save"; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | Net::SSH::AuthorizedKeysFile reads and modifies C<authorized_keys> files. | ||||||||||||||||||||||||||||
| 334 | C<authorized_keys> files contain public keys and meta information to | ||||||||||||||||||||||||||||
| 335 | be used by C<ssh> on the remote host to let users in without | ||||||||||||||||||||||||||||
| 336 | having to type their password. | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head1 METHODS | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =over 4 | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =item C<new> | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | Creates a new Net::SSH::AuthorizedKeysFile object and reads in the | ||||||||||||||||||||||||||||
| 345 | authorized_keys file. The filename | ||||||||||||||||||||||||||||
| 346 | defaults to C<$HOME/.ssh/authorized_keys> unless | ||||||||||||||||||||||||||||
| 347 | overridden with | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | Net::SSH::AuthorizedKeysFile->new( file => "/path/other_authkeys_file" ); | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Normally, the C<read> method described below will just silently ignore | ||||||||||||||||||||||||||||
| 352 | faulty lines and only gobble up keys that either one of the two parsers | ||||||||||||||||||||||||||||
| 353 | accepts. If you want it to be stricter, set | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | Net::SSH::AuthorizedKeysFile->new( file => "authkeys_file", | ||||||||||||||||||||||||||||
| 356 | abort_on_error => 1 ); | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | and read will immediately abort after the first faulty line. Also, | ||||||||||||||||||||||||||||
| 359 | the key parsers are fairly lenient in default mode. Adding | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | strict => 1 | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | adds sanity checks before a key is accepted. | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =item C<read> | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | Reads in the file defined by new(). By default, strict mode is off and | ||||||||||||||||||||||||||||
| 368 | read() will silently ignore faulty lines. If it's on (see new() above), | ||||||||||||||||||||||||||||
| 369 | read() will immediately abort after the first faulty line. A textual | ||||||||||||||||||||||||||||
| 370 | description of the last error will be available via error(). | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =item C<content> | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | Contains the original file content, read by C<read()> earlier. Can be | ||||||||||||||||||||||||||||
| 375 | used to set arbitrary content: | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | $keysfile->content( "some\nrandom\nlines\n" ); | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | and have C<parse()> operate on a string instead of an actual file | ||||||||||||||||||||||||||||
| 380 | this way. | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =item C<keys> | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Returns a list of Net::SSH::AuthorizedKey objects. Methods are described in | ||||||||||||||||||||||||||||
| 385 | L<Net::SSH::AuthorizedKey>. | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =item C<as_string> | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | String representation of all keys, ultimately the content that gets | ||||||||||||||||||||||||||||
| 390 | written out when calling the C<save()> method. | ||||||||||||||||||||||||||||
| 391 | Note that comments from the original file are lost. | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =item C<save> | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | Write changes back to the authorized_keys file using the as_string() | ||||||||||||||||||||||||||||
| 396 | method described above. Note that comments from the original file are lost. | ||||||||||||||||||||||||||||
| 397 | Optionally takes a file | ||||||||||||||||||||||||||||
| 398 | name parameter, so calling C<$akf-E<gt>save("foo.txt")> will save the data | ||||||||||||||||||||||||||||
| 399 | in the file "foo.txt" instead of the file the data was read from originally. | ||||||||||||||||||||||||||||
| 400 | Returns 1 if successful, and undef on error. In case of an error, error() | ||||||||||||||||||||||||||||
| 401 | contains a textual error description. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item C<sanity_check> | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Run a sanity check on the currently selected authorized_keys file. If | ||||||||||||||||||||||||||||
| 406 | it contains insanely long lines, then parsing with read() (and potential | ||||||||||||||||||||||||||||
| 407 | crashes because of out-of-memory errors) should be avoided. | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =item C<ssh_dir( [$user] )> | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | Locate the .ssh dir of a given user. If no user name is given, ssh_dir will | ||||||||||||||||||||||||||||
| 412 | look up the .ssh dir of the effective user. Typically returns something like | ||||||||||||||||||||||||||||
| 413 | "/home/gonzo/.ssh". | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | =item C<path_locate( [$user] )> | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | Locate the authorized_keys file of a given user. Typically returns something | ||||||||||||||||||||||||||||
| 418 | like "/home/gonzo/.ssh/authorized_keys". See C<ssh_dir()> for how the | ||||||||||||||||||||||||||||
| 419 | containing directory is located with and without a given user name. | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =item C<error> | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Description of last error that occurred. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =back | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =head1 LEGALESE | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | Copyright 2005-2009 by Mike Schilli, all rights reserved. | ||||||||||||||||||||||||||||
| 430 | This program is free software, you can redistribute it and/or | ||||||||||||||||||||||||||||
| 431 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 2005, Mike Schilli <m@perlmeister.com> | ||||||||||||||||||||||||||||