aboutsummaryrefslogtreecommitdiffstats
path: root/jprov/jailconf.py
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2026-02-16 14:46:24 +0200
committerJan Tuomi <jan@jantuomi.fi>2026-02-16 14:46:24 +0200
commit34f5f546841c8e9022ac0ccf957f636907e643bb (patch)
tree8885e5f2209baa04128d75ca9bb340a999723828 /jprov/jailconf.py
parent5f46fde558c467414fa151ad962caaf7f07628fa (diff)
UpdatesHEADmain
Diffstat (limited to 'jprov/jailconf.py')
-rw-r--r--jprov/jailconf.py62
1 files changed, 55 insertions, 7 deletions
diff --git a/jprov/jailconf.py b/jprov/jailconf.py
index 0478cbd..ba304ae 100644
--- a/jprov/jailconf.py
+++ b/jprov/jailconf.py
@@ -3,6 +3,25 @@
from __future__ import annotations
from .config import JailConfig, MainConfig
+
+
+def _format_value(value: object) -> str:
+ if isinstance(value, str):
+ return f"\"{value}\""
+ if isinstance(value, bool):
+ return "true" if value else "false"
+ if isinstance(value, (int, float)):
+ return str(value)
+ raise ValueError(f"unsupported jailconf value type: {type(value).__name__}")
+
+
+def _flatten(prefix: str, value: object, out: list[tuple[str, object]]) -> None:
+ if isinstance(value, dict):
+ for key, val in value.items():
+ new_prefix = f"{prefix}.{key}" if prefix else str(key)
+ _flatten(new_prefix, val, out)
+ else:
+ out.append((prefix, value))
def render_jail_conf(
main: MainConfig,
jail: JailConfig,
@@ -18,14 +37,43 @@ def render_jail_conf(
f"{name} {{",
f" host.hostname = \"{name}\";",
f" path = \"{root}\";",
- " mount.devfs;",
- " exec.clean;",
" exec.start = \"/bin/sh /etc/rc\";",
" exec.stop = \"/bin/sh /etc/rc.shutdown jail\";",
- "}",
]
- base = "\n".join(lines)
- if jail.extra_jail_conf:
- base += "\n" + jail.extra_jail_conf.rstrip() + "\n"
- return base
+ if jail.jailconf:
+ flattened: list[tuple[str, object]] = []
+ _flatten("", jail.jailconf, flattened)
+ for key, value in flattened:
+ if value is True:
+ lines.append(f" {key};")
+ continue
+ if value is False:
+ continue
+ lines.append(f" {key} = {_format_value(value)};")
+
+ # Exec commands from [cmds], with env prefix
+ env_prefix = ""
+ if jail.env:
+ pairs = [f"{k}={v}" for k, v in jail.env.items()]
+ env_prefix = "env " + " ".join(pairs) + " "
+
+ cmd_map = {
+ "prestart": "exec.prestart",
+ "poststart": "exec.poststart",
+ "prestop": "exec.prestop",
+ "poststop": "exec.poststop",
+ "start": "exec.start",
+ "stop": "exec.stop",
+ }
+ for key, jailconf_key in cmd_map.items():
+ cmd = jail.cmds.get(key)
+ if cmd:
+ if env_prefix and not cmd.startswith("env "):
+ cmd_value = f"{env_prefix}{cmd}"
+ else:
+ cmd_value = cmd
+ lines.append(f" {jailconf_key} = \"{cmd_value}\";")
+
+ lines.append("}")
+ return "\n".join(lines) + "\n"