Add *BSD compatibility (use GNU sed).

This commit is contained in:
Alexander Leidinger 2020-02-10 19:39:24 +01:00
parent db80971da3
commit 38281f1185
3 changed files with 68 additions and 27 deletions

View file

@ -38,6 +38,7 @@ SCRIPT_DIR=/usr/local/sbin
REPO=https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master
####### end user configuration ##########################
OS=$(uname -s)
usage() {
local script=$(basename $0)
@ -267,13 +268,25 @@ check_depends() {
fi
done
# give a helpful message for missing pidof
if [ -z $(find_binary pidof) ]; then
printf "$0 requires 'pidof' \n\n"
printf "In Debian: apt install sysvinit-utils\n"
printf "In Centos: yum install sysvinit-tools\n"
exit 1
fi
case $OS in
Linux)
# give a helpful message for missing pidof
if [ -z $(find_binary pidof) ]; then
printf "$0 requires 'pidof' \n\n"
printf "In Debian: apt install sysvinit-utils\n"
printf "In Centos: yum install sysvinit-tools\n"
exit 1
fi
;;
*BSD)
# give a helpful message for missing gsed
if [ -z $(find_binary gsed) ]; then
printf "$0 requires 'gsed' \n\n"
printf "In FreeBSD: 'pkg install textproc/gsed' or 'portmaster textproc/gsed'\n"
exit 1
fi
;;
esac
}
check_online() {

View file

@ -28,6 +28,7 @@ BOLDRED="\033[1m\033[31m"
BOLDYELLOW="\033[1m\033[33m"
BOLDWHITE="\033[1m\033[37m"
RESET="\033[0m"
OS=$(uname -s)
usage() {
local script=$(basename $0)
@ -71,7 +72,7 @@ update_paths() {
for x in $include_paths; do
dir=$(dirname $x)
sed -i "s|$dir|$BOTS_DIR|" $blacklist
${SED_CMD} -i "s|$dir|$BOTS_DIR|" $blacklist
done
else
printf "${BOLDRED}ERROR${RESET}: '$BOTS_DIR' does not exist => ${BOLDWHITE}running $INSTALLER${RESET}.\n"
@ -196,7 +197,7 @@ add_includes() {
printf "%-10s %-${col_size}s %s\n" "inserting:" "$text" "=> $file"
if [ "$DRY_RUN" = "N" ]; then
# $ph is just a placeholder so sed inserts a \t (tab)
sed -i "$line i $ph \t$text $ph" $file
${SED_CMD} -i "$line i $ph \t$text $ph" $file
fi
fi
done
@ -205,17 +206,17 @@ add_includes() {
if [ -n "$update" ]; then
#add blank line below inserts
line=$(( $line + $(echo $include_list | wc -w) ))
if ! sed -n "${line}p" $file | grep ^'}' 1>/dev/null; then
if ! ${SED_CMD} -n "${line}p" $file | grep ^'}' 1>/dev/null; then
text="include $conf_dir/$(echo $include_list | awk '{print $1}');"
sed -i "s|$text|$text\n|" $file
${SED_CMD} -i "s|$text|$text\n|" $file
fi
#add comment above inserts
text="include $conf_dir/$(echo $include_list | awk '{print $NF}');"
sed -i "s|$text|\n\n ##\n # Nginx Bad Bot Blocker Includes\n # REPO: https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker\n ##\n\t$text|" $file
${SED_CMD} -i "s|$text|\n\n ##\n # Nginx Bad Bot Blocker Includes\n # REPO: https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker\n ##\n\t$text|" $file
# remove placeholders
sed -i "s|$ph||g" $file
${SED_CMD} -i "s|$ph||g" $file
fi
fi
}
@ -238,7 +239,7 @@ find_includes() {
local end=$(find_line $file $end_range $end_first_last)
if [ -n "$start" ] && [ -n "$end" ]; then
sed -n "$start,$end"p $file > $tmp
${SED_CMD} -n "$start,$end"p $file > $tmp
line=$(find_line $tmp $search $search_first_last)
rm -f $tmp
fi
@ -254,9 +255,9 @@ find_includes() {
esac
# if inserting beyond the end of the stanza
if [ "$(sed -n $(( $line - 1))p $file | grep ^})" = "}" ]; then
if [ "$(${SED_CMD} -n $(( $line - 1))p $file | grep ^})" = "}" ]; then
# insert blank line
sed -i "$(( line - 1)) i \ " $file
${SED_CMD} -i "$(( line - 1)) i \ " $file
fi
echo $line
@ -310,6 +311,23 @@ check_depends() {
exit 1
fi
case $OS in
Linux)
SED_CMD=$(find_binary sed)
if [ -z ${SED_CMD} ]; then
printf "${BOLDRED}ERROR${RESET}: $0 requires: 'sed' => ${BOLDWHITE}please install sed.${RESET}\n"
exit 1
fi
;;
*BSD)
SED_CMD=$(find_binary gsed)
if [ -z ${SED_CMD} ]; then
printf "${BOLDRED}ERROR${RESET}: $0 requires: 'gsed' => ${BOLDWHITE}please install textproc/gsed.${RESET}\n"
exit 1
fi
;;
esac
# required for whitelisting public ip
if [ -z $(find_binary dig) ]; then
printf "${BOLDYELLOW}WARN${RESET}: $0 optionally requires: 'dig' => ${BOLDWHITE}cannot whitelist public ip address.${RESET}\n"
@ -332,7 +350,7 @@ check_nginx_directives() {
printf "${BOLDYELLOW}setup will fix conflict from: '$x' in $bot_config${RESET}\n"
if [ "$DRY_RUN" = "N" ]; then
printf "${BOLDRED}disabling '$x' in: $bot_config${RESET}\n"
sed -i "s/$x/#$x/g" $bot_config | grep $x
${SED_CMD} -i "s/$x/#$x/g" $bot_config | grep $x
printf " ${BOLDGREEN}disabled OK${RESET}\n\n"
fi
fi
@ -424,7 +442,7 @@ main() {
# configure ddos include
case "$INC_DDOS" in
n*|N*) VHOST_INCLUDES=$(echo $VHOST_INCLUDES | sed 's|ddos.conf||');;
n*|N*) VHOST_INCLUDES=$(echo $VHOST_INCLUDES | ${SED_CMD} 's|ddos.conf||');;
esac
# by default do not change any files

View file

@ -49,6 +49,7 @@ BOLDRED="\033[1m\033[31m"
BOLDYELLOW="\033[1m\033[33m"
BOLDWHITE="\033[1m\033[37m"
RESET="\033[0m"
OS=$(uname -s)
usage() {
local script=$(basename $0)
@ -91,14 +92,14 @@ check_version() {
if [ -f $file ]; then
# local version
version=$(grep "Version:" $file | sed 's|^.*: V||g')
date=$(grep "Updated:" $file | sed 's|^.*: ||g')
version=$(grep "Version:" $file | ${SED_CMD} 's|^.*: V||g')
date=$(grep "Updated:" $file | ${SED_CMD} 's|^.*: ||g')
print_message "\nLOCAL Version: $BOLDWHITE$version$RESET\n"
print_message "Updated: $date\n\n"
# remote version
curl -s --limit-rate 5k -r $range --location $url -o $tmp
remote_ver=$(grep "Version:" $tmp | sed 's|^.*: V||g')
remote_date=$(grep "Updated:" $tmp | sed 's|^.*: ||g')
remote_ver=$(grep "Version:" $tmp | ${SED_CMD} 's|^.*: V||g')
remote_date=$(grep "Updated:" $tmp | ${SED_CMD} 's|^.*: ||g')
print_message "REMOTE Version: $BOLDWHITE$remote_ver$RESET\n"
print_message "Updated: $remote_date\n"
rm -f $tmp
@ -154,7 +155,7 @@ update_paths() {
for x in $include_paths; do
dir=$(dirname $x)
sed -i "s|$dir|$BOTS_DIR|" $blacklist
${SED_CMD} -i "s|$dir|$BOTS_DIR|" $blacklist
done
else
printf "${BOLDRED}ERROR${RESET}: '$BOTS_DIR' does not exist => ${BOLDWHITE}running $INSTALLER${RESET}.\n"
@ -209,6 +210,15 @@ check_args() {
}
check_depends() {
case $OS in
Linux)
SED_CMD=$(find_binary sed)
;;
*BSD)
SED_CMD=$(find_binary gsed)
;;
esac
# centos does not have which by default
if [ -z $(find_binary curl) ]; then
printf "${BOLDRED}ERROR${RESET}: $0 requires: 'curl' => ${BOLDWHITE}cannot check remote version.${RESET}\n"
@ -236,9 +246,9 @@ log_output() {
if [ -n "$logger" ]; then
# remove ansi color codes
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
${SED_CMD} -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
# remove blank lines
sed -i '/^\s*$/d' $EMAIL_REPORT
${SED_CMD} -i '/^\s*$/d' $EMAIL_REPORT
# log output
$logger -t $script -f $EMAIL_REPORT 2>&1
print_message "Output logged to syslog\n";
@ -252,7 +262,7 @@ send_email() {
if [ -n $(find_binary mail) ]; then
print_message "Emailing report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
# remove ansi colour codes
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
${SED_CMD} -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
cat $EMAIL_REPORT | mail -s "Nginx Bad Bot Blocker Updated" $EMAIL
else
print_message "${BOLDYELLOW}WARN${RESET}: missing mail command => ${BOLDWHITE}disabling emails${RESET}.\n\n"
@ -263,7 +273,7 @@ send_email_via_mailgun() {
local report= subject= endpoint="https://api.mailgun.net/v3/$MG_DOMAIN/messages"
echo "Mailgunning report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
${SED_CMD} -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
report="$(cat $EMAIL_REPORT)"
subject='Nginx Bad Bot Blocker Updated'