def become_user(name):
'''
Change the current process' effective UID to that of the given user name.
Can only be called by super user 0. This function is only intended for use
from the ``init`` process during system boot.
:arg name: An OS user name. Must be found in the ``password`` database, or
a replacement authentication system.
:returns: The user's home directory.
'''
uid = ave.pwd.getpwnam_uid(name)
gid = ave.pwd.getpwnam_gid(name)
if os.geteuid() == uid:
return
if os.geteuid() != 0:
raise Exception('only root can execute with modified privileges')
try:
os.setgid(gid) # must be done before changing euid
os.setuid(uid)
except OSError, e:
if e.errno == errno.EPERM:
raise Exception(
'could not execute with modified privileges: %s' % str(e)
)
return ave.pwd.getpwnam_dir(name)
评论列表
文章目录