def set_process_owner(spec): import pwd, grp if ":" in spec: user, group = spec.split(":", 1) else: user, group = spec, None if group: os.setgid(grp.getgrnam(group).gr_gid) if user: os.setuid(pwd.getpwnam(user).pw_uid) return user, group
def change_uid_gid(uid, gid=None): """Try to change UID and GID to the provided values. UID and GID are given as names like 'nobody' not integer. Src: http://mail.mems-exchange.org/durusmail/quixote-users/4940/1/ """ if not os.geteuid() == 0: # Do not try to change the gid/uid if not root. return (uid, gid) = get_uid_gid(uid, gid) os.setgid(gid) os.setuid(uid)