Face unlock: Python, OpenCV and numpy from a root-owned pixi environment
CI / test (push) Successful in 2m41s
CI / test (push) Successful in 2m41s
- face/pixi.toml + pixi.lock (python 3.12, numpy, opencv-python-headless). `just install-face` passes the pixi on your PATH; install.py runs `pixi install --frozen` as root into /usr/local/lib/chandra/face-env (not the checkout's .pixi: the PAM module runs this Python as root, so it must not be user-writable), writes the helper's shebang for it and builds the PAM module with it as the default python. No python3-opencv needed; uninstall removes the env. - The PAM module now also refuses a Python that isn't root's; group-writable is accepted only for group root (conda/pixi files are 0775 root:root), in the module and chandra-face's checks. - Elevation re-runs on the same interpreter (sys.executable). "OpenCV is missing: run just install-face" instead of naming the distro package. Dropped a stray __pycache__. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
This commit is contained in:
10 files changed
+1105
-26
No files matched your search
+57
-9
@@ -5,10 +5,13 @@ scripts/as-root):
|
||||
run0 python3 face/install.py # install, fetch the face models
|
||||
run0 python3 face/install.py --pam # ... and use it for polkit-1 and sudo
|
||||
run0 python3 face/install.py --no-models
|
||||
run0 python3 face/install.py --pixi PATH # the pixi binary (`just install-face` passes it)
|
||||
run0 python3 face/install.py --remove # take it out of every PAM service, uninstall
|
||||
run0 python3 face/install.py --remove --purge # ... and delete the enrolled faces
|
||||
|
||||
Installs:
|
||||
/usr/local/lib/chandra/face-env/ Python + OpenCV + numpy, a pixi environment made from
|
||||
face/pixi.toml + pixi.lock (root's; nothing from the distro)
|
||||
/usr/local/lib/chandra/chandra-face the helper (enroll, compare, ...), root's
|
||||
/usr/local/bin/chandra-face -> the helper
|
||||
<PAM module dir>/pam_chandra_face.so the PAM module (built here with cc; needs libpam headers)
|
||||
@@ -20,7 +23,7 @@ PAM files are only changed with --pam (or later with `chandra-face pam enable SE
|
||||
is `-auth sufficient pam_chandra_face.so`, before the service's other auth lines, so the password
|
||||
still works whenever the face doesn't.
|
||||
"""
|
||||
import glob, os, shutil, subprocess, sys
|
||||
import glob, os, pwd, shutil, subprocess, sys
|
||||
|
||||
here = os.path.dirname(os.path.abspath(__file__))
|
||||
helper = "/usr/local/lib/chandra/chandra-face"
|
||||
@@ -28,6 +31,47 @@ link = "/usr/local/bin/chandra-face"
|
||||
conf = "/etc/chandra/face.conf"
|
||||
store = "/var/lib/chandra-face"
|
||||
models = "/usr/local/share/chandra/face"
|
||||
env_dir = "/usr/local/lib/chandra/face-env"
|
||||
env_python = os.path.join(env_dir, ".pixi/envs/default/bin/python3")
|
||||
|
||||
|
||||
def find_pixi():
|
||||
"""pixi: --pixi PATH, $PIXI, PATH, then root's or the invoking user's ~/.pixi/bin."""
|
||||
if "--pixi" in sys.argv:
|
||||
i = sys.argv.index("--pixi")
|
||||
if i + 1 < len(sys.argv) and sys.argv[i + 1]:
|
||||
return sys.argv[i + 1]
|
||||
cands = [os.environ.get("PIXI", ""), shutil.which("pixi") or "", "/root/.pixi/bin/pixi"]
|
||||
for var in ("SUDO_USER", "PKEXEC_UID", "RUN0_USER"):
|
||||
v = os.environ.get(var, "")
|
||||
try:
|
||||
home = pwd.getpwuid(int(v)).pw_dir if v.isdigit() else pwd.getpwnam(v).pw_dir if v else ""
|
||||
except KeyError:
|
||||
home = ""
|
||||
if home:
|
||||
cands.append(os.path.join(home, ".pixi/bin/pixi"))
|
||||
for c in cands:
|
||||
if c and os.access(c, os.X_OK):
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
def make_env():
|
||||
"""The root-owned pixi environment the helper and the PAM module run; its python3."""
|
||||
pixi = find_pixi()
|
||||
if not pixi:
|
||||
sys.exit("pixi not found: install it (https://pixi.sh) or pass --pixi PATH; "
|
||||
"`just install-face` passes the one on your PATH")
|
||||
os.makedirs(env_dir, 0o755, exist_ok=True)
|
||||
for f in ("pixi.toml", "pixi.lock"):
|
||||
shutil.copyfile(os.path.join(here, f), os.path.join(env_dir, f))
|
||||
os.chmod(os.path.join(env_dir, f), 0o644)
|
||||
env = dict(os.environ, PIXI_HOME="/root/.pixi")
|
||||
subprocess.check_call([pixi, "install", "--frozen", "--manifest-path",
|
||||
os.path.join(env_dir, "pixi.toml")], env=env)
|
||||
subprocess.check_call([env_python, "-I", "-c", "import cv2, numpy"])
|
||||
print("installed", env_dir, "(Python, OpenCV, numpy)")
|
||||
return env_python
|
||||
|
||||
|
||||
def pam_dir():
|
||||
@@ -54,6 +98,9 @@ if "--remove" in sys.argv:
|
||||
if os.path.lexists(p):
|
||||
os.remove(p)
|
||||
print("removed", p)
|
||||
if os.path.isdir(env_dir):
|
||||
shutil.rmtree(env_dir)
|
||||
print("removed", env_dir)
|
||||
if os.path.isdir(models):
|
||||
shutil.rmtree(models)
|
||||
print("removed", models)
|
||||
@@ -69,7 +116,12 @@ if "--remove" in sys.argv:
|
||||
|
||||
os.umask(0o022)
|
||||
os.makedirs(os.path.dirname(helper), exist_ok=True)
|
||||
shutil.copyfile(os.path.join(here, "chandra-face"), helper + ".new")
|
||||
py = make_env()
|
||||
# The helper runs on that Python (its shebang), so `chandra-face ...` from a shell does too.
|
||||
with open(os.path.join(here, "chandra-face")) as fh:
|
||||
src = fh.read().split("\n", 1)[1]
|
||||
with open(helper + ".new", "w") as fh:
|
||||
fh.write("#!%s -I\n" % py + src)
|
||||
os.chmod(helper + ".new", 0o755)
|
||||
os.replace(helper + ".new", helper)
|
||||
print("installed", helper)
|
||||
@@ -83,6 +135,7 @@ if not cc:
|
||||
sys.exit("no C compiler: install gcc (and the libpam headers: libpam0g-dev / pam-devel)")
|
||||
subprocess.check_call([cc, "-O2", "-Wall", "-shared", "-fPIC", "-o", mod + ".new",
|
||||
"-DCHANDRA_FACE_HELPER=\"%s\"" % helper,
|
||||
"-DCHANDRA_FACE_PYTHON=\"%s\"" % py,
|
||||
os.path.join(here, "pam_chandra_face.c"), "-lpam"])
|
||||
os.chmod(mod + ".new", 0o644)
|
||||
os.replace(mod + ".new", mod)
|
||||
@@ -97,14 +150,9 @@ os.makedirs(store, 0o755, exist_ok=True)
|
||||
os.chmod(store, 0o755)
|
||||
|
||||
if "--no-models" not in sys.argv:
|
||||
subprocess.check_call([sys.executable, "-I", helper, "--no-elevate", "fetch-models"])
|
||||
subprocess.check_call([py, "-I", helper, "--no-elevate", "fetch-models"])
|
||||
if "--pam" in sys.argv:
|
||||
subprocess.check_call([sys.executable, "-I", helper, "--no-elevate", "pam", "enable"])
|
||||
try:
|
||||
import cv2 # noqa
|
||||
except ImportError:
|
||||
print("note: install OpenCV for Python (python3-opencv / python-opencv / opencv-python) "
|
||||
"before enrolling a face")
|
||||
subprocess.check_call([py, "-I", helper, "--no-elevate", "pam", "enable"])
|
||||
print("""
|
||||
Settings > Session > Face unlock comes with the session itself (the compositor, its host and the
|
||||
Settings module), not with this install: if Settings doesn't show it yet, run `just install`
|
||||
|
||||
Reference in new issue
Block a user