晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/opt/cloudlinux/venv/lib64/python3.11/site-packages/websiteisolation/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/websiteisolation/id_registry.py
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2026 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

"""
Thin Python wrapper around the _lvdmap C extension (backed by liblve).

Public interface is unchanged — callers (lveapi.py, lvectllib.py) continue
to import and use these functions exactly as before.
"""
import pwd
import logging
import os

from .exceptions import LvdError

log = logging.getLogger(__name__)

LVD_IDS_DIR = '/etc/container/lvd_ids'

try:
    import _lvdmap
except ImportError:
    _lvdmap = None


def _require_lvdmap():
    if _lvdmap is None:
        raise LvdError("_lvdmap C extension is not installed")


def registry_path_by_username(username):
    """Return the registry file path for *username*, or None if the
    user does not exist or has no registry file."""
    try:
        uid = pwd.getpwnam(username).pw_uid
    except KeyError:
        return None
    path = os.path.join(LVD_IDS_DIR, str(uid))
    if not os.path.exists(path):
        return None
    return path


def assign_domain_id(uid, docroot):
    """Assign a domain ID for a docroot. Returns existing ID if already
    assigned, otherwise allocates the next sequential ID."""
    if os.geteuid() != 0:
        raise LvdError("domain ID assignment requires root")
    _require_lvdmap()
    domain_id = _lvdmap.assign(uid, docroot)
    log.info("assigned domain_id %d to docroot '%s' for uid %d",
             domain_id, docroot, uid)
    return domain_id


def get_domain_id(uid, docroot):
    """Look up domain ID by docroot. Returns None if not assigned."""
    if _lvdmap is None:
        return None
    r = _lvdmap.lookup(uid, docroot)
    return r if r != 0 else None


def get_all_entries(uid):
    """Return dict of docroot -> domain_id for a user."""
    if _lvdmap is None:
        return {}
    return _lvdmap.get_all_entries(uid)


def get_all_domain_ids():
    """Return the set of every assigned domain LVE ID across all users."""
    if _lvdmap is None:
        return set()
    return _lvdmap.get_all_domain_ids()


def reassign_docroot(uid, old_docroot, new_docroot):
    """Move a domain mapping from *old_docroot* to *new_docroot*.

    Uses remove + assign via _lvdmap; the domain_id will be newly
    allocated (the C API does not support specifying a target ID).
    """
    if os.geteuid() != 0:
        raise LvdError("domain ID reassignment requires root")
    _require_lvdmap()

    old_id = _lvdmap.remove(uid, old_docroot)
    if old_id is None:
        log.warning("reassign_docroot: old_docroot '%s' not in registry "
                    "for uid %d — nothing to reassign", old_docroot, uid)
        return None

    new_id = _lvdmap.assign(uid, new_docroot)
    log.info("reassigned docroot from '%s' (id=%d) to '%s' (id=%d) "
             "for uid %d", old_docroot, old_id, new_docroot, new_id, uid)
    return new_id


def remove_domain_id(uid, docroot):
    """Remove a docroot from the registry. Returns the old ID or None."""
    if os.geteuid() != 0:
        raise LvdError("domain ID removal requires root")
    _require_lvdmap()
    old_id = _lvdmap.remove(uid, docroot)
    if old_id is not None:
        log.info("removed domain_id %d for docroot '%s' uid %d",
                 old_id, docroot, uid)
    return old_id


def remove_all_entries(uid):
    """Remove all domain IDs for a user. Returns list of (docroot, id)."""
    if os.geteuid() != 0:
        raise LvdError("domain ID removal requires root")
    _require_lvdmap()
    removed = _lvdmap.remove_all(uid)
    if removed:
        log.info("removed all domain IDs for uid %d (%d entries)",
                 uid, len(removed))
    return removed


def create_empty_registry(uid):
    """Mark *uid* as domain-isolated by touching its per-user file.

    The file acts as a marker so that ``find_all_lve_ids_with_config()``
    (which lists ``LVD_IDS_DIR``) can detect the user before any domains
    are assigned.  The C library's ``lvd_map_assign()`` will later
    atomically replace this empty file with a proper hash-table via
    rename(2), so the marker never interferes with real data.
    """
    if os.geteuid() != 0:
        raise LvdError("domain ID assignment requires root")
    os.makedirs(LVD_IDS_DIR, mode=0o711, exist_ok=True)
    try:
        st = os.stat(LVD_IDS_DIR)
        if st.st_mode & 0o777 != 0o711:
            os.chmod(LVD_IDS_DIR, 0o711)
    except OSError:
        pass
    marker = os.path.join(LVD_IDS_DIR, str(uid))
    if not os.path.exists(marker):
        fd = os.open(marker, os.O_CREAT | os.O_WRONLY, 0o600)
        os.close(fd)