2

I need to write a code so that it could verify the DIRECTORY permission. Been 'google' around and looks to me that the use File::stat may match my needs. However, when I tried on my code as below, it doesn't give result as I aspected. I'm looking for if the directory is not having the read and execute permission , it will show the directory name. Can anybody help? Below are lines of code I found from web, tried but seem not giving a favor output.

#! /usr/bin/perl

use strict ;
use warnings ;
use File::stat ;

my $path = "/nfs/ch/test_dir" ;

opendir (DH, $path) || die "Fail to open dir:$!\n" ; ;
my @dir = readdir (DH) ;
closedir (DH) ;

foreach my $dir (@dir) {
 print "DIR: $dir\n" ;
 $dirmode = (stat($dir)) [2] ;
 printf "Permissions are %04o\n", $dirmode & 07777;
 print "DIRmode= $dirmode\n" ;
 }

The output is like this:- DIR: groupA Permissions are 0000 DIRmode= DIR: groupB Permissions are 0000 DIRmode=

I'm actually looking for code which could output the directory name when it found the directory at the search path is not having the GROUP and WORLD PERMISSION for read and execute. Thanks in advanced.

Grace
  • 440
  • 3
  • 6
  • 21
  • This could help : http://stackoverflow.com/questions/8647247/how-to-check-in-perl-if-the-file-permission-is-greater-than-755-i-e-group-worl – Vijay Jun 25 '13 at 05:23
  • Thanks Vijay, I have read the link before posting this question, the link seem pointing to a file only but I need my script to read through the permission of each sub-directory within a directory. Also, I couldn't find how to check if group and world mush have read and execute permission. Thanks – Grace Jun 25 '13 at 05:39

3 Answers3

2
#! /usr/bin/perl

use strict ;
use warnings ;
use File::stat ;
use Fcntl ':mode';

my $path = "/nfs/ch/test_dir" ;

opendir (DH, $path) || die "Fail to open dir:$!\n" ; ;
my @dir = readdir (DH) ;
closedir (DH) ;

foreach my $dir (@dir) {
 print "DIR: $dir\n" ;
 $dirmode = (stat($dir)) [2] ;
 printf "Permissions are %04o\n", $dirmode & 07777;
 $group_read = ($dirmode & S_IRGRP) >> 3; // group read permission
 $group_exe = ($dirmode & S_IXGRP) >> 3; // group execute permission 
print "group read = $group_read\n" ; // 0 for not set
print "group execute = group_exe\n" ; // 0 for not set
 }
TelKitty
  • 3,146
  • 3
  • 18
  • 21
  • hi thanks Telkitty, could you help me with the full code of looping through a directory to check if each sub-dirs are having the read and execute permission for group and world? I would like to report those dirs that is fail on read and execute rights. – Grace Jun 25 '13 at 05:42
1

I would use File::Find and stat.

#!/usr/bin/perl

use warnings;
use strict;

use File::Find;

my $full_path = "/Users/chrisblack/Misc";

find(\&wanted, $full_path);

sub wanted {
    if (! -e || ! -d) {
        return;
    }

    my $mode = (stat($File::Find::name))[2];
    $mode = $mode & 0777;
    if(($mode & 060) == 060 || ($mode & 070) == 070) {
        print $File::Find::name, " has read and write perms\n";
    }
}

Here's a test run:

$ ls -l
drwxrwxrwx  2 chrisblack  staff   68 Jun 24 22:40 a_dir
drwxr--r-x  2 chrisblack  staff   68 Jun 24 22:26 b_dir
drwx--xr-x  2 chrisblack  staff   68 Jun 24 22:26 c_dir
drwxrw-r-x  2 chrisblack  staff   68 Jun 24 22:26 d_dir
-rwxr--r--  1 chrisblack  staff  395 Jun 24 22:53 test.pl
-rwxrwx---  1 chrisblack  staff  191 May 17 08:36 test.py
$ ./test.pl
/Users/chrisblack/Misc/a_dir has read and write perms
/Users/chrisblack/Misc/d_dir has read and write perms
chrsblck
  • 3,948
  • 2
  • 17
  • 20
  • Thanks chrsblck for your sample codes, tried it but it gave permission denied error: Can't cd to (/nfs/test_dir/) groupA: Permission denied at /tmp/test.pl line 9 – Grace Jun 25 '13 at 06:09
  • @Grace that means you(the user running the script) don't have execute permissions on the directory you're trying to search. In this case: `/nfs/test_dir` – chrsblck Jun 25 '13 at 06:14
  • I supposed I have it as the permission to /nfs/test_dir is set to 755. – Grace Jun 25 '13 at 06:21
  • @Grace sorry, I misread your error. `groupA` is the directory with incorrect permissions. – chrsblck Jun 25 '13 at 06:23
  • Say I have following dir--> drwxr--r-x 7 grace users 4096 2013-06-20 20:01 groupA drwxr----- 7 grace users 4096 2013-06-24 19:48 groupB drwxr-xr-x 1 grace users 404 2013-06-24 23:03 groupC I want it to be able to report out the direcories "groupA and groupB is restricted in permission." May I know how am I going to do that? I want to capture the directory name wheneve it is lock in read and execute permission for GROUP and WORld. – Grace Jun 25 '13 at 06:31
  • Pardon me.. I'm not really understand the codes. as even I tried to change the directory permission on the check path, it still flagging permission denied error. – Grace Jun 25 '13 at 06:48
0

Perl code that works for me. To check if directory have "Write" permission or not,

open(FH, ">check_write.txt") 
or die("Cannot write. Check write permission!");
close(FH);
system "rm check_write.txt";
MayurKubavat
  • 341
  • 4
  • 10