Any/Backend/Ssh_Cmd.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 128 CC 16 SCAR 40.5
CC
16
Cov
0%
_make_cmd
110.0
_validate_backend_opts
42.0
_remap_child_error
6.0
CRAP 57.3
1 package Net::SSH::Any::Backend::Ssh_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_CHANNEL_ERROR SSHA_REMOTE_CMD_ERROR SSHA_CONNECTION_ERROR);
8 use parent 'Net::SSH::Any::Backend::_Cmd';
9  
10 sub _validate_backend_opts {
11 my ($any, %be_opts) = @_;
12 $any->SUPER::_validate_backend_opts(%be_opts) or return;
13  
14 $be_opts{local_ssh_cmd} //= $any->_find_cmd('ssh',
15 undef,
16 { MSWin => 'Cygwin', POSIX => 'OpenSSH' },
17 '/usr/bin/ssh') // return;
18 my $out = $any->_local_capture($be_opts{local_ssh_cmd}, '-V');
19 if ($?) {
20 $out =~ s/\s+/ /gs; $out =~ s/ $//;
21 $any->_set_error(SSHA_CONNECTION_ERROR, 'ssh not found or bad version, rc: '.($? >> 8)." output: $out");
22 return;
23 }
24  
25 my ($auth_type, $interactive_login);
26  
27 if (defined $be_opts{password}) {
28 $auth_type = 'password';
29 $interactive_login = 1;
30 if (my @too_more = grep defined($be_opts{$_}), qw(key_path passphrase)) {
31 croak "option(s) '".join("', '", @too_more)."' can not be used together with 'password'"
32 }
33 }
34 elsif (defined $be_opts{key_path}) {
35 $auth_type = 'publickey';
36 if (defined $be_opts{passphrase}) {
37 $auth_type .= ' with passphrase';
38 $interactive_login = 1;
39 }
40 }
41 else {
42 $auth_type = 'default';
43 }
44  
45 $any->{be_opts} = \%be_opts;
46 $any->{be_auth_type} = $auth_type;
47 $any->{be_interactive_login} = $interactive_login;
48 1;
49 }
50  
51 sub _make_cmd {
52 my ($any, $cmd_opts, $cmd) = @_;
53 my $be_opts = $any->{be_opts};
54  
55 my @args = ( $be_opts->{local_ssh_cmd},
56 $be_opts->{host} );
57 push @args, '-C';
58 push @args, -l => $be_opts->{user} if defined $be_opts->{user};
59 push @args, -p => $be_opts->{port} if defined $be_opts->{port};
60 push @args, -i => $any->_os_unix_path($be_opts->{key_path}) if defined $be_opts->{key_path};
61 push @args, -o => 'BatchMode=yes' unless grep defined($be_opts->{$_}), qw(password passphrase);
62 push @args, -o => 'StrictHostKeyChecking=no' unless $be_opts->{strict_host_key_checking};
63 push @args, -o => 'UserKnownHostsFile=' . $any->_os_unix_path($be_opts->{known_hosts_path})
64 if defined $be_opts->{known_hosts_path};
65  
66 if ($any->{be_auth_type} eq 'password') {
67 push @args, ( -o => 'PreferredAuthentications=keyboard-interactive,password',
68 -o => 'NumberOfPasswordPrompts=1' );
69 }
70 else {
71 push @args, -o => 'PreferredAuthentications=publickey';
72 }
73  
74 push @args, '-s' if delete $cmd_opts->{subsystem};
75  
76 push @args, _array_or_scalar_to_list($be_opts->{ssh_opts})
77 if defined $be_opts->{ssh_opts};
78  
79 return (@args, '--', $cmd);
80 }
81  
82 sub _remap_child_error {
83 my ($any, $proc) = @_;
84 my $rc = $proc->{rc} // 0;
85 if ($rc == (255 << 8)) {
86 # A remote command may actually exit with code 255, but it
87 # is quite uncommon.
88 # SSHA_CONNECTION_ERROR is not recoverable so we use
89 # SSHA_CHANNEL_ERROR instead.
90 $debug and $debug & 1024 and _debug "child error remaped to channel error";
91 $any->_or_set_error(SSHA_CHANNEL_ERROR, "child command exited with code 255, ssh was probably unable to connect to the remote server");
92 return
93 }
94 1;
95 }
96  
97 1;