This repository has been archived on 2025-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
Files
wilcon-agent/safe_kiddo_user_control.sh
2025-06-29 07:39:33 +02:00

120 lines
3.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# safe_kiddo_user_control.sh
# Deaktiviert/Aktiviert Userkonten mit optionalem Countdown, Sound und Shutdown nur bei aktivem Login
# Ausführen mit root-Rechten!
USER_TO_MANAGE="$1"
ACTION="$2"
COUNTDOWN_MODE="$3" # optional: "countdown"
SOUND_MODE="$4" # optional: "sound"
COUNTDOWN_TIME="$5" # optional: Sekundenanzahl (default 60)
if [[ -z "$COUNTDOWN_TIME" ]]; then
COUNTDOWN_TIME=60
fi
if [[ -z "$USER_TO_MANAGE" || -z "$ACTION" ]]; then
echo "Nutzung: $0 <username> <disable|enable> [countdown] [sound] [countdown_time_in_seconds]"
exit 1
fi
# Prüfe, ob notify-send verfügbar ist
if ! command -v notify-send &> /dev/null; then
echo "[WARNUNG] notify-send nicht gefunden. Bitte installiere libnotify-bin."
NOTIFY_AVAILABLE=false
else
NOTIFY_AVAILABLE=true
fi
# Prüfe, ob ein Sound-Tool verfügbar ist
if command -v paplay &> /dev/null; then
SOUND_PLAYER="paplay"
SOUND_FILE="/usr/share/sounds/freedesktop/stereo/dialog-warning.oga"
elif command -v aplay &> /dev/null;
SOUND_PLAYER="aplay"
SOUND_FILE="/usr/share/sounds/alsa/Front_Center.wav"
elif command -v canberra-gtk-play &> /dev/null; then
SOUND_PLAYER="canberra-gtk-play"
SOUND_FILE="dialog-warning"
else
SOUND_PLAYER=""
fi
function send_notify() {
local message="$1"
if [[ "$NOTIFY_AVAILABLE" == true ]]; then
sudo -u "$USER_TO_MANAGE" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u $USER_TO_MANAGE)/bus notify-send "Safe Kiddo" "$message"
else
echo "[INFO] Benachrichtigung übersprungen: $message"
fi
}
function is_user_logged_in() {
who | grep -q "^$USER_TO_MANAGE "
}
function play_beep() {
if [[ "$SOUND_MODE" == "sound" ]]; then
if [[ "$SOUND_PLAYER" == "paplay" ]]; then
paplay "$SOUND_FILE" &
elif [[ "$SOUND_PLAYER" == "aplay" ]]; then
aplay "$SOUND_FILE" &
elif [[ "$SOUND_PLAYER" == "canberra-gtk-play" ]]; then
canberra-gtk-play -i "$SOUND_FILE" -d "SafeKiddo" &
else
echo -ne '\007'
fi
fi
}
case "$ACTION" in
disable)
echo "[*] Deaktiviere Benutzer $USER_TO_MANAGE..."
sudo usermod -L "$USER_TO_MANAGE"
USER_WAS_LOGGED_IN=false
if is_user_logged_in; then
USER_WAS_LOGGED_IN=true
echo "[*] Benutzer ist eingeloggt."
if [[ "$COUNTDOWN_MODE" == "countdown" ]]; then
echo "[*] Starte dramatischen Countdown über $COUNTDOWN_TIME Sekunden..."
for ((i=COUNTDOWN_TIME; i>0; i--)); do
send_notify "ACHTUNG! Shutdown in $i Sekunden!"
play_beep
sleep 1
done
else
echo "[*] Schicke Warnung (ohne Countdown)..."
send_notify "Shutdown in $COUNTDOWN_TIME Sekunden! Speicher dein Spiel!"
echo "[*] Warten für $COUNTDOWN_TIME Sekunden..."
sleep "$COUNTDOWN_TIME"
fi
else
echo "[*] Benutzer ist NICHT eingeloggt. Countdown und Shutdown werden übersprungen."
fi
echo "[*] Benutzer abmelden (falls noch eingeloggt)..."
sudo pkill -KILL -u "$USER_TO_MANAGE" || true # || true, damit es keinen Fehler gibt, wenn der User nicht gefunden wird
if [[ "$USER_WAS_LOGGED_IN" == true ]]; then
echo "[*] Rechner wird jetzt heruntergefahren..."
sudo shutdown now
else
echo "[*] Kein aktiver Login – kein Shutdown."
fi
;;
enable)
echo "[*] Aktiviere Benutzer $USER_TO_MANAGE..."
sudo usermod -U "$USER_TO_MANAGE"
echo "[*] Benutzer $USER_TO_MANAGE kann sich wieder einloggen."
;;
*)
echo "Ungültige Aktion: $ACTION"
echo "Erlaubt sind: disable oder enable"
exit 1
;;
esac