| 1 | package Net::SSH::Any::Backend::Dbclient_Cmd; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use Net::SSH::Any::Util qw(_first_defined _array_or_scalar_to_list $debug _debug); | ||
| 8 | use Net::SSH::Any::Constants qw(SSHA_CONNECTION_ERROR SSHA_UNIMPLEMENTED_ERROR); | ||
| 9 | |||
| 10 | require Net::SSH::Any::Backend::_Cmd; | ||
| 11 | our @ISA = qw(Net::SSH::Any::Backend::_Cmd); | ||
| 12 | |||
| 13 | sub _validate_backend_opts { | ||
| 14 | my ($any, %be_opts) = @_; | ||
| 15 | $any->SUPER::_validate_backend_opts(%be_opts) or return; | ||
| 16 | |||
| 17 | defined $be_opts{host} or croak "host argument missing"; | ||
| 18 | my ($auth_type, $interactive_login); | ||
| 19 | |||
| 20 | $be_opts{local_dbclient_cmd} //= $any->_find_cmd(dbclient => undef, | ||
| 21 | { POSIX => 'Dropbear', | ||
| 22 | MSWin => 'Cygwin' }) // return; | ||
| 23 | |||
| 24 | $be_opts{dbk_path} //= "$be_opts{key_path}.dbk" if defined $be_opts{key_path}; | ||
| 25 | |||
| 26 | if (defined $be_opts{password}) { | ||
| 27 | # $auth_type = 'password'; | ||
| 28 | # $interactive_login = 1; | ||
| 29 | # if (my @too_more = grep defined($be_opts{$_}), qw(key_path passphrase)) { | ||
| 30 | # croak "option(s) '".join("', '", @too_more)."' can not be used together with 'password'" | ||
| 31 | # } | ||
| 32 | $any->_set_error(SSHA_UNIMPLEMENTED_ERROR, | ||
| 33 | "password authentication is not supported by the Dbclient_Cmd backend"); | ||
| 34 | return | ||
| 35 | } | ||
| 36 | elsif (defined (my $dbk = $be_opts{dbk_path})) { | ||
| 37 | $auth_type = 'publickey'; | ||
| 38 | unless (-e $dbk) { | ||
| 39 | my $key = $be_opts{key_path} // do { | ||
| 40 | $dbk =~ /^(.+)\.dbk$/ or do { | ||
| 41 | $any->_set_error(SSHA_CONNECTION_ERROR, 'cannot generate dropbear key file'); | ||
| 42 | return; | ||
| 43 | }; | ||
| 44 | $1; | ||
| 45 | }; | ||
| 46 | unless (-e $key) { | ||
| 47 | $any->_set_error(SSHA_CONNECTION_ERROR, "key file '$key' does not exists"); | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | my $convert_cmd = $be_opts{local_dropbearconvert_cmd} //= | ||
| 53 | $any->_find_cmd(dropbearconvert => $be_opts{local_dbclient_cmd}, | ||
| 54 | { POSIX => 'Dropbear', | ||
| 55 | MSWin => 'Cygwin' }, | ||
| 56 | '/usr/lib/dropbear/dropbearconvert') // return; | ||
| 57 | local $?; | ||
| 58 | my @cmd = ($convert_cmd, 'openssh', 'dropbear', $key, $dbk); | ||
| 59 | $debug and $debug & 1024 and _debug "generating dbk file with command '".join("', '", @cmd)."'"; | ||
| 60 | # FIXME: redirect command stderr to /dev/null | ||
| 61 | if (do { no warnings 'exec'; system @cmd}) { | ||
| 62 | $any->_set_error(SSHA_CONNECTION_ERROR, 'dropbearconvert failed, rc: ' . ($? >> 8)); | ||
| 63 | return | ||
| 64 | } | ||
| 65 | unless (-e $dbk) { | ||
| 66 | $any->_set_error(SSHA_CONNECTION_ERROR, 'dropbearconvert failed to convert key to dropbear format'); | ||
| 67 | return | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $auth_type = 'default'; | ||
| 73 | } | ||
| 74 | |||
| 75 | $be_opts{dbclient_opt_y} = 1 | ||
| 76 | unless $be_opts{strict_host_key_checking}; | ||
| 77 | |||
| 78 | if (defined (my $knp = $be_opts{known_hosts_path})) { | ||
| 79 | if (!$be_opts{strict_host_key_checking} and | ||
| 80 | $any->_os_unix_path($knp) eq '/dev/null') { | ||
| 81 | $be_opts{dbclient_opt_yy} = 1; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $any->_set_error(SSHA_CONNECTION_ERROR, | ||
| 85 | "dbclient does not support the given combination of " . | ||
| 86 | "strict_host_key_checking and known_hosts_path options"); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | $any->{be_opts} = \%be_opts; | ||
| 92 | $any->{be_auth_type} = $auth_type; | ||
| 93 | $any->{be_interactive_login} = $interactive_login; | ||
| 94 | 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _make_cmd { | ||
| 98 | my ($any, $cmd_opts, @cmd) = @_; | ||
| 99 | my $be_opts = $any->{be_opts}; | ||
| 100 | |||
| 101 | my @args = ( $be_opts->{local_dbclient_cmd} ); | ||
| 102 | |||
| 103 | push @args, -l => $be_opts->{user} if defined $be_opts->{user}; | ||
| 104 | push @args, -p => $be_opts->{port} if defined $be_opts->{port}; | ||
| 105 | push @args, -i => $be_opts->{dbk_path} if defined $be_opts->{dbk_path}; | ||
| 106 | |||
| 107 | push @args, _array_or_scalar_to_list($be_opts->{dbclient_opts}) | ||
| 108 | if defined $be_opts->{dbclient_opts}; | ||
| 109 | |||
| 110 | push @args, '-y' if $be_opts->{dbclient_opt_y}; | ||
| 111 | push @args, '-y' if $be_opts->{dbclient_opt_yy}; | ||
| 112 | |||
| 113 | push @args, '-s' if delete $cmd_opts->{subsystem}; | ||
| 114 | push @args, $be_opts->{host}; | ||
| 115 | |||
| 116 | if ($any->{be_auth_type} eq 'password') { | ||
| 117 | # croak "password authentication is not supported yet by the dropbear backend"; | ||
| 118 | } | ||
| 119 | |||
| 120 | return (@args, @cmd); | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | 1; |