From c9137d7db2cdeff3b3ddc9af584cd5be0cd86c47 Mon Sep 17 00:00:00 2001 From: James White Date: Sat, 20 Jun 2015 09:01:08 +0100 Subject: [PATCH] get-easylist.sh Version 0.2 Automatically detect various squid configuration values to be more compatible with different setups Ability to bypass check when running via cron Additional message output --- get-easylist.sh | 58 +++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/get-easylist.sh b/get-easylist.sh index 624401d..5925e39 100644 --- a/get-easylist.sh +++ b/get-easylist.sh @@ -3,7 +3,7 @@ ## get-easylist.sh ## Author: James White (james@jmwhite.co.uk) ## Description: Gets Adblock lists and converts them for use with SquidGuard -## Version: 0.1 +## Version: 0.2 ## ## Notes: ## A specific sed pattern file is required for the conversion @@ -12,7 +12,7 @@ ## SCRIPT_NAME=${0##*/} -SCRIPT_VERSION="0.1" +SCRIPT_VERSION="0.2" echo "Starting ${SCRIPT_NAME} Version: ${SCRIPT_VERSION}" @@ -21,17 +21,26 @@ if ! [ "$(id -u)" = 0 ]; then exit 1 fi +get_squid_build_flag() { + # $1: Build flag used during compile + squid -v | tr " " "\n" | grep -- "$1" | tail -n1 | cut -f2 -d '=' | sed "s/'//g" +} + get_squid_conf_value() { # $1: Squid config value # $2: Squid config filename grep -i "$1" "$2" | awk '{ print $2 }' } -# Squid/SquidGuard Configuration. -SQUID_USER="squid" -SQUID_CONF_DIR="/etc/squid" -SQUID_CONF_FILE=$(find ${SQUID_CONF_DIR} -name squid.conf) +echo "Scanning Squid and SquidGuard setup please wait..." + +# Squid/SquidGuard Configuration +SQUID_BIN=$(command -v squid squid2 squid3) +SQUID_USER=$(get_squid_build_flag "--with-default-user") +SQUID_CONF_DIR=$(get_squid_build_flag "--sysconfdir") +SQUID_CONF_FILE=$(find "${SQUID_CONF_DIR}" -name squid.conf) SQUID_LOG_FILE=$(get_squid_conf_value "access_log" "${SQUID_CONF_FILE}") +SQUIDGUARD_BIN=$(command -v squidguard squidGuard) SQUIDGUARD_CONF_FILE=$(find / -iname squidGuard.conf) SQUIDGUARD_DB_DIR=$(get_squid_conf_value "dbhome" "${SQUIDGUARD_CONF_FILE}") SQUIDGUARD_ADBLOCK_DIR="${SQUIDGUARD_DB_DIR}/adblock" @@ -45,6 +54,8 @@ show_message() { } show_message "The following paths have been detected" +echo "Squid Bin Path: ${SQUID_BIN}" +echo "Squid User: ${SQUID_USER}" echo "Squid Config Folder: ${SQUID_CONF_DIR}" echo "Squid Config File: ${SQUID_CONF_FILE}" echo "SquidGuard Config File: ${SQUIDGUARD_CONF_FILE}" @@ -52,16 +63,19 @@ echo "SquidGuard Database Folder: ${SQUIDGUARD_DB_DIR}" echo "SquidGuard Adblock Folder ${SQUIDGUARD_ADBLOCK_DIR}" echo "" -read -r -p "Does everything look OK? [Y/N] " SQUID_CONF_OK -case ${SQUID_CONF_OK} in - [yY][eE][sS]|[yY]) - echo "Great, will continue executing script" - ;; - *) - echo "Exiting..." - exit 1 + +if [ ! "$*" == "bypass_check" ] ; then + read -r -p "Does everything look OK? [Y/N] " SQUID_CONF_OK + case ${SQUID_CONF_OK} in + [yY][eE][sS]|[yY]) + echo "Great, will continue executing script" + ;; + *) + echo "Exiting..." + exit 1 ;; -esac + esac +fi # Pattern and URL files SED_PATTERN_FILE="patterns.sed" @@ -99,17 +113,17 @@ do grep -q -E '^\[Adblock.*\]$' "${LIST_FILE_PATH}" if [ ! $? -eq 0 ] ; then - echo "An non-adblock list was detected" + echo "An non-adblock list was detected." exit 1 fi - echo "Converting ${LIST_FILE_NAME} to compatible format for SquidGuard" + echo "Converting ${LIST_FILE_NAME} to an expressions list for SquidGuard" sed -e "${ADBLOCK_PATTERNS}" "${LIST_FILE_PATH}" > "${SQUIDGUARD_ADBLOCK_DIR}/${LIST_FILE_NAME}" done show_message "Rebuilding SquidGuard Database" -squidGuard -b -d -C all +${SQUIDGUARD_BIN} -b -d -C all # Make sure permissions are good, otherwise SquidGuard will go into emergency mode # https://help.ubuntu.com/community/SquidGuard @@ -117,13 +131,15 @@ squidGuard -b -d -C all chmod 644 "${SQUIDGUARD_CONF_FILE}" chmod -R 640 "${SQUIDGUARD_DB_DIR}" chmod -R 644 "$(dirname "${SQUID_LOG_FILE}")" -chown ${SQUID_USER}:${SQUID_USER} "${SQUIDGUARD_CONF_FILE}" -chown -R ${SQUID_USER}:${SQUID_USER} "${SQUIDGUARD_DB_DIR}" +chown "${SQUID_USER}":"${SQUID_USER}" "${SQUIDGUARD_CONF_FILE}" +chown -R "${SQUID_USER}":"${SQUID_USER}" "${SQUIDGUARD_DB_DIR}" find "${SQUIDGUARD_DB_DIR}" -type d -exec chmod 755 \{\} \; -print > /dev/null 2>&1 chmod 755 "$(dirname "${SQUID_LOG_FILE}")" show_message "Reloading squid" -squid -k reconfigure +${SQUID_BIN} -k reconfigure # Remove adblock folder in tmp rm -rf ${EASYLIST_TMP_DIR} > /dev/null 2>&1 + +echo "Adblock expressions lists are now installed!"