Created
November 28, 2020 07:25
-
-
Save zmarffy/0fbac1df3f2adaa4819da799a2a0508a to your computer and use it in GitHub Desktop.
A Python function that sets the user of itself if run with `sudo` to the user who used `sudo`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pwd | |
def demote_to_caller(): | |
if os.geteuid() == 0: | |
d = pwd.getpwnam(os.environ["SUDO_USER"]) | |
os.setgid(d.pw_gid) | |
os.setuid(d.pw_uid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment