CI / test (push) Canceled after 0s
- Add compositor/shell/backlight.mbt for backlight control in the shell - Update wit/capabilities.wit and wit/host/plasmoids.wit, then regenerate the component and interface ffi.mbt bindings (mostly export reordering) - Add world/launcher-widget/README.md
222 lines
9.9 KiB
Python
222 lines
9.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Install (or remove) face unlock. `just install` runs this (through session/install.py, as
|
|
root) along with the rest of Chandra; `just install --pam` adds --pam, `just uninstall` --remove.
|
|
By hand, 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` 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 (`just install` builds it as you into
|
|
build/; else it's built here with cc and the libpam headers)
|
|
/etc/chandra/face.conf settings (kept when it exists)
|
|
/etc/systemd/system/polkit-agent-helper@.service.d/50-chandra-face.conf
|
|
lets polkit's sandboxed PAM helper open cameras (only
|
|
where polkit has that unit)
|
|
/var/lib/chandra-face/ enrolled faces
|
|
/usr/local/share/chandra/face/*.onnx the YuNet and SFace models (downloaded, checksummed)
|
|
|
|
PAM files are only changed with --pam (or later with `chandra-face pam enable SERVICE`); the line
|
|
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, pwd, shutil, subprocess, sys
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
helper = "/usr/local/lib/chandra/chandra-face"
|
|
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")
|
|
polkit_unit = "polkit-agent-helper@.service"
|
|
polkit_dropin = "/etc/systemd/system/%s.d/50-chandra-face.conf" % polkit_unit
|
|
|
|
|
|
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 Python environment the helper and PAM module run; its python3."""
|
|
sys_py = shutil.which("python3") or "/usr/bin/python3"
|
|
use_sys = "--system-python" in sys.argv
|
|
pixi = None if use_sys else find_pixi()
|
|
if not pixi:
|
|
if os.access(sys_py, os.X_OK) and subprocess.run(
|
|
[sys_py, "-I", "-c", "import cv2, numpy"],
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
).returncode == 0:
|
|
print("using system Python", sys_py, "(OpenCV, numpy)")
|
|
return sys_py
|
|
if use_sys:
|
|
sys.exit("--system-python asked, but %s cannot import cv2, numpy" % sys_py)
|
|
sys.exit("pixi not found: install it (https://pixi.sh) or pass --pixi PATH, "
|
|
"or install system python-opencv + python-numpy and pass --system-python")
|
|
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 daemon_reload():
|
|
"""Make systemd read the unit files again; the next password dialog gets a new helper."""
|
|
if os.path.isdir("/run/systemd/system"):
|
|
subprocess.check_call(["systemctl", "daemon-reload"])
|
|
|
|
|
|
def allow_polkit_camera():
|
|
"""polkit 126+ runs PAM in a unit whose /dev has no cameras (PrivateDevices=yes): without
|
|
this drop-in the password dialog never finds one, though sudo and enrolling do."""
|
|
if not any(os.path.exists(os.path.join(d, polkit_unit)) for d in
|
|
("/etc/systemd/system", "/usr/local/lib/systemd/system", "/usr/lib/systemd/system",
|
|
"/lib/systemd/system")):
|
|
return # an older polkit (a setuid helper, no sandbox) or no systemd
|
|
os.makedirs(os.path.dirname(polkit_dropin), 0o755, exist_ok=True)
|
|
shutil.copyfile(os.path.join(here, "polkit-agent-helper.conf"), polkit_dropin)
|
|
os.chmod(polkit_dropin, 0o644)
|
|
daemon_reload()
|
|
print("installed", polkit_dropin)
|
|
|
|
|
|
def pam_dir():
|
|
"""Where this system's PAM modules live (next to pam_unix.so)."""
|
|
cands = ["/usr/lib/security", "/lib/security", "/usr/lib64/security", "/lib64/security"]
|
|
cands += glob.glob("/usr/lib/*-linux-gnu*/security") + glob.glob("/lib/*-linux-gnu*/security")
|
|
for d in cands:
|
|
if os.path.exists(os.path.join(d, "pam_unix.so")):
|
|
return os.path.realpath(d)
|
|
sys.exit("can't find the PAM module directory (no pam_unix.so)")
|
|
|
|
|
|
if os.geteuid() != 0:
|
|
sys.exit("run as root: run0 python3 face/install.py (or sudo), or `just install`")
|
|
|
|
mod = os.path.join(pam_dir(), "pam_chandra_face.so")
|
|
|
|
if "--remove" in sys.argv:
|
|
# PAM first, so no service points at a module that is about to go.
|
|
if os.path.exists(helper):
|
|
subprocess.call([sys.executable, "-I", helper, "--no-elevate", "pam", "disable"]
|
|
+ sorted(os.listdir("/etc/pam.d")))
|
|
if os.path.exists(polkit_dropin):
|
|
os.remove(polkit_dropin)
|
|
print("removed", polkit_dropin)
|
|
try:
|
|
os.rmdir(os.path.dirname(polkit_dropin)) # if nothing else is in it
|
|
except OSError:
|
|
pass
|
|
daemon_reload()
|
|
for p in (mod, link, helper):
|
|
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)
|
|
try:
|
|
os.rmdir(os.path.dirname(helper)) # /usr/local/lib/chandra, if nothing else is in it
|
|
except OSError:
|
|
pass
|
|
if "--purge" in sys.argv:
|
|
for p in (store,):
|
|
if os.path.isdir(p):
|
|
shutil.rmtree(p)
|
|
print("removed", p)
|
|
if os.path.exists(conf):
|
|
os.remove(conf)
|
|
print("removed", conf)
|
|
try:
|
|
os.rmdir(os.path.dirname(conf)) # /etc/chandra, if nothing else is in it
|
|
except OSError:
|
|
pass
|
|
sys.exit(0)
|
|
|
|
os.umask(0o022)
|
|
os.makedirs(os.path.dirname(helper), exist_ok=True)
|
|
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)
|
|
if os.path.lexists(link):
|
|
os.remove(link)
|
|
os.symlink(helper, link)
|
|
print("linked", link, "->", helper)
|
|
|
|
src = os.path.join(here, "pam_chandra_face.c")
|
|
built = os.path.join(os.path.dirname(here), "build", "pam_chandra_face.so") # from `just install` (same helper and python paths)
|
|
if os.path.exists(built) and os.path.getmtime(built) >= os.path.getmtime(src):
|
|
shutil.copyfile(built, mod + ".new")
|
|
else:
|
|
cc = shutil.which("cc") or shutil.which("gcc") or shutil.which("clang")
|
|
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, src, "-lpam"])
|
|
os.chmod(mod + ".new", 0o644)
|
|
os.replace(mod + ".new", mod)
|
|
print("installed", mod)
|
|
allow_polkit_camera()
|
|
|
|
if not os.path.exists(conf):
|
|
os.makedirs(os.path.dirname(conf), exist_ok=True)
|
|
shutil.copyfile(os.path.join(here, "face.conf"), conf)
|
|
os.chmod(conf, 0o644)
|
|
print("installed", conf)
|
|
os.makedirs(store, 0o755, exist_ok=True)
|
|
os.chmod(store, 0o755)
|
|
|
|
if "--no-models" not in sys.argv:
|
|
subprocess.check_call([py, "-I", helper, "--no-elevate", "fetch-models"])
|
|
if "--pam" in sys.argv:
|
|
subprocess.check_call([py, "-I", helper, "--no-elevate", "pam", "enable"])
|
|
# Say now when it can't work yet (no camera at the configured path, say), and what is there.
|
|
st = subprocess.run([py, "-I", helper, "--no-elevate", "status", "--machine"], capture_output=True, text=True)
|
|
for line in st.stdout.splitlines():
|
|
if line.startswith("problem "):
|
|
print("face unlock: NOT READY: " + line[len("problem "):], file=sys.stderr)
|
|
print("face unlock: add a face in Settings > Session > Face unlock (after logging in to the new "
|
|
"session), or now with `chandra-face enroll`, then `chandra-face pam enable`")
|