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

I have a script that depends on knowing the user name of the person running it. I can easily obtain this using getpwuid($>) or getlogin() or even 'whoami'.

The problem is that this script is executed by a parent script as a means of getting around a security issue using sudo.

my @script = ("sudo", "-u", "user", "/usr/local/bin/script"); system @script;

And to make matters worse this parent script sometimes gets called using a perl expect script.

What I'm trying to figure out is how I can determine the original user id for any given chain of events that causes my script to run. Wether it was nested within any number of parent scripts or if it was run using sudo -u.

Replies are listed 'Best First'.
Re: Obtaining userid from script called by sudo and other scripts
by Eliya (Vicar) on Feb 14, 2012 at 21:03 UTC

    Not sure I understand your script nesting problem, but as for sudo, the environment variable SUDO_USER (set by sudo to who called sudo) might help.

    $ sudo -u nobody perl -E 'say scalar getpwuid $>; say $ENV{SUDO_USER}' nobody eliya
      the SUDO_USER variable solved the problem: Thank you
Re: Obtaining userid from script called by sudo and other scripts
by MidLifeXis (Monsignor) on Feb 15, 2012 at 13:38 UTC

    If you don't trust the environment variable SUDO_USER, you could also walk the process tree upward until you find your answer.

    --MidLifeXis