select which transport to use by default

This commit is contained in:
2025-10-23 15:17:44 +03:00
parent 4448764bca
commit 62e2ea74be
3 changed files with 65 additions and 9 deletions

View File

@@ -91,9 +91,47 @@ else
exit 1
fi
# Ensure required config variables are set
if [[ -z "$EMAIL_RECIPIENT" || -z "$EMAIL_API_ENDPOINT" || -z "$TELEGRAM_BOT_TOKEN" || -z "$TELEGRAM_CHAT_ID" || -z "$LOG_FILE" ]]; then
ERR_MSG="ssh-notify Error: One or more required variables are missing in $CONFIG_FILE."
# Set default for PAM_TRANSPORTS if not specified
if [[ -z "$PAM_TRANSPORTS" ]]; then
PAM_TRANSPORTS="telegram email"
fi
# Validate and parse PAM_TRANSPORTS
ENABLE_EMAIL=false
ENABLE_TELEGRAM=false
for transport in $PAM_TRANSPORTS; do
case "$transport" in
email)
ENABLE_EMAIL=true
;;
telegram)
ENABLE_TELEGRAM=true
;;
*)
ERR_MSG="ssh-notify Warning: Unknown transport '$transport' in PAM_TRANSPORTS. Valid options: email, telegram"
echo "$ERR_MSG" | systemd-cat -p warning -t 'ssh-notify'
echo "$ERR_MSG" >&2
;;
esac
done
# Ensure required config variables are set based on enabled transports
if [[ "$ENABLE_EMAIL" == true && ( -z "$EMAIL_RECIPIENT" || -z "$EMAIL_API_ENDPOINT" ) ]]; then
ERR_MSG="ssh-notify Error: Email transport enabled but EMAIL_RECIPIENT or EMAIL_API_ENDPOINT missing in $CONFIG_FILE."
echo "$ERR_MSG" | systemd-cat -p err -t 'ssh-notify'
echo "$ERR_MSG" >&2
exit 1
fi
if [[ "$ENABLE_TELEGRAM" == true && ( -z "$TELEGRAM_BOT_TOKEN" || -z "$TELEGRAM_CHAT_ID" ) ]]; then
ERR_MSG="ssh-notify Error: Telegram transport enabled but TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID missing in $CONFIG_FILE."
echo "$ERR_MSG" | systemd-cat -p err -t 'ssh-notify'
echo "$ERR_MSG" >&2
exit 1
fi
if [[ -z "$LOG_FILE" ]]; then
ERR_MSG="ssh-notify Error: LOG_FILE missing in $CONFIG_FILE."
echo "$ERR_MSG" | systemd-cat -p err -t 'ssh-notify'
echo "$ERR_MSG" >&2
exit 1
@@ -162,8 +200,8 @@ send_telegram() {
[[ "$TEST_TYPE" == "both" || "$TEST_TYPE" == "email" ]] && send_email
[[ "$TEST_TYPE" == "both" || "$TEST_TYPE" == "telegram" ]] && send_telegram
else
send_email
send_telegram
[[ "$ENABLE_EMAIL" == true ]] && send_email
[[ "$ENABLE_TELEGRAM" == true ]] && send_telegram
fi
) &