You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pi-hole/pihole

348 lines
9.9 KiB

8 years ago
#!/bin/bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
8 years ago
# Controller for all pihole scripts and functions.
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
8 years ago
# Must be root to use this tool
if [[ ! $EUID -eq 0 ]];then
8 years ago
if [ -x "$(command -v sudo)" ];then
exec sudo bash "$0" "$@"
exit $?
else
echo "::: sudo is needed to run pihole commands. Please run this script as root or install sudo."
exit 1
fi
8 years ago
fi
8 years ago
webpageFunc() {
source /opt/pihole/webpage.sh
main "$@"
8 years ago
exit 0
}
whitelistFunc() {
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
8 years ago
exit 0
8 years ago
}
blacklistFunc() {
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
8 years ago
exit 0
8 years ago
}
wildcardFunc() {
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
exit 0
}
debugFunc() {
local automated
local web
# Pull off the `debug` leaving passed call augmentation flags in $1
shift
if [[ "$@" == *"-a"* ]]; then
automated="true"
fi
if [[ "$@" == *"-w"* ]]; then
web="true"
fi
AUTOMATED=${automated:-} WEBCALL=${web:-} "${PI_HOLE_SCRIPT_DIR}"/piholeDebug.sh
8 years ago
exit 0
8 years ago
}
flushFunc() {
"${PI_HOLE_SCRIPT_DIR}"/piholeLogFlush.sh
8 years ago
exit 0
8 years ago
}
updatePiholeFunc() {
"${PI_HOLE_SCRIPT_DIR}"/update.sh
8 years ago
exit 0
8 years ago
}
reconfigurePiholeFunc() {
8 years ago
/etc/.pihole/automated\ install/basic-install.sh --reconfigure
exit 0;
}
updateGravityFunc() {
"${PI_HOLE_SCRIPT_DIR}"/gravity.sh "$@"
8 years ago
exit 0
8 years ago
}
scanList(){
domain="${1}"
list="${2}"
method="${3}"
if [[ ${method} == "-exact" ]]; then
grep -i -E -l "(^|\s|\/)${domain}($|\s|\/)" ${list}
else
grep -i "${domain}" ${list}
fi
}
queryFunc() {
method="${3}"
# If domain contains non ASCII characters, convert domain to punycode if python exists
# Cr: https://serverfault.com/a/335079
if [ -z "${2}" ]; then
echo "::: No domain specified"
exit 1
elif [[ ${2} = *[![:ascii:]]* ]]; then
[ `which python` ] && domain=$(python -c 'import sys;print sys.argv[1].decode("utf-8").encode("idna")' "${2}")
else
domain="${2}"
fi
# Scan Whitelist, Blacklist and Wildcards
lists="/etc/pihole/whitelist.txt /etc/pihole/blacklist.txt $wildcardlist"
result=$(scanList ${domain} "${lists}" ${method})
if [ -n "$result" ]; then
echo "$result"
[[ ! -t 1 ]] && exit 0
fi
# Scan Domains lists
result=$(scanList ${domain} "/etc/pihole/*.domains" ${method})
if [ -n "$result" ]; then
sort -t . -k 2 -g <<< "$result"
else
[ -n "$method" ] && exact="exact "
echo "::: No ${exact}results found for ${domain}"
fi
8 years ago
exit 0
8 years ago
}
chronometerFunc() {
8 years ago
shift
"${PI_HOLE_SCRIPT_DIR}"/chronometer.sh "$@"
8 years ago
exit 0
8 years ago
}
uninstallFunc() {
"${PI_HOLE_SCRIPT_DIR}"/uninstall.sh
8 years ago
exit 0
8 years ago
}
versionFunc() {
8 years ago
shift
"${PI_HOLE_SCRIPT_DIR}"/version.sh "$@"
8 years ago
exit 0
8 years ago
}
restartDNS() {
dnsmasqPid=$(pidof dnsmasq)
if [[ ${dnsmasqPid} ]]; then
# service already running - reload config
if [ -x "$(command -v systemctl)" ]; then
systemctl restart dnsmasq
else
service dnsmasq restart
fi
else
# service not running, start it up
if [ -x "$(command -v systemctl)" ]; then
systemctl start dnsmasq
else
service dnsmasq start
fi
fi
}
piholeEnable() {
if [[ "${1}" == "0" ]] ; then
#Disable Pihole
sed -i 's/^addn-hosts=\/etc\/pihole\/gravity.list/#addn-hosts=\/etc\/pihole\/gravity.list/' /etc/dnsmasq.d/01-pihole.conf
echo "::: Blocking has been disabled!"
if [[ $# > 1 ]] ; then
if [[ ${2} == *"s"* ]] ; then
tt=${2%"s"}
echo "::: Blocking will be re-enabled in ${tt} seconds"
nohup bash -c "sleep ${tt}; pihole enable" </dev/null &>/dev/null &
elif [[ ${2} == *"m"* ]] ; then
tt=${2%"m"}
echo "::: Blocking will be re-enabled in ${tt} minutes"
tt=$((${tt}*60))
nohup bash -c "sleep ${tt}; pihole enable" </dev/null &>/dev/null &
else
echo "::: Unknown format for delayed reactivation of the blocking!"
echo "::: Example:"
echo "::: pihole disable 5s - will disable blocking for 5 seconds"
echo "::: pihole disable 7m - will disable blocking for 7 minutes"
8 years ago
echo "::: Blocking will not automatically be re-enabled!"
fi
fi
else
#Enable pihole
echo "::: Blocking has been enabled!"
sed -i 's/^#addn-hosts/addn-hosts/' /etc/dnsmasq.d/01-pihole.conf
fi
restartDNS
}
piholeLogging() {
shift
if [[ "${1}" == "off" ]] ; then
#Disable Logging
sed -i 's/^log-queries/#log-queries/' /etc/dnsmasq.d/01-pihole.conf
sed -i 's/^QUERY_LOGGING=true/QUERY_LOGGING=false/' /etc/pihole/setupVars.conf
pihole -f
echo "::: Logging has been disabled!"
elif [[ "${1}" == "on" ]] ; then
#Enable logging
sed -i 's/^#log-queries/log-queries/' /etc/dnsmasq.d/01-pihole.conf
sed -i 's/^QUERY_LOGGING=false/QUERY_LOGGING=true/' /etc/pihole/setupVars.conf
echo "::: Logging has been enabled!"
else
echo "::: Invalid option passed, please pass 'on' or 'off'"
exit 1
fi
restartDNS
}
piholeStatus() {
if [[ $(netstat -plnt | grep -c ':53 ') > 0 ]]; then
if [[ "${1}" != "web" ]] ; then
echo "::: DNS service is running"
fi
else
if [[ "${1}" == "web" ]] ; then
echo "-1";
else
echo "::: DNS service is NOT running"
fi
return
fi
if [[ $(grep -i "^#addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf) ]] ; then
#list is commented out
if [[ "${1}" == "web" ]] ; then
echo 0;
else
8 years ago
echo "::: Pi-hole blocking is Disabled";
fi
elif [[ $(grep -i "^addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf) ]] ; then
8 years ago
#list set
if [[ "${1}" == "web" ]] ; then
echo 1;
else
8 years ago
echo "::: Pi-hole blocking is Enabled";
fi
else
#addn-host not found
if [[ "${1}" == "web" ]] ; then
8 years ago
echo 99
else
8 years ago
echo "::: No hosts file linked to dnsmasq, adding it in enabled state"
8 years ago
fi
#add addn-host= to dnsmasq
echo "addn-hosts=/etc/pihole/gravity.list" >> /etc/dnsmasq.d/01-pihole.conf
restartDNS
fi
}
7 years ago
tailFunc() {
echo "Press Ctrl-C to exit"
7 years ago
tail -F /var/log/pihole.log
exit 0
}
piholeCheckoutFunc() {
source "${PI_HOLE_SCRIPT_DIR}"/piholeCheckout.sh
shift
checkout "$@"
}
tricorderFunc() {
if command -v openssl &> /dev/null; then
openssl s_client -quiet -connect tricorder.pi-hole.net:9998 2> /dev/null < /dev/stdin
else
nc tricorder.pi-hole.net 9999 < /dev/stdin
fi
}
helpFunc() {
cat << EOM
::: Control all Pi-hole specific functions
:::
::: Usage: pihole [options]
::: Add -h after -w (whitelist), -b (blacklist), -c (chronometer), or -a (admin) for more information on usage
:::
::: Options:
::: -w, whitelist Whitelist domain(s)
::: -b, blacklist Blacklist domain(s) (exact match)
::: -wild, wildcard Blacklist whole domain(s) (wildcard)
::: -d, debug Start a debugging session
::: Automated debugging can be enabled with '-a'.
::: 'pihole -d -a'
::: -f, flush Flush the 'pihole.log' file
::: -t, tail Output the last lines of the 'pihole.log' file. Lines are appended as the file grows
::: -up, updatePihole Update Pi-hole components
7 years ago
::: -r, reconfigure Reconfigure or Repair Pi-hole
::: -g, updateGravity Update the list of ad-serving domains
::: -c, chronometer Calculates stats and displays to an LCD
::: -h, help Show this help dialog
::: -v, version Show installed versions of Pi-hole and Web-Admin
::: -q, query Query the adlists for a specific domain
::: 'pihole -q domain -exact' shows exact matches only
::: -l, logging Enable or Disable logging (pass 'on' or 'off')
::: -a, admin Admin webpage options
::: uninstall Uninstall Pi-hole from your system! :(
::: status Display if Pi-hole is Enabled or Disabled
::: enable Enable Pi-hole DNS Blocking
::: disable Disable Pi-hole DNS Blocking
::: Blocking can also be disabled only temporarily, e.g.,
::: 'pihole disable 5m' - will disable blocking for 5 minutes
::: restartdns Restart dnsmasq
::: checkout Check out different branches
::: tricorder Upload log to Pi-hole's medical tricorder (uses SSL when possible)
EOM
exit 0
8 years ago
}
if [[ $# = 0 ]]; then
8 years ago
helpFunc
8 years ago
fi
# Handle redirecting to specific functions based on arguments
case "${1}" in
8 years ago
"-w" | "whitelist" ) whitelistFunc "$@";;
"-b" | "blacklist" ) blacklistFunc "$@";;
"-wild" | "wildcard" ) wildcardFunc "$@";;
"-d" | "debug" ) debugFunc "$@";;
8 years ago
"-f" | "flush" ) flushFunc;;
"-up" | "updatePihole" ) updatePiholeFunc;;
"-r" | "reconfigure" ) reconfigurePiholeFunc;;
"-g" | "updateGravity" ) updateGravityFunc "$@";;
"-c" | "chronometer" ) chronometerFunc "$@";;
"-h" | "help" ) helpFunc;;
"-v" | "version" ) versionFunc "$@";;
"-q" | "query" ) queryFunc "$@";;
"-l" | "logging" ) piholeLogging "$@";;
"uninstall" ) uninstallFunc;;
"enable" ) piholeEnable 1;;
"disable" ) piholeEnable 0 $2;;
8 years ago
"status" ) piholeStatus "$2";;
"restartdns" ) restartDNS;;
"-a" | "admin" ) webpageFunc "$@";;
7 years ago
"-t" | "tail" ) tailFunc;;
"checkout" ) piholeCheckoutFunc "$@";;
"tricorder" ) tricorderFunc;;
8 years ago
* ) helpFunc;;
esac