#!/bin/sh
# Thyra — partner verification. Checks that the Thyra installed on this Mac is a genuine, unmodified official build
# and that it is actually running. Uses macOS's own tools, so a modified copy of Thyra can't fake the result.
#
# IMPORTANT: get this script from https://kathisma.app/verify/verify-install.sh yourself (not from the Mac being
# checked, and not from the person being monitored), then run:   sh verify-install.sh
#
#   sh verify-install.sh                 checks the installed app (/Applications/Thyra.app; older installs:
#                                        /Applications/Vigil.app) and the programs running from it
#   sh verify-install.sh /path/X.app     checks only that copy's signature (nothing about what is running)
OFFICIAL_TEAMS="6ZN3DK886B"   # every team whose signature counts as official, space-separated
APP_ID="app.vigil.Vigil"       # internal identifiers kept from before the rename
DAEMON_ID="app.vigil.daemon"
AGENT_ID="app.vigil.agent"

teams=""
for t in $OFFICIAL_TEAMS; do teams="${teams:+$teams or }certificate leaf[subject.OU] = \"$t\""; done
req() { printf 'identifier "%s" and anchor apple generic and (%s)' "$1" "$teams"; }
fail=0
ok()   { printf '  PASS  %s\n' "$1"; }
bad()  { printf '  FAIL  %s\n' "$1"; fail=1; }
note() { printf '  NOTE  %s\n' "$1"; }

only_copy=0
if [ $# -gt 0 ]; then
    APP=${1%/}; only_copy=1
elif [ -d /Applications/Thyra.app ]; then
    APP=/Applications/Thyra.app
elif [ -d /Applications/Vigil.app ]; then
    APP=/Applications/Vigil.app   # installed before the rename to Thyra
else
    APP=/Applications/Thyra.app
fi
case "$APP" in
    */Vigil.app) DAEMON=vigild; AGENT=VigilAgent ;;
    *)           DAEMON=thyrad; AGENT=ThyraAgent ;;
esac

echo "Thyra verification (official signing team: $OFFICIAL_TEAMS)"
echo

echo "1. Is the app a genuine official build?"
if [ ! -d "$APP" ]; then
    bad "Thyra is not installed at $APP (older installs are at /Applications/Vigil.app)"
else
    [ "$APP" = /Applications/Vigil.app ] && note "This Mac has an install from before the rename (Vigil.app); its next update renames it to Thyra.app."
    if /usr/bin/codesign --verify --deep --strict -R="$(req "$APP_ID")" "$APP" 2>/dev/null; then
        ok "Signed by the official Thyra team and not modified since (app, capture agent and background service)"
    else
        bad "Not an official build, or it was changed after signing"
        /usr/bin/codesign -dv "$APP" 2>&1 | grep -E 'Identifier|TeamIdentifier|Signature' | sed 's/^/        /'
    fi
    version=$(/usr/libexec/PlistBuddy -c 'Print CFBundleShortVersionString' "$APP/Contents/Info.plist" 2>/dev/null)
    build=$(/usr/libexec/PlistBuddy -c 'Print CFBundleVersion' "$APP/Contents/Info.plist" 2>/dev/null)
    note "Version: ${version:-?} (build ${build:-?}) at $APP"
fi
echo

if [ "$only_copy" -eq 0 ]; then
    echo "2. Are the running programs the genuine ones?"
    check_process() { # name, expected path, signing identifier, expected user ("root" or "")
        pid=$(/usr/bin/pgrep -f "^$2" | head -1)
        if [ -z "$pid" ]; then bad "$1 is not running"; return; fi
        user=$(/bin/ps -o user= -p "$pid" | tr -d ' ')
        if [ -n "$4" ] && [ "$user" != "$4" ]; then bad "$1 is running as $user, expected $4"; return; fi
        # Verify the running process itself (by process ID), not just the file on disk.
        if /usr/bin/codesign --verify -R="$(req "$3")" "$pid" 2>/dev/null; then ok "$1 is running (as $user) and is the genuine program"
        else bad "$1 is running but is NOT the genuine program"; fi
    }
    check_process "Background service ($DAEMON)" "$APP/Contents/MacOS/$DAEMON" "$DAEMON_ID" root
    check_process "Screen capture agent" "$APP/Contents/Helpers/$AGENT.app/Contents/MacOS/$AGENT" "$AGENT_ID" ""
    echo

    echo "3. Will it start by itself?"
    for p in /Library/LaunchDaemons/app.vigil.daemon.plist /Library/LaunchAgents/app.vigil.agent.plist; do
        if [ ! -f "$p" ]; then bad "Missing $p"; continue; fi
        prog=$(/usr/libexec/PlistBuddy -c 'Print :ProgramArguments:0' "$p" 2>/dev/null)
        owner=$(/usr/bin/stat -f %Su "$p")
        case "$prog" in "$APP"/*) [ "$owner" = root ] && ok "$(basename "$p") starts the genuine program" || bad "$(basename "$p") is owned by $owner, expected root" ;;
            *) bad "$(basename "$p") starts something else: $prog" ;; esac
    done
    echo

    echo "4. Things only a person can check"
    note "Click the eye in the menu bar: it should say Monitoring, with a recent Last capture time."
    note "Use 'Request uninstall code' once: the code email must arrive in YOUR inbox."
    note "Reports should come to an address only you can read."
    echo
else
    note "Only this copy's signature was checked (not an installed, running Thyra)."
    echo
fi
if [ "$fail" -eq 0 ]; then
    if [ "$only_copy" -eq 0 ]; then echo "RESULT: GENUINE — this Mac is running an official, unmodified Thyra."
    else echo "RESULT: GENUINE — $APP is an official, unmodified Thyra build."; fi
else echo "RESULT: NOT VERIFIED — see the FAIL lines above. Reinstall from https://kathisma.app yourself."; fi
exit "$fail"
