1
1
mirror of https://github.com/docker-mailserver/docker-mailserver synced 2024-12-18 14:04:17 +01:00
docker-mailserver/target/bin/fail2ban
Nils Höll 85603193a2
feat(setup): Add fail2ban sub-command status <JAIL> (#3455)
* Added status command to fail2ban setup script

* Switched to `printf` for command output

Co-authored-by: Casper <casperklein@users.noreply.github.com>

* Update docs/content/config/security/fail2ban.md

Co-authored-by: Casper <casperklein@users.noreply.github.com>

---------

Co-authored-by: Casper <casperklein@users.noreply.github.com>
2023-08-02 12:09:01 +12:00

92 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
function __usage() {
echo "Usage: ./setup.sh fail2ban [<ban|unban> <IP>]"
echo " ./setup.sh fail2ban log"
echo " ./setup.sh fail2ban status"
}
fail2ban-client ping &>/dev/null || _exit_with_error "Fail2ban not running"
unset JAILS
declare -a JAILS
for LIST in $(fail2ban-client status | grep "Jail list" | cut -f2- | sed 's/,/ /g'); do
JAILS+=("${LIST}")
done
if [[ -z ${1} ]]; then
IPS_BANNED=0
for JAIL in "${JAILS[@]}"; do
BANNED_IPS=$(fail2ban-client status "${JAIL}" | grep -oP '(?<=Banned IP list:\s).+')
if [[ -n ${BANNED_IPS} ]]; then
echo "Banned in ${JAIL}: ${BANNED_IPS}"
IPS_BANNED=1
fi
done
[[ ${IPS_BANNED} -eq 0 ]] && _log 'info' "No IPs have been banned"
else
case "${1}" in
( 'help' ) __usage ; exit ;;
( 'ban' )
shift
if [[ -n ${1} ]]; then
RESULT=$(fail2ban-client set custom banip "${@}")
if [[ ${RESULT} -gt 0 ]]; then
echo "Banned custom IP: ${RESULT}"
else
_log 'error' "Banning '${*}' failed. Already banned?"
fi
else
_log 'warn' "You need to specify an IP address: Run './setup.sh fail2ban ban <IP>'"
exit 0
fi
;;
( 'unban' )
shift
if [[ -n ${1} ]]; then
for JAIL in "${JAILS[@]}"; do
RESULT=$(fail2ban-client set "${JAIL}" unbanip "${@}" 2>&1)
[[ ${RESULT} != *"is not banned"* ]] && [[ ${RESULT} != *"NOK"* ]] && echo "Unbanned IP from ${JAIL}: ${RESULT}"
done
else
_log 'warn' "You need to specify an IP address: Run './setup.sh fail2ban' to get a list of banned IP addresses"
exit 0
fi
;;
( 'log' )
cat /var/log/mail/fail2ban.log
;;
( 'status' )
for JAIL in "${JAILS[@]}"; do
printf '%s\n\n' "$(fail2ban-client status "${JAIL}" 2>&1)"
done
;;
( * )
__usage
_exit_with_error "Unknown command '${1}'"
;;
esac
fi
exit 0