testsAndMisc/linux_configuration/scripts/periodic_background/lib/services_browser.sh
Krzysztof kuhy Rudnicki 9085a4e508
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
check_and_enable_services: split into lib/ under the 250-line cap
1225 lines down to 202, with the 21 functions moved into eight sourced
libraries grouped by subject. The entry script now holds only the
configuration, the argument parsing and the order the checks run in.

check_hosts was the one function that could not move verbatim: at 260 lines
it would have stayed over the cap on its own, so its repair half is now
hosts_fix_nsswitch / hosts_fix_resolved / hosts_repair_all in
services_hosts_fix.sh. Repair order is preserved (resolver config, then the
hosts file, then the guard-lib migration), and hosts_repair_all returns
non-zero under --dry-run so a dry run can no longer promote a status to "ok"
on the strength of repairs that never happened.

SERVICE_STATUS was a write-only global across five libs, which is a real
SC2034 rather than noise: the pre-commit hook runs shellcheck with no -x and
genuinely cannot see the one reader. It now goes through set_service_status /
get_service_status, which also rejects a duplicate write — each check owns
exactly one summary row, so a second write means two checks are fighting over
it. No suppression was added anywhere.

Proof the behaviour is unchanged: `--status --dry-run` run as root against
this machine is byte-identical to the same command from a worktree at the
pre-split revision, modulo the Date: line. verify_shell_split.sh reports
check_hosts as the single expected difference; the other 20 functions hash
identically. 72 assertions across three test files pass, with 100% line
coverage on services_common.sh (54/54) and services_report.sh (27/27).

