Any/Backend/Sexec_Cmd.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 116 CC 13 SCAR 33.4
CC
13
Cov
0%
_make_cmd
56.0
_remap_child_error
30.0
_validate_backend_opts
12.0
CRAP 28.1
1 package Net::SSH::Any::Backend::Sexec_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 SSHA_CHANNEL_ERROR SSHA_REMOTE_CMD_ERROR);
8  
9 use parent 'Net::SSH::Any::Backend::_Cmd';
10  
11 sub _validate_backend_opts {
12 my ($any, %opts) = @_;
13 $any->SUPER::_validate_backend_opts(%opts) or return;
14  
15 grep defined $opts{$_}, qw(profile_path host)
16 or croak "host argument missing";
17  
18 my @auth_type;
19 if (defined $opts{password}) {
20 push @auth_type, 'password';
21 #if (my @too_much = grep defined($opts{$_}), qw(key_path passphrase)) {
22 # croak "option(s) '".join("', '", @too_much)."' can not be used together with 'password'"
23 #}
24 }
25 elsif (defined (my $key = $opts{key_path})) {
26 push @auth_type, 'publickey';
27 croak "pubkey authentication not support yet by Sexec_Cmd backend";
28 # my $ppk = "$key.ppk";
29 # $opts{ppk_path} = $ppk;
30 # unless (-e $ppk) {
31 # local $?;
32 # my $cmd = _first_defined $opts{local_puttygen_cmd},
33 # $any->{local_cmd}{puttygen}, 'puttygen';
34 # my @cmd = ($cmd, -O => 'private', -o => $ppk, $key);
35 # $debug and $debug & 1024 and _debug "generating ppk file with command '".join("', '", @cmd)."'";
36 # if (system @cmd) {
37 # $any->_set_error(SSHA_CONNECTION_ERROR, 'puttygen failed, rc: ' . ($? >> 8));
38 # return
39 # }
40 # unless (-e $ppk) {
41 # $any->_set_error(SSHA_CONNECTION_ERROR, 'puttygen failed to convert key to PPK format');
42 # return
43 # }
44 #}
45 }
46 else {
47 # $auth_type = 'default';
48 }
49  
50 $opts{local_sexec_cmd} = _first_defined $opts{local_sexec_cmd}, $any->{local_cmd}{sexec}, 'sexec';
51 $any->{be_opts} = \%opts;
52 $any->{be_auth_type} = join(',', @auth_type);
53 $any->{be_interactive_login} = 0;
54 1;
55 }
56  
57 sub _make_cmd {
58 my ($any, $cmd_opts, $cmd) = @_;
59 my $be_opts = $any->{be_opts};
60  
61 my ($sexec, $host, $profile_path, $user, $password, $port) =
62 @{$be_opts}{qw(local_sexec_cmd host profile_path user password port)};
63  
64 my @args = ($sexec, "-unat=y");
65 if (defined $profile_path) {
66 push @args, "-profile=$profile_path";
67 push @args, "-host=$host" if defined $host;
68 }
69 else {
70 push @args, $host;
71 }
72 push @args, "-user=$user" if defined $user;
73 push @args, "-port=$port" if defined $port;
74 push @args, "-pw=$password" if defined $password;
75  
76 push @args, _array_or_scalar_to_list($be_opts->{sexec_opts})
77 if defined $be_opts->{sexec_opts};
78  
79 delete $cmd_opts->{subsystem} and croak "running subsystems is not supported by backend";
80  
81 return (@args, "-cmd=$cmd");
82 }
83  
84 my %sexec_error_str = ( 0 => 'Success',
85 2 => 'Usage error',
86 100 => 'SSH session failure',
87 101 => 'Failure connecting to server',
88 102 => 'SSH host authentication failure',
89 103 => 'SSH user authentication failure',
90 200 => 'Session channel open request was rejected'.
91 201 => 'Execution request was rejected',
92 900 => 'The remote process was terminated by a signal',
93 901 => 'The remote process was terminated by a signal with core dumped' );
94  
95 sub _remap_child_error {
96 my ($any, $proc) = @_;
97 if (my $native_rc = $proc->{native_rc}) {
98 # if ($native_rc == 0) { do_nothing() }
99 # already handled by previous condition
100  
101 my $errstr = $sexec_error_str{$native_rc} // 'Unknown failure';
102 if ($native_rc < 900) {
103 $any->_os_set_error(SSHA_CHANNEL_ERROR, $errstr);
104 $proc->{rc} = 255;
105 }
106 elsif ($native_rc < 1000) {
107 $any->_os_set_error(SSHA_REMOTE_CMD_ERROR, $errstr);
108 $proc->{rc} = (1 << 8) | ($native_rc == 900 ? 0x7f : 0xff);
109 }
110 else {
111 $proc->{rc} = ($native_rc - 1000) << 8;
112 }
113 $debug and $debug & 1024 and _debug "native rc $native_rc mapped to $proc->{rc}";
114 }
115 1;
116 }
117  
118 1;
119  
120 __END__