| 1 | package Net::SSH::Mechanize::Session; | ||||||||||||
| 2 | 16 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 16 | ▶ | use MooseX::Params::Validate; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 16 | ▶ | use AnyEvent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 16 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 16 | our @CARP_NOT = qw(Net::SSH::Mechanize AnyEvent); | |||||||||||
| 7 | |||||||||||||
| 8 | 16 | our $VERSION = '0.1.3'; # VERSION | |||||||||||
| 9 | |||||||||||||
| 10 | 16 | extends 'AnyEvent::Subprocess::Running'; | |||||||||||
| 11 | |||||||||||||
| 12 | 16 | my $passwd_prompt_re = qr/assword:\s*/; | |||||||||||
| 13 | |||||||||||||
| 14 | 16 | my $initial_prompt_re = qr/^.*?\Q$ \E$/m; | |||||||||||
| 15 | 16 | my $sudo_initial_prompt_re = qr/^.*?\Q$ \E$/m; | |||||||||||
| 16 | |||||||||||||
| 17 | # Create a random text delimiter | ||||||||||||
| 18 | # We want chars A-Z, a-z, 0-9, _- => 26+26+10 = 64 different characters. | ||||||||||||
| 19 | # First we generate a random string of ASCII chars 1..65, | ||||||||||||
| 20 | 16 | my $delim = pack "C*", map { int(rand 64)+1 } 1..20; | |||||||||||
| 21 | |||||||||||||
| 22 | # Then we map it to the characters we want. | ||||||||||||
| 23 | 16 | $delim =~ tr/\x01-\x40/A-Za-z0-9_-/; | |||||||||||
| 24 | |||||||||||||
| 25 | 16 | my $prompt = "$delim"; | |||||||||||
| 26 | |||||||||||||
| 27 | 16 | my $sudo_passwd_prompt = "$delim-passwd"; | |||||||||||
| 28 | |||||||||||||
| 29 | 16 | my $prompt_re = qr/\Q$prompt\E$/sm; | |||||||||||
| 30 | |||||||||||||
| 31 | 16 | my $sudo_passwd_prompt_re = qr/^$sudo_passwd_prompt$/; | |||||||||||
| 32 | |||||||||||||
| 33 | |||||||||||||
| 34 | 16 | has 'connection_params' => ( | |||||||||||
| 35 | isa => 'Net::SSH::Mechanize::ConnectParams', | ||||||||||||
| 36 | is => 'rw', | ||||||||||||
| 37 | # Note: this made rw and unrequired so that it can be supplied | ||||||||||||
| 38 | # after AnyEvent::Subprocess::Job constructs the instance | ||||||||||||
| 39 | ); | ||||||||||||
| 40 | |||||||||||||
| 41 | 16 | has 'is_logged_in' => ( | |||||||||||
| 42 | isa => 'Bool', | ||||||||||||
| 43 | is => 'ro', | ||||||||||||
| 44 | writer => '_set_logged_in', | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | |||||||||||||
| 47 | has '_error_event' => ( | ||||||||||||
| 48 | is => 'rw', | ||||||||||||
| 49 | isa => 'AnyEvent::CondVar', | ||||||||||||
| 50 | 69 | ▶ | default => sub { return AnyEvent->condvar }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | 16 | ); | |||||||||||
| 52 | |||||||||||||
| 53 | |||||||||||||
| 54 | # The log-in timeout limit in seconds | ||||||||||||
| 55 | 16 | has 'login_timeout' => ( | |||||||||||
| 56 | is => 'rw', | ||||||||||||
| 57 | isa => 'Int', | ||||||||||||
| 58 | default => 30, | ||||||||||||
| 59 | ); | ||||||||||||
| 60 | |||||||||||||
| 61 | # helper function | ||||||||||||
| 62 | |||||||||||||
| 63 | 60 | ▶ | sub _croak_with { | ||||||||||
|
Subroutine
_croak_with: called CC=1 SCAR=3.5
| |||||||||||||
| 64 | 60 | my ($msg, $cv) = @_; | |||||||||||
| 65 | 0 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 66 | 0 | my $h = shift; | |||||||||||
| 67 | 0 | ▶ | return unless my $text = $h->rbuf; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | $h->{rbuf} = ''; | |||||||||||
| 69 | 0 | $cv->croak("$msg: $text"); | |||||||||||
| 70 | } | ||||||||||||
| 71 | 60 | } | |||||||||||
| 72 | |||||||||||||
| 73 | 24 | ▶ | sub _warn_with { | ||||||||||
|
Subroutine
_warn_with: called CC=1 SCAR=0.0
| |||||||||||||
| 74 | 24 | my ($msg) = @_; | |||||||||||
| 75 | 11 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 76 | 11 | my $h = shift; | |||||||||||
| 77 | 11 | ▶ | return unless my $text = $h->rbuf; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 11 | $h->{rbuf} = ''; | |||||||||||
| 79 | 11 | warn "$msg: $text"; | |||||||||||
| 80 | } | ||||||||||||
| 81 | 24 | } | |||||||||||
| 82 | |||||||||||||
| 83 | 57 | ▶ | sub _push_write { | ||||||||||
|
Subroutine
_push_write: called CC=1 SCAR=0.0
| |||||||||||||
| 84 | 57 | my $handle = shift; | |||||||||||
| 85 | |||||||||||||
| 86 | # print qq(writing: "@_"\n); # DB | ||||||||||||
| 87 | 57 | $handle->push_write(@_); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | |||||||||||||
| 91 | 171 | ▶ | sub _match { | ||||||||||
|
Subroutine
_match: called CC=3 SCAR=11.0
| |||||||||||||
| 92 | 171 | my $handle = shift; | |||||||||||
| 93 | 171 | my $re = shift; | |||||||||||
| 94 | 171 | ▶ | return unless $handle->{rbuf}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 54 | my @captures = $handle->{rbuf} =~ /$re/; | |||||||||||
| 96 | 54 | ▶ | if (!@captures) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | # print qq(not matching $re: "$handle->{rbuf}"\n); # DB | ||||||||||||
| 98 | 18 | return; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | # printf qq(matching $re with: "%s"\n), substr $handle->{rbuf}, 0, $+[0]; # DB | ||||||||||||
| 102 | |||||||||||||
| 103 | 36 | substr $handle->{rbuf}, 0, $+[0], ""; | |||||||||||
| 104 | 36 | return @captures; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 60 | ▶ | sub _define_automation { | ||||||||||
|
Subroutine
_define_automation: called CC=1 SCAR=0.0
| |||||||||||||
| 108 | 60 | my $self = shift; | |||||||||||
| 109 | 60 | my $states = {@_}; | |||||||||||
| 110 | 60 | my $function = (caller 1)[3]; | |||||||||||
| 111 | |||||||||||||
| 112 | 60 | my ($stdin, $stderr) = map { $self->delegate($_)->handle } qw(pty stderr); | |||||||||||
| 113 | |||||||||||||
| 114 | 60 | my $state = 'start'; | |||||||||||
| 115 | 60 | my $cb; | |||||||||||
| 116 | 100 | ▶ | $cb = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 117 | # printf "before: state is %s %s\n", $function, $state; # DB | ||||||||||||
| 118 | 100 | $state = $states->{$state}->(@_); | |||||||||||
| 119 | 100 | ▶ | exists $states->{$state} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | or die "something is wrong, next state returned is an unknown name: '$state'"; | ||||||||||||
| 121 | |||||||||||||
| 122 | # printf "after: state is %s %s\n", $function, $state; # DB | ||||||||||||
| 123 | 100 | ▶ | if (!$states->{$state}) { # terminal state, stop reading | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | # $stderr->on_read(undef); # cancel errors on stderr | ||||||||||||
| 125 | 11 | $stdin->{rbuf} = ''; | |||||||||||
| 126 | 11 | return 1; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | # $stdin->push_read($cb); | ||||||||||||
| 130 | 89 | return; | |||||||||||
| 131 | 60 | }; | |||||||||||
| 132 | 60 | $stdin->push_read($cb); | |||||||||||
| 133 | |||||||||||||
| 134 | # printf "$Coro::current exiting _define_automation\n"; # DB | ||||||||||||
| 135 | 60 | return $state; | |||||||||||
| 136 | }; | ||||||||||||
| 137 | |||||||||||||
| 138 | # FIXME check code for possible self-ref closures which may cause mem leaks | ||||||||||||
| 139 | |||||||||||||
| 140 | |||||||||||||
| 141 | 57 | ▶ | sub login_async { | ||||||||||
|
Subroutine
login_async: called CC=2 SCAR=7.5
Pod
documented
| |||||||||||||
| 142 | 57 | my $self = shift; | |||||||||||
| 143 | 57 | my $done = AnyEvent->condvar; | |||||||||||
| 144 | |||||||||||||
| 145 | 57 | my $stdin = $self->delegate('pty')->handle; | |||||||||||
| 146 | 57 | my $stderr = $self->delegate('stderr')->handle; | |||||||||||
| 147 | |||||||||||||
| 148 | # Make this a no-op if we've already logged in | ||||||||||||
| 149 | 57 | ▶ | if ($self->is_logged_in) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 0 | $done->send($stdin, $self); | |||||||||||
| 151 | 0 | return $done; | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | 0 | ▶ | $self->_error_event->cb(sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 155 | # print "_error_event sent\n"; # DB | ||||||||||||
| 156 | 0 | $done->croak(shift->recv); | |||||||||||
| 157 | 57 | }); | |||||||||||
| 158 | |||||||||||||
| 159 | 57 | my $timeout; | |||||||||||
| 160 | 57 | my $delay = $self->login_timeout; | |||||||||||
| 161 | $timeout = AnyEvent->timer( | ||||||||||||
| 162 | after => $delay, | ||||||||||||
| 163 | 10 | ▶ | cb => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 164 | 10 | undef $timeout; | |||||||||||
| 165 | # print "timing out login\n"; # DB | ||||||||||||
| 166 | 10 | $done->croak("login timed out after $delay seconds"); | |||||||||||
| 167 | }, | ||||||||||||
| 168 | 57 | ); | |||||||||||
| 169 | |||||||||||||
| 170 | # capture stderr output, interpret as an error | ||||||||||||
| 171 | 57 | $stderr->on_read(_croak_with "error" => $done); | |||||||||||
| 172 | |||||||||||||
| 173 | $self->_define_automation( | ||||||||||||
| 174 | 65 | ▶ | start => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.5
| |||||||||||||
| 175 | 65 | ▶ | if (_match($stdin => $passwd_prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 176 | 8 | ▶ | if (!$self->connection_params->has_password) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 177 | 0 | $done->croak('password requested but none provided'); | |||||||||||
| 178 | 0 | return 'auth_failure'; | |||||||||||
| 179 | } | ||||||||||||
| 180 | 8 | my $passwd = $self->connection_params->password; | |||||||||||
| 181 | 8 | _push_write($stdin => "$passwd\n"); | |||||||||||
| 182 | 8 | return 'sent_passwd'; | |||||||||||
| 183 | } | ||||||||||||
| 184 | |||||||||||||
| 185 | 57 | ▶ | if (_match($stdin => $initial_prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 186 | 0 | _push_write($stdin => qq(PS1=$prompt; export PS1\n)); | |||||||||||
| 187 | 0 | return 'expect_prompt'; | |||||||||||
| 188 | } | ||||||||||||
| 189 | # FIXME limit buffer size and time | ||||||||||||
| 190 | 57 | return 'start'; | |||||||||||
| 191 | }, | ||||||||||||
| 192 | |||||||||||||
| 193 | 11 | ▶ | sent_passwd => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=16.6
| |||||||||||||
| 194 | 11 | ▶ | if (_match($stdin => $passwd_prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | 0 | ▶ | my $msg = $stderr->{rbuf} || ''; | ||||||||||
|
Condition0%0/2
$stderr->{'rbuf'} || ''
MC/DC0%0/1
$stderr->{'rbuf'} || ''
$stderr->{'rbuf'}
Truth table0%0/2
A
A= $stderr->{'rbuf'}
| |||||||||||||
| 196 | 0 | $done->croak("auth failure: $msg"); | |||||||||||
| 197 | 0 | return 'auth_failure'; | |||||||||||
| 198 | } | ||||||||||||
| 199 | |||||||||||||
| 200 | 11 | ▶ | if (_match($stdin => $initial_prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 201 | 8 | _push_write($stdin => qq(PS1=$prompt; export PS1\n)); | |||||||||||
| 202 | 8 | return 'expect_prompt'; | |||||||||||
| 203 | } | ||||||||||||
| 204 | |||||||||||||
| 205 | 3 | return 'sent_passwd'; | |||||||||||
| 206 | }, | ||||||||||||
| 207 | |||||||||||||
| 208 | 8 | ▶ | expect_prompt => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 209 | 8 | ▶ | if (_match($stdin => $prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 210 | # Cancel stderr monitor | ||||||||||||
| 211 | 8 | $stderr->on_read(undef); | |||||||||||
| 212 | |||||||||||||
| 213 | 8 | $self->_set_logged_in(1); | |||||||||||
| 214 | 8 | $done->send($stdin, $self); # done | |||||||||||
| 215 | 8 | return 'finished'; | |||||||||||
| 216 | } | ||||||||||||
| 217 | |||||||||||||
| 218 | 0 | return 'expect_prompt'; | |||||||||||
| 219 | }, | ||||||||||||
| 220 | |||||||||||||
| 221 | 57 | auth_failure => 0, | |||||||||||
| 222 | finished => 0, | ||||||||||||
| 223 | ); | ||||||||||||
| 224 | |||||||||||||
| 225 | 57 | return $done; | |||||||||||
| 226 | } | ||||||||||||
| 227 | |||||||||||||
| 228 | |||||||||||||
| 229 | 57 | ▶ | sub login { | ||||||||||
|
Subroutine
login: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 230 | # return (shift->login_async(@_)->recv)[1]; | ||||||||||||
| 231 | 57 | my ($cv) = shift->login_async(@_); | |||||||||||
| 232 | # printf "$Coro::current about to call recv\n"; # DB | ||||||||||||
| 233 | 57 | my $v = ($cv->recv)[1]; | |||||||||||
| 234 | # printf "$Coro::current about to called recv\n"; # DB | ||||||||||||
| 235 | 8 | return $v; | |||||||||||
| 236 | } | ||||||||||||
| 237 | |||||||||||||
| 238 | 8 | ▶ | sub logout { | ||||||||||
|
Subroutine
logout: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 239 | 8 | my $self = shift; | |||||||||||
| 240 | 8 | ▶ | croak "cannot use session yet, as it is not logged in" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 241 | if !$self->is_logged_in; | ||||||||||||
| 242 | |||||||||||||
| 243 | 8 | _push_write($self->delegate('pty')->handle => "exit\n"); | |||||||||||
| 244 | 8 | return $self; | |||||||||||
| 245 | } | ||||||||||||
| 246 | |||||||||||||
| 247 | 21 | ▶ | sub capture_async { | ||||||||||
|
Subroutine
capture_async: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 248 | 21 | my $self = shift; | |||||||||||
| 249 | 21 | my ($cmd) = pos_validated_list( | |||||||||||
| 250 | \@_, | ||||||||||||
| 251 | { isa => 'Str' }, | ||||||||||||
| 252 | ); | ||||||||||||
| 253 | |||||||||||||
| 254 | 21 | ▶ | croak "cannot use session yet, as it is not logged in" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 255 | if !$self->is_logged_in; | ||||||||||||
| 256 | |||||||||||||
| 257 | 21 | my $stdin = $self->delegate('pty')->handle; | |||||||||||
| 258 | 21 | my $stderr = $self->delegate('stderr')->handle; | |||||||||||
| 259 | |||||||||||||
| 260 | 21 | $cmd =~ s/\s*\z/\n/ms; | |||||||||||
| 261 | |||||||||||||
| 262 | # send command | ||||||||||||
| 263 | 21 | _push_write($stdin => $cmd); | |||||||||||
| 264 | |||||||||||||
| 265 | # read result | ||||||||||||
| 266 | 21 | my $cumdata = ''; | |||||||||||
| 267 | |||||||||||||
| 268 | # we want the _error_event condvar to trigger a croak sent to $done. | ||||||||||||
| 269 | 21 | my $done = AnyEvent->condvar; | |||||||||||
| 270 | # FIXME check _error_event for expiry? | ||||||||||||
| 271 | 2 | ▶ | $self->_error_event->cb(sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 272 | # print "xxxx _error_event\n"; # DB | ||||||||||||
| 273 | 2 | $done->croak(shift->recv); | |||||||||||
| 274 | 21 | }); | |||||||||||
| 275 | |||||||||||||
| 276 | # capture stderr output, interpret as a warning | ||||||||||||
| 277 | 21 | $stderr->on_read(_warn_with "unexpected stderr from command"); | |||||||||||
| 278 | |||||||||||||
| 279 | 47 | ▶ | my $read_output_cb = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 280 | 47 | my ($handle) = @_; | |||||||||||
| 281 | 47 | ▶ | return unless defined $handle->{rbuf}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 282 | |||||||||||||
| 283 | # print "got: $handle->{rbuf}\n"; # DB | ||||||||||||
| 284 | |||||||||||||
| 285 | 47 | $cumdata .= $handle->{rbuf}; | |||||||||||
| 286 | 47 | $handle->{rbuf} = ''; | |||||||||||
| 287 | |||||||||||||
| 288 | 47 | ▶ | $cumdata =~ /(.*?)$prompt_re/ms | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 289 | or return; | ||||||||||||
| 290 | |||||||||||||
| 291 | # cancel stderr monitor | ||||||||||||
| 292 | 21 | $stderr->on_read(undef); | |||||||||||
| 293 | |||||||||||||
| 294 | 21 | $done->send($handle, $1); | |||||||||||
| 295 | 21 | return 1; | |||||||||||
| 296 | 21 | }; | |||||||||||
| 297 | |||||||||||||
| 298 | 21 | $stdin->push_read($read_output_cb); | |||||||||||
| 299 | |||||||||||||
| 300 | 21 | return $done; | |||||||||||
| 301 | } | ||||||||||||
| 302 | |||||||||||||
| 303 | |||||||||||||
| 304 | 21 | ▶ | sub capture { | ||||||||||
|
Subroutine
capture: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 305 | 21 | return (shift->capture_async(@_)->recv)[1]; | |||||||||||
| 306 | } | ||||||||||||
| 307 | |||||||||||||
| 308 | |||||||||||||
| 309 | 3 | ▶ | sub sudo_capture_async { | ||||||||||
|
Subroutine
sudo_capture_async: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||
| 310 | 3 | my $self = shift; | |||||||||||
| 311 | 3 | my ($cmd) = pos_validated_list( | |||||||||||
| 312 | \@_, | ||||||||||||
| 313 | { isa => 'Str' }, | ||||||||||||
| 314 | ); | ||||||||||||
| 315 | |||||||||||||
| 316 | 3 | ▶ | croak "cannot use session yet, as it is not logged in" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 317 | if !$self->is_logged_in; | ||||||||||||
| 318 | |||||||||||||
| 319 | 3 | my $done = AnyEvent->condvar; | |||||||||||
| 320 | 0 | ▶ | $self->_error_event->cb(sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 321 | # print "_error_event sent\n"; DB | ||||||||||||
| 322 | 0 | $done->croak(shift->recv); | |||||||||||
| 323 | 3 | }); | |||||||||||
| 324 | |||||||||||||
| 325 | # we know we'll need the password, so check this up-front | ||||||||||||
| 326 | 3 | ▶ | if (!$self->connection_params->has_password) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 327 | 0 | croak 'password requested but none provided'; | |||||||||||
| 328 | } | ||||||||||||
| 329 | |||||||||||||
| 330 | 3 | my $stdin = $self->delegate('pty')->handle; | |||||||||||
| 331 | 3 | my $stderr = $self->delegate('stderr')->handle; | |||||||||||
| 332 | |||||||||||||
| 333 | 3 | my $timeout; | |||||||||||
| 334 | 3 | my $delay = $self->login_timeout; | |||||||||||
| 335 | $timeout = AnyEvent->timer( | ||||||||||||
| 336 | after => $delay, | ||||||||||||
| 337 | 0 | ▶ | cb => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 338 | 0 | undef $timeout; | |||||||||||
| 339 | # print "timing out login\n"; # DB | ||||||||||||
| 340 | 0 | $done->croak("sudo_capture timed out after $delay seconds"); | |||||||||||
| 341 | }, | ||||||||||||
| 342 | 3 | ); | |||||||||||
| 343 | |||||||||||||
| 344 | # capture stderr output, interpret as an error | ||||||||||||
| 345 | 3 | $stderr->on_read(_croak_with "error" => $done); | |||||||||||
| 346 | |||||||||||||
| 347 | # ensure command has a trailing newline | ||||||||||||
| 348 | 3 | $cmd =~ s/\s*\z/\n/ms; | |||||||||||
| 349 | |||||||||||||
| 350 | # get captured result here | ||||||||||||
| 351 | 3 | my $cumdata = ''; | |||||||||||
| 352 | |||||||||||||
| 353 | # FIXME escape/untaint $passwd_prompt_re | ||||||||||||
| 354 | # use full path names | ||||||||||||
| 355 | |||||||||||||
| 356 | # Authenticate. Erase any cached sudo authentication first - we | ||||||||||||
| 357 | # want to guarantee that we will get a password prompt. Then | ||||||||||||
| 358 | # start a new shell with sudo. | ||||||||||||
| 359 | 3 | _push_write($stdin => "sudo -K; sudo -p '$sudo_passwd_prompt' sh\n"); | |||||||||||
| 360 | |||||||||||||
| 361 | $self->_define_automation( | ||||||||||||
| 362 | 6 | ▶ | start => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 363 | 6 | ▶ | if (_match($stdin => $sudo_passwd_prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 364 | 3 | my $passwd = $self->connection_params->password; | |||||||||||
| 365 | # print "sending password\n"; # DB | ||||||||||||
| 366 | 3 | _push_write($stdin => "$passwd\n"); | |||||||||||
| 367 | 3 | return 'sent_passwd'; | |||||||||||
| 368 | } | ||||||||||||
| 369 | |||||||||||||
| 370 | # FIXME limit buffer size and time | ||||||||||||
| 371 | 3 | return 'start'; | |||||||||||
| 372 | }, | ||||||||||||
| 373 | |||||||||||||
| 374 | 3 | ▶ | sent_passwd => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=18.6
| |||||||||||||
| 375 | 3 | ▶ | if (_match($stdin => $sudo_passwd_prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 376 | 0 | ▶ | my $msg = $stderr->{rbuf} || ''; | ||||||||||
|
Condition0%0/2
$stderr->{'rbuf'} || ''
MC/DC0%0/1
$stderr->{'rbuf'} || ''
$stderr->{'rbuf'}
Truth table0%0/2
A
A= $stderr->{'rbuf'}
| |||||||||||||
| 377 | 0 | $done->croak("auth failure: $msg"); | |||||||||||
| 378 | 0 | return 'auth_failure'; | |||||||||||
| 379 | } | ||||||||||||
| 380 | |||||||||||||
| 381 | 3 | ▶ | if (_match($stdin => $prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 382 | # Cancel stderr monitor | ||||||||||||
| 383 | 3 | $stderr->on_read(undef); | |||||||||||
| 384 | |||||||||||||
| 385 | 3 | _push_write($stdin => $cmd); | |||||||||||
| 386 | 3 | return 'sent_cmd'; | |||||||||||
| 387 | } | ||||||||||||
| 388 | |||||||||||||
| 389 | 0 | return 'sent_passwd'; | |||||||||||
| 390 | }, | ||||||||||||
| 391 | |||||||||||||
| 392 | 4 | ▶ | sent_cmd => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 393 | 4 | ▶ | if (my ($data) = _match($stdin => qr/(.*?)$prompt_re/sm)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 394 | 3 | $cumdata .= $data; | |||||||||||
| 395 | # print "got data: $data\n<$stdin->{rbuf}>\n"; # DB | ||||||||||||
| 396 | |||||||||||||
| 397 | 3 | $stdin->{rbuf} = ''; | |||||||||||
| 398 | |||||||||||||
| 399 | # capture stderr output, interpret as a warning | ||||||||||||
| 400 | 3 | $stderr->on_read(_warn_with "unexpected stderr from sudo command"); | |||||||||||
| 401 | |||||||||||||
| 402 | # exit sudo shell | ||||||||||||
| 403 | 3 | _push_write($stdin => "exit\n"); | |||||||||||
| 404 | |||||||||||||
| 405 | 3 | return 'exited_shell'; | |||||||||||
| 406 | } | ||||||||||||
| 407 | |||||||||||||
| 408 | 1 | $cumdata .= $stdin->{rbuf}; | |||||||||||
| 409 | 1 | $stdin->{rbuf} = ''; | |||||||||||
| 410 | 1 | return 'sent_cmd'; | |||||||||||
| 411 | }, | ||||||||||||
| 412 | |||||||||||||
| 413 | 3 | ▶ | exited_shell => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 414 | 3 | ▶ | if (_match($stdin => $prompt_re)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 415 | # Cancel stderr monitor | ||||||||||||
| 416 | 3 | $stderr->on_read(undef); | |||||||||||
| 417 | |||||||||||||
| 418 | # remove any output from the exit | ||||||||||||
| 419 | # FIXME should this check that everything has been consumed? | ||||||||||||
| 420 | 3 | $stdin->{rbuf} = ''; | |||||||||||
| 421 | |||||||||||||
| 422 | 3 | $done->send($stdin, $cumdata); # done, send data collected | |||||||||||
| 423 | 3 | return 'finished'; | |||||||||||
| 424 | } | ||||||||||||
| 425 | |||||||||||||
| 426 | 0 | return 'exited_shell'; | |||||||||||
| 427 | }, | ||||||||||||
| 428 | |||||||||||||
| 429 | 3 | auth_failure => 0, | |||||||||||
| 430 | finished => 0, | ||||||||||||
| 431 | ); | ||||||||||||
| 432 | |||||||||||||
| 433 | 3 | return $done; | |||||||||||
| 434 | } | ||||||||||||
| 435 | |||||||||||||
| 436 | 3 | ▶ | sub sudo_capture { | ||||||||||
|
Subroutine
sudo_capture: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 437 | 3 | return (shift->sudo_capture_async(@_)->recv)[1]; | |||||||||||
| 438 | } | ||||||||||||
| 439 | |||||||||||||
| 440 | |||||||||||||
| 441 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 442 | 16 | 1; | |||||||||||
| 443 | |||||||||||||
| 444 | __END__ | ||||||||||||