Hi there,
I looked around and couldn't find an answer to this question (and I'm now wondering is there is one :-)
I have a piece of code in which I need to switch from root to a particular user, run the command as this user and would like to return to root. I don't want to use, if possible at all, "su".
Here is the snippet code that is run as root initially:
As you can imagine, you can't set back to root (setuid(0)) - running as 'user' at this point.
Thanks in advance for any help.
D.
I looked around and couldn't find an answer to this question (and I'm now wondering is there is one :-)
I have a piece of code in which I need to switch from root to a particular user, run the command as this user and would like to return to root. I don't want to use, if possible at all, "su".
Here is the snippet code that is run as root initially:
Code:
import os
try:
if os.name == 'posix':
import pwd
pw = pwd.getpwnam(user)
uid = pw.pw_uid
os.setuid(uid)
except:
return 1 # Error
finally:
# Put back root user...
os.setuid(0)
Thanks in advance for any help.
D.
Comment