| 1 | package Net::SSH::Any::Backend::Net_SSH2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | our @CARP_NOT = qw(Net::SSH::Any); | ||
| 8 | |||
| 9 | use Net::SSH::Any::Util qw($debug _debug _debug_hexdump _debug_dump _first_defined _warn); | ||
| 10 | use Net::SSH::Any::Constants qw(:error); | ||
| 11 | |||
| 12 | use Net::SSH2; | ||
| 13 | use File::Spec; | ||
| 14 | use Errno (); | ||
| 15 | use Fcntl (); | ||
| 16 | use Time::HiRes (); | ||
| 17 | use Socket qw(SO_LINGER SO_KEEPALIVE); | ||
| 18 | use IO::Socket::INET; | ||
| 19 | |||
| 20 | our $stdin_buffer_size ||= 40000; | ||
| 21 | our $stdout_buffer_size ||= 262144; | ||
| 22 | |||
| 23 | my $windows = $^O =~ /^MSWin/; | ||
| 24 | |||
| 25 | use Config; | ||
| 26 | my %sig_name2num; | ||
| 27 | if (defined($Config{sig_name})) { | ||
| 28 | my $i = 0; | ||
| 29 | $sig_name2num{$_} = $i++ for split //, $Config{sig_name}; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _sig_name2num { | ||
| 33 | my $signal = shift; | ||
| 34 | return 0 unless defined $signal and length $signal; | ||
| 35 | my $num = $sig_name2num{$signal}; | ||
| 36 | (defined $num ? $num : 254); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _backend_api_version { 2 } | ||
| 40 | |||
| 41 | my %C = ( SOCKET_BLOCK_INBOUND => 1, | ||
| 42 | SOCKET_BLOCK_OUTBOUND => 2, | ||
| 43 | ERROR_EAGAIN => -37, | ||
| 44 | ERROR_FILE => -16, | ||
| 45 | ERROR_CHANNEL_CLOSED => -26, | ||
| 46 | ERROR_CHANNEL_EOF_SENT => -27, | ||
| 47 | KNOWNHOST_TYPE_PLAIN => 1, | ||
| 48 | KNOWNHOST_KEYENC_RAW => 2, | ||
| 49 | KNOWNHOST_KEY_SHIFT => (1<<18), | ||
| 50 | KNOWNHOST_CHECK_MATCH => 0, | ||
| 51 | KNOWNHOST_CHECK_MISMATCH => 1, | ||
| 52 | KNOWNHOST_CHECK_NOTFOUND => 2, | ||
| 53 | FLAG_COMPRESS => 2, | ||
| 54 | TRACE_TRANS => (1<<1), | ||
| 55 | TRACE_DUMP => (1<<10), | ||
| 56 | ); | ||
| 57 | |||
| 58 | do { | ||
| 59 | local ($@, $SIG{__DIE__}); | ||
| 60 | for my $c (keys %C) { | ||
| 61 | if (defined (my $v = eval "Net::SSH2::LIBSSH2_$c()")) { | ||
| 62 | $C{$c} = $v; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | |||
| 67 | sub __copy_error { | ||
| 68 | my ($any, $code) = @_; | ||
| 69 | my $ssh2 = $any->{be_ssh2} | ||
| 70 | or die "internal error: __copy_error called, but there is no ssh2 object"; | ||
| 71 | my ($error, $error_name, $error_msg) = $ssh2->error; | ||
| 72 | $error or die "internal error: __copy_error called, but there is no error"; | ||
| 73 | $error_msg ||= $error_name; | ||
| 74 | |||
| 75 | if ($error == $C{ERROR_EAGAIN}) { | ||
| 76 | # most libssh2 functions can't recover from an EAGAIN error | ||
| 77 | # leaving the connection in a broken state. We catch that case | ||
| 78 | # marking the connection as lost. Note that the functions that | ||
| 79 | # can recover, return LIBSSH2_ERROR_EAGAIN but leave the | ||
| 80 | # internal error field as 0 | ||
| 81 | $any->_set_error(SSHA_CONNECTION_ERROR, | ||
| 82 | "connection lost: internal libssh2 error, unhandled EAGAIN, $error_msg"); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $any->_set_error($code || SSHA_CHANNEL_ERROR, $error_msg); | ||
| 86 | } | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub __check_host_key { | ||
| 91 | my $any = shift; | ||
| 92 | my $ssh2 = $any->{be_ssh2} or croak "internal error: be_ssh2 is not set"; | ||
| 93 | my $be_opts = $any->{be_opts}; | ||
| 94 | |||
| 95 | my $hostkey_method = $ssh2->can('remote_hostkey'); | ||
| 96 | unless ($hostkey_method) { | ||
| 97 | carp "The version of Net::SSH2 installed ($Net::SSH2::VERSION) doesn't support " . | ||
| 98 | "checking the host key against a known_hosts file. This script is exposed to ". | ||
| 99 | "man-in-the-middle atacks!!!"; | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | my ($key, $type) = $hostkey_method->($ssh2); | ||
| 104 | |||
| 105 | my $known_hosts_path = $be_opts->{known_hosts_path}; | ||
| 106 | unless (defined $known_hosts_path) { | ||
| 107 | my $config_dir; | ||
| 108 | if ($windows) { | ||
| 109 | $any->_load_module('Win32') or return; | ||
| 110 | my $appdata = Win32::GetFolderPath(Win32::CSIDL_APPDATA()); | ||
| 111 | unless (defined $appdata) { | ||
| 112 | $any->_set_error(SSHA_CONNECTION_ERROR, "unable to determine directory for user application data"); | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | $config_dir = File::Spec->join($appdata, 'libnet-ssh-any-perl'); | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | my $home = (getpwuid $>)[7]; | ||
| 119 | $home = $ENV{HOME} unless defined $home; | ||
| 120 | unless (defined $home) { | ||
| 121 | $any->_set_error(SSHA_CONNECTION_ERROR, "unable to determine user home directory"); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | $config_dir = File::Spec->join($home, '.ssh'); | ||
| 125 | } | ||
| 126 | unless (-d $config_dir or mkdir $config_dir, 0700) { | ||
| 127 | $any->_set_error(SSHA_CONNECTION_ERROR, "unable to create directory '$config_dir': $^E"); | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | $known_hosts_path = File::Spec->join($config_dir, 'known_hosts'); | ||
| 131 | } | ||
| 132 | |||
| 133 | $debug and $debug & 1024 and _debug "reading known host keys from '$known_hosts_path'"; | ||
| 134 | |||
| 135 | local ($@, $SIG{__DIE__}); | ||
| 136 | |||
| 137 | my $kh = $ssh2->known_hosts; | ||
| 138 | my $ok = eval { $kh->readfile($known_hosts_path) }; | ||
| 139 | unless (defined $ok) { | ||
| 140 | $debug and $debug & 1024 and _debug "unable to read known hosts file: " . $ssh2->error; | ||
| 141 | if ($ssh2->error == $C{ERROR_FILE}) { | ||
| 142 | if (-f $known_hosts_path) { | ||
| 143 | $any->_set_error(SSHA_CONNECTION_ERROR, "unable to read known_hosts file at '$known_hosts_path'"); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | # a non-existent file is not an error, continue... | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $any->_set_error(SSHA_CONNECTION_ERROR, | ||
| 150 | "Unable to parse known_hosts file at '$known_hosts_path': ". ($ssh2->error)[2]); | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | if ($debug and $debug & 1024) { | ||
| 156 | _debug "remote key is of type $type"; | ||
| 157 | _debug_hexdump("key", $key); | ||
| 158 | } | ||
| 159 | |||
| 160 | my $key_type = ( $C{KNOWNHOST_TYPE_PLAIN} | | ||
| 161 | $C{KNOWNHOST_KEYENC_RAW} | | ||
| 162 | (($type + 1) << $C{KNOWNHOST_KEY_SHIFT}) ); | ||
| 163 | |||
| 164 | my $check = $kh->check($be_opts->{host}, $be_opts->{port}, $key, $key_type); | ||
| 165 | |||
| 166 | if ($check == $C{KNOWNHOST_CHECK_MATCH}) { | ||
| 167 | $debug and $debug & 1024 and _debug("host key matched"); | ||
| 168 | return 1; | ||
| 169 | } | ||
| 170 | elsif ($check == $C{KNOWNHOST_CHECK_MISMATCH}) { | ||
| 171 | $debug and $debug & 1024 and _debug("host key found but did not match"); | ||
| 172 | $any->_set_error(SSHA_CONNECTION_ERROR, "The host key for '$be_opts->{host}' has changed"); | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | elsif ($check == $C{KNOWNHOST_CHECK_NOTFOUND}) { | ||
| 176 | $debug and $debug & 1024 and _debug("host key not found in known_hosts"); | ||
| 177 | if ($be_opts->{strict_host_key_checking}) { | ||
| 178 | $any->_set_error(SSHA_CONNECTION_ERROR, "the authenticity of host '$be_opts->{host}' can't be established"); | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | else { | ||
| 182 | $debug and $debug & 1024 and _debug "saving host key to '$known_hosts_path'"; | ||
| 183 | eval { | ||
| 184 | $kh->add($be_opts->{host}, '', $key, "added by Perl module Net::SSH::Any (Net::SSH2 backend)", $key_type); | ||
| 185 | $kh->writefile($known_hosts_path); | ||
| 186 | }; | ||
| 187 | return 1; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | $debug and $debug & 1024 and _debug("host key check failure (check: $check)!"); | ||
| 192 | $any->_set_error(SSHA_CONNECTION_ERROR, "unable to check host key, libssh2_knownhost_check failed"); | ||
| 193 | () | ||
| 194 | } | ||
| 195 | |||
| 196 | sub _validate_backend_opts { | ||
| 197 | my ($any, %be_opts) = @_; | ||
| 198 | my $mod_ver = do { no warnings; 0 + $Net::SSH2::VERSION }; | ||
| 199 | if ($mod_ver < 0.59) { | ||
| 200 | $any->_set_error(SSHA_CONNECTION_ERROR, | ||
| 201 | "The version of Net::SSH2 available ($Net::SSH2::VERSION) is too old. ". | ||
| 202 | "0.59 or later required"); | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | my @lib_ver = Net::SSH2::version(); | ||
| 207 | $debug and $debug & 1024 and _debug "libssh2 version $lib_ver[2]"; | ||
| 208 | if ($lib_ver[1] < 0x010500) { | ||
| 209 | $any->_set_error(SSHA_CONNECTION_ERROR, | ||
| 210 | "Net::SSH2 was compiled against an old unsupported version of libssh2 ($lib_ver[2])"); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | |||
| 214 | my $ssh2 = $any->{be_ssh2} = Net::SSH2->new; | ||
| 215 | unless ($ssh2) { | ||
| 216 | $any->_set_error(SSHA_CONNECTION_ERROR, "Unable to create Net::SSH2 object"); | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | $debug and $debug & 2048 and $ssh2->trace(~0); #~$C{TRACE_TRANS}); | ||
| 220 | |||
| 221 | $ssh2->timeout(1000 * ($be_opts{timeout} // $be_opts{io_timeout})); | ||
| 222 | |||
| 223 | if ($be_opts{compress}) { | ||
| 224 | if (defined(my $flag_method = $ssh2->can('flag'))) { | ||
| 225 | $debug and $debug & 1024 and _debug "enabling compression"; | ||
| 226 | $flag_method->($ssh2, $C{FLAG_COMPRESS}, 1); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | $any->{be_opts} = \%be_opts; | ||
| 231 | 1; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub _connect { | ||
| 235 | my $any = shift; | ||
| 236 | my $ssh2 = $any->{be_ssh2} or return; | ||
| 237 | my $be_opts = $any->{be_opts}; | ||
| 238 | my $socket = IO::Socket::INET->new(PeerHost => $be_opts->{host}, | ||
| 239 | PeerPort => ($be_opts->{port} || 22), | ||
| 240 | ($be_opts->{timeout} ? (Timeout => $be_opts->{timeout}) : ())); | ||
| 241 | if ($socket) { | ||
| 242 | $socket->sockopt(SO_LINGER, pack(SS => 0, 0)); | ||
| 243 | $socket->sockopt(SO_KEEPALIVE, 1); | ||
| 244 | } | ||
| 245 | unless ($socket and $ssh2->connect($socket)) { | ||
| 246 | return $any->_set_error(SSHA_CONNECTION_ERROR, "Unable to connect to remote host"); | ||
| 247 | } | ||
| 248 | $debug and $debug & 1024 and _debug 'COMP_SC: ' . $ssh2->method('COMP_SC') . ' COMP_CS: ' .$ssh2->method('COMP_CS'); | ||
| 249 | |||
| 250 | __check_host_key($any) or return; | ||
| 251 | |||
| 252 | my %aa; | ||
| 253 | $aa{username} = _first_defined($be_opts->{user}, | ||
| 254 | eval { (getpwuid $<)[0] }, | ||
| 255 | eval { getlogin() }); | ||
| 256 | $aa{password} = $be_opts->{password} if defined $be_opts->{password}; | ||
| 257 | $aa{passphrase} = $be_opts->{passphrase} if defined $be_opts->{passphrase}; | ||
| 258 | if (defined (my $private = $be_opts->{key_path})) { | ||
| 259 | unless (-f $private) { | ||
| 260 | $any->_set_error(SSHA_CONNECTION_ERROR, "Private key '$private' does not exist on file system"); | ||
| 261 | return; | ||
| 262 | } | ||
| 263 | my $public = $private.".pub"; | ||
| 264 | unless (-f $public) { | ||
| 265 | $any->_set_error(SSHA_CONNECTION_ERROR, "Public key '$public' does not exist on file system"); | ||
| 266 | return; | ||
| 267 | } | ||
| 268 | $aa{privatekey} = $private; | ||
| 269 | $aa{publickey} = $public; | ||
| 270 | } | ||
| 271 | # TODO: use default user keys on ~/.ssh/id_dsa and ~/.ssh/id_rsa | ||
| 272 | |||
| 273 | $debug and $debug & 1024 and _debug_dump "Net::SSH2 authentication args", \%aa; | ||
| 274 | |||
| 275 | $ssh2->auth(%aa, interact => !$be_opts->{batch_mode}); | ||
| 276 | unless ($ssh2->auth_ok) { | ||
| 277 | $any->_set_error(SSHA_CONNECTION_ERROR, "Authentication failed"); | ||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | $any->{be_fileno} = fileno $ssh2->sock; | ||
| 282 | $debug and $debug & 1024 and _debug("SSH socket file descriptor: $any->{be_fileno}"); | ||
| 283 | $any->{be_select_bm} = ''; | ||
| 284 | vec ($any->{be_select_bm}, $any->{be_fileno}, 1) = 1; | ||
| 285 | 1; | ||
| 286 | } | ||
| 287 | |||
| 288 | # those are the operations that can be safely carried on in a | ||
| 289 | # non-blocking fashion: | ||
| 290 | my %non_blocking_method = (read => 1); | ||
| 291 | |||
| 292 | sub _channel_do { | ||
| 293 | my $any = shift; | ||
| 294 | my $channel = shift; | ||
| 295 | my $blocking = shift; | ||
| 296 | my $method = shift; | ||
| 297 | if ($any->error == SSHA_CONNECTION_ERROR) { | ||
| 298 | $debug and $debug & 1024 and _debug "skipping $channel->$method call because connection is broken"; | ||
| 299 | return | ||
| 300 | } | ||
| 301 | my $ssh2 = $any->{be_ssh2}; | ||
| 302 | $blocking ||= !$non_blocking_method{$method}; | ||
| 303 | $ssh2->blocking($blocking); | ||
| 304 | |||
| 305 | $debug and $debug & 1024 and _debug "calling $channel->$method with ", scalar(@_), " args"; | ||
| 306 | |||
| 307 | my $time_limit = time + $any->{io_timeout}; | ||
| 308 | while (1) { | ||
| 309 | my $rc = $channel->$method(@_); | ||
| 310 | $debug and $debug & 1024 and _debug "$channel->$method rc: ", $rc; | ||
| 311 | return $rc if defined $rc; | ||
| 312 | my ($error, $error_name, $error_msg) = $ssh2->error; | ||
| 313 | # We assume Net::SSH2 masked a LIBSSH2_ERROR_EAGAIN if | ||
| 314 | # both $rc and $ssh->error are unset | ||
| 315 | $debug and $debug & 1024 and _debug("rc: ", $rc, "error: ", $error, ", name: ", | ||
| 316 | $error_name, ", msg: ", $error_msg); | ||
| 317 | if ($error == $C{ERROR_EAGAIN} and not $blocking) { | ||
| 318 | # When an EAGAIN arrives and there is data queued for | ||
| 319 | # writting we have to repeat the operation unchanged until | ||
| 320 | # it succeeds or the timeout is reached otherwise we risk | ||
| 321 | # corrupting the connection! | ||
| 322 | if ($ssh2->block_directions & $C{SOCKET_BLOCK_OUTBOUND}) { | ||
| 323 | if ($time_limit < time) { | ||
| 324 | $any->_set_error(SSHA_CONNECTION_ERROR, "connection lost, timeout"); | ||
| 325 | return; | ||
| 326 | } | ||
| 327 | $debug and $debug & 1024 and _debug "waiting for the socket to become writable"; | ||
| 328 | select(undef, "$any->{__select_bm}", undef, 1); | ||
| 329 | } | ||
| 330 | else { | ||
| 331 | # otherwise we can safely return | ||
| 332 | $debug and $debug & 1024 and _debug "operation $method skipped"; | ||
| 333 | return 0 | ||
| 334 | } | ||
| 335 | } | ||
| 336 | else { | ||
| 337 | unless ($error == $C{ERROR_CHANNEL_CLOSED} or | ||
| 338 | $error == $C{ERROR_CHANNEL_EOF_SENT}) { | ||
| 339 | $error_msg ||= $error_name || "unknown libssh2 error"; | ||
| 340 | if ($error == $C{ERROR_EAGAIN}) { | ||
| 341 | $any->_set_error(SSHA_CONNECTION_ERROR, | ||
| 342 | "connection lost: internal libssh2 error, unhandled EAGAIN, $error_msg"); | ||
| 343 | } | ||
| 344 | else { | ||
| 345 | $any->_set_error(SSHA_CHANNEL_ERROR, $error_msg); | ||
| 346 | } | ||
| 347 | } | ||
| 348 | return | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | sub __parse_fh_opts { | ||
| 354 | my ($any, $opts, $channel) = @_; | ||
| 355 | my @name = qw(stdout stderr); | ||
| 356 | my $in_fh; | ||
| 357 | my @out_fh; | ||
| 358 | my $in_fh_comes_from_the_outside; | ||
| 359 | |||
| 360 | my $stdin_data = delete $opts->{stdin_data}; | ||
| 361 | unless (defined $stdin_data) { | ||
| 362 | if (defined (my $stdin_file = delete $opts->{stdin_file})) { | ||
| 363 | $in_fh = $any->_open_file('<', $stdin_file) or return; | ||
| 364 | } | ||
| 365 | elsif (defined(my $fh = delete $opts->{stdin_fh})) { | ||
| 366 | $in_fh = $fh; | ||
| 367 | $in_fh_comes_from_the_outside = 1; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | if ($in_fh and (-s $in_fh or (not $windows and -p $in_fh))) { | ||
| 372 | if ($in_fh_comes_from_the_outside) { | ||
| 373 | $in_fh = $any->_open_file('<&', $in_fh) or return; | ||
| 374 | } | ||
| 375 | binmode $in_fh; | ||
| 376 | if ($windows) { | ||
| 377 | my $true = 1; | ||
| 378 | ioctl($in_fh, 0x8004667e, \$true); | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | my $flags = fcntl($in_fh, Fcntl::F_GETFL(), 0); | ||
| 382 | fcntl($in_fh, Fcntl::F_SETFL(), $flags | Fcntl::O_NONBLOCK()); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | for my $stream (qw(stdout stderr)) { | ||
| 387 | my $fh = delete $opts->{"${stream}_fh"}; | ||
| 388 | unless ($fh) { | ||
| 389 | my $file = ( ( $stream eq 'stdout' and | ||
| 390 | delete($opts->{"stdout_discard"}) ) | ||
| 391 | ? File::Spec->devnull | ||
| 392 | : delete $opts->{"${stream}_file"} ); | ||
| 393 | if (defined $file) { | ||
| 394 | $fh = $any->_open_file('>', $file) or return; | ||
| 395 | } | ||
| 396 | if ($stream eq 'stderr' and not defined $fh) { | ||
| 397 | if (delete $opts->{stderr_to_stdout}) { | ||
| 398 | $channel->ext_data('merge'); | ||
| 399 | } | ||
| 400 | elsif (delete $opts->{stderr_discard}) { | ||
| 401 | $channel->ext_data('ignore'); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | } | ||
| 405 | push @out_fh, $fh; | ||
| 406 | } | ||
| 407 | |||
| 408 | grep /^std(?:out|err|in)_/, keys %$opts and | ||
| 409 | croak "invalid option(s) '" . join("', '", grep /^std(?:out|err)_/, keys %$opts) . "'"; | ||
| 410 | return ($stdin_data, $in_fh, @out_fh); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub __open_channel_and_exec { | ||
| 414 | my ($any, $opts, $cmd) = @_; | ||
| 415 | my $ssh2 = $any->{be_ssh2} or return; | ||
| 416 | my $window_size = delete $opts->{_window_size} || 2 * 1024 * 1024; | ||
| 417 | my $subsystem = delete $opts->{subsystem}; | ||
| 418 | croak "subsystem name missing" if $subsystem and not defined $cmd; | ||
| 419 | |||
| 420 | if (my $channel = $ssh2->channel("session", $window_size)) { | ||
| 421 | my @fhs = __parse_fh_opts($any, $opts, $channel) or return; | ||
| 422 | if ($any->_channel_do($channel, 1, | ||
| 423 | 'process', | ||
| 424 | ( (defined $cmd and length $cmd) | ||
| 425 | ? ( ($subsystem ? 'subsystem' : 'exec') => $cmd) | ||
| 426 | : 'shell'))) { | ||
| 427 | return ($channel, @fhs); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | return; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub _system { | ||
| 434 | my ($any, $opts, $cmd) = @_; | ||
| 435 | my ($channel, $in_data, $in_fh, $out_fh, $err_fh) = __open_channel_and_exec($any, $opts, $cmd) or return; | ||
| 436 | __io3($any, $channel, $opts->{timeout}, | ||
| 437 | $in_data, $in_fh, $out_fh || \*STDOUT, $err_fh || \*STDERR); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub _capture { | ||
| 441 | my ($any, $opts, $cmd) = @_; | ||
| 442 | my ($channel, $in_data, $in_fh, $out_fh, $err_fh) = __open_channel_and_exec($any, $opts, $cmd) or return; | ||
| 443 | die 'Internal error: $out_fh is not undef' if $out_fh; | ||
| 444 | (__io3($any, $channel, $opts->{timeout}, | ||
| 445 | $in_data, $in_fh, undef, $err_fh || \*STDERR))[0]; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub _capture2 { | ||
| 449 | my ($any, $opts, $cmd) = @_; | ||
| 450 | my ($channel, $in_data, $in_fh, $out_fh, $err_fh) = __open_channel_and_exec($any, $opts, $cmd) or return; | ||
| 451 | die 'Internal error: $out_fh is not undef' if $out_fh; | ||
| 452 | die 'Internal error: $err_fh is not undef' if $err_fh; | ||
| 453 | __io3($any, $channel, $opts->{timeout}, $in_data, $in_fh); | ||
| 454 | } | ||
| 455 | |||
| 456 | sub __write_all { | ||
| 457 | my $any = shift; | ||
| 458 | my $fh = shift; | ||
| 459 | if (ref $fh eq 'CODE') { | ||
| 460 | unless ($fh->($_[0])) { | ||
| 461 | $any->_set_error(SSHA_LOCAL_IO_ERROR, "Couldn't handle remote data, callback failed"); | ||
| 462 | return; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | elsif (defined $_[0]) { | ||
| 466 | my $off = 0; | ||
| 467 | while (length($_[0]) > $off) { | ||
| 468 | if (my $bytes = syswrite $fh, $_[0], 40000, $off) { | ||
| 469 | $off += $bytes; | ||
| 470 | } | ||
| 471 | elsif ($! == Errno::EAGAIN()) { | ||
| 472 | select undef, undef, undef, 0.05; | ||
| 473 | } | ||
| 474 | else { | ||
| 475 | $any->_set_error(SSHA_LOCAL_IO_ERROR, "Couldn't write to pipe", $!); | ||
| 476 | return; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | } | ||
| 480 | return 1; | ||
| 481 | } | ||
| 482 | |||
| 483 | sub _channel_close { | ||
| 484 | my ($any, $channel) = @_; | ||
| 485 | |||
| 486 | $any->_channel_do($channel, 1, 'close'); | ||
| 487 | $any->_channel_do($channel, 1, 'wait_closed'); | ||
| 488 | |||
| 489 | if ($any->error) { | ||
| 490 | $? = (255 << 8); | ||
| 491 | } | ||
| 492 | else { | ||
| 493 | my $code = $channel->exit_status || 0; | ||
| 494 | my $signal = _sig_name2num($channel->exit_signal) || 0; | ||
| 495 | $? = (($code << 8) | $signal); | ||
| 496 | } | ||
| 497 | 1 | ||
| 498 | } | ||
| 499 | |||
| 500 | sub __io3 { | ||
| 501 | my ($any, $channel, $timeout, $in_data, $in_fh, @out_fh) = @_; | ||
| 502 | my $ssh2 = $any->{be_ssh2} or return; | ||
| 503 | my $in = ''; | ||
| 504 | my $out; | ||
| 505 | my ($in_at_eof, $in_refill); | ||
| 506 | my @cap = ('', ''); | ||
| 507 | my ($eof_sent, $eof_received); | ||
| 508 | $timeout ||= $any->{timeout}; | ||
| 509 | my $start = time; | ||
| 510 | my $select_bm = $any->{be_select_bm}; | ||
| 511 | my $total = 0; | ||
| 512 | |||
| 513 | OUTER: | ||
| 514 | while (1) { | ||
| 515 | my $delay = 3; | ||
| 516 | unless ($eof_sent) { | ||
| 517 | my $window_write = $channel->window_write; | ||
| 518 | $debug and $debug & 1024 and _debug("window write: ", $window_write); | ||
| 519 | if ($window_write) { | ||
| 520 | if (length $in < $stdin_buffer_size and not $in_at_eof) { | ||
| 521 | if ($in_data and @$in_data) { | ||
| 522 | $in .= shift @$in_data while @$in_data and length $in < $stdin_buffer_size; | ||
| 523 | } | ||
| 524 | elsif ($in_fh) { | ||
| 525 | if (ref $in_fh eq 'CODE') { | ||
| 526 | if (defined (my $data = $in_fh->($stdin_buffer_size - length $in))) { | ||
| 527 | $in .= $data; | ||
| 528 | } | ||
| 529 | else { | ||
| 530 | undef $in_fh; | ||
| 531 | } | ||
| 532 | } | ||
| 533 | else { | ||
| 534 | my $bytes = sysread($in_fh, $in, $stdin_buffer_size, length $in); | ||
| 535 | $debug and $debug and _debug "stdin sysread: ", $bytes, " \$!: ", $!; | ||
| 536 | if (not defined $bytes and $! == Errno::EAGAIN()) { | ||
| 537 | $in_refill = 1; | ||
| 538 | } | ||
| 539 | else { | ||
| 540 | $in_refill = 0; | ||
| 541 | unless ($bytes) { | ||
| 542 | $debug and $debug & 1024 and _debug "end of in file reached"; | ||
| 543 | undef $in_fh; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | } | ||
| 547 | } | ||
| 548 | else { | ||
| 549 | $debug and $debug & 1024 and _debug "in_at_eof = 1"; | ||
| 550 | $in_at_eof = 1; | ||
| 551 | } | ||
| 552 | } | ||
| 553 | if (length $in) { | ||
| 554 | $debug and $debug & 1024 and _debug "bytes in stdin buffer: ", length $in; | ||
| 555 | if (select(undef, "$select_bm", undef, 0) > 0) { | ||
| 556 | my $bytes = $any->_channel_do($channel, 1, 'write', $in); | ||
| 557 | defined $bytes or last OUTER; | ||
| 558 | if ($bytes) { | ||
| 559 | $total += $bytes; | ||
| 560 | $delay = 0; | ||
| 561 | substr($in, 0, $bytes, ''); | ||
| 562 | } | ||
| 563 | } | ||
| 564 | else { | ||
| 565 | $debug and $debug & 1024 and _debug "socket is not ready for writting"; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | elsif ($in_at_eof) { | ||
| 569 | $any->_channel_do($channel, 1, 'send_eof') or last; | ||
| 570 | $eof_sent = 1; | ||
| 571 | } | ||
| 572 | } | ||
| 573 | } | ||
| 574 | unless ($eof_received) { | ||
| 575 | if ($debug and $debug & 1024) { | ||
| 576 | my ($size, $avail, $size0) = $channel->window_read; | ||
| 577 | _debug "window_read avail: $avail, size: $size/$size0"; | ||
| 578 | } | ||
| 579 | for my $ext (0, 1) { | ||
| 580 | my $bytes = $any->_channel_do($channel, 0, 'read', $out, $stdout_buffer_size, $ext); | ||
| 581 | defined $bytes or last OUTER; | ||
| 582 | if ($bytes) { | ||
| 583 | $total += $bytes; | ||
| 584 | $delay = 0; | ||
| 585 | if ($out_fh[$ext]) { | ||
| 586 | __write_all($any, $out_fh[$ext], $out) or last OUTER; | ||
| 587 | } | ||
| 588 | else { | ||
| 589 | $cap[$ext] .= $out; | ||
| 590 | } | ||
| 591 | # we reuse out to avoid the allocation of 256KB every time | ||
| 592 | $out = ''; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | if ($channel->eof) { | ||
| 596 | $eof_received = 1; | ||
| 597 | $debug and $debug & 1024 and _debug "eof_received"; | ||
| 598 | for my $fh (@out_fh) { | ||
| 599 | __write_all($any, $_) or last OUTER; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | } | ||
| 603 | last if $eof_sent and $eof_received; | ||
| 604 | |||
| 605 | $debug and $debug & 1024 and _debug "channel receive_window: ", join(', ', $channel->window_read); | ||
| 606 | |||
| 607 | if ($timeout) { | ||
| 608 | if ($delay) { | ||
| 609 | my $now = Time::HiRes::time(); | ||
| 610 | $start ||= $now; | ||
| 611 | if ($now - $start > $timeout) { | ||
| 612 | $any->_set_error(SSHA_TIMEOUT_ERROR, "command timed out"); | ||
| 613 | last; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | else { | ||
| 617 | undef $start; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | $any->_wait_for_data(!$eof_sent, $delay, ($in_refill ? [$in_fh] : ())); | ||
| 621 | } | ||
| 622 | |||
| 623 | # clear buffer memory | ||
| 624 | undef $in; undef $out; | ||
| 625 | |||
| 626 | $debug and $debug & 1024 and _debug "data moved:", $total; | ||
| 627 | |||
| 628 | $any->_channel_close($channel); | ||
| 629 | return @cap; | ||
| 630 | } | ||
| 631 | |||
| 632 | sub _wait_for_data { | ||
| 633 | my ($any, $write, $max_delay, $extra_read) = @_; | ||
| 634 | $debug and $debug & 1024 and _debug "waiting for data, write: " . ($write//'<undef>') . | ||
| 635 | ", max_delay: " . ($max_delay//'<undef>') . ", extra_read: " . ($extra_read//'<undef>'); | ||
| 636 | if ($max_delay) { | ||
| 637 | my $rbm = $any->{be_select_bm}; | ||
| 638 | my $wbm = ($write ? $rbm : ''); | ||
| 639 | if ($extra_read) { | ||
| 640 | vec($rbm, fileno($_), 1) = 1 for @$extra_read; | ||
| 641 | } | ||
| 642 | my $n = select($rbm, $wbm, undef, $max_delay); | ||
| 643 | $debug and $debug & 1024 and _debug "active sockets: ", $n; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | sub _channel_read { | ||
| 648 | my $any = shift; | ||
| 649 | my $channel = shift; | ||
| 650 | my $blocking = shift; | ||
| 651 | while (1) { | ||
| 652 | $debug and $debug & 1024 and _debug "iterating inside _channel_read loop"; | ||
| 653 | my $rc = $any->_channel_do($channel, 0, 'read', @_); | ||
| 654 | return $rc if $rc or not defined $rc or not $blocking; | ||
| 655 | return if $channel->eof; | ||
| 656 | $any->_wait_for_data(0, 1); | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | sub _dpipe { | ||
| 661 | my ($any, $opts, $cmd) = @_; | ||
| 662 | my ($channel) = __open_channel_and_exec($any, $opts, $cmd) or return; | ||
| 663 | # TODO: do something with the parsed options? | ||
| 664 | require Net::SSH::Any::Backend::Net_SSH2::DPipe; | ||
| 665 | Net::SSH::Any::Backend::Net_SSH2::DPipe->_make($any, $channel); | ||
| 666 | } | ||
| 667 | |||
| 668 | sub _sftp { | ||
| 669 | my ($any, $opts) = @_; | ||
| 670 | my $ssh2 = $any->{be_ssh2} or return; | ||
| 671 | my $dpipe = $any->dpipe({subsystem => 1}, 'sftp'); | ||
| 672 | require Net::SSH::Any::Backend::Net_SSH2::SFTP; | ||
| 673 | my $sftp_backend = Net::SSH::Any::Backend::Net_SSH2::SFTP->_new($any, $dpipe); | ||
| 674 | my $sftp = Net::SFTP::Foreign->new(backend => $sftp_backend, | ||
| 675 | autodisconnect => 2, | ||
| 676 | %$opts); | ||
| 677 | if ($sftp->error) { | ||
| 678 | $any->_set_error(SSHA_CHANNEL_ERROR, $sftp->error); | ||
| 679 | } | ||
| 680 | $sftp; | ||
| 681 | } | ||
| 682 | |||
| 683 | 1; |