I am facing the following problem, I am raising the provilegios with "Elevate" but after running os.mkdir () I would like him to lose his privileges and return to being a common user.
import os
import sys
import subprocess
from elevate import elevate
from os import popen
def is_root():
return os.getuid() == 0
print("before ", is_root())
user = os.getlogin()
elevate(graphical=False)
username = input("Enter the name of the user to be created in /home: ")
os.mkdir('/home/' + username)
print(os.listdir('/home'))
print("after ", is_root())
I made a checker if the user is privileged and even after running it appears as true where it means that he is with super power.
[garden@server ~]$ python3 mkdir.py
before False
[sudo] password for garden:
before True
Enter your name : user10
['garden', 'user10']
after True
I would like to know if there is a way to elevate privileges and then withdraw, so that in case of an exception the code is not running with an elevated user.
thank you