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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#compdef fail2ban-client
# ------------------------------------------------------------------------------
# Copyright (c) 2020 Github zsh-users - https://github.com/zsh-users
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for fail2ban-client (https://www.fail2ban.org/).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Felix Neumärker <xdch47@posteo.de>
#
# ------------------------------------------------------------------------------
_f2bc_jails() {
LANG=C fail2ban-client status 2> /dev/null | sed -n -e 's/.*Jail list:\s\+//' -e 'T' -e 's/,\s\+/\'$'\n/g' -e 'p'
}
_complete_f2bc_cmds() {
local cmds=(
'unban:unbans all IP addresses'
'set:set property'
'get:get property'
'status:gets the current status of the server'
'reload:reloads the configuration/jails'
'restart:restarts the server'
'start:starts the server and the jails'
'stop:stops all jails and terminate the server'
'ping:tests if the server is alive'
'flushlogs:flushes the logtarget if a file and reopens it'
'help:return this output'
'version:return the server version'
)
_describe -V "fail2ban commands" cmds
}
_complete_f2bc_cmdargs() {
local f2barg="$words[$NORMARG]"
case "$f2barg" in
unban)
local jail
if (( $words[(I)(--all)] == 0 )) ; then
for jail in $(_f2bc_jails) ; do
_complete_f2bc_ips $jail
done
local unban_opts=(--all)
_describe -o "unban options" unban_opts
else
_nothing
fi
;;
(set|get))
if (( $NORMARG + 1 == $CURRENT )) ; then
_complete_f2bc_jails
_complete_f2bc_settings
else
_complete_f2bc_jail${f2barg}
fi
;;
status)
if (( $NORMARG + 1 == $CURRENT )) ; then
_complete_f2bc_jails
elif (( $NORMARG + 2 == $CURRENT )) ; then
_values "flavor" basic cymru
else
_nothing
fi
;;
esac
}
_complete_f2bc_jails() {
local jails=($(_f2bc_jails))
_describe -V "jails" jails
}
_complete_f2bc_ips() {
local ips=("${(@f)$(LANG=C fail2ban-client status $1 2> /dev/null | sed -n -e 's/^.*Banned IP list:\s\+//' -e 'T' -e 's/\s\+/\'$'\n/g' -e 'p')}")
if [[ -n "${ips[@]}" ]] ; then
_describe -t "f2b_jail_$1" -V "banned ips of jail $1" ips
else
_nothing
fi
}
_complete_f2bc_jailset() {
if (( $NORMARG + 2 == $CURRENT )) ; then
case $words[$NORMARG+1] in
loglevel)
local loglevel=(CRITICAL ERROR WARNING NOTICE INFO DEBUG TRACEDEBUG HEAVYDEBUG)
_describe -V "loglevel" loglevel ;;
logtarget)
local logtarget=(STDOUT STDERR SYSLOG)
_describe -V "logtarget" logtarget
_files ;;
syslogsocket)
local syslogsocket=(auto)
_describe -V "logtarget" syslogsocket
_files ;;
dbfile)
_files ;;
dbpurgeage)
_message "sets the max age in <SECONDS> that history of bans will be kept" ;;
*)
# jail
local jailsettings=(
unbanip
banip
action
addaction
addfailregex
addignoreip
addignoreregex
addjournalmatch
addlogpath
bantime
datepattern
delaction
delfailregex
delignoreip
delignorerexgex
deljournalmatch
dellogpath
findtime
idle
ignorecache
ignorecommand
ignoreself
logencoding
maxlines
maxretry
usedns
)
_describe -t "f2b_jail_setting" -V "jail setting" jailsettings ;;
esac
else
local jail="$words[$NORMARG+1]"
if (( $NORMARG + 3 == $CURRENT )) ; then
case $words[$NORMARG+2] in
unbanip)
_complete_f2bc_ips "$jail" ;;
delfailregex)
_complete_f2bc_regex fail "$jail" ;;
delignorerexgex)
_complete_f2bc_regex ignore "$jail" ;;
dellogpath)
local filelist=("${(@f)$(LANG=C fail2ban-client status $jail 2> /dev/null | sed -n -e 's/^.*File list:\s\+//' -e 'T' -e 's/\s\+/\'$'\n/g' -e 'p')}")
if [[ -n "${filelist[@]}" ]] ; then
_describe -t "f2b_filelist" -V "filelist of jail $1" filelist
else
_nothing
fi ;;
idle)
_values 'fail2ban idle' on off ;;
ignoreself)
_values 'fail2ban ignoreself' true false ;;
delignoreip)
local ignoreips=("${(@f)$(fail2ban-client get "$jail" ignoreip 2> /dev/null | sed -e 's/^[|`]-\s\+//p')}")
if [[ -n "${ignoreips[@]}" ]] ; then
_describe -t "f2b_ignoreip" -V "fail2ban ignored ips" ignoreips
else
_nothing
fi ;;
delaction|action)
_complete_f2bc_action "$jail" ;;
addlogpath)
_files ;;
*)
_message "No completion for ${words[NORMARG+2]}" ;;
esac
elif (( $NORMARG + 4 == $CURRENT )) ; then
case $words[$NORMARG+2] in
action)
_complete_f2bc_actionproperties "$jail" $words[$NORMARG+3] ;;
addaction)
_files ;;
*)
_nothing ;;
esac
else
_nothing
fi
fi
}
_complete_f2bc_jailget() {
if (( $NORMARG + 2 == $CURRENT )) ; then
case $words[$NORMARG+1] in
(loglevel|logtarget|syslogsocket|dbfile|dbpurgeage))
_nothing ;;
*)
# jail
local jailprops=(
logpath
logencoding
journalmatch
ignoreself
ignoreip
ignorecommand
failregex
ignoreregex
findtime
bantime
datepattern
usedns
maxretry
maxlines
actions
action
actionproperties
actionmethods
)
_describe -t "f2b_jail_props" -V "jail properties" jailprops ;;
esac
else
local jail="$words[$NORMARG+1]"
if (( $NORMARG + 3 == $CURRENT )) ; then
case $words[$NORMARG+2] in
(action|actionproperties|actionmethods))
_complete_f2bc_action "$jail" ;;
*)
_nothing ;;
esac
elif (( $NORMARG + 4 == $CURRENT )) ; then
case $words[$NORMARG+2] in
(action|actionproperties|actionmethods))
_complete_f2bc_actionproperties "$jail" $words[$NORMARG+3] ;;
*)
_nothing ;;
esac
else
_nothing
fi
fi
}
_complete_f2bc_action() {
local jailactions=("${(@f)$(fail2ban-client get $1 actions 2>/dev/null | sed -e '1d' -e 's/,\s\+/\'$'\n/g')}")
if [[ -n "${jailactions[@]}" ]] ; then
_describe -t "f2b_jail_actions" -V "jail actions" jailactions
else
_nothing
fi
}
_complete_f2bc_actionproperties() {
local default_actionproperties=(
actionstart
actionstop
actioncheck
actionban
actionunban
timeout
)
local all_actionproperties=("${(@f)$(fail2ban-client get $1 actionproperties $2 2>/dev/null | sed -e '1d' -e 's/,\s\+/\'$'\n/g')}")
local add_actionproperties=("${(@)all_actionproperties:|default_actionproperties}")
_describe -t "f2b_actions_defprops" -V "default action properties" default_actionproperties
if [[ -n "${add_actionproperties[@]}" ]] ; then
_describe -t "f2b_actions_remprops" -V "additional action properties" add_actionproperties
else
_nothing
fi
}
_complete_f2bc_regex() {
local regex=("${(@f)$(fail2ban-client get $2 ${1}regex 2> /dev/null | sed -n -e 's/[|`]- \[\([0-9]\+\)\]:\s\+/\1:/p')}")
if [[ -n "${regex[@]}" ]] ; then
_describe -t "f2b_regex" -V "jail $2 ${1}regex" regex
else
_nothing
fi
}
_complete_f2bc_settings() {
local setargs=(loglevel logtarget syslogsocket dbfile dbpurgeage)
_describe -t "f2b_settings" -V "fail2ban-client settings" setargs
}
integer NORMARG
_arguments -A "-*" -n \
'-c[configuration directory]:_files -/' \
'-s[socket path]:_files' \
'-p[pidfile path]:_files' \
'--loglevel[logging level]:(CRITICAL ERROR WARNING, NOTICE INFO, DEBUG, TRACEDEBUG HEAVYDEBUG)' \
'--logtarget[logging target]:(stdout stderr syslog sysout)' \
'--syslogsocket:_files' \
'-d[dump configuration]' \
'(--dp --dump-pretty)'{--dp,--dump-pretty}'[dump the configuration using more human readable representation]' \
'(-t --test)'{-t,--test}'[test configuration]' \
'-i[interactive mode]' \
'-v[increase verbosity]' \
'-q[decrease verbosity]' \
'-x[force execution of the server (remove socket file)]' \
'-b[start server in background]' \
'-f[start server in foreground]' \
'--str2sec[convert time abbreviation format to seconds]:_message str2sec' \
'(-h --help)'{-h,--help}'[display this help message]' \
'(-V --version)'{-V,--version}'[print the version]' \
'1:fail2ban command:_complete_f2bc_cmds' \
'*:fail2ban command argument:_complete_f2bc_cmdargs'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: set et sw=2 ts=2 ft=zsh:
|