in reply to getcwd() as different user

Do you get the 'Operation not permitted' error when you're setting your uid or when you're running getcwd? This code works on my Linux box
#! /usr/bin/perl -w -T use strict; use Cwd; $ENV{PATH} = "/usr/local/bin:/usr/bin:/bin"; delete($ENV{BASH_ENV}); #500 is my non-root uid that I want to test with; my $newuid = 500; unless ($< == 0 and $> == 0) { die "must be root"; } system('whoami'); print getcwd."\n"; $> = $< = $newuid ; system('whoami'); print getcwd."\n";

Replies are listed 'Best First'.
Re^2: getcwd() as different user
by 0xbeef (Hermit) on Nov 18, 2005 at 22:42 UTC
    Oh grief, I misinterpreted the error to be the result of the "$<" operation. The actual problem is the fact that my now lesser-privileged user is not authorised to perform the next "$< = $> = $newid" iteration.

    Hmm, my question should actually read: "How do I perform seteid()" or "How do I temporarily drop my privileges before calling a function? ". The obvious answer is $>. *hides in shame*

    -0xbeef

      If anyone's interested, here's a snippet with the basic working logic. Although the "id -G" portion is not the safest way of doing this, I just don't know how:

      my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, +$blksize,$blocks) = stat($targetdir); $newuser = getpwuid($uid); $groups = `/usr/bin/id -G $newuser`; $) = "$groups"; $> = $uid; if ( not chdir("$targetdir")) { print "$targetdir is inaccessible to its owner\n"; next; } else { $currentdir = Cwd::getcwd(); my $lasterr = $!; if (not defined $currentdir) { # incorrect underlying mount-point permissions print "getcwd() failed for $targetdir:$lasterr\n"; } else { print "getcwd() for $over is OK."); } }