Family Guy has asked for the wisdom of the Perl Monks concerning the following question:

I am a relative novice on intern and i have been learning Perl recently. One of my co-workers gave me a script to write for him which is fine but apparently it needs to be admin enabled to create a certain file in the directory required, now the Co-worker in question has access but he wants the program to do it as the script is useless without that ability. So is it possible for a script to change user during it's operation and return to the same user, if so anyone got any hints ? i have a bog standard user name and password but the script need to change to the admin password that i have to create the file i am writing to

Replies are listed 'Best First'.
Re: Program changing users
by Joost (Canon) on Jul 20, 2004 at 10:19 UTC
    If you're feeling adventureous you can set the suid bit on the program (if you're on unix) but apparently it will not work everywhere because of security issues. I've never used it, because I can't figure out when it's safe to use.

    The safest way appears to be to make a suid wrapper program in C (shouldn't be much work) and let that execute the script for you.

    Option 2 is to use sudo or su -c:

    sudo program options
    if the user in question has sudo powers.

    Anyway, 95% of the time you don't want all of this anyway, because if YOU can't write to a specific directory, you shouldn't be able to run a program that can. Except for the exceptions, ofcourse.

Re: Program changing users
by ercparker (Hermit) on Jul 20, 2004 at 10:25 UTC
    as an example if I had a script running as a privileged user and wanted to
    drop owner script is running as to write to some dir I could do something
    like this:
    my ($login,$pass,$uid,$gid) = getpwnam('username'); $) = $gid; $> = $uid;
    checkout perldoc perlvar (info on Perl predefined variables)
Re: Program changing users
by gellyfish (Monsignor) on Jul 20, 2004 at 10:41 UTC

    You don't say what operating system this is to run on - the ways that you might achieve this are different on , say, windows than Unix

    /J\

Re: Program changing users
by danielcid (Scribe) on Jul 20, 2004 at 12:59 UTC

    To change the user(setuid, etc) of a process the owner of
    that process must be "root". So, if your program needs
    root access, why caring about it ? If you are using
    "Linux", just add an entry in the /etc/sudoers to permit
    this program to be executed as root... If you are using
    windows, I think that the only solution is to run it as
    the administrator.

    -DBC