| 1 | package Net::SSH::Perl::SSH2; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Net::SSH::Perl::Kex; | ||
| 6 | use Net::SSH::Perl::ChannelMgr; | ||
| 7 | use Net::SSH::Perl::Packet; | ||
| 8 | use Net::SSH::Perl::Buffer; | ||
| 9 | use Net::SSH::Perl::Constants qw( :protocol :msg2 :hosts | ||
| 10 | CHAN_INPUT_CLOSED CHAN_INPUT_WAIT_DRAIN | ||
| 11 | KEX_DEFAULT_PK_ALG ); | ||
| 12 | use Net::SSH::Perl::Cipher; | ||
| 13 | use Net::SSH::Perl::AuthMgr; | ||
| 14 | use Net::SSH::Perl::Comp; | ||
| 15 | use Net::SSH::Perl::Util qw( :hosts :win32 _read_yes_or_no ); | ||
| 16 | |||
| 17 | use base qw( Net::SSH::Perl ); | ||
| 18 | |||
| 19 | use Carp qw( croak ); | ||
| 20 | use File::Spec::Functions qw( catfile ); | ||
| 21 | use File::HomeDir (); | ||
| 22 | |||
| 23 | use Errno; | ||
| 24 | |||
| 25 | use vars qw( $VERSION $CONFIG $HOSTNAME ); | ||
| 26 | $VERSION = $Net::SSH::Perl::VERSION; | ||
| 27 | |||
| 28 | sub select_class { 'IO::Select' } | ||
| 29 | |||
| 30 | sub _dup { | ||
| 31 | my($fh, $mode) = @_; | ||
| 32 | |||
| 33 | if ( $^O eq 'MSWin32' ) { | ||
| 34 | # | ||
| 35 | # On Windows platform select() is working only for sockets. | ||
| 36 | # | ||
| 37 | my ( $r, $w ) = _socketpair() | ||
| 38 | or die "Could not create socketpair: $!\n"; | ||
| 39 | |||
| 40 | # TODO: full support (e.g. stdin) | ||
| 41 | |||
| 42 | return ( $mode eq '>' ) ? $w : $r; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $dup = Symbol::gensym; | ||
| 46 | my $str = "${mode}&$fh"; | ||
| 47 | open ($dup, $str) or die "Could not dupe: $!\n"; ## no critic | ||
| 48 | $dup; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub version_string { | ||
| 52 | my $class = shift; | ||
| 53 | sprintf "Net::SSH::Perl Version %s, protocol version %s.%s.", | ||
| 54 | $class->VERSION, PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _proto_init { | ||
| 58 | my $ssh = shift; | ||
| 59 | my $home = File::HomeDir->my_home; | ||
| 60 | my $config = $ssh->{config}; | ||
| 61 | |||
| 62 | unless ($config->get('user_known_hosts')) { | ||
| 63 | defined $home or croak "Cannot determine home directory, please set the environment variable HOME"; | ||
| 64 | $config->set('user_known_hosts', catfile($home, '.ssh', 'known_hosts2')); | ||
| 65 | } | ||
| 66 | unless ($config->get('global_known_hosts')) { | ||
| 67 | my $glob_known_hosts = $^O eq 'MSWin32' | ||
| 68 | ? catfile( $ENV{WINDIR}, 'ssh_known_hosts2' ) | ||
| 69 | : '/etc/ssh_known_hosts2'; | ||
| 70 | $config->set('global_known_hosts', $glob_known_hosts); | ||
| 71 | } | ||
| 72 | unless (my $if = $config->get('identity_files')) { | ||
| 73 | defined $home or croak "Cannot determine home directory, please set the environment variable HOME"; | ||
| 74 | $config->set('identity_files', [ catfile($home, '.ssh', 'id_ed25519'), | ||
| 75 | catfile($home, '.ssh', 'id_rsa'), catfile($home, '.ssh', 'id_ecdsa') ]); | ||
| 76 | } | ||
| 77 | |||
| 78 | for my $a (qw( password dsa kbd_interactive )) { | ||
| 79 | $config->set("auth_$a", 1) | ||
| 80 | unless defined $config->get("auth_$a"); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub kex { $_[0]->{kex} } | ||
| 85 | |||
| 86 | sub register_handler { | ||
| 87 | my($ssh, $type, $sub, @extra) = @_; | ||
| 88 | $ssh->{client_handlers}{$type} = { code => $sub, extra => \@extra }; | ||
| 89 | } | ||
| 90 | |||
| 91 | # handle SSH2_MSG_GLOBAL_REQUEST | ||
| 92 | sub client_input_global_request { | ||
| 93 | my $ssh = shift; | ||
| 94 | my $pack = shift or return; | ||
| 95 | my $req = $pack->get_str; | ||
| 96 | my $want_reply = ord $pack->get_char; | ||
| 97 | my $success = 0; | ||
| 98 | $ssh->debug("Global Request: $req, want reply: $want_reply"); | ||
| 99 | if ($req eq 'hostkeys-00@openssh.com') { | ||
| 100 | if ($ssh->config->get('hostkeys_seen')) { | ||
| 101 | die 'Server already sent hostkeys!'; | ||
| 102 | } | ||
| 103 | my $update_host_keys = $ssh->config->get('update_host_keys') || 'no'; | ||
| 104 | $success = $ssh->client_input_hostkeys($pack) | ||
| 105 | if $update_host_keys eq 'yes' || $update_host_keys eq 'ask'; | ||
| 106 | $ssh->config->set('hostkeys_seen', 1); | ||
| 107 | } | ||
| 108 | if ($want_reply) { | ||
| 109 | my $packet = $ssh->packet_start( | ||
| 110 | $success ? SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE | ||
| 111 | ); | ||
| 112 | $packet->send; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | # handle hostkeys-00@openssh.com global request from server | ||
| 117 | sub client_input_hostkeys { | ||
| 118 | my $ssh = shift; | ||
| 119 | my $pack = shift; | ||
| 120 | my %keys; | ||
| 121 | |||
| 122 | use Net::SSH::Perl::Key; | ||
| 123 | |||
| 124 | my $fp_hash = $ssh->config->get('fingerprint_hash'); | ||
| 125 | while ($pack->offset < $pack->length) { | ||
| 126 | my $blob = $pack->get_str; | ||
| 127 | my $key = Net::SSH::Perl::Key->new_from_blob($blob); | ||
| 128 | unless ($key) { | ||
| 129 | $ssh->debug("Invalid key sent by server"); | ||
| 130 | next; | ||
| 131 | } | ||
| 132 | |||
| 133 | my @allowed_key_types = split(',', | ||
| 134 | $ssh->config->get('host_key_algorithms') || | ||
| 135 | KEX_DEFAULT_PK_ALG); | ||
| 136 | my $key_type = $key->ssh_name; | ||
| 137 | $ssh->debug("Received $key_type key: " . $key->fingerprint($fp_hash)); | ||
| 138 | unless (grep { /^$key_type$/ } @allowed_key_types) { | ||
| 139 | unless ($key_type eq 'ssh-rsa' && grep { /^rsa-sha2-/ } @allowed_key_types) { | ||
| 140 | $ssh->debug("$key_type key not permitted by HostkeyAlgorithms"); | ||
| 141 | next; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | my $fp = $key->fingerprint($fp_hash); | ||
| 145 | if (exists $keys{$fp}) { | ||
| 146 | $ssh->debug("Received duplicate $key_type key"); | ||
| 147 | next; | ||
| 148 | } | ||
| 149 | $keys{$fp} = $key; | ||
| 150 | } | ||
| 151 | unless (%keys) { | ||
| 152 | $ssh->debug('Server sent no usable keys'); | ||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | |||
| 156 | my $host = $ssh->{host}; | ||
| 157 | my $port = $ssh->{config}->get('port'); | ||
| 158 | if (defined $port && $port =~ /\D/) { | ||
| 159 | my @serv = getservbyname(my $serv = $port, 'tcp'); | ||
| 160 | $port = $serv[2]; | ||
| 161 | } | ||
| 162 | my $u_hostfile = $ssh->{config}->get('user_known_hosts'); | ||
| 163 | my %known_keys = map { $_->fingerprint($fp_hash) => $_ } | ||
| 164 | _all_keys_for_host($host, $port, $u_hostfile); | ||
| 165 | |||
| 166 | my $retained = 0; | ||
| 167 | my @new_keys; | ||
| 168 | foreach my $fp (keys %keys) { | ||
| 169 | if ($known_keys{$fp}) { | ||
| 170 | $retained++; | ||
| 171 | } else { | ||
| 172 | push @new_keys, $keys{$fp}; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | my @deprecated_keys; | ||
| 177 | foreach my $fp (keys %known_keys) { | ||
| 178 | push @deprecated_keys, $known_keys{$fp} unless $keys{$fp}; | ||
| 179 | } | ||
| 180 | |||
| 181 | $ssh->debug(scalar(keys(%keys)) . ' keys from server: ' . scalar(@new_keys) . | ||
| 182 | " new, $retained retained. " . scalar(@deprecated_keys) . ' to remove'); | ||
| 183 | |||
| 184 | if ((@deprecated_keys || @new_keys) && $ssh->config->get('update_host_keys') eq 'ask') { | ||
| 185 | return 0 unless _read_yes_or_no("Update hostkeys in known hosts? (yes/no)", 'yes'); | ||
| 186 | } | ||
| 187 | foreach my $key (@deprecated_keys) { | ||
| 188 | $ssh->debug('Removing deprecated ' . $key->ssh_name . 'key from known hosts'); | ||
| 189 | _remove_host_from_hostfile($host, $port, $u_hostfile, $key); | ||
| 190 | } | ||
| 191 | |||
| 192 | if (@new_keys) { | ||
| 193 | my $packet = $ssh->packet_start(SSH2_MSG_GLOBAL_REQUEST); | ||
| 194 | $packet->put_str('hostkeys-prove-00@openssh.com'); | ||
| 195 | $packet->put_char(chr(1)); | ||
| 196 | foreach my $key (@new_keys) { | ||
| 197 | $packet->put_str($key->as_blob); | ||
| 198 | } | ||
| 199 | $packet->send; | ||
| 200 | $ssh->debug("Sent hostkeys-prove request"); | ||
| 201 | |||
| 202 | my $sigbuf = Net::SSH::Perl::Packet->read($ssh); | ||
| 203 | unless ($sigbuf && $sigbuf->type == SSH2_MSG_REQUEST_SUCCESS) { | ||
| 204 | $ssh->debug("hostkeys-prove request failed"); | ||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | $ssh->debug("Got hostkeys-prove request success"); | ||
| 208 | my @verified_keys; | ||
| 209 | foreach my $key (@new_keys) { | ||
| 210 | my $sig = $sigbuf->get_str; | ||
| 211 | # prepare signed data | ||
| 212 | my $data = Net::SSH::Perl::Buffer->new( MP => 'SSH2' ); | ||
| 213 | $data->put_str('hostkeys-prove-00@openssh.com'); | ||
| 214 | $data->put_str($ssh->session_id); | ||
| 215 | $data->put_str($key->as_blob); | ||
| 216 | # verify signature | ||
| 217 | unless ($key->verify($sig,$data->bytes)) { | ||
| 218 | $ssh->debug("Server failed to confirm ownership of " . | ||
| 219 | "private " . $ssh->ssh_name . " host key"); | ||
| 220 | next; | ||
| 221 | } | ||
| 222 | $ssh->debug('Learned new hostkey: ' . $key->ssh_name . ' ' . | ||
| 223 | $key->fingerprint($fp_hash)); | ||
| 224 | push @verified_keys, $key; | ||
| 225 | } | ||
| 226 | my %verified = map { $_->fingerprint($fp_hash) => 1 } @verified_keys; | ||
| 227 | my $hash_known_hosts = $ssh->config->get('hash_known_hosts'); | ||
| 228 | foreach my $key (@verified_keys) { | ||
| 229 | $ssh->debug('Adding ' . $key->ssh_name . ' key to known hosts'); | ||
| 230 | _add_host_to_hostfile($host, $port, $u_hostfile, $key, $hash_known_hosts); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | return 1; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub login { | ||
| 237 | my $ssh = shift; | ||
| 238 | $ssh->SUPER::login(@_); | ||
| 239 | my $suppress_shell = $_[2]; | ||
| 240 | $ssh->_login or $ssh->fatal_disconnect("Permission denied"); | ||
| 241 | |||
| 242 | $ssh->debug("Login completed, opening dummy shell channel."); | ||
| 243 | my $cmgr = $ssh->channel_mgr; | ||
| 244 | my $channel = $cmgr->new_channel( | ||
| 245 | ctype => 'session', local_window => 0, | ||
| 246 | local_maxpacket => 0, remote_name => 'client-session'); | ||
| 247 | $channel->open; | ||
| 248 | |||
| 249 | # check if a global request was sent | ||
| 250 | my $packet = Net::SSH::Perl::Packet->read($ssh); | ||
| 251 | if ($packet && $packet->type == SSH2_MSG_GLOBAL_REQUEST) { | ||
| 252 | my $p = $packet; | ||
| 253 | $packet = Net::SSH::Perl::Packet->read_expect($ssh, | ||
| 254 | SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); | ||
| 255 | $ssh->client_input_global_request($p); | ||
| 256 | } elsif ($packet->type != SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) { | ||
| 257 | die "Unexpected " . $packet->type . " response!"; | ||
| 258 | } | ||
| 259 | $cmgr->input_open_confirmation($packet); | ||
| 260 | |||
| 261 | unless ($suppress_shell) { | ||
| 262 | $ssh->debug("Got channel open confirmation, requesting shell."); | ||
| 263 | $channel->request("shell", 0); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | sub _login { | ||
| 268 | my $ssh = shift; | ||
| 269 | |||
| 270 | my $kex = Net::SSH::Perl::Kex->new($ssh); | ||
| 271 | $kex->exchange; | ||
| 272 | |||
| 273 | my $amgr = Net::SSH::Perl::AuthMgr->new($ssh); | ||
| 274 | $amgr->authenticate; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _session_channel { | ||
| 278 | my $ssh = shift; | ||
| 279 | my $cmgr = $ssh->channel_mgr; | ||
| 280 | |||
| 281 | my $channel = $cmgr->new_channel( | ||
| 282 | ctype => 'session', local_window => 32*1024, | ||
| 283 | local_maxpacket => 16*1024, remote_name => 'client-session', | ||
| 284 | rfd => _dup('STDIN', '<'), wfd => _dup('STDOUT', '>'), | ||
| 285 | efd => _dup('STDERR', '>')); | ||
| 286 | |||
| 287 | $channel; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub _make_input_channel_req { | ||
| 291 | my($r_exit) = @_; | ||
| 292 | return sub { | ||
| 293 | my($channel, $packet) = @_; | ||
| 294 | my $rtype = $packet->get_str; | ||
| 295 | my $reply = $packet->get_int8; | ||
| 296 | $channel->{ssh}->debug("input_channel_request: rtype $rtype reply $reply"); | ||
| 297 | if ($rtype eq "exit-status") { | ||
| 298 | $$r_exit = $packet->get_int32; | ||
| 299 | } | ||
| 300 | if ($reply) { | ||
| 301 | my $r_packet = $channel->{ssh}->packet_start(SSH2_MSG_CHANNEL_SUCCESS); | ||
| 302 | $r_packet->put_int($channel->{remote_id}); | ||
| 303 | $r_packet->send; | ||
| 304 | } | ||
| 305 | }; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub cmd { | ||
| 309 | my $ssh = shift; | ||
| 310 | my($cmd, $stdin) = @_; | ||
| 311 | my $cmgr = $ssh->channel_mgr; | ||
| 312 | my $channel = $ssh->_session_channel; | ||
| 313 | $channel->open; | ||
| 314 | |||
| 315 | $channel->register_handler(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, sub { | ||
| 316 | my($channel, $packet) = @_; | ||
| 317 | |||
| 318 | ## Experimental pty support: | ||
| 319 | if ($ssh->{config}->get('use_pty')) { | ||
| 320 | $ssh->debug("Requesting pty."); | ||
| 321 | |||
| 322 | my $packet = $channel->request_start('pty-req', 0); | ||
| 323 | |||
| 324 | my($term) = $ENV{TERM} =~ /(\w+)/; | ||
| 325 | $packet->put_str($term); | ||
| 326 | my $foundsize = 0; | ||
| 327 | if (eval "require Term::ReadKey") { | ||
| 328 | my @sz = Term::ReadKey::GetTerminalSize($ssh->sock); | ||
| 329 | if (defined $sz[0]) { | ||
| 330 | $foundsize = 1; | ||
| 331 | $packet->put_int32($sz[1]); # height | ||
| 332 | $packet->put_int32($sz[0]); # width | ||
| 333 | $packet->put_int32($sz[2]); # xpix | ||
| 334 | $packet->put_int32($sz[3]); # ypix | ||
| 335 | } | ||
| 336 | } | ||
| 337 | if (!$foundsize) { | ||
| 338 | $packet->put_int32(0) for 1..4; | ||
| 339 | } | ||
| 340 | |||
| 341 | # Array used to build Pseudo-tty terminal modes; fat commas separate opcodes from values for clarity. | ||
| 342 | |||
| 343 | my $terminal_mode_string; | ||
| 344 | if(!defined($ssh->{config}->get('terminal_mode_string'))) { | ||
| 345 | my @terminal_modes = ( | ||
| 346 | 5 => 0,0,0,4, # VEOF => 0x04 (^d) | ||
| 347 | 0 # string must end with a 0 opcode | ||
| 348 | ); | ||
| 349 | for my $char (@terminal_modes) { | ||
| 350 | $terminal_mode_string .= chr($char); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | $terminal_mode_string = $ssh->{config}->get('terminal_mode_string'); | ||
| 355 | } | ||
| 356 | $packet->put_str($terminal_mode_string); | ||
| 357 | $packet->send; | ||
| 358 | } | ||
| 359 | |||
| 360 | my $r_packet = $channel->request_start("exec", 0); | ||
| 361 | $r_packet->put_str($cmd); | ||
| 362 | $r_packet->send; | ||
| 363 | |||
| 364 | if (defined $stdin) { | ||
| 365 | if($ssh->{config}->get('use_pty') && !$ssh->{config}->get('no_append_veof')) { | ||
| 366 | my $append_string = $ssh->{config}->get('stdin_append'); | ||
| 367 | if(!defined($append_string)) { | ||
| 368 | $append_string = chr(4) . chr(4); | ||
| 369 | } | ||
| 370 | $stdin .= $append_string; | ||
| 371 | } | ||
| 372 | $channel->send_data($stdin); | ||
| 373 | |||
| 374 | $channel->drain_outgoing; | ||
| 375 | $channel->{istate} = CHAN_INPUT_WAIT_DRAIN; | ||
| 376 | $channel->send_eof; | ||
| 377 | $channel->{istate} = CHAN_INPUT_CLOSED; | ||
| 378 | } | ||
| 379 | }); | ||
| 380 | |||
| 381 | my($exit); | ||
| 382 | $channel->register_handler(SSH2_MSG_CHANNEL_REQUEST, | ||
| 383 | _make_input_channel_req(\$exit)); | ||
| 384 | |||
| 385 | my $h = $ssh->{client_handlers}; | ||
| 386 | my($stdout, $stderr); | ||
| 387 | if (my $r = $h->{stdout}) { | ||
| 388 | $channel->register_handler("_output_buffer", | ||
| 389 | $r->{code}, @{ $r->{extra} }); | ||
| 390 | } | ||
| 391 | else { | ||
| 392 | $channel->register_handler("_output_buffer", sub { | ||
| 393 | $stdout .= $_[1]->bytes; | ||
| 394 | }); | ||
| 395 | } | ||
| 396 | if (my $r = $h->{stderr}) { | ||
| 397 | $channel->register_handler("_extended_buffer", | ||
| 398 | $r->{code}, @{ $r->{extra} }); | ||
| 399 | } | ||
| 400 | else { | ||
| 401 | $channel->register_handler("_extended_buffer", sub { | ||
| 402 | $stderr .= $_[1]->bytes; | ||
| 403 | }); | ||
| 404 | } | ||
| 405 | |||
| 406 | $ssh->debug("Entering interactive session."); | ||
| 407 | $ssh->client_loop; | ||
| 408 | |||
| 409 | ($stdout, $stderr, $exit); | ||
| 410 | } | ||
| 411 | |||
| 412 | sub shell { | ||
| 413 | my $ssh = shift; | ||
| 414 | my $cmgr = $ssh->channel_mgr; | ||
| 415 | my $channel = $ssh->_session_channel; | ||
| 416 | $channel->open; | ||
| 417 | |||
| 418 | $channel->register_handler(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, sub { | ||
| 419 | my($channel, $packet) = @_; | ||
| 420 | my $r_packet = $channel->request_start('pty-req', 0); | ||
| 421 | my($term) = $ENV{TERM} =~ /(\S+)/; | ||
| 422 | $r_packet->put_str($term); | ||
| 423 | my $foundsize = 0; | ||
| 424 | if (eval "require Term::ReadKey") { | ||
| 425 | my @sz = Term::ReadKey::GetTerminalSize($ssh->sock); | ||
| 426 | if (defined $sz[0]) { | ||
| 427 | $foundsize = 1; | ||
| 428 | $r_packet->put_int32($sz[0]); # width | ||
| 429 | $r_packet->put_int32($sz[1]); # height | ||
| 430 | $r_packet->put_int32($sz[2]); # xpix | ||
| 431 | $r_packet->put_int32($sz[3]); # ypix | ||
| 432 | } | ||
| 433 | } | ||
| 434 | if (!$foundsize) { | ||
| 435 | $r_packet->put_int32(0) for 1..4; | ||
| 436 | } | ||
| 437 | $r_packet->put_str(""); | ||
| 438 | $r_packet->send; | ||
| 439 | $channel->{ssh}->debug("Requesting shell."); | ||
| 440 | $channel->request("shell", 0); | ||
| 441 | }); | ||
| 442 | |||
| 443 | my($exit); | ||
| 444 | $channel->register_handler(SSH2_MSG_CHANNEL_REQUEST, | ||
| 445 | _make_input_channel_req(\$exit)); | ||
| 446 | |||
| 447 | $channel->register_handler("_output_buffer", sub { | ||
| 448 | syswrite STDOUT, $_[1]->bytes; | ||
| 449 | }); | ||
| 450 | $channel->register_handler("_extended_buffer", sub { | ||
| 451 | syswrite STDERR, $_[1]->bytes; | ||
| 452 | }); | ||
| 453 | |||
| 454 | $ssh->debug("Entering interactive session."); | ||
| 455 | $ssh->client_loop; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub open2 { | ||
| 459 | my $ssh = shift; | ||
| 460 | my($cmd) = @_; | ||
| 461 | |||
| 462 | require Net::SSH::Perl::Handle::SSH2; | ||
| 463 | |||
| 464 | my $cmgr = $ssh->channel_mgr; | ||
| 465 | my $channel = $ssh->_session_channel; | ||
| 466 | $channel->open; | ||
| 467 | |||
| 468 | $channel->register_handler(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, sub { | ||
| 469 | my($channel, $packet) = @_; | ||
| 470 | $channel->{ssh}->debug("Sending command: $cmd"); | ||
| 471 | my $r_packet = $channel->request_start("exec", 1); | ||
| 472 | $r_packet->put_str($cmd); | ||
| 473 | $r_packet->send; | ||
| 474 | }); | ||
| 475 | |||
| 476 | my $exit; | ||
| 477 | $channel->register_handler(SSH2_MSG_CHANNEL_REQUEST, sub { | ||
| 478 | my($channel, $packet) = @_; | ||
| 479 | my $rtype = $packet->get_str; | ||
| 480 | my $reply = $packet->get_int8; | ||
| 481 | $channel->{ssh}->debug("input_channel_request: rtype $rtype reply $reply"); | ||
| 482 | if ($rtype eq "exit-status") { | ||
| 483 | $exit = $packet->get_int32; | ||
| 484 | } | ||
| 485 | if ($reply) { | ||
| 486 | my $r_packet = $channel->{ssh}->packet_start(SSH2_MSG_CHANNEL_SUCCESS); | ||
| 487 | $r_packet->put_int($channel->{remote_id}); | ||
| 488 | $r_packet->send; | ||
| 489 | } | ||
| 490 | }); | ||
| 491 | |||
| 492 | my $reply = sub { | ||
| 493 | my($channel, $packet) = @_; | ||
| 494 | if ($packet->type == SSH2_MSG_CHANNEL_FAILURE) { | ||
| 495 | $channel->{ssh}->fatal_disconnect("Request for " . | ||
| 496 | "exec failed on channel '" . $packet->get_int32 . "'"); | ||
| 497 | } | ||
| 498 | $channel->{ssh}->break_client_loop; | ||
| 499 | }; | ||
| 500 | |||
| 501 | $cmgr->register_handler(SSH2_MSG_CHANNEL_FAILURE, $reply); | ||
| 502 | $cmgr->register_handler(SSH2_MSG_CHANNEL_SUCCESS, $reply); | ||
| 503 | |||
| 504 | $ssh->client_loop; | ||
| 505 | |||
| 506 | my $read = Symbol::gensym; | ||
| 507 | my $write = Symbol::gensym; | ||
| 508 | tie *$read, 'Net::SSH::Perl::Handle::SSH2', 'r', $channel, \$exit; | ||
| 509 | tie *$write, 'Net::SSH::Perl::Handle::SSH2', 'w', $channel, \$exit; | ||
| 510 | |||
| 511 | return ($read, $write); | ||
| 512 | } | ||
| 513 | |||
| 514 | sub break_client_loop { $_[0]->{_cl_quit_pending} = 1 } | ||
| 515 | sub restore_client_loop { $_[0]->{_cl_quit_pending} = 0 } | ||
| 516 | sub _quit_pending { $_[0]->{_cl_quit_pending} } | ||
| 517 | |||
| 518 | sub client_loop { | ||
| 519 | my $ssh = shift; | ||
| 520 | my $cmgr = $ssh->channel_mgr; | ||
| 521 | |||
| 522 | my $h = $cmgr->handlers; | ||
| 523 | my $select_class = $ssh->select_class; | ||
| 524 | |||
| 525 | CLOOP: | ||
| 526 | $ssh->{_cl_quit_pending} = 0; | ||
| 527 | while (!$ssh->_quit_pending) { | ||
| 528 | while (my $packet = Net::SSH::Perl::Packet->read_poll($ssh)) { | ||
| 529 | if (my $code = $h->{ $packet->type }) { | ||
| 530 | $code->($cmgr, $packet); | ||
| 531 | } | ||
| 532 | else { | ||
| 533 | $ssh->debug("Warning: ignore packet type " . $packet->type); | ||
| 534 | } | ||
| 535 | } | ||
| 536 | last if $ssh->_quit_pending; | ||
| 537 | |||
| 538 | $cmgr->process_output_packets; | ||
| 539 | |||
| 540 | my $rb = $select_class->new; | ||
| 541 | my $wb = $select_class->new; | ||
| 542 | $rb->add($ssh->sock); | ||
| 543 | $cmgr->prepare_channels($rb, $wb); | ||
| 544 | |||
| 545 | #last unless $cmgr->any_open_channels; | ||
| 546 | my $oc = grep { defined } @{ $cmgr->{channels} }; | ||
| 547 | last unless $oc > 1; | ||
| 548 | |||
| 549 | my($rready, $wready) = $select_class->select($rb, $wb); | ||
| 550 | unless (defined $rready or defined $wready) { | ||
| 551 | next if ( $!{EAGAIN} || $!{EINTR} ); | ||
| 552 | die "select: $!"; | ||
| 553 | } | ||
| 554 | |||
| 555 | $cmgr->process_input_packets($rready, $wready); | ||
| 556 | |||
| 557 | for my $ab (@$rready) { | ||
| 558 | if ($ab == $ssh->{session}{sock}) { | ||
| 559 | my $buf; | ||
| 560 | my $len = sysread $ab, $buf, 8192; | ||
| 561 | if (! defined $len) { | ||
| 562 | croak "Connection failed: $!\n"; | ||
| 563 | } | ||
| 564 | $ssh->break_client_loop if $len == 0; | ||
| 565 | ($buf) = $buf =~ /(.*)/s; ## Untaint data. Anything allowed. | ||
| 566 | $ssh->incoming_data->append($buf); | ||
| 567 | } | ||
| 568 | } | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | sub channel_mgr { | ||
| 573 | my $ssh = shift; | ||
| 574 | unless (defined $ssh->{channel_mgr}) { | ||
| 575 | $ssh->{channel_mgr} = Net::SSH::Perl::ChannelMgr->new($ssh); | ||
| 576 | } | ||
| 577 | $ssh->{channel_mgr}; | ||
| 578 | } | ||
| 579 | |||
| 580 | 1; | ||
| 581 | __END__ |