Your IP : 216.73.216.228


Current Path : /opt/cloudlinux/venv/lib64/python3.11/site-packages/clcagefslib/webisolation/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/clcagefslib/webisolation/triggers.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

"""
Triggers for website isolation.
"""

import os
import subprocess
import logging


# F-17 (CLOS-5955) DiD: this module is reached from the root-side
# CAGEFSCTL_USER dispatch, where proxyexec sets HOME to the caller's
# home directory before the root command runs. Any child process that
# inherits that HOME can be attacked through Python's user-site loader
# (.pth files, sitecustomize) — the SSA regen script has a Python
# shebang, so a bare subprocess.run() with the inherited environment
# turns files under the tenant's home into root code execution before
# the helper body runs. Build the child environment from scratch,
# forwarding only a fixed set of locale/PATH vars and pinning
# PYTHONNOUSERSITE=1 + PYTHONDONTWRITEBYTECODE=1. HOME is intentionally
# NOT forwarded and NOT overridden here — the child starts without a
# HOME env var and lets libc/Python derive it from getpwuid(euid) if
# it needs one (root's own passwd entry, not the tenant's home).
_SAFE_ENV_KEYS = ("PATH", "LANG", "LC_ALL", "LC_CTYPE", "LC_MESSAGES",
                  "LC_COLLATE", "LC_NUMERIC", "LC_TIME")


def _sanitized_env() -> dict:
    env = {k: os.environ[k] for k in _SAFE_ENV_KEYS if k in os.environ}
    env.setdefault("PATH", "/usr/sbin:/usr/bin:/sbin:/bin")
    env["PYTHONNOUSERSITE"] = "1"
    env["PYTHONDONTWRITEBYTECODE"] = "1"
    return env


def trigger_xray_ini_regeneration(user: str, domain: str):
    """
    Call xray regeneration script if it exists.
    This ensures xray.ini files are created in per-website directories
    when website isolation is enabled.
    """
    xray_regen_script = "/usr/share/alt-php-xray/cloudlinux-xray-regenerate-ini"
    if not os.path.isfile(xray_regen_script):
        # ok when xray is not installed
        logging.debug("xray regeneration script not found: %s", xray_regen_script)
        return
    try:
        subprocess.run(
            [xray_regen_script, "--user", user, "--domain", domain],
            capture_output=True,
            timeout=30,
            check=True,
            env=_sanitized_env(),
        )
    except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
        logging.warning(f"Failed to regenerate xray ini for {user}: {e.stdout}")


def trigger_ssa_ini_regeneration(user: str):
    """
    Call SSA regeneration script if it exists.
    This ensures clos_ssa.ini files are created in per-website directories
    when website isolation is enabled.
    """
    ssa_regen_script = "/usr/share/clos_ssa/regenerate_clos_ssa_ini.py"
    if not os.path.isfile(ssa_regen_script):
        # ok when ssa is not installed
        logging.debug("ssa regeneration script not found: %s", ssa_regen_script)
        return
    try:
        subprocess.run(
            [ssa_regen_script, f"--user={user}"],
            capture_output=True,
            timeout=30,
            check=True,
            env=_sanitized_env(),
        )
    except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
        logging.warning(f"Failed to regenerate SSA ini for {user}: {e.stdout}")