From 312ee8bdb567bce45b0da6ee9ce5bb3c8ed605c4 Mon Sep 17 00:00:00 2001 From: Stuart Cardall Date: Wed, 26 Apr 2017 15:56:40 +0000 Subject: [PATCH] update-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 --- update-ngxblocker | 111 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 83 insertions(+), 28 deletions(-) diff --git a/update-ngxblocker b/update-ngxblocker index cc3e78896..fa62ca648 100755 --- a/update-ngxblocker +++ b/update-ngxblocker @@ -35,22 +35,36 @@ CONF_DIR=/etc/nginx/conf.d ##### end user configuration ############################################################## usage() { - local script=$(basename $0) - cat </dev/null; then + printf "$msg absolute path.\n" + exit 1 + fi + ;; + email) if ! echo $arg | grep -E ^[-_[:alnum:]]+@[-_[:alnum:]]+[\.][\.a-z]+ 1>/dev/null; then + printf "$msg email@domain.com\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 +} get_options() { - local options=$(getopt -o c:r:e:nh --long \ - bots:,conf:,repo:,email:,no-email,help,exec -- "$@" 2>/dev/null) + local arg= opts= - if [ $? -ne 0 ]; then - usage - exit 1 - fi + while getopts :c:r:e:nvh opts "$@" + do + if [ -n "${OPTARG}" ]; then + case "$opts" in + r) arg=$(sanitize_url ${OPTARG});; + e) arg=$(sanitize_email ${OPTARG});; + *) arg=$(sanitize_path ${OPTARG});; + esac + fi - eval set -- "$options" - - while :; do - case "$1" in - -h | --help) usage && exit 1;; - -c | --conf) CONF_DIR=$2; shift 2;; - -r | --repo) REPO=$2; shift 2;; - -e | --email) EMAIL=$2; shift 2;; - -n | --no-email) SEND_EMAIL=N; shift 2;; - *) break;; - esac - done + case "$opts" in + c) CONF_DIR=$arg; check_args $opts path $arg ;; + r) REPO=$arg; check_args $opts url $arg ;; + e) EMAIL=$arg; check_args $opts email $arg ;; + n) SEND_EMAIL=N ;; + v) check_version ;; + h) usage ;; + \?) usage ;; + :) check_args $OPTARG none none ;; + esac + done } main() {