install-ngxblocker: use getopts not getopt

long options for the command line are removed. getopt is replaced with
the newer getopts (which is compatible with Debian's DASH Shell)

* fixes https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/issues/35

* adds sanity checks for user command line switches

* adds -v command line option to print blacklist version
This commit is contained in:
Stuart Cardall 2017-04-26 15:53:31 +00:00
parent 6147522b7e
commit f67776d8a9

View file

@ -41,20 +41,34 @@ REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blo
usage() {
local script=$(basename $0)
cat <<EOF
$script: download Nginx Bad Bot Blocker configuration to: [ $CONF_DIR ] [ $BOTS_DIR ]
$script: INSTALL Nginx Bad Bot Blocker configuration to: [ $CONF_DIR ] [ $BOTS_DIR ]
Usage: $script [OPTIONS]
[ -b | --bots ] : Bot rules directory (default: $BOTS_DIR)
[ -c | --conf ] : NGINX conf directory (default: $CONF_DIR)
[ -r | --repo ] : Change repo url (default: $REPO)
[ -x | --exec ] : Actually change the files (default: don't change anything)
[ -h | --help ] : this help message
[ -b ] : Bot rules directory (default: $BOTS_DIR)
[ -c ] : NGINX conf directory (default: $CONF_DIR)
[ -r ] : Change repo url (default: $REPO)
[ -x ] : Actually change the files (default: don't change anything)
[ -v ] : Print blacklist version
[ -h ] : this help message
Examples:
$script (Don't change anything: display results on stdout)
$script -x (Download / update config files)
EOF
return 0
exit 0
}
check_version() {
local file=$CONF_DIR/globalblacklist.conf
if [ -f $file ]; then
grep Version $file
grep 'Updated:' $file
else
printf "Missing '$file' (pass -c \$path before -v)\n"
fi
exit 0
}
longest_str() {
@ -127,25 +141,56 @@ check_config() {
done
}
get_options() {
local options=$(getopt -o b:c:r:hx --long \
bots:,conf:,repo:,help,exec -- "$@" 2>/dev/null)
sanitize_path() {
echo $1 |tr -cd '[:alnum:] [=@=] [=.=] [=-=] [=/=] [=_=]' \
|tr -s '@.-/_' |awk '{print tolower($0)}'
}
if [ $? -ne 0 ]; then
usage
sanitize_url() {
echo $1 |tr -cd '[:alnum:] [=:=] [=.=] [=-=] [=/=]' \
|tr -s ':.-' |awk '{print tolower($0)}'
}
check_args() {
local option=$1 type=$2 arg=$3
local msg="ERROR: option '-$option' argument '$arg' requires:"
case "$type" in
path) if ! echo $arg | grep ^/ 1>/dev/null; then
printf "$msg absolute path.\n"
exit 1
fi
;;
url) if ! echo $arg | grep -E ^http[s]?://[0-9a-zA-Z-]+[.]+[/0-9a-zA-Z.]+ 1>/dev/null; then
printf "$msg url => http[s]://the.url\n"
exit 1
fi
;;
none) printf "$msg argument.\n"; exit 1;;
esac
}
eval set -- "$options"
get_options() {
local arg= opts=
while :; do
case "$1" in
-h | --help) usage && exit 1;;
-x | --exec) DRY_RUN=N; shift;;
-b | --bots) BOTS_DIR=$2; shift 2;;
-c | --conf) CONF_DIR=$2; shift 2;;
-r | --repo) REPO=$2; shift 2;;
*) break;;
while getopts :b:c:r:xvh opts "$@"
do
if [ -n "${OPTARG}" ]; then
case "$opts" in
r) arg=$(sanitize_url ${OPTARG});;
*) arg=$(sanitize_path ${OPTARG});;
esac
fi
case "$opts" in
b) BOTS_DIR=$arg; check_args $opts path $arg ;;
c) CONF_DIR=$arg; check_args $opts path $arg ;;
r) REPO=$arg; check_args $opts url $arg ;;
x) DRY_RUN=N ;;
v) check_version ;;
h) usage ;;
\?) usage ;;
:) check_args $OPTARG none none ;;
esac
done
}