lib/File/Util/SecureDir.pm

filter: Statementstmt 61.6% 135 / 219 Branchbran 54.6% 71 / 130 Conditioncond 62.5% 15 / 24 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 80.0% 24 / 30 Podpod 100% 5 / 5 totaltotal 61.2% 259 / 423 CC 79 SCAR 39.0
CC
79
Cov
59%
__ANON__
182.0
__ANON__
182.0
__ANON__
20.0
CRAP 49.3
↑ Summary
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package File::Util::SecureDir v0.1.0;
5 15 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 our @CARP_NOT = ( __PACKAGE__ );
12  
13 ########################################################################
14 # state: assigned once from the #! code at use-time, never reset.
15 # $script is captured when this module is first compiled.
16 ########################################################################
17  
18 15 my $script = $0;
19 15 my $default = 'other_no_write';
20 15 my $filter = '';
21 15 my $handler = '';
22 15 my $verbose = 0;
23 15 my $debug = 0;
24  
25 ########################################################################
26 # Win32 self-relative SD parser. Returns undef on a malformed buffer.
27 #
28 # This is 'our' only so tests can feed it hand-packed buffers on any
29 # platform: $File::Util::SecureDir::parse_sd->( $sd ). It is not part
30 # of the interface. The Win32 handler calls $sd_parser, a lexical copy
31 # taken here at load time, so reassigning the package variable later
32 # does not change what the filter does.
33 ########################################################################
34  
35 our $parse_sd
36 4 ▶= sub( $sd )
Subroutine __ANON__: called CC=15 SCAR=27.7
37 {
38 4 my $len = length $sd;
39 4 ▶ $len >= 20 or return;
Branch: 1 branch
BranchTrueFalseTotal
$len >= 20 or return 3 1 4
40  
41 3 my ( $rev, $sbz1, $control, $off_owner, undef, undef, $off_dacl )
42 = unpack 'C C v V V V V', $sd;
43  
44 my $owner_sid
45 = $off_owner
46 ? do
47 3 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$off_owner ? : 2 1 3
48 2 ▶ $off_owner + 2 <= $len or return;
Branch: 1 branch
BranchTrueFalseTotal
$off_owner + 2 <= $len or return 2 0 2
49 2 my $sub_count = unpack 'C', substr $sd, $off_owner + 1, 1;
50 2 my $sid_len = 8 + 4 * $sub_count;
51 2 ▶ $off_owner + $sid_len <= $len or return;
Branch: 1 branch
BranchTrueFalseTotal
$off_owner + $sid_len <= $len or return 2 0 2
52 2 substr $sd, $off_owner, $sid_len
53 }
54 : ''
55 ;
56  
57 3 ▶ my $null_dacl
Condition66%2/3
$control & 4 && $off_dacl
!ll&&!rl&&r
102
MC/DC50%1/2
$control & 4 && $off_dacl
$control & 4 $off_dacl
Truth table66%2/3
A && B
A= $control & 4B= $off_dacl
ABresult
0X0
100
111
58 = ! ( $control & 0x0004 )
59 || ! $off_dacl
60 ;
61  
62 my @acez
63 = $null_dacl
64 ? ()
65 : do
66 3 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$null_dacl ? : 1 2 3
67 2 ▶ $off_dacl + 8 <= $len or return;
Branch: 1 branch
BranchTrueFalseTotal
$off_dacl + 8 <= $len or return 2 0 2
68  
69 2 my ( undef, undef, undef, $ace_count )
70 = unpack 'C C v v', substr $sd, $off_dacl, 8;
71  
72 2 my $off = $off_dacl + 8;
73 2 my @result = ();
74  
75 2 for ( 1 .. $ace_count )
76 {
77 2 ▶ $off + 4 <= $len or return;
Branch: 1 branch
BranchTrueFalseTotal
$off + 4 <= $len or return 1 1 2
78  
79 1 my ( $ace_type, $ace_flags, $ace_size )
80 = unpack 'C C v', substr $sd, $off, 4;
81  
82 1 ▶ $ace_size >= 4 or return;
Branch: 1 branch
BranchTrueFalseTotal
$ace_size >= 4 or return 1 0 1
83  
84 1 ▶ unless( $ace_flags & 0x08 )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ace_flags & 8) 1 0 1
85 {
86 1 ▶ if( $ace_type == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($ace_type == 0) 1 0 1
87 {
88 1 ▶ $off + 12 <= $len or return;
Branch: 1 branch
BranchTrueFalseTotal
$off + 12 <= $len or return 1 0 1
89  
90 1 my $mask = unpack 'V', substr $sd, $off + 4, 4;
91 1 my $sub_count = unpack 'C', substr $sd, $off + 9, 1;
92 1 my $sid_len = 8 + 4 * $sub_count;
93  
94 1 ▶ $off + 8 + $sid_len <= $len or return;
Branch: 1 branch
BranchTrueFalseTotal
$off + 8 + $sid_len <= $len or return 1 0 1
95  
96 1 push @result,
97 [
98 $ace_type
99 , $mask
100 , substr $sd, $off + 8, $sid_len
101 ];
102 }
103 }
104  
105 1 $off += $ace_size;
106 }
107  
108 @result
109 1 }
110 ;
111  
112 {
113 2 owner => $owner_sid
114 , acez => \@acez
115 , null_dacl => $null_dacl
116 }
117 15 };
118  
119 15 my $sd_parser = $parse_sd;
120  
121 ########################################################################
122 # Win32 identity of the running process: the user SID in the process
123 # token.
124 #
125 # Why the token: the access token is what the kernel evaluates every
126 # DACL against, so comparing a directory's owner with the token's SID
127 # asks the question the OS itself will answer when the directory is
128 # used. It is the Win32 counterpart of the effective uid ($>) on Unix:
129 # the identity the process is actually running as, which differs from
130 # the login identity under RunAs, services, and elevation. Nothing
131 # here is derived from names or from the environment, so an attacker
132 # who controls %ENV (USERDOMAIN, USERNAME) or who creates a look-alike
133 # account cannot change which SID counts as "me". Handles are
134 # pointer-wide, which is why the prototypes switch to 'Q' on 64-bit
135 # perls.
136 #
137 # If the token cannot be read on some target (a perl without 64-bit
138 # integers cannot pass 'Q', or OpenProcessToken is refused in a sandbox)
139 # $choose_sid below falls back, with a warning, to resolving the login
140 # name. That does not use %ENV either, but a bare name is ambiguous when
141 # a local and a domain account share it, and it reports the login
142 # identity rather than the token's. So the token is always tried first
143 # and the fallback is used only when the token fails or yields a
144 # malformed SID, never to second-guess a token that works. It is never
145 # silent: the identity source is part of the security decision.
146 #
147 # 'our' only so tests can call these step by step, e.g.
148 # $File::Util::SecureDir::process_sid->(); they are not part of the
149 # interface. Handlers use the lexical copies taken at load time.
150 ########################################################################
151  
152 our $process_sid
153 = sub
154 0 ▶{
Subroutine __ANON__: not called CC=13 SCAR=52.0
155 0 require Win32::API;
156  
157 0 my $wide = $Config{ ptrsize } == 8;
158 0 ▶ my $handle = $wide ? 'Q' : 'N';
Branch: 1 branch
BranchTrueFalseTotal
$wide ? : 0 0 0
159 0 ▶ my $handle_fmt = $wide ? 'Q' : 'V';
Branch: 1 branch
BranchTrueFalseTotal
$wide ? : 0 0 0
160 0 my $TOKEN_QUERY = 0x0008;
161 0 my $TOKEN_USER = 1;
162  
163 0 ▶ my $GetCurrentProcess
Branch: 1 branch
BranchTrueFalseTotal
my $GetCurrentProcess = "Win32::API"->new("kernel32", "GetCurrentProcess", "", $handle) or die "Cannot import GetCurrentProcess: $^E" 0 0 0
164 = Win32::API->new( 'kernel32', 'GetCurrentProcess', '', $handle )
165 or die "Cannot import GetCurrentProcess: $^E";
166  
167 0 ▶ my $OpenProcessToken
Branch: 1 branch
BranchTrueFalseTotal
my $OpenProcessToken = "Win32::API"->new("advapi32", "OpenProcessToken", "${handle}NP", "N") or die "Cannot import OpenProcessToken: $^E" 0 0 0
168 = Win32::API->new( 'advapi32', 'OpenProcessToken', "${handle}NP", 'N' )
169 or die "Cannot import OpenProcessToken: $^E";
170  
171 0 ▶ my $GetTokenInformation
Branch: 1 branch
BranchTrueFalseTotal
my $GetTokenInformation = "Win32::API"->new("advapi32", "GetTokenInformation", "${handle}NPNP", "N") or die "Cannot import GetTokenInformation: $^E" 0 0 0
172 = Win32::API->new( 'advapi32', 'GetTokenInformation', "${handle}NPNP", 'N' )
173 or die "Cannot import GetTokenInformation: $^E";
174  
175 0 ▶ my $CloseHandle
Branch: 1 branch
BranchTrueFalseTotal
my $CloseHandle = "Win32::API"->new("kernel32", "CloseHandle", $handle, "N") or die "Cannot import CloseHandle: $^E" 0 0 0
176 = Win32::API->new( 'kernel32', 'CloseHandle', $handle, 'N' )
177 or die "Cannot import CloseHandle: $^E";
178  
179 0 my $token_buf = pack $handle_fmt, 0;
180  
181 0 ▶ $OpenProcessToken->Call( $GetCurrentProcess->Call(), $TOKEN_QUERY, $token_buf )
Branch: 1 branch
BranchTrueFalseTotal
$OpenProcessToken->Call($GetCurrentProcess->Call, $TOKEN_QUERY, $token_buf) or die "OpenProcessToken: $^E" 0 0 0
182 or die "OpenProcessToken: $^E";
183  
184 0 my $token = unpack $handle_fmt, $token_buf;
185 0 my $needed = pack 'V', 0;
186 0 my $buf = "\0";
187  
188 # the first call only reports the size needed.
189  
190 0 $GetTokenInformation->Call( $token, $TOKEN_USER, $buf, 0, $needed );
191  
192 0 my $size = unpack 'V', $needed;
193  
194 $size
195 or do
196 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$size or do { $CloseHandle->Call($token); die "GetTokenInformation size: $^E" } 0 0 0
197 0 $CloseHandle->Call( $token );
198 0 die "GetTokenInformation size: $^E";
199 };
200  
201 0 $buf = "\0" x $size;
202  
203 0 my $ok = $GetTokenInformation->Call( $token, $TOKEN_USER, $buf, $size, $needed );
204  
205 $ok
206 or do
207 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$ok or do { $CloseHandle->Call($token); die "GetTokenInformation: $^E" } 0 0 0
208 0 $CloseHandle->Call( $token );
209 0 die "GetTokenInformation: $^E";
210 };
211  
212 0 $CloseHandle->Call( $token );
213  
214 # TOKEN_USER starts with a pointer to the SID, which lives inside
215 # the same buffer; turn the pointer into an offset and bounds check
216 # it before copying the SID out.
217  
218 0 my $sid_ptr = unpack $handle_fmt, $buf;
219 0 my $buf_ptr = unpack $handle_fmt, pack 'P', $buf;
220 0 my $offset = $sid_ptr - $buf_ptr;
221  
222 0 ▶ $offset > 0 && $offset + 8 <= $size
Branch: 1 branch
BranchTrueFalseTotal
$offset > 0 and $offset + 8 <= $size or die "Token SID lies outside the token buffer" 0 0 0
Condition0%0/3
$offset > 0 and $offset + 8 <= $size
!ll&&!rl&&r
000
MC/DC0%0/2
$offset > 0 and $offset + 8 <= $size
$offset > 0 $offset + 8 <= $size
Truth table0%0/3
A and B
A= $offset > 0B= $offset + 8 <= $size
ABresult
0X0
100
111
223 or die "Token SID lies outside the token buffer";
224  
225 0 my $sub_count = unpack 'C', substr $buf, $offset + 1, 1;
226 0 my $sid_len = 8 + 4 * $sub_count;
227  
228 0 ▶ $offset + $sid_len <= $size
Branch: 1 branch
BranchTrueFalseTotal
$offset + $sid_len <= $size or die "Token SID is truncated" 0 0 0
229 or die "Token SID is truncated";
230  
231 0 substr $buf, $offset, $sid_len
232 15 };
233  
234 # Fallback: the SID of the login name, from the OS and not the environment.
235  
236 our $login_sid
237 = sub
238 0 ▶{
Subroutine __ANON__: not called CC=2 SCAR=17.9
239 0 require Win32;
240  
241 0 my ( $sid, $domain, $type ) = ( '', '', '' );
242  
243 0 ▶ Win32::LookupAccountName( '', Win32::LoginName(), $sid, $domain, $type )
Branch: 1 branch
BranchTrueFalseTotal
Win32::LookupAccountName("", Win32::LoginName(), $sid, $domain, $type) or die "LookupAccountName: $^E" 0 0 0
244 or die "LookupAccountName: $^E";
245  
246 0 $sid
247 15 };
248  
249 # A SID is: revision 1, a sub-authority count, then 8 + 4 * count bytes.
250  
251 my $sid_ok
252 18 ▶= sub( $sid )
Subroutine __ANON__: called CC=4 SCAR=13.9
253 {
254 18 ▶ defined $sid
Branch: 1 branch
BranchTrueFalseTotal
defined $sid and length $sid >= 8 or return 0 11 7 18
Condition100%3/3
defined $sid and length $sid >= 8
!ll&&!rl&&r
4311
MC/DC100%2/2
defined $sid and length $sid >= 8
defined $sid length $sid >= 8
Truth table100%3/3
A and B
A= defined $sidB= length $sid >= 8
ABresult
0X0
100
111
255 && length( $sid ) >= 8
256 or return 0;
257  
258 11 my ( $rev, $count ) = unpack 'C C', $sid;
259  
260 11 ▶ $rev == 1 && length( $sid ) == 8 + 4 * $count
Branch: 1 branch
BranchTrueFalseTotal
$rev == 1 and length $sid == 8 + 4 * $count 10 1 11
261 15 };
262  
263 # First getter that returns a well formed SID wins. A getter that dies
264 # or returns junk is skipped, and if a later one is used the failures
265 # are reported. If none works the filter cannot know who "me" is, so
266 # it dies rather than guess.
267  
268 our $choose_sid
269 11 ▶= sub( @getterz )
Subroutine __ANON__: called CC=5 SCAR=16.1
270 {
271 11 my @failz = ();
272  
273 11 for my $getter ( @getterz )
274 {
275 18 my $sid = eval { $getter->() };
276  
277 $sid_ok->( $sid )
278 or do
279 18 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
&$sid_ok($sid) or do { push @failz, $@ || "returned a malformed SID"; next } 9 9 18
280 9 ▶ push @failz, $@ || 'returned a malformed SID';
Condition100%2/2
$@ || 'returned a malformed SID'
l!l
36
MC/DC100%1/1
$@ || 'returned a malformed SID'
$@
Truth table100%2/2
A
A= $@
Aresult
11
00
281 next
282 9 };
283  
284 @failz
285 and carp 'Process identity: preferred source failed ('
286 9 ▶ . join( '; ' => map { s/\s+\z//r } @failz )
Branch: 1 branch
BranchTrueFalseTotal
@failz and carp('Process identity: preferred source failed (' . join("; ", map({s/\s+\z//ru;} @failz)) . '), using the fallback') 7 2 9
287 . '), using the fallback';
288  
289 9 return $sid
290 }
291  
292 die 'No usable process identity: '
293 2 . join( '; ' => map { s/\s+\z//r } @failz )
294 . "\n"
295 15 };
296  
297 15 my $sid_chooser = $choose_sid;
298 15 my $token_getter = $process_sid;
299 15 my $login_getter = $login_sid;
300  
301 ########################################################################
302 # handler builders. Only the selected filter is ever built, so a Win32
303 # setup failure dies in the #! code's 'use', before anything is filtered.
304 #
305 # $level: 1 = other_no_write, 2 = group_no_write, 3 = same_owner.
306 # Each handler reads its metadata once per directory, so the checks
307 # for all levels come from the same snapshot.
308 ########################################################################
309  
310 my $make_unix_handler
311 7 ▶= sub( $level )
Subroutine __ANON__: called CC=2 SCAR=6.9
312 {
313 7 ▶ my $mask = $level > 1 ? 0022 : 0002;
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 ? : 4 3 7
314  
315 41 ▶ sub( $path )
Subroutine __ANON__: called CC=5 SCAR=16.1
316 {
317 my @st = stat $path
318 or do
319 41 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = stat $path or do { carp("Failed reading directory '${path}': $!"); return 0 } 40 1 41
320 1 carp "Failed reading directory '$path': $!";
321 1 return 0
322 };
323  
324 40 ▶ $st[2] & $mask and return 0;
Branch: 1 branch
BranchTrueFalseTotal
$st[2] & $mask and return 0 20 20 40
325 20 ▶ $level < 3 and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$level < 3 and return 1 16 4 20
326  
327 # The effective uid ($>) defines who the process is running as:
328 # under sudo or a setuid program the real uid ($<) is the invoker,
329 # whose directories are exactly the ones not to trust. Read it
330 # on every call since it can change (seteuid) after load. Root
331 # is accepted along with the process owner; VMS equivalence of
332 # uid 0 is unverified.
333  
334 4 my $owner = $st[4];
335  
336 4 ▶ $owner == $> || $owner == 0
Branch: 1 branch
BranchTrueFalseTotal
$owner == $> or $owner == 0 3 1 4
337 }
338 7 };
339  
340 my $make_win32_handler
341 0 ▶= sub( $level )
Subroutine __ANON__: not called CC=2 SCAR=17.7
342 {
343 0 require Win32;
344 0 require Win32::API;
345  
346 my $build_sid
347 0 ▶ = sub( $str )
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 {
349 0 my ( undef, $rev, $auth, @sub ) = split /-/, $str;
350 0 pack 'C C x4 n V*', $rev, scalar @sub, $auth, @sub
351 0 };
352  
353 my @other_sids
354 = map
355 {
356 0 $_->$build_sid
357 }
358 qw( S-1-1-0 S-1-5-11 S-1-5-32-545 );
359  
360 my @root_sids
361 = map
362 {
363 0 $_->$build_sid
364 }
365 qw( S-1-5-18 S-1-5-32-544 );
366  
367 0 my $curr_sid = $sid_chooser->( $token_getter, $login_getter );
368  
369 0 my $write_mask
370 = 0x0002
371 | 0x0004
372 | 0x0010
373 | 0x0040
374 | 0x0100
375 | 0x00010000
376 | 0x00040000
377 | 0x00080000
378 | 0x10000000
379 | 0x40000000
380 ;
381  
382 0 ▶ my $GetFileSecurity
Branch: 1 branch
BranchTrueFalseTotal
my $GetFileSecurity = "Win32::API"->new("advapi32", "GetFileSecurityA", "PNPNP", "N") or die "Cannot import GetFileSecurityA: $^E" 0 0 0
383 = Win32::API->new( 'advapi32', 'GetFileSecurityA', 'PNPNP', 'N' )
384 or die "Cannot import GetFileSecurityA: $^E";
385  
386 my $fetch_sd
387 0 ▶ = sub( $path )
Subroutine __ANON__: not called CC=4 SCAR=30.0
388 {
389 0 my $needed = pack 'V', 0;
390 0 my $size = 512;
391 0 my $buf = "\0" x $size;
392  
393 0 ▶ if( $GetFileSecurity->Call( $path, 0x5, $buf, $size, $needed ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($GetFileSecurity->Call($path, 5, $buf, $size, $needed)) 0 0 0
394 {
395 0 return substr $buf, 0, unpack 'V', $needed;
396 }
397  
398 Win32::GetLastError() == 122
399 or do
400 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
Win32::GetLastError() == 122 or do { carp("GetFileSecurityA '${path}': $^E"); return } 0 0 0
401 0 carp "GetFileSecurityA '$path': $^E";
402 return
403 0 };
404  
405 0 $size = unpack 'V', $needed;
406 0 $buf = "\0" x $size;
407  
408 $GetFileSecurity->Call( $path, 0x5, $buf, $size, $needed )
409 or do
410 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$GetFileSecurity->Call($path, 5, $buf, $size, $needed) or do { carp("GetFileSecurityA '${path}': $^E"); return } 0 0 0
411 0 carp "GetFileSecurityA '$path': $^E";
412 return
413 0 };
414  
415 0 substr $buf, 0, unpack 'V', $needed
416 0 };
417  
418 0 ▶ sub( $path )
Subroutine __ANON__: not called CC=13 SCAR=52.0
419 {
420 0 ▶ my $sd = $path->$fetch_sd or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $sd = $path->$fetch_sd or return 0 0 0 0
421 my $info = $sd->$sd_parser
422 or do
423 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
my $info = $sd->$sd_parser or do { carp("Malformed security descriptor for directory '${path}'"); return 0 } 0 0 0
424 0 carp "Malformed security descriptor for directory '$path'";
425 0 return 0
426 };
427  
428 0 ▶ $info->{ null_dacl } and return 0;
Branch: 1 branch
BranchTrueFalseTotal
$info->{"null_dacl"} and return 0 0 0 0
429  
430 0 ▶ my $owner = $info->{ owner } // '';
Condition0%0/2
$info->{"owner"} // ""
l!l
00
MC/DC0%0/1
$info->{"owner"} // ""
$info->{"owner"}
Truth table0%0/2
A
A= $info->{"owner"}
Aresult
11
00
431  
432 0 for my $ace ( $info->{ acez }->@* )
433 {
434 0 my ( undef, $mask, $sid ) = @$ace;
435  
436 0 ▶ $mask & $write_mask or next;
Branch: 1 branch
BranchTrueFalseTotal
$mask & $write_mask or next 0 0 0
437  
438 0 ▶ grep { $_ eq $sid } @other_sids and return 0;
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $sid;} @other_sids and return 0 0 0 0
439  
440 0 ▶ $level > 1 or next;
Branch: 1 branch
BranchTrueFalseTotal
$level > 1 or next 0 0 0
441  
442 $sid ne $owner
443 0 ▶ && ! grep { $_ eq $sid } @root_sids
Branch: 1 branch
BranchTrueFalseTotal
$sid ne $owner and not grep({$_ eq $sid;} @root_sids) and return 0 0 0 0
Condition0%0/3
$sid ne $owner and not grep({$_ eq $sid;} @root_sids)
!ll&&!rl&&r
000
MC/DC0%0/2
$sid ne $owner and not grep({$_ eq $sid;} @root_sids)
$sid ne $owner not grep({$_ eq $sid;} @root_sids)
Truth table0%0/3
A and not(B)
A= $sid ne $ownerB= not grep({$_ eq $sid;} @root_sids)
ABresult
0X0
100
111
444 and return 0;
445 }
446  
447 0 ▶ $level > 2 or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$level > 2 or return 1 0 0 0
448 0 ▶ $owner ne '' or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$owner ne "" or return 0 0 0 0
449  
450 0 !! grep { $_ eq $owner } ( $curr_sid, @root_sids )
451 }
452 0 };
453  
454 15 ▶my $make_handler
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 15 15
455 = $^O eq 'MSWin32'
456 ? $make_win32_handler
457 : $make_unix_handler
458 ;
459  
460 ########################################################################
461 # named filters, least to most restrictive. Names resolve only here.
462 ########################################################################
463  
464 my @filterz
465 = (
466 1 ▶ [ all => sub { sub { 1 } } ]
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
467 3 ▶ , [ other_no_write => sub { $make_handler->( 1 ) } ]
Subroutine __ANON__: called CC=1 SCAR=0.0
468 1 ▶ , [ group_no_write => sub { $make_handler->( 2 ) } ]
Subroutine __ANON__: called CC=1 SCAR=0.0
469 3 ▶ , [ same_owner => sub { $make_handler->( 3 ) } ]
Subroutine __ANON__: called CC=1 SCAR=0.0
470 );
471  
472 ########################################################################
473 # private utilities
474 ########################################################################
475  
476 # 'filter', 'filter=NAME', 'verbose[=0|1]', 'debug[=0|1]'.
477 # 'filter' alone makes the default explicit.
478  
479 my $extract_argz
480 57 ▶= sub( @argz )
Subroutine __ANON__: called CC=5 SCAR=16.1
481 {
482 57 my %argz = ();
483  
484 57 for my $arg ( @argz )
485 {
486 52 ▶ $arg =~ m
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ / \A (?<key> filter | verbose | debug ) (?: = (?<val> .* ) )? \z /sux or croak("Botched import: unknown '${arg}'" . ' (use filter[=NAME], verbose[=0|1], debug[=0|1])') 46 6 52
487 {
488 \A
489 (?<key> filter | verbose | debug )
490 (?: = (?<val> .* ) )?
491 \z
492 }xs
493 or croak
494 "Botched import: unknown '$arg'"
495 . ' (use filter[=NAME], verbose[=0|1], debug[=0|1])';
496  
497 46 ▶ $argz{ $+{ key } } = $+{ val } // ( $+{ key } eq 'filter' ? '' : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$+{'key'} eq 'filter' ? : 4 10 14
Condition100%2/2
$+{"val"} // ($+{"key"} eq "filter" ? "" : 1)
l!l
3214
MC/DC100%1/1
$+{"val"} // ($+{"key"} eq "filter" ? "" : 1)
$+{"val"}
Truth table100%2/2
A
A= $+{"val"}
Aresult
11
00
498 }
499  
500 51 \%argz
501 15 };
502  
503 22 my $set_filter
504 22 ▶= sub( $name = '' )
Subroutine __ANON__: called CC=6 SCAR=17.9
505 {
506 22 state $valid = join ' ' => map { $_->[0] } @filterz;
507  
508 22 ▶ $filter
Branch: 1 branch
BranchTrueFalseTotal
$filter and croak("Bogus filter: already '${filter}'" . ' (set earlier, or latched as the default by a module that filtered)' . "; \"use File::Util::SecureDir qw( filter=NAME )\" must come first") 10 12 22
509 and croak
510 "Bogus filter: already '$filter'"
511 . ' (set earlier, or latched as the default by a module that filtered)'
512 . '; "use File::Util::SecureDir qw( filter=NAME )" must come first';
513  
514 12 ▶ length $name
Branch: 1 branch
BranchTrueFalseTotal
length $name or $name = $default 9 3 12
515 or $name = $default;
516  
517 my $entry
518 38 ▶ = first { $name eq $_->[0] } @filterz
Subroutine __ANON__: called CC=1 SCAR=0.0
519 12 ▶ or croak "Botched filter: unknown '$name' (valid: $valid)";
Branch: 1 branch
BranchTrueFalseTotal
my $entry = &first(sub { $name eq $_->[0]; } , @filterz) or croak("Botched filter: unknown '${name}' (valid: $valid)") 8 4 12
520  
521 # build first: a failed setup leaves nothing assigned.
522  
523 8 my $code = $entry->[1]->();
524  
525 8 ( $filter, $handler ) = ( $name, $code );
526  
527 8 ▶ say STDERR "File::Util::SecureDir: $filter"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 1 7 8
528 if $verbose;
529  
530 return
531 8 };
532  
533 ########################################################################
534 # interface
535 ########################################################################
536  
537 sub new
538 11 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
539 11 __PACKAGE__
540 }
541  
542 # read-only: the name of the filter, '' if none is set yet.
543  
544 17 ▶sub filter( $pkg, @extra )
Subroutine filter: called CC=2 SCAR=6.9
Pod documented
545 {
546 @extra
547 17 ▶ and croak 'Botched filter: read-only, set via "use ... filter=NAME"';
Branch: 1 branch
BranchTrueFalseTotal
@extra and croak("Botched filter: read-only, set via \"use ... filter=NAME\"") 1 16 17
548  
549 16 $filter
550 }
551  
552 33 ▶sub verbose( $pkg, @argz )
Subroutine verbose: called CC=2 SCAR=6.9
Pod documented
553 {
554 @argz
555 33 ▶ ? $verbose = !! $argz[0]
Branch: 1 branch
BranchTrueFalseTotal
@argz ? : 21 12 33
556 : $verbose
557 }
558  
559 31 ▶sub debug( $pkg, @argz )
Subroutine debug: called CC=2 SCAR=6.9
Pod documented
560 {
561 @argz
562 31 ▶ ? $debug = !! $argz[0]
Branch: 1 branch
BranchTrueFalseTotal
@argz ? : 21 10 31
563 : $debug
564 }
565  
566 41 ▶sub filter_dirs( $pkg, @dirz )
Subroutine filter_dirs: called CC=8 SCAR=20.8
Pod documented
567 {
568 # first use latches the default when the #! code did not choose.
569  
570 41 ▶ $handler
Branch: 1 branch
BranchTrueFalseTotal
$handler or &$set_filter() 39 2 41
571 or $set_filter->();
572  
573 41 ▶ if( $debug )
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 1 40 41
574 {
575 15 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
576  
577 1 $DB::single = 1;
578 }
579  
580 $verbose || $debug
581 41 ▶ or return grep { $_->$handler } @dirz;
Branch: 1 branch
BranchTrueFalseTotal
$verbose or $debug or return grep({$_->$handler;} @dirz) 2 39 41
Condition100%3/3
$verbose or $debug
l!l&&r!l&&!r
1139
MC/DC100%2/2
$verbose or $debug
$verbose $debug
Truth table100%3/3
A or B
A= $verboseB= $debug
ABresult
1X1
011
000
582  
583 grep
584 {
585 2 my $ok = $_->$handler;
586  
587 4 ▶ my $verdict = $ok ? 'accept' : 'reject';
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 2 2 4
588  
589 4 ▶ say STDERR "File::Util::SecureDir: $filter: $verdict '$_'"
Branch: 1 branch
BranchTrueFalseTotal
if $debug or not $ok 3 1 4
Condition100%3/3
$debug or not $ok
l!l&&r!l&&!r
211
MC/DC100%2/2
$debug or not $ok
$debug not $ok
Truth table100%3/3
A or not(B)
A= $debugB= not $ok
ABresult
1X1
011
000
590 if $debug || ! $ok;
591  
592 4 $ok
593 }
594 @dirz
595 }
596  
597 57 ▶sub import( $pkg, @argz )
Subroutine import: called CC=5 SCAR=16.1
598 {
599 57 my $argz = $extract_argz->( @argz );
600  
601 51 ▶ exists $argz->{ verbose } and $verbose = !! $argz->{ verbose };
Branch: 1 branch
BranchTrueFalseTotal
exists $argz->{"verbose"} and $verbose = !!$argz->{"verbose"} 11 40 51
602 51 ▶ exists $argz->{ debug } and $debug = !! $argz->{ debug };
Branch: 1 branch
BranchTrueFalseTotal
exists $argz->{"debug"} and $debug = !!$argz->{"debug"} 8 43 51
603  
604 51 ▶ if( exists $argz->{ filter } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $argz->{"filter"}) 27 24 51
605 {
606 27 my $file = ( caller )[1];
607  
608 27 ▶ $file eq $script
Branch: 1 branch
BranchTrueFalseTotal
$file eq $script or croak("Botched filter: can only be set from '${script}', not '${file}'") 20 7 27
609 or croak "Botched filter: can only be set from '$script', not '$file'";
610  
611 20 $argz->{ filter }->$set_filter;
612 }
613  
614 return
615 30 }
616  
617 15 1
618 __END__
↑ Summary