ops-jrz1/scripts/killswitch
Dan f8e77c44b1 Fix code review items from security scripts
- egress-watchdog: Use process substitution to avoid subshell gotcha
- killswitch: Rename USER to TARGET_USER (avoid shadowing builtin)
- Add documentation comments for UID range and grep -P dependency

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-03 09:35:40 -08:00

39 lines
902 B
Bash
Executable file

#!/run/current-system/sw/bin/bash
# killswitch - Immediately terminate all processes for a user
# Usage: killswitch <username> [reason]
set -euo pipefail
# NixOS paths
PATH="/run/current-system/sw/bin:$PATH"
if [ $# -lt 1 ]; then
echo "Usage: killswitch <username> [reason]" >&2
exit 1
fi
TARGET_USER="$1"
REASON="${2:-manual kill}"
if ! id "$TARGET_USER" &>/dev/null; then
echo "User not found: $TARGET_USER" >&2
exit 1
fi
# Don't allow killing root or system users
UID_NUM=$(id -u "$TARGET_USER")
if [ "$UID_NUM" -lt 1000 ]; then
echo "Refusing to kill system user: $TARGET_USER (uid $UID_NUM)" >&2
exit 1
fi
logger -t killswitch "Killing all processes for $TARGET_USER: $REASON"
# Kill all processes
pkill -u "$TARGET_USER" 2>/dev/null || true
# Terminate login session
loginctl terminate-user "$TARGET_USER" 2>/dev/null || true
echo "Killed $TARGET_USER: $REASON"