When I execute the following code as root
import os
try:
if os.getuid() == 0:
import pwd, grp
os.setgroups([])
os.setgid(grp.getgrnam('my_user').gr_gid)
os.setuid(pwd.getpwnam('my_group').pw_uid)
os.umask(077)
print 'dropped privileges successfully'
else:
print 'no need to drop privileges'
except:
print 'unable to drop privileges'
print os.system('ls -lsa ~/')
then the last statement prints ls: cannot open directory /root/: Permission denied
.
The cause is clear, but the question is: What do I need to do so that ~
will expand to /home/my_user
?