~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to utopic/etc/init.d/fail2ban

  • Committer: Dimitri John Ledkov
  • Date: 2014-11-19 12:58:41 UTC
  • Revision ID: dimitri.j.ledkov@intel.com-20141119125841-98dr37roy8dvcv3b
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /bin/sh
2
 
### BEGIN INIT INFO
3
 
# Provides:          fail2ban
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
9
 
# Default-Stop:      0 1 6
10
 
# Short-Description: Start/stop fail2ban
11
 
# Description:       Start/stop fail2ban, a daemon scanning the log files and
12
 
#                    banning potential attackers.
13
 
### END INIT INFO
14
 
 
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
20
 
#
21
 
PATH=/usr/sbin:/usr/bin:/sbin:/bin
22
 
DESC="authentication failure monitor"
23
 
NAME=fail2ban
24
 
 
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
29
 
 
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'
34
 
 
35
 
# Exit if the package is not installed
36
 
[ -x "$DAEMON" ] || exit 0
37
 
 
38
 
# Run as root by default.
39
 
FAIL2BAN_USER=root
40
 
 
41
 
# Read configuration variable file if it is present
42
 
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
43
 
DAEMON_ARGS="$FAIL2BAN_OPTS"
44
 
 
45
 
# Load the VERBOSE setting and other rcS variables
46
 
[ -f /etc/default/rcS ] && . /etc/default/rcS
47
 
 
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
50
 
log_daemon_msg () {
51
 
        [ -z "$1" ] && return 1
52
 
        echo -n "$1:"
53
 
        [ -z "$2" ] || echo -n " $2"
54
 
}
55
 
 
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
59
 
#  so we must be ok
60
 
. /lib/lsb/init-functions
61
 
 
62
 
#
63
 
# Shortcut function for abnormal init script interruption
64
 
#
65
 
report_bug()
66
 
{
67
 
        echo $*
68
 
        echo "Please submit a bug report to Debian BTS (reportbug fail2ban)"
69
 
        exit 1
70
 
}
71
 
 
72
 
#
73
 
# Helper function to check if socket is present, which is often left after
74
 
# abnormal exit of fail2ban and needs to be removed
75
 
#
76
 
check_socket()
77
 
{
78
 
        # Return
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
86
 
        return 0
87
 
}
88
 
 
89
 
#
90
 
# Function that starts the daemon/service
91
 
#
92
 
do_start()
93
 
{
94
 
        # Return
95
 
        #       0 if daemon has been started
96
 
        #       1 if daemon was already running
97
 
        #       2 if daemon could not be started
98
 
        do_status && return 1
99
 
 
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" \
104
 
                        || return 2
105
 
                DAEMON_ARGS="$DAEMON_ARGS -x"
106
 
        fi
107
 
 
108
 
        # Assure that /var/run/fail2ban exists
109
 
        [ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban
110
 
 
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" {} \;
119
 
        fi
120
 
 
121
 
        start-stop-daemon --start --quiet --chuid "$FAIL2BAN_USER" --exec $DAEMON -- \
122
 
                $DAEMON_ARGS start > /dev/null\
123
 
                || return 2
124
 
 
125
 
        return 0
126
 
}
127
 
 
128
 
 
129
 
#
130
 
# Function that checks the status of fail2ban and returns
131
 
# corresponding code
132
 
#
133
 
do_status()
134
 
{
135
 
        $DAEMON ping > /dev/null 2>&1
136
 
        return $?
137
 
}
138
 
 
139
 
#
140
 
# Function that stops the daemon/service
141
 
#
142
 
do_stop()
143
 
{
144
 
        # Return
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
151
 
 
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
155
 
        count=1
156
 
        while do_status && [ $count -lt 60 ]; do
157
 
                sleep 1
158
 
                count=$(($count+1))
159
 
        done
160
 
        [ $count -lt 60 ] || return 3 # failed to stop
161
 
 
162
 
        return 0
163
 
}
164
 
 
165
 
#
166
 
# Function to reload configuration
167
 
#
168
 
do_reload() {
169
 
        $DAEMON reload > /dev/null && return 0 || return 1
170
 
        return 0
171
 
}
172
 
 
173
 
# yoh:
174
 
# shortcut function to don't duplicate case statements and to don't use
175
 
# bashisms (arrays). Fixes #368218
176
 
#
177
 
log_end_msg_wrapper()
178
 
{
179
 
        if [ "$3" != "no" ]; then
180
 
                [ $1 -lt $2 ] && value=0 || value=1
181
 
                log_end_msg $value
182
 
        fi
183
 
}
184
 
 
185
 
command="$1"
186
 
case "$command" in
187
 
        start|force-start)
188
 
                [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
189
 
                do_start "$command"
190
 
                log_end_msg_wrapper $? 2 "$VERBOSE"
191
 
                ;;
192
 
 
193
 
        stop)
194
 
                [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
195
 
                do_stop
196
 
                log_end_msg_wrapper $? 2 "$VERBOSE"
197
 
                ;;
198
 
 
199
 
        restart|force-reload)
200
 
                log_daemon_msg "Restarting $DESC" "$NAME"
201
 
                do_stop
202
 
                case "$?" in
203
 
                        0|1)
204
 
                                do_start
205
 
                                log_end_msg_wrapper $? 1 "always"
206
 
                                ;;
207
 
                        *)
208
 
                                # Failed to stop
209
 
                                log_end_msg 1
210
 
                                ;;
211
 
                esac
212
 
                ;;
213
 
 
214
 
        reload|force-reload)
215
 
        log_daemon_msg "Reloading $DESC" "$NAME"
216
 
        do_reload
217
 
        log_end_msg $?
218
 
        ;;
219
 
 
220
 
        status)
221
 
                log_daemon_msg "Status of $DESC"
222
 
                do_status
223
 
                case $? in
224
 
                        0)  log_success_msg " $NAME is running" ;;
225
 
                        255)
226
 
                                check_socket
227
 
                                case $? in
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 ;;
233
 
                                esac
234
 
                                ;;
235
 
                        *)  report_bug "Unknown $NAME status code" && exit 4
236
 
                esac
237
 
                ;;
238
 
        *)
239
 
                echo "Usage: $SCRIPTNAME {start|force-start|stop|restart|force-reload|status}" >&2
240
 
                exit 3
241
 
                ;;
242
 
esac
243
 
 
244
 
: