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() {
LANG=C fail2ban-client status 2> /dev/null | sed -n -e 's/.*Jail list:\s\+//' -e 'T' -e 's/,\s\+/\'$'\n/g' -e 'p'
_fail2ban_client() {
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=(
'unban:unbans all IP addresses'
'set:set property'
@ -58,14 +79,14 @@ _complete_f2bc_cmds() {
_describe -V "fail2ban commands" cmds
}
_complete_f2bc_cmdargs() {
_fail2ban_client_command_arguments() {
local f2barg="$words[$NORMARG]"
case "$f2barg" in
unban)
(unban)
local jail
if (( $words[(I)(--all)] == 0 )) ; then
for jail in $(_f2bc_jails) ; do
_complete_f2bc_ips $jail
if (( $words[(I)(--all)] == 0 )); then
for jail in $(_fail2ban_client_print_jails); do
_fail2ban_client_jails "$jail"
done
local unban_opts=(--all)
_describe -o "unban options" unban_opts
@ -73,18 +94,18 @@ _complete_f2bc_cmdargs() {
_nothing
fi
;;
(set|get))
if (( $NORMARG + 1 == $CURRENT )) ; then
_complete_f2bc_jails
_complete_f2bc_settings
(set|get)
if (( $NORMARG + 1 == $CURRENT )); then
_fail2ban_client_jails
_fail2ban_client_settings
else
_complete_f2bc_jail${f2barg}
_fail2ban_client_jail_${f2barg}
fi
;;
status)
if (( $NORMARG + 1 == $CURRENT )) ; then
_complete_f2bc_jails
elif (( $NORMARG + 2 == $CURRENT )) ; then
(status)
if (( $NORMARG + 1 == $CURRENT )); then
_fail2ban_client_jails
elif (( $NORMARG + 2 == $CURRENT )); then
_values "flavor" basic cymru
else
_nothing
@ -93,69 +114,40 @@ _complete_f2bc_cmdargs() {
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
_fail2ban_client_jail_set() {
if (( $NORMARG + 2 == $CURRENT )); then
case $words[$NORMARG+1] in
loglevel)
(loglevel)
local loglevel=(CRITICAL ERROR WARNING NOTICE INFO DEBUG TRACEDEBUG HEAVYDEBUG)
_describe -V "loglevel" loglevel ;;
logtarget)
_describe -V "loglevel" loglevel
;;
(logtarget)
local logtarget=(STDOUT STDERR SYSLOG)
_describe -V "logtarget" logtarget
_files ;;
syslogsocket)
_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 ;;
_files
;;
(dbfile)
_files
;;
(dbpurgeage)
_message "sets the max age in <SECONDS> that history of bans will be kept"
;;
(*)
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]"
@ -163,11 +155,14 @@ _complete_f2bc_jailset() {
if (( $NORMARG + 3 == $CURRENT )) ; then
case $words[$NORMARG+2] in
unbanip)
_complete_f2bc_ips "$jail" ;;
_fail2ban_client_ips "$jail"
;;
delfailregex)
_complete_f2bc_regex fail "$jail" ;;
_fail2ban_client_regexs fail "$jail"
;;
delignorerexgex)
_complete_f2bc_regex ignore "$jail" ;;
_fail2ban_client_regexs 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')}")
@ -175,45 +170,56 @@ _complete_f2bc_jailset() {
_describe -t "f2b_filelist" -V "filelist of jail $1" filelist
else
_nothing
fi ;;
fi
;;
idle)
_values 'fail2ban idle' on off ;;
_values 'fail2ban idle' on off
;;
ignoreself)
_values 'fail2ban ignoreself' true false ;;
_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
fi
;;
delaction|action)
_fail2ban_client_actions "$jail"
;;
addlogpath)
_files
;;
*)
_message "No completion for ${words[NORMARG+2]}"
;;
esac
elif (( $NORMARG + 4 == $CURRENT )) ; then
case $words[$NORMARG+2] in
action)
_fail2ban_client_actionproperties "$jail" $words[$NORMARG+3]
;;
addaction)
_files
;;
*)
_nothing
;;
esac
else
_nothing
fi
fi
}
_complete_f2bc_jailget() {
_fail2ban_client_jail_get() {
if (( $NORMARG + 2 == $CURRENT )) ; then
case $words[$NORMARG+1] in
(loglevel|logtarget|syslogsocket|dbfile|dbpurgeage))
_nothing ;;
(loglevel|logtarget|syslogsocket|dbfile|dbpurgeage)
_nothing
;;
*)
# jail
local jailprops=(
@ -236,24 +242,29 @@ _complete_f2bc_jailget() {
actionproperties
actionmethods
)
_describe -t "f2b_jail_props" -V "jail properties" jailprops ;;
_describe -t "f2b_jail_props" -V "jail properties" jailprops
;;
esac
else
local jail="$words[$NORMARG+1]"
if (( $NORMARG + 3 == $CURRENT )) ; then
if (( $NORMARG + 3 == $CURRENT )); then
case $words[$NORMARG+2] in
(action|actionproperties|actionmethods))
_complete_f2bc_action "$jail" ;;
(action|actionproperties|actionmethods)
_fail2ban_client_actions "$jail"
;;
*)
_nothing ;;
_nothing
;;
esac
elif (( $NORMARG + 4 == $CURRENT )) ; then
elif (( $NORMARG + 4 == $CURRENT )); then
case $words[$NORMARG+2] in
(action|actionproperties|actionmethods))
_complete_f2bc_actionproperties "$jail" $words[$NORMARG+3] ;;
(action|actionproperties|actionmethods)
_fail2ban_client_actionproperties "$jail" $words[$NORMARG+3]
;;
*)
_nothing ;;
_nothing
;;
esac
else
_nothing
@ -261,7 +272,21 @@ _complete_f2bc_jailget() {
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')}")
if [[ -n "${jailactions[@]}" ]] ; then
@ -271,8 +296,22 @@ _complete_f2bc_action() {
fi
}
_complete_f2bc_actionproperties() {
local default_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=(
actionstart
actionstop
actioncheck
@ -292,43 +331,11 @@ _complete_f2bc_actionproperties() {
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
_fail2ban_client_print_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_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'
_fail2ban_client "$@"
# Local Variables:
# mode: Shell-Script