fix fail2ban-client

- fix wrong switch syntax
- rename function names according to the convention
This commit is contained in:
Shohei YOSHIDA 2026-03-08 22:15:05 +09:00
parent ce1145e8ad
commit 4a9df2bdc8
No known key found for this signature in database
GPG Key ID: C9A1BB11BB940CF2
1 changed files with 155 additions and 148 deletions

View File

@ -35,11 +35,32 @@
# #
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
_f2bc_jails() { _fail2ban_client() {
LANG=C fail2ban-client status 2> /dev/null | sed -n -e 's/.*Jail list:\s\+//' -e 'T' -e 's/,\s\+/\'$'\n/g' -e 'p' integer NORMARG
_arguments -A "-*" -n \
'-c[configuration directory]:dir:_files -/' \
'-s[socket path]:file:_files' \
'-p[pidfile path]:file:_files' \
'--loglevel[logging level]: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:_fail2ban_client_commands' \
'*:fail2ban command argument:_fail2ban_client_command_arguments'
} }
_complete_f2bc_cmds() { _fail2ban_client_commands() {
local cmds=( local cmds=(
'unban:unbans all IP addresses' 'unban:unbans all IP addresses'
'set:set property' 'set:set property'
@ -58,14 +79,14 @@ _complete_f2bc_cmds() {
_describe -V "fail2ban commands" cmds _describe -V "fail2ban commands" cmds
} }
_complete_f2bc_cmdargs() { _fail2ban_client_command_arguments() {
local f2barg="$words[$NORMARG]" local f2barg="$words[$NORMARG]"
case "$f2barg" in case "$f2barg" in
unban) (unban)
local jail local jail
if (( $words[(I)(--all)] == 0 )) ; then if (( $words[(I)(--all)] == 0 )); then
for jail in $(_f2bc_jails) ; do for jail in $(_fail2ban_client_print_jails); do
_complete_f2bc_ips $jail _fail2ban_client_jails "$jail"
done done
local unban_opts=(--all) local unban_opts=(--all)
_describe -o "unban options" unban_opts _describe -o "unban options" unban_opts
@ -73,18 +94,18 @@ _complete_f2bc_cmdargs() {
_nothing _nothing
fi fi
;; ;;
(set|get)) (set|get)
if (( $NORMARG + 1 == $CURRENT )) ; then if (( $NORMARG + 1 == $CURRENT )); then
_complete_f2bc_jails _fail2ban_client_jails
_complete_f2bc_settings _fail2ban_client_settings
else else
_complete_f2bc_jail${f2barg} _fail2ban_client_jail_${f2barg}
fi fi
;; ;;
status) (status)
if (( $NORMARG + 1 == $CURRENT )) ; then if (( $NORMARG + 1 == $CURRENT )); then
_complete_f2bc_jails _fail2ban_client_jails
elif (( $NORMARG + 2 == $CURRENT )) ; then elif (( $NORMARG + 2 == $CURRENT )); then
_values "flavor" basic cymru _values "flavor" basic cymru
else else
_nothing _nothing
@ -93,69 +114,40 @@ _complete_f2bc_cmdargs() {
esac esac
} }
_complete_f2bc_jails() { _fail2ban_client_jail_set() {
local jails=($(_f2bc_jails)) if (( $NORMARG + 2 == $CURRENT )); then
_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 case $words[$NORMARG+1] in
loglevel) (loglevel)
local loglevel=(CRITICAL ERROR WARNING NOTICE INFO DEBUG TRACEDEBUG HEAVYDEBUG) local loglevel=(CRITICAL ERROR WARNING NOTICE INFO DEBUG TRACEDEBUG HEAVYDEBUG)
_describe -V "loglevel" loglevel ;; _describe -V "loglevel" loglevel
logtarget) ;;
(logtarget)
local logtarget=(STDOUT STDERR SYSLOG) local logtarget=(STDOUT STDERR SYSLOG)
_describe -V "logtarget" logtarget _describe -V "logtarget" logtarget
_files ;; _files
syslogsocket) ;;
(syslogsocket)
local syslogsocket=(auto) local syslogsocket=(auto)
_describe -V "logtarget" syslogsocket _describe -V "logtarget" syslogsocket
_files ;; _files
dbfile) ;;
_files ;; (dbfile)
dbpurgeage) _files
_message "sets the max age in <SECONDS> that history of bans will be kept" ;; ;;
*) (dbpurgeage)
# jail _message "sets the max age in <SECONDS> that history of bans will be kept"
;;
(*)
local jailsettings=( local jailsettings=(
unbanip unbanip banip action addaction addfailregex addignoreip
banip addignoreregex addjournalmatch addlogpath bantime
action datepattern delaction delfailregex delignoreip
addaction delignorerexgex deljournalmatch dellogpath findtime idle
addfailregex ignorecache ignorecommand ignoreself logencoding maxlines
addignoreip maxretry usedns
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 ;; _describe -t "f2b_jail_setting" -V "jail setting" jailsettings
;;
esac esac
else else
local jail="$words[$NORMARG+1]" local jail="$words[$NORMARG+1]"
@ -163,11 +155,14 @@ _complete_f2bc_jailset() {
if (( $NORMARG + 3 == $CURRENT )) ; then if (( $NORMARG + 3 == $CURRENT )) ; then
case $words[$NORMARG+2] in case $words[$NORMARG+2] in
unbanip) unbanip)
_complete_f2bc_ips "$jail" ;; _fail2ban_client_ips "$jail"
;;
delfailregex) delfailregex)
_complete_f2bc_regex fail "$jail" ;; _fail2ban_client_regexs fail "$jail"
;;
delignorerexgex) delignorerexgex)
_complete_f2bc_regex ignore "$jail" ;; _fail2ban_client_regexs ignore "$jail"
;;
dellogpath) 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')}") 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')}")
@ -175,33 +170,43 @@ _complete_f2bc_jailset() {
_describe -t "f2b_filelist" -V "filelist of jail $1" filelist _describe -t "f2b_filelist" -V "filelist of jail $1" filelist
else else
_nothing _nothing
fi ;; fi
;;
idle) idle)
_values 'fail2ban idle' on off ;; _values 'fail2ban idle' on off
;;
ignoreself) ignoreself)
_values 'fail2ban ignoreself' true false ;; _values 'fail2ban ignoreself' true false
;;
delignoreip) delignoreip)
local ignoreips=("${(@f)$(fail2ban-client get "$jail" ignoreip 2> /dev/null | sed -e 's/^[|`]-\s\+//p')}") local ignoreips=("${(@f)$(fail2ban-client get "$jail" ignoreip 2> /dev/null | sed -e 's/^[|`]-\s\+//p')}")
if [[ -n "${ignoreips[@]}" ]] ; then if [[ -n "${ignoreips[@]}" ]] ; then
_describe -t "f2b_ignoreip" -V "fail2ban ignored ips" ignoreips _describe -t "f2b_ignoreip" -V "fail2ban ignored ips" ignoreips
else else
_nothing _nothing
fi ;; fi
;;
delaction|action) delaction|action)
_complete_f2bc_action "$jail" ;; _fail2ban_client_actions "$jail"
;;
addlogpath) addlogpath)
_files ;; _files
;;
*) *)
_message "No completion for ${words[NORMARG+2]}" ;; _message "No completion for ${words[NORMARG+2]}"
;;
esac esac
elif (( $NORMARG + 4 == $CURRENT )) ; then elif (( $NORMARG + 4 == $CURRENT )) ; then
case $words[$NORMARG+2] in case $words[$NORMARG+2] in
action) action)
_complete_f2bc_actionproperties "$jail" $words[$NORMARG+3] ;; _fail2ban_client_actionproperties "$jail" $words[$NORMARG+3]
;;
addaction) addaction)
_files ;; _files
;;
*) *)
_nothing ;; _nothing
;;
esac esac
else else
_nothing _nothing
@ -209,11 +214,12 @@ _complete_f2bc_jailset() {
fi fi
} }
_complete_f2bc_jailget() { _fail2ban_client_jail_get() {
if (( $NORMARG + 2 == $CURRENT )) ; then if (( $NORMARG + 2 == $CURRENT )) ; then
case $words[$NORMARG+1] in case $words[$NORMARG+1] in
(loglevel|logtarget|syslogsocket|dbfile|dbpurgeage)) (loglevel|logtarget|syslogsocket|dbfile|dbpurgeage)
_nothing ;; _nothing
;;
*) *)
# jail # jail
local jailprops=( local jailprops=(
@ -236,24 +242,29 @@ _complete_f2bc_jailget() {
actionproperties actionproperties
actionmethods actionmethods
) )
_describe -t "f2b_jail_props" -V "jail properties" jailprops ;; _describe -t "f2b_jail_props" -V "jail properties" jailprops
;;
esac esac
else else
local jail="$words[$NORMARG+1]" local jail="$words[$NORMARG+1]"
if (( $NORMARG + 3 == $CURRENT )) ; then if (( $NORMARG + 3 == $CURRENT )); then
case $words[$NORMARG+2] in case $words[$NORMARG+2] in
(action|actionproperties|actionmethods)) (action|actionproperties|actionmethods)
_complete_f2bc_action "$jail" ;; _fail2ban_client_actions "$jail"
;;
*) *)
_nothing ;; _nothing
;;
esac esac
elif (( $NORMARG + 4 == $CURRENT )) ; then elif (( $NORMARG + 4 == $CURRENT )); then
case $words[$NORMARG+2] in case $words[$NORMARG+2] in
(action|actionproperties|actionmethods)) (action|actionproperties|actionmethods)
_complete_f2bc_actionproperties "$jail" $words[$NORMARG+3] ;; _fail2ban_client_actionproperties "$jail" $words[$NORMARG+3]
;;
*) *)
_nothing ;; _nothing
;;
esac esac
else else
_nothing _nothing
@ -261,7 +272,21 @@ _complete_f2bc_jailget() {
fi fi
} }
_complete_f2bc_action() { _fail2ban_client_settings() {
local setargs=(loglevel logtarget syslogsocket dbfile dbpurgeage)
_describe -t "f2b_settings" -V "fail2ban-client settings" setargs
}
_fail2ban_client_regexs() {
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
}
_fail2ban_client_actions() {
local jailactions=("${(@f)$(fail2ban-client get $1 actions 2>/dev/null | sed -e '1d' -e 's/,\s\+/\'$'\n/g')}") local jailactions=("${(@f)$(fail2ban-client get $1 actions 2>/dev/null | sed -e '1d' -e 's/,\s\+/\'$'\n/g')}")
if [[ -n "${jailactions[@]}" ]] ; then if [[ -n "${jailactions[@]}" ]] ; then
@ -271,7 +296,21 @@ _complete_f2bc_action() {
fi fi
} }
_complete_f2bc_actionproperties() { _fail2ban_client_jails() {
local jails=($(_fail2ban_client_print_jails))
_describe -V "jails" jails
}
_fail2ban_client_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
}
_fail2ban_client_actionproperties() {
local default_actionproperties=( local default_actionproperties=(
actionstart actionstart
actionstop actionstop
@ -292,43 +331,11 @@ _complete_f2bc_actionproperties() {
fi fi
} }
_complete_f2bc_regex() { _fail2ban_client_print_jails() {
local regex=("${(@f)$(fail2ban-client get $2 ${1}regex 2> /dev/null | sed -n -e 's/[|`]- \[\([0-9]\+\)\]:\s\+/\1:/p')}") LANG=C fail2ban-client status 2> /dev/null | sed -n -e 's/.*Jail list:\s\+//' -e 'T' -e 's/,\s\+/\'$'\n/g' -e 'p'
if [[ -n "${regex[@]}" ]] ; then
_describe -t "f2b_regex" -V "jail $2 ${1}regex" regex
else
_nothing
fi
} }
_complete_f2bc_settings() { _fail2ban_client "$@"
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: # Local Variables:
# mode: Shell-Script # mode: Shell-Script