#!/bin/sh # ssh-login-notify installer — Discord alerts for every SSH login. # # curl -fsSL https://ssh-login-notify.pages.dev/install | sudo sh # curl -fsSL https://ssh-login-notify.pages.dev/install | sudo sh -s -- 'https://discord.com/api/webhooks/ID/TOKEN' --name 'Web 01' # curl -fsSL https://ssh-login-notify.pages.dev/install | sudo sh -s -- --uninstall # # (wget -qO- works in place of curl -fsSL) # # Asks for anything missing when run in a terminal. Options: # Discord webhook # --name NAME Name shown for this server (default: hostname) # --bot-name NAME Name the message is posted as (default: SSH Monitor) # --avatar URL Avatar image for the poster # --mention TEXT Ping on each alert: @here, <@USER_ID>, <@&ROLE_ID> # --ignore-users "a b" Accounts to not alert on # --ignore-ips "1.2.3.4" IPs to not alert on # --ipinfo-token TOKEN ipinfo.io token for higher lookup limits # -y, --yes Don't ask anything (use defaults / existing settings) # --no-test Don't send a test message after installing # --uninstall Remove ssh-login-notify from this machine # # Re-running keeps existing settings and only changes what you pass. set -eu BASE_URL=${SSH_NOTIFY_BASE_URL:-https://ssh-login-notify.pages.dev} BIN=/usr/local/sbin/ssh-login-notify CONF=/etc/ssh-login-notify.conf say() { printf '\033[1m==>\033[0m %s\n' "$*"; } die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; } have() { command -v "$1" >/dev/null 2>&1; } usage() { cat <, <@&ROLE_ID> --ignore-users "a b" Accounts to not alert on --ignore-ips "1.2.3.4" IPs to not alert on --ipinfo-token TOKEN ipinfo.io token for higher lookup limits -y, --yes Don't ask anything --no-test Don't send a test message --uninstall Remove it EOF } fetch() { # if have curl; then curl -fsSL --max-time 30 -o "$2" "$1" elif have wget; then wget -qO "$2" -T 30 "$1" else die "needs curl or wget"; fi } webhook_ok() { # GET on a webhook returns its JSON (with "token") when it's valid if have curl; then r=$(printf 'url = "%s"\n' "$1" | curl -fsS --max-time 10 -K - 2>/dev/null) || return 1 else r=$(wget -qO- -T 10 "$1" 2>/dev/null) || return 1; fi case "$r" in *'"token"'*) return 0 ;; esac return 1 } ask() { # -> answer on stdout (from the terminal, since stdin is this script) printf '%s' "$1" > /dev/tty IFS= read -r ans < /dev/tty || ans="" printf '%s' "$ans" } conf_has() { grep -q "^$1='..*'" "$CONF" 2>/dev/null; } # setting exists and isn't blank # Split our own flags from settings, which are passed straight to the installer. mode=install test=1 interactive=1 url="" got_name=0 got_mention=0 want_val="" for a do shift if [ -n "$want_val" ]; then [ "$want_val" = webhook ] && url=$a want_val=""; set -- "$@" "$a"; continue fi case "$a" in --uninstall) mode=uninstall; continue ;; --no-test) test=0; continue ;; -y|--yes) interactive=0; continue ;; -h|--help) usage; exit 0 ;; https://*) url=$a ;; --webhook=*) url=${a#*=} ;; --webhook) want_val=webhook ;; --name=*|--server-name=*) got_name=1 ;; --name|--server-name) got_name=1; want_val=x ;; --mention=*|--ping=*) got_mention=1 ;; --mention|--ping) got_mention=1; want_val=x ;; --bot-name|--avatar|--ignore-users|--ignore-ips|--ipinfo-token) want_val=x ;; --*=*) ;; *) die "unknown argument: $a (try --help)" ;; esac set -- "$@" "$a" done [ -z "$want_val" ] || die "missing a value after the last option" ( exec < /dev/tty ) 2>/dev/null || interactive=0 # subshell: a failed redirect can't kill us [ "$(id -u)" = 0 ] || die "run as root — pipe into 'sudo sh' (e.g. curl -fsSL $BASE_URL/install | sudo sh)" if [ $mode = uninstall ]; then [ -x "$BIN" ] || die "ssh-login-notify isn't installed" "$BIN" uninstall exit 0 fi have curl || have wget || die "needs curl or wget — install one, then re-run" have sshd || [ -x /usr/sbin/sshd ] || [ -x /usr/local/sbin/sshd ] || die "OpenSSH server (sshd) not found" fresh=1; conf_has WEBHOOK_URL && fresh=0 host=$(hostname 2>/dev/null || uname -n) if [ -z "$url" ] && [ $fresh = 1 ]; then [ $interactive = 1 ] || die "pass the webhook URL: ... | sudo sh -s -- 'https://discord.com/api/webhooks/...'" url=$(ask 'Discord webhook URL: ' | tr -d '[:space:]') [ -n "$url" ] || die "no webhook URL given" set -- "$@" "$url" fi if [ -n "$url" ]; then say "Checking webhook" webhook_ok "$url" || die "Discord didn't accept that webhook URL (deleted, mistyped, or no network?)" fi if [ $interactive = 1 ]; then if [ $got_name = 0 ] && ! conf_has SERVER_NAME; then n=$(ask "Name for this server [$host]: ") [ -n "$n" ] && set -- "$@" --name "$n" fi if [ $got_mention = 0 ] && [ $fresh = 1 ]; then m=$(ask 'Ping on each alert? (@here, <@&ROLE_ID>, blank for none): ') [ -n "$m" ] && set -- "$@" --mention "$m" fi fi tmp=$(mktemp -d 2>/dev/null || mktemp -d -t ssh-login-notify) trap 'rm -rf "$tmp"' EXIT INT TERM say "Downloading ssh-login-notify" fetch "$BASE_URL/ssh-login-notify.sh" "$tmp/ssh-login-notify.sh" \ || die "couldn't download $BASE_URL/ssh-login-notify.sh" head -n 1 "$tmp/ssh-login-notify.sh" | grep -q '^#!/bin/sh' \ && grep -q 'ssh-login-notify' "$tmp/ssh-login-notify.sh" \ || die "download looks wrong (got an HTML page or a captive portal?)" say "Installing" sh "$tmp/ssh-login-notify.sh" install "$@" if [ $test = 1 ]; then say "Sending a test message" "$BIN" test || die "installed, but the test message failed — check $CONF" fi say "All set. Change settings any time with: sudo ssh-login-notify set name \"New name\""