Don't ask to ask, just ask | |
PerlMonks |
Re: changing user mid-scriptby kschwab (Vicar) |
on Oct 10, 2001 at 02:14 UTC ( [id://117887]=note: print w/replies, xml ) | Need Help?? |
There is a concept of both a "real" and "effective"
uid in a process. Most things that a process does
are done in the context of the "effective uid". ( For
example, if you open a file, it will be owned by
the effective uid). If the process is running as the root user, it can change both it's real and effective uid. The catch is, you can only change these if your "effective" uid is 0 (root). So, yes, you can change them midstream, but your effective uid had to be '0' when you started, and you'll have to reset it back to '0' before you change it to something else. Further complicating the matter, you can't set it to '0' unless your real uid is '0'. Whee. This means the script has to be run as root, either directly, or via setuid flags on the script. This can be dangerous. Caveat Emptor. Items to peruse:
An example:
Update: I was addressing the question rather directly. (changing uid's midway through a script). It's probably a better idea to use two different scripts, and something like sudo or super that would allow one user to run a script as another user. Setuid scripts should be a last resort.
In Section
Seekers of Perl Wisdom
|
|