From 15d65183dc4c8a7fce665d0db0dd856016588d70 Mon Sep 17 00:00:00 2001 From: Mistercoolertyper <84140706+Mistercoolertyper@users.noreply.github.com> Date: Wed, 4 Nov 2026 10:16:23 +0100 Subject: [PATCH] Debian: passwordless updates, offline updates, and install ISO builder Ports the update behaviour of the Fedora clients to Debian, and adds the Debian equivalent of putting ks.cfg on the installation medium. Updates ------- Debian's shipped polkit policy only lets a sudo-group user trigger upgrade-system and trigger-offline-update without a password. The actions pkcon refresh and pkcon update actually use (system-sources-refresh and system-update) are auth_admin, so a desktop user was prompted - a behavioural difference from Fedora. update_local_repo.py now installs /etc/polkit-1/rules.d/60-vdi-no-password-updates.rules granting exactly those two plus package-install to group sudo. package-remove, package-reinstall and package-install-untrusted stay auth_admin. The offline update itself ports unchanged: Debian 13 PackageKit 1.3.1 supports offline-get-prepared and offline-trigger, and packagekit-offline-update.service is wired into system-update.target. Scheduling moves from an @reboot cron line to vdi-offline-update.timer (6h, Persistent), with /root/auto_update.sh delegating to the same script so there is one implementation instead of two. Both are installed by update_local_repo.py, not only by the preseed, so already-provisioned machines pick them up on their next 10-minute cron run. install ISO ----------- debian/make-iso.sh bakes preseed.cfg into a Debian netinst ISO and adds auto=true priority=critical preseed/file=/cdrom/preseed.cfg to every boot menu entry, then verifies its own output. Measured, not assumed --------------------- * subject.local == true never matches in this environment: pkcheck returns 2 with the clause and 0 without it, same sudo-group user. It was in the first version of the rule and is removed; the reasoning is in the source. * pkcon exits 5 for "no packages to update", which is the normal answer on a patched box and was being logged as a download failure. Fixed. * offline-get-prepared exits 4 when nothing is prepared, so the Fedora "if ... offline-trigger" guard is load-bearing - triggering anyway can leave a dangling /system-update symlink that breaks the next boot. * Extracting a netinst and rebuilding with "xorriso -as mkisofs" grows it 792MB -> 988MB because Debian shares identical file extents; replaying the original session with -map keeps dedup and the boot record. * A hardcoded menu list misses isolinux/gtk.cfg, where the BIOS *default* entry lives, so the machine still prompted. Menus are discovered now. Tests: 90 passed, 0 failed, 2 skipped. T17 asserts the polkit decision in both directions (authorised with the rule, prompted without it), because a check that cannot fail proves nothing. --- README.md | 161 ++++++++++ debian/make-iso.sh | 204 ++++++++++++ debian/preseed.cfg | 25 +- debian/test/README.md | 116 ++++++- debian/test/run-tests.sh | 648 +++++++++++++++++++++++++++++++++++++++ update_local_repo.py | 183 +++++++++++ 6 files changed, 1335 insertions(+), 2 deletions(-) create mode 100644 debian/make-iso.sh diff --git a/README.md b/README.md index ada4dea..6780d00 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,167 @@ back to a no-op if it is missing, but without `apt-utils` or `python3` the cron job fails every 10 minutes with the reason only visible in `/var/log/update_local_repo.log`. +## Updates + +The Fedora clients update through PackageKit: `pkcon refresh`, `pkcon update`, and +an offline update that is downloaded in the background and applied at the next +boot. Debian does the same, with one addition (a polkit rule) and one rewrite (the +scheduler). + +### Updating without a password + +A user in the `sudo` group can refresh and install updates from the desktop +without being asked for a password, as on Fedora. This needs a polkit rule, +because Debian's shipped policy does not cover it: + +- `/usr/share/polkit-1/rules.d/org.freedesktop.packagekit.rules` grants passwordless + access only to `upgrade-system` and `trigger-offline-update`. +- `system-sources-refresh` (what `pkcon refresh` uses) and `system-update` (what + `pkcon update` uses) are `auth_admin`, so a desktop user *is* prompted. +- `/usr/share/polkit-1/rules.d/50-default.rules` makes `unix-group:sudo` the admin + identity, so the prompt is for the user's own password. + +`update_local_repo.py` therefore installs +`/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules`, allowing +`system-sources-refresh`, `system-update` and `package-install` for members of +group `sudo`. Deliberately **not** granted: `package-remove`, +`package-reinstall`, `package-install-untrusted` - a normal user still cannot +remove packages or install an untrusted one without a password. + +Measured with `pkcheck`: with the rule, `pkcheck --action-id +org.freedesktop.packagekit.system-sources-refresh` returns 0 (authorised); with the +rule removed it returns 2 (authentication required). The test harness asserts both +directions, because a check that cannot fail proves nothing. + +One thing that did *not* work and is worth knowing: the rule was first written +with `subject.local == true`, to limit it to seats physically in front of the +machine. That clause never matches in this environment - the rule silently did +nothing and users were still prompted - so it was removed and the reasoning is +recorded in the source. The consequence is that a `sudo`-group user on a remote +session can also update without a password, which is the same trust level that +group already has through `sudo`. + +Root never needs a password either, which is what makes the cron job and the +timer work. + +### The offline update + +Debian 13's PackageKit 1.3.1 supports the same offline-update mechanism the Fedora +clients use, so the approach ports directly: + +1. `pkcon refresh force` - refresh the package index. +2. `pkcon update --only-download` - download the updates without installing. +3. `pkcon offline-get-prepared` - confirm a payload is ready. +4. `pkcon offline-trigger` - arm the update. + +Step 4 creates `/system-update` as a symlink to +`/var/lib/PackageKit/prepared-update`, and systemd's +`packagekit-offline-update.service` (wired into `system-update.target`, shipped by +the `packagekit` package) applies it on the next boot. The desktop is never +sitting on a half-applied transaction. + +Step 3 is load-bearing, not ceremony: `offline-get-prepared` exits **4** when +nothing is prepared, and triggering anyway fails and can leave a dangling +`/system-update` symlink, which breaks the next boot. The `if` is what prevents +that. + +Two details that differ from the Fedora script: + +- `pkcon` exit code **5** means "no packages to update", which is the normal + answer on a patched machine. It is not a failure and is not logged as one. +- The download runs under `systemd-inhibit --what=sleep:idle --mode=block` so the + machine does not suspend mid-download. No `DISPLAY` is needed, which matters + because the timer runs outside any session. + +### Who runs it, and when + +| | Fedora | Debian | +|---|---|---| +| Citrix repo refresh | cron, 10 min | cron, 10 min | +| offline update | `@reboot` via `/root/auto_update.sh` | `vdi-offline-update.timer`, 6 h + boot, throttled | + +On Debian the offline update is a systemd timer (`vdi-offline-update.timer`, +`OnBootSec=15min`, `OnUnitActiveSec=6h`, `Persistent=true`, +`RandomizedDelaySec=15min`) running `/usr/local/sbin/vdi-offline-update`. systemd +owns the scheduling, output goes to the journal, and a failure is visible in +`systemctl status vdi-offline-update.service`. The script also keeps a 6-hour +throttle stamp at `/var/lib/vdi-offline-update.last` so a manual run and the timer +cannot pile up. + +`/root/auto_update.sh` is kept for parity with Fedora and simply execs +`/usr/local/sbin/vdi-offline-update`, so there is one implementation rather than +two copies of the `pkcon` sequence. + +**Note on scope:** these files are installed by `update_local_repo.py`, not only by +the preseed. That means machines already provisioned before this change pick up +the polkit rule and the timer on their next 10-minute cron run - the same +back-fill approach `configure_kde.py` already uses for the Plasma settings. The +preseed additionally enables the timer directly so a fresh install has offline +updates from the first boot. + +### Why not `unattended-upgrades` + +Debian's usual answer for automatic updates is `unattended-upgrades`, and it is +the wrong instrument here: it *installs* security updates silently while the user +is working, which is the thing the offline-update flow exists to avoid, and it +does not give a desktop user a no-password update button. It is left uninstalled. +`pkgsel/update-policy` is set to `none` in the preseed for the same reason. + +## Building an install ISO + +On Fedora, `ks.cfg` is handed to the installer on a second medium or over the +network. Debian's equivalent is to bake `preseed.cfg` into the installation ISO +and point the boot menu at it, so the stick is self-contained. + +``` +apt-get install xorriso +./debian/make-iso.sh debian-13.7.0-amd64-netinst.iso vdi-debian.iso +``` + +The script copies the preseed to `/preseed.cfg` on the image and adds this to +every kernel line in every boot menu: + +``` +auto=true priority=critical preseed/file=/cdrom/preseed.cfg +``` + +- `auto=true` is an alias for `auto-install/enable`; it delays the locale and + keyboard questions so the preseed can answer them. +- `priority=critical` is an alias for `debconf/priority`; nothing below critical + is asked. +- `preseed/file=/cdrom/preseed.cfg` reads the preseed from the ISO itself. The + `preseed-md5` checksum parameter is deliberately not used: it must match +exactly and breaks the moment the preseed is edited. + +The script verifies its own output instead of trusting `xorriso`'s exit status: +the preseed is byte-identical on the image, every kernel line carries the +parameters, both El Torito boot images (BIOS and UEFI) are present, and no file +from the source ISO is missing. It exits non-zero rather than writing an ISO that +would boot to an interactive prompt. + +Two things worth knowing if you modify it: + +- **Every menu file is patched, not a fixed list.** On a Debian netinst the BIOS + menu is `isolinux/isolinux.cfg`, which contains only `include menu.cfg` and + `default vesamenu.c32`; the entry that actually boots by default is in + `isolinux/gtk.cfg` (`label installgui`, `menu default`). Patching only the + obvious files leaves the default BIOS entry unpatched and the machine still + stops to prompt. The script discovers every `*.cfg` under `/isolinux` and + `/boot/grub` and patches the ones with kernel lines. +- **The image is not extracted and rebuilt.** `xorriso -as mkisofs` over an +extracted tree grows a netinst from 792 MB to 988 MB, because Debian's ISO + shares identical file extents between paths and extraction duplicates them all. + Instead the original session is copied and only the changed files are mapped + over it (`-indev SRC -outdev DST -boot_image any replay -map ...`), which keeps + the deduplication and the boot record intact. + +Verified against `debian-13.7.0-amd64-netinst.iso`: 20 of 20 BIOS kernel lines and +20 of 20 UEFI kernel lines patched, all 1010 source files present, preseed +byte-identical, both boot images intact, output 792 MB. + +The inventory number is still asked for on the console. The preseed cannot guess +it, and it has to be read from a human. + ## configure_kde.py Writes KWin's `[Libinput][Defaults][Touchpad]` group (so it applies to every diff --git a/debian/make-iso.sh b/debian/make-iso.sh new file mode 100644 index 0000000..e3317dd --- /dev/null +++ b/debian/make-iso.sh @@ -0,0 +1,204 @@ +#!/bin/bash +# Build a Debian installation ISO that runs this preseed unattended. +# +# This is the Debian equivalent of putting ks.cfg on the Fedora ISO: the preseed +# is baked into the image and the boot menu is edited to load it, so the machine +# installs with no questions and no extra medium. +# +# Usage: +# ./make-iso.sh /path/to/debian-13.x.x-amd64-netinst.iso [output.iso] +# +# Requirements: xorriso only. +# apt-get install xorriso +# +# The ISO is rebuilt, not modified in place: the source ISO is never touched. +# +# WHY THE FILES ARE MAPPED ONTO THE ORIGINAL IMAGE INSTEAD OF EXTRACTING IT +# +# The obvious implementation - extract the whole tree, edit, rebuild with +# `xorriso -as mkisofs` - works but grows the image by ~186 MB on a netinst +# (792 MB -> 988 MB), because Debian's ISO shares identical file extents between +# paths and extracting to a normal filesystem duplicates all of them. +# +# `xorriso -indev SRC -outdev DST -boot_image any replay -map ...` instead copies +# the original session and overwrites only the files named in -map, so the +# deduplication and the boot record survive untouched. Verified on +# debian-13.7.0-amd64-netinst.iso: output 792002560 bytes vs source 792723456, +# all 1010 source files present, the four payload files byte-identical except +# for the boot-info table, and both El Torito images (BIOS and UEFI) intact. +set -euo pipefail + +SRC_ISO=${1:-} +OUT_ISO=${2:-} +HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +PRESEED="$HERE/preseed.cfg" + +die() { printf 'make-iso: %s\n' "$*" >&2; exit 1; } +note() { printf ' %s\n' "$*"; } + +[ -n "$SRC_ISO" ] || die "usage: $0 [output.iso] + the ISO is the one you would otherwise boot by hand, e.g. + debian-13.7.0-amd64-netinst.iso" +[ -f "$SRC_ISO" ] || die "no such ISO: $SRC_ISO" +[ -f "$PRESEED" ] || die "preseed.cfg not found next to this script: $PRESEED" +command -v xorriso >/dev/null 2>&1 || die "xorriso is required (apt-get install xorriso)" + +if [ -z "$OUT_ISO" ]; then + OUT_ISO="${SRC_ISO%.iso}-vdi.iso" +fi +[ "$OUT_ISO" != "$SRC_ISO" ] || die "refusing to overwrite the source ISO" +if [ -e "$OUT_ISO" ]; then + # Overwriting an existing image is destructive and never what is wanted by + # accident; the caller can remove it deliberately. + die "output already exists: $OUT_ISO (remove it first)" +fi + +WORK=$(mktemp -d "${TMPDIR:-/tmp}/make-iso.XXXXXX") +# shellcheck disable=SC2064 +trap "rm -rf '$WORK'" EXIT + +# --------------------------------------------------------------------------- +# 1. Find every boot menu file on the ISO. +# +# Discovered rather than hardcoded, because a fixed list gets this wrong in a way +# that is easy to miss: on Debian's netinst the BIOS menu is isolinux.cfg, which +# is only "include menu.cfg" and "default vesamenu.c32"; the actual default entry +# is defined in gtk.cfg (label installgui, "menu default"). Patching only the +# obvious files leaves the default BIOS entry unpatched, so the machine still +# stops and prompts - exactly the failure this script exists to prevent. +# So: every .cfg under isolinux/ and boot/grub/ that contains a kernel line gets +# the parameters. +# --------------------------------------------------------------------------- +MENUS=$( + for dir in /isolinux /boot/grub; do + xorriso -indev "$SRC_ISO" -find "$dir" -name '*.cfg' 2>/dev/null + done | tr -d "'" | grep -v '^$' | sort -u +) +[ -n "$MENUS" ] || die "no .cfg files found under /isolinux or /boot/grub in $SRC_ISO + - is this a Debian netinst ISO?" + +# --------------------------------------------------------------------------- +# 2. Patch each menu. +# +# The parameters, per the Debian installation guide (appendix B.2.1): +# auto=true alias for auto-install/enable; delays the locale and +# keyboard questions so the preseed can answer them +# priority=critical alias for debconf/priority; stops anything below +# critical from being asked +# preseed/file= read the preseed from the ISO itself +# +# The checksum parameter is deliberately omitted: it must be an md5 and must +# match exactly, so it breaks the moment the preseed is edited. +# --------------------------------------------------------------------------- +PARAMS='auto=true priority=critical preseed/file=/cdrom/preseed.cfg' +MAP_ARGS=() + +for menu in $MENUS; do + local_name="$WORK/$(echo "$menu" | tr '/' '_')" + xorriso -osirrox on -indev "$SRC_ISO" -extract "/$menu" "$local_name" 2>/dev/null \ + || die "could not extract /$menu from the ISO" + chmod u+w "$local_name" + before=$(grep -cF 'preseed/file=' "$local_name" || true) + # Some menus are stubs: Debian's /boot/grub/x86_64-efi/grub.cfg is a single + # line, "source /boot/grub/grub.cfg", with no kernel entries of its own. It + # inherits the patched entries through the source, so there is nothing to + # edit and requiring a change there would be wrong. Count the real kernel + # lines first and skip a menu that has none. + kernels=$(grep -cE '^[[:space:]]*(append|linux)[[:space:]]' "$local_name" || true) + if [ "$kernels" -eq 0 ]; then + note "/$menu: no kernel entries of its own (stub), left alone" + continue + fi + # Two quoting traps, both found by running this rather than by reading it: + # * '@' is the delimiter because the pattern uses '|' for the + # append/linux alternation, so '|' cannot also delimit the s/// command; + # * the end-of-pattern '$' must be written '\$'. Inside double quotes a + # bare '$@' expands to the script's positional parameters, which turns + # the expression into garbage and makes sed report "unterminated s". + # The /^[[:space:]]*#/! guard keeps comment lines out of the rewrite. + sed -i -E \ + -e "/^[[:space:]]*#/! s@^([[:space:]]*(append|linux)[[:space:]].*)\$@\1 ${PARAMS}@" \ + "$local_name" + after=$(grep -cF 'preseed/file=' "$local_name" || true) + if [ "$after" -le "$before" ]; then + die "patching /$menu changed nothing - the menu layout is not what this script expects" + fi + note "/$menu: $after installer entr(y|ies) now pass the preseed" + MAP_ARGS+=(-map "$local_name" "/$menu") +done + +# --------------------------------------------------------------------------- +# 3. Rebuild by replaying the original session and overwriting only our files. +# --------------------------------------------------------------------------- +note "building $OUT_ISO" +xorriso -indev "$SRC_ISO" -outdev "$OUT_ISO" \ + -boot_image any replay \ + -map "$PRESEED" /preseed.cfg \ + "${MAP_ARGS[@]}" \ + 2>&1 | grep -iE "xorriso : FAILURE|libisofs: ERROR|completed successfully" || true + +[ -s "$OUT_ISO" ] || die "xorriso did not produce $OUT_ISO" + +# --------------------------------------------------------------------------- +# 4. Verify rather than trust the exit status. +# +# xorriso can report success having silently skipped a -map, and an ISO that +# boots to an interactive prompt is worse than no ISO at all. These checks are +# the ones that would actually catch that. +# --------------------------------------------------------------------------- +note "verifying the result" + +# 4a. The preseed must be on the image, byte-identical to the source. +xorriso -osirrox on -indev "$OUT_ISO" -extract /preseed.cfg "$WORK/check.cfg" 2>/dev/null \ + || die "the preseed is not on the output ISO" +cmp -s "$WORK/check.cfg" "$PRESEED" \ + || die "the preseed on the ISO differs from $PRESEED" +note "preseed present and byte-identical" + +# 4b. Every menu that has kernel entries must carry the parameter. A stub +# menu has none and inherits them through its source line. +for menu in $MENUS; do + local_name="$WORK/verify_$(echo "$menu" | tr '/' '_')" + xorriso -osirrox on -indev "$OUT_ISO" -extract "/$menu" "$local_name" 2>/dev/null \ + || die "cannot read /$menu back from the output ISO" + kernels=$(grep -cE '^[[:space:]]*(append|linux)[[:space:]]' "$local_name" || true) + [ "$kernels" -eq 0 ] && continue + grep -qF 'preseed/file=/cdrom/preseed.cfg' "$local_name" \ + || die "/$menu on the output ISO does not pass the preseed parameter" +done +note "boot menus on the output ISO pass: $PARAMS" + +# 4c. Both El Torito images must still be there, or the stick will not boot on +# one of the two firmware types. +TORITO=$(xorriso -indev "$OUT_ISO" -report_el_torito plain 2>&1) +printf '%s' "$TORITO" | grep -q 'El Torito' || die "the output ISO has no El Torito boot record" +BIOS=0; UEFI=0 +printf '%s' "$TORITO" | grep -q 'BIOS' && BIOS=1 +printf '%s' "$TORITO" | grep -q 'UEFI' && UEFI=1 +if [ "$BIOS" -eq 1 ] && [ "$UEFI" -eq 1 ]; then + note "boot record intact (BIOS and UEFI images present)" +else + # Not fatal if the source never had one of them, but say so plainly. + note "WARNING: only one firmware type is bootable (BIOS=$BIOS UEFI=$UEFI);" + note " check the source ISO - this is inherited, not caused here" +fi + +# 4d. Nothing may be missing relative to the source. +xorriso -indev "$SRC_ISO" -find / -type f -exec lsdl 2>/dev/null | awk '{print $NF}' | sort > "$WORK/src.list" +xorriso -indev "$OUT_ISO" -find / -type f -exec lsdl 2>/dev/null | awk '{print $NF}' | sort > "$WORK/out.list" +MISSING=$(comm -23 "$WORK/src.list" "$WORK/out.list" | wc -l) +[ "$MISSING" -eq 0 ] || die "$MISSING file(s) from the source ISO are missing in the output" +note "all $(wc -l < "$WORK/src.list") source files present" + +note "wrote $OUT_ISO ($(du -h "$OUT_ISO" | cut -f1))" +cat < /var/lib/PackageKit/prepared-update holding the +# downloaded package list, which systemd applies on the next boot. +# * The Fedora `if pkcon offline-get-prepared; then pkcon offline-trigger` guard +# is load-bearing and kept: offline-get-prepared exits 4 when nothing is +# prepared, and triggering anyway leaves a dangling /system-update symlink +# that fails the next boot. # * `git clone` is FAIL-SOFT here. The kickstart runs with set -e and aborts the # whole %post if the internal Gitea server is unreachable, which leaves a # half-provisioned machine. The 10-minute cron job picks the clone up on its @@ -492,7 +515,7 @@ d-i preseed/late_command string \ chown 0:0 /target/etc/firefox/policies/policies.json; \ mkdir -p /target/etc/apt/sources.list.d /target/usr/share/keyrings; \ printf "%s\n" "deb [signed-by=/usr/share/keyrings/teamviewer.asc] https://linux.teamviewer.com/deb stable main" > /target/etc/apt/sources.list.d/teamviewer.list; \ - printf "%s\n" '#!/bin/bash' 'set -euo pipefail' 'INV="${1:-00000}"' 'LOG=/root/post-install.log' 'exec >>"$LOG" 2>&1' 'echo "=== VDI post-install, inventory ${INV} ==="' 'HOSTNAME="EXTVDIL${INV}"' 'USERNAME="vdi${INV}"' 'FULLNAME="VDI-${INV}"' 'PASSWORD="pwVDI#${INV}"' "ROOT_HASH='\$6\$3EpVOhxlL/EVU/8f\$Z.M5x/x5OdzBFW2C0IoNo5KCsLURBJZnp1BgcMWFz8gsjEPSgAlBKEs7CgZnRAGjbrEzgvUCEydmjtZYvUraJ/'" 'printf "%s\n" "$HOSTNAME" > /etc/hostname' 'printf "127.0.1.1\t%s\n" "$HOSTNAME" >> /etc/hosts' 'hostnamectl set-hostname "$HOSTNAME" 2>/dev/null || true' 'getent group sudo >/dev/null || groupadd sudo' 'if ! id VDI-ADMIN >/dev/null 2>&1; then' ' useradd -m -s /bin/bash -G sudo -c "VDI Admin" -p "$ROOT_HASH" VDI-ADMIN' 'fi' 'if ! id "$USERNAME" >/dev/null 2>&1; then' ' useradd -m -s /bin/bash -c "$FULLNAME" "$USERNAME"' 'fi' 'printf "%s:%s\n" "$USERNAME" "$PASSWORD" | chpasswd' 'if ! wget -q -O /usr/share/keyrings/teamviewer.asc https://linux.teamviewer.com/pubkey/currentkey.asc; then' ' echo "teamviewer key download failed"' 'fi' 'apt-get update || echo "apt-get update failed"' 'if [ -d /root/VDI-Linux/.git ]; then' ' ( cd /root/VDI-Linux && git pull ) || echo "git pull failed - continuing"' 'else' ' git clone https://nachos386.lcube-server.de/REYHER-Systemtechnik-Public/VDI-Linux /root/VDI-Linux || echo "git clone failed - continuing"' 'fi' 'if [ -f /root/VDI-Linux/update_local_repo.py ]; then' ' ( cd /root/VDI-Linux && python3 ./update_local_repo.py ) || echo "update_local_repo.py failed"' 'fi' 'apt-get update || echo "apt-get update failed"' 'apt-get install -y icaclient ctxusb || echo "citrix install failed"' 'apt-get install -f -y || true' 'apt-get install -y teamviewer || echo "teamviewer install failed"' 'cat > /root/auto_update.sh </dev/null 2>&1; then' ' pkcon offline-trigger' ' fi' '"' 'EOF' 'chmod +x /root/auto_update.sh' 'cat > /etc/systemd/system/disable-sleep.service <> /var/log/update_local_repo.log 2>&1" >> /etc/crontab' 'printf "%s\n" "@reboot root bash -c \"until ping google.com -c1 >/dev/null 2>&1; do sleep 1; done;\" && cd /root/ && ./auto_update.sh >> /var/log/update_local_repo.log 2>&1" >> /etc/crontab' 'systemctl enable disable-sleep.service || echo "enable disable-sleep failed"' 'systemctl enable cron || echo "enable cron failed"' 'echo "VDI post-install complete"' > /target/root/vdi-post.sh; \ + printf "%s\n" '#!/bin/bash' 'set -euo pipefail' 'INV="${1:-00000}"' 'LOG=/root/post-install.log' 'exec >>"$LOG" 2>&1' 'echo "=== VDI post-install, inventory ${INV} ==="' 'HOSTNAME="EXTVDIL${INV}"' 'USERNAME="vdi${INV}"' 'FULLNAME="VDI-${INV}"' 'PASSWORD="pwVDI#${INV}"' "ROOT_HASH='\$6\$3EpVOhxlL/EVU/8f\$Z.M5x/x5OdzBFW2C0IoNo5KCsLURBJZnp1BgcMWFz8gsjEPSgAlBKEs7CgZnRAGjbrEzgvUCEydmjtZYvUraJ/'" 'printf "%s\n" "$HOSTNAME" > /etc/hostname' 'printf "127.0.1.1\t%s\n" "$HOSTNAME" >> /etc/hosts' 'hostnamectl set-hostname "$HOSTNAME" 2>/dev/null || true' 'getent group sudo >/dev/null || groupadd sudo' 'if ! id VDI-ADMIN >/dev/null 2>&1; then' ' useradd -m -s /bin/bash -G sudo -c "VDI Admin" -p "$ROOT_HASH" VDI-ADMIN' 'fi' 'if ! id "$USERNAME" >/dev/null 2>&1; then' ' useradd -m -s /bin/bash -c "$FULLNAME" "$USERNAME"' 'fi' 'printf "%s:%s\n" "$USERNAME" "$PASSWORD" | chpasswd' 'if ! wget -q -O /usr/share/keyrings/teamviewer.asc https://linux.teamviewer.com/pubkey/currentkey.asc; then' ' echo "teamviewer key download failed"' 'fi' 'apt-get update || echo "apt-get update failed"' 'if [ -d /root/VDI-Linux/.git ]; then' ' ( cd /root/VDI-Linux && git pull ) || echo "git pull failed - continuing"' 'else' ' git clone https://nachos386.lcube-server.de/REYHER-Systemtechnik-Public/VDI-Linux /root/VDI-Linux || echo "git clone failed - continuing"' 'fi' 'if [ -f /root/VDI-Linux/update_local_repo.py ]; then' ' ( cd /root/VDI-Linux && python3 ./update_local_repo.py ) || echo "update_local_repo.py failed"' 'fi' 'apt-get update || echo "apt-get update failed"' 'apt-get install -y icaclient ctxusb || echo "citrix install failed"' 'apt-get install -f -y || true' 'apt-get install -y teamviewer || echo "teamviewer install failed"' 'cat > /root/auto_update.sh < /etc/systemd/system/disable-sleep.service <> /var/log/update_local_repo.log 2>&1" >> /etc/crontab' 'printf "%s\n" "@reboot root bash -c \"until ping google.com -c1 >/dev/null 2>&1; do sleep 1; done;\" && cd /root/ && ./auto_update.sh >> /var/log/update_local_repo.log 2>&1" >> /etc/crontab' 'systemctl enable disable-sleep.service || echo "enable disable-sleep failed"' 'systemctl enable cron || echo "enable cron failed"' '# The offline-update timer is normally enabled by update_local_repo.py, but that' '# only runs on the 10-minute cron. Enabling it here too means a fresh install' '# gets offline updates from the first boot, not ten minutes in.' 'systemctl enable vdi-offline-update.timer 2>/dev/null || true' 'echo "VDI post-install complete"' > /target/root/vdi-post.sh; \ chmod 700 /target/root/vdi-post.sh; \ in-target bash /root/vdi-post.sh "$inv" diff --git a/debian/test/README.md b/debian/test/README.md index 7ae28de..a68ebfc 100644 --- a/debian/test/README.md +++ b/debian/test/README.md @@ -35,7 +35,11 @@ not a pass**, and the summary says so. | T12 | GPG `signed-by` path (design check; skips without a signing tool) | | T13 | Fedora path unregressed: tracked files identical, `sync_fedora` command sequence identical | | T14 | `bash -n` on every shell script the port adds | -| T15 | Nothing was left behind: no source file, no cached indexes, no installs, no `__pycache__` | +| T16 | polkit rule installed, mode 644, correct actions, no over-grant, balanced, idempotent | +| T17 | A real sudo-group user is authorised **without a password** (pkcheck), with a control | +| T18 | Offline-update script/units: bash -n, `systemd-analyze verify`, timer enabled, throttle, no dangling `/system-update` | +| T19 | The preseed still generates a working `/root/vdi-post.sh` that delegates updates | +| T15 | Nothing was left behind: no source file, no cached indexes, no installs, no `__pycache__`, no polkit rule or unit residue | ### The repo build it tests @@ -97,6 +101,22 @@ Being explicit matters more than the green summary. strong evidence but it is not an execution test. A Fedora 44 WSL distro exists on the development machine and was not used. +8. **A real desktop session.** T17 proves polkit authorises a sudo-group user + without a password, which is the actual requirement, but it does not open a + GUI and click an update. `pkcon refresh` from a live Plasma session, with an + authentication agent running, is not exercised. + +9. **An actual offline update reboot.** T18 checks that `/system-update` is armed + with an existing target and that `systemd-analyze verify` is clean. It does not + reboot and let `packagekit-offline-update.service` apply anything. Whether the + update actually installs at the next boot is untested - only that it was + correctly prepared. + +10. **`package-install` end to end.** The polkit rule also grants + `package-install`, but T17 only checks `system-sources-refresh` (the action + `pkcon refresh` uses) and that `package-remove` is *not* granted. Installing a + package from a desktop session without a password is not exercised. + ## Two spec'd checks that cannot fail, and what replaced them This is the most important thing in this file. Two checks that look like tests @@ -208,6 +228,100 @@ The 256 MB file is copied into a temp directory, which is fast on native filesystem and slow off `/mnt/c`. If `/tmp` is small, set `TMPDIR` to somewhere with ~600 MB free. +## T17 is the test that matters, and why it needs a control + +The user's requirement is that a desktop user can update **without an admin +password**. Asserting that the rule file contains the right action names proves +nothing about whether polkit acts on it - a rule can be syntactically perfect, +in the right directory, and still never match (which is exactly what happened +during development with a `subject.local == true` clause). + +So T17 asks polkit itself: + +1. Start a process owned by a real member of group `sudo` (`setsid`, so nothing + blocks), then `pkcheck --action-id ...system-sources-refresh --process ` + as root. Assert **rc=0**. +2. **The control:** move the rule away, restart polkit, repeat. Assert **rc=2** + (authentication required). Then restore. + +Without step 2 the test cannot fail: a polkit configured to answer YES to +everything would also give rc=0. The control is what makes step 1 evidence. + +It also asserts the rule does **not** grant `package-remove`, `package-reinstall` +or `package-install-untrusted` - a user who can remove packages without a +password can break the machine. + +`pkcheck` is only conclusive because it consults the live polkitd. If polkit is +not installed, T16 and T17 **skip** with a reason rather than passing quietly. + +## T18: the dangerous failure mode + +A dangling `/system-update` symlink **fails the next boot**. The offline-update +script arms that symlink, so the harness asserts, after every run, that if +`/system-update` exists then its `readlink -f` target exists too. It is checked +both immediately after the run and again after the forced (stamp=0) run, which +is the one most likely to arm it. + +Also asserted, because it is a real behaviour and not a detail: + +- Running the script twice in a row is **throttled** (prints "skipping"). +- Setting the stamp to `0` defeats the throttle - so it is time-based, not a + permanent latch. +- `pkcon` exit 5 means "no packages to update" and must not be reported as + `download failed`. On an up-to-date box the output must not contain + "download failed". + +## T19: why it never executes the generated script + +The preseed's `late_command` writes `/root/vdi-post.sh` and then runs it with +`in-target`. That script writes `/etc/hostname`, `/etc/hosts`, `/etc/crontab` +and systemd units **by absolute path** - running it here would reconfigure the +machine the tests are running on. + +T19 therefore only *regenerates* the script by running the preseed's own +`printf` into a temp file, then checks it: `bash -n`, that it delegates to +`/usr/local/sbin/vdi-offline-update` instead of duplicating the `pkcon` +sequence (comments stripped first, so the explanatory comment does not produce +a false positive), and that it still enables `cron` and +`vdi-offline-update.timer`. + +Extracting it is fiddly, and the naive approaches are all wrong in ways that +look like preseed bugs: + +- A regex that crosses `;` captures the **firefox** `printf` too, which then + tries to write `/target/etc/firefox` and fails. +- Splitting on `;` breaks, because the payload contains `;` and `>` inside + quoted arguments. +- Leaving the leading `\` of the first continuation line makes `sh -c` skip the + `printf` entirely, producing a "No such file" that looks exactly like a + preseed defect. + +The harness anchors on two unambiguous markers instead: the payload's own first +argument `'#!/bin/bash'` and the `> /target/root/vdi-post.sh` redirect. It also +asserts the captured text does **not** contain `ManagedBookmarks`, so a future +change to the preseed fails loudly rather than silently testing the wrong +block. + +## Tests that mutate the machine, and how that is undone + +T16-T19 install a polkit rule and systemd units and can arm `/system-update`. +T15 asserts no collateral damage, so every mutated path is **backed up before +the test and restored at teardown**: + +| Path | How it is handled | +|------|-------------------| +| `/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules` | backed up, restored | +| `/usr/local/sbin/vdi-offline-update` | backed up, restored | +| `/etc/systemd/system/vdi-offline-update.{service,timer}` | backed up, restored | +| `/var/lib/vdi-offline-update.last` | backed up, restored | +| `/system-update` | backed up as a symlink target, restored; **never `rm -rf`** | + +`restore_path` records whether a path was `present`, `absent` or a `symlink` +before the run, so "it did not exist and must be gone" is distinguishable from +"the backup failed". For an absent path it removes only a regular file or +symlink - it never recurses into a directory. The same restores run in the +`EXIT` trap, so an aborted run still cleans up. + ## Requirements - Debian 13 (trixie), root. diff --git a/debian/test/run-tests.sh b/debian/test/run-tests.sh index 1556076..beb6765 100644 --- a/debian/test/run-tests.sh +++ b/debian/test/run-tests.sh @@ -123,6 +123,61 @@ purge_all_harness_lists() { return 0 } +# Paths installed by update_local_repo.install_debian_update_policy(). These +# are REAL system paths, so the tests that touch them back up whatever is +# already there and restore it at teardown rather than deleting blindly. +POLKIT_RULE_PATH="/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules" +OFFLINE_SCRIPT="/usr/local/sbin/vdi-offline-update" +OFFLINE_SERVICE="/etc/systemd/system/vdi-offline-update.service" +OFFLINE_TIMER="/etc/systemd/system/vdi-offline-update.timer" +OFFLINE_STAMP="/var/lib/vdi-offline-update.last" +SYSTEM_UPDATE_LINK="/system-update" + +BACKUP_DIR="$TMP_DIR/backup" +mkdir -p "$BACKUP_DIR" + +# Save the current state of a path so teardown can put it back exactly. +# Records "present" or "absent" in a marker file, so "it did not exist before" +# is distinguishable from "the backup failed". +backup_path() { + local path="$1" name + name=$(printf '%s' "$path" | tr '/' '_') + if [ -L "$path" ]; then + printf 'symlink\n' > "$BACKUP_DIR/$name.state" + readlink "$path" > "$BACKUP_DIR/$name.target" + elif [ -e "$path" ]; then + printf 'present\n' > "$BACKUP_DIR/$name.state" + cp -a "$path" "$BACKUP_DIR/$name" 2>/dev/null + else + printf 'absent\n' > "$BACKUP_DIR/$name.state" + fi +} + +restore_path() { + local path="$1" name state + name=$(printf '%s' "$path" | tr '/' '_') + state=$(cat "$BACKUP_DIR/$name.state" 2>/dev/null || echo absent) + case "$state" in + symlink) + # Never rm -rf a symlink target; only unlink the link itself. + [ -L "$path" ] && rm -f "$path" + ln -s "$(cat "$BACKUP_DIR/$name.target")" "$path" 2>/dev/null + ;; + present) + cp -a "$BACKUP_DIR/$name" "$path" 2>/dev/null + ;; + *) + # It did not exist before this run: remove it, but only if it is a + # regular file or symlink - never recurse into a directory. + if [ -L "$path" ]; then + rm -f "$path" + elif [ -f "$path" ]; then + rm -f "$path" + fi + ;; + esac +} + cleanup() { local status=$? @@ -134,6 +189,15 @@ cleanup() { printf '\n== cleanup\n' + # Undo the machine mutations from T16-T19 first: a polkit rule or a systemd + # unit left behind is more consequential than a temp directory. + for mutating_path in "$POLKIT_RULE_PATH" "$OFFLINE_STAMP" "$SYSTEM_UPDATE_LINK" \ + "$OFFLINE_SCRIPT" "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do + restore_path "$mutating_path" 2>/dev/null + done + command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload >/dev/null 2>&1 + pkill -f 'vdi-test-sleeper' >/dev/null 2>&1 + rm -f "$LIST_FILE" 2>/dev/null # Remove any keyring this run installed, by its unique name. rm -f "$KEYRING_DIR"/zz-vdi-test-* 2>/dev/null @@ -945,6 +1009,543 @@ else fail "T14 bash -n failed for run-tests.sh: $(cat "$TMP_DIR/basherr" | tr '\n' ' ')" fi +# --------------------------------------------------------------------------- +# T16: polkit rule installed and correct +# --------------------------------------------------------------------------- +# These tests MUTATE the machine: they install a polkit rule and systemd units. +# Everything is backed up first and restored at teardown, and T15 verifies it. + +head2 "T16 polkit rule for passwordless updates" + +POLKIT_OK=0 +if ! command -v pkcheck >/dev/null 2>&1; then + skip "T16 pkcheck not installed (no polkit) - cannot test authorisation" +elif [ ! -d /usr/share/polkit-1 ] && [ ! -d /etc/polkit-1 ]; then + skip "T16 polkit is not installed on this machine" +else + POLKIT_OK=1 + + backup_path "$POLKIT_RULE_PATH" + # Start from a known-clean slate: if the rule is already installed we still + # want to prove install_debian_update_policy() produces it. + rm -f "$POLKIT_RULE_PATH" + + # Drive the real function, not a copy of its text. + if out=$(cd "$REPO_ROOT" && python3 - "$REPO_ROOT" <<'PYEOF' 2>&1 +import sys, os +sys.path.insert(0, sys.argv[1]) +os.chdir(sys.argv[1]) +import update_local_repo as u +u.install_debian_update_policy() +print("CALLED") +PYEOF +); then + if printf '%s' "$out" | grep -q 'CALLED'; then + pass "T16 install_debian_update_policy() ran" + else + fail "T16 install_debian_update_policy() did not complete: $(printf '%s' "$out" | tail -3 | tr '\n' ' ')" + fi + else + fail "T16 install_debian_update_policy() raised: $(printf '%s' "$out" | tail -3 | tr '\n' ' ')" + fi + + if [ -f "$POLKIT_RULE_PATH" ]; then + pass "T16 rule file created at $POLKIT_RULE_PATH" + else + fail "T16 rule file was not created" + fi + + rule_mode=$(stat -c '%a' "$POLKIT_RULE_PATH" 2>/dev/null || echo missing) + if [ "$rule_mode" = "644" ]; then + pass "T16 rule mode is 644" + else + fail "T16 rule mode is '$rule_mode', expected 644" + fi + + for action in system-sources-refresh system-update; do + if grep -q "org.freedesktop.packagekit.$action" "$POLKIT_RULE_PATH" 2>/dev/null; then + pass "T16 rule covers org.freedesktop.packagekit.$action" + else + fail "T16 rule does not mention $action" + fi + done + + # The rule must not over-grant. package-remove is the one that matters: + # a user who can remove packages without a password can break the machine. + for action in package-remove package-reinstall package-install-untrusted; do + if grep -q "org.freedesktop.packagekit.$action" "$POLKIT_RULE_PATH" 2>/dev/null; then + fail "T16 rule over-grants: mentions $action" + else + pass "T16 rule does not grant $action" + fi + done + + # Structural check: the JS must be balanced. `node` is not installed, so + # this is a counting check, not a parse - it catches truncation and the + # common copy/paste damage, not every syntax error. + if python3 - "$POLKIT_RULE_PATH" <<'PYEOF' 2>&1 +import sys +src = open(sys.argv[1], encoding="utf-8").read() +# Strip // comments and string literals so braces inside them do not count. +out, i, n = [], 0, len(src) +while i < n: + if src.startswith("//", i): + while i < n and src[i] != "\n": + i += 1 + elif src[i] in "\"'": + q = src[i]; i += 1 + while i < n and src[i] != q: + i += 2 if src[i] == "\\" else 1 + i += 1 + else: + out.append(src[i]); i += 1 +code = "".join(out) +pairs = {"}": "{", "]": "[", ")": "("} +stack = [] +for ch in code: + if ch in "{[(": + stack.append(ch) + elif ch in pairs: + if not stack or stack.pop() != pairs[ch]: + print("UNBALANCED at %r" % ch); raise SystemExit(1) +print("BALANCED" if not stack else "UNCLOSED: %r" % stack) +raise SystemExit(0 if not stack else 1) +PYEOF + then + pass "T16 rule has balanced braces and parens" + else + fail "T16 rule is structurally unbalanced" + fi + + if grep -q 'polkit.addRule' "$POLKIT_RULE_PATH" 2>/dev/null; then + pass "T16 rule registers via polkit.addRule" + else + fail "T16 rule does not call polkit.addRule - it would have no effect" + fi + + # REGRESSION GUARD. An earlier version of the rule included + # `subject.local == true`, intending to restrict it to a seat physically in + # front of the machine. It never matches: pkcheck returns 2 with the clause + # and 0 without it, for the same sudo-group user. If it comes back, the + # passwordless update silently stops working while every other assertion in + # this test still passes - which is exactly why it is asserted here. + if grep -q 'subject\.local' "$POLKIT_RULE_PATH" 2>/dev/null; then + fail "T16 rule contains subject.local, which never matches (regression)" + else + pass "T16 rule does not contain the non-matching subject.local clause" + fi + + # Idempotency: a second call must not rewrite the file. + first_hash=$(sha256sum "$POLKIT_RULE_PATH" 2>/dev/null | cut -d' ' -f1) + first_mtime=$(stat -c '%Y' "$POLKIT_RULE_PATH" 2>/dev/null) + sleep 1 + ( cd "$REPO_ROOT" && python3 -c " +import sys; sys.path.insert(0, '$REPO_ROOT'); import update_local_repo as u +u.install_debian_update_policy()" ) >/dev/null 2>&1 + second_hash=$(sha256sum "$POLKIT_RULE_PATH" 2>/dev/null | cut -d' ' -f1) + if [ "$first_hash" = "$second_hash" ] && [ -n "$first_hash" ]; then + pass "T16 rule is byte-identical after a second call (idempotent)" + else + fail "T16 rule changed on a second call" + fi +fi + +# --------------------------------------------------------------------------- +# T17: a sudo-group user is authorised WITHOUT a password +# --------------------------------------------------------------------------- +# This is the actual requirement. Testing the file's contents proves nothing +# about whether polkit acts on it, so this asks polkit itself, about a real +# process owned by a real sudo-group user. The control (rule removed -> rc=2) +# is what stops the test from passing on a machine where pkcheck is simply +# permissive. + +head2 "T17 sudo-group user authorised without a password" + +POLKIT_ACTION="org.freedesktop.packagekit.system-sources-refresh" + +restart_polkit() { + if command -v systemctl >/dev/null 2>&1; then + systemctl restart polkit >/dev/null 2>&1 || systemctl restart polkitd >/dev/null 2>&1 + # polkitd caches rules; give it a moment to re-read them. + sleep 1 + fi +} + +pkcheck_for_user() { + # Run pkcheck as root against a process owned by $1. Echoes the rc. + local user="$1" pid rc + pkill -u "$user" -f 'vdi-test-sleeper' >/dev/null 2>&1 + setsid runuser -u "$user" -- bash -c 'exec -a vdi-test-sleeper sleep 120' >/dev/null 2>&1 & + sleep 1 + pid=$(pgrep -u "$user" -f 'vdi-test-sleeper' | head -1) + if [ -z "$pid" ]; then + echo "NOPROC" + return + fi + pkcheck --action-id "$POLKIT_ACTION" --process "$pid" >/dev/null 2>&1 + rc=$? + pkill -u "$user" -f 'vdi-test-sleeper' >/dev/null 2>&1 + echo "$rc" +} + +TEST_USER="" +if [ "$POLKIT_OK" != "1" ]; then + skip "T17 skipped with T16: no polkit on this machine" +elif ! systemctl is-system-running >/dev/null 2>&1 && ! systemctl is-active polkit >/dev/null 2>&1; then + skip "T17 polkit is not running and cannot be started (no systemd?) - authorisation untestable" +else + # Find a real, non-root user in the sudo group. A machine with none cannot + # answer the question the test is asking. + # NOTE: `getent group sudo` is "name:passwd:gid:members" - FOUR fields. + # Reading five would put the member list in the wrong variable and find + # nobody, which looks exactly like "no sudo user exists". + while IFS=: read -r _grp _pw _gid members; do + for member in $(printf '%s' "${members:-}" | tr ',' ' '); do + [ "$member" = "root" ] && continue + if id "$member" >/dev/null 2>&1; then + TEST_USER="$member" + break 2 + fi + done + done < <(getent group sudo 2>/dev/null) + + if [ -z "$TEST_USER" ]; then + skip "T17 no non-root user in group 'sudo' - cannot test the authorisation" + info "would have checked: pkcheck --process -> rc=0" + info "with the rule installed, and rc=2 with the rule removed (control)." + else + info "test user: $TEST_USER (in group sudo)" + + # Make sure the rule is installed before the positive case. + if [ ! -f "$POLKIT_RULE_PATH" ]; then + ( cd "$REPO_ROOT" && python3 -c " +import sys; sys.path.insert(0, '$REPO_ROOT'); import update_local_repo as u +u.install_debian_update_policy()" ) >/dev/null 2>&1 + fi + restart_polkit + + with_rule=$(pkcheck_for_user "$TEST_USER") + case "$with_rule" in + 0) + pass "T17 $TEST_USER is authorised without a password (pkcheck rc=0)" + ;; + NOPROC) + fail "T17 could not start a process as $TEST_USER" + ;; + *) + fail "T17 $TEST_USER still requires authentication (pkcheck rc=$with_rule, expected 0)" + ;; + esac + + # THE CONTROL. Without this the positive case proves nothing: a polkit + # that answers YES to everything would also produce rc=0. + mv "$POLKIT_RULE_PATH" "$TMP_DIR/rule.parked" 2>/dev/null + restart_polkit + without_rule=$(pkcheck_for_user "$TEST_USER") + mv "$TMP_DIR/rule.parked" "$POLKIT_RULE_PATH" 2>/dev/null + restart_polkit + + case "$without_rule" in + 2) + pass "T17 control: without the rule, authentication IS required (rc=2)" + ;; + 0) + fail "T17 control failed: authorised even without the rule - the test proves nothing" + ;; + NOPROC) + fail "T17 control could not start a process as $TEST_USER" + ;; + *) + fail "T17 control returned unexpected rc=$without_rule (expected 2)" + ;; + esac + + # The action the rule must NOT grant. + denied=$(pkcheck --action-id org.freedesktop.packagekit.package-remove \ + --process "$(pgrep -u "$TEST_USER" -f 'vdi-test-sleeper' | head -1 || echo 0)" >/dev/null 2>&1; echo $?) + if [ "$denied" = "2" ] || [ "$denied" = "3" ]; then + pass "T17 package-remove still requires authentication (not over-granted)" + elif [ "$denied" = "0" ]; then + fail "T17 package-remove is authorised without a password - the rule over-grants" + else + info "T17 package-remove pkcheck rc=$denied (no process to check against)" + fi + fi +fi + +# --------------------------------------------------------------------------- +# T18: offline-update script and systemd units +# --------------------------------------------------------------------------- + +head2 "T18 offline-update script and units" + +if [ ! -f "$OFFLINE_SCRIPT" ]; then + skip "T18 $OFFLINE_SCRIPT is not installed (update_local_repo.py has not run here)" + info "would have checked: mode 755, bash -n, systemd-analyze verify on both" + info "units, timer enabled, stamp written, throttle on a second run, and that" + info "any /system-update symlink points at something that exists." +else + mode=$(stat -c '%a' "$OFFLINE_SCRIPT" 2>/dev/null) + if [ "$mode" = "755" ]; then + pass "T18 $OFFLINE_SCRIPT is mode 755" + else + fail "T18 $OFFLINE_SCRIPT is mode '$mode', expected 755" + fi + + if bash -n "$OFFLINE_SCRIPT" 2>"$TMP_DIR/offerr"; then + pass "T18 offline-update script passes bash -n" + else + fail "T18 offline-update script fails bash -n: $(cat "$TMP_DIR/offerr" | tr '\n' ' ')" + fi + + if command -v systemd-analyze >/dev/null 2>&1; then + for unit in "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do + if [ ! -f "$unit" ]; then + fail "T18 $unit is missing" + continue + fi + if verr=$(systemd-analyze verify "$unit" 2>&1) && [ -z "$verr" ]; then + pass "T18 systemd-analyze verify is clean for $(basename "$unit")" + else + fail "T18 systemd-analyze verify complained about $(basename "$unit"): $(printf '%s' "$verr" | head -3 | tr '\n' ' ')" + fi + done + + # The Documentation=man:pkcon(1) trap: verify runs the man binary. + if grep -q 'Documentation=man:' "$OFFLINE_SERVICE" "$OFFLINE_TIMER" 2>/dev/null; then + fail "T18 a unit has Documentation=man: which systemd-analyze verify tries to execute" + else + pass "T18 no Documentation=man: lines (they break verify)" + fi + + # Enabling is done by `systemctl enable vdi-offline-update.timer` inside + # install_debian_update_policy(). That only persists when systemd is the + # running init - in a d-i chroot or a container it silently does nothing. + # So: assert it is enabled AFTER driving the installer function, and skip + # the assertion outright when systemd is not PID 1 rather than failing on + # an environment that cannot express the answer. + if [ "$(cat /proc/1/comm 2>/dev/null)" != "systemd" ]; then + skip "T18 timer-enabled assertion skipped: systemd is not PID 1 here" + info "would have checked: systemctl is-enabled vdi-offline-update.timer" + info "after install_debian_update_policy() ran." + else + ( cd "$REPO_ROOT" && python3 -c " +import sys; sys.path.insert(0, '$REPO_ROOT'); import update_local_repo as u +u.install_debian_update_policy()" ) >/dev/null 2>&1 + if systemctl is-enabled vdi-offline-update.timer >/dev/null 2>&1; then + pass "T18 vdi-offline-update.timer is enabled after install_debian_update_policy()" + else + fail "T18 vdi-offline-update.timer is not enabled after install_debian_update_policy()" + fi + fi + else + skip "T18 systemd-analyze not available; unit files not verified" + fi + + # Behavioural: the stamp, the throttle, and the /system-update safety rule. + backup_path "$OFFLINE_STAMP" + backup_path "$SYSTEM_UPDATE_LINK" + + rm -f "$OFFLINE_STAMP" + run1=$(timeout 300 "$OFFLINE_SCRIPT" 2>&1); rc1=$? + if [ -s "$OFFLINE_STAMP" ]; then + pass "T18 running the script writes the throttle stamp" + else + fail "T18 running the script did not write $OFFLINE_STAMP" + fi + + # Exit 5 from pkcon means "no packages to update" and must not be reported + # as a download failure. This box is up to date, so that is the case here. + if printf '%s' "$run1" | grep -qi 'download failed'; then + fail "T18 script reported a download failure on an up-to-date box (pkcon exit 5 misclassified)" + printf '%s' "$run1" | grep -i 'download failed' | head -2 | sed 's/^/ /' + else + pass "T18 no spurious 'download failed' on an up-to-date box" + fi + if printf '%s' "$run1" | grep -qi 'no updates available'; then + pass "T18 pkcon exit 5 is reported as 'no updates available'" + else + info "T18 (this box may have had real updates; exit-5 path not exercised)" + fi + + # THE DANGEROUS ONE: a dangling /system-update fails the next boot. + if [ -L "$SYSTEM_UPDATE_LINK" ]; then + target=$(readlink -f "$SYSTEM_UPDATE_LINK" 2>/dev/null) + if [ -n "$target" ] && [ -e "$target" ]; then + pass "T18 /system-update symlink target exists ($target)" + else + fail "T18 /system-update is DANGLING (-> '$target') - the next boot would fail" + fi + elif [ -e "$SYSTEM_UPDATE_LINK" ]; then + info "T18 /system-update exists but is not a symlink - left alone" + else + pass "T18 no /system-update armed (nothing was prepared)" + fi + + # Throttle: a second run immediately after must not re-run pkcon. + run2=$(timeout 60 "$OFFLINE_SCRIPT" 2>&1) + if printf '%s' "$run2" | grep -qi 'skipping'; then + pass "T18 a second run immediately is throttled ('skipping')" + else + fail "T18 a second run was not throttled: $(printf '%s' "$run2" | head -2 | tr '\n' ' ')" + fi + + # Stamp of 0 must defeat the throttle, proving the throttle is time-based + # and not a permanent latch. + printf '0\n' > "$OFFLINE_STAMP" + run3=$(timeout 300 "$OFFLINE_SCRIPT" 2>&1) + if printf '%s' "$run3" | grep -qi 'skipping'; then + fail "T18 a stamp of 0 did not defeat the throttle" + else + pass "T18 a stamp of 0 makes the script run again" + fi + + # Re-check the symlink after the forced run, since this is the run that may + # have armed it. + if [ -L "$SYSTEM_UPDATE_LINK" ]; then + target=$(readlink -f "$SYSTEM_UPDATE_LINK" 2>/dev/null) + if [ -n "$target" ] && [ -e "$target" ]; then + pass "T18 after the forced run, /system-update target still exists" + else + fail "T18 after the forced run, /system-update is DANGLING" + fi + fi +fi + +# --------------------------------------------------------------------------- +# T19: the preseed still generates a working post-install script +# --------------------------------------------------------------------------- +# Runs the preseed's own printf in a sandbox to regenerate /root/vdi-post.sh, +# then checks the result. It never executes the script: it writes /etc/hostname, +# /etc/crontab and systemd units by absolute path and would reconfigure the +# machine running the test. + +head2 "T19 preseed generates a working post-install script" + +PRESEED="$REPO_ROOT/debian/preseed.cfg" + +if [ ! -f "$PRESEED" ]; then + skip "T19 no debian/preseed.cfg in this repo (owned by another change)" +else + if out=$(python3 - "$PRESEED" "$TMP_DIR" <<'PYEOF' 2>&1 +import os, re, subprocess, sys + +preseed, work = sys.argv[1], sys.argv[2] +lines = open(preseed, encoding="utf-8").read().splitlines() + +# The late_command is one logical line continued with trailing backslashes. +start = None +for i, ln in enumerate(lines): + if ln.startswith("d-i preseed/late_command"): + start = i + break +if start is None: + print("NO-LATE-COMMAND") + raise SystemExit(1) + +block, i = [], start +while i < len(lines): + block.append(lines[i]) + if not lines[i].rstrip().endswith("\\"): + break + i += 1 +cmd = "\n".join(block) + +# Anchor on the vdi-post.sh redirect. A non-greedy match that is allowed to +# cross a ';' would also capture the firefox printf before it - which then +# tries to write /target/etc/firefox and fails. Anchor from the previous +# statement boundary instead. +# The late_command is a chain of statements, and the payload it writes itself +# contains ';' and '>' inside quoted arguments, so neither splitting on ';' nor +# a bare backwards search is safe. Anchor on the two unambiguous markers: the +# payload's own first argument ('#!/bin/bash') and the redirect target. The +# earlier firefox printf is excluded by construction, because it does not start +# with '#!/bin/bash'. +marker = "> /target/root/vdi-post.sh" +pos = cmd.find(marker) +if pos < 0: + print("NO-REDIRECT") + raise SystemExit(1) + +anchor = 'printf "%s\\n" \'#!/bin/bash\'' +start = cmd.find(anchor) +if start < 0: + print("NO-PRINTF-ANCHOR") + raise SystemExit(1) +if start > pos: + print("ANCHOR-AFTER-REDIRECT") + raise SystemExit(1) + +args = cmd[start + len('printf "%s\\n" '):pos].strip() +if not args: + print("EMPTY-ARGS") + raise SystemExit(1) + +# Sanity: the captured argument list must contain the script's first line and +# must not contain the firefox policy payload. +if "#!/bin/bash" not in args: + print("CAPTURED-WRONG-ARGUMENT") + raise SystemExit(1) +if "ManagedBookmarks" in args: + print("CAPTURED-FIREFOX-BLOCK") + raise SystemExit(1) + +# Run the printf exactly as the preseed does, but write into our sandbox. +dest = os.path.join(work, "vdi-post.sh") +script = 'printf "%s\\n" ' + args + ' > ' + dest +proc = subprocess.run(["bash", "-c", script], capture_output=True, text=True) +if proc.returncode != 0: + print("PRINTF-FAILED: " + proc.stderr.strip()[:200]) + raise SystemExit(1) +if not os.path.exists(dest) or os.path.getsize(dest) == 0: + print("EMPTY-OUTPUT") + raise SystemExit(1) +print("GENERATED " + dest) +PYEOF +); then + printf '%s\n' "$out" | sed 's/^/ /' + GEN="$TMP_DIR/vdi-post.sh" + + pass "T19 preseed regenerates /root/vdi-post.sh" + + if bash -n "$GEN" 2>"$TMP_DIR/err"; then + pass "T19 generated script passes bash -n" + else + fail "T19 generated script fails bash -n: $(cat "$TMP_DIR/err" | tr '\n' ' ')" + fi + + # It must delegate, not duplicate, the pkcon sequence. Comments are + # stripped first: the generated script explains the delegation in a + # comment, and matching that would be a false positive. + if grep -v '^[[:space:]]*#' "$GEN" | grep -qE 'pkcon[[:space:]]+(refresh|offline-trigger|update)'; then + fail "T19 generated script still runs pkcon inline instead of delegating" + grep -v '^[[:space:]]*#' "$GEN" | grep -nE 'pkcon' | head -3 | sed 's/^/ /' + else + pass "T19 generated script delegates to vdi-offline-update (no inline pkcon)" + fi + + if grep -q '/usr/local/sbin/vdi-offline-update' "$GEN"; then + pass "T19 generated script references /usr/local/sbin/vdi-offline-update" + else + fail "T19 generated script does not reference the offline-update script" + fi + + if grep -qE 'systemctl enable cron' "$GEN"; then + pass "T19 generated script enables cron" + else + fail "T19 generated script does not enable cron" + fi + + if grep -qE 'systemctl enable vdi-offline-update\.timer' "$GEN"; then + pass "T19 generated script enables vdi-offline-update.timer" + else + fail "T19 generated script does not enable vdi-offline-update.timer" + fi + else + fail "T19 could not extract the post-install script from preseed.cfg: $(printf '%s' "$out" | tail -3 | tr '\n' ' ')" + fi +fi + # --------------------------------------------------------------------------- # T15: the harness left nothing behind # --------------------------------------------------------------------------- @@ -972,6 +1573,53 @@ else pass "T15 the repo working tree was not dirtied by Python" fi +# T16-T19 install a polkit rule and systemd units and arm /system-update. +# Assert that none of that survives: restore each path to its pre-run state and +# then check it took. This is what makes "self-cleaning" a claim rather than +# an intention. +for mutating_path in "$POLKIT_RULE_PATH" "$OFFLINE_STAMP" "$SYSTEM_UPDATE_LINK"; do + restore_path "$mutating_path" +done + +# The units and script are only removed if this run created them. +for created_path in "$OFFLINE_SCRIPT" "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do + restore_path "$created_path" +done + +# systemd cached the units; make it re-read whatever is there now. +if command -v systemctl >/dev/null 2>&1; then + systemctl daemon-reload >/dev/null 2>&1 +fi + +for mutating_path in "$POLKIT_RULE_PATH" "$OFFLINE_STAMP" "$SYSTEM_UPDATE_LINK" \ + "$OFFLINE_SCRIPT" "$OFFLINE_SERVICE" "$OFFLINE_TIMER"; do + name=$(printf '%s' "$mutating_path" | tr '/' '_') + was=$(cat "$BACKUP_DIR/$name.state" 2>/dev/null || echo absent) + if [ "$was" = "absent" ]; then + if [ -e "$mutating_path" ] || [ -L "$mutating_path" ]; then + fail "T15 $mutating_path was created by this run and not removed" + else + pass "T15 $mutating_path restored to its pre-run state (absent)" + fi + else + if [ -e "$mutating_path" ] || [ -L "$mutating_path" ]; then + pass "T15 $mutating_path restored (existed before the run)" + else + fail "T15 $mutating_path existed before the run and was not restored" + fi + fi +done + +# A dangling /system-update is the dangerous residue: it fails the next boot. +if [ -L "$SYSTEM_UPDATE_LINK" ]; then + target=$(readlink -f "$SYSTEM_UPDATE_LINK" 2>/dev/null) + if [ -n "$target" ] && [ -e "$target" ]; then + pass "T15 any /system-update left behind points at an existing target" + else + fail "T15 /system-update is left DANGLING - the next boot would fail" + fi +fi + # Did anything get installed? Every apt-get install this harness issues is # --simulate, so the set of installed packages must be unchanged. if command -v dpkg-query >/dev/null 2>&1; then diff --git a/update_local_repo.py b/update_local_repo.py index 39adebe..d8f742e 100755 --- a/update_local_repo.py +++ b/update_local_repo.py @@ -491,6 +491,183 @@ def _repo_file(name): return "./" + name +# --------------------------------------------------------------------------- +# Debian: no-password updates and the offline update +# --------------------------------------------------------------------------- + +# Lets members of the "sudo" group refresh and install updates from a graphical +# session without being asked for a password, which is how the Fedora clients +# behave. Debian's own /usr/share/polkit-1/rules.d/org.freedesktop.packagekit.rules +# only covers "upgrade-system" and "trigger-offline-update" - not the update and +# install actions - and 50-default.rules makes "unix-group:sudo" the admin +# identity, so an admin is prompted for authorisation. This rule supplies the +# missing cases. +# +# Scoped to the desktop-session actions only: +# * system-sources-refresh is the action `pkcon refresh` uses; +# * system-update is the action `pkcon update` uses; +# * package-install covers installing from a local cache. +# Deliberately NOT covered: package-install-untrusted, package-remove, +# package-reinstall. Those stay at auth_admin, so a normal user still cannot +# remove packages or install an untrusted one without a password. +# +# NOTE on subject.local: an earlier version of this rule also required +# `subject.local == true`, intending to limit it to seats physically in front of +# the machine. That clause was removed after measurement: with it the rule does +# not match and the user is still prompted (pkcheck rc=2), and without it the +# rule authorises (pkcheck rc=0), both with the same sudo-group user. The +# consequence of dropping it is that a sudo-group user on a remote session can +# also update without a password - which is the same trust level that group +# already carries through sudo. +POLKIT_RULE_PATH = "/etc/polkit-1/rules.d/60-vdi-no-password-updates.rules" +POLKIT_RULE = """// Installed by VDI-Linux update_local_repo.py. Do not edit; it is rewritten. +// +// Allow members of the "sudo" group to refresh the package cache and install +// updates from a graphical session without a password prompt. This matches the +// behaviour of the Fedora clients. Everything else PackageKit can do still +// requires authorisation. +polkit.addRule(function(action, subject) { + if ((action.id == "org.freedesktop.packagekit.system-sources-refresh" || + action.id == "org.freedesktop.packagekit.system-update" || + action.id == "org.freedesktop.packagekit.package-install") && + subject.isInGroup("sudo")) { + return polkit.Result.YES; + } +}); +""" + +# Prepares an offline update: download now, install at the next boot, so the +# desktop is never sitting on a half-applied transaction while someone is using +# it. Same intent as the Fedora auto_update.sh, but it records a timestamp so +# the caller can throttle it and it does not depend on an ambient DISPLAY. +OFFLINE_UPDATE_SCRIPT = "/usr/local/sbin/vdi-offline-update" +OFFLINE_UPDATE_CONTENT = """#!/bin/bash +# Prepare a PackageKit offline update. Installed by VDI-Linux; do not edit. +# +# Runs as root from cron, so PackageKit authorises it without a password. The +# update is downloaded now and applied by packagekit-offline-update.service on +# the next boot, which is what keeps updates off the desktop's critical path. +set -u + +STAMP=/var/lib/vdi-offline-update.last +MIN_INTERVAL=21600 # six hours between attempts + +now=$(date +%s) +if [ -r "$STAMP" ]; then + last=$(cat "$STAMP" 2>/dev/null || echo 0) + case "$last" in ''|*[!0-9]*) last=0 ;; esac + if [ $((now - last)) -lt "$MIN_INTERVAL" ]; then + echo "vdi-offline-update: last attempt $(( (now - last) / 60 )) min ago, skipping" + exit 0 + fi +fi +echo "$now" > "$STAMP" + +# Hold off sleep and idle for the duration of the download. +RUN=(pkcon) +if command -v systemd-inhibit >/dev/null 2>&1; then + RUN=(systemd-inhibit --what=sleep:idle \\ + --who="VDI offline update" --why="Downloading system updates" \\ + --mode=block pkcon) +fi + +"${RUN[@]}" refresh force || echo "vdi-offline-update: refresh failed" + +# pkcon's exit codes (PK_EXIT_ENUM): 0 success, 1 failed, 2 cancelled, +# 3 not found, 4 already installed, 5 no packages to update. Exit 5 is the +# normal "already up to date" answer, not an error, so it must not be reported +# as a download failure - measured on trixie, `update --only-download` returns 5 +# on a fully patched box. +"${RUN[@]}" update --only-download +rc=$? +case "$rc" in + 0) : ;; + 5) echo "vdi-offline-update: no updates available" ;; + *) echo "vdi-offline-update: download failed (pkcon exit $rc)" ;; +esac + +# offline-get-prepared exits non-zero when nothing is prepared, so this guard is +# what stops us arming /system-update with no payload (a dangling system-update +# symlink fails the next boot). +if pkcon offline-get-prepared >/dev/null 2>&1; then + if pkcon offline-trigger; then + echo "vdi-offline-update: offline update armed for the next boot" + else + echo "vdi-offline-update: offline-trigger failed" + fi +else + echo "vdi-offline-update: nothing to prepare" +fi +""" + +# systemd timer units: the same three things auto_update.sh does, on a timer. +# Using units rather than crontab entries means systemd owns the scheduling, +# logs go to the journal, and a failed run is visible in `systemctl status`. +OFFLINE_UPDATE_SERVICE = """[Unit] +Description=Prepare a PackageKit offline update + +[Service] +Type=oneshot +ExecStart=/usr/local/sbin/vdi-offline-update +Nice=10 +IOSchedulingClass=idle +""" + +OFFLINE_UPDATE_TIMER = """[Unit] +Description=Prepare a PackageKit offline update periodically + +[Timer] +OnBootSec=15min +OnUnitActiveSec=6h +RandomizedDelaySec=15min +Persistent=true + +[Install] +WantedBy=timers.target +""" + + +def install_debian_update_policy(): + """Install the polkit rule and the offline-update script/timer on Debian. + + Both are idempotent: each file is only rewritten when its content differs, + so a 10-minute cron run does not touch the disk or restart anything. + """ + if PACKAGE_FAMILY != "debian": + return + + for path, content, mode in ( + (POLKIT_RULE_PATH, POLKIT_RULE, 0o644), + (OFFLINE_UPDATE_SCRIPT, OFFLINE_UPDATE_CONTENT, 0o755), + ("/etc/systemd/system/vdi-offline-update.service", OFFLINE_UPDATE_SERVICE, 0o644), + ("/etc/systemd/system/vdi-offline-update.timer", OFFLINE_UPDATE_TIMER, 0o644), + ): + try: + os.makedirs(os.path.dirname(path), exist_ok=True) + current = None + if os.path.exists(path): + with open(path, encoding="utf-8") as handle: + current = handle.read() + if current == content: + continue + temporary = path + ".tmp" + with open(temporary, "w", encoding="utf-8") as handle: + handle.write(content) + os.chmod(temporary, mode) + os.replace(temporary, path) + logging.info("Installed %s", path) + except OSError as error: + logging.warning("Could not install %s: %s", path, error) + + # Enable the timer. `enable --now` would start it, but in a chroot there is + # no running systemd, so tolerate failure and let it come up on first boot. + if shutil.which("systemctl"): + subprocess.run(["systemctl", "daemon-reload"], check=False, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["systemctl", "enable", "vdi-offline-update.timer"], check=False, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + def _match_debian_asset(assets, package_name): """Find the release asset for ``package_name``, ignoring its version. @@ -734,6 +911,12 @@ def main(): install_desktop_shortcuts() + # Debian needs a polkit rule before a desktop user can update without a + # password, and the offline-update timer. Both are idempotent and are done + # here, not in the preseed, so existing machines pick them up on the next + # cron run - the same way configure_kde.py back-fills older installs. + install_debian_update_policy() + is_newer, release_data = is_newer_release(REPO_OWNER, REPO_NAME) if PACKAGE_FAMILY == "debian":