services_units.sh measures 84.51% (60/71). The 11 uncovered lines are the
else-branches of [[ -f /etc/systemd/system/*.service ]] and
[[ -f /usr/local/bin/*.sh ]] probes — absolute root-owned paths that exist on
this machine and that no PATH shim can redirect. Reaching them needs a
root-prefix rewrite of live enforcement logic, which is not a line-count fix
and would forfeit the verbatim-move proof for everything else.

Found while testing, deliberately NOT fixed here: in all three unit checks
the is-active branch assigns status="warning" unconditionally, overwriting
the status="error" the is-enabled branch set just above it. A timer that is
both disabled and inactive therefore never gets repaired, because
report_and_fix only repairs on "error". It is identical in the pre-split
file, so this split neither caused nor hides it. The tests pin the current
behaviour and name it; fixing it changes enforcement and earns its own commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F3PzGc75gBcdxP3PkHXGcV
2026-08-21 13:47:05 +02:00

166 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# lib/services_browser.sh — checks whose subject is a browser policy, a VM
# hosts-enforcement marker, or the per-user workout locker service.
#
# Sourced by check_and_enable_services.sh. check_workout_locker queries a USER
# unit from root and must go through user_systemctl (see its comment) rather
# than `sudo -u ... systemctl --user`.
check_guest_mode_removal() {
header "Chromium Guest Mode Removal"
local status="ok"
local issues=()
# Check if managed policy files exist for any browser
local policy_found=false
for policy_dir in \
/etc/chromium/policies/managed \
/etc/opt/chrome/policies/managed \
/etc/thorium/policies/managed \
/etc/brave/policies/managed; do
if [[ -d $policy_dir ]] && ls "$policy_dir"/*.json &>/dev/null 2>&1; then
# Check for guest mode policy
if grep -rl 'BrowserGuestModeEnabled' "$policy_dir" &>/dev/null 2>&1; then
msg "Guest mode policy found in $policy_dir"
policy_found=true
fi
fi
done
if [[ $policy_found == false ]]; then
# Only flag as issue if a Chromium browser is actually installed
if command -v thorium-browser &>/dev/null || command -v chromium &>/dev/null || command -v google-chrome &>/dev/null || command -v brave-browser &>/dev/null; then
issues+=("No guest mode removal policies found for installed browsers")
status="error"
else
note "No Chromium-based browsers detected, skipping"
fi
fi
if [[ $status != "ok" ]]; then
for issue in "${issues[@]}"; do
err "$issue"
done
((ISSUES_FOUND++)) || true
if [[ $STATUS_ONLY -eq 0 ]]; then
note "Removing guest mode..."
if [[ -f $REMOVE_GUEST_MODE_SCRIPT ]]; then
run bash "$REMOVE_GUEST_MODE_SCRIPT"
((FIXES_APPLIED++)) || true
else
err_missing_script "Script not found: $REMOVE_GUEST_MODE_SCRIPT"
fi
fi
fi
set_service_status "guest_mode_removal" "$status"
}
check_vbox_hosts() {
header "VirtualBox Hosts Enforcement"
local status="ok"
local issues=()
# Only check if VirtualBox is installed.
# This is NOT "skipped" in the sense of ducking a check: VirtualBox is absent
# AND deliberately blocked by pacman policy — pacman_blocked_keywords.txt lists
# virtualbox/vbox with the comment "can bypass /etc/hosts restrictions", which
# is the very thing this check enforces. With no VirtualBox there are no VMs to
# enforce /etc/hosts inside, so the check is genuinely not applicable. Report
# n/a so it reads as deliberate rather than something that got ducked.
if ! command -v VBoxManage &>/dev/null; then
note "VirtualBox not installed (blocked by pacman policy) — not applicable"
set_service_status "vbox_hosts" "n/a"
return
fi
# Check if enforcement marker exists
if [[ -f /var/lib/vbox-hosts-enforced ]]; then
msg "VirtualBox hosts enforcement marker exists"
else
issues+=("VirtualBox hosts enforcement not applied")
status="error"
fi
if [[ $status != "ok" ]]; then
for issue in "${issues[@]}"; do
err "$issue"
done
((ISSUES_FOUND++)) || true
if [[ $STATUS_ONLY -eq 0 ]]; then
note "Enforcing hosts in VirtualBox VMs..."
if [[ -f $VBOX_HOSTS_SCRIPT ]]; then
run bash "$VBOX_HOSTS_SCRIPT"
((FIXES_APPLIED++)) || true
else
err_missing_script "Script not found: $VBOX_HOSTS_SCRIPT"
fi
fi
fi
set_service_status "vbox_hosts" "$status"
}
check_workout_locker() {
header "Workout Lock Screen"
local status="ok"
local issues=()
local user="${SUDO_USER:-$USER}"
# Check screen_lock.py script exists
if [[ -f $WORKOUT_LOCKER_SCRIPT ]]; then
msg "screen_lock.py exists at $WORKOUT_LOCKER_SCRIPT"
else
issues+=("screen_lock.py not found at $WORKOUT_LOCKER_SCRIPT")
status="error"
fi
# Check user service is enabled
if user_systemctl "$user" is-enabled workout-locker.service &>/dev/null 2>&1; then
msg "workout-locker.service is enabled (user service)"
else
issues+=("workout-locker.service is not enabled (user service)")
status="error"
fi
# Check user service is active (advisory — it only runs at login)
if user_systemctl "$user" is-active workout-locker.service &>/dev/null 2>&1; then
msg "workout-locker.service is active"
else
issues+=("workout-locker.service is not active (expected at login time)")
if [[ $status != "error" ]]; then status="warning"; fi
fi
if [[ $status != "ok" ]]; then
for issue in "${issues[@]}"; do
if [[ $status == "error" ]]; then
err "$issue"
else
warn "$issue"
fi
done
((ISSUES_FOUND++)) || true
if [[ $STATUS_ONLY -eq 0 && $status == "error" ]]; then
note "Installing workout locker service..."
if [[ -f $WORKOUT_LOCKER_INSTALL_SCRIPT ]]; then
run sudo -u "$user" bash "$WORKOUT_LOCKER_INSTALL_SCRIPT"
((FIXES_APPLIED++)) || true
# Re-verify
if [[ $DRY_RUN -eq 0 ]] && user_systemctl "$user" is-enabled workout-locker.service &>/dev/null 2>&1; then
status="ok"
fi
else
err_missing_script "Install script not found: $WORKOUT_LOCKER_INSTALL_SCRIPT"
fi
fi
fi
set_service_status "workout_locker" "$status"
}