File: fail2ban.init

package info (click to toggle)
fail2ban 0.7.5-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 620 kB
  • ctags: 754
  • sloc: python: 3,245; sh: 735; makefile: 43
file content (196 lines) | stat: -rw-r--r-- 4,601 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /bin/sh
### BEGIN INIT INFO
# Provides:          fail2ban
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Should-Start:      $time $network $syslog iptables firehol shorewall ipmasq
# Should-Stop:       $network $syslog iptables firehol shorewall ipmasq
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop fail2ban
# Description:       Start/stop fail2ban, a daemon scanning the log files and
#                    banning potential attackers.
### END INIT INFO

# Author: Aaron Isotton <aaron@isotton.com>
# Modified: by Yaroslav Halchenko <debian@onerussian.com>
#  reindented + minor corrections + to work on sarge without modifications
#
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="authentication failure monitor"
NAME=fail2ban

# fail2ban-client is not a daemon itself but starts a daemon and
# loads its with configuration
DAEMON=/usr/bin/$NAME-client
SOCKFILE=/tmp/$NAME.sock
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
DAEMON_ARGS="$FAIL2BAN_OPTS"

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Predefine what can be missing from lsb source later on -- necessary to run
# on sarge. Just present it in a bit more compact way from what was shipped
log_daemon_msg () {
	[ -z "$1" ] && return 1
	echo -n "$1:"
	[ -z "$2" ] || echo -n " $2"
}

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
# Actually has to (>=2.0-7) present in sarge. log_daemon_msg is predefined
#  so we must be ok
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#	0 if daemon has been started
	#	1 if daemon was already running
	#	2 if daemon could not be started
	do_status && return 1
	start-stop-daemon --start --quiet --chuid root --exec $DAEMON -- \
		$DAEMON_ARGS start > /dev/null\
		|| return 2
}

#
# Shortcut function for abnormal init script interruption
#
report_bug()
{
	echo $*
	echo "Please submit a bug report to Debian BTS (reportbug fail2ban)"
	exit 1
}

#
# Function that checks the status of fail2ban and returns
# corresponding code
#
do_status()
{
	$DAEMON status > /dev/null
	case $? in
		0) return 0
			;;
		255)
			if [ -S $SOCKFILE ]; then
				if [ -r $SOCKFILE ]; then
					return 1
				else
					return 4
				fi
			else
				return 3
			fi
			;;
		*)
			report_bug "Unknown return code from fail2ban."
	esac
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#	0 if daemon has been stopped
	#	1 if daemon was already stopped
	#	2 if daemon could not be stopped
	#	other if a failure occurred
	$DAEMON status > /dev/null || return 1
	$DAEMON stop > /dev/null || return 2
	return 0
}

#
# Function to reload configuration
#
do_reload() {
	$DAEMON reload > /dev/null && return 0 || return 1
	return 0
}

# yoh:
# shortcut function to don't duplicate case statements and to don't use
# bashisms (arrays). Fixes #368218
#
log_end_msg_wrapper()
{
	[ $1 -lt $2 ] && value=0 || value=1
	log_end_msg $value
}

case "$1" in
	start)
		[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
		do_start
		[ "$VERBOSE" != no ] && log_end_msg_wrapper $? 2
		;;
	stop)
		[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
		do_stop
		[ "$VERBOSE" != no ] && log_end_msg_wrapper $? 2
		;;
	restart|force-reload)
		log_daemon_msg "Restarting $DESC" "$NAME"
		do_stop
		case "$?" in
			0|1)
                # now we need actually to wait a bit since it might take time
   		        # for server to react on client's stop request
				count=1
				while do_status && [ $count -lt 10 ]; do
					sleep 1
					count=$(($count+1))
				done

				[ $count -lt 10 ] || log_end_msg 1 # failed to stop

				do_start
				log_end_msg_wrapper $? 1
				;;
			*)
				# Failed to stop
				log_end_msg 1
				;;
 		esac
		;;

	reload|force-reload)
        log_daemon_msg "Reloading $DESC" "$NAME"
        do_reload
        log_end_msg $?
        ;;

	status)
		log_daemon_msg "Status of $DESC"
		do_status
		case $? in
			0)  log_success_msg " $NAME is running" ;;
			1)  log_failure_msg " $NAME is not running but $SOCKFILE exists" ;;
			3)  log_warning_msg " $NAME is not running" ;;
			4)  log_failure_msg " $SOCKFILE not readable, status of $NAME unknown";;
			*)  report_bug "Unknown status code"
		esac
		;;
	*)
		echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
		exit 3
		;;
esac

: