2016-10-26 08:36:02 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
2017-02-22 17:55:20 +00:00
|
|
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
#
|
2017-05-14 01:11:44 +00:00
|
|
|
# Whitelist and blacklist domains
|
2016-10-26 08:36:02 +00:00
|
|
|
#
|
2017-02-22 17:55:20 +00:00
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
# Globals
|
2016-10-26 08:36:02 +00:00
|
|
|
basename=pihole
|
2018-02-18 20:18:52 +00:00
|
|
|
piholeDir=/etc/"${basename}"
|
2019-04-25 10:10:42 +00:00
|
|
|
gravityDBfile="${piholeDir}/gravity.db"
|
2018-08-13 16:17:14 +00:00
|
|
|
|
2016-10-26 08:36:02 +00:00
|
|
|
reload=false
|
|
|
|
addmode=true
|
|
|
|
verbose=true
|
2018-07-08 18:37:33 +00:00
|
|
|
wildcard=false
|
2019-04-25 13:01:13 +00:00
|
|
|
web=false
|
2016-10-26 08:36:02 +00:00
|
|
|
|
|
|
|
domList=()
|
|
|
|
|
2019-04-25 10:10:42 +00:00
|
|
|
listType=""
|
2019-04-25 10:30:38 +00:00
|
|
|
listname=""
|
2016-10-26 08:36:02 +00:00
|
|
|
|
2019-04-25 10:45:08 +00:00
|
|
|
colfile="/opt/pihole/COL_TABLE"
|
|
|
|
source ${colfile}
|
2017-06-21 11:49:05 +00:00
|
|
|
|
2019-11-30 12:33:16 +00:00
|
|
|
getTypeID() {
|
|
|
|
if [[ "$1" == "whitelist" ]]; then
|
|
|
|
echo "0"
|
|
|
|
elif [[ "$1" == "blacklist" ]]; then
|
|
|
|
echo "1"
|
|
|
|
elif [[ "$1" == "regex_whitelist" ]]; then
|
|
|
|
echo "2"
|
|
|
|
elif [[ "$1" == "regex_blacklist" ]]; then
|
|
|
|
echo "3"
|
|
|
|
fi
|
|
|
|
}
|
2017-06-21 11:49:05 +00:00
|
|
|
|
2016-10-26 08:36:02 +00:00
|
|
|
helpFunc() {
|
2019-04-25 10:10:42 +00:00
|
|
|
if [[ "${listType}" == "whitelist" ]]; then
|
2018-07-20 19:42:11 +00:00
|
|
|
param="w"
|
2019-04-25 10:10:42 +00:00
|
|
|
type="whitelist"
|
2019-07-22 20:26:27 +00:00
|
|
|
elif [[ "${listType}" == "regex_blacklist" && "${wildcard}" == true ]]; then
|
|
|
|
param="-wild"
|
|
|
|
type="wildcard blacklist"
|
2019-08-05 18:56:01 +00:00
|
|
|
elif [[ "${listType}" == "regex_blacklist" ]]; then
|
|
|
|
param="-regex"
|
|
|
|
type="regex blacklist filter"
|
2019-07-22 20:26:27 +00:00
|
|
|
elif [[ "${listType}" == "regex_whitelist" && "${wildcard}" == true ]]; then
|
2019-08-05 19:10:52 +00:00
|
|
|
param="-white-wild"
|
2019-07-22 20:26:27 +00:00
|
|
|
type="wildcard whitelist"
|
2019-08-05 18:56:01 +00:00
|
|
|
elif [[ "${listType}" == "regex_whitelist" ]]; then
|
2019-08-05 19:10:52 +00:00
|
|
|
param="-white-regex"
|
2019-08-05 18:56:01 +00:00
|
|
|
type="regex whitelist filter"
|
2018-07-20 19:42:11 +00:00
|
|
|
else
|
|
|
|
param="b"
|
2019-04-25 10:10:42 +00:00
|
|
|
type="blacklist"
|
2018-07-20 19:42:11 +00:00
|
|
|
fi
|
2017-05-14 01:11:44 +00:00
|
|
|
|
2017-05-16 00:18:32 +00:00
|
|
|
echo "Usage: pihole -${param} [options] <domain> <domain2 ...>
|
|
|
|
Example: 'pihole -${param} site.com', or 'pihole -${param} site1.com site2.com'
|
2019-04-25 10:10:42 +00:00
|
|
|
${type^} one or more domains
|
2017-05-14 01:11:44 +00:00
|
|
|
|
2017-05-16 00:23:53 +00:00
|
|
|
Options:
|
2019-04-25 10:10:42 +00:00
|
|
|
-d, --delmode Remove domain(s) from the ${type}
|
|
|
|
-nr, --noreload Update ${type} without reloading the DNS server
|
2017-05-14 01:11:44 +00:00
|
|
|
-q, --quiet Make output less verbose
|
|
|
|
-h, --help Show this help dialog
|
2017-10-07 15:29:47 +00:00
|
|
|
-l, --list Display all your ${type}listed domains
|
|
|
|
--nuke Removes all entries in a list"
|
2017-05-14 15:43:20 +00:00
|
|
|
|
2017-05-16 00:18:32 +00:00
|
|
|
exit 0
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:00:38 +00:00
|
|
|
EscapeRegexp() {
|
2018-07-20 19:42:11 +00:00
|
|
|
# This way we may safely insert an arbitrary
|
|
|
|
# string in our regular expressions
|
|
|
|
# This sed is intentionally executed in three steps to ease maintainability
|
|
|
|
# The first sed removes any amount of leading dots
|
2019-04-25 10:45:08 +00:00
|
|
|
echo $* | sed 's/^\.*//' | sed "s/[]\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
|
2016-12-02 11:57:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
HandleOther() {
|
2018-07-20 19:42:11 +00:00
|
|
|
# Convert to lowercase
|
|
|
|
domain="${1,,}"
|
|
|
|
|
|
|
|
# Check validity of domain (don't check for regex entries)
|
|
|
|
if [[ "${#domain}" -le 253 ]]; then
|
2019-07-22 19:03:42 +00:00
|
|
|
if [[ ( "${listType}" == "regex_blacklist" || "${listType}" == "regex_whitelist" ) && "${wildcard}" == false ]]; then
|
2018-07-20 19:42:11 +00:00
|
|
|
validDomain="${domain}"
|
|
|
|
else
|
|
|
|
validDomain=$(grep -P "^((-|_)*[a-z\\d]((-|_)*[a-z\\d])*(-|_)*)(\\.(-|_)*([a-z\\d]((-|_)*[a-z\\d])*))*$" <<< "${domain}") # Valid chars check
|
|
|
|
validDomain=$(grep -P "^[^\\.]{1,63}(\\.[^\\.]{1,63})*$" <<< "${validDomain}") # Length of each label
|
|
|
|
fi
|
|
|
|
fi
|
2017-05-14 01:11:44 +00:00
|
|
|
|
2018-07-20 19:42:11 +00:00
|
|
|
if [[ -n "${validDomain}" ]]; then
|
|
|
|
domList=("${domList[@]}" ${validDomain})
|
2018-06-29 03:21:01 +00:00
|
|
|
else
|
2018-07-20 19:42:11 +00:00
|
|
|
echo -e " ${CROSS} ${domain} is not a valid argument or domain name!"
|
2018-06-29 03:21:01 +00:00
|
|
|
fi
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 10:10:42 +00:00
|
|
|
ProcessDomainList() {
|
2019-08-05 19:03:47 +00:00
|
|
|
local is_regexlist
|
2019-07-08 19:23:46 +00:00
|
|
|
if [[ "${listType}" == "regex_blacklist" ]]; then
|
2019-07-22 17:35:16 +00:00
|
|
|
# Regex black filter list
|
2019-07-08 19:23:46 +00:00
|
|
|
listname="regex blacklist filters"
|
2019-08-05 19:03:47 +00:00
|
|
|
is_regexlist=true
|
2019-07-22 17:35:16 +00:00
|
|
|
elif [[ "${listType}" == "regex_whitelist" ]]; then
|
|
|
|
# Regex white filter list
|
|
|
|
listname="regex whitelist filters"
|
2019-08-05 19:03:47 +00:00
|
|
|
is_regexlist=true
|
2019-04-25 10:30:38 +00:00
|
|
|
else
|
|
|
|
# Whitelist / Blacklist
|
|
|
|
listname="${listType}"
|
2019-08-05 19:03:47 +00:00
|
|
|
is_regexlist=false
|
2019-04-25 10:30:38 +00:00
|
|
|
fi
|
|
|
|
|
2018-07-20 19:42:11 +00:00
|
|
|
for dom in "${domList[@]}"; do
|
2019-04-25 10:30:38 +00:00
|
|
|
# Format domain into regex filter if requested
|
|
|
|
if [[ "${wildcard}" == true ]]; then
|
|
|
|
dom="(^|\\.)${dom//\./\\.}$"
|
|
|
|
fi
|
|
|
|
|
2019-04-26 15:06:01 +00:00
|
|
|
# Logic: If addmode then add to desired list and remove from the other;
|
|
|
|
# if delmode then remove from desired list but do not add to the other
|
2018-07-20 19:42:11 +00:00
|
|
|
if ${addmode}; then
|
2019-04-25 10:10:42 +00:00
|
|
|
AddDomain "${dom}" "${listType}"
|
2019-08-05 19:03:47 +00:00
|
|
|
if ! ${is_regexlist}; then
|
2019-04-25 10:30:38 +00:00
|
|
|
RemoveDomain "${dom}" "${listAlt}"
|
|
|
|
fi
|
2018-07-20 19:42:11 +00:00
|
|
|
else
|
2019-04-25 10:10:42 +00:00
|
|
|
RemoveDomain "${dom}" "${listType}"
|
2018-07-20 19:42:11 +00:00
|
|
|
fi
|
2017-05-14 01:11:44 +00:00
|
|
|
done
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddDomain() {
|
2019-11-30 12:33:16 +00:00
|
|
|
local domain list num typeID
|
2019-04-26 15:06:01 +00:00
|
|
|
# Use printf to escape domain. %q prints the argument in a form that can be reused as shell input
|
2019-04-26 18:54:01 +00:00
|
|
|
domain="$1"
|
2018-07-20 19:42:11 +00:00
|
|
|
list="$2"
|
2019-11-30 12:33:16 +00:00
|
|
|
typeID="$(getTypeID "${list}")"
|
2018-07-20 19:42:11 +00:00
|
|
|
|
2019-04-25 10:10:42 +00:00
|
|
|
# Is the domain in the list we want to add it to?
|
2019-11-30 12:33:16 +00:00
|
|
|
num="$(sqlite3 "${gravityDBfile}" "SELECT COUNT(*) FROM domainlist WHERE domain = '${domain}' AND type = ${typeID};")"
|
2018-07-20 19:42:11 +00:00
|
|
|
|
2019-04-26 15:06:01 +00:00
|
|
|
if [[ "${num}" -ne 0 ]]; then
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
|
|
|
echo -e " ${INFO} ${1} already exists in ${listname}, no need to add!"
|
|
|
|
fi
|
|
|
|
return
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
2019-04-26 15:06:01 +00:00
|
|
|
|
|
|
|
# Domain not found in the table, add it!
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
2019-04-26 16:15:53 +00:00
|
|
|
echo -e " ${INFO} Adding ${1} to the ${listname}..."
|
2019-04-26 15:06:01 +00:00
|
|
|
fi
|
|
|
|
reload=true
|
2019-04-26 16:15:53 +00:00
|
|
|
# Insert only the domain here. The enabled and date_added fields will be filled
|
|
|
|
# with their default values (enabled = true, date_added = current timestamp)
|
2019-11-30 12:33:16 +00:00
|
|
|
sqlite3 "${gravityDBfile}" "INSERT INTO domainlist (domain,type) VALUES ('${domain}',${typeID});"
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoveDomain() {
|
2019-11-30 12:33:16 +00:00
|
|
|
local domain list num typeID
|
2019-04-26 15:06:01 +00:00
|
|
|
# Use printf to escape domain. %q prints the argument in a form that can be reused as shell input
|
2019-04-26 18:54:01 +00:00
|
|
|
domain="$1"
|
2018-07-20 19:42:11 +00:00
|
|
|
list="$2"
|
2019-11-30 12:33:16 +00:00
|
|
|
typeID="$(getTypeID "${list}")"
|
2019-04-25 10:10:42 +00:00
|
|
|
|
|
|
|
# Is the domain in the list we want to remove it from?
|
2019-11-30 12:33:16 +00:00
|
|
|
num="$(sqlite3 "${gravityDBfile}" "SELECT COUNT(*) FROM domainlist WHERE domain = '${domain}' AND type = ${typeID};")"
|
2019-04-25 10:10:42 +00:00
|
|
|
|
2019-04-26 15:06:01 +00:00
|
|
|
if [[ "${num}" -eq 0 ]]; then
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
|
|
|
echo -e " ${INFO} ${1} does not exist in ${list}, no need to remove!"
|
|
|
|
fi
|
|
|
|
return
|
2019-04-25 10:10:42 +00:00
|
|
|
fi
|
2019-04-26 15:06:01 +00:00
|
|
|
|
|
|
|
# Domain found in the table, remove it!
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
2019-04-26 16:15:53 +00:00
|
|
|
echo -e " ${INFO} Removing ${1} from the ${listname}..."
|
2019-04-26 15:06:01 +00:00
|
|
|
fi
|
|
|
|
reload=true
|
|
|
|
# Remove it from the current list
|
2019-11-30 12:33:16 +00:00
|
|
|
sqlite3 "${gravityDBfile}" "DELETE FROM domainlist WHERE domain = '${domain}' AND type = ${typeID};"
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Displaylist() {
|
2019-11-30 12:33:16 +00:00
|
|
|
local list listname count num_pipes domain enabled status nicedate typeID
|
2019-04-25 10:10:42 +00:00
|
|
|
|
2019-04-26 15:06:01 +00:00
|
|
|
listname="${listType}"
|
2019-11-30 12:33:16 +00:00
|
|
|
typeID="$(getTypeID "${listType}")"
|
|
|
|
data="$(sqlite3 "${gravityDBfile}" "SELECT domain,enabled,date_modified FROM domainlist WHERE type = ${typeID};" 2> /dev/null)"
|
2019-04-25 10:10:42 +00:00
|
|
|
|
|
|
|
if [[ -z $data ]]; then
|
2019-07-22 17:35:16 +00:00
|
|
|
echo -e "Not showing empty list"
|
2019-04-25 10:10:42 +00:00
|
|
|
else
|
|
|
|
echo -e "Displaying ${listname}:"
|
2018-07-20 19:42:11 +00:00
|
|
|
count=1
|
2019-04-25 10:10:42 +00:00
|
|
|
while IFS= read -r line
|
|
|
|
do
|
2019-04-25 10:30:38 +00:00
|
|
|
# Count number of pipes seen in this line
|
|
|
|
# This is necessary because we can only detect the pipe separating the fields
|
|
|
|
# from the end backwards as the domain (which is the first field) may contain
|
|
|
|
# pipe symbols as they are perfectly valid regex filter control characters
|
|
|
|
num_pipes="$(grep -c "^" <<< "$(grep -o "|" <<< "${line}")")"
|
|
|
|
|
2019-04-25 10:45:08 +00:00
|
|
|
# Extract domain and enabled status based on the obtained number of pipe characters
|
2019-05-02 15:11:39 +00:00
|
|
|
domain="$(cut -d'|' -f"-$((num_pipes-1))" <<< "${line}")"
|
|
|
|
enabled="$(cut -d'|' -f"$((num_pipes))" <<< "${line}")"
|
|
|
|
datemod="$(cut -d'|' -f"$((num_pipes+1))" <<< "${line}")"
|
2019-04-25 10:45:08 +00:00
|
|
|
|
|
|
|
# Translate boolean status into human readable string
|
2019-04-25 10:10:42 +00:00
|
|
|
if [[ "${enabled}" -eq 1 ]]; then
|
|
|
|
status="enabled"
|
|
|
|
else
|
|
|
|
status="disabled"
|
|
|
|
fi
|
2019-04-25 10:30:38 +00:00
|
|
|
|
2019-04-26 15:07:16 +00:00
|
|
|
# Get nice representation of numerical date stored in database
|
2019-05-02 15:11:39 +00:00
|
|
|
nicedate=$(date --rfc-2822 -d "@${datemod}")
|
2019-04-26 15:07:16 +00:00
|
|
|
|
2019-05-02 15:11:39 +00:00
|
|
|
echo " ${count}: ${domain} (${status}, last modified ${nicedate})"
|
2018-07-20 19:42:11 +00:00
|
|
|
count=$((count+1))
|
2019-04-25 10:10:42 +00:00
|
|
|
done <<< "${data}"
|
2017-06-21 11:49:05 +00:00
|
|
|
fi
|
2019-04-26 15:13:36 +00:00
|
|
|
exit 0;
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 15:29:47 +00:00
|
|
|
NukeList() {
|
2019-11-30 12:33:16 +00:00
|
|
|
local typeID
|
|
|
|
typeID=$(getTypeID "${list}")
|
|
|
|
sqlite3 "${gravityDBfile}" "DELETE FROM domainlist WHERE type = ${typeID};"
|
2017-10-07 15:29:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 08:36:02 +00:00
|
|
|
for var in "$@"; do
|
2018-07-20 19:42:11 +00:00
|
|
|
case "${var}" in
|
2019-04-25 10:10:42 +00:00
|
|
|
"-w" | "whitelist" ) listType="whitelist"; listAlt="blacklist";;
|
|
|
|
"-b" | "blacklist" ) listType="blacklist"; listAlt="whitelist";;
|
2019-07-08 19:23:46 +00:00
|
|
|
"--wild" | "wildcard" ) listType="regex_blacklist"; wildcard=true;;
|
|
|
|
"--regex" | "regex" ) listType="regex_blacklist";;
|
2019-08-05 19:10:52 +00:00
|
|
|
"--white-regex" | "white-regex" ) listType="regex_whitelist";;
|
|
|
|
"--white-wild" | "white-wild" ) listType="regex_whitelist"; wildcard=true;;
|
2018-07-20 19:42:11 +00:00
|
|
|
"-nr"| "--noreload" ) reload=false;;
|
|
|
|
"-d" | "--delmode" ) addmode=false;;
|
|
|
|
"-q" | "--quiet" ) verbose=false;;
|
|
|
|
"-h" | "--help" ) helpFunc;;
|
|
|
|
"-l" | "--list" ) Displaylist;;
|
|
|
|
"--nuke" ) NukeList;;
|
2019-04-25 13:01:13 +00:00
|
|
|
"--web" ) web=true;;
|
2018-07-20 19:42:11 +00:00
|
|
|
* ) HandleOther "${var}";;
|
|
|
|
esac
|
2016-10-26 08:36:02 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
2018-07-20 19:42:11 +00:00
|
|
|
helpFunc
|
2016-10-26 08:36:02 +00:00
|
|
|
fi
|
|
|
|
|
2019-04-25 10:10:42 +00:00
|
|
|
ProcessDomainList
|
2016-10-26 08:36:02 +00:00
|
|
|
|
2019-04-25 13:01:13 +00:00
|
|
|
# Used on web interface
|
|
|
|
if $web; then
|
|
|
|
echo "DONE"
|
|
|
|
fi
|
|
|
|
|
2017-07-24 11:26:39 +00:00
|
|
|
if [[ "${reload}" != false ]]; then
|
2019-04-25 10:10:42 +00:00
|
|
|
pihole restartdns reload
|
2016-10-26 08:36:02 +00:00
|
|
|
fi
|