4
# Required-Start: $local_fs $remote_fs
5
# Required-Stop: $local_fs $remote_fs
6
# Should-Start: $time $network $syslog iptables firehol shorewall ipmasq arno-iptables-firewall iptables-persistent ferm
7
# Should-Stop: $network $syslog iptables firehol shorewall ipmasq arno-iptables-firewall iptables-persistent ferm
8
# Default-Start: 2 3 4 5
10
# Short-Description: Start/stop fail2ban
11
# Description: Start/stop fail2ban, a daemon scanning the log files and
12
# banning potential attackers.
15
# Author: Aaron Isotton <aaron@isotton.com>
16
# Modified: by Yaroslav Halchenko <debian@onerussian.com>
17
# reindented + minor corrections + to work on sarge without modifications
18
# Modified: by Glenn Aaldering <glenn@openvideo.nl>
19
# added exit codes for status command
21
PATH=/usr/sbin:/usr/bin:/sbin:/bin
22
DESC="authentication failure monitor"
25
# fail2ban-client is not a daemon itself but starts a daemon and
26
# loads its with configuration
27
DAEMON=/usr/bin/$NAME-client
28
SCRIPTNAME=/etc/init.d/$NAME
30
# Ad-hoc way to parse out socket file name
31
SOCKFILE=`grep -h '^[^#]*socket *=' /etc/$NAME/$NAME.conf /etc/$NAME/$NAME.local 2>/dev/null \
32
| tail -n 1 | sed -e 's/.*socket *= *//g' -e 's/ *$//g'`
33
[ -z "$SOCKFILE" ] && SOCKFILE='/tmp/fail2ban.sock'
35
# Exit if the package is not installed
36
[ -x "$DAEMON" ] || exit 0
38
# Run as root by default.
41
# Read configuration variable file if it is present
42
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
43
DAEMON_ARGS="$FAIL2BAN_OPTS"
45
# Load the VERBOSE setting and other rcS variables
46
[ -f /etc/default/rcS ] && . /etc/default/rcS
48
# Predefine what can be missing from lsb source later on -- necessary to run
49
# on sarge. Just present it in a bit more compact way from what was shipped
51
[ -z "$1" ] && return 1
53
[ -z "$2" ] || echo -n " $2"
56
# Define LSB log_* functions.
57
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
58
# Actually has to (>=2.0-7) present in sarge. log_daemon_msg is predefined
60
. /lib/lsb/init-functions
63
# Shortcut function for abnormal init script interruption
68
echo "Please submit a bug report to Debian BTS (reportbug fail2ban)"
73
# Helper function to check if socket is present, which is often left after
74
# abnormal exit of fail2ban and needs to be removed
79
# 0 if socket is present and readable
80
# 1 if socket file is not present
81
# 2 if socket file is present but not readable
82
# 3 if socket file is present but is not a socket
83
[ -e "$SOCKFILE" ] || return 1
84
[ -r "$SOCKFILE" ] || return 2
85
[ -S "$SOCKFILE" ] || return 3
90
# Function that starts the daemon/service
95
# 0 if daemon has been started
96
# 1 if daemon was already running
97
# 2 if daemon could not be started
100
if [ -e "$SOCKFILE" ]; then
101
log_failure_msg "Socket file $SOCKFILE is present"
102
[ "$1" = "force-start" ] \
103
&& log_success_msg "Starting anyway as requested" \
105
DAEMON_ARGS="$DAEMON_ARGS -x"
108
# Assure that /var/run/fail2ban exists
109
[ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban
111
if [ "$FAIL2BAN_USER" != "root" ]; then
112
# Make the socket directory, IP lists and fail2ban log
113
# files writable by fail2ban
114
chown "$FAIL2BAN_USER" /var/run/fail2ban
115
# Create the logfile if it doesn't exist
116
touch /var/log/fail2ban.log
117
chown "$FAIL2BAN_USER" /var/log/fail2ban.log
118
find /proc/net/xt_recent -name 'fail2ban-*' -exec chown "$FAIL2BAN_USER" {} \;
121
start-stop-daemon --start --quiet --chuid "$FAIL2BAN_USER" --exec $DAEMON -- \
122
$DAEMON_ARGS start > /dev/null\
130
# Function that checks the status of fail2ban and returns
135
$DAEMON ping > /dev/null 2>&1
140
# Function that stops the daemon/service
145
# 0 if daemon has been stopped
146
# 1 if daemon was already stopped
147
# 2 if daemon could not be stopped
148
# other if a failure occurred
149
$DAEMON status > /dev/null 2>&1 || return 1
150
$DAEMON stop > /dev/null || return 2
152
# now we need actually to wait a bit since it might take time
153
# for server to react on client's stop request. Especially
154
# important for restart command on slow boxes
156
while do_status && [ $count -lt 60 ]; do
160
[ $count -lt 60 ] || return 3 # failed to stop
166
# Function to reload configuration
169
$DAEMON reload > /dev/null && return 0 || return 1
174
# shortcut function to don't duplicate case statements and to don't use
175
# bashisms (arrays). Fixes #368218
177
log_end_msg_wrapper()
179
if [ "$3" != "no" ]; then
180
[ $1 -lt $2 ] && value=0 || value=1
188
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
190
log_end_msg_wrapper $? 2 "$VERBOSE"
194
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
196
log_end_msg_wrapper $? 2 "$VERBOSE"
199
restart|force-reload)
200
log_daemon_msg "Restarting $DESC" "$NAME"
205
log_end_msg_wrapper $? 1 "always"
215
log_daemon_msg "Reloading $DESC" "$NAME"
221
log_daemon_msg "Status of $DESC"
224
0) log_success_msg " $NAME is running" ;;
228
1) log_failure_msg " $NAME is not running" && exit 3 ;;
229
0) log_failure_msg " $NAME is not running but $SOCKFILE exists" && exit 3 ;;
230
2) log_failure_msg " $SOCKFILE not readable, status of $NAME is unknown" && exit 3 ;;
231
3) log_failure_msg " $SOCKFILE exists but not a socket, status of $NAME is unknown" && exit 3 ;;
232
*) report_bug "Unknown return code from $NAME:check_socket." && exit 4 ;;
235
*) report_bug "Unknown $NAME status code" && exit 4
239
echo "Usage: $SCRIPTNAME {start|force-start|stop|restart|force-reload|status}" >&2