There is a perl script that needs to run as root but we must make sure the user who runs the script did not log-in originally as user 'foo' as it will be removed during the script.
So how can I find out if the user, who might have su-ed several times since she logged in has not impersonated 'foo' at any time in that chain?
I found an interesting perl script that was calling the following two shell scripts, but I think that would only work on Solaris.
my $shell_parent =
`ps -ef | grep -v grep | awk \'{print \$2\" \"\$3}\' | egrep \"^@_\" | awk \'{print \$2}'`;
my $parent_owner =
`ps -ef | grep -v grep | awk \'{print \$1\" \"\$2}\' | grep @_ | awk \'{print \$1}\'`;
This needs to work on both Linux and Solaris and I'd rather eliminate the repeated calls to he the shell and keep the whole thing in Perl.
Here's a Perl program that checks for direct setuid change:
But since you mentioned that the setuid change may have occured anytime before, you probably have to parse the output of
ps
: I would do it using the following command. This command only uses features defined in POSIX, so I hope it is portable to all kinds of systems:Maybe the following is what you want. The function
hasBeenUser
reads the process table and then follows the process chain from the current process down the parent process. If any of the processes on the way has auser
orreal user
field equal to the username in question, the function returns a nonzero value.Quick and dirty and (UNIX only):
The
who am i
command returns the owner of the TTY - i.e. who you were when you logged in.If you want to do this in pure perl:
This will return the correct user, even after multiple su's. This usually freaks out your (less experienced) sysadmins.
I recognized a corner case when calling scripts from mc (at least in our RHEL's), which results that the
who am i
does not output anything. To circumvent that, I produced the following one-liner in bash:Essentially, this walks backwards on the tree output of
ps -u $USER fh
and then crops on the topmost username column.Thoughts, better solutions are welcome :-)