Chroot.pm

filter: Statementstmt 51.5% 51 / 99 Branchbran 26.4% 18 / 68 Conditioncond 12.1% 4 / 33 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 78.5% 11 / 14 Podpod 100% 10 / 10 totaltotal 39.1% 94 / 240 SCAR 63.3
CC
45
Cov
36%
rpath
462.0
vchdir
22.4
new
13.9
CRAP 563.5
↑ Summary example.pl »
1 package Filesys::Virtual::Chroot;
2 #
3 # Copyright (C) 2014 Colin Faber
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 #
18 # Original author: Colin Faber <cfaber@gmail.com>
19 # Original creation date: 11/12/2015
20 # Version: $Id: Chroot.pm,v 1.3 2015/11/13 05:26:02 cfaber Exp $
21 #
22  
23  
24 3 ▶our $VERSION = $1 if('$Revision: 1.3 $' =~ /([\d.]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if "\$Revision: 1.3 \$" =~ /([\d.]+)/ 3 0 3
25  
26 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 NAME
31  
32 Filesys::Virtual::Chroot - Virtual File system Tool
33  
34 =head1 DESCRIPTION
35  
36 Provide a virtual chroot environment. This module only simulates a
37 chroot environment and only provides a advisory functions for your
38 application. This module is B<NOT> intended to provide application
39 security!!!
40  
41  
42 =head1 SYNOPSIS
43  
44 #!/usr/bin/perl
45 use strict;
46 use Filesys::Virtual::Chroot;
47  
48 my $cr = Filesys::Virtual::Chroot->new(
49 c => '/tmp',
50 i => 0
51 ) || die $Filesys::Virtual::Chroot::errstr;
52  
53 print " Root: " . $cr->rroot . "\n";
54 print " Fake: " . $cr->vpwd . "\n";
55  
56 if($cr->vchdir($ARGV[0])){
57 print " Change directory success\r\n";
58 print " Root: " . $cr->rroot . "\n";
59 print " Real: " . $cr->rcwd . "\n";
60 print " Fake: " . $cr->vcwd . "\n";
61 } else {
62 print $cr->errstr . "\n";
63 }
64  
65 exit;
66  
67 =head1 METHODS
68  
69 =head2 new( %Options )
70  
71 Create a new Filesys::Virtual::Chroot object.
72  
73 =head3 Options
74  
75 chroot - The full path of the directory which will be virtual chroot'd
76  
77 c - Same as chroot
78  
79 no_force_case - Don't force case matching, Turn this on, on windows machines.
80  
81 i - Same as no_force_case
82  
83 =head3 Error handling
84  
85 If something happens which results in an error, nothing will be returned and the Filesys::Virtual::Chroot::errstr will be set with the error message.
86  
87 =cut
88  
89 sub new {
90 2 ▶ my ($class, %o) = @_;
Subroutine new: called CC=9 SCAR=26.3
Pod documented
91 my $self = bless {
92 i => (defined $o{i} ? $o{i} : $o{force_case}),
93 c => (defined $o{c} ? $o{c} : $o{chroot})
94 2 ▶ }, __PACKAGE__;
Branch: 2 branches
BranchTrueFalseTotal
defined $o{'i'} ? : 2 0 2
defined $o{'c'} ? : 2 0 2
95  
96 2 ▶ if(!defined $self->{c}){
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $self->{'c'}) { } 0 2 2
elsif (-l $self->{'c'}) { } 0 2 2
elsif (not -d $self->{'c'}) { } 0 2 2
97 0 return &_se('The (c or chroot) option is required and MUST be a valid directory path');
98 } elsif(-l $self->{c}){
99 0 return &_se('The (c or chroot) option may not be a symbolic link: ' . $self->{c});
100 } elsif(!-d $self->{c}){
101 0 return &_se('Unable to read the (c or chroot) directory path: ' . $self->{c} . " $!");
102 } else {
103 2 ▶ my $current = $1 if(Cwd::cwd() =~ /(.+)/s);
Branch: 1 branch
BranchTrueFalseTotal
if Cwd::cwd() =~ /(.+)/s 2 0 2
104  
105 # Figure out the virtual root
106 2 ▶ chdir($self->{c}) || return &_se("chdir() to: $self->{c} failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"c"} or return &_se(qq[chdir() to: $self->{"c"} failed: $!]) 0 2 2
107  
108 2 $self->{rr} = Cwd::cwd();
109  
110 # Slice the trailing slash
111 2 $self->{rr} =~ s|/$||g;
112  
113 # Return the current working directory
114 2 ▶ chdir($current) || return &_se("chdir() unable to return to working directory: $current $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $current or return &_se("chdir() unable to return to working directory: $current $!") 0 2 2
115 }
116  
117  
118 2 return $self;
119 }
120  
121 =head2 errstr()
122  
123 Return the last error message captured.
124  
125 =cut
126  
127 sub errstr {
128 0 ▶ my ($self, $err) = @_;
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
129 0 ▶ $self->{'.e'} = $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
130 0 return $self->{'.e'};
131 }
132  
133 # internal routine for error handling.
134 sub _se {
135 0 ▶ my ($self, $err) = @_;
Subroutine _se: not called CC=2 SCAR=17.9
136 0 ▶ if(ref($self) ne 'Filesys::Virtual::Chroot'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne 'Filesys::Virtual::Chroot') { } 0 0 0
137 0 $Filesys::Virtual::Chroot::errstr = $self;
138 } else {
139 0 $self->errstr($err);
140 }
141  
142 0 return;
143 }
144  
145 =head2 rroot()
146  
147 Return the real full root path of the virtual chroot'd environment.
148  
149 =cut
150  
151 sub rroot {
152 return $_[0]->{rr}
153 4 ▶}
Subroutine rroot: called CC=1 SCAR=0.0
Pod documented
154  
155  
156 =head2 lchdir()
157  
158 Return the last real directory that was changed to with $cr->vchdir()
159  
160 =cut
161  
162 sub lchdir {
163 3 ▶ my ($self, $str) = @_;
Subroutine lchdir: called CC=2 SCAR=6.9
Pod documented
164  
165 3 ▶ $self->{lpath} = $str if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 1 2 3
166  
167 3 return $self->{lpath};
168 }
169  
170  
171 =head2 vchdir(path)
172  
173 Change the virtual directory and return the virtual directory that was changed to.
174  
175 =cut
176  
177 sub vchdir {
178 1 ▶ my ($self, $path) = @_;
Subroutine vchdir: called CC=12 SCAR=31.1
Pod documented
179  
180 1 my $proot = $self->rroot;
181  
182 # Clean up the entry
183 1 ▶ $path =~ s/^\s+|\s+$//g if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 1 0 1
184  
185 1 $proot =~ s/(\W)/\\$1/g;
186  
187 1 my $lpath;
188 1 ▶ if($path && $path !~ /^\//){
Branch: 1 branch
BranchTrueFalseTotal
if ($path and not $path =~ m[^/]) { } 0 1 1
Condition33%1/3
$path and not $path =~ m[^/]
!ll&&!rl&&r
010
MC/DC0%0/2
$path and not $path =~ m[^/]
$path not $path =~ m[^/]
Truth table33%1/3
A and not(B)
A= $pathB= not $path =~ m[^/]
ABresult
0X0
100
111
189 0 $lpath = '/' . $path;
190 } else {
191 1 $lpath = $path;
192 }
193  
194 1 ▶ if($path){
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 1 0 1
195 1 $path = $self->rroot . '/' . $path;
196 } else {
197 0 $path = $self->rroot;
198 }
199  
200 # Remove any duplicate slashes in the path. i.e. /root//some////path/////
201 1 $path =~ s/\/+/\//g;
202  
203 1 $path =~ /(.+)/s;
204  
205 1 my $current = Cwd::cwd();
206  
207 1 ▶ chdir($1) || return $self->_se("Unable to chdir() to: ($1) $lpath $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $1 or return $self->_se("Unable to chdir() to: ($1) $lpath $!") 0 1 1
208  
209 1 $self->lchdir( Cwd::cwd() );
210  
211 1 ▶ if(($self->lchdir !~ /^$proot.*?/ && !$self->{i}) || ($self->lchdir !~ /^$proot.*?/i && $self->{i})){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->lchdir =~ /^$proot.*?/ || $self->{'i'} or not $self->lchdir =~ /^$proot.*?/i and $self->{'i'}) { } 0 1 1
Condition33%3/9
not $self->lchdir =~ /^$proot.*?/ || $self->{"i"} or not $self->lchdir =~ /^$proot.*?/i and $self->{"i"}
l!l&&r!l&&!r
001
$self->lchdir =~ /^$proot.*?/ || $self->{'i'}
l!l&&r!l&&!r
100
not $self->lchdir =~ /^$proot.*?/i and $self->{"i"}
!ll&&!rl&&r
100
MC/DC0%0/4
not $self->lchdir =~ /^$proot.*?/ || $self->{"i"} or not $self->lchdir =~ /^$proot.*?/i and $self->{"i"}
$self->lchdir =~ /^$proot.*?/ $self->{'i'} not $self->lchdir =~ /^$proot.*?/i $self->{"i"}
Truth table14%1/7
not(A || B) or not(C) and D
A= $self->lchdir =~ /^$proot.*?/B= $self->{'i'}C= not $self->lchdir =~ /^$proot.*?/iD= $self->{"i"}
ABCDresult
00XX1
1X111
01111
1X0X0
1X100
010X0
01100
212  
213 0 ▶ chdir($current) || return &_se("chdir() unable to return to working directory: $current $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $current or return &_se("chdir() unable to return to working directory: $current $!") 0 0 0
214 0 return $self->_se('chdir failed: directory below root!');
215 } else {
216 1 my $spath = Cwd::cwd();
217  
218 1 $spath =~ s/$proot//g;
219  
220 1 ▶ return ($spath ? $spath : '/');
Branch: 1 branch
BranchTrueFalseTotal
$spath ? : 1 0 1
221 }
222 }
223  
224  
225 =head2 rpath(file)
226  
227 Return the real full path of <file> if <file> is within the virtual chroot environment
228  
229 =cut
230  
231 sub rpath {
232 0 ▶ my ($self, $obj) = @_;
Subroutine rpath: not called CC=21 SCAR=61.4
Pod documented
233  
234 0 my $proot = $self->rroot;
235  
236 # Grab the file / directory we're checking
237 0 my @p = split(/\//, $obj);
238  
239 0 $obj = pop @p;
240  
241 0 my $path = join('/', @p);
242  
243 # Clean up the entry
244 0 ▶ if($path){
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
245 0 $path =~ s/^\s+|\s+$//g;
246 0 ▶ $path =~ s/\/+/\//g if $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 0 0
247 }
248  
249 # If the request is below the root and the path is the root
250 # return information on the root.
251 0 ▶ if(!defined $obj || ($obj eq '..' && defined $path && $path eq '/')){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $obj or $obj eq ".." and defined $path and $path eq "/") 0 0 0
Condition0%0/9
not defined $obj or $obj eq ".." and defined $path and $path eq "/"
l!l&&r!l&&!r
000
$obj eq ".." and defined $path and $path eq "/"
!ll&&!rl&&r
000
$obj eq ".." and defined $path
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $obj or $obj eq ".." and defined $path and $path eq "/"
not defined $obj $obj eq ".." defined $path $path eq "/"
Truth table0%0/5
not(A) or B and C and D
A= not defined $objB= $obj eq ".."C= defined $pathD= $path eq "/"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
252 0 return $proot;
253 }
254  
255 0 $proot =~ s/(\W)/\\$1/g;
256  
257 0 my $lpath;
258 0 ▶ if($path && $path !~ /^\//){
Branch: 1 branch
BranchTrueFalseTotal
if ($path and not $path =~ m[^/]) { } 0 0 0
Condition0%0/3
$path and not $path =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$path and not $path =~ m[^/]
$path not $path =~ m[^/]
Truth table0%0/3
A and not(B)
A= $pathB= not $path =~ m[^/]
ABresult
0X0
100
111
259 0 $lpath = '/' . $path;
260 } else {
261 0 $lpath = $path;
262 }
263  
264 0 ▶ if($path){
Branch: 1 branch
BranchTrueFalseTotal
if ($path) { } 0 0 0
265 0 $path = $self->rroot . '/' . $path;
266 } else {
267 0 $path = $self->rroot;
268 }
269  
270 # Remove any duplicate slashes in the path. i.e. /root//some////path/////
271 0 $path =~ s/\/+/\//g;
272  
273 0 ▶ my $current = $1 if(Cwd::cwd() =~ /(.+)/s);
Branch: 1 branch
BranchTrueFalseTotal
if Cwd::cwd() =~ /(.+)/s 0 0 0
274  
275 0 $path =~ /(.+)/s;
276  
277 0 ▶ if(!chdir($1)){
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $1) 0 0 0
278 0 my $err = $!;
279 0 return $self->_se("Unable to chdir() to: [$1] $lpath $err. While verifying $obj");
280 }
281  
282 0 $self->lchdir( Cwd::cwd() );
283  
284 0 ▶ if(($self->lchdir !~ /^$proot.*?/ && !$self->{i}) || ($self->lchdir !~ /^$proot.*?/i && $self->{i})){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->lchdir =~ /^$proot.*?/ || $self->{'i'} or not $self->lchdir =~ /^$proot.*?/i and $self->{'i'}) { } 0 0 0
Condition0%0/9
not $self->lchdir =~ /^$proot.*?/ || $self->{"i"} or not $self->lchdir =~ /^$proot.*?/i and $self->{"i"}
l!l&&r!l&&!r
000
$self->lchdir =~ /^$proot.*?/ || $self->{'i'}
l!l&&r!l&&!r
000
not $self->lchdir =~ /^$proot.*?/i and $self->{"i"}
!ll&&!rl&&r
000
MC/DC0%0/4
not $self->lchdir =~ /^$proot.*?/ || $self->{"i"} or not $self->lchdir =~ /^$proot.*?/i and $self->{"i"}
$self->lchdir =~ /^$proot.*?/ $self->{'i'} not $self->lchdir =~ /^$proot.*?/i $self->{"i"}
Truth table0%0/7
not(A || B) or not(C) and D
A= $self->lchdir =~ /^$proot.*?/B= $self->{'i'}C= not $self->lchdir =~ /^$proot.*?/iD= $self->{"i"}
ABCDresult
00XX1
1X111
01111
1X0X0
1X100
010X0
01100
285 0 ▶ chdir($current) || return &_se("chdir() unable to return to working directory: $current $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $current or return &_se("chdir() unable to return to working directory: $current $!") 0 0 0
286 0 return $self->_se('chdir failed: directory below root!');
287 } else {
288 0 ▶ my $r = ($self->rcwd ?
Branch: 3 branches
BranchTrueFalseTotal
$self->rcwd ? : 0 0 0
$obj ? : 0 0 0
$obj ? : 0 0 0
289 ($obj ? $self->rcwd . '/' . $obj : $self->rcwd) :
290 ($obj ? '/' . $obj : '/')
291 );
292  
293 0 ▶ chdir($current) || return &_se("chdir() unable to return to working directory: $current $!");
Branch: 1 branch
BranchTrueFalseTotal
chdir $current or return &_se("chdir() unable to return to working directory: $current $!") 0 0 0
294 0 return $r;
295 }
296 }
297  
298 =head2 vcwd()
299  
300 Return the virtual current working directory
301  
302 =cut
303  
304 sub vcwd {
305 1 ▶ my ($self) = @_;
Subroutine vcwd: called CC=3 SCAR=13.0
Pod documented
306  
307 1 my $cwd = Cwd::cwd();
308  
309 1 my $proot = $self->rroot;
310  
311 1 $proot =~ s/(\W)/\\$1/g;
312  
313 1 ▶ if(!($cwd =~ s/$proot//g)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($cwd =~ s/$proot//g) 1 0 1
314 1 return '/';
315 }
316  
317 0 $cwd =~ s|/+|/|g;
318  
319 0 ▶ return ($cwd ? $cwd : '/');
Branch: 1 branch
BranchTrueFalseTotal
$cwd ? : 0 0 0
320 }
321  
322  
323 =head2 vpwd(path)
324  
325 aliase for the vcwd() command.
326  
327 =cut
328  
329 sub vpwd {
330 1 ▶ Filesys::Virtual::Chroot::vcwd(@_);
Subroutine vpwd: called CC=1 SCAR=0.0
Pod documented
331 }
332  
333  
334 =head2 rcwd()
335  
336 Return the real current working directory
337  
338 =cut
339  
340 1 ▶sub rcwd { Cwd::cwd() }
Subroutine rcwd: called CC=1 SCAR=0.0
Pod documented
341  
342  
343 =head2 rpwd()
344  
345 aliase for the rcwd() command.
346  
347 =cut
348  
349 sub rpwd {
350 1 ▶ Filesys::Virtual::Chroot::rcwd(@_);
Subroutine rpwd: called CC=1 SCAR=0.0
Pod documented
351 }
352  
353 3 1;
↑ Summary example.pl »