1package filetest; 2 3our $VERSION = '1.02'; 4 5=head1 NAME 6 7filetest - Perl pragma to control the filetest permission operators 8 9=head1 SYNOPSIS 10 11 $can_perhaps_read = -r "file"; # use the mode bits 12 { 13 use filetest 'access'; # intuit harder 14 $can_really_read = -r "file"; 15 } 16 $can_perhaps_read = -r "file"; # use the mode bits again 17 18=head1 DESCRIPTION 19 20This pragma tells the compiler to change the behaviour of the filetest 21permission operators, C<-r> C<-w> C<-x> C<-R> C<-W> C<-X> 22(see L<perlfunc>). 23 24The default behaviour of file test operators is to use the simple 25mode bits as returned by the stat() family of system calls. However, 26many operating systems have additional features to define more complex 27access rights, for example ACLs (Access Control Lists). 28For such environments, C<use filetest> may help the permission 29operators to return results more consistent with other tools. 30 31The C<use filetest> or C<no filetest> statements affect file tests defined in 32their block, up to the end of the closest enclosing block (they are lexically 33block-scoped). 34 35Currently, only the C<access> sub-pragma is implemented. It enables (or 36disables) the use of access() when available, that is, on most UNIX systems and 37other POSIX environments. See details below. 38 39=head2 Consider this carefully 40 41The stat() mode bits are probably right for most of the files and 42directories found on your system, because few people want to use the 43additional features offered by access(). But you may encounter surprises 44if your program runs on a system that uses ACLs, since the stat() 45information won't reflect the actual permissions. 46 47There may be a slight performance decrease in the filetest operations 48when the filetest pragma is in effect, because checking bits is very 49cheap. 50 51Also, note that using the file tests for security purposes is a lost cause 52from the start: there is a window open for race conditions (who is to 53say that the permissions will not change between the test and the real 54operation?). Therefore if you are serious about security, just try 55the real operation and test for its success - think in terms of atomic 56operations. Filetests are more useful for filesystem administrative 57tasks, when you have no need for the content of the elements on disk. 58 59=head2 The "access" sub-pragma 60 61UNIX and POSIX systems provide an abstract access() operating system call, 62which should be used to query the read, write, and execute rights. This 63function hides various distinct approaches in additional operating system 64specific security features, like Access Control Lists (ACLs) 65 66The extended filetest functionality is used by Perl only when the argument 67of the operators is a filename, not when it is a filehandle. 68 69=head2 Limitation with regard to C<_> 70 71Because access() does not invoke stat() (at least not in a way visible 72to Perl), B<the stat result cache "_" is not set>. This means that the 73outcome of the following two tests is different. The first has the stat 74bits of C</etc/passwd> in C<_>, and in the second case this still 75contains the bits of C</etc>. 76 77 { -d '/etc'; 78 -w '/etc/passwd'; 79 print -f _ ? 'Yes' : 'No'; # Yes 80 } 81 82 { use filetest 'access'; 83 -d '/etc'; 84 -w '/etc/passwd'; 85 print -f _ ? 'Yes' : 'No'; # No 86 } 87 88Of course, unless your OS does not implement access(), in which case the 89pragma is simply ignored. Best not to use C<_> at all in a file where 90the filetest pragma is active! 91 92As a side effect, as C<_> doesn't work, stacked filetest operators 93(C<-f -w $file>) won't work either. 94 95This limitation might be removed in a future version of perl. 96 97=cut 98 99$filetest::hint_bits = 0x00400000; # HINT_FILETEST_ACCESS 100 101sub import { 102 if ( $_[1] eq 'access' ) { 103 $^H |= $filetest::hint_bits; 104 } else { 105 die "filetest: the only implemented subpragma is 'access'.\n"; 106 } 107} 108 109sub unimport { 110 if ( $_[1] eq 'access' ) { 111 $^H &= ~$filetest::hint_bits; 112 } else { 113 die "filetest: the only implemented subpragma is 'access'.\n"; 114 } 115} 116 1171; 118