From perldoc, 
stat function returns a 13 element list
I think
my $perms = stat($conffile)
will not work as you expect.


On Saturday 26 November 2005 04:03, Abid Khwaja wrote:
> Thanks for the pointers.  Here's what I did:
>
> 1. stat the file to see the mode set by the system
>
> demerzel:~/etc abid$ ls -l
> total 16
> -rw-rw----   1 root  uucp  311 Nov 14 15:20 slist.conf
> demerzel:~/etc abid$ stat -s slist.conf
> st_dev=234881026 st_ino=6858828 st_mode=0100660 st_nlink=1 st_uid=0
> st_gid=66 st_rdev=0 st_size=311 st_atime=1132939903
> st_mtime=1131999637 st_ctime=1132948249 st_blksize=4096 st_blocks=8
> st_flags=0
>
> 2. check for root:uucp and 660
>
> use Fcntl;
> use File::stat;
>
> my $etcgroup = "/etc/group";
> my $conffile = "/Users/abid/etc/slist.conf";
> my $shouldbegroup = "uucp";
> my $shouldbeuid = 0;
> my $shouldbemode = 0100660;     # -rw-rw---- perms
>
> my $shouldbegid;
>
> sysopen(GROUP, $etcgroup, O_RDONLY)
>
>          || die "can't find/open $etcgroup: $!\n";
>
> while (<GROUP>) {
>          if (/^$shouldbegroup/ && /\d+/) {
>                  $shouldbegid = $&;
>                  last;
>          }
> }
>
> close(GROUP)
>
>          || die "can't close $etcgroup: $!\n";
>
> # get config file permissions
> my $perms = stat($conffile)
>
>          || die "can't find $conffile: $!\n";
>
> # are permissions set correctly?
> unless (($perms->uid == $shouldbeuid) &&
>          ($perms->gid == $shouldbegid) &&
>          (($perms->mode & $shouldbemode) == $shouldbemode)) {
>                  print "Aborting!  Incorrect config file perms!\n";
>                  exit 1
>          }
>
> On Nov 24, 2005, at 9:37 PM, John W. Krahn wrote:
> > Abid Khwaja wrote:
> >> I've been trying to figure out how to use File::stat to check file
> >> modes but haven't had much luck understanding how it works from the
> >> documentation.  My goal is to check if a file is owned by a specific
> >> user, group owned by a specific group and has mode 660.  I have the
> >> uid and gid checks down but need help with with the mode check.
> >
> > The documentation for the stat function explains how to do that:
> >
> > perldoc -f stat
> > [snip]
> >         You can import symbolic mode constants ("S_IF*") and functions
> >         ("S_IS*") from the Fcntl module:
> >
> >             use Fcntl ’:mode’;
> >
> >             $mode = (stat($filename))[2];
> >
> >             $user_rwx      = ($mode & S_IRWXU) >> 6;
> >             $group_read    = ($mode & S_IRGRP) >> 3;
> >             $other_execute =  $mode & S_IXOTH;
> >
> >             printf "Permissions are %04o\n", S_IMODE($mode), "\n";
> >
> >             $is_setuid     =  $mode & S_ISUID;
> >             $is_setgid     =  S_ISDIR($mode);
> >
> >
> > Also the STAT(2) man page may help.
> >
> >> So I'm doing the following test:
> >>
> >> use File::stat;
> >> my $conffile = "/etc/slist.conf";
> >> my $perms = stat($conffile)
> >>
> >>    || die "can't find $conffile: $!\n";
> >>
> >> my $mode = $perms->mode;
> >> print "$mode\n";
> >>
> >> against the following file:
> >>
> >> ----------   1 joe  uucp  311 Nov 14 15:20 slist.conf
> >>
> >> When I run the code, here's what I get:
> >>
> >> 32768
> >>
> >> The output varies as I change the file mode but I don't see the
> >> relation
> >> between the code output and the mode.  If someone can explain to
> >> me  how
> >> this works, it would be greatly appreciated.  I'm running this on
> >> a MacOS
> >> X box but the code needs to run cross-unix-platform.
> >
> > That is because most discussions about the mode assume an octal
> > representation
> > where the three least significant bits are the world permissions
> > and the next
> > three bits are the group permissions and the next three bits are
> > the owner
> > permissions.  Of those three bits the least significant bit is execute
> > permission and the next bit is write permission and the next bit is
> > read
> > permission.  For example:
> >
> > $ touch TEST
> > $ chmod 0752 TEST
> >          ^^^
> >          ogw
> >
> > $ ls -l TEST
> > -rwxr-x-w-  1 john users 0 2005-11-24 18:20 TEST
> >  ^^^^^^^^^
> >  ooogggwww
> >
> > $ perl -le'
> > use File::stat;
> > my $st = stat "TEST" or die "stat: $!";
> > printf "%o\n",  $st->[2];
> > '
> > 100752
> >    ^^^
> >    ogw
> >
> >
> >
> > John
> > --
> > use Perl;
> > program
> > fulfillment
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > <http://learn.perl.org/> <http://learn.perl.org/first-response>

-- 
If you can't convince them, confuse them.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to