Files
OpenVPN-Monitoring-Simple/DOCS/General/privilege-separation/ovpmon-helper
T

397 lines
17 KiB
Python
Raw Normal View History

#!/usr/bin/python3
"""ovpmon-helper: the only root-side entry point for the unprivileged ovpmon services.
Usage (via doas): ovpmon-helper install-config
ovpmon-helper publish-crl
ovpmon-helper service start|stop|restart|status
install-config reads the staged OpenVPN server config, validates it against a strict
allowlist of directives and installs it atomically to the OpenVPN config path
(default /etc/openvpn/server.conf).
Optional configuration: /etc/ovpmon/helper.conf (INI, section [helper]). The helper runs through
doas with a stripped environment, so it reads its own root-owned file; the file must be owned by
root and not writable by group/other, otherwise the helper refuses to run. Without the file the
built-in defaults below apply (Alpine/OpenRC layout, unchanged behaviour). Keys:
[helper]
install_dir = /opt/OpenVPN-Monitoring-Simple # PKI = <install_dir>/APP_PROFILER/easy-rsa/pki
service_manager = openrc # openrc | systemd
service_unit = openvpn # systemd default: openvpn-server@server
config_path = /etc/openvpn/server.conf # Debian/Ubuntu: /etc/openvpn/server/server.conf
staged_path = /var/lib/ovpmon/staging/server.conf
Every value is validated strictly (absolute paths without "..", fixed prefixes, unit-name
pattern, fixed set of managers); unknown keys and invalid values make the helper refuse.
OpenRC runs `/sbin/rc-service <unit> <action>`; systemd runs `systemctl <action> <unit>`
(fixed argv, fixed PATH).
"""
import configparser
import ipaddress
import json
import os
import pwd
import re
import shlex
import stat
import subprocess
import sys
CONF_PATH = "/etc/ovpmon/helper.conf"
# Built-in defaults = the values that were hard-coded before helper.conf existed
DEFAULT_INSTALL_DIR = "/opt/OpenVPN-Monitoring-Simple"
DEFAULT_SERVICE_MANAGER = "openrc"
DEFAULT_SERVICE_UNIT = "openvpn"
DEFAULT_SYSTEMD_UNIT = "openvpn-server@server"
DEFAULT_CONFIG_PATH = "/etc/openvpn/server.conf"
DEFAULT_STAGED_PATH = "/var/lib/ovpmon/staging/server.conf"
# Active settings (defaults until apply_settings() is called by load_config())
INSTALL_DIR = DEFAULT_INSTALL_DIR
SERVICE_MANAGER = DEFAULT_SERVICE_MANAGER
SERVICE_UNIT = DEFAULT_SERVICE_UNIT
STAGED = DEFAULT_STAGED_PATH
TARGET = DEFAULT_CONFIG_PATH
PKI_DIR = INSTALL_DIR + "/APP_PROFILER/easy-rsa/pki"
CRL_SRC = PKI_DIR + "/crl.pem"
SCRIPTS_DIR = "/etc/openvpn/scripts"
STATUS_LOG = "/var/log/openvpn/openvpn-status.log"
CRL_PUBLISHED = "/etc/openvpn/crl.pem"
CRL_MAX = 1024 * 1024
SERVICE_USER = "ovpmon"
MAX_SIZE = 64 * 1024
CIPHERS_RE = re.compile(r"^[A-Za-z0-9:_-]{1,200}$")
UNIT_RE = re.compile(r"^[A-Za-z0-9@._-]{1,64}$")
PATH_RE = re.compile(r"^/[A-Za-z0-9._/+@-]{1,200}$")
MANAGERS = ("openrc", "systemd")
CONF_KEYS = ("install_dir", "service_manager", "service_unit", "config_path", "staged_path")
RC_SERVICE = "/sbin/rc-service"
SYSTEMCTL_CANDIDATES = ("/usr/bin/systemctl", "/bin/systemctl")
os.environ["PATH"] = "/usr/sbin:/usr/bin:/sbin:/bin"
class Reject(Exception):
pass
def under(path, base):
real = os.path.realpath(path)
base = os.path.realpath(base)
return real == base or real.startswith(base.rstrip("/") + "/")
def need(cond, msg):
if not cond:
raise Reject(msg)
# ---------------------------------------------------------------------------
# Optional configuration /etc/ovpmon/helper.conf
# ---------------------------------------------------------------------------
def clean_path(key, value, prefix=None, min_parts=1, suffix=None):
"""Strict check of an absolute path from helper.conf; returns it without a trailing slash."""
need(PATH_RE.fullmatch(value) is not None, key + ": must be an absolute path of letters, digits and . _ / + @ -")
path = value.rstrip("/") or "/"
parts = path.split("/")[1:]
need("" not in parts and "." not in parts and ".." not in parts, key + ": no empty, '.' or '..' components")
if prefix is not None:
need(path.startswith(prefix), key + ": must be below " + prefix)
need(len(parts) >= min_parts, key + ": path too short")
if suffix is not None:
need(path.endswith(suffix), key + ": must end with " + suffix)
return path
def parse_config(text):
"""Parse and validate the text of helper.conf. Returns a dict with the keys that are set."""
cp = configparser.ConfigParser(interpolation=None, strict=True)
try:
need(all(ord(c) < 128 for c in text), "helper.conf: ASCII only")
cp.read_string(text)
except configparser.Error as e:
raise Reject("helper.conf: " + str(e).splitlines()[0][:150])
need(cp.sections() == ["helper"], "helper.conf: exactly one section [helper] is required")
raw = dict(cp["helper"])
unknown = sorted(set(raw) - set(CONF_KEYS))
need(not unknown, "helper.conf: unknown key(s): " + ", ".join(unknown)[:100])
out = {}
if "install_dir" in raw:
out["install_dir"] = clean_path("install_dir", raw["install_dir"], min_parts=2)
if "service_manager" in raw:
need(raw["service_manager"] in MANAGERS, "service_manager: must be one of " + ", ".join(MANAGERS))
out["service_manager"] = raw["service_manager"]
if "service_unit" in raw:
need(UNIT_RE.fullmatch(raw["service_unit"]) is not None and raw["service_unit"][0] not in "-.",
"service_unit: must match " + UNIT_RE.pattern + " and not start with - or .")
out["service_unit"] = raw["service_unit"]
if "config_path" in raw:
out["config_path"] = clean_path("config_path", raw["config_path"], prefix="/etc/openvpn/", min_parts=3, suffix=".conf")
if "staged_path" in raw:
out["staged_path"] = clean_path("staged_path", raw["staged_path"], prefix="/var/lib/ovpmon/", min_parts=4)
return out
def apply_settings(settings):
"""Set the module-level settings from a validated dict (missing keys: built-in defaults)."""
global INSTALL_DIR, SERVICE_MANAGER, SERVICE_UNIT, STAGED, TARGET, PKI_DIR, CRL_SRC
INSTALL_DIR = settings.get("install_dir", DEFAULT_INSTALL_DIR)
SERVICE_MANAGER = settings.get("service_manager", DEFAULT_SERVICE_MANAGER)
default_unit = DEFAULT_SYSTEMD_UNIT if SERVICE_MANAGER == "systemd" else DEFAULT_SERVICE_UNIT
SERVICE_UNIT = settings.get("service_unit", default_unit)
TARGET = settings.get("config_path", DEFAULT_CONFIG_PATH)
STAGED = settings.get("staged_path", DEFAULT_STAGED_PATH)
PKI_DIR = INSTALL_DIR + "/APP_PROFILER/easy-rsa/pki"
CRL_SRC = PKI_DIR + "/crl.pem"
def load_config(path=None, owner_uid=0):
"""Read helper.conf (if it exists) and apply it. The file must be a regular file (no symlink)
owned by owner_uid (root) and not writable by group/other, otherwise Reject."""
path = path or CONF_PATH
try:
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
except FileNotFoundError:
apply_settings({})
return {}
except OSError as e:
raise Reject("helper.conf cannot be opened: " + e.strerror)
try:
st = os.fstat(fd)
need(stat.S_ISREG(st.st_mode), "helper.conf must be a regular file")
need(st.st_uid == owner_uid and not st.st_mode & 0o022, "helper.conf must be owned by root and not group/other-writable")
need(st.st_size <= 4096, "helper.conf too large")
data = os.read(fd, 4097)
finally:
os.close(fd)
settings = parse_config(data.decode("ascii", "replace"))
apply_settings(settings)
return settings
def is_int(x, lo, hi):
return re.fullmatch(r"\d{1,6}", x) is not None and lo <= int(x) <= hi
def valid_route(r):
parts = r.split()
try:
if len(parts) == 1:
ipaddress.IPv4Network(parts[0], strict=False)
elif len(parts) == 2:
ipaddress.IPv4Address(parts[0])
ipaddress.IPv4Network("0.0.0.0/" + parts[1])
else:
return False
return True
except ValueError:
return False
def check_script(path):
need(re.fullmatch(r"/etc/openvpn/scripts/[A-Za-z0-9_.-]{1,64}", path), "script path not allowed")
real = os.path.realpath(path)
need(os.path.dirname(real) == SCRIPTS_DIR and os.path.isfile(real), "script must be a file in " + SCRIPTS_DIR)
st, dst = os.stat(real), os.stat(SCRIPTS_DIR)
need(st.st_uid == 0 and not st.st_mode & 0o022, "script must be root-owned and not group/other-writable")
need(dst.st_uid == 0 and not dst.st_mode & 0o022, SCRIPTS_DIR + " must be root-owned and not writable")
def check_line(tokens):
d, a = tokens[0], tokens[1:]
if d == "dev":
need(a == ["tun"], "dev must be tun")
elif d == "proto":
need(len(a) == 1 and a[0] in ("udp", "tcp", "udp4", "tcp4", "udp6", "tcp6"), "bad proto")
elif d in ("tls-server", "client-to-client", "duplicate-cn", "persist-key", "persist-tun"):
need(not a, d + " takes no arguments")
elif d == "explicit-exit-notify":
need(len(a) == 1 and is_int(a[0], 1, 10), "bad explicit-exit-notify")
elif d in ("port", "management-port"):
need(len(a) == 1 and is_int(a[0], 1, 65535), "bad port")
elif d in ("ca", "cert", "key", "dh"):
need(len(a) == 1 and under(a[0], PKI_DIR), d + " must be a file inside the PKI directory")
elif d == "crl-verify":
need(a == [CRL_PUBLISHED], "crl-verify must be " + CRL_PUBLISHED)
elif d == "tls-auth":
need(len(a) == 2 and under(a[0], PKI_DIR) and a[1] in ("0", "1"), "bad tls-auth")
elif d == "tun-mtu":
need(len(a) == 1 and is_int(a[0], 576, 9000), "bad tun-mtu")
elif d == "mssfix":
need(len(a) == 1 and is_int(a[0], 536, 1500), "bad mssfix")
elif d == "topology":
need(a == ["subnet"], "topology must be subnet")
elif d == "server":
need(len(a) == 2, "bad server")
net = ipaddress.IPv4Network(f"{a[0]}/{a[1]}", strict=True)
need(8 <= net.prefixlen <= 30, "bad server prefix")
elif d == "ifconfig-pool-persist":
need(a == ["/etc/openvpn/ipp.txt"], "ifconfig-pool-persist path not allowed")
elif d in ("log", "log-append"):
need(a == ["/var/log/openvpn/openvpn.log"], d + " path not allowed")
elif d == "verb":
need(len(a) == 1 and is_int(a[0], 0, 9), "bad verb")
elif d == "status":
need(len(a) == 2 and a[0] == STATUS_LOG and is_int(a[1], 1, 3600), "bad status")
elif d == "status-version":
need(a in (["1"], ["2"], ["3"]), "bad status-version")
elif d == "push":
need(len(a) == 1, "push takes one quoted argument")
p = a[0]
if p == "redirect-gateway def1 bypass-dhcp":
return
m = re.fullmatch(r"route (.+)", p)
if m:
need(valid_route(m.group(1)), "bad pushed route")
return
m = re.fullmatch(r"dhcp-option DNS (\S+)", p)
need(m is not None, "pushed option not allowed")
ipaddress.ip_address(m.group(1))
elif d == "user":
need(a == ["nobody"], "user must be nobody")
elif d == "group":
need(a == ["nogroup"], "group must be nogroup")
elif d in ("data-ciphers", "data-ciphers-fallback"):
need(len(a) == 1 and CIPHERS_RE.match(a[0]), "bad cipher list")
elif d == "auth":
need(len(a) == 1 and a[0] in ("SHA256", "SHA384", "SHA512"), "bad auth")
elif d == "keepalive":
need(len(a) == 2 and is_int(a[0], 1, 3600) and is_int(a[1], 1, 7200), "bad keepalive")
elif d == "script-security":
need(a == ["2"], "script-security must be 2")
elif d in ("client-connect", "client-disconnect"):
need(len(a) == 1, d + " takes one argument")
check_script(a[0])
elif d == "management":
need(len(a) == 2 and ipaddress.ip_address(a[0]).is_loopback and is_int(a[1], 1, 65535), "management must be loopback")
else:
raise Reject("directive not allowed: " + d)
def validate(text):
seen = set()
for n, raw in enumerate(text.splitlines(), 1):
line = raw.strip()
if not line or line.startswith("#") or line.startswith(";"):
continue
need(all(32 <= ord(c) < 127 for c in line), f"line {n}: non-printable or non-ASCII character")
try:
tokens = shlex.split(line, comments=False)
except ValueError as e:
raise Reject(f"line {n}: {e}")
try:
check_line(tokens)
except Reject as e:
raise Reject(f"line {n}: {e}")
except ValueError as e:
raise Reject(f"line {n}: invalid value ({e})")
seen.add(tokens[0])
for req in ("user", "group", "server", "ca", "cert", "key"):
need(req in seen, f"required directive missing: {req}")
def install_config():
uid = pwd.getpwnam(SERVICE_USER).pw_uid
fd = os.open(STAGED, os.O_RDONLY | os.O_NOFOLLOW)
try:
st = os.fstat(fd)
need(st.st_uid == uid and stat.S_ISREG(st.st_mode), "staged config must be a regular file owned by " + SERVICE_USER)
need(st.st_size <= MAX_SIZE, "staged config too large")
data = os.read(fd, MAX_SIZE + 1)
finally:
os.close(fd)
text = data.decode("ascii") # one read: validate exactly what gets installed
validate(text)
tmp = TARGET + ".tmp"
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
with os.fdopen(fd, "w") as f:
f.write(text)
os.chmod(tmp, 0o644)
os.replace(tmp, TARGET)
# OpenRC's openvpn service reads /etc/openvpn/openvpn.conf; systemd units use their own config path
if SERVICE_MANAGER == "openrc" and os.path.dirname(TARGET) == "/etc/openvpn" and not os.path.lexists("/etc/openvpn/openvpn.conf"):
os.symlink(os.path.basename(TARGET), "/etc/openvpn/openvpn.conf")
def prepare_status_log():
"""Let the unprivileged monitoring gatherer read the status log."""
import grp
gid = grp.getgrnam(SERVICE_USER).gr_gid
if not os.path.exists(STATUS_LOG):
open(STATUS_LOG, "a").close()
os.chown(STATUS_LOG, 0, gid)
os.chmod(STATUS_LOG, 0o640)
def publish_crl():
"""Copy the CRL generated by easy-rsa to a root-owned, world-readable path.
OpenVPN reads the CRL as the unprivileged user 'nobody', who cannot enter the 0700 pki/ directory."""
uid = pwd.getpwnam(SERVICE_USER).pw_uid
need(under(CRL_SRC, PKI_DIR), "CRL source outside PKI directory")
fd = os.open(CRL_SRC, os.O_RDONLY | os.O_NOFOLLOW)
try:
st = os.fstat(fd)
need(stat.S_ISREG(st.st_mode) and st.st_uid in (0, uid), "CRL must be a regular file owned by " + SERVICE_USER)
need(0 < st.st_size <= CRL_MAX, "CRL size out of range")
data = os.read(fd, CRL_MAX + 1)
finally:
os.close(fd)
need(data.startswith(b"-----BEGIN X509 CRL-----") and data.rstrip().endswith(b"-----END X509 CRL-----"), "not a PEM CRL")
r = subprocess.run(["/usr/bin/openssl", "crl", "-noout", "-inform", "PEM"], input=data, capture_output=True, timeout=20)
need(r.returncode == 0, "openssl rejects the CRL")
tmp = CRL_PUBLISHED + ".tmp"
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
with os.fdopen(fd, "wb") as f:
f.write(data)
os.chmod(tmp, 0o644)
os.replace(tmp, CRL_PUBLISHED)
def service_command(action):
"""Fixed argv of the service manager (no shell, values come from the validated helper.conf)."""
if SERVICE_MANAGER == "systemd":
systemctl = next((p for p in SYSTEMCTL_CANDIDATES if os.path.exists(p)), SYSTEMCTL_CANDIDATES[0])
return [systemctl, action, SERVICE_UNIT]
return [RC_SERVICE, SERVICE_UNIT, action]
def service(action):
need(action in ("start", "stop", "restart", "status"), "invalid action")
if action in ("start", "restart"):
need(os.path.isfile(TARGET), "server.conf is not installed")
prepare_status_log()
if os.path.isfile(CRL_SRC):
try:
publish_crl()
except Exception:
pass # a broken CRL must not stop the VPN; crl-verify will then keep the previously published file
r = subprocess.run(service_command(action), capture_output=True, text=True, timeout=60)
return r.returncode, (r.stdout + r.stderr).strip()[-500:]
def main(argv):
try:
load_config()
if argv == ["install-config"]:
install_config()
print(json.dumps({"status": "ok"}))
return 0
if argv == ["publish-crl"]:
publish_crl()
print(json.dumps({"status": "ok"}))
return 0
if len(argv) == 2 and argv[0] == "service":
rc, out = service(argv[1])
print(json.dumps({"status": "ok" if rc == 0 else "error", "output": out}))
return 0 if rc == 0 else 2
raise Reject("usage: install-config | publish-crl | service start|stop|restart|status")
except Reject as e:
print(json.dumps({"status": "rejected", "error": str(e)}))
return 3
except Exception as e: # never leak a traceback with paths to the caller
print(json.dumps({"status": "error", "error": type(e).__name__ + ": " + str(e)[:200]}))
return 4
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))