2018-06-13 05:47:08 +00:00
|
|
|
#!/usr/bin/env bash
|
2018-06-13 06:29:07 +00:00
|
|
|
# shellcheck disable=SC1090
|
2018-06-13 05:47:08 +00:00
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
|
|
|
# (c) 2018 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
#
|
|
|
|
# Query Domain Lists
|
|
|
|
#
|
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
|
|
|
# Globals
|
|
|
|
piholeDir="/etc/pihole"
|
2019-05-01 14:56:16 +00:00
|
|
|
gravityDBfile="${piholeDir}/gravity.db"
|
2019-06-03 18:23:27 +00:00
|
|
|
regexlist="/etc/pihole/regex.list"
|
2018-06-13 05:47:08 +00:00
|
|
|
options="$*"
|
|
|
|
adlist=""
|
|
|
|
all=""
|
|
|
|
exact=""
|
|
|
|
blockpage=""
|
|
|
|
matchType="match"
|
|
|
|
|
|
|
|
colfile="/opt/pihole/COL_TABLE"
|
2018-06-13 06:25:43 +00:00
|
|
|
source "${colfile}"
|
2018-06-13 05:47:08 +00:00
|
|
|
|
2019-06-03 18:23:27 +00:00
|
|
|
# Scan an array of files for matching strings
|
2018-06-13 05:47:08 +00:00
|
|
|
# Scan an array of files for matching strings
|
|
|
|
scanList(){
|
2018-07-20 20:13:42 +00:00
|
|
|
# Escape full stops
|
2019-06-03 18:23:27 +00:00
|
|
|
local domain="${1}" esc_domain="${1//./\\.}" lists="${2}" type="${3:-}"
|
2018-07-20 20:13:42 +00:00
|
|
|
|
|
|
|
# Prevent grep from printing file path
|
|
|
|
cd "$piholeDir" || exit 1
|
|
|
|
|
|
|
|
# Prevent grep -i matching slowly: http://bit.ly/2xFXtUX
|
|
|
|
export LC_CTYPE=C
|
|
|
|
|
|
|
|
# /dev/null forces filename to be printed when only one list has been generated
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
case "${type}" in
|
2019-06-03 18:23:27 +00:00
|
|
|
"exact" ) grep -i -E -l "(^|(?<!#)\\s)${esc_domain}($|\\s|#)" ${lists} /dev/null 2>/dev/null;;
|
2019-06-03 23:03:37 +00:00
|
|
|
"rx" ) awk 'NR==FNR{regexps[$0]}{for (r in regexps)if($0 ~ r)print r}' <(echo "${lists}") <(echo "${domain}") 2>/dev/null;;
|
2019-06-03 18:23:27 +00:00
|
|
|
* ) grep -i "${esc_domain}" ${lists} /dev/null 2>/dev/null;;
|
2018-07-20 20:13:42 +00:00
|
|
|
esac
|
2018-06-13 05:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [[ "${options}" == "-h" ]] || [[ "${options}" == "--help" ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
echo "Usage: pihole -q [option] <domain>
|
2018-06-13 05:47:08 +00:00
|
|
|
Example: 'pihole -q -exact domain.com'
|
|
|
|
Query the adlists for a specified domain
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-adlist Print the name of the block list URL
|
|
|
|
-exact Search the block lists for exact domain matches
|
|
|
|
-all Return all query matches within a block list
|
|
|
|
-h, --help Show this help dialog"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Handle valid options
|
|
|
|
if [[ "${options}" == *"-bp"* ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
exact="exact"; blockpage=true
|
2018-06-13 05:47:08 +00:00
|
|
|
else
|
2018-07-20 20:13:42 +00:00
|
|
|
[[ "${options}" == *"-adlist"* ]] && adlist=true
|
|
|
|
[[ "${options}" == *"-all"* ]] && all=true
|
|
|
|
if [[ "${options}" == *"-exact"* ]]; then
|
|
|
|
exact="exact"; matchType="exact ${matchType}"
|
|
|
|
fi
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Strip valid options, leaving only the domain and invalid options
|
|
|
|
# This allows users to place the options before or after the domain
|
|
|
|
options=$(sed -E 's/ ?-(bp|adlists?|all|exact) ?//g' <<< "${options}")
|
|
|
|
|
|
|
|
# Handle remaining options
|
|
|
|
# If $options contain non ASCII characters, convert to punycode
|
|
|
|
case "${options}" in
|
2018-07-20 20:13:42 +00:00
|
|
|
"" ) str="No domain specified";;
|
|
|
|
*" "* ) str="Unknown query option specified";;
|
|
|
|
*[![:ascii:]]* ) domainQuery=$(idn2 "${options}");;
|
|
|
|
* ) domainQuery="${options}";;
|
2018-06-13 05:47:08 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
if [[ -n "${str:-}" ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
echo -e "${str}${COL_NC}\\nTry 'pihole -q --help' for more information."
|
|
|
|
exit 1
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
2019-05-04 10:47:25 +00:00
|
|
|
scanDatabaseTable() {
|
2019-05-30 19:44:47 +00:00
|
|
|
local domain table type querystr result
|
2019-05-04 16:25:11 +00:00
|
|
|
domain="$(printf "%q" "${1}")"
|
2019-05-04 10:47:25 +00:00
|
|
|
table="${2}"
|
|
|
|
type="${3:-}"
|
|
|
|
|
2019-05-04 11:19:50 +00:00
|
|
|
# As underscores are legitimate parts of domains, we escape them when using the LIKE operator.
|
|
|
|
# Underscores are SQLite wildcards matching exactly one character. We obviously want to suppress this
|
2019-05-04 10:47:25 +00:00
|
|
|
# behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched
|
2019-05-04 11:19:50 +00:00
|
|
|
# as a literal underscore character. We pretreat the $domain variable accordingly to escape underscores.
|
2019-05-04 10:47:25 +00:00
|
|
|
case "${type}" in
|
2019-06-03 22:59:58 +00:00
|
|
|
"exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
|
|
|
|
"retrievetable" ) querystr="SELECT domain FROM vw_${table}";;
|
|
|
|
* ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
|
2019-05-04 10:47:25 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
# Send prepared query to gravity database
|
|
|
|
result="$(sqlite3 "${gravityDBfile}" "${querystr}")" 2> /dev/null
|
2019-05-04 11:15:30 +00:00
|
|
|
if [[ -z "${result}" ]]; then
|
2019-05-04 11:19:50 +00:00
|
|
|
# Return early when there are no matches in this table
|
2019-05-04 10:47:25 +00:00
|
|
|
return
|
|
|
|
fi
|
2019-05-04 11:15:30 +00:00
|
|
|
|
2019-06-03 22:59:58 +00:00
|
|
|
# If we are only retrieving the table
|
|
|
|
# Just output and return
|
|
|
|
if [[ "${type}" == "retrievetable" ]]; then
|
|
|
|
echo "${result[*]}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2019-05-04 11:19:50 +00:00
|
|
|
# Mark domain as having been white-/blacklist matched (global variable)
|
2019-05-04 11:15:30 +00:00
|
|
|
wbMatch=true
|
2019-05-30 19:44:47 +00:00
|
|
|
|
|
|
|
# Print table name
|
2019-05-31 06:39:18 +00:00
|
|
|
echo " ${matchType^} found in ${COL_BOLD}${table^}${COL_NC}"
|
2019-05-30 19:44:47 +00:00
|
|
|
|
|
|
|
# Loop over results and print them
|
2019-05-04 11:19:50 +00:00
|
|
|
mapfile -t results <<< "${result}"
|
2019-05-04 11:15:30 +00:00
|
|
|
for result in "${results[@]}"; do
|
|
|
|
if [[ -n "${blockpage}" ]]; then
|
|
|
|
echo "π ${result}"
|
|
|
|
exit 0
|
|
|
|
fi
|
2019-05-30 19:44:47 +00:00
|
|
|
echo " ${result}"
|
2019-05-04 11:15:30 +00:00
|
|
|
done
|
2019-05-04 10:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Scan Whitelist and Blacklist
|
|
|
|
scanDatabaseTable "${domainQuery}" "whitelist" "${exact}"
|
|
|
|
scanDatabaseTable "${domainQuery}" "blacklist" "${exact}"
|
2018-06-13 05:47:08 +00:00
|
|
|
|
2019-06-03 22:59:58 +00:00
|
|
|
# Scan Regex table
|
|
|
|
regexlist=$(scanDatabaseTable "" "regex" "retrievetable")
|
|
|
|
|
|
|
|
if [[ -n "${regexlist}" ]]; then
|
2019-06-03 18:23:27 +00:00
|
|
|
# Return portion(s) of string that is found in the regex list
|
|
|
|
mapfile -t results <<< "$(scanList "${domainQuery}" "${regexlist}" "rx")"
|
|
|
|
|
2019-06-03 22:59:58 +00:00
|
|
|
# If a result is found
|
|
|
|
if [[ -n "${results[*]}" ]]; then
|
|
|
|
# Count the matches
|
|
|
|
regexCount=${#results[@]}
|
|
|
|
# Determine plural string
|
|
|
|
[[ $regexCount -gt 1 ]] && plu="es"
|
|
|
|
# Form output strings
|
|
|
|
str="${COL_BOLD}${regexCount}${COL_NC} ${matchType}${plu:-} found in ${COL_BOLD}regex${COL_NC} table"
|
2019-06-03 18:55:29 +00:00
|
|
|
result="${COL_BOLD}$(IFS=$'\n'; echo "${results[*]}")${COL_NC}"
|
2019-06-03 18:23:27 +00:00
|
|
|
|
|
|
|
if [[ -z "${blockpage}" ]]; then
|
|
|
|
wcMatch=true
|
|
|
|
echo " $str"
|
2018-07-20 20:13:42 +00:00
|
|
|
fi
|
2019-06-03 18:23:27 +00:00
|
|
|
|
|
|
|
case "${blockpage}" in
|
|
|
|
true ) echo "π ${regexlist##*/}"; exit 0;;
|
|
|
|
* ) awk '{print " "$0}' <<< "${result}";;
|
|
|
|
esac
|
|
|
|
fi
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Get version sorted *.domains filenames (without dir path)
|
|
|
|
lists=("$(cd "$piholeDir" || exit 0; printf "%s\\n" -- *.domains | sort -V)")
|
|
|
|
|
|
|
|
# Query blocklists for occurences of domain
|
|
|
|
mapfile -t results <<< "$(scanList "${domainQuery}" "${lists[*]}" "${exact}")"
|
|
|
|
|
|
|
|
# Handle notices
|
|
|
|
if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
echo -e " ${INFO} No ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC} within the block lists"
|
|
|
|
exit 0
|
2018-06-13 05:47:08 +00:00
|
|
|
elif [[ -z "${results[*]}" ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
# Result found in WL/BL/Wildcards
|
|
|
|
exit 0
|
2018-06-13 05:47:08 +00:00
|
|
|
elif [[ -z "${all}" ]] && [[ "${#results[*]}" -ge 100 ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
echo -e " ${INFO} Over 100 ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC}
|
|
|
|
This can be overridden using the -all option"
|
|
|
|
exit 0
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove unwanted content from non-exact $results
|
|
|
|
if [[ -z "${exact}" ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
# Delete lines starting with #
|
|
|
|
# Remove comments after domain
|
|
|
|
# Remove hosts format IP address
|
|
|
|
mapfile -t results <<< "$(IFS=$'\n'; sed \
|
|
|
|
-e "/:#/d" \
|
|
|
|
-e "s/[ \\t]#.*//g" \
|
|
|
|
-e "s/:.*[ \\t]/:/g" \
|
|
|
|
<<< "${results[*]}")"
|
|
|
|
# Exit if result was in a comment
|
|
|
|
[[ -z "${results[*]}" ]] && exit 0
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Get adlist file content as array
|
2019-05-01 15:06:14 +00:00
|
|
|
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
2019-05-01 14:56:16 +00:00
|
|
|
# Retrieve source URLs from gravity database
|
|
|
|
mapfile -t adlists <<< "$(sqlite3 "${gravityDBfile}" "SELECT address FROM vw_adlists;" 2> /dev/null)"
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Print "Exact matches for" title
|
|
|
|
if [[ -n "${exact}" ]] && [[ -z "${blockpage}" ]]; then
|
2018-07-20 20:13:42 +00:00
|
|
|
plural=""; [[ "${#results[*]}" -gt 1 ]] && plural="es"
|
|
|
|
echo " ${matchType^}${plural} for ${COL_BOLD}${domainQuery}${COL_NC} found in:"
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
for result in "${results[@]}"; do
|
2018-07-20 20:13:42 +00:00
|
|
|
fileName="${result/:*/}"
|
2018-06-13 05:47:08 +00:00
|
|
|
|
2018-07-20 20:13:42 +00:00
|
|
|
# Determine *.domains URL using filename's number
|
|
|
|
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
|
|
|
fileNum="${fileName/list./}"; fileNum="${fileNum%%.*}"
|
|
|
|
fileName="${adlists[$fileNum]}"
|
2018-06-13 05:47:08 +00:00
|
|
|
|
2018-07-20 20:13:42 +00:00
|
|
|
# Discrepency occurs when adlists has been modified, but Gravity has not been run
|
|
|
|
if [[ -z "${fileName}" ]]; then
|
|
|
|
fileName="${COL_LIGHT_RED}(no associated adlists URL found)${COL_NC}"
|
|
|
|
fi
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
|
2018-07-20 20:13:42 +00:00
|
|
|
if [[ -n "${blockpage}" ]]; then
|
|
|
|
echo "${fileNum} ${fileName}"
|
|
|
|
elif [[ -n "${exact}" ]]; then
|
|
|
|
echo " ${fileName}"
|
2018-06-13 05:47:08 +00:00
|
|
|
else
|
2018-07-20 20:13:42 +00:00
|
|
|
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
|
|
|
count=""
|
|
|
|
echo " ${matchType^} found in ${COL_BOLD}${fileName}${COL_NC}:"
|
|
|
|
fileName_prev="${fileName}"
|
|
|
|
fi
|
|
|
|
: $((count++))
|
|
|
|
|
|
|
|
# Print matching domain if $max_count has not been reached
|
|
|
|
[[ -z "${all}" ]] && max_count="50"
|
|
|
|
if [[ -z "${all}" ]] && [[ "${count}" -ge "${max_count}" ]]; then
|
|
|
|
[[ "${count}" -gt "${max_count}" ]] && continue
|
|
|
|
echo " ${COL_GRAY}Over ${count} results found, skipping rest of file${COL_NC}"
|
|
|
|
else
|
|
|
|
echo " ${result#*:}"
|
|
|
|
fi
|
2018-06-13 05:47:08 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
exit 0
|