blib/lib/File/Find/Rule/Permissions.pm

filter: Statementstmt 92.5% 62 / 67 Branchbran 91.0% 51 / 56 Conditioncond 86.6% 26 / 30 MC/DCmcdc 68.4% 13 / 19 Subroutinesub 81.8% 9 / 11 Podpod 25.0% 1 / 4 totaltotal 86.6% 162 / 187 SCAR 37.2
CC
40
Cov
91%
__ANON__
35.7
getusergroupdetails
5.0
permissions
2.0
CRAP 41.2
↑ Summary
1 package File::Find::Rule::Permissions;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Devel::AssertOS::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base qw(File::Find::Rule);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 use vars qw(
9 $VERSION @EXPORT
10 %UIDsByUsername %UsernamesByUID %GIDsByGroupname
11 %GroupnamesByGID %UIDinGID
12 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 @EXPORT = @File::Find::Rule::EXPORT;
14 2 $VERSION = '2.02';
15  
16 2 ▶use Fcntl qw(:mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 File::Find::Rule::Permissions - rule to match on file permissions and user
21 access
22  
23 =head1 SYNOPSIS
24  
25 use File::Find::Rule::Permissions;
26  
27 # Which files can the 'nobody' user read in the current directory?
28 @readable = File::Find::Rule::Permissions->file()
29 ->permissions(isReadable => 1, user => 'nobody')
30 ->in('.');
31
32 # Which files can UID 42 *not* read in the current directory?
33 @notreadable = File::Find::Rule::Permissions->file()
34 ->permissions(isReadable => 0, user => 42)
35 ->in('.');
36
37 # Find big insecurity badness!
38 @eek = File::Find::Rule::Permissions->permissions(
39 isWriteable => 1,
40 isExecutable => 1,
41 user => 'nobody'
42 )->in('/web');
43  
44 =head1 DESCRIPTION
45  
46 An extension for File::Find::Rule to work with file permission bits and
47 determine whether a given user can read, write or execute files.
48  
49 =head1 METHODS
50  
51 =head2 B<permissions>
52  
53 Takes at least one parameter and up to four. The mandatory parameter
54 must be one of isReadable, isWriteable or isExecutable, which take
55 values of 1 or 0 (actually true or false). Any of those three that
56 are missing are ignored - ie, we match regardless of their truth or
57 falsehood. A value of 1 means that we must only match files where
58 the user can read/write/execute (as appropriate) the file, and a
59 value of 0 means we must only match if the user can NOT
60 read/write/execute the file. To supply none of these three is clearly
61 an error, as it is equivalent to not caring what the permissions are,
62 which is equivalent to seeing if the file exists, which
63 File::Find::Rule already does quite nicely thankyouverymuch.
64  
65 The 'user' parameter is optional. By default, we check access for the
66 current effective userid, which is normally the user running the
67 program. This can be changed using this parameter, which takes a
68 numeric uid or a username. Note, however, that if the user running
69 the program can't get at parts of the filesystem that the desired user
70 can, the results will be incomplete.
71  
72 The astute reader will have noticed that File::Find::Rule already
73 handles some of these rules (checking permissions for the effective
74 uid), but not for an arbitrary user. That this module can also check
75 for the effective uid is more of a lucky accident that just falls out
76 of the code when checking for any arbitrary user :-)
77  
78 =head1 BUGS
79  
80 I assume a Unix-a-like system, both when looking at file permissions,
81 and when divining users' membership of groups. Patches for other
82 systems are welcome.
83  
84 We divine which groups a user belongs to when the module is loaded. If
85 group membership changes underneath the program, incorrect results may
86 be returned. I consider this to be Just Fine, given that most shells
87 also have the same limitation.
88  
89 =cut
90  
91 # figure out who has what UID and which UIDs are in which group
92 2 (%UIDsByUsername, %UsernamesByUID, %GIDsByGroupname,
93 %GroupnamesByGID, %UIDinGID) = ();
94 2 getusergroupdetails();
95  
96 # we override these in the test suite to avoid having to be root.
97 # or we will do when that bit is written, anyway.
98  
99 0 ▶sub stat { return CORE::stat(shift); }
Subroutine stat: not called CC=1 SCAR=6.9
Pod documented
100 0 ▶sub geteuid { return $>; }
Subroutine geteuid: not called CC=1 SCAR=6.9
Pod undocumented
101 sub getusergroupdetails {
102 2 ▶ while(my($name, undef, $uid, $gid) = getpwent()) {
Subroutine getusergroupdetails: called CC=5 SCAR=16.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (my($name, undef, $uid, $gid) = getpwent) 40 2 42
103 40 $UIDsByUsername{$name} = $uid;
104 40 $UsernamesByUID{$uid} = $name;
105 40 $UIDinGID{$gid}{$uid} = 1;
106 }
107 2 ▶ while(my($grname, $grpass, $gid, $members) = getgrent()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = getgrent) 80 2 82
108 80 $GIDsByGroupname{$grname} = $gid;
109 80 $GroupnamesByGID{$gid} = $grname;
110
111 80 foreach my $member (split(/\s+/, $members)) {
112 18 ▶ next unless(exists($UIDsByUsername{$member}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $File::Find::Rule::Permissions::UIDsByUsername{$member} 0 18 18
113 18 $UIDinGID{$gid}{$UIDsByUsername{$member}} = 1;
114 }
115 }
116 }
117  
118 sub File::Find::Rule::permissions {
119 26 ▶ my $self = shift()->_force_object;
Subroutine permissions: called CC=2 SCAR=7.0
Pod undocumented
120 26 ▶ my %criteria = UNIVERSAL::isa($_[0], "HASH") ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 0 26 26
121  
122 $self->exec(sub {
123 13312 ▶ my $file = shift;
Subroutine __ANON__: called CC=35 SCAR=35.8
124 13312 my $userid;
125
126 # first check that we've got the mandatory parameters
127 13312 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $criteria{"isReadable"} or exists $criteria{"isWriteable"} or exists $criteria{"isExecutable"}) 0 13312 13312
Condition83%5/6
exists $criteria{"isReadable"} or exists $criteria{"isWriteable"} or exists $criteria{"isExecutable"}
l!l&&r!l&&!r
921640960
exists $criteria{"isReadable"} or exists $criteria{"isWriteable"}
l!l&&r!l&&!r
512040964096
MC/DC0%0/3
exists $criteria{"isReadable"} or exists $criteria{"isWriteable"} or exists $criteria{"isExecutable"}
exists $criteria{"isReadable"} exists $criteria{"isWriteable"} exists $criteria{"isExecutable"}
Truth table75%3/4
A or B or C
A= exists $criteria{"isReadable"}B= exists $criteria{"isWriteable"}C= exists $criteria{"isExecutable"}
ABCresult
1XX1
01X1
0011
0000
128 !exists($criteria{isReadable}) &&
129 !exists($criteria{isWriteable}) &&
130 !exists($criteria{isExecutable})
131 0 ) { die("File::Find::Rule::Permissions::permissions: no criteria\n"); }
132
133 # if a user has been specified, first get their UID (from their
134 # username if necessary). If a user *hasn't* been specified,
135 # then we pretend one has anyway
136 13312 ▶ $criteria{user} = geteuid() unless(exists($criteria{user}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $criteria{"user"} 0 13312 13312
137 13312 ▶ if($criteria{user} =~ /^\d+$/) { $userid = $criteria{user}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($criteria{'user'} =~ /^\d+$/) { } 0 13312 13312
138 13312 else { $userid = $UIDsByUsername{$criteria{user}}; }
139
140 # now divine the user's permissions. first get the file's mode
141 # bits and ownership
142 13312 my($mode, $file_uid, $file_gid) = (&stat($file))[2,4,5];
143
144 # now check user/group perms. Set isReadable etc if the mode has
145 # the owner bit set and the user is the owner, or has the group bit
146 # set and the user is in the right group
147 my $isReadable = $mode & (
148 (($userid == $file_uid) ? S_IRUSR : 0) |
149 13312 ▶ ($UIDinGID{$file_gid}{$userid} ? S_IRGRP : 0)
Branch: 2 branches
BranchTrueFalseTotal
$userid == $file_uid ? : 3584 9728 13312
$File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid} ? : 6144 7168 13312
150 );
151 my $isWriteable = $mode & (
152 (($userid == $file_uid) ? S_IWUSR : 0) |
153 13312 ▶ ($UIDinGID{$file_gid}{$userid} ? S_IWGRP : 0)
Branch: 2 branches
BranchTrueFalseTotal
$userid == $file_uid ? : 3584 9728 13312
$File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid} ? : 6144 7168 13312
154 );
155 my $isExecutable = $mode & (
156 (($userid == $file_uid) ? S_IXUSR : 0) |
157 13312 ▶ ($UIDinGID{$file_gid}{$userid} ? S_IXGRP : 0)
Branch: 2 branches
BranchTrueFalseTotal
$userid == $file_uid ? : 3584 9728 13312
$File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid} ? : 6144 7168 13312
158 );
159 # now check "other" perms. Set isReadable etc if "other" bit is
160 # set and user is *not* owner and *not* in right group
161 13312 ▶ if($userid != $file_uid && !$UIDinGID{$file_gid}{$userid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userid != $file_uid and not $File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid}) 3584 9728 13312
Condition100%3/3
$userid != $file_uid and not $File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid}
!ll&&!rl&&r
358461443584
MC/DC100%2/2
$userid != $file_uid and not $File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid}
$userid != $file_uid not $File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid}
Truth table100%3/3
A and not(B)
A= $userid != $file_uidB= not $File::Find::Rule::Permissions::UIDinGID{$file_gid}{$userid}
ABresult
0X0
100
111
162 3584 $isReadable = $mode & S_IROTH;
163 3584 $isWriteable = $mode & S_IWOTH;
164 3584 $isExecutable = $mode & S_IXOTH;
165 }
166  
167 # root can read and write anything, can execute anything
168 # with any x bit set
169 13312 ▶ $isReadable = $isWriteable = 1 if($userid == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $userid == 0 3072 10240 13312
170 13312 ▶ $isExecutable = 1 if($userid == 0 && $mode & 0111);
Branch: 1 branch
BranchTrueFalseTotal
if $userid == 0 and $mode & 73 2688 10624 13312
Condition100%3/3
$userid == 0 and $mode & 73
!ll&&!rl&&r
102403842688
MC/DC100%2/2
$userid == 0 and $mode & 73
$userid == 0 $mode & 73
Truth table100%3/3
A and B
A= $userid == 0B= $mode & 73
ABresult
0X0
100
111
171
172 # Why do all those constants look like incantations to the elder gods?
173 #
174 # S'IXOTH, S'IXOTH IRGRP!
175
176 13312 ▶ if(exists($criteria{isReadable}) && $criteria{isReadable}) { # must be readable
Branch: 2 branches
BranchTrueFalseTotal
if (exists $criteria{'isReadable'} and $criteria{'isReadable'}) { } 3072 10240 13312
elsif (exists $criteria{'isReadable'} and not $criteria{'isReadable'}) { } 2048 16384 18432
Condition100%3/3
exists $criteria{"isReadable"} and $criteria{"isReadable"}
!ll&&!rl&&r
819220483072
MC/DC100%2/2
exists $criteria{"isReadable"} and $criteria{"isReadable"}
exists $criteria{"isReadable"} $criteria{"isReadable"}
Truth table100%3/3
A and B
A= exists $criteria{"isReadable"}B= $criteria{"isReadable"}
ABresult
0X0
100
111
177 3072 ▶ return 0 unless($isReadable);
Branch: 1 branch
BranchTrueFalseTotal
unless $isReadable 1280 1792 3072
178 ▶ } elsif(exists($criteria{isReadable}) && !$criteria{isReadable}) { # must not be ...
Condition66%2/3
exists $criteria{"isReadable"} and not $criteria{"isReadable"}
!ll&&!rl&&r
819202048
MC/DC50%1/2
exists $criteria{"isReadable"} and not $criteria{"isReadable"}
exists $criteria{"isReadable"} not $criteria{"isReadable"}
Truth table66%2/3
A and not(B)
A= exists $criteria{"isReadable"}B= not $criteria{"isReadable"}
ABresult
0X0
100
111
179 2048 ▶ return 0 if($isReadable);
Branch: 1 branch
BranchTrueFalseTotal
if $isReadable 1280 768 2048
180 }
181 10752 ▶ if(exists($criteria{isWriteable}) && $criteria{isWriteable}) { # must be writeable
Branch: 2 branches
BranchTrueFalseTotal
if (exists $criteria{'isWriteable'} and $criteria{'isWriteable'}) { } 2304 8448 10752
elsif (exists $criteria{'isWriteable'} and not $criteria{'isWriteable'}) { } 2304 12288 14592
Condition100%3/3
exists $criteria{"isWriteable"} and $criteria{"isWriteable"}
!ll&&!rl&&r
614423042304
MC/DC100%2/2
exists $criteria{"isWriteable"} and $criteria{"isWriteable"}
exists $criteria{"isWriteable"} $criteria{"isWriteable"}
Truth table100%3/3
A and B
A= exists $criteria{"isWriteable"}B= $criteria{"isWriteable"}
ABresult
0X0
100
111
182 2304 ▶ return 0 unless($isWriteable);
Branch: 1 branch
BranchTrueFalseTotal
unless $isWriteable 896 1408 2304
183 ▶ } elsif(exists($criteria{isWriteable}) && !$criteria{isWriteable}) {
Condition66%2/3
exists $criteria{"isWriteable"} and not $criteria{"isWriteable"}
!ll&&!rl&&r
614402304
MC/DC50%1/2
exists $criteria{"isWriteable"} and not $criteria{"isWriteable"}
exists $criteria{"isWriteable"} not $criteria{"isWriteable"}
Truth table66%2/3
A and not(B)
A= exists $criteria{"isWriteable"}B= not $criteria{"isWriteable"}
ABresult
0X0
100
111
184 2304 ▶ return 0 if($isWriteable);
Branch: 1 branch
BranchTrueFalseTotal
if $isWriteable 1408 896 2304
185 }
186 8448 ▶ if(exists($criteria{isExecutable}) && $criteria{isExecutable}) {# must be executable
Branch: 2 branches
BranchTrueFalseTotal
if (exists $criteria{'isExecutable'} and $criteria{'isExecutable'}) { } 2048 6400 8448
elsif (exists $criteria{'isExecutable'} and not $criteria{'isExecutable'}) { } 2048 8704 10752
Condition100%3/3
exists $criteria{"isExecutable"} and $criteria{"isExecutable"}
!ll&&!rl&&r
435220482048
MC/DC100%2/2
exists $criteria{"isExecutable"} and $criteria{"isExecutable"}
exists $criteria{"isExecutable"} $criteria{"isExecutable"}
Truth table100%3/3
A and B
A= exists $criteria{"isExecutable"}B= $criteria{"isExecutable"}
ABresult
0X0
100
111
187 2048 ▶ return 0 unless($isExecutable);
Branch: 1 branch
BranchTrueFalseTotal
unless $isExecutable 832 1216 2048
188 ▶ } elsif(exists($criteria{isExecutable}) && !$criteria{isExecutable}) {
Condition66%2/3
exists $criteria{"isExecutable"} and not $criteria{"isExecutable"}
!ll&&!rl&&r
435202048
MC/DC50%1/2
exists $criteria{"isExecutable"} and not $criteria{"isExecutable"}
exists $criteria{"isExecutable"} not $criteria{"isExecutable"}
Truth table66%2/3
A and not(B)
A= exists $criteria{"isExecutable"}B= not $criteria{"isExecutable"}
ABresult
0X0
100
111
189 2048 ▶ return 0 if($isExecutable);
Branch: 1 branch
BranchTrueFalseTotal
if $isExecutable 1216 832 2048
190 }
191
192 6400 return 1;
193 26 } );
194 }
195  
196  
197 =head1 FEEDBACK
198  
199 I welcome constructive criticism. If you need to report a bug, it would
200 be most helpful - and it'll get fixed quicker - if you include sufficient
201 information for me to be able to replicate it consistently. Especially
202 useful are test scripts which fail with the current implementation but
203 should pass.
204  
205 Please report bugs either by email or using L<http://rt.cpan.org/>.
206  
207 =head1 SOURCE CODE REPOSITORY
208  
209 L<git://github.com/DrHyde/perl-modules-File-Find-Rule-Permissions.git>
210  
211 =head1 SEE ALSO
212  
213 File::Find::Rule
214  
215 =head1 AUTHOR, COPYRIGHT and LICENCE
216  
217 Copyright 2003-2009 David Cantrell E<lt>david@cantrell.org.ukE<gt>
218  
219 Based on code by Kate Pugh (File::Find::Rule::MP3Info) and Richard Clamp.
220  
221 This software is free-as-in-speech software, and may be used,
222 distributed, and modified under the terms of either the GNU
223 General Public Licence version 2 or the Artistic Licence. It's
224 up to you which one you use. The full text of the licences can
225 be found in the files GPL2.txt and ARTISTIC.txt, respectively.
226  
227 =head1 CONSPIRACY
228  
229 This module is also free-as-in-mason software.
230  
231 =cut
232  
233 2 1;
↑ Summary