| 1 | package Net::SSH::Any::Backend::Plink_Cmd; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use Net::SSH::Any::Util qw(_first_defined _array_or_scalar_to_list $debug _debug); | ||
| 7 | use Net::SSH::Any::Constants qw(SSHA_CONNECTION_ERROR); | ||
| 8 | |||
| 9 | use parent 'Net::SSH::Any::Backend::_Cmd'; | ||
| 10 | |||
| 11 | sub _validate_backend_opts { | ||
| 12 | my ($any, %be_opts) = @_; | ||
| 13 | $any->SUPER::_validate_backend_opts(%be_opts) or return; | ||
| 14 | |||
| 15 | $be_opts{local_plink_cmd} //= $any->_find_cmd(plink => undef, 'PuTTY') // return; | ||
| 16 | |||
| 17 | my $out = $any->_local_capture($be_opts{local_plink_cmd}, '-V'); | ||
| 18 | if ($out =~ /plink:\s*(?:Unidentified\s+build\s*,|Pre-release|Release)\s*(.*?)\s*$/mi) { | ||
| 19 | $any->{be_plink_version} = $1; | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | $out =~ s/\s+/ /gs; $out =~ s/\s$//; | ||
| 23 | $any->_set_error(SSHA_CONNECTION_ERROR, "plink not found or bad version, output: $out"); | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | my ($auth_type, $interactive_login); | ||
| 28 | |||
| 29 | if (defined $be_opts{password}) { | ||
| 30 | $auth_type = 'password'; | ||
| 31 | if (my @too_much = grep defined($be_opts{$_}), qw(key_path passphrase)) { | ||
| 32 | croak "option(s) '".join("', '", @too_much)."' can not be used together with 'password'" | ||
| 33 | } | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | if (defined (my $key = $be_opts{key_path})) { | ||
| 37 | my $ppk = "$key.ppk"; | ||
| 38 | $be_opts{ppk_key_path} = $ppk; | ||
| 39 | unless (-e $ppk) { | ||
| 40 | local $?; | ||
| 41 | my $puttygen = $be_opts{local_puttygen_cmd} //= | ||
| 42 | $any->_find_cmd(puttygen => $be_opts{local_pink_cmd}, 'PuTTY') // return; | ||
| 43 | |||
| 44 | my @cmd = ($puttygen, -O => 'private', -o => $ppk, $key); | ||
| 45 | $debug and $debug & 1024 and _debug "generating ppk file with command '".join("', '", @cmd)."'"; | ||
| 46 | my $out = $any->_local_capture(@cmd); | ||
| 47 | if ($?) { | ||
| 48 | $out =~ s/\s+/ /gs; $out =~ s/\s$//; | ||
| 49 | $any->_set_error(SSHA_CONNECTION_ERROR, 'puttygen failed, rc: '.($? >> 8).", output: $out"); | ||
| 50 | return | ||
| 51 | } | ||
| 52 | } | ||
| 53 | # fallback | ||
| 54 | } | ||
| 55 | if (defined (my $ppk = $be_opts{ppk_key_path})) { | ||
| 56 | unless (-e $ppk) { | ||
| 57 | $any->_set_error(SSHA_CONNECTION_ERROR, 'puttygen failed to convert key to PPK format'); | ||
| 58 | return | ||
| 59 | } | ||
| 60 | $auth_type = 'publickey'; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $auth_type = 'default'; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | $any->{be_opts} = \%be_opts; | ||
| 68 | $any->{be_auth_type} = $auth_type; | ||
| 69 | $any->{be_interactive_login} = 0; | ||
| 70 | 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _make_cmd { | ||
| 74 | my ($any, $cmd_opts, $cmd) = @_; | ||
| 75 | my $be_opts = $any->{be_opts}; | ||
| 76 | |||
| 77 | my @args = ( $be_opts->{local_plink_cmd}, | ||
| 78 | '-ssh', | ||
| 79 | '-batch' ); | ||
| 80 | |||
| 81 | push @args, '-C' if $be_opts->{compress}; | ||
| 82 | push @args, -l => $be_opts->{user} if defined $be_opts->{user}; | ||
| 83 | push @args, -P => $be_opts->{port} if defined $be_opts->{port}; | ||
| 84 | push @args, -i => $be_opts->{ppk_key_path} if defined $be_opts->{ppk_key_path}; | ||
| 85 | |||
| 86 | if ($any->{be_auth_type} eq 'password') { | ||
| 87 | # Add some guard here, user should allow this thing explicitly! | ||
| 88 | push @args, -pw => $be_opts->{password}; | ||
| 89 | } | ||
| 90 | |||
| 91 | push @args, _array_or_scalar_to_list($be_opts->{plink_opts}) | ||
| 92 | if defined $be_opts->{plink_opts}; | ||
| 93 | |||
| 94 | push @args, '-s' if delete $cmd_opts->{subsystem}; | ||
| 95 | push @args, $be_opts->{host}; | ||
| 96 | |||
| 97 | return (@args, $cmd); | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ |