Skip to content

Commit

Permalink
sync python2, types update
Browse files Browse the repository at this point in the history
  • Loading branch information
dimov-cz committed Nov 23, 2023
1 parent b150ae9 commit a78d170
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 12 deletions.
89 changes: 78 additions & 11 deletions files/docker/systemctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import fnmatch
import re
from types import GeneratorType
import pickle

__copyright__ = "(C) 2016-2023 Guido U. Draheim, licensed under the EUPL"
__version__ = "1.5.7113"
Expand Down Expand Up @@ -2960,8 +2961,10 @@ def do_start_service_from(self, conf):
exe, newcmd = self.exec_newcmd(cmd, env, conf)
logg.info(" pre-start %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
if not forkpid:
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug(" pre-start done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand Down Expand Up @@ -3136,7 +3139,9 @@ def do_start_service_from(self, conf):
logg.info("post-fail %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug("post-fail done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand All @@ -3149,7 +3154,9 @@ def do_start_service_from(self, conf):
logg.info("post-start %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug("post-start done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand Down Expand Up @@ -3257,8 +3264,10 @@ def do_start_socket_from(self, conf):
exe, newcmd = self.exec_newcmd(cmd, env, conf)
logg.info(" pre-start %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
if not forkpid:
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug(" pre-start done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand Down Expand Up @@ -3298,7 +3307,9 @@ def do_start_socket_from(self, conf):
logg.info("post-fail %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug("post-fail done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand All @@ -3309,7 +3320,9 @@ def do_start_socket_from(self, conf):
logg.info("post-start %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug("post-start done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand Down Expand Up @@ -3447,6 +3460,8 @@ def expand_list(self, group_lines, conf):
if item:
result.append(self.expand_special(item, conf))
return result
def get_PermissionsStartOnly(self, conf):
return conf.getbool("Service", "PermissionsStartOnly", "no")
def get_User(self, conf):
return self.expand_special(conf.get(Service, "User", ""), conf)
def get_Group(self, conf):
Expand Down Expand Up @@ -3534,7 +3549,7 @@ def dup2_journal_log(self, conf):
os.dup2(inp.fileno(), sys.stdin.fileno())
os.dup2(out.fileno(), sys.stdout.fileno())
os.dup2(err.fileno(), sys.stderr.fileno())
def execve_from(self, conf, cmd, env):
def execve_from(self, conf, cmd, env, runAsRoot = False):
""" this code is commonly run in a child process // returns exit-code"""
# |
runs = conf.get(Service, "Type", "simple").lower()
Expand All @@ -3544,6 +3559,10 @@ def execve_from(self, conf, cmd, env):
#
runuser = self.get_User(conf)
rungroup = self.get_Group(conf)
if runAsRoot is True:
runuser = "root"
rungroup = "root"
""" logg.debug("Executing as %s:%s", runuser, rungroup)"""
xgroups = self.get_SupplementaryGroups(conf)
envs = shutil_setuid(runuser, rungroup, xgroups)
badpath = self.chdir_workingdir(conf) # some dirs need setuid before
Expand Down Expand Up @@ -3743,7 +3762,9 @@ def do_stop_service_from(self, conf):
logg.info("post-stop %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug("post-stop done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand Down Expand Up @@ -3781,7 +3802,9 @@ def do_stop_socket_from(self, conf):
logg.info("post-stop %s", shell_cmd(newcmd))
forkpid = os.fork()
if not forkpid:
self.execve_from(conf, newcmd, env) # pragma: no cover
permissionsStartOnly = self.get_PermissionsStartOnly(conf)
runAsRoot = permissionsStartOnly
self.execve_from(conf, newcmd, env, runAsRoot) # pragma: no cover
run = subprocess_waitpid(forkpid)
logg.debug("post-stop done (%s) <-%s>",
run.returncode or "OK", run.signal or "")
Expand Down Expand Up @@ -6404,6 +6427,50 @@ def version_info(self):
return [self.systemd_version(), self.systemd_features()]
def test_float(self):
return 0. # "Unknown result type"
def getEnvVarsFilePath(self):
return '/run/systemd/systemd.envs'
def getEnvVars(self):
fp = self.getEnvVarsFilePath()
vars = {}
if os.path.isfile(fp):
with open(fp, 'rb') as f:
vars = pickle.load(f)
return vars
def setEnvVar(self, varName, varValue = None):
vars = self.getEnvVars()
if varValue is None:
if varName in vars:
del vars[varName]
else:
vars[varName] = varValue
with open(self.getEnvVarsFilePath(), 'wb') as f:
pickle.dump(vars, f)
def get_environment_modules(self, *args):
if len(args) == 0:
return 1
varName = args[0]
vars = self.getEnvVars()
if varName in vars:
return vars[varName]
return ''
def set_environment_modules(self, *args):
if len(args) == 0:
return 1
boom = args[0].split('=', 2)
if len(boom) != 2:
return 2
varName = boom[0]
varValue = boom[1]
logg.debug("Set env variable %s to \"%s\"", varName, varValue)
self.setEnvVar(varName, varValue)
return 0
def unset_environment_modules(self, *args):
if len(args) == 0:
return 1
varName = args[0]
logg.debug("Unset env variable %s", varName)
self.setEnvVar(varName)
return 0

def print_begin(argv, args):
script = os.path.realpath(argv[0])
Expand Down
9 changes: 8 additions & 1 deletion types/systemctl3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,13 @@ class Systemctl:
def create_port_ipv6_socket(self, conf: SystemctlConf, addr: str, port: str, dgram: bool) -> Optional[socket.socket]: ...
def extend_exec_env(self, env: Dict[str, str]) -> Dict[str, str]: ...
def expand_list(self, group_lines: List[str], conf: SystemctlConf) -> List[str]: ...
def get_PermissionsStartOnly(self, conf: SystemctlConf) -> bool: ...
def get_User(self, conf: SystemctlConf) -> Optional[str]: ...
def get_Group(self, conf: SystemctlConf) -> Optional[str]: ...
def get_SupplementaryGroups(self, conf: SystemctlConf) -> List[str]: ...
def skip_journal_log(self, conf: SystemctlConf) -> bool: ...
def dup2_journal_log(self, conf: SystemctlConf) -> None: ...
def execve_from(self, conf: SystemctlConf, cmd: List[str], env: Dict[str, str]) -> NoReturn:
def execve_from(self, conf: SystemctlConf, cmd: List[str], env: Dict[str, str], runAsRoot: bool = False) -> NoReturn:
# cmd_args: Sequence[str]
cmd_args: List[Union[str, bytes]]
def test_start_unit(self, unit: str) -> None: ...
Expand Down Expand Up @@ -698,6 +699,12 @@ class Systemctl:
def systemd_features(self) -> str: ...
def version_info(self) -> List[str]: ...
def test_float(self) -> float: ...
def getEnvVarsFilePath(self) -> str: ...
def getEnvVars(self) -> Dict[str, str]: ...
def setEnvVar(self, varName: str, varValue: Optional[str] = None) -> None: ...
def get_environment_modules(self, *args: str) -> str: ...
def set_environment_modules(self, *args: str) -> int: ...
def unset_environment_modules(self, *args: str) -> int: ...

def print_begin(argv: List[str], args: List[str]) -> None: ...
def print_begin2(args: List[str]) -> None: ...
Expand Down

0 comments on commit a78d170

Please sign in to comment.