Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: getcwd() as different user

by pileofrogs (Priest)
on Nov 18, 2005 at 21:59 UTC ( [id://509972]=note: print w/replies, xml ) Need Help??


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."); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://509972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-29 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found