Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I have the following lines in a script

#!/bin/perl $path = "/home/private/commondata/mydir"; $path =~ /(.+)/; $path = $1; print $path; system "mkdir","-p","$path";

I have done "chmod +s myscript" to set the setuid bit, and the script is in /home/private area with the permissions of "private" itself.I want that when any user runs the script, it sohuld run with the permission of "private" and make the directory commondata/mydir.

But when i run the script, i have the folowing error mkdir: "/home/private/commondata/mydir": Permission denied

I dont understand , as if i am correct after setting the setuid bit, the script will have the permission to write in the area with "private" permission!!!!

Thanks for your help in Advance

20040623 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: permisson prob in suid script
by pbeckingham (Parson) on Jun 23, 2004 at 11:53 UTC

    You should convert the system line to a call to File::Path::mkpath - don't call the shell while running a suid script - but before calling it, set the real to the effective user id, then don't forget to restore it afterwards:

    my $original = $<; $> = $); mkpath ........... $> = $original;

    This does what you want. The chmod +s ... that you did only makes this possible - doesn't make it happen.

      Thanks for your interest in my problem...I am actually blocked...and now facing other problem..after your reply i tried to print the $< and $> varaibles, but surprise, they are both the same..i dont understand, the suid bit is set and the real user id must be different than the effective user id. And i did run the script as a third user. Thanks for your response in Advance!!

        Can you look at the file permissions? You should see something like:

        % ls -l file -rwsr-xr-x owner group ... file
        And if you don't see that, you can achieve with the following command, run as root:
        % chmod 6755 file

Re: permisson prob in suid script
by tbone1 (Monsignor) on Jun 23, 2004 at 13:15 UTC
    What are the permissions on the parent directory, /home/private, for the user id under which you are running the script? Does it have write permission there?

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      Yes , /home/private has the permissions of user "private", under which the script runs as it is a suid script