| 1 | package Net::SSH::Any::Backend::Net_OpenSSH; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | BEGIN { die "Net::OpenSSH does not work on Windows" if $^O =~ /Win32|Win64|Cygwin/i } | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | our @CARP_NOT = qw(Net::SSH::Any); | ||
| 10 | |||
| 11 | require Net::SSH::Any::Backend::_Cmd; | ||
| 12 | our @ISA = qw(Net::SSH::Any::Backend::_Cmd); | ||
| 13 | |||
| 14 | use Net::SSH::Any::Util; | ||
| 15 | use Net::SSH::Any::Constants qw(:error); | ||
| 16 | use Net::OpenSSH; | ||
| 17 | use Net::OpenSSH::Constants qw(:error); | ||
| 18 | |||
| 19 | sub _backend_api_version { 2 } | ||
| 20 | |||
| 21 | my @error_translation; | ||
| 22 | $error_translation[OSSH_MASTER_FAILED ] = SSHA_CONNECTION_ERROR; | ||
| 23 | $error_translation[OSSH_SLAVE_FAILED ] = SSHA_CHANNEL_ERROR; | ||
| 24 | $error_translation[OSSH_SLAVE_PIPE_FAILED] = SSHA_LOCAL_IO_ERROR; | ||
| 25 | $error_translation[OSSH_SLAVE_TIMEOUT ] = SSHA_TIMEOUT_ERROR; | ||
| 26 | $error_translation[OSSH_SLAVE_CMD_FAILED ] = SSHA_REMOTE_CMD_ERROR; | ||
| 27 | $error_translation[OSSH_SLAVE_SFTP_FAILED] = SSHA_CHANNEL_ERROR; | ||
| 28 | $error_translation[OSSH_ENCODING_ERROR ] = SSHA_ENCODING_ERROR; | ||
| 29 | |||
| 30 | sub __check_and_copy_error { | ||
| 31 | my $any = shift; | ||
| 32 | if (my $ssh = $any->{be_ssh}) { | ||
| 33 | my $error = $ssh->error or return 1; | ||
| 34 | $any->_set_error($error_translation[$error] // SSHA_CHANNEL_ERROR, $error); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | $any->_set_error(SSHA_CONNECTION_ERROR, "Unable to create Net::OpenSSH object"); | ||
| 38 | } | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _validate_backend_opts { | ||
| 43 | my ($any, %be_opts) = @_; | ||
| 44 | $any->SUPER::_validate_backend_opts(%be_opts) or return; | ||
| 45 | |||
| 46 | my $instance = $be_opts{instance} // do { | ||
| 47 | |||
| 48 | my $be_opts{ssh_cmd} //= delete $be_opts{local_ssh_cmd} // $any->_find_cmd($_, $be_opts{ssh_cmd}, 'OpenSSH'); | ||
| 49 | |||
| 50 | for (qw(rsync sshfs scp)) { | ||
| 51 | $be_opts{"${_}_cmd"} //= delete $be_opts{"local_${_}_cmd"} // | ||
| 52 | $any->_find_cmd({relaxed => 1}, $_, $be_opts{ssh_cmd}, 'OpenSSH'); | ||
| 53 | } | ||
| 54 | my @master_opts = _array_or_scalar_to_list delete $be_opts{master_opts}; | ||
| 55 | my $strict_host_key_checking = delete $be_opts{strict_host_key_checking}; | ||
| 56 | push @master_opts, -o => 'StrictHostKeyChecking='.($strict_host_key_checking ? 'yes' : 'no'); | ||
| 57 | my $known_hosts_path = delete $be_opts{known_hosts_path}; | ||
| 58 | push @master_opts, -o => "UserKnownHostsFile=$known_hosts_path" | ||
| 59 | if defined $known_hosts_path; | ||
| 60 | push @master_opts, '-C' if delete $be_opts{compress}; | ||
| 61 | delete $be_opts{io_timeout}; | ||
| 62 | Net::OpenSSH->new(%be_opts, master_opts => \@master_opts, connect => 0); | ||
| 63 | }; | ||
| 64 | $any->{be_opts} = \%be_opts; | ||
| 65 | $any->{be_ssh} = $instance; | ||
| 66 | __check_and_copy_error($any); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _make_cmd { shift->{be_ssh}->make_remote_command(@_) } | ||
| 70 | |||
| 71 | sub _check_connection { | ||
| 72 | my $any = shift; | ||
| 73 | $any->{be_ssh}->wait_for_master; | ||
| 74 | __check_and_copy_error($any); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _connect { shift->_check_connection } | ||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |