docker-mailserver/target/bin/fail2ban

84 lines
1.7 KiB
Plaintext
Raw Normal View History

#! /bin/bash
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
if ! IPTABLES_OUTPUT=$(iptables -L -n 2>&1)
then
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
_exit_with_error "IPTables is not functioning correctly
The output of \`iptables -L\` was:
${IPTABLES_OUTPUT}
Possible causes for this error are
1. Missing capabilities (you need CAP_NET_RAW & CAP_NET_ADMIN, see \`capsh --print\`)
2. Modifications caused by user-patches.sh
3. Host is configured incorrectly
"
fi
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
function __usage { echo "Usage: ${0} [<unban> <ip-address>]" ; }
2020-11-05 11:41:18 +01:00
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
2020-11-05 11:41:18 +01:00
IP_COUNT=0
for JAIL in "${JAILS[@]}"
do
2020-11-05 11:41:18 +01:00
BANNED_IP="$(iptables -L "f2b-${JAIL}" -n 2>/dev/null | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -v '0.0.0.0')"
2020-11-05 11:41:18 +01:00
if [[ -n ${BANNED_IP} ]]
then
2020-11-05 11:41:18 +01:00
echo "Banned in ${JAIL}: ${BANNED_IP//$'\n'/, }"
IP_COUNT=$(( IP_COUNT + 1 ))
fi
done
if [[ ${IP_COUNT} -eq 0 ]]
then
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
_log 'info' 'No IPs have been banned'
fi
2020-11-05 11:41:18 +01:00
else
2020-11-05 11:41:18 +01:00
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
case "${1}" in
2020-11-05 11:41:18 +01:00
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
( 'unban' )
shift
if [[ -n ${1} ]]
then
2020-11-05 11:41:18 +01:00
for JAIL in "${JAILS[@]}"
do
2020-11-05 12:40:24 +01:00
RESULT="$(fail2ban-client set "${JAIL}" unbanip "${@}" 2>&1)"
[[ ${RESULT} != *"is not banned"* ]] && [[ ${RESULT} != *"NOK"* ]] && echo -e "Unbanned IP from ${JAIL}: ${RESULT}"
done
2020-11-05 11:41:18 +01:00
else
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
_log 'warn' "You need to specify an IP address: Run './setup.sh debug fail2ban' to get a list of banned IP addresses"
2020-11-05 11:41:18 +01:00
exit 0
fi
;;
scripts: refactored scripts located under `target/bin/` (#2500) * refactored scripts located under `target/bin/` The scripts under `target/bin/` now use the new log and I replaced some `""` with `''` on the way. The functionality stays the same, this mostly style and log. * corrected fail2ban (script and tests) * corrected OpenDKIM log output in tests * reverted (some) changes to `sedfile` Moreover, a few messages for BATS were streamlined and a regression in the linting script reverted. * apple PR feedback * improve log output from `fail2ban` script The new output has a single, clear message with the '[ ERROR ] ' prefix, and then output that explains the error afterwards. This is coherent with the logging style which should be used while providing more information than just a single line about IPTables not functioning. * simplified `setquota` script * consistently named the `__usage` function Before, scripts located under `target/bin/` were using `usage` or `__usage`. Now, they're using `__usage` as they should. * improved `sedfile` With `sedfile`, we cannot use the helper functions in a nice way because it is used early in the Dockerfile at a stage where the helper scripts are not yet copied. The script has been adjusted to be canonical with all the other scripts under `target/bin/`. * fixed tests * removed `__usage` from places where it does not belong `__usage` is to be used on wrong user input, not on other failures as well. This was fixed in `delquota` and `setquota`. * apply PR review feedback
2022-03-26 09:30:09 +01:00
( * )
__usage
_exit_with_error "Unknown command '${1}'"
;;
2020-11-05 11:41:18 +01:00
esac
fi
2020-11-05 11:41:18 +01:00
exit 0