Files
ssh-login-notifier/install.sh
Patrick Asmus 6c2a6dc12b Initial Commit
2026-04-26 21:05:42 +02:00

171 lines
4.9 KiB
Bash

#!/bin/bash
# SSH Login Notifier - Installationsscript
# https://git.techniverse.net/scriptos/ssh-login-notifier
set -e
INSTALL_DIR="/opt/ssh-login-notifier"
PROFILE_HOOK="/etc/profile.d/ssh-login-notify.sh"
ZSH_HOOK_MARKER="# SSH Login Notifier"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# --- Hilfsfunktionen ---
info() { echo -e "\e[32m[INFO]\e[0m $1"; }
warn() { echo -e "\e[33m[WARN]\e[0m $1"; }
error() { echo -e "\e[31m[ERROR]\e[0m $1"; exit 1; }
check_root() {
if [[ $EUID -ne 0 ]]; then
error "Dieses Script muss als root ausgefuehrt werden (sudo)."
fi
}
check_os() {
if [[ ! -f /etc/os-release ]]; then
error "Betriebssystem nicht erkannt. Nur Debian/Ubuntu werden unterstuetzt."
fi
. /etc/os-release
case "$ID" in
debian|ubuntu) info "Erkanntes OS: ${PRETTY_NAME}" ;;
*) warn "Ungetestetes OS: ${PRETTY_NAME} - Installation wird fortgesetzt." ;;
esac
}
check_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64) info "Architektur: x64 (amd64)" ;;
aarch64) info "Architektur: ARM64 (aarch64)" ;;
armv7l) info "Architektur: ARMv7 (armhf)" ;;
*) warn "Ungetestete Architektur: ${arch}" ;;
esac
}
check_dependencies() {
local missing=()
if ! command -v curl &>/dev/null; then
missing+=("curl")
fi
if ! command -v mail &>/dev/null && ! command -v sendmail &>/dev/null && ! command -v msmtp &>/dev/null; then
warn "Kein Mail-Tool gefunden (mail, sendmail, msmtp)."
warn "E-Mail-Benachrichtigungen funktionieren erst nach Installation eines Mail-Tools."
fi
if [[ ${#missing[@]} -gt 0 ]]; then
info "Installiere fehlende Abhaengigkeiten: ${missing[*]}"
apt-get update -qq
apt-get install -y -qq "${missing[@]}"
fi
}
# --- Installation ---
install_files() {
info "Erstelle Installationsverzeichnis: ${INSTALL_DIR}"
mkdir -p "$INSTALL_DIR"
info "Kopiere Dateien..."
cp "${SCRIPT_DIR}/ssh-login-notify.sh" "${INSTALL_DIR}/ssh-login-notify.sh"
cp "${SCRIPT_DIR}/uninstall.sh" "${INSTALL_DIR}/uninstall.sh"
if [[ -f "${INSTALL_DIR}/config.conf" ]]; then
warn "Bestehende config.conf gefunden - wird nicht ueberschrieben."
info "Neue Konfiguration als config.conf.new abgelegt."
cp "${SCRIPT_DIR}/config.conf" "${INSTALL_DIR}/config.conf.new"
else
cp "${SCRIPT_DIR}/config.conf" "${INSTALL_DIR}/config.conf"
fi
chmod 755 "${INSTALL_DIR}/ssh-login-notify.sh"
chmod 755 "${INSTALL_DIR}/uninstall.sh"
chmod 644 "${INSTALL_DIR}/config.conf"
chown root:root "${INSTALL_DIR}" -R
info "Dateien installiert nach ${INSTALL_DIR}"
}
install_profile_hook() {
if [[ -f "$PROFILE_HOOK" ]]; then
info "Profile-Hook ist bereits vorhanden."
return
fi
info "Erstelle Profile-Hook: ${PROFILE_HOOK}"
cat > "$PROFILE_HOOK" << 'HOOKEOF'
# SSH Login Notifier - wird bei interaktivem Login ausgefuehrt
# Installiert von: https://git.techniverse.net/scriptos/ssh-login-notifier
[ -n "$SSH_CONNECTION" ] && [ -x /opt/ssh-login-notifier/ssh-login-notify.sh ] && /opt/ssh-login-notifier/ssh-login-notify.sh
HOOKEOF
chmod 644 "$PROFILE_HOOK"
info "Profile-Hook aktiviert (Bash, Sh)."
}
install_zsh_hook() {
local zshrc="/etc/zsh/zshrc"
if [[ ! -f "$zshrc" ]]; then
info "Keine /etc/zsh/zshrc gefunden - Zsh-Hook wird uebersprungen."
return
fi
if grep -qF "$ZSH_HOOK_MARKER" "$zshrc" 2>/dev/null; then
info "Zsh-Hook ist bereits in ${zshrc} vorhanden."
return
fi
info "Fuege Zsh-Hook in ${zshrc} ein..."
cat >> "$zshrc" << 'HOOKEOF'
# SSH Login Notifier
# Installiert von: https://git.techniverse.net/scriptos/ssh-login-notifier
[ -n "$SSH_CONNECTION" ] && [ -x /opt/ssh-login-notifier/ssh-login-notify.sh ] && /opt/ssh-login-notifier/ssh-login-notify.sh
HOOKEOF
info "Zsh-Hook aktiviert."
}
print_summary() {
echo ""
echo "=========================================="
echo " SSH Login Notifier - Installation"
echo "=========================================="
echo ""
echo " Status: Installiert und aktiv"
echo " Version: 1.0.0"
echo " Pfad: ${INSTALL_DIR}"
echo " Konfig: ${INSTALL_DIR}/config.conf"
echo " Hook: ${PROFILE_HOOK}"
echo ""
echo " Naechste Schritte:"
echo " 1. Konfiguration anpassen:"
echo " sudo nano ${INSTALL_DIR}/config.conf"
echo ""
echo " 2. Test durch SSH-Login auf diesen Server"
echo ""
echo " Deinstallation:"
echo " sudo ${INSTALL_DIR}/uninstall.sh"
echo ""
echo "=========================================="
}
# --- Hauptprogramm ---
echo ""
echo "SSH Login Notifier - Installation"
echo "================================="
echo ""
check_root
check_os
check_arch
check_dependencies
install_files
install_profile_hook
install_zsh_hook
print_summary