lib/Rex/Interface/Fs/LibSSH.pm

filter: Statementstmt 23.9% 33 / 138 Branchbran 7.4% 4 / 54 Conditioncond 16.6% 5 / 30 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 41.6% 10 / 24 Podpod 0.0% 0 / 14 totaltotal 18.9% 53 / 280 CC 42 SCAR 36.0
CC
42
Cov
19%
_brace_expand
132.0
glob
45.5
upload
30.0
CRAP 36.7
« lib/Rex/Interface/File/LibSSH.pm ↑ Summary blib/lib/Rex/LibSSH.pm »
1 # ABSTRACT: Rex filesystem operations via exec channels (no SFTP)
2  
3 package Rex::Interface::Fs::LibSSH;
4 3 our $VERSION = '0.005';
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Rex;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Rex::Interface::Fs::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use base qw(Rex::Interface::Fs::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
15 8 my ( $that, %args ) = @_;
16 8 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
17 8 return bless {%args}, $proto;
18 }
19  
20 0 ▶sub _run {
Subroutine _run: not called CC=1 SCAR=6.9
21 0 my ( $self, $cmd ) = @_;
22 0 my $exec = Rex::Interface::Exec->create('LibSSH');
23 0 return $exec->exec($cmd);
24 }
25  
26 0 ▶sub is_file {
Subroutine is_file: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my ( $self, $path ) = @_;
28 0 $path = _q($path);
29 0 $self->_run("test -f $path || test -c $path || test -b $path || test -p $path || test -S $path");
30 0 ▶ return $? == 0 ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
31 }
32  
33 0 ▶sub is_dir {
Subroutine is_dir: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my ( $self, $path ) = @_;
35 0 $path = _q($path);
36 0 $self->_run("test -d $path");
37 0 ▶ return $? == 0 ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
38 }
39  
40 0 ▶sub is_readable {
Subroutine is_readable: not called CC=2 SCAR=17.9
Pod undocumented
41 0 my ( $self, $path ) = @_;
42 0 $path = _q($path);
43 0 $self->_run("test -r $path");
44 0 ▶ return $? == 0 ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
45 }
46  
47 0 ▶sub is_writable {
Subroutine is_writable: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my ( $self, $path ) = @_;
49 0 $path = _q($path);
50 0 $self->_run("test -w $path");
51 0 ▶ return $? == 0 ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
52 }
53  
54 0 ▶sub readlink {
Subroutine readlink: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ( $self, $path ) = @_;
56 0 $path = _q($path);
57 0 my $out = $self->_run("readlink $path");
58 0 chomp $out;
59 0 return $out;
60 }
61  
62 0 ▶sub stat {
Subroutine stat: not called CC=3 SCAR=24.8
Pod undocumented
63 0 my ( $self, $path ) = @_;
64 0 $path = _q($path);
65  
66 # %a = octal perms, %s = size, %u = uid, %g = gid, %X = atime, %Y = mtime
67 0 my $out = $self->_run("stat -c '%a %s %u %g %X %Y' $path 2>/dev/null");
68 0 ▶ return undef unless $out && $out =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $out and $out =~ /\S/ 0 0 0
Condition0%0/3
$out and $out =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$out and $out =~ /\S/
$out $out =~ /\S/
Truth table0%0/3
A and B
A= $outB= $out =~ /\S/
ABresult
0X0
100
111
69 0 chomp $out;
70 0 my ( $mode, $size, $uid, $gid, $atime, $mtime ) = split /\s+/, $out;
71 return (
72 0 mode => sprintf( '%04o', oct($mode) ),
73 size => $size + 0,
74 uid => $uid + 0,
75 gid => $gid + 0,
76 atime => $atime + 0,
77 mtime => $mtime + 0,
78 );
79 }
80  
81 0 ▶sub ls {
Subroutine ls: not called CC=4 SCAR=30.0
Pod undocumented
82 0 my ( $self, $path ) = @_;
83 0 $path = _q($path);
84 0 my $out = $self->_run("ls -1a $path 2>/dev/null");
85 0 ▶ return () unless defined $out && length $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out and length $out 0 0 0
Condition0%0/3
defined $out and length $out
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and length $out
defined $out length $out
Truth table0%0/3
A and B
A= defined $outB= length $out
ABresult
0X0
100
111
86 0 ▶ return grep { $_ ne '.' && $_ ne '..' } split /\n/, $out;
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
87 }
88  
89 8 ▶sub glob {
Subroutine glob: called CC=9 SCAR=38.2
Pod undocumented
90 8 my ( $self, $pattern ) = @_;
91  
92 # The pattern is intentionally NOT _q()-wrapped: the remote shell
93 # has to expand *, ?, [...] in place. Quoting would suppress globbing.
94 # The security-relevant asymmetry: every other path here is _q()-wrapped;
95 # glob is the one place we trust the remote shell to parse something
96 # beyond a literal token. As a defense against a malicious or malformed
97 # pattern reaching this method, reject any character that would let the
98 # pattern break out of the command structure beyond what globbing
99 # allows. Legitimate glob patterns contain only path bytes and the
100 # glob meta-chars *, ?, [...], {a,b}, /, ., -, _, ~.
101 8 ▶ die "LibSSH glob: pattern contains NUL byte\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $pattern and index($pattern, "\0") != -1 0 8 8
Condition33%1/3
defined $pattern and index($pattern, "\0") != -1
!ll&&!rl&&r
080
MC/DC0%0/2
defined $pattern and index($pattern, "\0") != -1
defined $pattern index($pattern, "\0") != -1
Truth table33%1/3
A and B
A= defined $patternB= index($pattern, "\0") != -1
ABresult
0X0
100
111
102 if defined $pattern && index( $pattern, "\0" ) >= 0;
103 8 ▶ die "LibSSH glob: pattern contains shell metacharacter outside glob syntax\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $pattern and $pattern =~ /[\0';|&<>`$()\n\r\\]/ 8 0 8
Condition33%1/3
defined $pattern and $pattern =~ /[\0';|&<>`$()\n\r\\]/
!ll&&!rl&&r
008
MC/DC0%0/2
defined $pattern and $pattern =~ /[\0';|&<>`$()\n\r\\]/
defined $pattern $pattern =~ /[\0';|&<>`$()\n\r\\]/
Truth table33%1/3
A and B
A= defined $patternB= $pattern =~ /[\0';|&<>`$()\n\r\\]/
ABresult
0X0
100
111
104 if defined $pattern
105 && $pattern =~ /[\0';|&<>`$()\n\r\\]/;
106  
107 # Like perl's glob (stock Rex::Interface::Fs::SSH): braces expand
108 # locally and every alternative is returned; only an alternative with
109 # a wildcard (or a ~) goes to the remote shell, where unmatched ones
110 # yield nothing. A plain path is returned as given, existing or not.
111 0 my @result;
112 0 for my $alt ( _brace_expand($pattern) ) {
113 0 ▶ if ( $alt !~ /[*?\[~]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alt =~ /[*?\[~]/) 0 0 0
114 0 push @result, $alt;
115 0 next;
116 }
117  
118 # Blanks stay part of one word (backslash itself was rejected
119 # above, so this cannot unescape anything). The shell leaves
120 # unmatched wildcards verbatim, hence the -e/-L test; names are
121 # NUL-separated so whitespace in file names survives.
122 0 ( my $word = $alt ) =~ s/([ \t])/\\$1/g;
123 0 my $out = $self->_run(
124 qq{for f in $word; do if [ -e "\$f" ] || [ -L "\$f" ]; then printf '%s\\0' "\$f"; fi; done}
125 );
126 0 ▶ push @result, grep { length } split /\0/, $out
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and length $out 0 0 0
Condition0%0/3
defined $out and length $out
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and length $out
defined $out length $out
Truth table0%0/3
A and B
A= defined $outB= length $out
ABresult
0X0
100
111
127 if defined $out && length $out;
128 }
129 0 return @result;
130 }
131  
132 # Expand the first {a,b,...} group (nested groups recurse); an unbalanced
133 # or comma-less group is literal, as in perl's glob.
134 0 ▶sub _brace_expand {
Subroutine _brace_expand: not called CC=11 SCAR=48.8
135 0 my ($str) = @_;
136 0 my $start = index $str, '{';
137 0 ▶ return $str if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 0 0
138  
139 0 my ( $depth, @comma, $end ) = (0);
140 0 for my $i ( $start .. length($str) - 1 ) {
141 0 my $c = substr $str, $i, 1;
142 0 ▶ if ( $c eq '{' ) { $depth++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq '{') { } 0 0 0
elsif ($c eq '}') { } 0 0 0
elsif ($c eq ',' and $depth == 1) { } 0 0 0
143 0 ▶ elsif ( $c eq '}' ) { if ( --$depth == 0 ) { $end = $i; last } }
Branch: 1 branch
BranchTrueFalseTotal
if (--$depth == 0) 0 0 0
144 0 ▶ elsif ( $c eq ',' && $depth == 1 ) { push @comma, $i }
Condition0%0/3
$c eq "," and $depth == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$c eq "," and $depth == 1
$c eq "," $depth == 1
Truth table0%0/3
A and B
A= $c eq ","B= $depth == 1
ABresult
0X0
100
111
145 }
146 0 ▶ return $str if !defined $end || !@comma;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end and @comma 0 0 0
Condition0%0/3
defined $end and @comma
!ll&&!rl&&r
000
MC/DC0%0/2
defined $end and @comma
defined $end @comma
Truth table0%0/3
A and B
A= defined $endB= @comma
ABresult
0X0
100
111
147  
148 0 my ( $pre, $post ) = ( substr( $str, 0, $start ), substr( $str, $end + 1 ) );
149 0 my @alts;
150 0 my $from = $start + 1;
151 0 for my $to ( @comma, $end ) {
152 0 push @alts, substr $str, $from, $to - $from;
153 0 $from = $to + 1;
154 }
155 0 return map { _brace_expand( $pre . $_ . $post ) } @alts;
156 }
157  
158 0 ▶sub mkdir {
Subroutine mkdir: not called CC=2 SCAR=17.9
Pod undocumented
159 0 my ( $self, $path ) = @_;
160 0 $path = _q($path);
161 0 $self->_run("mkdir -p $path");
162 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
163 }
164  
165 0 ▶sub rename {
Subroutine rename: not called CC=2 SCAR=17.9
Pod undocumented
166 0 my ( $self, $from, $to ) = @_;
167 0 $self->_run( 'mv ' . _q($from) . ' ' . _q($to) );
168 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
169 }
170  
171 0 ▶sub unlink {
Subroutine unlink: not called CC=2 SCAR=17.9
Pod undocumented
172 0 my ( $self, @files ) = @_;
173 0 for my $f (@files) {
174 0 $self->_run( 'rm -f ' . _q($f) );
175 }
176 }
177  
178 0 ▶sub upload {
Subroutine upload: not called CC=5 SCAR=34.0
Pod undocumented
179 0 my ( $self, $local, $remote ) = @_;
180 0 $remote = _q($remote);
181  
182 0 my $content = do {
183 0 local $/;
184 0 ▶ open( my $fh, '<:raw', $local ) or die "upload: cannot read $local: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $local or die "upload: cannot read ${local}: $!" 0 0 0
185 0 <$fh>;
186 };
187  
188 0 ▶ my $ssh = Rex::is_ssh()
Branch: 1 branch
BranchTrueFalseTotal
my $ssh = Rex::is_ssh() or die "LibSSH upload: no active SSH connection" 0 0 0
189 or die "LibSSH upload: no active SSH connection";
190  
191 0 ▶ my $ch = $ssh->channel or die "LibSSH upload: failed to open channel";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = $ssh->channel or die "LibSSH upload: failed to open channel" 0 0 0
192 0 $ch->exec("cat > $remote");
193 0 $ch->write($content);
194 0 $ch->send_eof;
195 0 $ch->read; # drain any output
196 0 my $exit = $ch->exit_status;
197 0 $ch->close;
198  
199 0 ▶ die "upload: writing to $remote failed (exit $exit)" if $exit != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit != 0 0 0 0
200 }
201  
202 0 ▶sub download {
Subroutine download: not called CC=5 SCAR=34.0
Pod undocumented
203 0 my ( $self, $remote, $local ) = @_;
204 0 $remote = _q($remote);
205  
206 0 ▶ my $ssh = Rex::is_ssh()
Branch: 1 branch
BranchTrueFalseTotal
my $ssh = Rex::is_ssh() or die "LibSSH download: no active SSH connection" 0 0 0
207 or die "LibSSH download: no active SSH connection";
208  
209 0 ▶ my $ch = $ssh->channel or die "LibSSH download: failed to open channel";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = $ssh->channel or die "LibSSH download: failed to open channel" 0 0 0
210 0 $ch->exec("cat $remote");
211 0 my $content = $ch->read;
212 0 my $exit = $ch->exit_status;
213 0 $ch->close;
214  
215 0 ▶ die "download: reading $remote failed (exit $exit)" if $exit != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit != 0 0 0 0
216  
217 0 ▶ open( my $fh, '>:raw', $local ) or die "download: cannot write $local: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $local or die "download: cannot write ${local}: $!" 0 0 0
218 0 print $fh $content;
219 0 close $fh;
220 }
221  
222 # Shell-quote a single path component.
223 #
224 # Single-quote wrap with '\''-escaping of embedded single quotes is the
225 # canonical POSIX-safe form. Inside single quotes the shell performs no
226 # expansion, so this defends against spaces, $, `, \, newlines and the
227 # empty string automatically.
228 #
229 # NUL (\0) cannot be represented in argv (libc terminates arguments on
230 # NUL), so reject it loudly rather than letting libssh silently truncate.
231 9 ▶sub _q {
Subroutine _q: called CC=3 SCAR=11.0
232 9 my ($path) = @_;
233 9 ▶ die "LibSSH _q: path contains NUL byte\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and index($path, "\0") != -1 1 8 9
Condition66%2/3
defined $path and index($path, "\0") != -1
!ll&&!rl&&r
081
MC/DC50%1/2
defined $path and index($path, "\0") != -1
defined $path index($path, "\0") != -1
Truth table66%2/3
A and B
A= defined $pathB= index($path, "\0") != -1
ABresult
0X0
100
111
234 if defined $path && index( $path, "\0" ) >= 0;
235 8 $path =~ s/'/'"'"'/g;
236 8 return "'$path'";
237 }
238  
239 3 1;
240  
241 __END__
« lib/Rex/Interface/File/LibSSH.pm ↑ Summary blib/lib/Rex/LibSSH.pm »