I am setting ??5 (o+rx) permission for every directory in a path.
Normally, any user is allowed to traverse the path.
On the command line, no problem.
From my perl script, which is started as root and drops privileges to another user, it is no longer true.
What's wrong in my script ? (tested on SuSE and CENTOS, no selinux).
Code:
#!/usr/bin/perl -W
#
# THIS SCRIPT CREATES A USER AND USES THE TERRIFYING "rm -r" COMMAND
# USE IT AT YOUR OWN RISK
#
use strict;
use POSIX;
my $uname = 'test0918';
my $uid = `grep $uname /etc/passwd|cut -f3 -d":"`;
if (!$uid) {
system("useradd $uname");
$uid = `grep $uname /etc/passwd|cut -f3 -d":"`;
}
my $gid = `grep $uname /etc/passwd|cut -f4 -d":"`;
my $test_root = '/tmp/test_perms';
my $test_folders = 'level1/level2/level3';
if (-d $test_root) {
system("/bin/rm -r $test_root");
}
system("mkdir -p $test_root/$test_folders");
system("chmod -R 705 $test_root");
system("id; ls -ld $test_root; ls -lR $test_root");
setgid($gid);
setuid($uid);
system("id; ls -ld $test_root; ls -lR $test_root");
Output :
Code:
uid=0(root) gid=0(root) groups=0(root)
drwx---r-x 3 root root 4096 18-09 10:59 /tmp/test_perms
/tmp/test_perms:
total 4
drwx---r-x 3 root root 4096 18-09 10:59 level1
/tmp/test_perms/level1:
total 4
drwx---r-x 3 root root 4096 18-09 10:59 level2
/tmp/test_perms/level1/level2:
total 4
drwx---r-x 2 root root 4096 18-09 10:59 level3
/tmp/test_perms/level1/level2/level3:
total 0
uid=1007(test0918) gid=100(users) groups=0(root)
drwx---r-x 3 root root 4096 18-09 10:59 /tmp/test_perms
ls: cannot open directory /tmp/test_perms: Permission denied