diff --git a/setup.sh b/setup.sh index 26da098c..6269e9ec 100755 --- a/setup.sh +++ b/setup.sh @@ -60,6 +60,7 @@ SUBCOMMANDS: debug: $0 debug fetchmail + $0 debug fail2ban $0 debug show-mail-logs $0 debug inspect $0 debug login @@ -180,6 +181,46 @@ case $1 in fetchmail) _docker_image debug-fetchmail ;; + fail2ban) + shift + JAILS=$(_docker_container fail2ban-client status | grep "Jail list" | cut -f2- | sed 's/,//g') + if [ -z "$1" ]; then + IP_COUNT=0 + for JAIL in $JAILS; do + BANNED_IP=$(_docker_container iptables -L f2b-$JAIL -n | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -v '0.0.0.0') + if [ -n "$BANNED_IP" ]; then + BANNED_IP=$(echo $BANNED_IP | sed -e 's/\n/,/g') + echo "Banned in $JAIL: $BANNED_IP" + IP_COUNT=$((IP_COUNT+1)) + fi + done + if [ "$IP_COUNT" -eq 0 ]; then + echo "No IPs have been banned" + fi + else + case $1 in + unban) + shift + if [ -n "$1" ]; then + for JAIL in $JAILS; do + RESULT=`_docker_container fail2ban-client set $JAIL unbanip $@` + case "$RESULT" in + *"is not banned"*) ;; + *"NOK"*) ;; + *) echo -n "unbanned IP from $JAIL: " + echo "$RESULT";; + esac + done + else + echo "You need to specify an IP address. Run \"./setup.sh debug fail2ban\" to get a list of banned IP addresses." + fi + ;; + *) + _usage + ;; + esac + fi + ;; show-mail-logs) _docker_container cat /var/log/mail/mail.log ;; diff --git a/test/tests.bats b/test/tests.bats index d6cc26be..ddd5ac04 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -1131,6 +1131,21 @@ load 'test_helper/bats-assert/load' run ./setup.sh -c mail debug login ls assert_success } +@test "checking setup.sh: setup.sh debug fail2ban" { + + run docker exec mail_fail2ban /bin/sh -c "fail2ban-client set dovecot banip 192.0.66.4" + run docker exec mail_fail2ban /bin/sh -c "fail2ban-client set dovecot banip 192.0.66.5" + sleep 10 + run ./setup.sh -c mail_fail2ban debug fail2ban + assert_output "Banned in dovecot: 192.0.66.5 192.0.66.4" + run ./setup.sh -c mail_fail2ban debug fail2ban unban 192.0.66.4 + assert_output --partial "unbanned IP from dovecot: 192.0.66.4" + run ./setup.sh -c mail_fail2ban debug fail2ban + assert_output "Banned in dovecot: 192.0.66.5" + run ./setup.sh -c mail_fail2ban debug fail2ban unban 192.0.66.5 + run ./setup.sh -c mail_fail2ban debug fail2ban unban + assert_output --partial "You need to specify an IP address. Run" +} # # LDAP